2 * Written in 2011 by Gregor Pintar <grpintar@gmail.com>
4 * To the extent possible under law, the author(s) have dedicated
5 * all copyright and related and neighboring rights to this software
6 * to the public domain worldwide.
8 * This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication.
11 * If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
18 #include <kripto/cast.h>
19 #include <kripto/loadstore.h>
20 #include <kripto/rotate.h>
21 #include <kripto/memwipe.h>
22 #include <kripto/block.h>
23 #include <kripto/desc/block.h>
24 #include <kripto/object/block.h>
26 #include <kripto/block/noekeon.h>
30 struct kripto_block_object obj
;
36 static const uint8_t rc
[34] =
38 0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A,
39 0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A,
40 0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39,
41 0x72, 0xE4, 0xD3, 0xBD, 0x61, 0xC2, 0x9F, 0x25,
45 #define THETA(X0, X1, X2, X3, K0, K1, K2, K3) \
48 T ^= ROL32_08(T) ^ ROR32_08(T); \
51 X0 ^= K0; X1 ^= K1; X2 ^= K2; X3 ^= K3; \
53 T ^= ROL32_08(T) ^ ROR32_08(T); \
58 #define GAMMA(X0, X1, X2, X3) \
62 T = X3; X3 = X0; X0 = T; \
68 #define PI1(X1, X2, X3) \
75 #define PI2(X1, X2, X3) \
82 static void noekeon_encrypt
84 const kripto_block
*s
,
96 x0
= LOAD32B(CU8(pt
));
97 x1
= LOAD32B(CU8(pt
) + 4);
98 x2
= LOAD32B(CU8(pt
) + 8);
99 x3
= LOAD32B(CU8(pt
) + 12);
101 for(r
= 0; r
< s
->rounds
; r
++)
104 THETA(x0
, x1
, x2
, x3
, s
->k
[0], s
->k
[1], s
->k
[2], s
->k
[3]);
106 GAMMA(x0
, x1
, x2
, x3
);
110 THETA(x0
, x1
, x2
, x3
, s
->k
[0], s
->k
[1], s
->k
[2], s
->k
[3]);
112 STORE32B(x0
, U8(ct
));
113 STORE32B(x1
, U8(ct
) + 4);
114 STORE32B(x2
, U8(ct
) + 8);
115 STORE32B(x3
, U8(ct
) + 12);
118 static void noekeon_decrypt
120 const kripto_block
*s
,
132 x0
= LOAD32B(CU8(ct
));
133 x1
= LOAD32B(CU8(ct
) + 4);
134 x2
= LOAD32B(CU8(ct
) + 8);
135 x3
= LOAD32B(CU8(ct
) + 12);
137 for(r
= s
->rounds
; r
; r
--)
139 THETA(x0
, x1
, x2
, x3
, s
->dk
[0], s
->dk
[1], s
->dk
[2], s
->dk
[3]);
142 GAMMA(x0
, x1
, x2
, x3
);
145 THETA(x0
, x1
, x2
, x3
, s
->dk
[0], s
->dk
[1], s
->dk
[2], s
->dk
[3]);
148 STORE32B(x0
, U8(pt
));
149 STORE32B(x1
, U8(pt
) + 4);
150 STORE32B(x2
, U8(pt
) + 8);
151 STORE32B(x3
, U8(pt
) + 12);
154 static void noekeon_setup
164 if(!s
->rounds
) s
->rounds
= 16;
167 s
->k
[0] = s
->k
[1] = s
->k
[2] = s
->k
[3] = 0;
169 for(i
= 0; i
< key_len
; i
++)
170 s
->k
[i
>> 2] |= key
[i
] << (24 - ((i
& 3) << 3));
177 THETA(s
->dk
[0], s
->dk
[1], s
->dk
[2], s
->dk
[3], 0, 0, 0, 0);
180 static kripto_block
*noekeon_create
189 s
= malloc(sizeof(kripto_block
));
192 s
->obj
.desc
= kripto_block_noekeon
;
195 noekeon_setup(s
, key
, key_len
);
200 static kripto_block
*noekeon_recreate
209 noekeon_setup(s
, key
, key_len
);
214 static void noekeon_destroy(kripto_block
*s
)
216 kripto_memwipe(s
, sizeof(kripto_block
));
220 static const kripto_block_desc noekeon
=
233 const kripto_block_desc
*const kripto_block_noekeon
= &noekeon
;