3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
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).
28 * THIS ONLY WORKS FOR LITTEL ENDIAN!!!
36 #define P32 0xB7E15163 /* e -2 */
37 #define Q32 0x9E3779B9 /* Golden Ratio -1 */
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)) );
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)) );
49 uint8_t rc6_init(void* key, uint16_t keylength_b, rc6_ctx_t *s){
50 return rc6_initl(key, keylength_b, 20, s);
54 uint8_t rc6_initl(void* key, uint16_t keylength_b, uint8_t rounds, rc6_ctx_t *s){
60 if(!(s->S=malloc((2*rounds+4)*sizeof(uint32_t))))
72 ((uint8_t*)&l)[i] = ((uint8_t*)key)[(c-1)*4 + i];
74 l = ((uint32_t*)key)[c-1];
78 for(i=1; i<2*rounds+4; ++i){
79 s->S[i] = s->S[i-1] + Q32;
83 v = 3 * ((c > 2*rounds+4)?c:(2*rounds+4));
85 a = s->S[i] = rotl32(s->S[i] + a + b, 3);
87 b = l = rotl32(l+a+b, a+b);
89 b = ((uint32_t*)key)[j] = rotl32(((uint32_t*)key)[j]+a+b, a+b);
91 i = (i+1) % (2*rounds+4);
97 void rc6_free(rc6_ctx_t *s){
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])
107 void rc6_enc(void* block, rc6_ctx_t *s){
109 uint32_t t,u,x; /* greetings to Linux? */
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];
123 A += s->S[2*s->rounds+2];
124 C += s->S[2*s->rounds+3];
127 void rc6_dec(void* block, rc6_ctx_t *s){
129 uint32_t t,u,x; /* greetings to Linux? */
131 C -= s->S[2*s->rounds+3];
132 A -= s->S[2*s->rounds+2];
134 for (i=s->rounds; i>0; --i){
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;