3 This file is part of the Crypto-avr-lib/microcrypt-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/>.
23 * \brief SEED parts in C for AVR
29 #include <avr/pgmspace.h>
31 #include "seed_sbox.h"
36 static uint64_t f_function(uint64_t a, uint32_t k0, uint32_t k1);
37 uint32_t g_function(uint32_t x);
39 uint32_t bigendian_sum32(uint32_t a, uint32_t b);
40 uint32_t bigendian_sub32(uint32_t a, uint32_t b);
42 /******************************************************************************/
44 uint64_t bigendian_rotl8_64(uint64_t a){
47 a = (a<<8) | (a>>(64-8));
50 a = (a>>8) | (a<<(64-8));
54 /******************************************************************************/
56 uint64_t bigendian_rotr8_64(uint64_t a){
59 a = (a>>8) | (a<<(64-8));
62 a = (a<<8) | (a>>(64-8));
66 /******************************************************************************/
68 uint64_t f_function(uint64_t a, uint32_t k0, uint32_t k1){
71 c = a & 0x00000000FFFFFFFFLL;
72 d = (a>>32) & 0x00000000FFFFFFFFLL;
77 c = bigendian_sum32(c,d);
79 d = bigendian_sum32(c,d);
81 c = bigendian_sum32(c,d);
82 a = ((uint64_t)d << 32) | c;
86 /******************************************************************************/
93 #define X3 (((uint8_t*)(&x))[0])
94 #define X2 (((uint8_t*)(&x))[1])
95 #define X1 (((uint8_t*)(&x))[2])
96 #define X0 (((uint8_t*)(&x))[3])
98 #define Z3 (((uint8_t*)(&z))[0])
99 #define Z2 (((uint8_t*)(&z))[1])
100 #define Z1 (((uint8_t*)(&z))[2])
101 #define Z0 (((uint8_t*)(&z))[3])
104 uint32_t g_function(uint32_t x){
106 /* sbox substitution */
107 X3 = pgm_read_byte(&(seed_sbox2[X3]));
108 X2 = pgm_read_byte(&(seed_sbox1[X2]));
109 X1 = pgm_read_byte(&(seed_sbox2[X1]));
110 X0 = pgm_read_byte(&(seed_sbox1[X0]));
111 /* now the permutation */
112 Z0 = (X0 & M0) ^ (X1 & M1) ^ (X2 & M2) ^ (X3 & M3);
113 Z1 = (X0 & M1) ^ (X1 & M2) ^ (X2 & M3) ^ (X3 & M0);
114 Z2 = (X0 & M2) ^ (X1 & M3) ^ (X2 & M0) ^ (X3 & M1);
115 Z3 = (X0 & M3) ^ (X1 & M0) ^ (X2 & M1) ^ (X3 & M2);
119 /******************************************************************************/
125 keypair_t getnextkeys(uint32_t *keystate, uint8_t curround){
131 /* ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
132 ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
133 ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
134 ret.k0 = bigendian_sub32(ret.k0, pgm_read_dword(&(seed_kc[curround])));
135 ret.k0 = g_function(ret.k0);
136 ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
137 ret.k1 = bigendian_sum32(ret.k1, pgm_read_dword(&(seed_kc[curround])));
138 ret.k1 = g_function(ret.k1);
141 /* odd round (1,3,5, ...) */
142 ((uint64_t*)keystate)[1] = bigendian_rotl8_64( ((uint64_t*)keystate)[1] );
144 /* even round (0,2,4, ...) */
145 ((uint64_t*)keystate)[0] = bigendian_rotr8_64(((uint64_t*)keystate)[0]);
152 /******************************************************************************/
154 keypair_t getprevkeys(uint32_t *keystate, uint8_t curround){
161 /* odd round (1,3,5, ..., 15) */
162 ((uint64_t*)keystate)[1] = bigendian_rotr8_64( ((uint64_t*)keystate)[1] );
164 /* even round (0,2,4, ..., 14) */
165 ((uint64_t*)keystate)[0] = bigendian_rotl8_64(((uint64_t*)keystate)[0]);
167 /* ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
168 ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
169 ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
170 ret.k0 = bigendian_sub32(ret.k0, pgm_read_dword(&(seed_kc[curround])));
171 ret.k0 = g_function(ret.k0);
172 ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
173 ret.k1 = bigendian_sum32(ret.k1, pgm_read_dword(&(seed_kc[curround])));
174 ret.k1 = g_function(ret.k1);
179 /******************************************************************************/
185 /******************************************************************************/
187 void seed_init(uint8_t * key, seed_ctx_t * ctx){
188 memcpy(ctx->k, key, 128/8);
191 /******************************************************************************/
193 #define L (((uint64_t*)buffer)[0])
194 #define R (((uint64_t*)buffer)[1])
196 void seed_enc(void * buffer, seed_ctx_t * ctx){
200 k = getnextkeys(ctx->k, 2*r);
202 DEBUG_S("\r\n\tDBG ka,0: "); uart_hexdump(&k.k0, 4);
203 DEBUG_S("\r\n\tDBG ka,1: "); uart_hexdump(&k.k1, 4);
204 DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+0, 8);
205 DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+8, 8);
207 L ^= f_function(R,k.k0,k.k1);
209 k = getnextkeys(ctx->k, 2*r+1);
211 DEBUG_S("\r\n\tDBG kb,0: "); uart_hexdump(&k.k0, 4);
212 DEBUG_S("\r\n\tDBG kb,1: "); uart_hexdump(&k.k1, 4);
213 DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+8, 8);
214 DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+0, 8);
216 R ^= f_function(L,k.k0,k.k1);
218 /* just an exchange without temp. variable */
224 /******************************************************************************/
226 #define L (((uint64_t*)buffer)[0])
227 #define R (((uint64_t*)buffer)[1])
229 void seed_dec(void * buffer, seed_ctx_t * ctx){
233 k = getprevkeys(ctx->k, 2*r+1);
235 DEBUG_S("\r\n\tDBG ka,0: "); uart_hexdump(&k.k0, 4);
236 DEBUG_S("\r\n\tDBG ka,1: "); uart_hexdump(&k.k1, 4);
237 DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+0, 8);
238 DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+8, 8);
240 L ^= f_function(R,k.k0,k.k1);
242 k = getprevkeys(ctx->k, 2*r+0);
244 DEBUG_S("\r\n\tDBG kb,0: "); uart_hexdump(&k.k0, 4);
245 DEBUG_S("\r\n\tDBG kb,1: "); uart_hexdump(&k.k1, 4);
246 DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+8, 8);
247 DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+0, 8);
249 R ^= f_function(L,k.k0,k.k1);
251 /* just an exchange without temp. variable */