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

randombit / botan / 23565603004

25 Mar 2026 09:42PM UTC coverage: 89.475% (-0.003%) from 89.478%
23565603004

push

github

web-flow
Merge pull request #5483 from randombit/jack/macos-15-intel

Enable the macos-15-intel CI builds again

105368 of 117762 relevant lines covered (89.48%)

11686839.19 hits per line

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

95.42
/src/lib/block/idea/idea.cpp
1
/*
2
* IDEA
3
* (C) 1999-2010,2015 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/internal/idea.h>
9

10
#include <botan/internal/ct_utils.h>
11
#include <botan/internal/loadstor.h>
12

13
#if defined(BOTAN_HAS_CPUID)
14
   #include <botan/internal/cpuid.h>
15
#endif
16

17
namespace Botan {
18

19
namespace {
20

21
/*
22
* Multiplication modulo 65537
23
*/
24
inline uint16_t mul(uint16_t x, uint16_t y) {
932,094✔
25
   uint32_t P = static_cast<uint32_t>(x) * y;
932,094✔
26
   const uint16_t P_is_zero = static_cast<uint16_t>(ct_is_zero(P));
932,094✔
27

28
   P = (P & 0xFFFF) - (P >> 16);
932,094✔
29
   const uint16_t R1 = static_cast<uint16_t>(P - (P >> 16));
932,094✔
30
   const uint16_t R0 = 1 - x - y;
932,094✔
31

32
   return choose(P_is_zero, R0, R1);
932,094✔
33
}
34

35
/*
36
* Find multiplicative inverses modulo 65537
37
*
38
* 65537 is prime; thus Fermat's little theorem tells us that
39
* x^65537 == x modulo 65537, which means
40
* x^(65537-2) == x^-1 modulo 65537 since
41
* x^(65537-2) * x == 1 mod 65537
42
*
43
* Do the exponentiation with a basic square and multiply: all bits are
44
* of exponent are 1 so we always multiply
45
*/
46
uint16_t mul_inv(uint16_t x) {
58,554✔
47
   uint16_t y = x;
58,554✔
48

49
   for(size_t i = 0; i != 15; ++i) {
936,864✔
50
      y = mul(y, y);  // square
878,310✔
51
      y = mul(y, x);
878,310✔
52
   }
53

54
   return y;
58,554✔
55
}
56

57
/**
58
* IDEA is involutional, depending only on the key schedule
59
*/
60
void idea_op(const uint8_t in[], uint8_t out[], size_t blocks, const uint16_t K[52]) {
5,932✔
61
   const size_t BLOCK_SIZE = 8;
5,932✔
62

63
   CT::poison(in, blocks * 8);
5,932✔
64
   CT::poison(out, blocks * 8);
5,932✔
65
   CT::poison(K, 52);
5,932✔
66

67
   for(size_t i = 0; i < blocks; ++i) {
11,908✔
68
      uint16_t X1 = 0;
5,976✔
69
      uint16_t X2 = 0;
5,976✔
70
      uint16_t X3 = 0;
5,976✔
71
      uint16_t X4 = 0;
5,976✔
72
      load_be(in + BLOCK_SIZE * i, X1, X2, X3, X4);
5,976✔
73

74
      for(size_t j = 0; j != 8; ++j) {
53,784✔
75
         X1 = mul(X1, K[6 * j + 0]);
47,808✔
76
         X2 += K[6 * j + 1];
47,808✔
77
         X3 += K[6 * j + 2];
47,808✔
78
         X4 = mul(X4, K[6 * j + 3]);
47,808✔
79

80
         const uint16_t T0 = X3;
47,808✔
81
         X3 = mul(X3 ^ X1, K[6 * j + 4]);
47,808✔
82

83
         const uint16_t T1 = X2;
47,808✔
84
         X2 = mul((X2 ^ X4) + X3, K[6 * j + 5]);
47,808✔
85
         X3 += X2;
47,808✔
86

87
         X1 ^= X2;
47,808✔
88
         X4 ^= X3;
47,808✔
89
         X2 ^= T0;
47,808✔
90
         X3 ^= T1;
47,808✔
91
      }
92

93
      X1 = mul(X1, K[48]);
5,976✔
94
      X2 += K[50];
5,976✔
95
      X3 += K[49];
5,976✔
96
      X4 = mul(X4, K[51]);
5,976✔
97

98
      store_be(out + BLOCK_SIZE * i, X1, X3, X2, X4);
5,976✔
99
   }
100

101
   CT::unpoison(in, blocks * 8);
5,932✔
102
   CT::unpoison(out, blocks * 8);
5,932✔
103
   CT::unpoison(K, 52);
5,932✔
104
}
5,932✔
105

106
}  // namespace
107

108
size_t IDEA::parallelism() const {
3,253✔
109
#if defined(BOTAN_HAS_IDEA_AVX2)
110
   if(CPUID::has(CPUID::Feature::AVX2)) {
3,253✔
111
      return 16;
112
   }
113
#endif
114

115
#if defined(BOTAN_HAS_IDEA_SSE2)
116
   if(CPUID::has(CPUID::Feature::SSE2)) {
×
117
      return 8;
×
118
   }
119
#endif
120

121
   return 1;
122
}
123

124
std::string IDEA::provider() const {
1,084✔
125
#if defined(BOTAN_HAS_IDEA_AVX2)
126
   if(auto feat = CPUID::check(CPUID::Feature::AVX2)) {
1,084✔
127
      return *feat;
2,168✔
128
   }
1,084✔
129
#endif
130

131
#if defined(BOTAN_HAS_IDEA_SSE2)
132
   if(auto feat = CPUID::check(CPUID::Feature::SSE2)) {
×
133
      return *feat;
×
134
   }
×
135
#endif
136

137
   return "base";
×
138
}
139

140
/*
141
* IDEA Encryption
142
*/
143
void IDEA::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
5,676✔
144
   assert_key_material_set();
5,676✔
145

146
#if defined(BOTAN_HAS_IDEA_AVX2)
147
   if(CPUID::has(CPUID::Feature::AVX2)) {
3,508✔
148
      while(blocks >= 16) {
3,563✔
149
         avx2_idea_op_16(in, out, m_EK.data());
55✔
150
         in += 16 * BLOCK_SIZE;
55✔
151
         out += 16 * BLOCK_SIZE;
55✔
152
         blocks -= 16;
55✔
153
      }
154
   }
155
#endif
156

157
#if defined(BOTAN_HAS_IDEA_SSE2)
158
   if(CPUID::has(CPUID::Feature::SSE2)) {
3,508✔
159
      while(blocks >= 8) {
1,887✔
160
         sse2_idea_op_8(in, out, m_EK.data());
5✔
161
         in += 8 * BLOCK_SIZE;
5✔
162
         out += 8 * BLOCK_SIZE;
5✔
163
         blocks -= 8;
5✔
164
      }
165
   }
166
#endif
167

168
   idea_op(in, out, blocks, m_EK.data());
3,508✔
169
}
3,508✔
170

171
/*
172
* IDEA Decryption
173
*/
174
void IDEA::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
4,592✔
175
   assert_key_material_set();
4,592✔
176

177
#if defined(BOTAN_HAS_IDEA_AVX2)
178
   if(CPUID::has(CPUID::Feature::AVX2)) {
2,424✔
179
      while(blocks >= 16) {
2,479✔
180
         avx2_idea_op_16(in, out, m_DK.data());
55✔
181
         in += 16 * BLOCK_SIZE;
55✔
182
         out += 16 * BLOCK_SIZE;
55✔
183
         blocks -= 16;
55✔
184
      }
185
   }
186
#endif
187

188
#if defined(BOTAN_HAS_IDEA_SSE2)
189
   if(CPUID::has(CPUID::Feature::SSE2)) {
2,424✔
190
      while(blocks >= 8) {
1,345✔
191
         sse2_idea_op_8(in, out, m_DK.data());
5✔
192
         in += 8 * BLOCK_SIZE;
5✔
193
         out += 8 * BLOCK_SIZE;
5✔
194
         blocks -= 8;
5✔
195
      }
196
   }
197
#endif
198

199
   idea_op(in, out, blocks, m_DK.data());
2,424✔
200
}
2,424✔
201

202
bool IDEA::has_keying_material() const {
14,604✔
203
   return !m_EK.empty();
14,604✔
204
}
205

206
/*
207
* IDEA Key Schedule
208
*/
209
void IDEA::key_schedule(std::span<const uint8_t> key) {
3,253✔
210
   m_EK.resize(52);
3,253✔
211
   m_DK.resize(52);
3,253✔
212

213
   CT::poison(key.data(), 16);
3,253✔
214
   CT::poison(m_EK.data(), 52);
3,253✔
215
   CT::poison(m_DK.data(), 52);
3,253✔
216

217
   secure_vector<uint64_t> K(2);
3,253✔
218

219
   K[0] = load_be<uint64_t>(key.data(), 0);
3,253✔
220
   K[1] = load_be<uint64_t>(key.data(), 1);
3,253✔
221

222
   for(size_t off = 0; off != 48; off += 8) {
22,771✔
223
      for(size_t i = 0; i != 8; ++i) {
175,662✔
224
         m_EK[off + i] = static_cast<uint16_t>(K[i / 4] >> (48 - 16 * (i % 4)));
156,144✔
225
      }
226

227
      const uint64_t Kx = (K[0] >> 39);
19,518✔
228
      const uint64_t Ky = (K[1] >> 39);
19,518✔
229

230
      K[0] = (K[0] << 25) | Ky;
19,518✔
231
      K[1] = (K[1] << 25) | Kx;
19,518✔
232
   }
233

234
   for(size_t i = 0; i != 4; ++i) {
16,265✔
235
      m_EK[48 + i] = static_cast<uint16_t>(K[i / 4] >> (48 - 16 * (i % 4)));
13,012✔
236
   }
237

238
   m_DK[0] = mul_inv(m_EK[48]);
3,253✔
239
   m_DK[1] = -m_EK[49];
3,253✔
240
   m_DK[2] = -m_EK[50];
3,253✔
241
   m_DK[3] = mul_inv(m_EK[51]);
3,253✔
242

243
   for(size_t i = 0; i != 8 * 6; i += 6) {
29,277✔
244
      m_DK[i + 4] = m_EK[46 - i];
26,024✔
245
      m_DK[i + 5] = m_EK[47 - i];
26,024✔
246
      m_DK[i + 6] = mul_inv(m_EK[42 - i]);
26,024✔
247
      m_DK[i + 7] = -m_EK[44 - i];
26,024✔
248
      m_DK[i + 8] = -m_EK[43 - i];
26,024✔
249
      m_DK[i + 9] = mul_inv(m_EK[45 - i]);
26,024✔
250
   }
251

252
   std::swap(m_DK[49], m_DK[50]);
3,253✔
253

254
   CT::unpoison(key.data(), 16);
3,253✔
255
   CT::unpoison(m_EK.data(), 52);
3,253✔
256
   CT::unpoison(m_DK.data(), 52);
3,253✔
257
}
3,253✔
258

259
void IDEA::clear() {
2,168✔
260
   zap(m_EK);
2,168✔
261
   zap(m_DK);
2,168✔
262
}
2,168✔
263

264
}  // namespace Botan
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