3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 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 * \email daniel.otte@rub.de
24 * \brief implementation of the MUGI key stream generator
25 * \license GPLv3 or later
31 #include <avr/pgmspace.h>
37 #include "test_src/cli.h" / * only for debugging * /
39 void dump_mugi_ctx(mugi_ctx_t* ctx){
41 cli_putstr_P(PSTR("\r\n== MUGI CTX DUMP==\r\n a:"));
42 cli_hexdump(&(ctx->a[0]), 8);
44 cli_hexdump(&(ctx->a[1]), 8);
46 cli_hexdump(&(ctx->a[2]), 8);
47 cli_putstr_P(PSTR("\r\n b: "));
49 cli_putstr_P(PSTR("\r\n "));
50 cli_hexdump(&(ctx->b[i*4+0]), 8);
52 cli_hexdump(&(ctx->b[i*4+1]), 8);
54 cli_hexdump(&(ctx->b[i*4+2]), 8);
56 cli_hexdump(&(ctx->b[i*4+3]), 8);
61 #define C0 0x08c9bcf367e6096all
62 #define C1 0x3ba7ca8485ae67bbll
63 #define C2 0x2bf894fe72f36e3cll
65 #define GF256MUL_2(a) (gf256mul(2, (a), 0x1b))
67 uint64_t changeendian64(uint64_t a){
69 r[0] = ((uint8_t*)&a)[7];
70 r[1] = ((uint8_t*)&a)[6];
71 r[2] = ((uint8_t*)&a)[5];
72 r[3] = ((uint8_t*)&a)[4];
73 r[4] = ((uint8_t*)&a)[3];
74 r[5] = ((uint8_t*)&a)[2];
75 r[6] = ((uint8_t*)&a)[1];
76 r[7] = ((uint8_t*)&a)[0];
77 return *((uint64_t*)r);
81 uint64_t rotl64(uint64_t a, uint8_t i){
90 uint64_t rotr64(uint64_t a, uint8_t i){
99 #define T(x) (((uint8_t*)&t)[(x)])
100 #define D(y) (((uint8_t*)dest)[(y)])
101 static void mugi_f(uint64_t* dest, uint64_t* a, uint64_t* b){
108 T(i) = pgm_read_byte(aes_sbox+T(i));
110 x = T(0) ^ T(1) ^ T(2) ^ T(3);
112 GF256MUL_2(T(0)^T(1))
116 GF256MUL_2(T(1)^T(2))
120 GF256MUL_2(T(2)^T(3))
124 GF256MUL_2(T(3)^T(0))
127 x = T(4) ^ T(5) ^ T(6) ^ T(7);
129 GF256MUL_2(T(4)^T(5))
133 GF256MUL_2(T(5)^T(6))
137 GF256MUL_2(T(6)^T(7))
141 GF256MUL_2(T(7)^T(4))
147 void mugi_rho(mugi_ctx_t* ctx){
150 ctx->a[1] = ctx->a[2];
151 ctx->a[2] = ctx->a[0];
153 mugi_f(&t, &(ctx->a[0]), &(ctx->b[4]));
155 bx = rotl64(ctx->b[10], 17);
156 mugi_f(&t, &(ctx->a[0]), &bx);
161 void mugi_rho_init(uint64_t* a){
167 mugi_f(&t, &(a[0]), NULL);
169 mugi_f(&t, &(a[0]), NULL);
174 void mugi_lambda(uint64_t* b, uint64_t *a){
178 for(i=15; i!=0; --i){
183 b[10] ^= rotl64(b[14], 32);
186 void mugi_init(const void* key, const void* iv, mugi_ctx_t* ctx){
189 memcpy(ctx->a, key, 128/8);
190 ctx->a[2] = rotl64(ctx->a[0], 7) ^ rotr64(ctx->a[1], 7) ^ C0;
192 mugi_rho_init(ctx->a);
193 ctx->b[15-i] = ctx->a[0];
195 ctx->a[0] ^= ((uint64_t*)iv)[0];
196 ctx->a[1] ^= ((uint64_t*)iv)[1];
197 ctx->a[2] ^= rotl64(((uint64_t*)iv)[0], 7) ^ rotr64(((uint64_t*)iv)[1], 7) ^ C0;
199 mugi_rho_init(ctx->a);
204 mugi_lambda(ctx->b, &a0);
209 uint64_t mugi_gen(mugi_ctx_t* ctx){
213 mugi_lambda(ctx->b, &r);