]> git.cryptolib.org Git - avr-crypto-lib.git/blob - A5_1.c
insereated GPLv3 stub
[avr-crypto-lib.git] / A5_1.c
1 /* A5_1.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:                A5_1.c
21  * Author:      Daniel Otte
22  * Date:        24.06.2006
23  * License: GPL
24  * Description: Implementation of the A5/1 stream cipher algorithm, as used in GSM.
25  * ! Warning, this is weak crypto !
26  * 
27  */
28  
29 #include <stdint.h>
30 #include <string.h>
31 #include "A5_1.h"
32 #include <avr/pgmspace.h>
33
34 uint8_t a5_1_clock_core(a5_1_ctx_t *c, uint8_t clockoverride);
35
36
37 /*
38  * length is length of key in bytes!
39  */
40
41 void a5_1_init(a5_1_ctx_t *c, void* key, uint8_t keylength_b, void* iv, uint8_t ivlength_b){
42         uint8_t i,t;
43         memset(c->r1, 0, 3);
44         memset(c->r2, 0, 3);
45         memset(c->r3, 0, 3);
46         for(i=0; i<keylength_b; ++i){
47                 t=((uint8_t*)key)[i/8];
48                 t=1&(t>>i);
49                 c->r1[0] ^= t;
50                 c->r2[0] ^= t;
51                 c->r3[0] ^= t;
52                 a5_1_clock_core(c, 0x7);
53         }
54         for(i=0; i<ivlength_b; ++i){
55                 t=((uint8_t*)iv)[i/8];
56                 t=1&(t>>i);
57                 c->r1[0] ^= t;
58                 c->r2[0] ^= t;
59                 c->r3[0] ^= t;
60                 a5_1_clock_core(c, 0x7);
61         }
62         for(i=0; i<100; ++i)
63                 a5_1_clock_core(c,0);
64 }
65
66 static
67 void shiftreg(uint8_t* d){
68         uint8_t c, c2;
69         c=d[0]>>7;
70         d[0] <<= 1;
71         c2=d[1]>>7;
72         d[1] = (d[1]<<1) | c;
73         d[2] = (d[2]<<1) | c2;
74
75
76         uint8_t parity3_lut[] PROGMEM = {0, 1, 1, 0,
77                                              1, 0, 0, 1};       
78         uint8_t clock_lut[] PROGMEM =  {0x7, 0x6, 0x5, 0x3,
79                                             0x3, 0x5, 0x6, 0x7}; 
80                                             
81 uint8_t a5_1_clock_core(a5_1_ctx_t *c, uint8_t clockoverride){
82         uint8_t ret,clk,fb;
83         ret = (0x04&c->r1[2]) | (0x20&c->r2[2]) | (0x40&c->r3[2]);
84         ret = ret^(ret>>6);
85         ret &= 0x7;
86         ret = pgm_read_byte(parity3_lut+ret);
87         clk = (0x08&c->r1[1]) | (0x10&c->r2[1]) | (0x20&c->r3[1]);
88         clk >>= 3;
89         clk = pgm_read_byte(clock_lut+clk);
90         clk |= clockoverride;
91         
92         if(clk&1){
93                 fb = c->r1[2] ^ (1&((c->r1[1])>>5));
94                 fb &= 0x7;
95                 fb = pgm_read_byte(parity3_lut+fb);
96                 shiftreg(c->r1);
97                 c->r1[0] |= fb;
98                 c->r1[2] &= 0x07;
99         }
100         clk>>=1;
101         if(clk&1){
102                 fb = c->r2[2]>>4 ;
103                 fb &= 0x7;
104                 fb = pgm_read_byte(parity3_lut+fb);
105                 shiftreg(c->r2);
106                 c->r2[0] |= fb;
107                 c->r2[2] &= 0x3F;
108         
109         }
110         clk>>=1;
111         if(clk&1){
112                 fb = (c->r3[2]>>4) ^ (1&((c->r3[0])>>7));
113                 fb &= 0x7;
114                 fb = pgm_read_byte(parity3_lut+fb);
115                 shiftreg(c->r3);
116                 c->r3[0] |= fb;
117                 c->r3[2] &= 0x7F;
118         }
119         return ret;
120 }
121
122 uint8_t a5_1_clock(a5_1_ctx_t *c){
123         return a5_1_clock_core(c, 0);
124 }
125
126
127 uint8_t a5_1_gen(a5_1_ctx_t *c){
128         uint8_t ret=0;
129         ret = a5_1_clock(c);
130         ret <<= 1;
131         ret = a5_1_clock(c);
132         ret <<= 1;
133         ret = a5_1_clock(c);
134         ret <<= 1;
135         ret = a5_1_clock(c);
136         ret <<= 1;
137         ret = a5_1_clock(c);
138         ret <<= 1;
139         ret = a5_1_clock(c);
140         ret <<= 1;
141         ret = a5_1_clock(c);
142         ret <<= 1;
143         ret = a5_1_clock(c);
144         return ret;
145 }
146
147
148
149