• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.16
/src/lib/misc/fpe_fe1/fpe_fe1.cpp
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
#include <botan/fpe_fe1.h>
9

10
#include <botan/mac.h>
11
#include <botan/numthry.h>
12
#include <botan/reducer.h>
13
#include <botan/internal/divide.h>
14
#include <botan/internal/fmt.h>
15
#include <botan/internal/loadstor.h>
16

17
namespace Botan {
18

19
namespace {
20

21
// Normally FPE is for SSNs, CC#s, etc, nothing too big
22
const size_t MAX_N_BYTES = 128 / 8;
23

24
/*
25
* Factor n into a and b which are as close together as possible.
26
* Assumes n is composed mostly of small factors which is the case for
27
* typical uses of FPE (typically, n is a power of 10)
28
*/
29
void factor(BigInt n, BigInt& a, BigInt& b) {
15✔
30
   a = BigInt::one();
15✔
31
   b = BigInt::one();
15✔
32

33
   size_t n_low_zero = low_zero_bits(n);
15✔
34

35
   a <<= (n_low_zero / 2);
15✔
36
   b <<= n_low_zero - (n_low_zero / 2);
15✔
37
   n >>= n_low_zero;
15✔
38

39
   for(size_t i = 0; i != PRIME_TABLE_SIZE; ++i) {
98,130✔
40
      while(n % PRIMES[i] == 0) {
98,256✔
41
         a *= PRIMES[i];
141✔
42
         if(a > b)
141✔
43
            std::swap(a, b);
141✔
44
         n /= BigInt::from_word(PRIMES[i]);
282✔
45
      }
46
   }
47

48
   if(a > b)
15✔
49
      std::swap(a, b);
×
50
   a *= n;
15✔
51

52
   if(a <= 1 || b <= 1)
30✔
53
      throw Internal_Error("Could not factor n for use in FPE");
×
54
}
15✔
55

56
}
57

58
FPE_FE1::FPE_FE1(const BigInt& n, size_t rounds, bool compat_mode, std::string_view mac_algo) : m_rounds(rounds) {
15✔
59
   if(m_rounds < 3)
15✔
60
      throw Invalid_Argument("FPE_FE1 rounds too small");
×
61

62
   m_mac = MessageAuthenticationCode::create_or_throw(mac_algo);
15✔
63

64
   m_n_bytes = BigInt::encode(n);
15✔
65

66
   if(m_n_bytes.size() > MAX_N_BYTES)
15✔
67
      throw Invalid_Argument("N is too large for FPE encryption");
×
68

69
   factor(n, m_a, m_b);
15✔
70

71
   if(compat_mode) {
15✔
72
      if(m_a < m_b)
12✔
73
         std::swap(m_a, m_b);
12✔
74
   } else {
75
      if(m_a > m_b)
3✔
76
         std::swap(m_a, m_b);
×
77
   }
78

79
   mod_a = std::make_unique<Modular_Reducer>(m_a);
15✔
80
}
15✔
81

82
FPE_FE1::~FPE_FE1() = default;
62✔
83

84
void FPE_FE1::clear() { m_mac->clear(); }
×
85

86
std::string FPE_FE1::name() const { return fmt("FPE_FE1({},{})", m_mac->name(), m_rounds); }
×
87

88
Key_Length_Specification FPE_FE1::key_spec() const { return m_mac->key_spec(); }
15✔
89

90
bool FPE_FE1::has_keying_material() const { return m_mac->has_keying_material(); }
×
91

92
void FPE_FE1::key_schedule(const uint8_t key[], size_t length) { m_mac->set_key(key, length); }
15✔
93

94
BigInt FPE_FE1::F(const BigInt& R,
86✔
95
                  size_t round,
96
                  const secure_vector<uint8_t>& tweak_mac,
97
                  secure_vector<uint8_t>& tmp) const {
98
   tmp = BigInt::encode_locked(R);
86✔
99

100
   m_mac->update(tweak_mac);
86✔
101
   m_mac->update_be(static_cast<uint32_t>(round));
86✔
102

103
   m_mac->update_be(static_cast<uint32_t>(tmp.size()));
86✔
104
   m_mac->update(tmp.data(), tmp.size());
86✔
105

106
   tmp = m_mac->final();
86✔
107
   return BigInt(tmp.data(), tmp.size());
86✔
108
}
109

110
secure_vector<uint8_t> FPE_FE1::compute_tweak_mac(const uint8_t tweak[], size_t tweak_len) const {
22✔
111
   m_mac->update_be(static_cast<uint32_t>(m_n_bytes.size()));
22✔
112
   m_mac->update(m_n_bytes.data(), m_n_bytes.size());
22✔
113

114
   m_mac->update_be(static_cast<uint32_t>(tweak_len));
22✔
115
   if(tweak_len > 0)
22✔
116
      m_mac->update(tweak, tweak_len);
16✔
117

118
   return m_mac->final();
22✔
119
}
120

121
BigInt FPE_FE1::encrypt(const BigInt& input, const uint8_t tweak[], size_t tweak_len) const {
11✔
122
   const secure_vector<uint8_t> tweak_mac = compute_tweak_mac(tweak, tweak_len);
11✔
123

124
   BigInt X = input;
11✔
125

126
   secure_vector<uint8_t> tmp;
11✔
127

128
   BigInt L, R, Fi;
11✔
129
   for(size_t i = 0; i != m_rounds; ++i) {
54✔
130
      ct_divide(X, m_b, L, R);
43✔
131
      Fi = F(R, i, tweak_mac, tmp);
43✔
132
      X = m_a * R + mod_a->reduce(L + Fi);
215✔
133
   }
134

135
   return X;
11✔
136
}
55✔
137

138
BigInt FPE_FE1::decrypt(const BigInt& input, const uint8_t tweak[], size_t tweak_len) const {
11✔
139
   const secure_vector<uint8_t> tweak_mac = compute_tweak_mac(tweak, tweak_len);
11✔
140

141
   BigInt X = input;
11✔
142
   secure_vector<uint8_t> tmp;
11✔
143

144
   BigInt W, R, Fi;
11✔
145
   for(size_t i = 0; i != m_rounds; ++i) {
54✔
146
      ct_divide(X, m_a, R, W);
43✔
147

148
      Fi = F(R, m_rounds - i - 1, tweak_mac, tmp);
43✔
149
      X = m_b * mod_a->reduce(W - Fi) + R;
215✔
150
   }
151

152
   return X;
11✔
153
}
55✔
154

155
BigInt FPE_FE1::encrypt(const BigInt& x, uint64_t tweak) const {
×
156
   uint8_t tweak8[8];
×
157
   store_be(tweak, tweak8);
×
158
   return encrypt(x, tweak8, sizeof(tweak8));
×
159
}
160

161
BigInt FPE_FE1::decrypt(const BigInt& x, uint64_t tweak) const {
×
162
   uint8_t tweak8[8];
×
163
   store_be(tweak, tweak8);
×
164
   return decrypt(x, tweak8, sizeof(tweak8));
×
165
}
166

167
namespace FPE {
168

169
BigInt fe1_encrypt(const BigInt& n, const BigInt& X, const SymmetricKey& key, const std::vector<uint8_t>& tweak) {
6✔
170
   FPE_FE1 fpe(n, 3, true, "HMAC(SHA-256)");
6✔
171
   fpe.set_key(key);
6✔
172
   return fpe.encrypt(X, tweak.data(), tweak.size());
12✔
173
}
6✔
174

175
BigInt fe1_decrypt(const BigInt& n, const BigInt& X, const SymmetricKey& key, const std::vector<uint8_t>& tweak) {
6✔
176
   FPE_FE1 fpe(n, 3, true, "HMAC(SHA-256)");
6✔
177
   fpe.set_key(key);
6✔
178
   return fpe.decrypt(X, tweak.data(), tweak.size());
12✔
179
}
6✔
180

181
}
182

183
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc