Coverage Report

Created: 2025-04-11 06:45

/src/botan/src/lib/block/idea/idea.cpp
Line
Count
Source (jump to first uncovered line)
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
0
inline uint16_t mul(uint16_t x, uint16_t y) {
25
0
   const uint32_t P = static_cast<uint32_t>(x) * y;
26
0
   const auto P_mask = CT::Mask<uint16_t>(CT::Mask<uint32_t>::is_zero(P));
27
28
0
   const uint32_t P_hi = P >> 16;
29
0
   const uint32_t P_lo = P & 0xFFFF;
30
31
0
   const uint16_t carry = (P_lo < P_hi);
32
0
   const uint16_t r_1 = static_cast<uint16_t>((P_lo - P_hi) + carry);
33
0
   const uint16_t r_2 = 1 - x - y;
34
35
0
   return P_mask.select(r_2, r_1);
36
0
}
37
38
/*
39
* Find multiplicative inverses modulo 65537
40
*
41
* 65537 is prime; thus Fermat's little theorem tells us that
42
* x^65537 == x modulo 65537, which means
43
* x^(65537-2) == x^-1 modulo 65537 since
44
* x^(65537-2) * x == 1 mod 65537
45
*
46
* Do the exponentiation with a basic square and multiply: all bits are
47
* of exponent are 1 so we always multiply
48
*/
49
0
uint16_t mul_inv(uint16_t x) {
50
0
   uint16_t y = x;
51
52
0
   for(size_t i = 0; i != 15; ++i) {
53
0
      y = mul(y, y);  // square
54
0
      y = mul(y, x);
55
0
   }
56
57
0
   return y;
58
0
}
59
60
/**
61
* IDEA is involutional, depending only on the key schedule
62
*/
63
0
void idea_op(const uint8_t in[], uint8_t out[], size_t blocks, const uint16_t K[52]) {
64
0
   const size_t BLOCK_SIZE = 8;
65
66
0
   CT::poison(in, blocks * 8);
67
0
   CT::poison(out, blocks * 8);
68
0
   CT::poison(K, 52);
69
70
0
   for(size_t i = 0; i < blocks; ++i) {
71
0
      uint16_t X1, X2, X3, X4;
72
0
      load_be(in + BLOCK_SIZE * i, X1, X2, X3, X4);
73
74
0
      for(size_t j = 0; j != 8; ++j) {
75
0
         X1 = mul(X1, K[6 * j + 0]);
76
0
         X2 += K[6 * j + 1];
77
0
         X3 += K[6 * j + 2];
78
0
         X4 = mul(X4, K[6 * j + 3]);
79
80
0
         const uint16_t T0 = X3;
81
0
         X3 = mul(X3 ^ X1, K[6 * j + 4]);
82
83
0
         const uint16_t T1 = X2;
84
0
         X2 = mul((X2 ^ X4) + X3, K[6 * j + 5]);
85
0
         X3 += X2;
86
87
0
         X1 ^= X2;
88
0
         X4 ^= X3;
89
0
         X2 ^= T0;
90
0
         X3 ^= T1;
91
0
      }
92
93
0
      X1 = mul(X1, K[48]);
94
0
      X2 += K[50];
95
0
      X3 += K[49];
96
0
      X4 = mul(X4, K[51]);
97
98
0
      store_be(out + BLOCK_SIZE * i, X1, X3, X2, X4);
99
0
   }
100
101
0
   CT::unpoison(in, blocks * 8);
102
0
   CT::unpoison(out, blocks * 8);
103
0
   CT::unpoison(K, 52);
104
0
}
105
106
}  // namespace
107
108
0
size_t IDEA::parallelism() const {
109
0
#if defined(BOTAN_HAS_IDEA_SSE2)
110
0
   if(CPUID::has(CPUID::Feature::SSE2)) {
111
0
      return 8;
112
0
   }
113
0
#endif
114
115
0
   return 1;
116
0
}
117
118
0
std::string IDEA::provider() const {
119
0
#if defined(BOTAN_HAS_IDEA_SSE2)
120
0
   if(CPUID::has(CPUID::Feature::SSE2)) {
121
0
      return "sse2";
122
0
   }
123
0
#endif
124
125
0
   return "base";
126
0
}
127
128
/*
129
* IDEA Encryption
130
*/
131
0
void IDEA::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
132
0
   assert_key_material_set();
