Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / sshd / libtomcrypt / rc6.c
1 #include "mycrypt.h"
2
3 #ifdef RC6
4
5 const struct _cipher_descriptor rc6_desc =
6 {
7     "rc6",
8     3,
9     8, 128, 16, 20,
10     &rc6_setup,
11     &rc6_ecb_encrypt,
12     &rc6_ecb_decrypt,
13     &rc6_test,
14     &rc6_keysize
15 };
16
17 #ifdef CLEAN_STACK
18 static int _rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
19 #else
20 int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
21 #endif
22 {
23     unsigned long L[64], S[50], A, B, i, j, v, s, t, l;
24
25     _ARGCHK(key != NULL);
26     _ARGCHK(skey != NULL);
27
28     /* test parameters */
29     if (num_rounds != 0 && num_rounds != 20) { 
30        return CRYPT_INVALID_ROUNDS;
31     }
32
33     /* key must be between 64 and 1024 bits */
34     if (keylen < 8 || keylen > 128) {
35        return CRYPT_INVALID_KEYSIZE;
36     }
37
38     /* copy the key into the L array */
39     for (A = i = j = 0; i < (unsigned long)keylen; ) { 
40         A = (A << 8) | ((unsigned long)(key[i++] & 255));
41         if (!(i & 3)) {
42            L[j++] = BSWAP(A);
43            A = 0;
44         }
45     }
46
47     /* handle odd sized keys */
48     if (keylen & 3) { 
49        A <<= (8 * (4 - (keylen&3))); 
50        L[j++] = BSWAP(A); 
51     }
52
53     /* setup the S array */
54     t = 44;                                     /* fixed at 20 rounds */
55     S[0] = 0xB7E15163UL;
56     for (i = 1; i < t; i++) 
57         S[i] = S[i - 1] + 0x9E3779B9UL;
58
59     /* mix buffer */
60     s = 3 * MAX(t, j);
61     l = j;
62     for (A = B = i = j = v = 0; v < s; v++) { 
63         A = S[i] = ROL(S[i] + A + B, 3);
64         B = L[j] = ROL(L[j] + A + B, (A+B));
65         i = (i + 1) % t;
66         j = (j + 1) % l;
67     }
68     
69     /* copy to key */
70     for (i = 0; i < t; i++) { 
71         skey->rc6.K[i] = S[i];
72     }
73     return CRYPT_OK;
74 }
75
76 #ifdef CLEAN_STACK
77 int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
78 {
79    int x;
80    x = _rc6_setup(key, keylen, num_rounds, skey);
81    burn_stack(sizeof(unsigned long) * 122);
82    return x;
83 }
84 #endif
85
86 #ifdef CLEAN_STACK
87 static void _rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
88 #else
89 void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
90 #endif
91 {
92    unsigned long a,b,c,d,t,u;
93    int r;
94    
95    _ARGCHK(key != NULL);
96    _ARGCHK(pt != NULL);
97    _ARGCHK(ct != NULL);
98    LOAD32L(a,&pt[0]);LOAD32L(b,&pt[4]);LOAD32L(c,&pt[8]);LOAD32L(d,&pt[12]);
99    b += key->rc6.K[0];
100    d += key->rc6.K[1];
101    for (r = 0; r < 20; r++) {
102        t = (b * (b + b + 1)); t = ROL(t, 5);
103        u = (d * (d + d + 1)); u = ROL(u, 5);
104        a = ROL(a^t,u) + key->rc6.K[r+r+2];
105        c = ROL(c^u,t) + key->rc6.K[r+r+3];
106        t = a; a = b; b = c; c = d; d = t;
107    }
108    a += key->rc6.K[42];
109    c += key->rc6.K[43];
110    STORE32L(a,&ct[0]);STORE32L(b,&ct[4]);STORE32L(c,&ct[8]);STORE32L(d,&ct[12]);
111 }
112
113 #ifdef CLEAN_STACK
114 void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
115 {
116    _rc6_ecb_encrypt(pt, ct, key);
117    burn_stack(sizeof(unsigned long) * 6 + sizeof(int));
118 }
119 #endif
120
121 #ifdef CLEAN_STACK
122 static void _rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
123 #else
124 void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
125 #endif
126 {
127    unsigned long a,b,c,d,t,u;
128    int r;
129
130    _ARGCHK(key != NULL);
131    _ARGCHK(pt != NULL);
132    _ARGCHK(ct != NULL);
133    
134    LOAD32L(a,&ct[0]);LOAD32L(b,&ct[4]);LOAD32L(c,&ct[8]);LOAD32L(d,&ct[12]);
135    a -= key->rc6.K[42];
136    c -= key->rc6.K[43];
137    for (r = 19; r >= 0; r--) {
138        t = d; d = c; c = b; b = a; a = t;
139        t = (b * (b + b + 1)); t = ROL(t, 5);
140        u = (d * (d + d + 1)); u = ROL(u, 5);
141        c = ROR(c - key->rc6.K[r+r+3], t) ^ u;
142        a = ROR(a - key->rc6.K[r+r+2], u) ^ t;
143    }
144    b -= key->rc6.K[0];
145    d -= key->rc6.K[1];
146    STORE32L(a,&pt[0]);STORE32L(b,&pt[4]);STORE32L(c,&pt[8]);STORE32L(d,&pt[12]);
147 }
148
149 #ifdef CLEAN_STACK
150 void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
151 {
152    _rc6_ecb_decrypt(ct, pt, key);
153    burn_stack(sizeof(unsigned long) * 6 + sizeof(int));
154 }
155 #endif
156
157 int rc6_test(void)
158 {
159  #ifndef LTC_TEST
160     return CRYPT_NOP;
161  #else    
162    static const struct {
163        int keylen;
164        unsigned char key[32], pt[16], ct[16];
165    } tests[] = {
166    {
167        16,
168        { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
169          0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
170          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
171          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
172        { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79,
173          0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 },
174        { 0x52, 0x4e, 0x19, 0x2f, 0x47, 0x15, 0xc6, 0x23,
175          0x1f, 0x51, 0xf6, 0x36, 0x7e, 0xa4, 0x3f, 0x18 }
176    },
177    {
178        24,
179        { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
180          0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
181          0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0,
182          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
183        { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79,
184          0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 },
185        { 0x68, 0x83, 0x29, 0xd0, 0x19, 0xe5, 0x05, 0x04,
186          0x1e, 0x52, 0xe9, 0x2a, 0xf9, 0x52, 0x91, 0xd4 }
187    },
188    {
189        32,
190        { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
191          0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
192          0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0,
193          0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe },
194        { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79,
195          0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 },
196        { 0xc8, 0x24, 0x18, 0x16, 0xf0, 0xd7, 0xe4, 0x89,
197          0x20, 0xad, 0x16, 0xa1, 0x67, 0x4e, 0x5d, 0x48 }
198    }
199    };
200    unsigned char buf[2][16];
201    int x, err;
202    symmetric_key key;
203
204    for (x  = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
205       /* setup key */
206       if ((err = rc6_setup(tests[x].key, tests[x].keylen, 0, &key)) != CRYPT_OK) {
207          return err;
208       }
209
210       /* encrypt and decrypt */
211       rc6_ecb_encrypt(tests[x].pt, buf[0], &key);
212       rc6_ecb_decrypt(buf[0], buf[1], &key);
213
214       /* compare */
215       if (memcmp(buf[0], tests[x].ct, 16) || memcmp(buf[1], tests[x].pt, 16)) {
216          return CRYPT_FAIL_TESTVECTOR;
217       }
218    }
219    return CRYPT_OK;
220   #endif
221 }
222
223 int rc6_keysize(int *desired_keysize)
224 {
225    _ARGCHK(desired_keysize != NULL);
226    if (*desired_keysize < 8) {
227       return CRYPT_INVALID_KEYSIZE;
228    } else if (*desired_keysize > 128) {
229       *desired_keysize = 128;
230    }
231    return CRYPT_OK;
232 }
233
234 #endif /*RC6*/
235
236