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