Botan 3.13.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#include <botan/symkey.h>
14#include <memory>
15
16namespace Botan {
17
19
20/**
21* Format Preserving Encryption using the scheme FE1 from the paper
22* "Format-Preserving Encryption" by Bellare, Rogaway, et al
23* (https://eprint.iacr.org/2009/251)
24*/
25class BOTAN_PUBLIC_API(2, 5) FPE_FE1 final : public SymmetricAlgorithm {
26 public:
27 /**
28 * Create an FE1 format preserving encryption object
29 * @param n the modulus. All plaintext and ciphertext values must be
30 * less than this. The value must not be prime and should be easily
31 * factored into roughly equal size values. The common case is that
32 * the modulus is a power of 10.
33 * @param rounds the number of rounds to use. Must be at least 3.
34 * @param compat_mode An error in versions before 2.5.0 chose incorrect
35 * values for a and b. Set compat_mode to true to select this version.
36 * @param mac_algo the PRF to use as the encryption function
37 */
39 size_t rounds = 5,
40 bool compat_mode = false,
41 std::string_view mac_algo = "HMAC(SHA-256)");
42
43 ~FPE_FE1() override;
44
45 /**
46 * Return the key lengths supported by this object
47 * @return the key length specification
48 */
49 Key_Length_Specification key_spec() const override;
50
51 /**
52 * Test whether a key has been set on this object
53 * @return true if a key has been set
54 */
55 bool has_keying_material() const override;
56
57 /**
58 * Return the name of this algorithm
59 * @return the algorithm name
60 */
61 std::string name() const override;
62
63 /**
64 * Reset the internal state, including the key
65 */
66 void clear() override;
67
68 /**
69 * Encrypt X from and onto the group Z_n using key and tweak
70 * @param x the plaintext to encrypt, where 0 <= x < n
71 * @param tweak will modify the ciphertext
72 * @param tweak_len length of tweak
73 */
74 BigInt encrypt(const BigInt& x, const uint8_t tweak[], size_t tweak_len) const;
75
76 /**
77 * Decrypt X from and onto the group Z_n using key and tweak
78 * @param x the ciphertext to decrypt, where 0 <= x < n
79 * @param tweak must match the value used to encrypt
80 * @param tweak_len length of tweak
81 */
82 BigInt decrypt(const BigInt& x, const uint8_t tweak[], size_t tweak_len) const;
83
84 /**
85 * Encrypt X from and onto the group Z_n using key and tweak
86 * @param x the plaintext to encrypt, where 0 <= x < n
87 * @param tweak will modify the ciphertext
88 * @return the ciphertext
89 */
90 BigInt encrypt(const BigInt& x, uint64_t tweak) const;
91
92 /**
93 * Decrypt X from and onto the group Z_n using key and tweak
94 * @param x the ciphertext to decrypt, where 0 <= x < n
95 * @param tweak must match the value used to encrypt
96 * @return the plaintext
97 */
98 BigInt decrypt(const BigInt& x, uint64_t tweak) const;
99
100 FPE_FE1(const FPE_FE1& other) = delete;
101 /**
102 * Move constructor
103 */
104 FPE_FE1(FPE_FE1&& other) noexcept;
105 FPE_FE1& operator=(const FPE_FE1& other) = delete;
106 FPE_FE1& operator=(FPE_FE1&& other) = delete;
107
108 private:
109 void key_schedule(std::span<const uint8_t> key) override;
110
111 BigInt F(const BigInt& R, size_t round, const secure_vector<uint8_t>& tweak, secure_vector<uint8_t>& tmp) const;
112
113 secure_vector<uint8_t> compute_tweak_mac(const uint8_t tweak[], size_t tweak_len) const;
114
115 std::unique_ptr<MessageAuthenticationCode> m_mac;
116 std::vector<uint8_t> m_n_bytes;
117 BigInt m_n;
118 BigInt m_a;
119 BigInt m_b;
120 size_t m_rounds;
121};
122
123class OctetString;
124
125namespace FPE {
126
127/**
128* Format Preserving Encryption using the scheme FE1 from the paper
129* "Format-Preserving Encryption" by Bellare, Rogaway, et al
130* (https://eprint.iacr.org/2009/251)
131*
132* Encrypt X from and onto the group Z_n using key and tweak
133* @param n the modulus
134* @param X the plaintext as a BigInt
135* @param key a random key
136* @param tweak will modify the ciphertext (think of as an IV)
137*
138* @warning This function is hardcoded to use only 3 rounds which
139* may be insecure for some values of n. Prefer FPE_FE1 class
140*/
142 fe1_encrypt(const BigInt& n, const BigInt& X, const OctetString& key, const std::vector<uint8_t>& tweak);
143
144/**
145* Decrypt X from and onto the group Z_n using key and tweak
146* @param n the modulus
147* @param X the ciphertext as a BigInt
148* @param key is the key used for encryption
149* @param tweak the same tweak used for encryption
150*
151* @warning This function is hardcoded to use only 3 rounds which
152* may be insecure for some values of n. Prefer FPE_FE1 class
153*/
155 fe1_decrypt(const BigInt& n, const BigInt& X, const OctetString& key, const std::vector<uint8_t>& tweak);
156
157} // namespace FPE
158
159} // namespace Botan
160
161#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
BigInt encrypt(const BigInt &x, const uint8_t tweak[], size_t tweak_len) const
Definition fpe_fe1.cpp:151
void clear() override
Definition fpe_fe1.cpp:103
FPE_FE1(FPE_FE1 &&other) noexcept
FPE_FE1 & operator=(const FPE_FE1 &other)=delete
std::string name() const override
Definition fpe_fe1.cpp:107
BOTAN_FUTURE_EXPLICIT FPE_FE1(const BigInt &n, size_t rounds=5, bool compat_mode=false, std::string_view mac_algo="HMAC(SHA-256)")
Definition fpe_fe1.cpp:72
FPE_FE1(const FPE_FE1 &other)=delete
bool has_keying_material() const override
Definition fpe_fe1.cpp:115
FPE_FE1 & operator=(FPE_FE1 &&other)=delete
~FPE_FE1() override
Key_Length_Specification key_spec() const override
Definition fpe_fe1.cpp:111
BigInt decrypt(const BigInt &x, const uint8_t tweak[], size_t tweak_len) const
Definition fpe_fe1.cpp:172
BigInt fe1_decrypt(const BigInt &n, const BigInt &X, const SymmetricKey &key, const std::vector< uint8_t > &tweak)
Definition fpe_fe1.cpp:213
BigInt fe1_encrypt(const BigInt &n, const BigInt &X, const SymmetricKey &key, const std::vector< uint8_t > &tweak)
Definition fpe_fe1.cpp:207
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128