Coverage Report

Created: 2024-11-29 06:10

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