1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
9 /**********************************************************************\
10 * To commemorate the 1996 RSA Data Security Conference, the following *
11 * code is released into the public domain by its author. Prost! *
13 * This cipher uses 16-bit words and little-endian byte ordering. *
14 * I wonder which processor it was optimized for? *
16 * Thanks to CodeView, SoftIce, and D86 for helping bring this code to *
18 \**********************************************************************/
23 Implementation of RC2 with fixed effective key length of 64bits
28 const struct ltc_cipher_descriptor rc2_desc = {
37 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
40 /* 256-entry permutation table, probably derived somehow from pi */
41 static const unsigned char permute[256] = {
42 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
43 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
44 23,154, 89,245,135,179, 79, 19, 97, 69,109,141, 9,129,125, 50,
45 189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
46 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
47 18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,
48 111,191, 14,218, 70,105, 7, 87, 39,242, 29,155,188,148, 67, 3,
49 248, 17,199,246,144,239, 62,231, 6,195,213, 47,200,102, 30,215,
50 8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,
51 150, 26,210,113, 90, 21, 73,116, 75,159,208, 94, 4, 24,164,236,
52 194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,
53 153,124, 58,133, 35,184,180,122,252, 2, 54, 91, 37, 85,151, 49,
54 45, 93,250,152,227,138,146,174, 5,223, 41, 16,103,108,186,201,
55 211, 0,230,207,225,158,168, 44, 99, 22, 1, 63, 88,226,137,169,
56 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
57 197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173
61 Initialize the RC2 block cipher
62 @param key The symmetric key you wish to pass
63 @param keylen The key length in bytes
64 @param bits The effective key length in bits
65 @param num_rounds The number of rounds desired (0 for default)
66 @param skey The key in as scheduled by this function.
67 @return CRYPT_OK if successful
69 int rc2_setup_ex(const unsigned char *key, int keylen, int bits, int num_rounds, symmetric_key *skey)
71 unsigned *xkey = skey->rc2.xkey;
72 unsigned char tmp[128];
76 LTC_ARGCHK(key != NULL);
77 LTC_ARGCHK(skey != NULL);
79 if (keylen == 0 || keylen > 128 || bits > 1024) {
80 return CRYPT_INVALID_KEYSIZE;
86 if (num_rounds != 0 && num_rounds != 16) {
87 return CRYPT_INVALID_ROUNDS;
90 for (i = 0; i < keylen; i++) {
91 tmp[i] = key[i] & 255;
94 /* Phase 1: Expand input key to 128 bytes */
96 for (i = keylen; i < 128; i++) {
97 tmp[i] = permute[(tmp[i - 1] + tmp[i - keylen]) & 255];
101 /* Phase 2 - reduce effective key size to "bits" */
102 T8 = (unsigned)(bits+7)>>3;
103 TM = (255 >> (unsigned)(7 & -bits));
104 tmp[128 - T8] = permute[tmp[128 - T8] & TM];
105 for (i = 127 - T8; i >= 0; i--) {
106 tmp[i] = permute[tmp[i + 1] ^ tmp[i + T8]];
109 /* Phase 3 - copy to xkey in little-endian order */
110 for (i = 0; i < 64; i++) {
111 xkey[i] = (unsigned)tmp[2*i] + ((unsigned)tmp[2*i+1] << 8);
114 #ifdef LTC_CLEAN_STACK
115 zeromem(tmp, sizeof(tmp));
122 Initialize the RC2 block cipher
124 The effective key length is here always keylen * 8
126 @param key The symmetric key you wish to pass
127 @param keylen The key length in bytes
128 @param num_rounds The number of rounds desired (0 for default)
129 @param skey The key in as scheduled by this function.
130 @return CRYPT_OK if successful
132 int rc2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
134 return rc2_setup_ex(key, keylen, keylen * 8, num_rounds, skey);
137 /**********************************************************************\
138 * Encrypt an 8-byte block of plaintext using the given key. *
139 \**********************************************************************/
141 Encrypts a block of text with RC2
142 @param pt The input plaintext (8 bytes)
143 @param ct The output ciphertext (8 bytes)
144 @param skey The key as scheduled
145 @return CRYPT_OK if successful
147 #ifdef LTC_CLEAN_STACK
148 static int _rc2_ecb_encrypt( const unsigned char *pt,
152 int rc2_ecb_encrypt( const unsigned char *pt,
158 unsigned x76, x54, x32, x10, i;
160 LTC_ARGCHK(pt != NULL);
161 LTC_ARGCHK(ct != NULL);
162 LTC_ARGCHK(skey != NULL);
164 xkey = skey->rc2.xkey;
166 x76 = ((unsigned)pt[7] << 8) + (unsigned)pt[6];
167 x54 = ((unsigned)pt[5] << 8) + (unsigned)pt[4];
168 x32 = ((unsigned)pt[3] << 8) + (unsigned)pt[2];
169 x10 = ((unsigned)pt[1] << 8) + (unsigned)pt[0];
171 for (i = 0; i < 16; i++) {
172 x10 = (x10 + (x32 & ~x76) + (x54 & x76) + xkey[4*i+0]) & 0xFFFF;
173 x10 = ((x10 << 1) | (x10 >> 15));
175 x32 = (x32 + (x54 & ~x10) + (x76 & x10) + xkey[4*i+1]) & 0xFFFF;
176 x32 = ((x32 << 2) | (x32 >> 14));
178 x54 = (x54 + (x76 & ~x32) + (x10 & x32) + xkey[4*i+2]) & 0xFFFF;
179 x54 = ((x54 << 3) | (x54 >> 13));
181 x76 = (x76 + (x10 & ~x54) + (x32 & x54) + xkey[4*i+3]) & 0xFFFF;
182 x76 = ((x76 << 5) | (x76 >> 11));
184 if (i == 4 || i == 10) {
185 x10 = (x10 + xkey[x76 & 63]) & 0xFFFF;
186 x32 = (x32 + xkey[x10 & 63]) & 0xFFFF;
187 x54 = (x54 + xkey[x32 & 63]) & 0xFFFF;
188 x76 = (x76 + xkey[x54 & 63]) & 0xFFFF;
192 ct[0] = (unsigned char)x10;
193 ct[1] = (unsigned char)(x10 >> 8);
194 ct[2] = (unsigned char)x32;
195 ct[3] = (unsigned char)(x32 >> 8);
196 ct[4] = (unsigned char)x54;
197 ct[5] = (unsigned char)(x54 >> 8);
198 ct[6] = (unsigned char)x76;
199 ct[7] = (unsigned char)(x76 >> 8);
204 #ifdef LTC_CLEAN_STACK
205 int rc2_ecb_encrypt( const unsigned char *pt,
209 int err = _rc2_ecb_encrypt(pt, ct, skey);
210 burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 5);
215 /**********************************************************************\
216 * Decrypt an 8-byte block of ciphertext using the given key. *
217 \**********************************************************************/
219 Decrypts a block of text with RC2
220 @param ct The input ciphertext (8 bytes)
221 @param pt The output plaintext (8 bytes)
222 @param skey The key as scheduled
223 @return CRYPT_OK if successful
225 #ifdef LTC_CLEAN_STACK
226 static int _rc2_ecb_decrypt( const unsigned char *ct,
230 int rc2_ecb_decrypt( const unsigned char *ct,
235 unsigned x76, x54, x32, x10;
239 LTC_ARGCHK(pt != NULL);
240 LTC_ARGCHK(ct != NULL);
241 LTC_ARGCHK(skey != NULL);
243 xkey = skey->rc2.xkey;
245 x76 = ((unsigned)ct[7] << 8) + (unsigned)ct[6];
246 x54 = ((unsigned)ct[5] << 8) + (unsigned)ct[4];
247 x32 = ((unsigned)ct[3] << 8) + (unsigned)ct[2];
248 x10 = ((unsigned)ct[1] << 8) + (unsigned)ct[0];
250 for (i = 15; i >= 0; i--) {
251 if (i == 4 || i == 10) {
252 x76 = (x76 - xkey[x54 & 63]) & 0xFFFF;
253 x54 = (x54 - xkey[x32 & 63]) & 0xFFFF;
254 x32 = (x32 - xkey[x10 & 63]) & 0xFFFF;
255 x10 = (x10 - xkey[x76 & 63]) & 0xFFFF;
258 x76 = ((x76 << 11) | (x76 >> 5));
259 x76 = (x76 - ((x10 & ~x54) + (x32 & x54) + xkey[4*i+3])) & 0xFFFF;
261 x54 = ((x54 << 13) | (x54 >> 3));
262 x54 = (x54 - ((x76 & ~x32) + (x10 & x32) + xkey[4*i+2])) & 0xFFFF;
264 x32 = ((x32 << 14) | (x32 >> 2));
265 x32 = (x32 - ((x54 & ~x10) + (x76 & x10) + xkey[4*i+1])) & 0xFFFF;
267 x10 = ((x10 << 15) | (x10 >> 1));
268 x10 = (x10 - ((x32 & ~x76) + (x54 & x76) + xkey[4*i+0])) & 0xFFFF;
271 pt[0] = (unsigned char)x10;
272 pt[1] = (unsigned char)(x10 >> 8);
273 pt[2] = (unsigned char)x32;
274 pt[3] = (unsigned char)(x32 >> 8);
275 pt[4] = (unsigned char)x54;
276 pt[5] = (unsigned char)(x54 >> 8);
277 pt[6] = (unsigned char)x76;
278 pt[7] = (unsigned char)(x76 >> 8);
283 #ifdef LTC_CLEAN_STACK
284 int rc2_ecb_decrypt( const unsigned char *ct,
288 int err = _rc2_ecb_decrypt(ct, pt, skey);
289 burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 4 + sizeof(int));
295 Performs a self-test of the RC2 block cipher
296 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
303 static const struct {
305 unsigned char key[16], pt[8], ct[8];
309 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
311 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
312 { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff }
315 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
316 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
317 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
318 { 0x27, 0x8b, 0x27, 0xe4, 0x2e, 0x2f, 0x0d, 0x49 }
321 { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
322 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
323 { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
324 { 0x30, 0x64, 0x9e, 0xdf, 0x9b, 0xe7, 0xd2, 0xc2 }
327 { 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
328 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
329 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
330 { 0x61, 0xa8, 0xa2, 0x44, 0xad, 0xac, 0xcc, 0xf0 }
333 { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x00,
334 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
335 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
336 { 0x6c, 0xcf, 0x43, 0x08, 0x97, 0x4c, 0x26, 0x7f }
339 { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
340 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2 },
341 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
342 { 0x1a, 0x80, 0x7d, 0x27, 0x2b, 0xbe, 0x5d, 0xb1 }
345 { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
346 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2 },
347 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
348 { 0x22, 0x69, 0x55, 0x2a, 0xb0, 0xf8, 0x5c, 0xa6 }
353 unsigned char tmp[2][8];
355 for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
356 zeromem(tmp, sizeof(tmp));
357 if (tests[x].bits == (tests[x].keylen * 8)) {
358 if ((err = rc2_setup(tests[x].key, tests[x].keylen, 0, &skey)) != CRYPT_OK) {
363 if ((err = rc2_setup_ex(tests[x].key, tests[x].keylen, tests[x].bits, 0, &skey)) != CRYPT_OK) {
368 rc2_ecb_encrypt(tests[x].pt, tmp[0], &skey);
369 rc2_ecb_decrypt(tmp[0], tmp[1], &skey);
371 if (compare_testvector(tmp[0], 8, tests[x].ct, 8, "RC2 CT", x) ||
372 compare_testvector(tmp[1], 8, tests[x].pt, 8, "RC2 PT", x)) {
373 return CRYPT_FAIL_TESTVECTOR;
376 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
377 for (y = 0; y < 8; y++) tmp[0][y] = 0;
378 for (y = 0; y < 1000; y++) rc2_ecb_encrypt(tmp[0], tmp[0], &skey);
379 for (y = 0; y < 1000; y++) rc2_ecb_decrypt(tmp[0], tmp[0], &skey);
380 for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
386 /** Terminate the context
387 @param skey The scheduled key
389 void rc2_done(symmetric_key *skey)
391 LTC_UNUSED_PARAM(skey);
395 Gets suitable key size
396 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable.
397 @return CRYPT_OK if the input key size is acceptable.
399 int rc2_keysize(int *keysize)
401 LTC_ARGCHK(keysize != NULL);
403 return CRYPT_INVALID_KEYSIZE;
404 } else if (*keysize > 128) {
415 /* ref: $Format:%D$ */
416 /* git commit: $Format:%H$ */
417 /* commit time: $Format:%ai$ */