Botan 3.0.0
Crypto and TLS for C&
Public Member Functions | Protected Member Functions | List of all members
Botan::FPE_FE1 Class Referencefinal

#include <fpe_fe1.h>

Inheritance diagram for Botan::FPE_FE1:
Botan::SymmetricAlgorithm

Public Member Functions

void clear () override
 
BigInt decrypt (const BigInt &x, const uint8_t tweak[], size_t tweak_len) const
 
BigInt decrypt (const BigInt &x, uint64_t tweak) const
 
BigInt encrypt (const BigInt &x, const uint8_t tweak[], size_t tweak_len) const
 
BigInt encrypt (const BigInt &x, uint64_t tweak) const
 
 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
 
size_t maximum_keylength () const
 
size_t minimum_keylength () const
 
std::string name () const override
 
void set_key (const SymmetricKey &key)
 
void set_key (const uint8_t key[], size_t length)
 
void set_key (std::span< const uint8_t > key)
 
bool valid_keylength (size_t length) const
 
 ~FPE_FE1 ()
 

Protected Member Functions

void assert_key_material_set () const
 
void assert_key_material_set (bool predicate) const
 

Detailed Description

Format Preserving Encryption using the scheme FE1 from the paper "Format-Preserving Encryption" by Bellare, Rogaway, et al (https://eprint.iacr.org/2009/251)

Definition at line 24 of file fpe_fe1.h.

Constructor & Destructor Documentation

◆ FPE_FE1()

Botan::FPE_FE1::FPE_FE1 ( const BigInt n,
size_t  rounds = 5,
bool  compat_mode = false,
std::string_view  mac_algo = "HMAC(SHA-256)" 
)
Parameters
nthe modulus. All plaintext and ciphertext values must be less than this.
roundsthe number of rounds to use. Must be at least 3.
compat_modeAn error in versions before 2.5.0 chose incorrect values for a and b. Set compat_mode to true to select this version.
mac_algothe PRF to use as the encryption function

Definition at line 60 of file fpe_fe1.cpp.

63 :
64 m_rounds(rounds)
65 {
66 if(m_rounds < 3)
67 throw Invalid_Argument("FPE_FE1 rounds too small");
68
70
71 m_n_bytes = BigInt::encode(n);
72
73 if(m_n_bytes.size() > MAX_N_BYTES)
74 throw Invalid_Argument("N is too large for FPE encryption");
75
76 factor(n, m_a, m_b);
77
78 if(compat_mode)
79 {
80 if(m_a < m_b)
81 std::swap(m_a, m_b);
82 }
83 else
84 {
85 if(m_a > m_b)
86 std::swap(m_a, m_b);
87 }
88
89 mod_a = std::make_unique<Modular_Reducer>(m_a);
90 }
static std::vector< uint8_t > encode(const BigInt &n)
Definition: bigint.h:780
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition: mac.cpp:134

References Botan::MessageAuthenticationCode::create_or_throw(), and Botan::BigInt::encode().

◆ ~FPE_FE1()

Botan::FPE_FE1::~FPE_FE1 ( )
default

Member Function Documentation

◆ assert_key_material_set() [1/2]

void Botan::SymmetricAlgorithm::assert_key_material_set ( ) const
inlineprotectedinherited

Definition at line 182 of file sym_algo.h.

183 {
185 }
virtual bool has_keying_material() const =0
void assert_key_material_set() const
Definition: sym_algo.h:182

Referenced by Botan::Salsa20::cipher_bytes(), Botan::AES_128::decrypt_n(), Botan::AES_192::decrypt_n(), Botan::AES_256::decrypt_n(), Botan::ARIA_128::decrypt_n(), Botan::ARIA_192::decrypt_n(), Botan::ARIA_256::decrypt_n(), Botan::Blowfish::decrypt_n(), Botan::Camellia_128::decrypt_n(), Botan::Camellia_192::decrypt_n(), Botan::Camellia_256::decrypt_n(), Botan::CAST_128::decrypt_n(), Botan::DES::decrypt_n(), Botan::TripleDES::decrypt_n(), Botan::GOST_28147_89::decrypt_n(), Botan::IDEA::decrypt_n(), Botan::Lion::decrypt_n(), Botan::Noekeon::decrypt_n(), Botan::SEED::decrypt_n(), Botan::Serpent::decrypt_n(), Botan::SHACAL2::decrypt_n(), Botan::SM4::decrypt_n(), Botan::Threefish_512::decrypt_n(), Botan::Twofish::decrypt_n(), Botan::AES_128::encrypt_n(), Botan::AES_192::encrypt_n(), Botan::AES_256::encrypt_n(), Botan::ARIA_128::encrypt_n(), Botan::ARIA_192::encrypt_n(), Botan::ARIA_256::encrypt_n(), Botan::Blowfish::encrypt_n(), Botan::Camellia_128::encrypt_n(), Botan::Camellia_192::encrypt_n(), Botan::Camellia_256::encrypt_n(), Botan::CAST_128::encrypt_n(), Botan::DES::encrypt_n(), Botan::TripleDES::encrypt_n(), Botan::GOST_28147_89::encrypt_n(), Botan::IDEA::encrypt_n(), Botan::Lion::encrypt_n(), Botan::Noekeon::encrypt_n(), Botan::SEED::encrypt_n(), Botan::Serpent::encrypt_n(), Botan::SHACAL2::encrypt_n(), Botan::SM4::encrypt_n(), Botan::Threefish_512::encrypt_n(), Botan::Twofish::encrypt_n(), Botan::GHASH::final(), Botan::GHASH::ghash_update(), Botan::ChaCha::seek(), Botan::CTR_BE::seek(), Botan::Salsa20::seek(), Botan::OCB_Mode::set_associated_data_n(), Botan::Salsa20::set_iv_bytes(), Botan::GHASH::update(), and Botan::GHASH::update_associated_data().

◆ assert_key_material_set() [2/2]

void Botan::SymmetricAlgorithm::assert_key_material_set ( bool  predicate) const
inlineprotectedinherited

Definition at line 187 of file sym_algo.h.

188 {
189 if(!predicate)
190 throw_key_not_set_error();
191 }

◆ clear()

void Botan::FPE_FE1::clear ( )
overridevirtual

Reset the internal state. This includes not just the key, but any partial message that may have been in process.

Implements Botan::SymmetricAlgorithm.

Definition at line 94 of file fpe_fe1.cpp.

95 {
96 m_mac->clear();
97 }

◆ decrypt() [1/2]

BigInt Botan::FPE_FE1::decrypt ( const BigInt x,
const uint8_t  tweak[],
size_t  tweak_len 
) const

Decrypt X from and onto the group Z_n using key and tweak

Parameters
xthe ciphertext to encrypt <= n
tweakmust match the value used to encrypt
tweak_lenlength of tweak

Definition at line 166 of file fpe_fe1.cpp.

167 {
168 const secure_vector<uint8_t> tweak_mac = compute_tweak_mac(tweak, tweak_len);
169
170 BigInt X = input;
171 secure_vector<uint8_t> tmp;
172
173 BigInt W, R, Fi;
174 for(size_t i = 0; i != m_rounds; ++i)
175 {
176 ct_divide(X, m_a, R, W);
177
178 Fi = F(R, m_rounds-i-1, tweak_mac, tmp);
179 X = m_b * mod_a->reduce(W - Fi) + R;
180 }
181
182 return X;
183 }
FE_25519 X
Definition: ge.cpp:26
void ct_divide(const BigInt &x, const BigInt &y, BigInt &q_out, BigInt &r_out)
Definition: divide.cpp:51

References Botan::ct_divide(), and X.

Referenced by decrypt(), and Botan::FPE::fe1_decrypt().

◆ decrypt() [2/2]

BigInt Botan::FPE_FE1::decrypt ( const BigInt x,
uint64_t  tweak 
) const

Definition at line 192 of file fpe_fe1.cpp.

193 {
194 uint8_t tweak8[8];
195 store_be(tweak, tweak8);
196 return decrypt(x, tweak8, sizeof(tweak8));
197 }
BigInt decrypt(const BigInt &x, const uint8_t tweak[], size_t tweak_len) const
Definition: fpe_fe1.cpp:166
constexpr void store_be(uint16_t in, uint8_t out[2])
Definition: loadstor.h:449

References decrypt(), and Botan::store_be().

◆ encrypt() [1/2]

BigInt Botan::FPE_FE1::encrypt ( const BigInt x,
const uint8_t  tweak[],
size_t  tweak_len 
) const

Encrypt X from and onto the group Z_n using key and tweak

Parameters
xthe plaintext to encrypt <= n
tweakwill modify the ciphertext
tweak_lenlength of tweak

Definition at line 147 of file fpe_fe1.cpp.

148 {
149 const secure_vector<uint8_t> tweak_mac = compute_tweak_mac(tweak, tweak_len);
150
151 BigInt X = input;
152
153 secure_vector<uint8_t> tmp;
154
155 BigInt L, R, Fi;
156 for(size_t i = 0; i != m_rounds; ++i)
157 {
158 ct_divide(X, m_b, L, R);
159 Fi = F(R, i, tweak_mac, tmp);
160 X = m_a * R + mod_a->reduce(L + Fi);
161 }
162
163 return X;
164 }

References Botan::ct_divide(), and X.

Referenced by encrypt(), and Botan::FPE::fe1_encrypt().

◆ encrypt() [2/2]

BigInt Botan::FPE_FE1::encrypt ( const BigInt x,
uint64_t  tweak 
) const

Definition at line 185 of file fpe_fe1.cpp.

186 {
187 uint8_t tweak8[8];
188 store_be(tweak, tweak8);
189 return encrypt(x, tweak8, sizeof(tweak8));
190 }
BigInt encrypt(const BigInt &x, const uint8_t tweak[], size_t tweak_len) const
Definition: fpe_fe1.cpp:147

References encrypt(), and Botan::store_be().

◆ has_keying_material()

bool Botan::FPE_FE1::has_keying_material ( ) const
overridevirtual
Returns
true if a key has been set on this object

Implements Botan::SymmetricAlgorithm.

Definition at line 109 of file fpe_fe1.cpp.

110 {
111 return m_mac->has_keying_material();
112 }

◆ key_spec()

Key_Length_Specification Botan::FPE_FE1::key_spec ( ) const
overridevirtual
Returns
object describing limits on key size

Implements Botan::SymmetricAlgorithm.

Definition at line 104 of file fpe_fe1.cpp.

105 {
106 return m_mac->key_spec();
107 }

◆ maximum_keylength()

size_t Botan::SymmetricAlgorithm::maximum_keylength ( ) const
inlineinherited
Returns
maximum allowed key length

Definition at line 123 of file sym_algo.h.

124 {
125 return key_spec().maximum_keylength();
126 }
size_t maximum_keylength() const
Definition: sym_algo.h:72
virtual Key_Length_Specification key_spec() const =0

◆ minimum_keylength()

size_t Botan::SymmetricAlgorithm::minimum_keylength ( ) const
inlineinherited
Returns
minimum allowed key length

Definition at line 131 of file sym_algo.h.

132 {
133 return key_spec().minimum_keylength();
134 }
size_t minimum_keylength() const
Definition: sym_algo.h:64

◆ name()

std::string Botan::FPE_FE1::name ( ) const
overridevirtual
Returns
the algorithm name

Implements Botan::SymmetricAlgorithm.

Definition at line 99 of file fpe_fe1.cpp.

100 {
101 return fmt("FPE_FE1({},{})", m_mac->name(), m_rounds);
102 }
std::string fmt(std::string_view format, const T &... args)
Definition: fmt.h:60

References Botan::fmt().

◆ set_key() [1/3]

void Botan::SymmetricAlgorithm::set_key ( const SymmetricKey key)
inlineinherited

◆ set_key() [2/3]

void Botan::SymmetricAlgorithm::set_key ( const uint8_t  key[],
size_t  length 
)
inherited

Set the symmetric key of this object.

Parameters
keythe to be set as a byte array.
lengthin bytes of key param

Definition at line 17 of file sym_algo.cpp.

18 {
19 if(!valid_keylength(length))
20 throw Invalid_Key_Length(name(), length);
21 key_schedule(key, length);
22 }
bool valid_keylength(size_t length) const
Definition: sym_algo.h:141
virtual std::string name() const =0

References Botan::SymmetricAlgorithm::name(), and Botan::SymmetricAlgorithm::valid_keylength().

◆ set_key() [3/3]

void Botan::SymmetricAlgorithm::set_key ( std::span< const uint8_t >  key)
inlineinherited

Set the symmetric key of this object.

Parameters
keythe contiguous byte range to be set.

Definition at line 159 of file sym_algo.h.

160 {
161 set_key(key.data(), key.size());
162 }

◆ valid_keylength()

bool Botan::SymmetricAlgorithm::valid_keylength ( size_t  length) const
inlineinherited

Check whether a given key length is valid for this algorithm.

Parameters
lengththe key length to be checked.
Returns
true if the key length is valid.

Definition at line 141 of file sym_algo.h.

142 {
143 return key_spec().valid_keylength(length);
144 }
bool valid_keylength(size_t length) const
Definition: sym_algo.h:54

Referenced by Botan::SymmetricAlgorithm::set_key().


The documentation for this class was generated from the following files: