-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecim-128.c
538 lines (454 loc) · 16.3 KB
/
decim-128.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
* DECIM-128 reference implementation.
*
* This code is supposed to run on any conforming C implementation (C90
* or later).
*
* Because DECIM-128 is a hardware candidate, we deliberately made a very
* easy to understand and not at all optimised implementation.
*
* (c) 2005 X-CRYPT project. This software is provided 'as-is', without
* any express or implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* excluding commercial applications, and to alter it and redistribute it
* freely except for commercial applications.
*
* France Telecom believes that the Decim-128 cipher is covered in part by
* the patent application under reference PCT/FR 04/2070 in co-ownership
* between France Telecom and l'Universite de Caen Basse-Normandie.
* The X-CRYPT project members do not know of other licensed, patented
* or otherwise legally restricted existing work that the Decim cipher
* would be based on.
*
* Technical remarks and questions can be addressed to
*/
#include"decim-128.h"
/* Not needed */
void ECRYPT_init(void){}
void ECRYPT_keysetup(ECRYPT_ctx* ctx, const u8* key, u32 keysize, u32 ivsize)
{
/* save the key */
memcpy(ctx->key, key, 16);
/* save the IV size in the cipher's context */
ctx->iv_size = ivsize;
}
void ECRYPT_ivsetup(ECRYPT_ctx* ctx, const u8* iv)
{
int i, j;
/* reset ABSG internal state */
ctx->immediate_finding=0;
ctx->bit_searched=0;
ctx->searching=0;
/* clear the output buffer */
ctx->buffer_end=0;
for(i=0;i<64;i++) ctx->buffer[i]=0;
for (i=0;i<288;i++) ctx->lfsr_state[i] = 0 ;
ctx->out = 0;
ctx->bool_out = 0 ;
ctx->bits_in_byte = 0 ;
ctx->stream_byte = 0 ;
/* key injection */
for (i=0;i<16;i++)
for (j=0;j<8;j++)
ctx->lfsr_state[i*8+j] = (ctx->key[i]>>j) & 0x01;
for (i=0;i<16;i++)
for (j=0;j<8;j++)
{
ctx->lfsr_state[i*8+j+128 ] = (ctx->key[i]>>j) & 0x01;
}
/* iv injection*/
for (i=0;i<16;i++)
for (j=0;j<8;j++)
{
ctx->lfsr_state[i*8+j+128 ] ^= (iv[i]>>j) & 0x01;
}
/*finishing to complete the initial state*/
for (i=256;i<288;i++)
{
if (i%2)
ctx->lfsr_state[i] = 1;
}
for (j=0;j<1152;j++)
decim_lfsr_init(ctx);
/* reset ABSG state */
ctx->immediate_finding=0;
ctx->bit_searched=0;
ctx->searching=0;
ctx->buffer_end=0;
for(i=0;i<64;i++) ctx->buffer[i]=0;
ctx->out = 0;
ctx->bool_out = 0 ;
ctx->bits_in_byte = 0 ;
ctx->stream_byte = 0 ;
/* fill the buffer */
while(ctx->buffer_end<64)
{
decim_step(ctx);
decim_step(ctx);
decim_step(ctx);
decim_step(ctx);
}
ctx->bits_in_byte = 0;
ctx->stream_byte = 0;
}
/*
* Encryption/Decryption
* action: silently ignored since this is an involutional cipher
* ctx : the context with all the internal state
* input : the plaintext bytes
* output: the encrypted bytes
* msglen: the message's length in bytes
*/
void ECRYPT_process_bytes(int action, ECRYPT_ctx* ctx, const u8* input, u8* output, u32 msglen)
{
u8 stream_byte;
int is_stream_byte = 0;
u32 bytes_processed = 0;
int i;
while(bytes_processed < msglen)
{
decim_step(ctx);
decim_step(ctx);
decim_step(ctx);
decim_step(ctx);
decim_process_buffer(ctx, &is_stream_byte, &stream_byte);
if(is_stream_byte){
output[bytes_processed] = input[bytes_processed] ^ stream_byte;
bytes_processed++;
}
/*
* manage the case where the buffer is empty
* this will happen with very low probability
* */
if((ctx->buffer_end==0) && (bytes_processed < msglen)){
printf("ERROR BUFFER EMPTY\n");
for(i=0;i<4;i++){
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_process_buffer(ctx, &is_stream_byte, &stream_byte);
if(is_stream_byte){
output[bytes_processed] = input[bytes_processed] ^ stream_byte;
bytes_processed++;
}
}
}/* now, buffer refilled */
}/* end of while: all bytes (en/de)crypted */
}
/*
* Keystream generation
* ctx : the context with all the internal state
* Keystream: the keystream
* length : the keystream size in bytes
*/
void ECRYPT_keystream_bytes(ECRYPT_ctx* ctx, u8* keystream, u32 length)
{
u8 stream_byte;
int is_stream_byte = 0;
u32 bytes_outputed = 0;
int i;
while(bytes_outputed < length)
{
decim_step(ctx);
decim_step(ctx);
decim_step(ctx);
decim_step(ctx);
decim_process_buffer(ctx, &is_stream_byte, &stream_byte);
if(is_stream_byte){
keystream[bytes_outputed] = stream_byte;
bytes_outputed++;
}
/*
* manage the case where the buffer is empty
* this will happen with very low probability
* */
if((ctx->buffer_end==0) && (bytes_outputed < length)){
printf("ERROR BUFFER EMPTY\n");
for(i=0;i<4;i++){
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
ctx->buffer_end++; ctx->buffer[ctx->buffer_end]=ctx->bool_out;
decim_process_buffer(ctx, &is_stream_byte, &stream_byte);
if(is_stream_byte){
keystream[bytes_outputed] = stream_byte;
bytes_outputed++;
}
}
}/* now, buffer refilled */
}/* end of while: all keystream generated */
}
/*
* Here follows the auxiliary functions
* */
void decim_lfsr_clock(ECRYPT_ctx *ctx)
{
memcpy(ctx->lfsr_state, ctx->lfsr_state+1, 16);
memcpy(ctx->lfsr_state+16, ctx->lfsr_state+17, 16);
memcpy(ctx->lfsr_state+32, ctx->lfsr_state+33, 16);
memcpy(ctx->lfsr_state+48, ctx->lfsr_state+49, 16);
memcpy(ctx->lfsr_state+64, ctx->lfsr_state+65, 16);
memcpy(ctx->lfsr_state+80, ctx->lfsr_state+81, 16);
memcpy(ctx->lfsr_state+96, ctx->lfsr_state+97, 16);
memcpy(ctx->lfsr_state+112, ctx->lfsr_state+113, 16);
memcpy(ctx->lfsr_state+128, ctx->lfsr_state+129, 16);
memcpy(ctx->lfsr_state+144, ctx->lfsr_state+145, 16);
memcpy(ctx->lfsr_state+160, ctx->lfsr_state+161, 16);
memcpy(ctx->lfsr_state+176, ctx->lfsr_state+177, 16);
memcpy(ctx->lfsr_state+192, ctx->lfsr_state+193, 16);
memcpy(ctx->lfsr_state+208, ctx->lfsr_state+209, 16);
memcpy(ctx->lfsr_state+224, ctx->lfsr_state+225, 16);
memcpy(ctx->lfsr_state+240, ctx->lfsr_state+241, 16);
memcpy(ctx->lfsr_state+256, ctx->lfsr_state+257, 16);
memcpy(ctx->lfsr_state+272, ctx->lfsr_state+273, 16);
}
void decim_lfsr_filter(ECRYPT_ctx *ctx)
{
u8 b,c;
/* compute the boolean function */
c = ctx->lfsr_state[1];
b = ctx->lfsr_state[21];
c ^= ctx->lfsr_state[21];
b += ctx->lfsr_state[39];
c ^= ctx->lfsr_state[39];
b += ctx->lfsr_state[51];
c ^= ctx->lfsr_state[51];
b += ctx->lfsr_state[73];
c ^= ctx->lfsr_state[73];
b += ctx->lfsr_state[120];
c ^= ctx->lfsr_state[120];
b += ctx->lfsr_state[159];
c ^= ctx->lfsr_state[159];
b += ctx->lfsr_state[187];
c ^= ctx->lfsr_state[187];
b += ctx->lfsr_state[203];
c ^= ctx->lfsr_state[203];
b += ctx->lfsr_state[236];
c ^= ctx->lfsr_state[236];
b += ctx->lfsr_state[244];
c ^= ctx->lfsr_state[244];
b += ctx->lfsr_state[263];
c ^= ctx->lfsr_state[263];
b += ctx->lfsr_state[276];
c ^= ctx->lfsr_state[276];
b += ctx->lfsr_state[287];
c ^= ctx->lfsr_state[287];
ctx->bool_out = ((b>>1)&0x01)^c;
/* compute the next bit for the LFSR*/
b = ctx->lfsr_state[0];
b ^= ctx->lfsr_state[3];
b ^= ctx->lfsr_state[4];
b ^= ctx->lfsr_state[41];
b ^= ctx->lfsr_state[84];
b ^= ctx->lfsr_state[103];
b ^= ctx->lfsr_state[134];
b ^= ctx->lfsr_state[163];
b ^= ctx->lfsr_state[164];
b ^= ctx->lfsr_state[165];
b ^= ctx->lfsr_state[206];
b ^= ctx->lfsr_state[253];
b ^= ctx->lfsr_state[270];
b ^= ctx->lfsr_state[283];
ctx->lfsr_state[288] = b; /* next bit */
}
void decim_absg(ECRYPT_ctx *ctx, u8 bit)
{
ctx->bit_searched = (ctx->searching ? ctx->bit_searched : bit);
ctx->out = ctx->searching & (ctx->immediate_finding ^ bit);
ctx->immediate_finding = ctx->searching & (ctx->bit_searched ^ bit);
ctx->searching = !ctx->searching |ctx->immediate_finding;
}
void decim_lfsr_init(ECRYPT_ctx *ctx)
{
decim_lfsr_filter(ctx);
ctx->lfsr_state[288] ^= ctx->bool_out^ctx->lfsr_state[1];
decim_lfsr_clock(ctx);
}
void decim_step(ECRYPT_ctx *ctx)
{
decim_lfsr_filter(ctx);
decim_lfsr_clock(ctx);
decim_absg(ctx, ctx->bool_out);
if ( !ctx->searching && (ctx->buffer_end<64)){
ctx->buffer[ctx->buffer_end]=ctx->out;
ctx->buffer_end++;
}
}
void decim_process_buffer(ECRYPT_ctx *ctx, int *is_stream_byte, u8 *stream_byte)
{
int i;
if( (*is_stream_byte = (ctx->bits_in_byte == 8) ))
{
*stream_byte = ctx->stream_byte;
ctx->bits_in_byte = 0;
ctx->stream_byte = 0;
}
ctx->stream_byte |= (ctx->buffer[0])<<ctx->bits_in_byte ;
ctx->bits_in_byte++ ;
for(i=0;i<63;i++) ctx->buffer[i]=ctx->buffer[i+1];
ctx->buffer_end--;
}
#ifdef DECIM_VECTORS
int main (int argc, char **argv)
{
static u8 pkey[33][10] = { /* a bench of keys */
{ 0xf6, 0x3d, 0xb4, 0x85, 0x4f, 0x85, 0x7d, 0xa9, 0x55, 0x43 } ,
{ 0x0c, 0x54, 0x8c, 0xcd, 0x36, 0xd5, 0x37, 0x7b, 0x25, 0xe8 } ,
{ 0x1f, 0xf6, 0x98, 0x74, 0xd3, 0xa1, 0x8b, 0x38, 0x97, 0xcb } ,
{ 0x39, 0xc2, 0x7f, 0x5e, 0x31, 0x13, 0x33, 0x0f, 0x2f, 0x8c } ,
{ 0xd9, 0xda, 0xbf, 0x32, 0x90, 0x99, 0x7e, 0xb8, 0x9c, 0xef } ,
{ 0x86, 0x1b, 0x51, 0xd2, 0xb4, 0x2c, 0x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x5b, 0xef, 0xda, 0x4e, 0x16, 0x40 } ,
{ 0xc5, 0x4b, 0x88, 0x35, 0xc6, 0x82, 0x63, 0xe3, 0xf0, 0x80 } ,
{ 0xf6, 0xa7, 0x0d, 0x51, 0xca, 0x65, 0x02, 0x29, 0x9a, 0x3d } ,
{ 0x0a, 0x5d, 0x6f, 0x5e, 0xda, 0x77, 0x49, 0xbf, 0x51, 0xd7 } ,
{ 0xcb, 0xf3, 0x95, 0x0b, 0x21, 0x20, 0x91, 0x96, 0x11, 0xff } ,
{ 0xba, 0x46, 0x04, 0x5c, 0x88, 0xbb, 0x62, 0x54, 0xd0, 0x93 } ,
{ 0xcb, 0x10, 0x2d, 0xd4, 0xbd, 0x72, 0x28, 0x70, 0xe9, 0xc2 } ,
{ 0xf5, 0xe5, 0x08, 0x53, 0xfa, 0xb5, 0x7b, 0x68, 0x1a, 0x98 } ,
{ 0x87, 0x58, 0x9a, 0x26, 0x15, 0x96, 0xfb, 0x02, 0x50, 0x77 } ,
{ 0xbb, 0x6e, 0xb8, 0x8e, 0xfb, 0x29, 0xe9, 0x93, 0x17, 0x7d } };
static u8 piv[32][8] = { /* initialization vectors 64bits */
{ 0x0c, 0x8e, 0xdb, 0x88, 0x01, 0xaa, 0x95, 0xb4 },
{ 0x36, 0x84, 0xba, 0xf4, 0x11, 0xdc, 0x47, 0xdf },
{ 0xab, 0x85, 0xb1, 0x6e, 0xa6, 0x4b, 0x3d, 0x53 },
{ 0x55, 0x28, 0x6c, 0x63, 0xd6, 0x18, 0x67, 0x0c },
{ 0x6c, 0xe5, 0xae, 0xc5, 0xf3, 0x55, 0xf1, 0xa5 },
{ 0x08, 0x5b, 0x40, 0xfe, 0x90, 0x14, 0x57, 0xe4 },
{ 0x3e, 0x3f, 0xe4, 0x63, 0x0c, 0xc5, 0xac, 0x9a },
{ 0xe0, 0xc5, 0x23, 0x1c, 0xe5, 0x6a, 0xed, 0x2d },
{ 0x23, 0x64, 0xca, 0xe9, 0x56, 0xff, 0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},
{ 0xd6, 0x75, 0xdd, 0xe0, 0x53, 0xa7, 0x03, 0xde },
{ 0x33, 0x2c, 0x01, 0x19, 0xee, 0xd4, 0xba, 0x17 } };
int msglen = 24;
u8 *output = (u8 *)calloc(msglen, 1);
u8 *message = (u8 *)calloc(msglen, 1);
u32 keysize = 128;
u32 ivsize = 128;
int i, j;
ECRYPT_ctx ctx ;
printf("\nGenerating test vector...\n");
for(j=0;j<32;j++) {
const u8 *key = pkey[j];
const u8 *iv = piv[j];
printf("\n\nKey: ");
for (i=0;i<16;i++) printf("%2.2x ",key[i]);
printf("\nIV : ");
for (i=0;i<16;i++) printf("%2.2x ",iv[i]);
ECRYPT_keysetup(&ctx, key, keysize, ivsize);
ECRYPT_ivsetup(&ctx, iv);
ECRYPT_process_bytes(0, &ctx, message, output, msglen);
printf("\nOut: ");
for (i=0; i<msglen; i++) printf("%2.2x ",output[i]);
}
free(message);
free(output);
return 0;
}
#elif DECIM_KEYSTREAM
int main(int argc, char **argv)
{
u32 msglen;
u8 *keystream;
u32 keysize = 128;
u32 ivsize = 128;
static u8 key[] = { 0xf6, 0x3d, 0xb4, 0x85, 0x4f, 0x85, 0x7d, 0xa9, 0x55, 0x43, 0xf6, 0x3d, 0xb4, 0x85, 0x4f, 0x85};
static u8 iv[] = { 0x0c, 0x8e, 0xdb, 0x88, 0x01, 0xaa, 0x95, 0xb4, 0x0c, 0x8e, 0xdb, 0x88, 0x01, 0xaa, 0x95, 0xb4};
ECRYPT_ctx ctx ;
if(argc < 2){
fprintf(stderr, "Please enter the amount of bytes to be generated\n");
exit(EXIT_FAILURE);
}
msglen = atoi(argv[1]);
keystream = (u8 *)calloc(msglen, 1);
ECRYPT_keysetup(&ctx, key, keysize, ivsize);
ECRYPT_ivsetup(&ctx, iv);
ECRYPT_keystream_bytes(&ctx, keystream, msglen);
int i;
for (i=0;i<msglen-1;i++)
printf("%x ",keystream[i]);
printf("\n");
return 0;
}
#endif