3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2010 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 void rc6_init(const void* key, uint16_t keylength_b, rc6_ctx_t *s){
50 rc6_initl(key, keylength_b, 20, s);
54 uint8_t rc6_initl(const void* key, uint16_t keylength_b, uint8_t rounds, rc6_ctx_t *s){
58 c =( keylength_b+31)/32;
59 uint32_t local_key[c];
62 if(!(s->S=malloc((2*rounds+4)*sizeof(uint32_t))))
67 memcpy(local_key, key, (keylength_b+7)/8);
70 for(i=1; i<2*rounds+4; ++i){
71 s->S[i] = s->S[i-1] + Q32;
75 v = 3 * ((c > (2*rounds+4))?c:(2*rounds+4));
77 a = s->S[i] = rotl32(s->S[i] + a + b, 3);
78 b = ((uint32_t*)local_key)[j] = rotl32(((uint32_t*)local_key)[j]+a+b, a+b);
79 i = (i+1) % (2*rounds+4);
85 void rc6_free(rc6_ctx_t *s){
90 #define A (((uint32_t*)block)[0])
91 #define B (((uint32_t*)block)[1])
92 #define C (((uint32_t*)block)[2])
93 #define D (((uint32_t*)block)[3])
95 void rc6_enc(void* block, rc6_ctx_t *s){
97 uint32_t t,u,x; /* greetings to Linux? */
104 t = rotl32(B * (2*B+1), LG_W);
105 u = rotl32(D * (2*D+1), LG_W);
106 A = rotl32((A ^ t), u) + *p++;
107 C = rotl32((C ^ u), t) + *p++;
118 void rc6_dec(void* block, rc6_ctx_t *s){
120 uint32_t t,u,x; /* greetings to Linux? */
122 p = &(s->S[2*(s->rounds)+3]);
133 u = rotl32(D * (2*D+1), LG_W);
134 t = rotl32(B * (2*B+1), LG_W);
135 C = rotr32(C - *p--, t) ^ u;
136 A = rotr32(A - *p--, u) ^ t;