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 CAPACITY_WORDS 6
52 #define RATE_BITS (RATE_WORDS * WORD_SIZE)
53 #define CAPACITY_BITS (CAPACITY_WORDS * WORD_SIZE)
55 #define RATE_BYTES (RATE_BITS / 8)
56 #define CAPACITY_BYTES (CAPACITY_BITS / 8)
58 #define TAG_HEADER 0x01
59 #define TAG_PAYLOAD 0x02
60 #define TAG_TRAILER 0x04
62 #define TAG_BRANCHING 0x10
63 #define TAG_MERGING 0x20
66 #define SET_TAG(ctx, t) do { \
67 ((uint8_t*)&(ctx)->s[15])[0] ^= (t); \
70 #define TOGGLE_BIT(buf, bit_addr) do { \
71 ((uint8_t*)(buf))[(bit_addr) / 8] ^= (1 << ((bit_addr) & 7)); \
74 #define TRUNCATE_BUFFER(buf, bits) do { \
76 ((uint8_t*)(buf))[(bits) / 8] &= 0xff >> (7 - ((bits) & 7)); \
81 void norx32_dump(const norx32_ctx_t *ctx)
83 printf("\n--- DUMP STATE ---");
84 printf("\n\t%08lX %08lX %08lX %08lX", ctx->s[ 0], ctx->s[ 1], ctx->s[ 2], ctx->s[ 3]);
85 printf("\n\t%08lX %08lX %08lX %08lX", ctx->s[ 4], ctx->s[ 5], ctx->s[ 6], ctx->s[ 7]);
86 printf("\n\t%08lX %08lX %08lX %08lX", ctx->s[ 8], ctx->s[ 9], ctx->s[10], ctx->s[11]);
87 printf("\n\t%08lX %08lX %08lX %08lX", ctx->s[12], ctx->s[13], ctx->s[14], ctx->s[15]);
88 printf("\n--- END ---\n");
92 static void phi(uint32_t *a, uint32_t *b)
94 *a = (*a ^ *b) ^ ((*a & *b) << 1);
97 static void xrot(uint32_t *a, uint32_t *b, uint8_t r)
101 *a = (x << (32 - r)) | (x >> r);
109 static const uint8_t g2_table[8][4] PROGMEM = {
120 static void rho(uint32_t *(v[4]), uint8_t ra, uint8_t rb)
128 static void f32(norx32_ctx_t *ctx)
130 uint8_t i, j, rounds;
141 v[j] = &ctx->s[pgm_read_byte(p++)];
149 static const uint32_t init_state[] PROGMEM = {
153 U6, U7, U8 ^ (1l << 15), U9
156 static void norx32_process_block(
163 memxor(ctx->s, block, RATE_BYTES);
166 static void norx32_process_last_block(
173 while (length_b >= RATE_BITS) {
174 norx32_process_block(ctx, block, tag);
175 block = (uint8_t*)block + RATE_BYTES;
176 length_b -= RATE_BITS;
178 memcpy(out_block, ctx->s, RATE_BYTES);
179 out_block = (uint8_t*)out_block + RATE_BYTES;
184 memxor(ctx->s, block, (length_b + 7) / 8);
186 memcpy(out_block, ctx->s, (length_b + 7) / 8);
187 out_block = (uint8_t*)out_block + (length_b + 7) / 8;
189 TRUNCATE_BUFFER(out_block, length_b);
193 TOGGLE_BIT(ctx->s, length_b);
195 ((uint8_t*)ctx->s)[length_b / 8] ^= 1;
197 if (length_b == RATE_BITS - 1) {
202 TOGGLE_BIT(ctx->s, RATE_BITS - 1);
204 ((uint8_t*)ctx->s)[RATE_BYTES - 1] ^= 0x80;
208 /******************************************************************************/
216 uint16_t tag_size_b )
219 if (ctx == NULL || nonce == NULL || key == NULL) {
222 if (tag_size_b > 320) {
225 if (rounds < 1 || rounds > 63) {
231 memcpy_P(ctx->s, init_state, sizeof(ctx->s));
232 memcpy(&ctx->s[1], nonce, 2 * sizeof(ctx->s[1]));
233 memcpy(&ctx->s[4], key, 4 * sizeof(ctx->s[4]));
234 v = ((uint32_t)rounds) << 26;
235 v ^= ((uint32_t)parallel) << 18;
245 void norx32_finalize(norx32_ctx_t *ctx, void *tag)
247 SET_TAG(ctx, TAG_TAG);
251 memcpy(tag, ctx->s, (ctx->a + 7) / 8);
253 TRUNCATE_BUFFER(tag, ctx->a);
258 void norx32_add_header_block(norx32_ctx_t *ctx, const void *block)
260 norx32_process_block(ctx, block, TAG_HEADER);
263 void norx32_add_header_last_block(
268 norx32_process_last_block(ctx, NULL, block, length_b, TAG_HEADER);
271 void norx32_encrypt_block(norx32_ctx_t *ctx, void *dest, const void *src)
273 norx32_process_block(ctx, src, TAG_PAYLOAD);
275 memcpy(dest, ctx->s, RATE_BYTES);
279 void norx32_encrypt_last_block(
285 norx32_process_last_block(ctx, dest, src, length_b, TAG_PAYLOAD);
288 void norx32_add_trailer_block(norx32_ctx_t *ctx, const void *block)
290 norx32_process_block(ctx, block, TAG_TRAILER);
293 void norx32_add_trailer_last_block(
298 norx32_process_last_block(ctx, NULL, block, length_b, TAG_TRAILER);
301 /******************************************************************************/
303 void norx32_default_simple (
309 size_t header_length_B,
310 const void *data_src,
311 size_t data_length_B,
313 size_t trailer_length_B )
316 norx32_init(&ctx, nonce, key, 4, 1, 4 * WORD_SIZE);
317 if (header && header_length_B) {
318 norx32_add_header_last_block(&ctx, header, header_length_B * 8);
320 if (data_src && data_length_B) {
321 norx32_encrypt_last_block(&ctx, data_dest, data_src, data_length_B * 8);
323 if (trailer && trailer_length_B) {
324 norx32_add_trailer_last_block(&ctx, trailer, trailer_length_B * 8);
326 norx32_finalize(&ctx, tag_dest);