5 const struct _cipher_descriptor rc5_desc =
18 static int _rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
20 int rc5_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;
25 _ARGCHK(skey != NULL);
29 if (num_rounds == 0) {
30 num_rounds = rc5_desc.default_rounds;
33 if (num_rounds < 12 || num_rounds > 24) {
34 return CRYPT_INVALID_ROUNDS;
37 /* key must be between 64 and 1024 bits */
38 if (keylen < 8 || keylen > 128) {
39 return CRYPT_INVALID_KEYSIZE;
42 /* copy the key into the L array */
43 for (A = i = j = 0; i < (unsigned long)keylen; ) {
44 A = (A << 8) | ((unsigned long)(key[i++] & 255));
51 if ((keylen & 3) != 0) {
52 A <<= (unsigned long)((8 * (4 - (keylen&3))));
56 /* setup the S array */
57 t = (unsigned long)(2 * (num_rounds + 1));
59 for (i = 1; i < t; i++) S[i] = S[i - 1] + 0x9E3779B9UL;
64 for (A = B = i = j = v = 0; v < s; v++) {
65 A = S[i] = ROL(S[i] + A + B, 3);
66 B = L[j] = ROL(L[j] + A + B, (A+B));
72 for (i = 0; i < t; i++) {
73 skey->rc5.K[i] = S[i];
75 skey->rc5.rounds = num_rounds;
80 int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
83 x = _rc5_setup(key, keylen, num_rounds, skey);
84 burn_stack(sizeof(unsigned long) * 122 + sizeof(int));
90 static void _rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
92 void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
105 for (r = 0; r < key->rc5.rounds; r++) {
106 A = ROL(A ^ B, B) + key->rc5.K[r+r+2];
107 B = ROL(B ^ A, A) + key->rc5.K[r+r+3];
114 void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
116 _rc5_ecb_encrypt(pt, ct, key);
117 burn_stack(sizeof(unsigned long) * 2 + sizeof(int));
122 static void _rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
124 void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
129 _ARGCHK(key != NULL);
135 for (r = key->rc5.rounds - 1; r >= 0; r--) {
136 B = ROR(B - key->rc5.K[r+r+3], A) ^ A;
137 A = ROR(A - key->rc5.K[r+r+2], B) ^ B;
146 void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
148 _rc5_ecb_decrypt(ct, pt, key);
149 burn_stack(sizeof(unsigned long) * 2 + sizeof(int));
158 static const struct {
159 unsigned char key[16], pt[8], ct[8];
162 { 0x91, 0x5f, 0x46, 0x19, 0xbe, 0x41, 0xb2, 0x51,
163 0x63, 0x55, 0xa5, 0x01, 0x10, 0xa9, 0xce, 0x91 },
164 { 0x21, 0xa5, 0xdb, 0xee, 0x15, 0x4b, 0x8f, 0x6d },
165 { 0xf7, 0xc0, 0x13, 0xac, 0x5b, 0x2b, 0x89, 0x52 }
168 { 0x78, 0x33, 0x48, 0xe7, 0x5a, 0xeb, 0x0f, 0x2f,
169 0xd7, 0xb1, 0x69, 0xbb, 0x8d, 0xc1, 0x67, 0x87 },
170 { 0xF7, 0xC0, 0x13, 0xAC, 0x5B, 0x2B, 0x89, 0x52 },
171 { 0x2F, 0x42, 0xB3, 0xB7, 0x03, 0x69, 0xFC, 0x92 }
174 { 0xDC, 0x49, 0xdb, 0x13, 0x75, 0xa5, 0x58, 0x4f,
175 0x64, 0x85, 0xb4, 0x13, 0xb5, 0xf1, 0x2b, 0xaf },
176 { 0x2F, 0x42, 0xB3, 0xB7, 0x03, 0x69, 0xFC, 0x92 },
177 { 0x65, 0xc1, 0x78, 0xb2, 0x84, 0xd1, 0x97, 0xcc }
180 unsigned char buf[2][8];
184 for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
186 if ((err = rc5_setup(tests[x].key, 16, 12, &key)) != CRYPT_OK) {
190 /* encrypt and decrypt */
191 rc5_ecb_encrypt(tests[x].pt, buf[0], &key);
192 rc5_ecb_decrypt(buf[0], buf[1], &key);
195 if (memcmp(buf[0], tests[x].ct, 8) != 0 || memcmp(buf[1], tests[x].pt, 8) != 0) {
196 return CRYPT_FAIL_TESTVECTOR;
203 int rc5_keysize(int *desired_keysize)
205 _ARGCHK(desired_keysize != NULL);
206 if (*desired_keysize < 8) {
207 return CRYPT_INVALID_KEYSIZE;
208 } else if (*desired_keysize > 128) {
209 *desired_keysize = 128;