2 * Copyright (C) 2011 Gregor Pintar <grpintar@gmail.com>
4 * Permission is granted to deal in this work without any restriction,
5 * including unlimited rights to use, publicly perform, publish,
6 * reproduce, relicence, modify, merge, and/or distribute in any form,
7 * for any purpose, with or without fee, and by any means.
9 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind,
10 * to the utmost extent permitted by applicable law. In no event
11 * shall a licensor, author or contributor be held liable for any
12 * issues arising in any way out of dealing in the work.
19 #include <kripto/cast.h>
20 #include <kripto/loadstore.h>
21 #include <kripto/rotate.h>
22 #include <kripto/memwipe.h>
23 #include <kripto/block.h>
24 #include <kripto/desc/block.h>
25 #include <kripto/object/block.h>
27 #include <kripto/block/noekeon.h>
31 struct kripto_block_object obj
;
37 static const uint8_t rc
[34] =
39 0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A,
40 0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A,
41 0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39,
42 0x72, 0xE4, 0xD3, 0xBD, 0x61, 0xC2, 0x9F, 0x25,
46 #define THETA(X0, X1, X2, X3, K0, K1, K2, K3) \
49 T ^= ROL32(T, 8) ^ ROR32(T, 8); \
52 X0 ^= K0; X1 ^= K1; X2 ^= K2; X3 ^= K3; \
54 T ^= ROL32(T, 8) ^ ROR32(T, 8); \
59 #define GAMMA(X0, X1, X2, X3) \
63 T = X3; X3 = X0; X0 = T; \
69 #define PI1(X1, X2, X3) \
76 #define PI2(X1, X2, X3) \
83 static void noekeon_encrypt
85 const kripto_block
*s
,
97 x0
= LOAD32B(CU8(pt
));
98 x1
= LOAD32B(CU8(pt
) + 4);
99 x2
= LOAD32B(CU8(pt
) + 8);
100 x3
= LOAD32B(CU8(pt
) + 12);
102 for(r
= 0; r
< s
->rounds
; r
++)
105 THETA(x0
, x1
, x2
, x3
, s
->k
[0], s
->k
[1], s
->k
[2], s
->k
[3]);
107 GAMMA(x0
, x1
, x2
, x3
);
111 THETA(x0
, x1
, x2
, x3
, s
->k
[0], s
->k
[1], s
->k
[2], s
->k
[3]);
113 STORE32B(x0
, U8(ct
));
114 STORE32B(x1
, U8(ct
) + 4);
115 STORE32B(x2
, U8(ct
) + 8);
116 STORE32B(x3
, U8(ct
) + 12);
119 static void noekeon_decrypt
121 const kripto_block
*s
,
133 x0
= LOAD32B(CU8(ct
));
134 x1
= LOAD32B(CU8(ct
) + 4);
135 x2
= LOAD32B(CU8(ct
) + 8);
136 x3
= LOAD32B(CU8(ct
) + 12);
138 for(r
= s
->rounds
; r
; r
--)
140 THETA(x0
, x1
, x2
, x3
, s
->dk
[0], s
->dk
[1], s
->dk
[2], s
->dk
[3]);
143 GAMMA(x0
, x1
, x2
, x3
);
146 THETA(x0
, x1
, x2
, x3
, s
->dk
[0], s
->dk
[1], s
->dk
[2], s
->dk
[3]);
149 STORE32B(x0
, U8(pt
));
150 STORE32B(x1
, U8(pt
) + 4);
151 STORE32B(x2
, U8(pt
) + 8);
152 STORE32B(x3
, U8(pt
) + 12);
155 static void noekeon_setup
165 if(!s
->rounds
) s
->rounds
= 16;
168 s
->k
[0] = s
->k
[1] = s
->k
[2] = s
->k
[3] = 0;
170 for(i
= 0; i
< key_len
; i
++)
171 s
->k
[i
>> 2] |= key
[i
] << (24 - ((i
& 3) << 3));
178 THETA(s
->dk
[0], s
->dk
[1], s
->dk
[2], s
->dk
[3], 0, 0, 0, 0);
181 static kripto_block
*noekeon_create
190 s
= malloc(sizeof(kripto_block
));
193 s
->obj
.desc
= kripto_block_noekeon
;
196 noekeon_setup(s
, key
, key_len
);
201 static kripto_block
*noekeon_recreate
210 noekeon_setup(s
, key
, key_len
);
215 static void noekeon_destroy(kripto_block
*s
)
217 kripto_memwipe(s
, sizeof(kripto_block
));
221 static const kripto_block_desc noekeon
=
233 const kripto_block_desc
*const kripto_block_noekeon
= &noekeon
;