]> git.cryptolib.org Git - avr-crypto-lib.git/blob - seed-stub.c
even more ASM-fun now only decryption needs C-Stub
[avr-crypto-lib.git] / seed-stub.c
1 /* seed.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  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  * \file        seed.c
21  * \author      Daniel Otte 
22  * \date        2007-06-1
23  * \brief       SEED parts in C for AVR
24  * \par License 
25  * GPL
26  * 
27  */
28 #include <stdint.h>
29 #include <avr/pgmspace.h>
30 #include <string.h>
31 #include "seed.h"
32 #include "uart.h"
33 #include "debug.h"
34
35
36
37 /******************************************************************************/
38 typedef struct {
39         uint32_t k0, k1;
40 } keypair_t;
41
42 uint64_t  seed_f_function(const uint64_t* a, uint32_t k0, uint32_t k1);
43 keypair_t seed_getnextkeys(uint32_t *keystate, uint8_t curround);
44 keypair_t seed_getprevkeys(uint32_t *keystate, uint8_t curround);
45
46 /******************************************************************************/
47 #if 0
48 #define L (((uint64_t*)buffer)[0])
49 #define R (((uint64_t*)buffer)[1])
50
51 void seed_enc(void * buffer, seed_ctx_t * ctx){
52         uint8_t r;
53         keypair_t k;
54         for(r=0; r<8; ++r){
55                         k = seed_getnextkeys(ctx->k, 2*r);
56 /*
57         DEBUG_S("\r\n\tDBG ka,0: "); uart_hexdump(&k.k0, 4);
58         DEBUG_S("\r\n\tDBG ka,1: "); uart_hexdump(&k.k1, 4);
59         DEBUG_S("\r\n\t DBG L:   "); uart_hexdump((uint8_t*)buffer+0, 8);
60         DEBUG_S("\r\n\t DBG R:   "); uart_hexdump((uint8_t*)buffer+8, 8);
61 */
62                         L ^= seed_f_function(&R,k.k0,k.k1);
63                         
64                         k = seed_getnextkeys(ctx->k, 2*r+1);
65 /*
66         DEBUG_S("\r\n\tDBG kb,0: "); uart_hexdump(&k.k0, 4);
67         DEBUG_S("\r\n\tDBG kb,1: "); uart_hexdump(&k.k1, 4);
68         DEBUG_S("\r\n\t DBG L:   "); uart_hexdump((uint8_t*)buffer+8, 8);
69         DEBUG_S("\r\n\t DBG R:   "); uart_hexdump((uint8_t*)buffer+0, 8);
70 */
71                         R ^= seed_f_function(&L,k.k0,k.k1);
72         }
73         /* just an exchange without temp. variable */
74         L ^= R;
75         R ^= L;
76         L ^= R;
77 }
78 #endif
79
80 /******************************************************************************/
81
82 #define L (((uint64_t*)buffer)[0])
83 #define R (((uint64_t*)buffer)[1])
84
85 void seed_dec(void * buffer, seed_ctx_t * ctx){
86         int8_t r;
87         keypair_t k;
88         for(r=7; r>=0; --r){
89                         k = seed_getprevkeys(ctx->k, 2*r+1);
90 /*
91         DEBUG_S("\r\n\tDBG ka,0: "); uart_hexdump(&k.k0, 4);
92         DEBUG_S("\r\n\tDBG ka,1: "); uart_hexdump(&k.k1, 4);
93         DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+0, 8);
94         DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+8, 8);
95 */
96                         L ^= seed_f_function(&R,k.k0,k.k1);
97                         
98                         k = seed_getprevkeys(ctx->k, 2*r+0);
99 /*
100         DEBUG_S("\r\n\tDBG kb,0: "); uart_hexdump(&k.k0, 4);
101         DEBUG_S("\r\n\tDBG kb,1: "); uart_hexdump(&k.k1, 4);
102         DEBUG_S("\r\n\t DBG L: "); uart_hexdump((uint8_t*)buffer+8, 8);
103         DEBUG_S("\r\n\t DBG R: "); uart_hexdump((uint8_t*)buffer+0, 8);
104 */
105                         R ^= seed_f_function(&L,k.k0,k.k1);
106         }
107         /* just an exchange without temp. variable */
108         L ^= R;
109         R ^= L;
110         L ^= R;
111 }
112
113
114
115
116
117
118
119
120