1 // SPDX-License-Identifier: BSD-2-Clause 2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis 3 * 4 * LibTomCrypt is a library that provides various cryptographic 5 * algorithms in a highly modular and flexible manner. 6 * 7 * The library is free for all purposes without any express 8 * guarantee it works. 9 */ 10 11 /** 12 @file rc5.c 13 LTC_RC5 code by Tom St Denis 14 */ 15 16 #include "tomcrypt_private.h" 17 18 #ifdef LTC_RC5 19 20 const struct ltc_cipher_descriptor rc5_desc = 21 { 22 "rc5", 23 2, 24 8, 128, 8, 12, 25 &rc5_setup, 26 &rc5_ecb_encrypt, 27 &rc5_ecb_decrypt, 28 &rc5_test, 29 &rc5_done, 30 &rc5_keysize, 31 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 32 }; 33 34 static const ulong32 stab[50] = { 35 0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL, 36 0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL, 37 0x9b58ecf3UL, 0x399066acUL, 0xd7c7e065UL, 0x75ff5a1eUL, 0x1436d3d7UL, 0xb26e4d90UL, 0x50a5c749UL, 0xeedd4102UL, 38 0x8d14babbUL, 0x2b4c3474UL, 0xc983ae2dUL, 0x67bb27e6UL, 0x05f2a19fUL, 0xa42a1b58UL, 0x42619511UL, 0xe0990ecaUL, 39 0x7ed08883UL, 0x1d08023cUL, 0xbb3f7bf5UL, 0x5976f5aeUL, 0xf7ae6f67UL, 0x95e5e920UL, 0x341d62d9UL, 0xd254dc92UL, 40 0x708c564bUL, 0x0ec3d004UL, 0xacfb49bdUL, 0x4b32c376UL, 0xe96a3d2fUL, 0x87a1b6e8UL, 0x25d930a1UL, 0xc410aa5aUL, 41 0x62482413UL, 0x007f9dccUL 42 }; 43 44 /** 45 Initialize the LTC_RC5 block cipher 46 @param key The symmetric key you wish to pass 47 @param keylen The key length in bytes 48 @param num_rounds The number of rounds desired (0 for default) 49 @param skey The key in as scheduled by this function. 50 @return CRYPT_OK if successful 51 */ 52 #ifdef LTC_CLEAN_STACK 53 static int _rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) 54 #else 55 int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) 56 #endif 57 { 58 ulong32 L[64], *S, A, B, i, j, v, s, t, l; 59 60 LTC_ARGCHK(skey != NULL); 61 LTC_ARGCHK(key != NULL); 62 63 /* test parameters */ 64 if (num_rounds == 0) { 65 num_rounds = rc5_desc.default_rounds; 66 } 67 68 if (num_rounds < 12 || num_rounds > 24) { 69 return CRYPT_INVALID_ROUNDS; 70 } 71 72 /* key must be between 64 and 1024 bits */ 73 if (keylen < 8 || keylen > 128) { 74 return CRYPT_INVALID_KEYSIZE; 75 } 76 77 skey->rc5.rounds = num_rounds; 78 S = skey->rc5.K; 79 80 /* copy the key into the L array */ 81 for (A = i = j = 0; i < (ulong32)keylen; ) { 82 A = (A << 8) | ((ulong32)(key[i++] & 255)); 83 if ((i & 3) == 0) { 84 L[j++] = BSWAP(A); 85 A = 0; 86 } 87 } 88 89 if ((keylen & 3) != 0) { 90 A <<= (ulong32)((8 * (4 - (keylen&3)))); 91 L[j++] = BSWAP(A); 92 } 93 94 /* setup the S array */ 95 t = (ulong32)(2 * (num_rounds + 1)); 96 XMEMCPY(S, stab, t * sizeof(*S)); 97 98 /* mix buffer */ 99 s = 3 * MAX(t, j); 100 l = j; 101 for (A = B = i = j = v = 0; v < s; v++) { 102 A = S[i] = ROLc(S[i] + A + B, 3); 103 B = L[j] = ROL(L[j] + A + B, (A+B)); 104 if (++i == t) { i = 0; } 105 if (++j == l) { j = 0; } 106 } 107 return CRYPT_OK; 108 } 109 110 #ifdef LTC_CLEAN_STACK 111 int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) 112 { 113 int x; 114 x = _rc5_setup(key, keylen, num_rounds, skey); 115 burn_stack(sizeof(ulong32) * 122 + sizeof(int)); 116 return x; 117 } 118 #endif 119 120 /** 121 Encrypts a block of text with LTC_RC5 122 @param pt The input plaintext (8 bytes) 123 @param ct The output ciphertext (8 bytes) 124 @param skey The key as scheduled 125 @return CRYPT_OK if successful 126 */ 127 #ifdef LTC_CLEAN_STACK 128 static int _rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey) 129 #else 130 int rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey) 131 #endif 132 { 133 ulong32 A, B; 134 const ulong32 *K; 135 int r; 136 LTC_ARGCHK(skey != NULL); 137 LTC_ARGCHK(pt != NULL); 138 LTC_ARGCHK(ct != NULL); 139 140 LOAD32L(A, &pt[0]); 141 LOAD32L(B, &pt[4]); 142 A += skey->rc5.K[0]; 143 B += skey->rc5.K[1]; 144 K = skey->rc5.K + 2; 145 146 if ((skey->rc5.rounds & 1) == 0) { 147 for (r = 0; r < skey->rc5.rounds; r += 2) { 148 A = ROL(A ^ B, B) + K[0]; 149 B = ROL(B ^ A, A) + K[1]; 150 A = ROL(A ^ B, B) + K[2]; 151 B = ROL(B ^ A, A) + K[3]; 152 K += 4; 153 } 154 } else { 155 for (r = 0; r < skey->rc5.rounds; r++) { 156 A = ROL(A ^ B, B) + K[0]; 157 B = ROL(B ^ A, A) + K[1]; 158 K += 2; 159 } 160 } 161 STORE32L(A, &ct[0]); 162 STORE32L(B, &ct[4]); 163 164 return CRYPT_OK; 165 } 166 167 #ifdef LTC_CLEAN_STACK 168 int rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey) 169 { 170 int err = _rc5_ecb_encrypt(pt, ct, skey); 171 burn_stack(sizeof(ulong32) * 2 + sizeof(int)); 172 return err; 173 } 174 #endif 175 176 /** 177 Decrypts a block of text with LTC_RC5 178 @param ct The input ciphertext (8 bytes) 179 @param pt The output plaintext (8 bytes) 180 @param skey The key as scheduled 181 @return CRYPT_OK if successful 182 */ 183 #ifdef LTC_CLEAN_STACK 184 static int _rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey) 185 #else 186 int rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey) 187 #endif 188 { 189 ulong32 A, B; 190 const ulong32 *K; 191 int r; 192 LTC_ARGCHK(skey != NULL); 193 LTC_ARGCHK(pt != NULL); 194 LTC_ARGCHK(ct != NULL); 195 196 LOAD32L(A, &ct[0]); 197 LOAD32L(B, &ct[4]); 198 K = skey->rc5.K + (skey->rc5.rounds << 1); 199 200 if ((skey->rc5.rounds & 1) == 0) { 201 K -= 2; 202 for (r = skey->rc5.rounds - 1; r >= 0; r -= 2) { 203 B = ROR(B - K[3], A) ^ A; 204 A = ROR(A - K[2], B) ^ B; 205 B = ROR(B - K[1], A) ^ A; 206 A = ROR(A - K[0], B) ^ B; 207 K -= 4; 208 } 209 } else { 210 for (r = skey->rc5.rounds - 1; r >= 0; r--) { 211 B = ROR(B - K[1], A) ^ A; 212 A = ROR(A - K[0], B) ^ B; 213 K -= 2; 214 } 215 } 216 A -= skey->rc5.K[0]; 217 B -= skey->rc5.K[1]; 218 STORE32L(A, &pt[0]); 219 STORE32L(B, &pt[4]); 220 221 return CRYPT_OK; 222 } 223 224 #ifdef LTC_CLEAN_STACK 225 int rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey) 226 { 227 int err = _rc5_ecb_decrypt(ct, pt, skey); 228 burn_stack(sizeof(ulong32) * 2 + sizeof(int)); 229 return err; 230 } 231 #endif 232 233 /** 234 Performs a self-test of the LTC_RC5 block cipher 235 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled 236 */ 237 int rc5_test(void) 238 { 239 #ifndef LTC_TEST 240 return CRYPT_NOP; 241 #else 242 static const struct { 243 unsigned char key[16], pt[8], ct[8]; 244 } tests[] = { 245 { 246 { 0x91, 0x5f, 0x46, 0x19, 0xbe, 0x41, 0xb2, 0x51, 247 0x63, 0x55, 0xa5, 0x01, 0x10, 0xa9, 0xce, 0x91 }, 248 { 0x21, 0xa5, 0xdb, 0xee, 0x15, 0x4b, 0x8f, 0x6d }, 249 { 0xf7, 0xc0, 0x13, 0xac, 0x5b, 0x2b, 0x89, 0x52 } 250 }, 251 { 252 { 0x78, 0x33, 0x48, 0xe7, 0x5a, 0xeb, 0x0f, 0x2f, 253 0xd7, 0xb1, 0x69, 0xbb, 0x8d, 0xc1, 0x67, 0x87 }, 254 { 0xF7, 0xC0, 0x13, 0xAC, 0x5B, 0x2B, 0x89, 0x52 }, 255 { 0x2F, 0x42, 0xB3, 0xB7, 0x03, 0x69, 0xFC, 0x92 } 256 }, 257 { 258 { 0xDC, 0x49, 0xdb, 0x13, 0x75, 0xa5, 0x58, 0x4f, 259 0x64, 0x85, 0xb4, 0x13, 0xb5, 0xf1, 0x2b, 0xaf }, 260 { 0x2F, 0x42, 0xB3, 0xB7, 0x03, 0x69, 0xFC, 0x92 }, 261 { 0x65, 0xc1, 0x78, 0xb2, 0x84, 0xd1, 0x97, 0xcc } 262 } 263 }; 264 unsigned char tmp[2][8]; 265 int x, y, err; 266 symmetric_key key; 267 268 for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) { 269 /* setup key */ 270 if ((err = rc5_setup(tests[x].key, 16, 12, &key)) != CRYPT_OK) { 271 return err; 272 } 273 274 /* encrypt and decrypt */ 275 rc5_ecb_encrypt(tests[x].pt, tmp[0], &key); 276 rc5_ecb_decrypt(tmp[0], tmp[1], &key); 277 278 /* compare */ 279 if (compare_testvector(tmp[0], 8, tests[x].ct, 8, "RC5 Encrypt", x) != 0 || 280 compare_testvector(tmp[1], 8, tests[x].pt, 8, "RC5 Decrypt", x) != 0) { 281 return CRYPT_FAIL_TESTVECTOR; 282 } 283 284 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 285 for (y = 0; y < 8; y++) tmp[0][y] = 0; 286 for (y = 0; y < 1000; y++) rc5_ecb_encrypt(tmp[0], tmp[0], &key); 287 for (y = 0; y < 1000; y++) rc5_ecb_decrypt(tmp[0], tmp[0], &key); 288 for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; 289 } 290 return CRYPT_OK; 291 #endif 292 } 293 294 /** Terminate the context 295 @param skey The scheduled key 296 */ 297 void rc5_done(symmetric_key *skey) 298 { 299 LTC_UNUSED_PARAM(skey); 300 } 301 302 /** 303 Gets suitable key size 304 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. 305 @return CRYPT_OK if the input key size is acceptable. 306 */ 307 int rc5_keysize(int *keysize) 308 { 309 LTC_ARGCHK(keysize != NULL); 310 if (*keysize < 8) { 311 return CRYPT_INVALID_KEYSIZE; 312 } 313 if (*keysize > 128) { 314 *keysize = 128; 315 } 316 return CRYPT_OK; 317 } 318 319 #endif 320 321 322 323 324 /* ref: $Format:%D$ */ 325 /* git commit: $Format:%H$ */ 326 /* commit time: $Format:%ai$ */ 327