# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / sshd / libtomcrypt / rc2.c
1 /**********************************************************************\
2 * To commemorate the 1996 RSA Data Security Conference, the following  *
3 * code is released into the public domain by its author.  Prost!       *
4 *                                                                      *
5 * This cipher uses 16-bit words and little-endian byte ordering.       *
6 * I wonder which processor it was optimized for?                       *
7 *                                                                      *
8 * Thanks to CodeView, SoftIce, and D86 for helping bring this code to  *
9 * the public.                                                          *
10 \**********************************************************************/
11
12 #include <mycrypt.h>
13
14 #ifdef RC2
15
16 const struct _cipher_descriptor rc2_desc = {
17    "rc2",
18    12, 8, 128, 8, 16,
19    &rc2_setup,
20    &rc2_ecb_encrypt,
21    &rc2_ecb_decrypt,
22    &rc2_test,
23    &rc2_keysize
24 };
25
26
27 /**********************************************************************\
28 * Expand a variable-length user key (between 1 and 128 bytes) to a     *
29 * 64-short working rc2 key, of at most "bits" effective key bits.      *
30 * The effective key bits parameter looks like an export control hack.  *
31 * For normal use, it should always be set to 1024.  For convenience,   *
32 * zero is accepted as an alias for 1024.                               *
33 \**********************************************************************/
34
35    /* 256-entry permutation table, probably derived somehow from pi */
36     static const unsigned char permute[256] = {
37         217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
38         198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
39          23,154, 89,245,135,179, 79, 19, 97, 69,109,141,  9,129,125, 50,
40         189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
41          84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
42          18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,
43         111,191, 14,218, 70,105,  7, 87, 39,242, 29,155,188,148, 67,  3,
44         248, 17,199,246,144,239, 62,231,  6,195,213, 47,200,102, 30,215,
45           8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,
46         150, 26,210,113, 90, 21, 73,116, 75,159,208, 94,  4, 24,164,236,
47         194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,
48         153,124, 58,133, 35,184,180,122,252,  2, 54, 91, 37, 85,151, 49,
49          45, 93,250,152,227,138,146,174,  5,223, 41, 16,103,108,186,201,
50         211,  0,230,207,225,158,168, 44, 99, 22,  1, 63, 88,226,137,169,
51          13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
52         197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173
53     };
54
55 int rc2_setup(const unsigned char *key, int keylen, int rounds, symmetric_key *skey)
56 {
57    unsigned *xkey = skey->rc2.xkey;
58    unsigned char tmp[128];
59    unsigned T8, TM;
60    int i, bits;
61
62    _ARGCHK(key != NULL);
63    _ARGCHK(skey != NULL);
64
65    if (keylen < 8 || keylen > 128) {
66       return CRYPT_INVALID_KEYSIZE;
67    }
68
69    if (rounds != 0 && rounds != 16) {
70       return CRYPT_INVALID_ROUNDS;
71    }
72
73    for (i = 0; i < keylen; i++) {
74        tmp[i] = key[i];
75    }
76
77     /* Phase 1: Expand input key to 128 bytes */
78     if (keylen < 128) {
79         for (i = keylen; i < 128; i++) {
80             tmp[i] = permute[(int)((tmp[i - 1] + tmp[i - keylen]) & 255)];
81         }
82     }
83     
84     /* Phase 2 - reduce effective key size to "bits" */
85     bits = keylen*8;
86     T8   = (unsigned)(bits+7)>>3;
87     TM   = (255 >> (unsigned)(7 & -bits));
88     tmp[128 - T8] = permute[(int)(tmp[128 - T8] & TM)];
89     for (i = 127 - T8; i >= 0; i--) {
90         tmp[i] = permute[(int)(tmp[i + 1] ^ tmp[i + T8])];
91     }
92
93     /* Phase 3 - copy to xkey in little-endian order */
94     i = 63;
95     do {
96         xkey[i] =  (unsigned)tmp[2*i] + ((unsigned)tmp[2*i+1] << 8);
97     } while (i-- > 0);
98
99 #ifdef CLEAN_STACK
100     zeromem(tmp, sizeof(tmp));
101 #endif
102     
103     return CRYPT_OK;
104 }
105
106 /**********************************************************************\
107 * Encrypt an 8-byte block of plaintext using the given key.            *
108 \**********************************************************************/
109 #ifdef CLEAN_STACK
110 static void _rc2_ecb_encrypt( const unsigned char *plain,
111                             unsigned char *cipher,
112                             symmetric_key *skey)
113 #else
114 void rc2_ecb_encrypt( const unsigned char *plain,
115                             unsigned char *cipher,
116                             symmetric_key *skey)
117 #endif
118 {
119     unsigned *xkey = skey->rc2.xkey;
120     unsigned x76, x54, x32, x10, i;
121
122     _ARGCHK(plain != NULL);
123     _ARGCHK(cipher != NULL);
124     _ARGCHK(skey != NULL);
125
126     x76 = ((unsigned)plain[7] << 8) + (unsigned)plain[6];
127     x54 = ((unsigned)plain[5] << 8) + (unsigned)plain[4];
128     x32 = ((unsigned)plain[3] << 8) + (unsigned)plain[2];
129     x10 = ((unsigned)plain[1] << 8) + (unsigned)plain[0];
130
131     for (i = 0; i < 16; i++) {
132         x10 = (x10 + (x32 & ~x76) + (x54 & x76) + xkey[4*i+0]) & 0xFFFF;
133         x10 = ((x10 << 1) | (x10 >> 15)) & 0xFFFF;
134
135         x32 = (x32 + (x54 & ~x10) + (x76 & x10) + xkey[4*i+1]) & 0xFFFF;
136         x32 = ((x32 << 2) | (x32 >> 14)) & 0xFFFF;
137
138         x54 = (x54 + (x76 & ~x32) + (x10 & x32) + xkey[4*i+2]) & 0xFFFF;
139         x54 = ((x54 << 3) | (x54 >> 13)) & 0xFFFF;
140
141         x76 = (x76 + (x10 & ~x54) + (x32 & x54) + xkey[4*i+3]) & 0xFFFF;
142         x76 = ((x76 << 5) | (x76 >> 11)) & 0xFFFF;
143
144         if (i == 4 || i == 10) {
145             x10 = (x10 + xkey[x76 & 63]) & 0xFFFF;
146             x32 = (x32 + xkey[x10 & 63]) & 0xFFFF;
147             x54 = (x54 + xkey[x32 & 63]) & 0xFFFF;
148             x76 = (x76 + xkey[x54 & 63]) & 0xFFFF;
149         }
150     }
151
152     cipher[0] = (unsigned char)x10;
153     cipher[1] = (unsigned char)(x10 >> 8);
154     cipher[2] = (unsigned char)x32;
155     cipher[3] = (unsigned char)(x32 >> 8);
156     cipher[4] = (unsigned char)x54;
157     cipher[5] = (unsigned char)(x54 >> 8);
158     cipher[6] = (unsigned char)x76;
159     cipher[7] = (unsigned char)(x76 >> 8);
160 }
161
162 #ifdef CLEAN_STACK
163 void rc2_ecb_encrypt( const unsigned char *plain,
164                             unsigned char *cipher,
165                             symmetric_key *skey)
166 {
167     _rc2_ecb_encrypt(plain, cipher, skey);
168     burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 5);
169 }
170 #endif
171
172 /**********************************************************************\
173 * Decrypt an 8-byte block of ciphertext using the given key.           *
174 \**********************************************************************/
175
176 #ifdef CLEAN_STACK
177 static void _rc2_ecb_decrypt( const unsigned char *cipher,
178                             unsigned char *plain,
179                             symmetric_key *skey)
180 #else
181 void rc2_ecb_decrypt( const unsigned char *cipher,
182                             unsigned char *plain,
183                             symmetric_key *skey)
184 #endif
185 {
186     unsigned x76, x54, x32, x10;
187     unsigned *xkey = skey->rc2.xkey;
188     int i;
189
190     _ARGCHK(plain != NULL);
191     _ARGCHK(cipher != NULL);
192     _ARGCHK(skey != NULL);
193
194     x76 = ((unsigned)cipher[7] << 8) + (unsigned)cipher[6];
195     x54 = ((unsigned)cipher[5] << 8) + (unsigned)cipher[4];
196     x32 = ((unsigned)cipher[3] << 8) + (unsigned)cipher[2];
197     x10 = ((unsigned)cipher[1] << 8) + (unsigned)cipher[0];
198
199     for (i = 15; i >= 0; i--) {
200         if (i == 4 || i == 10) {
201             x76 = (x76 - xkey[x54 & 63]) & 0xFFFF;
202             x54 = (x54 - xkey[x32 & 63]) & 0xFFFF;
203             x32 = (x32 - xkey[x10 & 63]) & 0xFFFF;
204             x10 = (x10 - xkey[x76 & 63]) & 0xFFFF;
205         }
206
207         x76 = ((x76 << 11) | (x76 >> 5)) & 0xFFFF;
208         x76 = (x76 - ((x10 & ~x54) + (x32 & x54) + xkey[4*i+3])) & 0xFFFF;
209
210         x54 = ((x54 << 13) | (x54 >> 3)) & 0xFFFF;
211         x54 = (x54 - ((x76 & ~x32) + (x10 & x32) + xkey[4*i+2])) & 0xFFFF;
212
213         x32 = ((x32 << 14) | (x32 >> 2)) & 0xFFFF;
214         x32 = (x32 - ((x54 & ~x10) + (x76 & x10) + xkey[4*i+1])) & 0xFFFF;
215
216         x10 = ((x10 << 15) | (x10 >> 1)) & 0xFFFF;
217         x10 = (x10 - ((x32 & ~x76) + (x54 & x76) + xkey[4*i+0])) & 0xFFFF;
218     }
219
220     plain[0] = (unsigned char)x10;
221     plain[1] = (unsigned char)(x10 >> 8);
222     plain[2] = (unsigned char)x32;
223     plain[3] = (unsigned char)(x32 >> 8);
224     plain[4] = (unsigned char)x54;
225     plain[5] = (unsigned char)(x54 >> 8);
226     plain[6] = (unsigned char)x76;
227     plain[7] = (unsigned char)(x76 >> 8);
228 }
229
230 #ifdef CLEAN_STACK
231 void rc2_ecb_decrypt( const unsigned char *cipher,
232                             unsigned char *plain,
233                             symmetric_key *skey)
234 {
235     _rc2_ecb_decrypt(cipher, plain, skey);
236     burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 4 + sizeof(int));
237 }
238 #endif
239
240 int rc2_test(void)
241 {
242  #ifndef LTC_TEST
243     return CRYPT_NOP;
244  #else    
245    static const struct {
246         int keylen;
247         unsigned char key[16], pt[8], ct[8];
248    } tests[] = {
249
250    { 8,
251      { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
253      { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
254      { 0x30, 0x64, 0x9e, 0xdf, 0x9b, 0xe7, 0xd2, 0xc2 }
255
256    },
257    { 16,
258      { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
259        0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2 },
260      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
261      { 0x22, 0x69, 0x55, 0x2a, 0xb0, 0xf8, 0x5c, 0xa6 }
262    }
263   };
264     int x, err;
265     symmetric_key skey;
266     unsigned char buf[2][8];
267
268     for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
269         zeromem(buf, sizeof(buf));
270         if ((err = rc2_setup(tests[x].key, tests[x].keylen, 0, &skey)) != CRYPT_OK) {
271            return err;
272         }
273         
274         rc2_ecb_encrypt(tests[x].pt, buf[0], &skey);
275         rc2_ecb_decrypt(buf[0], buf[1], &skey);
276         
277         if (memcmp(buf[0], tests[x].ct, 8) != 0 || memcmp(buf[1], tests[x].pt, 8) != 0) {
278            return CRYPT_FAIL_TESTVECTOR;
279         }
280     }
281     return CRYPT_OK;
282    #endif
283 }
284
285 int rc2_keysize(int *keysize)
286 {
287    _ARGCHK(keysize != NULL);
288    if (*keysize < 8) {
289        return CRYPT_INVALID_KEYSIZE;
290    } else if (*keysize > 128) {
291        *keysize = 128;
292    }
293    return CRYPT_OK;
294 }
295
296 #endif
297
298
299