5 const struct _cipher_descriptor rc6_desc =
18 static int _rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
20 int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
23 unsigned long L[64], S[50], A, B, i, j, v, s, t, l;
26 _ARGCHK(skey != NULL);
29 if (num_rounds != 0 && num_rounds != 20) {
30 return CRYPT_INVALID_ROUNDS;
33 /* key must be between 64 and 1024 bits */
34 if (keylen < 8 || keylen > 128) {
35 return CRYPT_INVALID_KEYSIZE;
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));
47 /* handle odd sized keys */
49 A <<= (8 * (4 - (keylen&3)));
53 /* setup the S array */
54 t = 44; /* fixed at 20 rounds */
56 for (i = 1; i < t; i++)
57 S[i] = S[i - 1] + 0x9E3779B9UL;
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));
70 for (i = 0; i < t; i++) {
71 skey->rc6.K[i] = S[i];
77 int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
80 x = _rc6_setup(key, keylen, num_rounds, skey);
81 burn_stack(sizeof(unsigned long) * 122);
87 static void _rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
89 void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
92 unsigned long a,b,c,d,t,u;
98 LOAD32L(a,&pt[0]);LOAD32L(b,&pt[4]);LOAD32L(c,&pt[8]);LOAD32L(d,&pt[12]);
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;
110 STORE32L(a,&ct[0]);STORE32L(b,&ct[4]);STORE32L(c,&ct[8]);STORE32L(d,&ct[12]);
114 void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
116 _rc6_ecb_encrypt(pt, ct, key);
117 burn_stack(sizeof(unsigned long) * 6 + sizeof(int));
122 static void _rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
124 void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
127 unsigned long a,b,c,d,t,u;
130 _ARGCHK(key != NULL);
134 LOAD32L(a,&ct[0]);LOAD32L(b,&ct[4]);LOAD32L(c,&ct[8]);LOAD32L(d,&ct[12]);
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;
146 STORE32L(a,&pt[0]);STORE32L(b,&pt[4]);STORE32L(c,&pt[8]);STORE32L(d,&pt[12]);
150 void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
152 _rc6_ecb_decrypt(ct, pt, key);
153 burn_stack(sizeof(unsigned long) * 6 + sizeof(int));
162 static const struct {
164 unsigned char key[32], pt[16], ct[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 }
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 }
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 }
200 unsigned char buf[2][16];
204 for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
206 if ((err = rc6_setup(tests[x].key, tests[x].keylen, 0, &key)) != CRYPT_OK) {
210 /* encrypt and decrypt */
211 rc6_ecb_encrypt(tests[x].pt, buf[0], &key);
212 rc6_ecb_decrypt(buf[0], buf[1], &key);
215 if (memcmp(buf[0], tests[x].ct, 16) || memcmp(buf[1], tests[x].pt, 16)) {
216 return CRYPT_FAIL_TESTVECTOR;
223 int rc6_keysize(int *desired_keysize)
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;