]> git.cryptolib.org Git - arm-crypto-lib.git/blob - rc6/rc6.c
adding trivium
[arm-crypto-lib.git] / rc6 / rc6.c
1 /* rc6.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  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 <string.h>
34 #include "rc6.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 void rc6_init(const void* key, uint16_t keylength_b, rc6_ctx_t *s){
50         rc6_initl(key, keylength_b, 20, s);
51 }
52  
53  
54 uint8_t rc6_initl(const 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;
58         c =( keylength_b+31)/32;
59         uint32_t local_key[c];
60         if (rounds>125)
61                 return 2;
62         if(!(s->S=malloc((2*rounds+4)*sizeof(uint32_t))))
63                 return 1;
64         
65         s->rounds=rounds;
66         local_key[c-1] = 0;
67         memcpy(local_key, key, (keylength_b+7)/8);
68         
69         s->S[0] = P32;
70         for(i=1; i<2*rounds+4; ++i){
71                 s->S[i] = s->S[i-1] + Q32;
72         }
73         
74         a=b=j=i=0;
75         v = 3 * ((c > (2*rounds+4))?c:(2*rounds+4));
76         for(p=1; p<=v; ++p){
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);
80                 j = (j+1) % c;
81         }
82         return 0;
83 }
84  
85 void rc6_free(rc6_ctx_t *s){
86         free(s->S);
87
88  
89 #define LG_W 5
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])
94  
95 void rc6_enc(void* block, rc6_ctx_t *s){
96         uint8_t i;
97         uint32_t t,u,x; /* greetings to Linux? */
98         uint32_t *p;
99         p=s->S;
100         B += *p++;
101         D += *p++;
102         i=s->rounds;
103         do{
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++;
108                 x = A;
109                 A = B;
110                 B = C;
111                 C = D;
112                 D = x;
113         }while(--i);
114         A += *p++;
115         C += *p;
116 }
117  
118 void rc6_dec(void* block, rc6_ctx_t *s){
119         uint8_t i;
120         uint32_t t,u,x; /* greetings to Linux? */
121         uint32_t *p;
122         p = &(s->S[2*(s->rounds)+3]);
123         C -= *p--;
124         A -= *p--;
125         
126         i=s->rounds;
127         do{
128                 x=D;
129                 D=C;
130                 C=B;
131                 B=A;
132                 A=x;
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;
137         }while(--i);
138         D -= *p--;
139         B -= *p;
140 }
141