]> pd.if.org Git - zpackage/blob - libtomcrypt/src/ciphers/rc6.c
remove xcbc
[zpackage] / libtomcrypt / src / ciphers / rc6.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9
10 /**
11    @file rc6.c
12    LTC_RC6 code by Tom St Denis
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_RC6
17
18 const struct ltc_cipher_descriptor rc6_desc =
19 {
20     "rc6",
21     3,
22     8, 128, 16, 20,
23     &rc6_setup,
24     &rc6_ecb_encrypt,
25     &rc6_ecb_decrypt,
26     &rc6_test,
27     &rc6_done,
28     &rc6_keysize,
29     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
30 };
31
32 static const ulong32 stab[44] = {
33 0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL,
34 0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL,
35 0x9b58ecf3UL, 0x399066acUL, 0xd7c7e065UL, 0x75ff5a1eUL, 0x1436d3d7UL, 0xb26e4d90UL, 0x50a5c749UL, 0xeedd4102UL,
36 0x8d14babbUL, 0x2b4c3474UL, 0xc983ae2dUL, 0x67bb27e6UL, 0x05f2a19fUL, 0xa42a1b58UL, 0x42619511UL, 0xe0990ecaUL,
37 0x7ed08883UL, 0x1d08023cUL, 0xbb3f7bf5UL, 0x5976f5aeUL, 0xf7ae6f67UL, 0x95e5e920UL, 0x341d62d9UL, 0xd254dc92UL,
38 0x708c564bUL, 0x0ec3d004UL, 0xacfb49bdUL, 0x4b32c376UL };
39
40  /**
41     Initialize the LTC_RC6 block cipher
42     @param key The symmetric key you wish to pass
43     @param keylen The key length in bytes
44     @param num_rounds The number of rounds desired (0 for default)
45     @param skey The key in as scheduled by this function.
46     @return CRYPT_OK if successful
47  */
48 #ifdef LTC_CLEAN_STACK
49 static int _rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
50 #else
51 int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
52 #endif
53 {
54     ulong32 L[64], S[50], A, B, i, j, v, s, l;
55
56     LTC_ARGCHK(key != NULL);
57     LTC_ARGCHK(skey != NULL);
58
59     /* test parameters */
60     if (num_rounds != 0 && num_rounds != 20) {
61        return CRYPT_INVALID_ROUNDS;
62     }
63
64     /* key must be between 64 and 1024 bits */
65     if (keylen < 8 || keylen > 128) {
66        return CRYPT_INVALID_KEYSIZE;
67     }
68
69     /* copy the key into the L array */
70     for (A = i = j = 0; i < (ulong32)keylen; ) {
71         A = (A << 8) | ((ulong32)(key[i++] & 255));
72         if (!(i & 3)) {
73            L[j++] = BSWAP(A);
74            A = 0;
75         }
76     }
77
78     /* handle odd sized keys */
79     if (keylen & 3) {
80        A <<= (8 * (4 - (keylen&3)));
81        L[j++] = BSWAP(A);
82     }
83
84     /* setup the S array */
85     XMEMCPY(S, stab, 44 * sizeof(stab[0]));
86
87     /* mix buffer */
88     s = 3 * MAX(44, j);
89     l = j;
90     for (A = B = i = j = v = 0; v < s; v++) {
91         A = S[i] = ROLc(S[i] + A + B, 3);
92         B = L[j] = ROL(L[j] + A + B, (A+B));
93         if (++i == 44) { i = 0; }
94         if (++j == l)  { j = 0; }
95     }
96
97     /* copy to key */
98     for (i = 0; i < 44; i++) {
99         skey->rc6.K[i] = S[i];
100     }
101     return CRYPT_OK;
102 }
103
104 #ifdef LTC_CLEAN_STACK
105 int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
106 {
107    int x;
108    x = _rc6_setup(key, keylen, num_rounds, skey);
109    burn_stack(sizeof(ulong32) * 122);
110    return x;
111 }
112 #endif
113
114 /**
115   Encrypts a block of text with LTC_RC6
116   @param pt The input plaintext (16 bytes)
117   @param ct The output ciphertext (16 bytes)
118   @param skey The key as scheduled
119 */
120 #ifdef LTC_CLEAN_STACK
121 static int _rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
122 #else
123 int rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
124 #endif
125 {
126    ulong32 a,b,c,d,t,u, *K;
127    int r;
128
129    LTC_ARGCHK(skey != NULL);
130    LTC_ARGCHK(pt   != NULL);
131    LTC_ARGCHK(ct   != NULL);
132    LOAD32L(a,&pt[0]);LOAD32L(b,&pt[4]);LOAD32L(c,&pt[8]);LOAD32L(d,&pt[12]);
133
134    b += skey->rc6.K[0];
135    d += skey->rc6.K[1];
136
137 #define RND(a,b,c,d) \
138        t = (b * (b + b + 1)); t = ROLc(t, 5); \
139        u = (d * (d + d + 1)); u = ROLc(u, 5); \
140        a = ROL(a^t,u) + K[0];                \
141        c = ROL(c^u,t) + K[1]; K += 2;
142
143    K = skey->rc6.K + 2;
144    for (r = 0; r < 20; r += 4) {
145        RND(a,b,c,d);
146        RND(b,c,d,a);
147        RND(c,d,a,b);
148        RND(d,a,b,c);
149    }
150
151 #undef RND
152
153    a += skey->rc6.K[42];
154    c += skey->rc6.K[43];
155    STORE32L(a,&ct[0]);STORE32L(b,&ct[4]);STORE32L(c,&ct[8]);STORE32L(d,&ct[12]);
156    return CRYPT_OK;
157 }
158
159 #ifdef LTC_CLEAN_STACK
160 int rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
161 {
162    int err = _rc6_ecb_encrypt(pt, ct, skey);
163    burn_stack(sizeof(ulong32) * 6 + sizeof(int));
164    return err;
165 }
166 #endif
167
168 /**
169   Decrypts a block of text with LTC_RC6
170   @param ct The input ciphertext (16 bytes)
171   @param pt The output plaintext (16 bytes)
172   @param skey The key as scheduled
173 */
174 #ifdef LTC_CLEAN_STACK
175 static int _rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
176 #else
177 int rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
178 #endif
179 {
180    ulong32 a,b,c,d,t,u, *K;
181    int r;
182
183    LTC_ARGCHK(skey != NULL);
184    LTC_ARGCHK(pt   != NULL);
185    LTC_ARGCHK(ct   != NULL);
186
187    LOAD32L(a,&ct[0]);LOAD32L(b,&ct[4]);LOAD32L(c,&ct[8]);LOAD32L(d,&ct[12]);
188    a -= skey->rc6.K[42];
189    c -= skey->rc6.K[43];
190
191 #define RND(a,b,c,d) \
192        t = (b * (b + b + 1)); t = ROLc(t, 5); \
193        u = (d * (d + d + 1)); u = ROLc(u, 5); \
194        c = ROR(c - K[1], t) ^ u; \
195        a = ROR(a - K[0], u) ^ t; K -= 2;
196
197    K = skey->rc6.K + 40;
198
199    for (r = 0; r < 20; r += 4) {
200        RND(d,a,b,c);
201        RND(c,d,a,b);
202        RND(b,c,d,a);
203        RND(a,b,c,d);
204    }
205
206 #undef RND
207
208    b -= skey->rc6.K[0];
209    d -= skey->rc6.K[1];
210    STORE32L(a,&pt[0]);STORE32L(b,&pt[4]);STORE32L(c,&pt[8]);STORE32L(d,&pt[12]);
211
212    return CRYPT_OK;
213 }
214
215 #ifdef LTC_CLEAN_STACK
216 int rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
217 {
218    int err = _rc6_ecb_decrypt(ct, pt, skey);
219    burn_stack(sizeof(ulong32) * 6 + sizeof(int));
220    return err;
221 }
222 #endif
223
224 /**
225   Performs a self-test of the LTC_RC6 block cipher
226   @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
227 */
228 int rc6_test(void)
229 {
230  #ifndef LTC_TEST
231     return CRYPT_NOP;
232  #else
233    static const struct {
234        int keylen;
235        unsigned char key[32], pt[16], ct[16];
236    } tests[] = {
237    {
238        16,
239        { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
240          0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
241          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
243        { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79,
244          0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 },
245        { 0x52, 0x4e, 0x19, 0x2f, 0x47, 0x15, 0xc6, 0x23,
246          0x1f, 0x51, 0xf6, 0x36, 0x7e, 0xa4, 0x3f, 0x18 }
247    },
248    {
249        24,
250        { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
251          0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
252          0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0,
253          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
254        { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79,
255          0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 },
256        { 0x68, 0x83, 0x29, 0xd0, 0x19, 0xe5, 0x05, 0x04,
257          0x1e, 0x52, 0xe9, 0x2a, 0xf9, 0x52, 0x91, 0xd4 }
258    },
259    {
260        32,
261        { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
262          0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
263          0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0,
264          0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe },
265        { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79,
266          0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 },
267        { 0xc8, 0x24, 0x18, 0x16, 0xf0, 0xd7, 0xe4, 0x89,
268          0x20, 0xad, 0x16, 0xa1, 0x67, 0x4e, 0x5d, 0x48 }
269    }
270    };
271    unsigned char tmp[2][16];
272    int x, y, err;
273    symmetric_key key;
274
275    for (x  = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
276       /* setup key */
277       if ((err = rc6_setup(tests[x].key, tests[x].keylen, 0, &key)) != CRYPT_OK) {
278          return err;
279       }
280
281       /* encrypt and decrypt */
282       rc6_ecb_encrypt(tests[x].pt, tmp[0], &key);
283       rc6_ecb_decrypt(tmp[0], tmp[1], &key);
284
285       /* compare */
286       if (compare_testvector(tmp[0], 16, tests[x].ct, 16, "RC6 Encrypt", x) ||
287             compare_testvector(tmp[1], 16, tests[x].pt, 16, "RC6 Decrypt", x)) {
288          return CRYPT_FAIL_TESTVECTOR;
289       }
290
291       /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
292       for (y = 0; y < 16; y++) tmp[0][y] = 0;
293       for (y = 0; y < 1000; y++) rc6_ecb_encrypt(tmp[0], tmp[0], &key);
294       for (y = 0; y < 1000; y++) rc6_ecb_decrypt(tmp[0], tmp[0], &key);
295       for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
296    }
297    return CRYPT_OK;
298   #endif
299 }
300
301 /** Terminate the context
302    @param skey    The scheduled key
303 */
304 void rc6_done(symmetric_key *skey)
305 {
306   LTC_UNUSED_PARAM(skey);
307 }
308
309 /**
310   Gets suitable key size
311   @param keysize [in/out] The length of the recommended key (in bytes).  This function will store the suitable size back in this variable.
312   @return CRYPT_OK if the input key size is acceptable.
313 */
314 int rc6_keysize(int *keysize)
315 {
316    LTC_ARGCHK(keysize != NULL);
317    if (*keysize < 8) {
318       return CRYPT_INVALID_KEYSIZE;
319    } else if (*keysize > 128) {
320       *keysize = 128;
321    }
322    return CRYPT_OK;
323 }
324
325 #endif /*LTC_RC6*/
326
327
328
329 /* ref:         $Format:%D$ */
330 /* git commit:  $Format:%H$ */
331 /* commit time: $Format:%ai$ */