1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 /**
11 @file noekeon.c
12 Implementation of the Noekeon block cipher by Tom St Denis
13 */
14 #include "tomcrypt_private.h"
15
16 #ifdef LTC_NOEKEON
17
18 const struct ltc_cipher_descriptor noekeon_desc =
19 {
20 "noekeon",
21 16,
22 16, 16, 16, 16,
23 &noekeon_setup,
24 &noekeon_ecb_encrypt,
25 &noekeon_ecb_decrypt,
26 &noekeon_test,
27 &noekeon_done,
28 &noekeon_keysize,
29 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
30 };
31
32 static const ulong32 RC[] = {
33 0x00000080UL, 0x0000001bUL, 0x00000036UL, 0x0000006cUL,
34 0x000000d8UL, 0x000000abUL, 0x0000004dUL, 0x0000009aUL,
35 0x0000002fUL, 0x0000005eUL, 0x000000bcUL, 0x00000063UL,
36 0x000000c6UL, 0x00000097UL, 0x00000035UL, 0x0000006aUL,
37 0x000000d4UL
38 };
39
40 #define kTHETA(a, b, c, d) \
41 temp = a^c; temp = temp ^ ROLc(temp, 8) ^ RORc(temp, 8); \
42 b ^= temp; d ^= temp; \
43 temp = b^d; temp = temp ^ ROLc(temp, 8) ^ RORc(temp, 8); \
44 a ^= temp; c ^= temp;
45
46 #define THETA(k, a, b, c, d) \
47 temp = a^c; temp = temp ^ ROLc(temp, 8) ^ RORc(temp, 8); \
48 b ^= temp ^ k[1]; d ^= temp ^ k[3]; \
49 temp = b^d; temp = temp ^ ROLc(temp, 8) ^ RORc(temp, 8); \
50 a ^= temp ^ k[0]; c ^= temp ^ k[2];
51
52 #define GAMMA(a, b, c, d) \
53 b ^= ~(d|c); \
54 a ^= c&b; \
55 temp = d; d = a; a = temp;\
56 c ^= a ^ b ^ d; \
57 b ^= ~(d|c); \
58 a ^= c&b;
59
60 #define PI1(a, b, c, d) \
61 b = ROLc(b, 1); c = ROLc(c, 5); d = ROLc(d, 2);
62
63 #define PI2(a, b, c, d) \
64 b = RORc(b, 1); c = RORc(c, 5); d = RORc(d, 2);
65
66 /**
67 Initialize the Noekeon block cipher
68 @param key The symmetric key you wish to pass
69 @param keylen The key length in bytes
70 @param num_rounds The number of rounds desired (0 for default)
71 @param skey The key in as scheduled by this function.
72 @return CRYPT_OK if successful
73 */
noekeon_setup(const unsigned char * key,int keylen,int num_rounds,symmetric_key * skey)74 int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
75 {
76 ulong32 temp;
77
78 LTC_ARGCHK(key != NULL);
79 LTC_ARGCHK(skey != NULL);
80
81 if (keylen != 16) {
82 return CRYPT_INVALID_KEYSIZE;
83 }
84
85 if (num_rounds != 16 && num_rounds != 0) {
86 return CRYPT_INVALID_ROUNDS;
87 }
88
89 LOAD32H(skey->noekeon.K[0],&key[0]);
90 LOAD32H(skey->noekeon.K[1],&key[4]);
91 LOAD32H(skey->noekeon.K[2],&key[8]);
92 LOAD32H(skey->noekeon.K[3],&key[12]);
93
94 LOAD32H(skey->noekeon.dK[0],&key[0]);
95 LOAD32H(skey->noekeon.dK[1],&key[4]);
96 LOAD32H(skey->noekeon.dK[2],&key[8]);
97 LOAD32H(skey->noekeon.dK[3],&key[12]);
98
99 kTHETA(skey->noekeon.dK[0], skey->noekeon.dK[1], skey->noekeon.dK[2], skey->noekeon.dK[3]);
100
101 return CRYPT_OK;
102 }
103
104 /**
105 Encrypts a block of text with Noekeon
106 @param pt The input plaintext (16 bytes)
107 @param ct The output ciphertext (16 bytes)
108 @param skey The key as scheduled
109 @return CRYPT_OK if successful
110 */
111 #ifdef LTC_CLEAN_STACK
_noekeon_ecb_encrypt(const unsigned char * pt,unsigned char * ct,const symmetric_key * skey)112 static int _noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
113 #else
114 int noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
115 #endif
116 {
117 ulong32 a,b,c,d,temp;
118 int r;
119
120 LTC_ARGCHK(skey != NULL);
121 LTC_ARGCHK(pt != NULL);
122 LTC_ARGCHK(ct != NULL);
123
124 LOAD32H(a,&pt[0]); LOAD32H(b,&pt[4]);
125 LOAD32H(c,&pt[8]); LOAD32H(d,&pt[12]);
126
127 #define ROUND(i) \
128 a ^= RC[i]; \
129 THETA(skey->noekeon.K, a,b,c,d); \
130 PI1(a,b,c,d); \
131 GAMMA(a,b,c,d); \
132 PI2(a,b,c,d);
133
134 for (r = 0; r < 16; ++r) {
135 ROUND(r);
136 }
137
138 #undef ROUND
139
140 a ^= RC[16];
141 THETA(skey->noekeon.K, a, b, c, d);
142
143 STORE32H(a,&ct[0]); STORE32H(b,&ct[4]);
144 STORE32H(c,&ct[8]); STORE32H(d,&ct[12]);
145
146 return CRYPT_OK;
147 }
148
149 #ifdef LTC_CLEAN_STACK
noekeon_ecb_encrypt(const unsigned char * pt,unsigned char * ct,const symmetric_key * skey)150 int noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
151 {
152 int err = _noekeon_ecb_encrypt(pt, ct, skey);
153 burn_stack(sizeof(ulong32) * 5 + sizeof(int));
154 return err;
155 }
156 #endif
157
158 /**
159 Decrypts a block of text with Noekeon
160 @param ct The input ciphertext (16 bytes)
161 @param pt The output plaintext (16 bytes)
162 @param skey The key as scheduled
163 @return CRYPT_OK if successful
164 */
165 #ifdef LTC_CLEAN_STACK
_noekeon_ecb_decrypt(const unsigned char * ct,unsigned char * pt,const symmetric_key * skey)166 static int _noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
167 #else
168 int noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
169 #endif
170 {
171 ulong32 a,b,c,d, temp;
172 int r;
173
174 LTC_ARGCHK(skey != NULL);
175 LTC_ARGCHK(pt != NULL);
176 LTC_ARGCHK(ct != NULL);
177
178 LOAD32H(a,&ct[0]); LOAD32H(b,&ct[4]);
179 LOAD32H(c,&ct[8]); LOAD32H(d,&ct[12]);
180
181
182 #define ROUND(i) \
183 THETA(skey->noekeon.dK, a,b,c,d); \
184 a ^= RC[i]; \
185 PI1(a,b,c,d); \
186 GAMMA(a,b,c,d); \
187 PI2(a,b,c,d);
188
189 for (r = 16; r > 0; --r) {
190 ROUND(r);
191 }
192
193 #undef ROUND
194
195 THETA(skey->noekeon.dK, a,b,c,d);
196 a ^= RC[0];
197 STORE32H(a,&pt[0]); STORE32H(b, &pt[4]);
198 STORE32H(c,&pt[8]); STORE32H(d, &pt[12]);
199 return CRYPT_OK;
200 }
201
202 #ifdef LTC_CLEAN_STACK
noekeon_ecb_decrypt(const unsigned char * ct,unsigned char * pt,const symmetric_key * skey)203 int noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
204 {
205 int err = _noekeon_ecb_decrypt(ct, pt, skey);
206 burn_stack(sizeof(ulong32) * 5 + sizeof(int));
207 return err;
208 }
209 #endif
210
211 /**
212 Performs a self-test of the Noekeon block cipher
213 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
214 */
noekeon_test(void)215 int noekeon_test(void)
216 {
217 #ifndef LTC_TEST
218 return CRYPT_NOP;
219 #else
220 static const struct {
221 int keylen;
222 unsigned char key[16], pt[16], ct[16];
223 } tests[] = {
224 {
225 16,
226 { 0xAA, 0x3C, 0x8C, 0x86, 0xD9, 0x8B, 0xF8, 0xBE, 0x21, 0xE0, 0x36, 0x09, 0x78, 0xFB, 0xE4, 0x90 },
227 { 0xE4, 0x96, 0x6C, 0xD3, 0x13, 0xA0, 0x6C, 0xAF, 0xD0, 0x23, 0xC9, 0xFD, 0x45, 0x32, 0x23, 0x16 },
228 { 0xA6, 0xEC, 0xB8, 0xA8, 0x61, 0xFD, 0x62, 0xD9, 0x13, 0x02, 0xFE, 0x9E, 0x47, 0x01, 0x3F, 0xC3 }
229 },
230 {
231 16,
232 { 0xED, 0x43, 0xD1, 0x87, 0x21, 0x7E, 0xE0, 0x97, 0x3D, 0x76, 0xC3, 0x37, 0x2E, 0x7D, 0xAE, 0xD3 },
233 { 0xE3, 0x38, 0x32, 0xCC, 0xF2, 0x2F, 0x2F, 0x0A, 0x4A, 0x8B, 0x8F, 0x18, 0x12, 0x20, 0x17, 0xD3 },
234 { 0x94, 0xA5, 0xDF, 0xF5, 0xAE, 0x1C, 0xBB, 0x22, 0xAD, 0xEB, 0xA7, 0x0D, 0xB7, 0x82, 0x90, 0xA0 }
235 },
236 {
237 16,
238 { 0x6F, 0xDC, 0x23, 0x38, 0xF2, 0x10, 0xFB, 0xD3, 0xC1, 0x8C, 0x02, 0xF6, 0xB4, 0x6A, 0xD5, 0xA8 },
239 { 0xDB, 0x29, 0xED, 0xB5, 0x5F, 0xB3, 0x60, 0x3A, 0x92, 0xA8, 0xEB, 0x9C, 0x6D, 0x9D, 0x3E, 0x8F },
240 { 0x78, 0xF3, 0x6F, 0xF8, 0x9E, 0xBB, 0x8C, 0x6A, 0xE8, 0x10, 0xF7, 0x00, 0x22, 0x15, 0x30, 0x3D }
241 },
242 {
243 16,
244 { 0x2C, 0x0C, 0x02, 0xEF, 0x6B, 0xC4, 0xF2, 0x0B, 0x2E, 0xB9, 0xE0, 0xBF, 0xD9, 0x36, 0xC2, 0x4E },
245 { 0x84, 0xE2, 0xFE, 0x64, 0xB1, 0xB9, 0xFE, 0x76, 0xA8, 0x3F, 0x45, 0xC7, 0x40, 0x7A, 0xAF, 0xEE },
246 { 0x2A, 0x08, 0xD6, 0xA2, 0x1C, 0x63, 0x08, 0xB0, 0xF8, 0xBC, 0xB3, 0xA1, 0x66, 0xF7, 0xAE, 0xCF }
247 },
248 {
249 16,
250 { 0x6F, 0x30, 0xF8, 0x9F, 0xDA, 0x6E, 0xA0, 0x91, 0x04, 0x0F, 0x6C, 0x8B, 0x7D, 0xF7, 0x2A, 0x4B },
251 { 0x65, 0xB6, 0xA6, 0xD0, 0x42, 0x14, 0x08, 0x60, 0x34, 0x8D, 0x37, 0x2F, 0x01, 0xF0, 0x46, 0xBE },
252 { 0x66, 0xAC, 0x0B, 0x62, 0x1D, 0x68, 0x11, 0xF5, 0x27, 0xB1, 0x13, 0x5D, 0xF3, 0x2A, 0xE9, 0x18 }
253 },
254 {
255 16,
256 { 0xCA, 0xA4, 0x16, 0xB7, 0x1C, 0x92, 0x2E, 0xAD, 0xEB, 0xA7, 0xDB, 0x69, 0x92, 0xCB, 0x35, 0xEF },
257 { 0x81, 0x6F, 0x8E, 0x4D, 0x96, 0xC6, 0xB3, 0x67, 0x83, 0xF5, 0x63, 0xC7, 0x20, 0x6D, 0x40, 0x23 },
258 { 0x44, 0xF7, 0x63, 0x62, 0xF0, 0x43, 0xBB, 0x67, 0x4A, 0x75, 0x12, 0x42, 0x46, 0x29, 0x28, 0x19 }
259 },
260 {
261 16,
262 { 0x6B, 0xCF, 0x22, 0x2F, 0xE0, 0x1B, 0xB0, 0xAA, 0xD8, 0x3C, 0x91, 0x99, 0x18, 0xB2, 0x28, 0xE8 },
263 { 0x7C, 0x37, 0xC7, 0xD0, 0xAC, 0x92, 0x29, 0xF1, 0x60, 0x82, 0x93, 0x89, 0xAA, 0x61, 0xAA, 0xA9 },
264 { 0xE5, 0x89, 0x1B, 0xB3, 0xFE, 0x8B, 0x0C, 0xA1, 0xA6, 0xC7, 0xBE, 0x12, 0x73, 0x0F, 0xC1, 0x19 }
265 },
266 {
267 16,
268 { 0xE6, 0xD0, 0xF1, 0x03, 0x2E, 0xDE, 0x70, 0x8D, 0xD8, 0x9E, 0x36, 0x5C, 0x05, 0x52, 0xE7, 0x0D },
269 { 0xE2, 0x42, 0xE7, 0x92, 0x0E, 0xF7, 0x82, 0xA2, 0xB8, 0x21, 0x8D, 0x26, 0xBA, 0x2D, 0xE6, 0x32 },
270 { 0x1E, 0xDD, 0x75, 0x22, 0xB9, 0x36, 0x8A, 0x0F, 0x32, 0xFD, 0xD4, 0x48, 0x65, 0x12, 0x5A, 0x2F }
271 }
272 };
273 symmetric_key key;
274 unsigned char tmp[2][16];
275 int err, i, y;
276
277 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
278 zeromem(&key, sizeof(key));
279 if ((err = noekeon_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
280 return err;
281 }
282
283 noekeon_ecb_encrypt(tests[i].pt, tmp[0], &key);
284 noekeon_ecb_decrypt(tmp[0], tmp[1], &key);
285 if (compare_testvector(tmp[0], 16, tests[i].ct, 16, "Noekeon Encrypt", i) ||
286 compare_testvector(tmp[1], 16, tests[i].pt, 16, "Noekeon Decrypt", i)) {
287 return CRYPT_FAIL_TESTVECTOR;
288 }
289
290 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
291 for (y = 0; y < 16; y++) tmp[0][y] = 0;
292 for (y = 0; y < 1000; y++) noekeon_ecb_encrypt(tmp[0], tmp[0], &key);
293 for (y = 0; y < 1000; y++) noekeon_ecb_decrypt(tmp[0], tmp[0], &key);
294 for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
295 }
296 return CRYPT_OK;
297 #endif
298 }
299
300 /** Terminate the context
301 @param skey The scheduled key
302 */
noekeon_done(symmetric_key * skey)303 void noekeon_done(symmetric_key *skey)
304 {
305 LTC_UNUSED_PARAM(skey);
306 }
307
308 /**
309 Gets suitable key size
310 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable.
311 @return CRYPT_OK if the input key size is acceptable.
312 */
noekeon_keysize(int * keysize)313 int noekeon_keysize(int *keysize)
314 {
315 LTC_ARGCHK(keysize != NULL);
316 if (*keysize < 16) {
317 return CRYPT_INVALID_KEYSIZE;
318 }
319 *keysize = 16;
320 return CRYPT_OK;
321 }
322
323 #endif
324
325
326 /* ref: $Format:%D$ */
327 /* git commit: $Format:%H$ */
328 /* commit time: $Format:%ai$ */
329