1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
11
/* Implementation of the Noekeon block cipher by Tom St Denis */
16
const struct _cipher_descriptor noekeon_desc =
28
static const ulong32 RC[] = {
29
0x00000080UL, 0x0000001bUL, 0x00000036UL, 0x0000006cUL,
30
0x000000d8UL, 0x000000abUL, 0x0000004dUL, 0x0000009aUL,
31
0x0000002fUL, 0x0000005eUL, 0x000000bcUL, 0x00000063UL,
32
0x000000c6UL, 0x00000097UL, 0x00000035UL, 0x0000006aUL,
36
#define kTHETA(a, b, c, d) \
37
temp = a^c; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \
38
b ^= temp; d ^= temp; \
39
temp = b^d; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \
42
#define THETA(k, a, b, c, d) \
43
temp = a^c; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \
44
b ^= temp ^ k[1]; d ^= temp ^ k[3]; \
45
temp = b^d; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \
46
a ^= temp ^ k[0]; c ^= temp ^ k[2];
48
#define GAMMA(a, b, c, d) \
51
temp = d; d = a; a = temp;\
56
#define PI1(a, b, c, d) \
57
a = ROL(a, 1); c = ROL(c, 5); d = ROL(d, 2);
59
#define PI2(a, b, c, d) \
60
a = ROR(a, 1); c = ROR(c, 5); d = ROR(d, 2);
62
int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
67
_ARGCHK(skey != NULL);
70
return CRYPT_INVALID_KEYSIZE;
73
if (num_rounds != 16 && num_rounds != 0) {
74
return CRYPT_INVALID_ROUNDS;
77
LOAD32H(skey->noekeon.K[0],&key[0]);
78
LOAD32H(skey->noekeon.K[1],&key[4]);
79
LOAD32H(skey->noekeon.K[2],&key[8]);
80
LOAD32H(skey->noekeon.K[3],&key[12]);
82
LOAD32H(skey->noekeon.dK[0],&key[0]);
83
LOAD32H(skey->noekeon.dK[1],&key[4]);
84
LOAD32H(skey->noekeon.dK[2],&key[8]);
85
LOAD32H(skey->noekeon.dK[3],&key[12]);
87
kTHETA(skey->noekeon.dK[0], skey->noekeon.dK[1], skey->noekeon.dK[2], skey->noekeon.dK[3]);
93
static void _noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
95
void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
101
_ARGCHK(key != NULL);
105
LOAD32H(a,&pt[0]); LOAD32H(b,&pt[4]);
106
LOAD32H(c,&pt[8]); LOAD32H(d,&pt[12]);
110
THETA(key->noekeon.K, a,b,c,d); \
115
for (r = 0; r < 16; ++r) {
122
THETA(key->noekeon.K, a, b, c, d);
124
STORE32H(a,&ct[0]); STORE32H(b,&ct[4]);
125
STORE32H(c,&ct[8]); STORE32H(d,&ct[12]);
129
void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
131
_noekeon_ecb_encrypt(pt, ct, key);
132
burn_stack(sizeof(ulong32) * 5 + sizeof(int));
137
static void _noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
139
void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
142
ulong32 a,b,c,d, temp;
145
_ARGCHK(key != NULL);
149
LOAD32H(a,&ct[0]); LOAD32H(b,&ct[4]);
150
LOAD32H(c,&ct[8]); LOAD32H(d,&ct[12]);
154
THETA(key->noekeon.dK, a,b,c,d); \
160
for (r = 16; r > 0; --r) {
166
THETA(key->noekeon.dK, a,b,c,d);
168
STORE32H(a,&pt[0]); STORE32H(b, &pt[4]);
169
STORE32H(c,&pt[8]); STORE32H(d, &pt[12]);
173
void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
175
_noekeon_ecb_decrypt(ct, pt, key);
176
burn_stack(sizeof(ulong32) * 5 + sizeof(int));
180
int noekeon_test(void)
185
static const struct {
187
unsigned char key[16], pt[16], ct[16];
191
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
192
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
193
{ 0x18, 0xa6, 0xec, 0xe5, 0x28, 0xaa, 0x79, 0x73,
194
0x28, 0xb2, 0xc0, 0x91, 0xa0, 0x2f, 0x54, 0xc5}
198
unsigned char tmp[2][16];
201
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
202
zeromem(&key, sizeof(key));
203
if ((err = noekeon_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
207
noekeon_ecb_encrypt(tests[i].pt, tmp[0], &key);
208
noekeon_ecb_decrypt(tmp[0], tmp[1], &key);
209
if (memcmp(tmp[0], tests[i].ct, 16) || memcmp(tmp[1], tests[i].pt, 16)) {
211
printf("\n\nTest %d failed\n", i);
212
if (memcmp(tmp[0], tests[i].ct, 16)) {
214
for (i = 0; i < 16; i++) {
215
printf("%02x ", tmp[0][i]);
220
for (i = 0; i < 16; i++) {
221
printf("%02x ", tmp[1][i]);
226
return CRYPT_FAIL_TESTVECTOR;
229
/* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
230
for (y = 0; y < 16; y++) tmp[0][y] = 0;
231
for (y = 0; y < 1000; y++) noekeon_ecb_encrypt(tmp[0], tmp[0], &key);
232
for (y = 0; y < 1000; y++) noekeon_ecb_decrypt(tmp[0], tmp[0], &key);
233
for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
239
int noekeon_keysize(int *desired_keysize)
241
_ARGCHK(desired_keysize != NULL);
242
if (*desired_keysize < 16) {
243
return CRYPT_INVALID_KEYSIZE;
245
*desired_keysize = 16;