]> git.cryptolib.org Git - avr-crypto-lib.git/blob - rc6.c
+RC5 +size-statistics tool +small modification to nessie_bc_test (optional free(...
[avr-crypto-lib.git] / rc6.c
1 /* 
2  * File:        rc6.c
3  * Author:      Daniel Otte
4  * Date:        06.08.2006
5  * License: GPL
6  * Description: Implementation of the RC6 cipher algorithm.
7  *      This implementation is restricted to 32-bit words and to keys up to 65535 bit in length (but this is
8  *  quite easy to expand), but free in the choice of number of rounds (0 to 125).
9  *      so it is RC6-32/r/b
10  * THIS ONLY WORKS FOR LITTEL ENDIAN!!!
11  */
12  
13  #include <stdint.h>
14  #include <stdlib.h>
15  #include "rc6.h"
16  #include "config.h"
17  
18  #define P32 0xB7E15163         /* e -2 */
19  #define Q32 0x9E3779B9         /* Golden Ratio -1 */
20  
21 uint32_t rotl32(uint32_t a, uint8_t n){
22         n &= 0x1f; /* higher rotates would not bring anything */
23         return ( (a<<n)| (a>>(32-n)) );
24  }
25
26 uint32_t rotr32(uint32_t a, uint8_t n){
27         n &= 0x1f; /* higher rotates would not bring anything */
28         return ( (a>>n)| (a<<(32-n)) );
29  }
30  
31  uint8_t rc6_init(rc6_ctx_t *s,void* key, uint16_t keylength){
32         return rc6_initl(s, key, keylength, 20);
33  }
34  
35  
36  uint8_t rc6_initl(rc6_ctx_t *s,void* key, uint16_t keylength, uint8_t rounds){
37         uint8_t i,j;
38         uint16_t v,p,c;
39         uint32_t a,b, l=0;
40         if (rounds>125)
41                 return 2;
42         if(!(s->S=malloc((2*rounds+4)*sizeof(uint32_t))))
43                 return 1;
44         
45         s->rounds=rounds;
46         
47         c = keylength/32;
48         if (keylength%32){ 
49                 ++c;
50                 j=(keylength%32)/8;
51                 if(keylength%8)
52                         ++j;
53                 for (i=0; i<j; ++i) 
54                         ((uint8_t*)&l)[i] = ((uint8_t*)key)[(c-1)*4 + i];
55         } else {
56                 l = ((uint32_t*)key)[c-1];
57         }
58         
59         s->S[0] = P32;
60         for(i=1; i<2*rounds+4; ++i){
61                 s->S[i] = s->S[i-1] + Q32;
62         }
63         
64         a=b=j=i=0;
65         v = 3 * ((c > 2*rounds+4)?c:(2*rounds+4));
66         for(p=1; p<=v; ++p){
67                 a = s->S[i] = rotl32(s->S[i] + a + b, 3);
68                 if (j==c-1){
69                         b = l = rotl32(l+a+b, a+b);
70                 } else {
71                         b = ((uint32_t*)key)[j] = rotl32(((uint32_t*)key)[j]+a+b, a+b);
72                 }
73                 i = (i+1) % (2*rounds+4);
74                 j = (j+1) % c;
75         }
76         return 0;
77  }
78  
79  void rc6_free(rc6_ctx_t *s){
80         free(s->S);
81  } 
82  
83  #define LG_W 5
84  #define A (((uint32_t*)block)[0])
85  #define B (((uint32_t*)block)[1])
86  #define C (((uint32_t*)block)[2])
87  #define D (((uint32_t*)block)[3])
88  
89  void rc6_enc(rc6_ctx_t *s, void* block){
90         uint8_t i;
91         uint32_t t,u,x; /* greetings to Linux? */
92         B += s->S[0];
93         D += s->S[1];
94         for (i=1; i<=s->rounds; ++i){
95                 t = rotl32(B * (2*B+1), LG_W);
96                 u = rotl32(D * (2*D+1), LG_W);
97                 A = rotl32((A ^ t), u) + s->S[2*i];
98                 C = rotl32((C ^ u), t) + s->S[2*i+1];
99                 x = A;
100                 A = B;
101                 B = C;
102                 C = D;
103                 D = x;
104         }
105         A += s->S[2*s->rounds+2];
106         C += s->S[2*s->rounds+3];
107  }
108  
109  void rc6_dec(rc6_ctx_t *s, void* block)
110  {
111         uint8_t i;
112         uint32_t t,u,x; /* greetings to Linux? */
113  
114         C -= s->S[2*s->rounds+3];
115         A -= s->S[2*s->rounds+2];
116         
117         for (i=s->rounds; i>0; --i){
118                 x=D;
119                 D=C;
120                 C=B;
121                 B=A;
122                 A=x;
123                 u = rotl32(D * (2*D+1), LG_W);
124                 t = rotl32(B * (2*B+1), LG_W);
125                 C = rotr32(C - s->S[2*i+1], t) ^ u;
126                 A = rotr32(A - s->S[2*i+0], u) ^ t;
127         }
128         D -= s->S[1];
129         B -= s->S[0];
130  }
131