# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / sshd / libtomcrypt / rc5.c
1 #include "mycrypt.h"
2
3 #ifdef RC5
4
5 const struct _cipher_descriptor rc5_desc =
6 {
7     "rc5",
8     2,
9     8, 128, 8, 12,
10     &rc5_setup,
11     &rc5_ecb_encrypt,
12     &rc5_ecb_decrypt,
13     &rc5_test,
14     &rc5_keysize
15 };
16
17 #ifdef CLEAN_STACK
18 static int _rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
19 #else
20 int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
21 #endif
22 {
23     unsigned long L[64], S[50], A, B, i, j, v, s, t, l;
24
25     _ARGCHK(skey != NULL);
26     _ARGCHK(key != NULL);
27
28     /* test parameters */
29     if (num_rounds == 0) { 
30        num_rounds = rc5_desc.default_rounds;
31     }
32
33     if (num_rounds < 12 || num_rounds > 24) { 
34        return CRYPT_INVALID_ROUNDS;
35     }
36
37     /* key must be between 64 and 1024 bits */
38     if (keylen < 8 || keylen > 128) {
39        return CRYPT_INVALID_KEYSIZE;
40     }
41
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));
45         if ((i & 3) == 0) {
46            L[j++] = BSWAP(A);
47            A = 0;
48         }
49     }
50
51     if ((keylen & 3) != 0) { 
52        A <<= (unsigned long)((8 * (4 - (keylen&3)))); 
53        L[j++] = BSWAP(A);
54     }
55
56     /* setup the S array */
57     t = (unsigned long)(2 * (num_rounds + 1));
58     S[0] = 0xB7E15163UL;
59     for (i = 1; i < t; i++) S[i] = S[i - 1] + 0x9E3779B9UL;
60
61     /* mix buffer */
62     s = 3 * MAX(t, j);
63     l = j;
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));
67         i = (i + 1) % t;
68         j = (j + 1) % l;
69     }
70     
71     /* copy to key */
72     for (i = 0; i < t; i++) {
73         skey->rc5.K[i] = S[i];
74     }
75     skey->rc5.rounds = num_rounds;
76     return CRYPT_OK;
77 }
78
79 #ifdef CLEAN_STACK
80 int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
81 {
82    int x;
83    x = _rc5_setup(key, keylen, num_rounds, skey);
84    burn_stack(sizeof(unsigned long) * 122 + sizeof(int));
85    return x;
86 }
87 #endif
88
89 #ifdef CLEAN_STACK
90 static void _rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
91 #else
92 void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
93 #endif
94 {
95    unsigned long A, B;
96    int r;
97    _ARGCHK(key != NULL);
98    _ARGCHK(pt != NULL);
99    _ARGCHK(ct != NULL);
100
101    LOAD32L(A, &pt[0]);
102    LOAD32L(B, &pt[4]);
103    A += key->rc5.K[0];
104    B += key->rc5.K[1];
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];
108    }
109    STORE32L(A, &ct[0]);
110    STORE32L(B, &ct[4]);
111 }
112
113 #ifdef CLEAN_STACK
114 void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
115 {
116    _rc5_ecb_encrypt(pt, ct, key);
117    burn_stack(sizeof(unsigned long) * 2 + sizeof(int));
118 }
119 #endif
120
121 #ifdef CLEAN_STACK
122 static void _rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
123 #else
124 void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
125 #endif
126 {
127    unsigned long A, B;
128    int r;
129    _ARGCHK(key != NULL);
130    _ARGCHK(pt != NULL);
131    _ARGCHK(ct != NULL);
132
133    LOAD32L(A, &ct[0]);
134    LOAD32L(B, &ct[4]);
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;
138    }
139    A -= key->rc5.K[0];
140    B -= key->rc5.K[1];
141    STORE32L(A, &pt[0]);
142    STORE32L(B, &pt[4]);
143 }
144
145 #ifdef CLEAN_STACK
146 void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
147 {
148    _rc5_ecb_decrypt(ct, pt, key);
149    burn_stack(sizeof(unsigned long) * 2 + sizeof(int));
150 }
151 #endif
152
153 int rc5_test(void)
154 {
155  #ifndef LTC_TEST
156     return CRYPT_NOP;
157  #else    
158    static const struct {
159        unsigned char key[16], pt[8], ct[8];
160    } tests[] = {
161    {
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 }
166    },
167    {
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 }
172    },
173    {
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 }
178    }
179    };
180    unsigned char buf[2][8];
181    int x, err;
182    symmetric_key key;
183
184    for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
185       /* setup key */
186       if ((err = rc5_setup(tests[x].key, 16, 12, &key)) != CRYPT_OK) {
187          return err;
188       }
189
190       /* encrypt and decrypt */
191       rc5_ecb_encrypt(tests[x].pt, buf[0], &key);
192       rc5_ecb_decrypt(buf[0], buf[1], &key);
193
194       /* compare */
195       if (memcmp(buf[0], tests[x].ct, 8) != 0 || memcmp(buf[1], tests[x].pt, 8) != 0) {
196          return CRYPT_FAIL_TESTVECTOR;
197       }
198    }
199    return CRYPT_OK;
200   #endif
201 }
202
203 int rc5_keysize(int *desired_keysize)
204 {
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;
210    }
211    return CRYPT_OK;
212 }
213
214 #endif
215
216
217