3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006-2014 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <avr/pgmspace.h>
36 #define U0 0x243f6a88l
37 #define U1 0x85a308d3l
38 #define U2 0x13198a2el
39 #define U3 0x03707344l
40 #define U4 0x254f537al
41 #define U5 0x38531d48l
42 #define U6 0x839c6E83l
43 #define U7 0xf97a3ae5l
44 #define U8 0x8c91d88cl
45 #define U9 0x11eafb59l
50 #define U0 0x886a3f24l
51 #define U1 0xd308a385l
52 #define U2 0x2e8a1913l
53 #define U3 0x44737003l
54 #define U4 0x7a534f25l
55 #define U5 0x481d5338l
56 #define U6 0x836e9c83l
57 #define U7 0xe53a7af9l
58 #define U8 0xxxxxxxxxx
59 #define U9 0x59fbea11l
66 #define CAPACITY_WORDS 6
68 #define RATE_BITS (RATE_WORDS * WORD_SIZE)
69 #define CAPACITY_BITS (CAPACITY_WORDS * WORD_SIZE)
71 #define RATE_BYTES (RATE_BITS / 8)
72 #define CAPACITY_BYTES (CAPACITY_BITS / 8)
74 #define TAG_HEADER 0x01
75 #define TAG_PAYLOAD 0x02
76 #define TAG_TRAILER 0x04
78 #define TAG_BRANCHING 0x10
79 #define TAG_MERGING 0x20
82 #define SET_TAG(ctx,t) do { ((uint8_t*)&(ctx)->s[15])[0] ^= (t); } while (0)
85 void norx32_dump(const norx32_ctx_t *ctx)
87 printf("\n--- DUMP STATE ---");
88 printf("\n\t%08lX %08lX %08lX %08lX", ctx->s[ 0], ctx->s[ 1], ctx->s[ 2], ctx->s[ 3]);
89 printf("\n\t%08lX %08lX %08lX %08lX", ctx->s[ 4], ctx->s[ 5], ctx->s[ 6], ctx->s[ 7]);
90 printf("\n\t%08lX %08lX %08lX %08lX", ctx->s[ 8], ctx->s[ 9], ctx->s[10], ctx->s[11]);
91 printf("\n\t%08lX %08lX %08lX %08lX", ctx->s[12], ctx->s[13], ctx->s[14], ctx->s[15]);
92 printf("\n--- END ---\n");
95 static void phi(uint32_t *(a[2]))
98 x = (*(a[0]) & *(a[1])) << 1;
99 *(a[0]) ^= *(a[1]) ^ x;
102 static void xrot(uint32_t *a, const uint32_t *b, uint8_t r)
106 *a = (x << (32 - r)) | (x >> r);
114 void g32(uint32_t *(a[4]))
126 static uint8_t g2_table[4][4] = {
133 void f32(norx32_ctx_t *ctx)
155 a[0] = &ctx->s[g2_table[i][0]];
156 a[1] = &ctx->s[g2_table[i][1]];
157 a[2] = &ctx->s[g2_table[i][2]];
158 a[3] = &ctx->s[g2_table[i][3]];
164 static const uint32_t init_state[] PROGMEM = {
168 U6, U7, U8 ^ (1l << 15), U9
171 static void norx32_process_block(
178 memxor(ctx->s, block, RATE_BYTES);
181 static void norx32_process_last_block(
188 while (length_b >= RATE_BITS) {
189 norx32_process_block(ctx, block, tag);
190 block = (uint8_t*)block + RATE_BYTES;
191 length_b -= RATE_BITS;
193 memcpy(out_block, ctx->s, RATE_BYTES);
194 out_block = (uint8_t*)out_block + RATE_BYTES;
199 memxor(ctx->s, block, (length_b + 7) / 8);
201 memcpy(out_block, ctx->s, (length_b + 7) / 8);
202 out_block = (uint8_t*)out_block + (length_b + 7) / 8;
203 if ((length_b & 7) != 0) {
204 ((uint8_t*)out_block)[length_b / 8 + 1] &= 0xff << (7 - (length_b & 7));
207 ((uint8_t*)ctx->s)[length_b / 8] ^= 1 << (length_b & 7);
208 if (length_b == RATE_BITS - 1) {
212 ((uint8_t*)ctx->s)[RATE_BYTES - 1] ^= 0x80;
216 /******************************************************************************/
227 if (ctx == NULL || nonce == NULL || key == NULL) {
230 if (tag_size_b > 320) {
233 if (rounds < 1 || rounds > 63) {
239 memcpy_P(ctx->s, init_state, sizeof(ctx->s));
240 memcpy(&ctx->s[1], nonce, 2 * sizeof(ctx->s[1]));
241 memcpy(&ctx->s[4], key, 4 * sizeof(ctx->s[4]));
242 v = ((uint32_t)rounds) << 26;
243 v ^= ((uint32_t)parallel) << 18;
253 void norx32_finalize(norx32_ctx_t *ctx, void *tag)
255 SET_TAG(ctx, TAG_TAG);
259 memcpy(tag, ctx->s, (ctx->a + 7) / 8);
263 void norx32_add_header_block(norx32_ctx_t *ctx, const void *block)
265 norx32_process_block(ctx, block, TAG_HEADER);
268 void norx32_add_header_last_block(
273 norx32_process_last_block(ctx, NULL, block, length_b, TAG_HEADER);
276 void norx32_encrypt_block(norx32_ctx_t *ctx, void *dest, const void *src)
278 norx32_process_block(ctx, src, TAG_PAYLOAD);
280 memcpy(dest, ctx->s, RATE_BYTES);
284 void norx32_encrypt_last_block(
290 norx32_process_last_block(ctx, dest, src, length_b, TAG_PAYLOAD);
293 void norx32_add_trailer_block(norx32_ctx_t *ctx, const void *block)
295 norx32_process_block(ctx, block, TAG_TRAILER);
298 void norx32_add_trailer_last_block(
303 norx32_process_last_block(ctx, NULL, block, length_b, TAG_TRAILER);
306 /******************************************************************************/
308 void norx32_default_simple (
314 size_t header_length_B,
315 const void *data_src,
316 size_t data_length_B,
318 size_t trailer_length_B )
321 norx32_init(&ctx, nonce, key, 4 * WORD_SIZE, 4, 1);
322 if (header && header_length_B) {
323 norx32_add_header_last_block(&ctx, header, header_length_B * 8);
325 if (data_src && data_length_B) {
326 norx32_encrypt_last_block(&ctx, data_dest, data_src, data_length_B * 8);
328 if (trailer && trailer_length_B) {
329 norx32_add_trailer_last_block(&ctx, trailer, trailer_length_B * 8);
331 norx32_finalize(&ctx, tag_dest);