Botan 3.4.0
Crypto and TLS for C&
fpe_fe1.h
Go to the documentation of this file.
1/*
2* Format Preserving Encryption (FE1 scheme)
3* (C) 2009,2018 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_FPE_FE1_H_
9#define BOTAN_FPE_FE1_H_
10
11#include <botan/bigint.h>
12#include <botan/sym_algo.h>
13
14namespace Botan {
15
16class Modular_Reducer;
17class MessageAuthenticationCode;
18
19/**
20* Format Preserving Encryption using the scheme FE1 from the paper
21* "Format-Preserving Encryption" by Bellare, Rogaway, et al
22* (https://eprint.iacr.org/2009/251)
23*/
25 public:
26 /**
27 * @param n the modulus. All plaintext and ciphertext values must be
28 * less than this.
29 * @param rounds the number of rounds to use. Must be at least 3.
30 * @param compat_mode An error in versions before 2.5.0 chose incorrect
31 * values for a and b. Set compat_mode to true to select this version.
32 * @param mac_algo the PRF to use as the encryption function
33 */
34 FPE_FE1(const BigInt& n,
35 size_t rounds = 5,
36 bool compat_mode = false,
37 std::string_view mac_algo = "HMAC(SHA-256)");
38
39 ~FPE_FE1() override;
40
41 Key_Length_Specification key_spec() const override;
42
43 bool has_keying_material() const override;
44
45 std::string name() const override;
46
47 void clear() override;
48
49 /**
50 * Encrypt X from and onto the group Z_n using key and tweak
51 * @param x the plaintext to encrypt <= n
52 * @param tweak will modify the ciphertext
53 * @param tweak_len length of tweak
54 */
55 BigInt encrypt(const BigInt& x, const uint8_t tweak[], size_t tweak_len) const;
56
57 /**
58 * Decrypt X from and onto the group Z_n using key and tweak
59 * @param x the ciphertext to encrypt <= n
60 * @param tweak must match the value used to encrypt
61 * @param tweak_len length of tweak
62 */
63 BigInt decrypt(const BigInt& x, const uint8_t tweak[], size_t tweak_len) const;
64
65 BigInt encrypt(const BigInt& x, uint64_t tweak) const;
66
67 BigInt decrypt(const BigInt& x, uint64_t tweak) const;
68
69 private:
70 void key_schedule(std::span<const uint8_t> key) override;
71
72 BigInt F(const BigInt& R, size_t round, const secure_vector<uint8_t>& tweak, secure_vector<uint8_t>& tmp) const;
73
74 secure_vector<uint8_t> compute_tweak_mac(const uint8_t tweak[], size_t tweak_len) const;
75
76 std::unique_ptr<MessageAuthenticationCode> m_mac;
77 std::unique_ptr<Modular_Reducer> mod_a;
78 std::vector<uint8_t> m_n_bytes;
79 BigInt m_a;
80 BigInt m_b;
81 size_t m_rounds;
82};
83
84namespace FPE {
85
86/**
87* Format Preserving Encryption using the scheme FE1 from the paper
88* "Format-Preserving Encryption" by Bellare, Rogaway, et al
89* (https://eprint.iacr.org/2009/251)
90*
91* Encrypt X from and onto the group Z_n using key and tweak
92* @param n the modulus
93* @param X the plaintext as a BigInt
94* @param key a random key
95* @param tweak will modify the ciphertext (think of as an IV)
96*
97* @warning This function is hardcoded to use only 3 rounds which
98* may be insecure for some values of n. Prefer FPE_FE1 class
99*/
101 fe1_encrypt(const BigInt& n, const BigInt& X, const SymmetricKey& key, const std::vector<uint8_t>& tweak);
102
103/**
104* Decrypt X from and onto the group Z_n using key and tweak
105* @param n the modulus
106* @param X the ciphertext as a BigInt
107* @param key is the key used for encryption
108* @param tweak the same tweak used for encryption
109*
110* @warning This function is hardcoded to use only 3 rounds which
111* may be insecure for some values of n. Prefer FPE_FE1 class
112*/
114 fe1_decrypt(const BigInt& n, const BigInt& X, const SymmetricKey& key, const std::vector<uint8_t>& tweak);
115
116} // namespace FPE
117
118} // namespace Botan
119
120#endif
~FPE_FE1() override
std::string name
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
FE_25519 X
Definition ge.cpp:25
BigInt fe1_decrypt(const BigInt &n, const BigInt &X, const SymmetricKey &key, const std::vector< uint8_t > &tweak)
Definition fpe_fe1.cpp:193
BigInt fe1_encrypt(const BigInt &n, const BigInt &X, const SymmetricKey &key, const std::vector< uint8_t > &tweak)
Definition fpe_fe1.cpp:187
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61