]> git.cryptolib.org Git - avr-crypto-lib.git/blob - norx/norx32.c
22e8837640bc6d926e986f9bbd0acd9c8e01b34a
[avr-crypto-lib.git] / norx / norx32.c
1 /* norx32.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2014 Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19
20
21 #include <inttypes.h>
22 #include <avr/pgmspace.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <memxor.h>
26 #include <norx32.h>
27
28 #include <stdio.h>
29 #include "cli.h"
30
31 #define R0  8
32 #define R1 11
33 #define R2 16
34 #define R3 31
35
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
46
47 #define WORD_SIZE 32
48
49 #define RATE_WORDS 10
50 #define CAPACITY_WORDS 6
51
52 #define RATE_BITS (RATE_WORDS * WORD_SIZE)
53 #define CAPACITY_BITS (CAPACITY_WORDS * WORD_SIZE)
54
55 #define RATE_BYTES (RATE_BITS / 8)
56 #define CAPACITY_BYTES (CAPACITY_BITS / 8)
57
58 #define TAG_HEADER    0x01
59 #define TAG_PAYLOAD   0x02
60 #define TAG_TRAILER   0x04
61 #define TAG_TAG       0x08
62 #define TAG_BRANCHING 0x10
63 #define TAG_MERGING   0x20
64
65
66 #define SET_TAG(ctx, t) do { \
67         ((uint8_t*)&(ctx)->s[15])[0] ^= (t); \
68     } while (0)
69
70 #define TOGGLE_BIT(buf, bit_addr) do { \
71         ((uint8_t*)(buf))[(bit_addr) / 8] ^= (1 << ((bit_addr) & 7)); \
72     } while (0)
73
74 #define TRUNCATE_BUFFER(buf, bits) do { \
75         if (bits & 7) { \
76             ((uint8_t*)(buf))[(bits) / 8] &= 0xff >> (7 - ((bits) & 7)); \
77         } \
78     } while (0)
79
80 #if 0
81 void norx32_dump(const norx32_ctx_t *ctx)
82 {
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");
89 }
90 #endif
91
92 static void phi(uint32_t *a, uint32_t *b)
93 {
94     *a = (*a ^ *b) ^ ((*a & *b) << 1);
95 }
96
97 static void xrot(uint32_t *a, uint32_t *b, uint8_t r)
98 {
99     uint32_t x;
100     x = *a ^ *b;
101     *a = (x << (32 - r)) | (x >> r);
102 }
103
104 #define A (v[3])
105 #define B (v[2])
106 #define C (v[1])
107 #define D (v[0])
108
109 static const uint8_t g2_table[8][4] PROGMEM = {
110         {0, 4,  8, 12},
111         {1, 5,  9, 13},
112         {2, 6, 10, 14},
113         {3, 7, 11, 15},
114         {0, 5, 10, 15},
115         {1, 6, 11, 12},
116         {2, 7,  8, 13},
117         {3, 4,  9, 14}
118 };
119
120 static void rho(uint32_t *(v[4]), uint8_t ra, uint8_t rb)
121 {
122     phi(A, B);
123     xrot(D, A, ra);
124     phi(C, D);
125     xrot(B, C, rb);
126 }
127
128 static void f32(norx32_ctx_t *ctx)
129 {
130     uint8_t i, j, rounds;
131     uint32_t *(v[4]);
132     const uint8_t *p;
133     rounds = ctx->r;
134     do {
135         p = &g2_table[0][0];
136         i = 8;
137         do {
138             j = 4;
139             do {
140                 --j;
141                 v[j] = &ctx->s[pgm_read_byte(p++)];
142             } while(j);
143             rho(v, R0, R1);
144             rho(v, R2, R3);
145         } while (--i);
146     } while (--rounds);
147 }
148
149 static const uint32_t init_state[] PROGMEM = {
150         U0,  0,  0, U1,
151          0,  0,  0,  0,
152         U2, U3, U4, U5,
153         U6, U7, U8 ^ (1l << 15), U9
154 };
155
156 static void norx32_process_block(
157     norx32_ctx_t *ctx,
158     const void *block,
159     uint8_t tag )
160 {
161     SET_TAG(ctx, tag);
162     f32(ctx);
163     memxor(ctx->s, block, RATE_BYTES);
164 }
165
166 static void norx32_process_last_block(
167     norx32_ctx_t *ctx,
168     void *out_block,
169     const void *block,
170     uint16_t length_b,
171     uint8_t tag )
172 {
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;
177         if (out_block) {
178             memcpy(out_block, ctx->s, RATE_BYTES);
179             out_block = (uint8_t*)out_block + RATE_BYTES;
180         }
181     }
182     SET_TAG(ctx, tag);
183     f32(ctx);
184     memxor(ctx->s, block, (length_b + 7) / 8);
185     if (out_block) {
186         memcpy(out_block, ctx->s, (length_b + 7) / 8);
187         out_block = (uint8_t*)out_block + (length_b + 7) / 8;
188 #ifndef NO_BIT_MODE
189         TRUNCATE_BUFFER(out_block, length_b);
190 #endif
191     }
192 #ifndef NO_BIT_MODE
193     TOGGLE_BIT(ctx->s, length_b);
194 #else
195     ((uint8_t*)ctx->s)[length_b / 8] ^= 1;
196 #endif
197     if (length_b == RATE_BITS - 1) {
198         SET_TAG(ctx, tag);
199         f32(ctx);
200     }
201 #ifndef NO_BIT_MODE
202     TOGGLE_BIT(ctx->s, RATE_BITS - 1);
203 #else
204     ((uint8_t*)ctx->s)[RATE_BYTES - 1] ^= 0x80;
205 #endif
206 }
207
208 /******************************************************************************/
209
210 int8_t norx32_init (
211     norx32_ctx_t *ctx,
212     const void* nonce,
213     const void* key,
214     uint8_t rounds,
215     uint8_t parallel,
216     uint16_t tag_size_b )
217 {
218     uint32_t v;
219     if (ctx == NULL || nonce == NULL || key == NULL) {
220         return -1;
221     }
222     if (tag_size_b > 320) {
223         return -1;
224     }
225     if (rounds < 1 || rounds > 63) {
226         return -1;
227     }
228     if (parallel != 1) {
229         return -2;
230     }
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;
236     v ^= tag_size_b;
237     ctx->s[14] ^= v;
238     ctx->d = parallel;
239     ctx->a = tag_size_b;
240     ctx->r = rounds;
241     f32(ctx);
242     return 0;
243 }
244
245 void norx32_finalize(norx32_ctx_t *ctx, void *tag)
246 {
247     SET_TAG(ctx, TAG_TAG);
248     f32(ctx);
249     f32(ctx);
250     if (tag) {
251         memcpy(tag, ctx->s, (ctx->a + 7) / 8);
252 #ifndef NO_BIT_MODE
253         TRUNCATE_BUFFER(tag, ctx->a);
254 #endif
255     }
256 }
257
258 void norx32_add_header_block(norx32_ctx_t *ctx, const void *block)
259 {
260     norx32_process_block(ctx, block, TAG_HEADER);
261 }
262
263 void norx32_add_header_last_block(
264     norx32_ctx_t *ctx,
265     const void *block,
266     uint16_t length_b )
267 {
268     norx32_process_last_block(ctx, NULL, block, length_b, TAG_HEADER);
269 }
270
271 void norx32_encrypt_block(norx32_ctx_t *ctx, void *dest, const void *src)
272 {
273     norx32_process_block(ctx, src, TAG_PAYLOAD);
274     if (dest) {
275         memcpy(dest, ctx->s, RATE_BYTES);
276     }
277 }
278
279 void norx32_encrypt_last_block(
280     norx32_ctx_t *ctx,
281     void *dest,
282     const void *src,
283     uint16_t length_b )
284 {
285     norx32_process_last_block(ctx, dest, src, length_b, TAG_PAYLOAD);
286 }
287
288 void norx32_add_trailer_block(norx32_ctx_t *ctx, const void *block)
289 {
290     norx32_process_block(ctx, block, TAG_TRAILER);
291 }
292
293 void norx32_add_trailer_last_block(
294     norx32_ctx_t *ctx,
295     const void *block,
296     uint16_t length_b )
297 {
298     norx32_process_last_block(ctx, NULL, block, length_b, TAG_TRAILER);
299 }
300
301 /******************************************************************************/
302
303 void norx32_default_simple (
304     void *data_dest,
305     void *tag_dest,
306     const void *key,
307     const void *nonce,
308     const void *header,
309     size_t header_length_B,
310     const void *data_src,
311     size_t data_length_B,
312     const void *trailer,
313     size_t trailer_length_B )
314 {
315     norx32_ctx_t ctx;
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);
319     }
320     if (data_src && data_length_B) {
321         norx32_encrypt_last_block(&ctx, data_dest, data_src, data_length_B * 8);
322     }
323     if (trailer && trailer_length_B) {
324         norx32_add_trailer_last_block(&ctx, trailer, trailer_length_B * 8);
325     }
326     norx32_finalize(&ctx, tag_dest);
327 }