Botan 3.13.0
Crypto and TLS for C&
zfec.h
Go to the documentation of this file.
1/*
2 * Forward error correction based on Vandermonde matrices
3 *
4 * (C) 1997-1998 Luigi Rizzo (luigi@iet.unipi.it)
5 * (C) 2009,2017,2021 Jack Lloyd
6 *
7 * Distributed under the terms given in license.txt
8 */
9
10#ifndef BOTAN_ZFEC_H_
11#define BOTAN_ZFEC_H_
12
13#include <botan/types.h>
14#include <functional>
15#include <map>
16#include <string>
17#include <vector>
18
19namespace Botan {
20
21/**
22* A forward error correction code compatible with the zfec
23* library (https://github.com/tahoe-lafs/zfec)
24*
25* This algorithm is *not constant time* and is likely susceptible to
26* side channels. Do not use this class to encode information that
27* should be kept secret. (If nothing else, because the first K shares
28* are simply the original input!)
29*/
30class BOTAN_PUBLIC_API(3, 0) ZFEC final {
31 public:
32 /**
33 * Callback invoked with each produced share
34 *
35 * Receives the share index, a pointer to the share contents, and the
36 * length of the share in bytes.
37 */
38 typedef std::function<void(size_t, const uint8_t[], size_t)> output_cb_t;
39
40 /**
41 * FEC constructor
42 * @param K the number of shares needed for recovery
43 * @param N the number of shares generated
44 */
45 ZFEC(size_t K, size_t N);
46
47 /**
48 * Return how many shares are needed for recovery
49 * @return the value of K
50 */
51 size_t recovery_threshold() const { return m_K; }
52
53 /**
54 * Return how many shares are generated
55 * @return the value of N
56 */
57 size_t generated_shares() const { return m_N; }
58
59 /**
60 * Return the name of the provider implementing this object
61 * @return the provider name
62 */
63 std::string provider() const;
64
65 /**
66 * Encode the input into N shares
67 * @param input the data to FEC
68 * @param size the length in bytes of input
69 * @param output_cb the output callback
70 */
71 void encode(const uint8_t input[], size_t size, const output_cb_t& output_cb) const;
72
73 /**
74 * Encode K existing shares into N shares
75 * @param shares exactly K shares of data to FEC
76 * @param share_size the length in bytes of each share
77 * @param output_cb the output callback
78 */
79 void encode_shares(const std::vector<const uint8_t*>& shares,
80 size_t share_size,
81 const output_cb_t& output_cb) const;
82
83 /**
84 * Recover the original data from K shares
85 * @param shares map of share id to share contents
86 * @param share_size size in bytes of each share
87 * @param output_cb the output callback
88 */
89 void decode_shares(const std::map<size_t, const uint8_t*>& shares,
90 size_t share_size,
91 const output_cb_t& output_cb) const;
92
93 private:
94 static void addmul(uint8_t z[], const uint8_t x[], uint8_t y, size_t size);
95
96 static void linear_combination(uint8_t z[], const uint8_t* const x[], const uint8_t y[], size_t k, size_t size);
97
98#if defined(BOTAN_HAS_ZFEC_VPERM)
99 static size_t linear_combination_vperm(
100 uint8_t z[], const uint8_t* const x[], const uint8_t y[], size_t k, size_t size);
101#endif
102
103#if defined(BOTAN_HAS_ZFEC_GFNI)
104 static void linear_combination_gfni(
105 uint8_t z[], const uint8_t* const x[], const uint8_t y[], size_t k, size_t size);
106#endif
107
108 const size_t m_K, m_N;
109 std::vector<uint8_t> m_enc_matrix;
110};
111
112} // namespace Botan
113
114#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
ZFEC(size_t K, size_t N)
Definition zfec.cpp:359
std::function< void(size_t, const uint8_t[], size_t)> output_cb_t
Definition zfec.h:38
size_t recovery_threshold() const
Definition zfec.h:51
size_t generated_shares() const
Definition zfec.h:57