-
Notifications
You must be signed in to change notification settings - Fork 11
/
eme2.c
298 lines (263 loc) · 8.9 KB
/
eme2.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
LICENSE TERMS
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
1. source code distributions include the above copyright notice, this
list of conditions and the following disclaimer;
2. binary distributions include the above copyright notice, this list
of conditions and the following disclaimer in their documentation;
3. the name of the copyright holder is not used to endorse products
built using this software without specific written permission.
DISCLAIMER
This software is provided 'as is' with no explicit or implied warranties
in respect of its properties, including, but not limited to, correctness
and/or fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 04/11/2008
*/
#include "mode_hdr.h"
#include "eme2.h"
#include "gf_mulx.h"
#define BLOCK_PWR2 4
#define BLOCK_SIZE (1 << BLOCK_PWR2)
#define BLOCK_LEN_MASK (BLOCK_SIZE - 1)
#define N_BLOCKS(x) (((x) - 1) >> BLOCK_PWR2) + 1
UNIT_TYPEDEF(buf_unit, UNIT_BITS);
BUFR_TYPEDEF(buf_type, UNIT_BITS, BLOCK_SIZE);
INT_RETURN eme2_encrypt( const unsigned char pt_buf[], unsigned char ct_buf[], unsigned int pt_len,
const unsigned char ad_buf[], unsigned int ad_len,
const eme2_key key[1], unsigned int key_len )
{ buf_type t_buf, t_star, mp, mm, mc, mc1, m, m1;
aes_encrypt_ctx ctx[1];
unsigned int len, tt;
if( pt_len < BLOCK_SIZE )
return EXIT_FAILURE;
if( key_len == 48 || key_len == 8 * 48 )
aes_encrypt_key128( key->aes_key, ctx );
else if( key_len == 64 || key_len == 8 * 64 )
aes_encrypt_key256( key->aes_key, ctx );
else
return EXIT_FAILURE;
if(ad_len > 0)
{
memset( t_star, 0, BLOCK_SIZE );
copy_block( mm, key->adt_key );
gf_mulx( mm );
len = 0;
while(len < ad_len)
{
tt = ( ad_len - len < BLOCK_SIZE ? ad_len - len : BLOCK_SIZE );
memcpy( t_buf, ad_buf + len, tt );
if( tt < BLOCK_SIZE )
{
memset( UI8_PTR(t_buf) + tt, 0, BLOCK_SIZE - tt );
UI8_PTR(t_buf)[tt] = 0x80;
gf_mulx( mm );
}
xor_block_aligned( t_buf, t_buf, mm );
aes_encrypt( UI8_PTR(t_buf), UI8_PTR(t_buf), ctx );
xor_block_aligned( t_buf, t_buf, mm );
gf_mulx( mm );
xor_block_aligned( t_star, t_star, t_buf );
len += BLOCK_SIZE;
}
}
else
aes_encrypt( key->adt_key, UI8_PTR(t_star), ctx );
copy_block( mm, key->ecb_key );
copy_block_aligned( mp, t_star );
len = 0;
while( pt_len >= len + BLOCK_SIZE )
{
copy_block( t_buf, pt_buf + len );
xor_block_aligned( t_buf, t_buf, mm );
aes_encrypt( UI8_PTR(t_buf), UI8_PTR(t_buf), ctx );
xor_block_aligned( mp, mp, t_buf );
copy_block( ct_buf + len, t_buf );
gf_mulx( mm );
len += BLOCK_SIZE;
}
if( pt_len & BLOCK_LEN_MASK )
{
tt = len - 1;
while( ++tt < pt_len )
{
ct_buf[tt] = pt_buf[tt];
UI8_PTR(mp)[tt - len] ^= pt_buf[tt];
}
UI8_PTR(mp)[tt - len] ^= 0x80;
aes_encrypt( UI8_PTR(mp), UI8_PTR(mm), ctx );
aes_encrypt( UI8_PTR(mm), UI8_PTR(mc), ctx );
}
else
aes_encrypt( UI8_PTR(mp), UI8_PTR(mc), ctx );
copy_block_aligned( mc1, mc );
xor_block_aligned( m, mp, mc );
copy_block_aligned( m1, m );
len = BLOCK_SIZE;
while( pt_len >= len + BLOCK_SIZE )
{
copy_block( t_buf, ct_buf + len );
if( len & 0x07f0 )
{
gf_mulx( m );
xor_block_aligned( t_buf, t_buf, m );
}
else
{
xor_block_aligned( mp, t_buf, m1 );
aes_encrypt( UI8_PTR(mp), UI8_PTR(mc), ctx );
xor_block_aligned( m, mp, mc );
xor_block_aligned( t_buf, mc, m1 );
}
xor_block_aligned( mc1, mc1, t_buf);
copy_block( ct_buf + len, t_buf );
len += BLOCK_SIZE;
}
if( pt_len & BLOCK_LEN_MASK )
{
tt = len - 1;
while( ++tt < pt_len )
{
ct_buf[tt] = pt_buf[tt] ^ UI8_PTR(mm)[tt - len];
UI8_PTR(mc1)[tt - len] ^= ct_buf[tt];
}
UI8_PTR(mc1)[tt - len] ^= 0x80;
}
xor_block_aligned( mc1, mc1, t_star );
copy_block( ct_buf, mc1 );
copy_block( mm, key->ecb_key );
len = 0;
while( pt_len >= len + BLOCK_SIZE )
{
aes_encrypt( ct_buf + len, UI8_PTR(t_buf), ctx );
xor_block( t_buf, t_buf , mm );
copy_block( ct_buf + len, t_buf );
gf_mulx( mm );
len += BLOCK_SIZE;
}
return EXIT_SUCCESS;
}
INT_RETURN eme2_decrypt( const unsigned char ct_buf[], unsigned char pt_buf[], unsigned int pt_len,
const unsigned char ad_buf[], unsigned int ad_len,
const eme2_key key[1], unsigned int key_len )
{ buf_type t_buf, t_star, mc, mm, mp, mp1, m, m1;
unsigned int len, tt;
union
{ aes_encrypt_ctx e[1];
aes_decrypt_ctx d[1];
} ctx;
if( pt_len < BLOCK_SIZE )
return EXIT_FAILURE;
if( key_len == 48 || key_len == 8 * 48 )
aes_encrypt_key128( key->aes_key, ctx.e );
else if( key_len == 64 || key_len == 8 * 64 )
aes_encrypt_key256( key->aes_key, ctx.e );
else
return EXIT_FAILURE;
if(ad_len > 0)
{
memset( t_star, 0, BLOCK_SIZE );
copy_block( mm, key->adt_key );
gf_mulx( mm );
len = 0;
while(len < ad_len)
{
tt = ( ad_len - len < BLOCK_SIZE ? ad_len - len : BLOCK_SIZE );
memcpy( t_buf, ad_buf + len, tt );
if( tt < BLOCK_SIZE )
{
memset( UI8_PTR(t_buf) + tt, 0, BLOCK_SIZE - tt );
UI8_PTR(t_buf)[tt] = 0x80;
gf_mulx( mm );
}
xor_block_aligned( t_buf, t_buf, mm );
aes_encrypt( UI8_PTR(t_buf), UI8_PTR(t_buf), ctx.e );
xor_block_aligned( t_buf, t_buf, mm );
gf_mulx( mm );
xor_block_aligned( t_star, t_star, t_buf );
len += BLOCK_SIZE;
}
}
else
aes_encrypt( key->adt_key, UI8_PTR(t_star), ctx.e );
if( key_len == 48 || key_len == 8 * 48 )
aes_decrypt_key128( key->aes_key, ctx.d );
else if( key_len == 64 || key_len == 8 * 64 )
aes_decrypt_key256( key->aes_key, ctx.d );
copy_block( mm, key->ecb_key );
copy_block_aligned( mc, t_star );
len = 0;
while( pt_len >= len + BLOCK_SIZE )
{
copy_block( t_buf, ct_buf + len );
xor_block_aligned( t_buf, t_buf, mm );
aes_decrypt( UI8_PTR(t_buf), UI8_PTR(t_buf), ctx.d );
xor_block_aligned( mc, mc, t_buf );
copy_block( pt_buf + len, t_buf );
gf_mulx( mm );
len += BLOCK_SIZE;
}
if( pt_len & BLOCK_LEN_MASK )
{
tt = len - 1;
while( ++tt < pt_len )
{
pt_buf[tt] = ct_buf[tt];
UI8_PTR(mc)[tt - len] ^= pt_buf[tt];
}
UI8_PTR(mc)[tt - len] ^= 0x80;
aes_decrypt( UI8_PTR(mc), UI8_PTR(mm), ctx.d );
aes_decrypt( UI8_PTR(mm), UI8_PTR(mp), ctx.d );
}
else
aes_decrypt( UI8_PTR(mc), UI8_PTR(mp), ctx.d );
copy_block_aligned( mp1, mp );
xor_block_aligned(m, mp, mc);
copy_block_aligned( m1, m );
len = BLOCK_SIZE;
while( pt_len >= len + BLOCK_SIZE )
{
copy_block( t_buf, pt_buf + len );
if( len & 0x07f0 )
{
gf_mulx( m );
xor_block_aligned( t_buf, t_buf, m );
}
else
{
xor_block_aligned( mc, t_buf, m1 );
aes_decrypt( UI8_PTR(mc), UI8_PTR(mp), ctx.d );
xor_block_aligned( m, mp, mc );
xor_block_aligned( t_buf, mp, m1 );
}
xor_block_aligned( mp1, mp1, t_buf );
copy_block( pt_buf + len, t_buf );
len += BLOCK_SIZE;
}
if( pt_len & BLOCK_LEN_MASK )
{
tt = len - 1;
while( ++tt < pt_len )
{
pt_buf[tt] = ct_buf[tt] ^ UI8_PTR(mm)[tt - len];
UI8_PTR(mp1)[tt - len] ^= pt_buf[tt];
}
UI8_PTR(mp1)[tt - len] ^= 0x80;
}
xor_block_aligned( mp1, mp1, t_star );
copy_block( pt_buf, mp1 );
copy_block( mm, key->ecb_key );
len = 0;
while( pt_len >= len + BLOCK_SIZE )
{
aes_decrypt( pt_buf + len, UI8_PTR(t_buf), ctx.d );
xor_block_aligned( t_buf, t_buf, mm );
copy_block( pt_buf + len, t_buf );
gf_mulx( mm );
len += BLOCK_SIZE;
}
return EXIT_SUCCESS;
}