4 * email: daniel.otte@rub.de
11 #include <avr/pgmspace.h>
12 #include "mickey128.h"
15 RTAPS = { 0,4,5, 8,10,11,14 ,16,20, 25,30, 32,35,36,38,
16 42,43,46, 50,51,53,54,55, 56,57,60,61,62,63,
17 65,66,69, 73,74,76,79, 80,81,82,85,86, 90,91,92,95,
18 97,100,101, 105,106,107,108,109,111, 112,113,115,116,117,
19 127, 128,129,130,131,133,135, 136,137,140,142, 145,148,150,
22 0011.0001 0100.1101 0001.0001 0100.0010 0101.1001
23 0100.1100 1110.1100 1111.0011
24 0010.0110 1001.0110 0110.0111 1001.1100
25 0011.0010 1011.1110 0011.1011
26 1000.0000 1010.1111 0101.0011 0101.0010
29 1000110010110010100010000100001010011010001100100011011111001111011001
30 0001101001111001100011100101001100011111011101110000000001111101011100
33 1000.1100 1011.0010 1000.1000 0100.0010 1001.1010 0011.0010
34 0011.0111 1100.1111 0110.0100 0110.1001 1110.0110 0011.1001
35 0100.1100 0111.1101 1101.1100 0000.0001 1111.0101 1100.1010
39 uint8_t rtaps[] PROGMEM = {
40 0x31, 0x4D, 0x11, 0x42,
41 0x59, 0x4C, 0xEC, 0xF3,
42 0x26, 0x96, 0x67, 0x9C,
43 0x32, 0xBE, 0x3B, 0x80,
44 0xAF, 0x53, 0x52, 0x37
48 void memxor_P(void* d, PGM_VOID_P s, uint8_t length_B){
50 *((uint8_t*)d) ^= pgm_read_byte(s);
56 #define SHL0(a) c1=((a)>>7); (a)=(((a)<<1)|c0)
57 #define SHL1(a) c0=((a)>>7); (a)=(((a)<<1)|c1)
59 #define SHLX0(a) c1=((a)>>7); (a)^=(((a)<<1)|c0)
60 #define SHLX1(a) c0=((a)>>7); (a)^=(((a)<<1)|c1)
63 void clock_r(uint8_t* r, uint8_t ibit, uint8_t cbit){
64 uint8_t i,c0=0,c1=0; /* carry */
65 ibit ^= ((r[159/8])>>(159%8))&1; /* ibit is now the same as feedback_bit */
78 memxor_P(r, rtaps, 20);
82 /* comp0 (filling spaces with zero) (one at each side)
83 * 0101.1110 1111.0010 1101.0110 0101.1101
84 * 0101.0101 0000.1001 0010.0110 0111.1001
85 * 0110.0010 0111.0000 0000.0000 0111.1001
86 * 0011.0001 1101.1001 1010.1111 0011.0111
87 * 1011.1110 0000.0110 1011.1110 0000.1111
95 uint8_t comp0[] PROGMEM = {
96 0x5E, 0xF2, 0xD6, 0x5D,
97 0x55, 0x09, 0x26, 0x79,
98 0x62, 0x70, 0x00, 0x79,
99 0x31, 0xD9, 0xAF, 0x37,
100 0xBE, 0x06, 0xBE, 0x0F
104 /* comp1 (shifting one bit right to make calculation easier, so inserting two zeros)
105 * 0110.0000 0011.1110 0011.0010 1111.1010
106 * 0011.0000 0111.1001 0110.1100 1111.1101
107 * 1100.0001 1000.0111 0000.0001 1111.1000
108 * 1000.1010 1100.0110 1100.0001 1100.1100
109 * 0110.1010 1011.0111 1110.1000 1111.1111
118 uint8_t comp1[] PROGMEM = {
119 0x60, 0x3E, 0x32, 0xFA, 0x30, 0x79, 0x6C, 0xFD, 0xC1, 0x87,
120 0x01, 0xF8, 0x8A, 0xC6, 0xC1, 0xCC, 0x6A, 0xB7, 0xE8, 0xFF
124 * 0000.1100 1111.1000 1001.1000 1011.1110
125 * 0001.1001 0011.1100 0110.1101 0111.1111
126 * 0000.0111 1100.0011 0000.0000 0011.1110
127 * 1010.0010 1100.0111 0000.0110 0110.0110
128 * 1010.1101 1101.1010 0010.1111 1111.1110
137 uint8_t comp1[] PROGMEM = {
138 0x0C, 0xF8, 0x98, 0xBE, 0x19, 0x3C, 0x6D, 0x7F, 0x07, 0xC3,
139 0x00, 0x3E, 0xA2, 0xC7, 0x06, 0x66, 0xAD, 0xDA, 0x2F, 0xFE
143 * 0011.0000 0001.1111 0001.1001 0111.1101
144 * 1001.1000 0011.1100 1011.0110 1111.1110
145 * 1110.0000 1100.0011 0000.0000 0111.1100
146 * 0100.0101 1110.0011 0110.0000 0110.0110
147 * 1011.0101 0101.1011 1111.0100 0111.1111
156 uint8_t comp1[] PROGMEM = {
157 0x30, 0x1F, 0x19, 0x7D,
158 0x98, 0x3C, 0xB6, 0xFE,
159 0xE0, 0xC3, 0x00, 0x7C,
160 0x45, 0xE3, 0x60, 0x66,
161 0xB5, 0x5B, 0xF4, 0x7F
165 * 1010.1111 0001.1111 0011.1100 1100.0100
166 * 0010.0010 1010.0011 0010.1111 0000.1110
167 * 1000.0001 0100.1101 1110.0101 0110.0110
168 * 1001.0001 0100.1011 0101.0100 1101.0100
169 * 1100.0001 0000.1011 0110.0011 1000.0011
177 uint8_t fb0[] PROGMEM = {
178 0xAF, 0x1F, 0x3C, 0xC4,
179 0x22, 0xA3, 0x2F, 0x0E,
180 0x81, 0x4D, 0xE5, 0x66,
181 0x91, 0x4B, 0x54, 0xD4,
182 0xC1, 0x0B, 0x63, 0x83
186 * 1010.1011 0111.0111 1111.0100 1001.1011
187 * 1001.0000 1000.1100 0111.1001 0111.0000
188 * 1011.0110 0001.1000 1001.1010 0110.1111
189 * 1110.0111 0111.1110 0100.1011 0110.1100
190 * 1110.1111 1000.0000 1010.0111 0001.0001
198 uint8_t fb1[] PROGMEM = {
199 0xAB, 0x77, 0xF4, 0x9B,
200 0x90, 0x8C, 0x79, 0x70,
201 0xB6, 0x18, 0x9A, 0x6F,
202 0xE7, 0x7E, 0x4B, 0x6C,
203 0xEF, 0x80, 0xA7, 0x11
207 void clock_s(uint8_t* s, uint8_t ibit, uint8_t cbit){
208 uint8_t s0[20], s1[20];
212 memxor_P(s0, comp0, 20);
214 s1[19-i]= c|((s[19-i])>>1);
217 memxor_P(s1, comp1, 20);
221 s[i]=((s[i])<<1) ^ ((s0[i])&(s1[i])) ^ c;
226 memxor_P(s, cbit?fb1:fb0, 20);
231 void clock_kg(uint8_t* r, uint8_t* s, uint8_t mixing, uint8_t input){
233 rb = ((s[ 54/8])>>(( 54%8))) ^ ((r[106/8])>>(((106%8))));
234 sb = ((s[106/8])>>((106%8))) ^ ((r[ 53/8])>>((( 53%8))));
237 mixing = input ^ (mixing & ((s[80/8]>>((80%8))) & 1));
238 clock_r(r, mixing, rb);
239 clock_s(s, input, sb);
242 void mickey128_init(void* key, uint16_t keysize_b,
243 void* iv, uint16_t ivsize_b,
244 mickey128_ctx_t* ctx){
246 memset(ctx->r, 0, 20);
247 memset(ctx->s, 0, 20);
248 for(i=0; i<ivsize_b; ++i){
249 clock_kg(ctx->r, ctx->s, 1, 1&((((uint8_t*)iv)[i/8])>>(7-(i%8))));
251 for(i=0; i<keysize_b; ++i){
252 clock_kg(ctx->r, ctx->s, 1, 1&((((uint8_t*)key)[i/8])>>(7-(i%8))));
254 for(i=0; i<160; ++i){
255 clock_kg(ctx->r, ctx->s, 1, 0);
259 uint8_t mickey128_getbit(mickey128_ctx_t* ctx){
261 ret = 1&(*(ctx->r) ^ *(ctx->s));
262 clock_kg(ctx->r, ctx->s, 0, 0);
266 uint8_t mickey128_getbyte(mickey128_ctx_t* ctx){
270 ret |= 1&(((ctx->r)[0]) ^ ((ctx->s)[0]));
271 clock_kg(ctx->r, ctx->s, 0, 0);