8#include <botan/fpe_fe1.h>
10#include <botan/exceptn.h>
12#include <botan/numthry.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/divide.h>
15#include <botan/internal/fmt.h>
16#include <botan/internal/loadstor.h>
23const size_t MAX_N_BYTES = 128 / 8;
47 a <<= (n_low_zero / 2);
48 b <<= n_low_zero - (n_low_zero / 2);
52 while(n %
PRIMES[i] == 0) {
66 if(a <= 1 || b <= 1) {
67 throw Invalid_Argument(
"FPE_FE1 modulus must be composite with at least one prime factor under 65521");
74 m_n(n), m_rounds(rounds) {
81 m_n_bytes = m_n.serialize();
83 if(m_n_bytes.size() > MAX_N_BYTES) {
87 factor(m_n, m_a, m_b);
109 return fmt(
"FPE_FE1({},{})", m_mac->name(), m_rounds);
113 return m_mac->key_spec();
117 return m_mac->has_keying_material();
120void FPE_FE1::key_schedule(std::span<const uint8_t> key) {
124BigInt FPE_FE1::F(
const BigInt& R,
130 m_mac->update(tweak_mac);
131 m_mac->update_be(
static_cast<uint32_t
>(round));
133 m_mac->update_be(
static_cast<uint32_t
>(tmp.size()));
134 m_mac->update(tmp.data(), tmp.size());
136 tmp = m_mac->final();
141 m_mac->update_be(
static_cast<uint32_t
>(m_n_bytes.size()));
142 m_mac->update(m_n_bytes.data(), m_n_bytes.size());
144 m_mac->update_be(
static_cast<uint32_t
>(tweak_len));
146 m_mac->update(tweak, tweak_len);
149 return m_mac->final();
165 for(
size_t i = 0; i != m_rounds; ++i) {
167 Fi = F(R, i, tweak_mac, tmp);
187 for(
size_t i = 0; i != m_rounds; ++i) {
190 Fi = F(R, m_rounds - i - 1, tweak_mac, tmp);
201 return encrypt(x, tweak8,
sizeof(tweak8));
207 return decrypt(x, tweak8,
sizeof(tweak8));
213 FPE_FE1 fpe(n, 3,
true,
"HMAC(SHA-256)");
219 FPE_FE1 fpe(n, 3,
true,
"HMAC(SHA-256)");
#define BOTAN_ARG_CHECK(expr, msg)
const word * data() const
static BigInt from_bytes(std::span< const uint8_t > bytes)
static BigInt from_word(word n)
BigInt encrypt(const BigInt &x, const uint8_t tweak[], size_t tweak_len) const
std::string name() const override
BOTAN_FUTURE_EXPLICIT FPE_FE1(const BigInt &n, size_t rounds=5, bool compat_mode=false, std::string_view mac_algo="HMAC(SHA-256)")
bool has_keying_material() const override
Key_Length_Specification key_spec() const override
BigInt decrypt(const BigInt &x, const uint8_t tweak[], size_t tweak_len) const
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
void set_key(const OctetString &key)
constexpr void unpoison(const T *p, size_t n)
constexpr void poison(const T *p, size_t n)
BigInt fe1_decrypt(const BigInt &n, const BigInt &X, const SymmetricKey &key, const std::vector< uint8_t > &tweak)
BigInt fe1_encrypt(const BigInt &n, const BigInt &X, const SymmetricKey &key, const std::vector< uint8_t > &tweak)
std::string fmt(std::string_view format, const T &... args)
size_t low_zero_bits(const BigInt &n)
const size_t PRIME_TABLE_SIZE
BigInt ct_modulo(const BigInt &x, const BigInt &y)
void ct_divide(const BigInt &x, const BigInt &y, BigInt &q_out, BigInt &r_out)
std::vector< T, secure_allocator< T > > secure_vector
constexpr auto store_be(ParamTs &&... params)