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