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/>.
23 * \brief SEED parts in C for AVR
29 #include <avr/pgmspace.h>
31 #include "seed_sbox.h"
37 uint32_t g_function(uint32_t x);
38 /******************************************************************************/
41 void changeendian32(uint32_t * a){
42 *a = (*a & 0x000000FF) << 24 |
43 (*a & 0x0000FF00) << 8 |
44 (*a & 0x00FF0000) >> 8 |
45 (*a & 0xFF000000) >> 24;
48 /******************************************************************************/
50 uint32_t bigendian_sum32(uint32_t a, uint32_t b){
58 /******************************************************************************/
60 uint32_t bigendian_sub32(uint32_t a, uint32_t b){
68 /******************************************************************************/
70 uint64_t bigendian_rotl8_64(uint64_t a){
73 a = (a<<8) | (a>>(64-8));
76 a = (a>>8) | (a<<(64-8));
80 /******************************************************************************/
82 uint64_t bigendian_rotr8_64(uint64_t a){
85 a = (a>>8) | (a<<(64-8));
88 a = (a<<8) | (a>>(64-8));
92 /******************************************************************************/
94 uint64_t f_function(const uint64_t* a, uint32_t k0, uint32_t k1){
97 c = *a & 0x00000000FFFFFFFFLL;
98 d = (*a>>32) & 0x00000000FFFFFFFFLL;
103 c = bigendian_sum32(c,d);
105 d = bigendian_sum32(c,d);
107 c = bigendian_sum32(c,d);
108 return ((uint64_t)d << 32) | c;
111 /******************************************************************************/
117 #define X3 (((uint8_t*)(&x))[0])
118 #define X2 (((uint8_t*)(&x))[1])
119 #define X1 (((uint8_t*)(&x))[2])
120 #define X0 (((uint8_t*)(&x))[3])
122 #define Z3 (((uint8_t*)(&z))[0])
123 #define Z2 (((uint8_t*)(&z))[1])
124 #define Z1 (((uint8_t*)(&z))[2])
125 #define Z0 (((uint8_t*)(&z))[3])
128 uint32_t g_function(uint32_t x){
130 /* sbox substitution */
131 X3 = pgm_read_byte(&(seed_sbox2[X3]));
132 X2 = pgm_read_byte(&(seed_sbox1[X2]));
133 X1 = pgm_read_byte(&(seed_sbox2[X1]));
134 X0 = pgm_read_byte(&(seed_sbox1[X0]));
135 /* now the permutation */
136 Z0 = (X0 & M0) ^ (X1 & M1) ^ (X2 & M2) ^ (X3 & M3);
137 Z1 = (X0 & M1) ^ (X1 & M2) ^ (X2 & M3) ^ (X3 & M0);
138 Z2 = (X0 & M2) ^ (X1 & M3) ^ (X2 & M0) ^ (X3 & M1);
139 Z3 = (X0 & M3) ^ (X1 & M0) ^ (X2 & M1) ^ (X3 & M2);
142 /******************************************************************************/
147 keypair_t getnextkeys(uint32_t *keystate, uint8_t curround){
153 /* ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
154 ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
155 ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
156 ret.k0 = bigendian_sub32(ret.k0, pgm_read_dword(&(seed_kc[curround])));
157 ret.k0 = g_function(ret.k0);
158 ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
159 ret.k1 = bigendian_sum32(ret.k1, pgm_read_dword(&(seed_kc[curround])));
160 ret.k1 = g_function(ret.k1);
163 /* odd round (1,3,5, ...) */
164 ((uint64_t*)keystate)[1] = bigendian_rotl8_64( ((uint64_t*)keystate)[1] );
166 /* even round (0,2,4, ...) */
167 ((uint64_t*)keystate)[0] = bigendian_rotr8_64(((uint64_t*)keystate)[0]);
174 /******************************************************************************/
176 keypair_t getprevkeys(uint32_t *keystate, uint8_t curround){
183 /* odd round (1,3,5, ..., 15) */
184 ((uint64_t*)keystate)[1] = bigendian_rotr8_64( ((uint64_t*)keystate)[1] );
186 /* even round (0,2,4, ..., 14) */
187 ((uint64_t*)keystate)[0] = bigendian_rotl8_64(((uint64_t*)keystate)[0]);
189 /* ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
190 ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
191 ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
192 ret.k0 = bigendian_sub32(ret.k0, pgm_read_dword(&(seed_kc[curround])));
193 ret.k0 = g_function(ret.k0);
194 ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
195 ret.k1 = bigendian_sum32(ret.k1, pgm_read_dword(&(seed_kc[curround])));
196 ret.k1 = g_function(ret.k1);
201 /******************************************************************************/
207 /******************************************************************************/
209 void seed_init(const void * key, seed_ctx_t * ctx){
210 memcpy(ctx->k, key, 128/8);
213 /******************************************************************************/
215 #define L (((uint64_t*)buffer)[0])
216 #define R (((uint64_t*)buffer)[1])
218 void seed_enc(void * buffer, const seed_ctx_t * ctx){
222 k = getnextkeys(((seed_ctx_t*)ctx)->k, 2*r);
224 DEBUG_S("\r\n\tDBG ka,0: "); cli_hexdump(&k.k0, 4);
225 DEBUG_S("\r\n\tDBG ka,1: "); cli_hexdump(&k.k1, 4);
226 DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+0, 8);
227 DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+8, 8);
229 L ^= f_function(&R,k.k0,k.k1);
231 k = getnextkeys(((seed_ctx_t*)ctx)->k, 2*r+1);
233 DEBUG_S("\r\n\tDBG kb,0: "); cli_hexdump(&k.k0, 4);
234 DEBUG_S("\r\n\tDBG kb,1: "); cli_hexdump(&k.k1, 4);
235 DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+8, 8);
236 DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+0, 8);
238 R ^= f_function(&L,k.k0,k.k1);
240 /* just an exchange without temp. variable */
246 /******************************************************************************/
248 #define L (((uint64_t*)buffer)[0])
249 #define R (((uint64_t*)buffer)[1])
251 void seed_dec(void * buffer, seed_ctx_t * ctx){
255 k = getprevkeys(((seed_ctx_t*)ctx)->k, 2*r+1);
257 DEBUG_S("\r\n\tDBG ka,0: "); cli_hexdump(&k.k0, 4);
258 DEBUG_S("\r\n\tDBG ka,1: "); cli_hexdump(&k.k1, 4);
259 DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+0, 8);
260 DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+8, 8);
262 L ^= f_function(&R,k.k0,k.k1);
264 k = getprevkeys(((seed_ctx_t*)ctx)->k, 2*r+0);
266 DEBUG_S("\r\n\tDBG kb,0: "); cli_hexdump(&k.k0, 4);
267 DEBUG_S("\r\n\tDBG kb,1: "); cli_hexdump(&k.k1, 4);
268 DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+8, 8);
269 DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+0, 8);
271 R ^= f_function(&L,k.k0,k.k1);
273 /* just an exchange without temp. variable */