1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/*
* IDEA
* (C) 1999-2010,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/idea.h>
#include <botan/loadstor.h>
#include <botan/internal/ct_utils.h>
namespace Botan {
namespace {
/*
* Multiplication modulo 65537
*/
inline u16bit mul(u16bit x, u16bit y)
{
const u32bit P = static_cast<u32bit>(x) * y;
const u16bit Z_mask = static_cast<u16bit>(ct_expand_mask_32(P) & 0xFFFF);
const u32bit P_hi = P >> 16;
const u32bit P_lo = P & 0xFFFF;
const u16bit r_1 = (P_lo - P_hi) + (P_lo < P_hi);
const u16bit r_2 = 1 - x - y;
return ct_select_mask_16(Z_mask, r_1, r_2);
}
/*
* Find multiplicative inverses modulo 65537
*
* 65537 is prime; thus Fermat's little theorem tells us that
* x^65537 == x modulo 65537, which means
* x^(65537-2) == x^-1 modulo 65537 since
* x^(65537-2) * x == 1 mod 65537
*
* Do the exponentiation with a basic square and multiply: all bits are
* of exponent are 1 so we always multiply
*/
u16bit mul_inv(u16bit x)
{
u16bit y = x;
for(size_t i = 0; i != 15; ++i)
{
y = mul(y, y); // square
y = mul(y, x);
}
return y;
}
/**
* IDEA is involutional, depending only on the key schedule
*/
void idea_op(const byte in[], byte out[], size_t blocks, const u16bit K[52])
{
const size_t BLOCK_SIZE = 8;
BOTAN_CONST_TIME_POISON(in, blocks * 8);
BOTAN_CONST_TIME_POISON(out, blocks * 8);
BOTAN_CONST_TIME_POISON(K, 52 * 2);
for(size_t i = 0; i != blocks; ++i)
{
u16bit X1 = load_be<u16bit>(in + BLOCK_SIZE*i, 0);
u16bit X2 = load_be<u16bit>(in + BLOCK_SIZE*i, 1);
u16bit X3 = load_be<u16bit>(in + BLOCK_SIZE*i, 2);
u16bit X4 = load_be<u16bit>(in + BLOCK_SIZE*i, 3);
for(size_t j = 0; j != 8; ++j)
{
X1 = mul(X1, K[6*j+0]);
X2 += K[6*j+1];
X3 += K[6*j+2];
X4 = mul(X4, K[6*j+3]);
u16bit T0 = X3;
X3 = mul(X3 ^ X1, K[6*j+4]);
u16bit T1 = X2;
X2 = mul((X2 ^ X4) + X3, K[6*j+5]);
X3 += X2;
X1 ^= X2;
X4 ^= X3;
X2 ^= T0;
X3 ^= T1;
}
X1 = mul(X1, K[48]);
X2 += K[50];
X3 += K[49];
X4 = mul(X4, K[51]);
store_be(out + BLOCK_SIZE*i, X1, X3, X2, X4);
}
BOTAN_CONST_TIME_UNPOISON(in, blocks * 8);
BOTAN_CONST_TIME_UNPOISON(out, blocks * 8);
BOTAN_CONST_TIME_UNPOISON(K, 52 * 2);
}
}
/*
* IDEA Encryption
*/
void IDEA::encrypt_n(const byte in[], byte out[], size_t blocks) const
{
idea_op(in, out, blocks, EK.data());
}
/*
* IDEA Decryption
*/
void IDEA::decrypt_n(const byte in[], byte out[], size_t blocks) const
{
idea_op(in, out, blocks, DK.data());
}
/*
* IDEA Key Schedule
*/
void IDEA::key_schedule(const byte key[], size_t)
{
EK.resize(52);
DK.resize(52);
BOTAN_CONST_TIME_POISON(key, 16);
BOTAN_CONST_TIME_POISON(EK.data(), 52 * 2);
BOTAN_CONST_TIME_POISON(DK.data(), 52 * 2);
for(size_t i = 0; i != 8; ++i)
EK[i] = load_be<u16bit>(key, i);
for(size_t i = 1, j = 8, offset = 0; j != 52; i %= 8, ++i, ++j)
{
EK[i+7+offset] = static_cast<u16bit>((EK[(i % 8) + offset] << 9) |
(EK[((i+1) % 8) + offset] >> 7));
offset += (i == 8) ? 8 : 0;
}
DK[51] = mul_inv(EK[3]);
DK[50] = -EK[2];
DK[49] = -EK[1];
DK[48] = mul_inv(EK[0]);
for(size_t i = 1, j = 4, counter = 47; i != 8; ++i, j += 6)
{
DK[counter--] = EK[j+1];
DK[counter--] = EK[j];
DK[counter--] = mul_inv(EK[j+5]);
DK[counter--] = -EK[j+3];
DK[counter--] = -EK[j+4];
DK[counter--] = mul_inv(EK[j+2]);
}
DK[5] = EK[47];
DK[4] = EK[46];
DK[3] = mul_inv(EK[51]);
DK[2] = -EK[50];
DK[1] = -EK[49];
DK[0] = mul_inv(EK[48]);
BOTAN_CONST_TIME_UNPOISON(key, 16);
BOTAN_CONST_TIME_UNPOISON(EK.data(), 52 * 2);
BOTAN_CONST_TIME_UNPOISON(DK.data(), 52 * 2);
}
void IDEA::clear()
{
zap(EK);
zap(DK);
}
}
|