133
134
0
#if defined(BOTAN_HAS_IDEA_SSE2)
135
0
   if(CPUID::has(CPUID::Feature::SSE2)) {
136
0
      while(blocks >= 8) {
137
0
         sse2_idea_op_8(in, out, m_EK.data());
138
0
         in += 8 * BLOCK_SIZE;
139
0
         out += 8 * BLOCK_SIZE;
140
0
         blocks -= 8;
141
0
      }
142
0
   }
143
0
#endif
144
145
0
   idea_op(in, out, blocks, m_EK.data());
146
0
}
147
148
/*
149
* IDEA Decryption
150
*/
151
0
void IDEA::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
152
0
   assert_key_material_set();
153
154
0
#if defined(BOTAN_HAS_IDEA_SSE2)
155
0
   if(CPUID::has(CPUID::Feature::SSE2)) {
156
0
      while(blocks >= 8) {
157
0
         sse2_idea_op_8(in, out, m_DK.data());
158
0
         in += 8 * BLOCK_SIZE;
159
0
         out += 8 * BLOCK_SIZE;
160
0
         blocks -= 8;
161
0
      }
162
0
   }
163
0
#endif
164
165
0
   idea_op(in, out, blocks, m_DK.data());
166
0
}
167
168
0
bool IDEA::has_keying_material() const {
169
0
   return !m_EK.empty();
170
0
}
171
172
/*
173
* IDEA Key Schedule
174
*/
175
0
void IDEA::key_schedule(std::span<const uint8_t> key) {
176
0
   m_EK.resize(52);
177
0
   m_DK.resize(52);
178
179
0
   CT::poison(key.data(), 16);
180
0
   CT::poison(m_EK.data(), 52);
181
0
   CT::poison(m_DK.data(), 52);
182
183
0
   secure_vector<uint64_t> K(2);
184
185
0
   K[0] = load_be<uint64_t>(key.data(), 0);
186
0
   K[1] = load_be<uint64_t>(key.data(), 1);
187
188
0
   for(size_t off = 0; off != 48; off += 8) {
189
0
      for(size_t i = 0; i != 8; ++i) {
190
0
         m_EK[off + i] = static_cast<uint16_t>(K[i / 4] >> (48 - 16 * (i % 4)));
191
0
      }
192
193
0
      const uint64_t Kx = (K[0] >> 39);
194
0
      const uint64_t Ky = (K[1] >> 39);
195
196
0
      K[0] = (K[0] << 25) | Ky;
197
0
      K[1] = (K[1] << 25) | Kx;
198
0
   }
199
200
0
   for(size_t i = 0; i != 4; ++i) {
201
0
      m_EK[48 + i] = static_cast<uint16_t>(K[i / 4] >> (48 - 16 * (i % 4)));
202
0
   }
203
204
0
   m_DK[0] = mul_inv(m_EK[48]);
205
0
   m_DK[1] = -m_EK[49];
206
0
   m_DK[2] = -m_EK[50];
207
0
   m_DK[3] = mul_inv(m_EK[51]);
208
209
0
   for(size_t i = 0; i != 8 * 6; i += 6) {
210
0
      m_DK[i + 4] = m_EK[46 - i];
211
0
      m_DK[i + 5] = m_EK[47 - i];
212
0
      m_DK[i + 6] = mul_inv(m_EK[42 - i]);
213
0
      m_DK[i + 7] = -m_EK[44 - i];
214
0
      m_DK[i + 8] = -m_EK[43 - i];
215
0
      m_DK[i + 9] = mul_inv(m_EK[45 - i]);
216
0
   }
217
218
0
   std::swap(m_DK[49], m_DK[50]);
219
220
0
   CT::unpoison(key.data(), 16);
221
0
   CT::unpoison(m_EK.data(), 52);
222
0
   CT::unpoison(m_DK.data(), 52);
223
0
}
224
225
0
void IDEA::clear() {
226
0
   zap(m_EK);
227
0
   zap(m_DK);
228
0
}
229
230
}  // namespace Botan