8 #include <botan/fpe_fe1.h>
9 #include <botan/numthry.h>
10 #include <botan/hmac.h>
11 #include <botan/sha2_32.h>
20 const size_t MAX_N_BYTES = 128/8;
37 a <<= (n_low_zero / 2);
38 b <<= n_low_zero - (n_low_zero / 2);
59 throw Exception(
"Could not factor n for use in FPE");
83 const std::vector<uint8_t>& tweak);
88 std::unique_ptr<MessageAuthenticationCode>
m_mac;
94 const std::vector<uint8_t>& tweak)
96 m_mac.reset(
new HMAC(
new SHA_256));
101 if(n_bin.size() > MAX_N_BYTES)
102 throw Exception(
"N is too large for FPE encryption");
104 m_mac->update_be(static_cast<uint32_t>(n_bin.size()));
105 m_mac->update(n_bin.data(), n_bin.size());
107 m_mac->update_be(static_cast<uint32_t>(tweak.size()));
108 m_mac->update(tweak.data(), tweak.size());
113 BigInt FPE_Encryptor::operator()(
size_t round_no,
const BigInt& R)
118 m_mac->update_be(static_cast<uint32_t>(round_no));
120 m_mac->update_be(static_cast<uint32_t>(r_bin.size()));
121 m_mac->update(r_bin.data(), r_bin.size());
123 secure_vector<uint8_t> X =
m_mac->final();
124 return BigInt(X.data(), X.size());
134 const std::vector<uint8_t>& tweak)
136 FPE_Encryptor F(key, n, tweak);
141 const size_t r = rounds(a, b);
145 for(
size_t i = 0; i != r; ++i)
150 BigInt W = (L + F(i, R)) % a;
162 const std::vector<uint8_t>& tweak)
164 FPE_Encryptor F(key, n, tweak);
169 const size_t r = rounds(a, b);
173 for(
size_t i = 0; i != r; ++i)
178 BigInt L = (W - F(r-i-1, R)) % a;
const size_t PRIME_TABLE_SIZE
const uint16_t BOTAN_DLL PRIMES[]
std::unique_ptr< MessageAuthenticationCode > m_mac
static secure_vector< uint8_t > encode_locked(const BigInt &n, Base base=Binary)
BigInt fe1_decrypt(const BigInt &n, const BigInt &X0, const SymmetricKey &key, const std::vector< uint8_t > &tweak)
size_t low_zero_bits(const BigInt &n)
std::vector< T > unlock(const secure_vector< T > &in)
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
BigInt fe1_encrypt(const BigInt &n, const BigInt &X0, const SymmetricKey &key, const std::vector< uint8_t > &tweak)
std::vector< uint8_t > m_mac_n_t