Remove unnecessary defines
[libeap.git] / src / crypto / crypto_internal.c
1 /*
2  * WPA Supplicant / Crypto wrapper for internal crypto implementation
3  * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "crypto.h"
19 #include "md5.h"
20 #include "sha1.h"
21 #include "aes.h"
22 #include "tls/rsa.h"
23 #include "tls/bignum.h"
24 #include "tls/pkcs1.h"
25 #include "tls/pkcs8.h"
26 #include "sha1_i.h"
27 #include "md5_i.h"
28 #include "des_i.h"
29
30 struct crypto_hash {
31         enum crypto_hash_alg alg;
32         union {
33                 struct MD5Context md5;
34                 struct SHA1Context sha1;
35         } u;
36         u8 key[64];
37         size_t key_len;
38 };
39
40
41 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
42                                       size_t key_len)
43 {
44         struct crypto_hash *ctx;
45         u8 k_pad[64];
46         u8 tk[20];
47         size_t i;
48
49         ctx = os_zalloc(sizeof(*ctx));
50         if (ctx == NULL)
51                 return NULL;
52
53         ctx->alg = alg;
54
55         switch (alg) {
56         case CRYPTO_HASH_ALG_MD5:
57                 MD5Init(&ctx->u.md5);
58                 break;
59         case CRYPTO_HASH_ALG_SHA1:
60                 SHA1Init(&ctx->u.sha1);
61                 break;
62         case CRYPTO_HASH_ALG_HMAC_MD5:
63                 if (key_len > sizeof(k_pad)) {
64                         MD5Init(&ctx->u.md5);
65                         MD5Update(&ctx->u.md5, key, key_len);
66                         MD5Final(tk, &ctx->u.md5);
67                         key = tk;
68                         key_len = 16;
69                 }
70                 os_memcpy(ctx->key, key, key_len);
71                 ctx->key_len = key_len;
72
73                 os_memcpy(k_pad, key, key_len);
74                 os_memset(k_pad + key_len, 0, sizeof(k_pad) - key_len);
75                 for (i = 0; i < sizeof(k_pad); i++)
76                         k_pad[i] ^= 0x36;
77                 MD5Init(&ctx->u.md5);
78                 MD5Update(&ctx->u.md5, k_pad, sizeof(k_pad));
79                 break;
80         case CRYPTO_HASH_ALG_HMAC_SHA1:
81                 if (key_len > sizeof(k_pad)) {
82                         SHA1Init(&ctx->u.sha1);
83                         SHA1Update(&ctx->u.sha1, key, key_len);
84                         SHA1Final(tk, &ctx->u.sha1);
85                         key = tk;
86                         key_len = 20;
87                 }
88                 os_memcpy(ctx->key, key, key_len);
89                 ctx->key_len = key_len;
90
91                 os_memcpy(k_pad, key, key_len);
92                 os_memset(k_pad + key_len, 0, sizeof(k_pad) - key_len);
93                 for (i = 0; i < sizeof(k_pad); i++)
94                         k_pad[i] ^= 0x36;
95                 SHA1Init(&ctx->u.sha1);
96                 SHA1Update(&ctx->u.sha1, k_pad, sizeof(k_pad));
97                 break;
98         default:
99                 os_free(ctx);
100                 return NULL;
101         }
102
103         return ctx;
104 }
105
106
107 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
108 {
109         if (ctx == NULL)
110                 return;
111
112         switch (ctx->alg) {
113         case CRYPTO_HASH_ALG_MD5:
114         case CRYPTO_HASH_ALG_HMAC_MD5:
115                 MD5Update(&ctx->u.md5, data, len);
116                 break;
117         case CRYPTO_HASH_ALG_SHA1:
118         case CRYPTO_HASH_ALG_HMAC_SHA1:
119                 SHA1Update(&ctx->u.sha1, data, len);
120                 break;
121         }
122 }
123
124
125 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
126 {
127         u8 k_pad[64];
128         size_t i;
129
130         if (ctx == NULL)
131                 return -2;
132
133         if (mac == NULL || len == NULL) {
134                 os_free(ctx);
135                 return 0;
136         }
137
138         switch (ctx->alg) {
139         case CRYPTO_HASH_ALG_MD5:
140                 if (*len < 16) {
141                         *len = 16;
142                         os_free(ctx);
143                         return -1;
144                 }
145                 *len = 16;
146                 MD5Final(mac, &ctx->u.md5);
147                 break;
148         case CRYPTO_HASH_ALG_SHA1:
149                 if (*len < 20) {
150                         *len = 20;
151                         os_free(ctx);
152                         return -1;
153                 }
154                 *len = 20;
155                 SHA1Final(mac, &ctx->u.sha1);
156                 break;
157         case CRYPTO_HASH_ALG_HMAC_MD5:
158                 if (*len < 16) {
159                         *len = 16;
160                         os_free(ctx);
161                         return -1;
162                 }
163                 *len = 16;
164
165                 MD5Final(mac, &ctx->u.md5);
166
167                 os_memcpy(k_pad, ctx->key, ctx->key_len);
168                 os_memset(k_pad + ctx->key_len, 0,
169                           sizeof(k_pad) - ctx->key_len);
170                 for (i = 0; i < sizeof(k_pad); i++)
171                         k_pad[i] ^= 0x5c;
172                 MD5Init(&ctx->u.md5);
173                 MD5Update(&ctx->u.md5, k_pad, sizeof(k_pad));
174                 MD5Update(&ctx->u.md5, mac, 16);
175                 MD5Final(mac, &ctx->u.md5);
176                 break;
177         case CRYPTO_HASH_ALG_HMAC_SHA1:
178                 if (*len < 20) {
179                         *len = 20;
180                         os_free(ctx);
181                         return -1;
182                 }
183                 *len = 20;
184
185                 SHA1Final(mac, &ctx->u.sha1);
186
187                 os_memcpy(k_pad, ctx->key, ctx->key_len);
188                 os_memset(k_pad + ctx->key_len, 0,
189                           sizeof(k_pad) - ctx->key_len);
190                 for (i = 0; i < sizeof(k_pad); i++)
191                         k_pad[i] ^= 0x5c;
192                 SHA1Init(&ctx->u.sha1);
193                 SHA1Update(&ctx->u.sha1, k_pad, sizeof(k_pad));
194                 SHA1Update(&ctx->u.sha1, mac, 20);
195                 SHA1Final(mac, &ctx->u.sha1);
196                 break;
197         }
198
199         os_free(ctx);
200
201         return 0;
202 }
203
204
205 struct crypto_cipher {
206         enum crypto_cipher_alg alg;
207         union {
208                 struct {
209                         size_t used_bytes;
210                         u8 key[16];
211                         size_t keylen;
212                 } rc4;
213                 struct {
214                         u8 cbc[32];
215                         size_t block_size;
216                         void *ctx_enc;
217                         void *ctx_dec;
218                 } aes;
219                 struct {
220                         struct des3_key_s key;
221                         u8 cbc[8];
222                 } des3;
223                 struct {
224                         u32 ek[32];
225                         u32 dk[32];
226                         u8 cbc[8];
227                 } des;
228         } u;
229 };
230
231
232 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
233                                           const u8 *iv, const u8 *key,
234                                           size_t key_len)
235 {
236         struct crypto_cipher *ctx;
237
238         ctx = os_zalloc(sizeof(*ctx));
239         if (ctx == NULL)
240                 return NULL;
241
242         ctx->alg = alg;
243
244         switch (alg) {
245         case CRYPTO_CIPHER_ALG_RC4:
246                 if (key_len > sizeof(ctx->u.rc4.key)) {
247                         os_free(ctx);
248                         return NULL;
249                 }
250                 ctx->u.rc4.keylen = key_len;
251                 os_memcpy(ctx->u.rc4.key, key, key_len);
252                 break;
253         case CRYPTO_CIPHER_ALG_AES:
254                 if (key_len > sizeof(ctx->u.aes.cbc)) {
255                         os_free(ctx);
256                         return NULL;
257                 }
258                 ctx->u.aes.ctx_enc = aes_encrypt_init(key, key_len);
259                 if (ctx->u.aes.ctx_enc == NULL) {
260                         os_free(ctx);
261                         return NULL;
262                 }
263                 ctx->u.aes.ctx_dec = aes_decrypt_init(key, key_len);
264                 if (ctx->u.aes.ctx_dec == NULL) {
265                         aes_encrypt_deinit(ctx->u.aes.ctx_enc);
266                         os_free(ctx);
267                         return NULL;
268                 }
269                 ctx->u.aes.block_size = key_len;
270                 os_memcpy(ctx->u.aes.cbc, iv, ctx->u.aes.block_size);
271                 break;
272         case CRYPTO_CIPHER_ALG_3DES:
273                 if (key_len != 24) {
274                         os_free(ctx);
275                         return NULL;
276                 }
277                 des3_key_setup(key, &ctx->u.des3.key);
278                 os_memcpy(ctx->u.des3.cbc, iv, 8);
279                 break;
280         case CRYPTO_CIPHER_ALG_DES:
281                 if (key_len != 8) {
282                         os_free(ctx);
283                         return NULL;
284                 }
285                 des_key_setup(key, ctx->u.des.ek, ctx->u.des.dk);
286                 os_memcpy(ctx->u.des.cbc, iv, 8);
287                 break;
288         default:
289                 os_free(ctx);
290                 return NULL;
291         }
292
293         return ctx;
294 }
295
296
297 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
298                           u8 *crypt, size_t len)
299 {
300         size_t i, j, blocks;
301
302         switch (ctx->alg) {
303         case CRYPTO_CIPHER_ALG_RC4:
304                 if (plain != crypt)
305                         os_memcpy(crypt, plain, len);
306                 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
307                          ctx->u.rc4.used_bytes, crypt, len);
308                 ctx->u.rc4.used_bytes += len;
309                 break;
310         case CRYPTO_CIPHER_ALG_AES:
311                 if (len % ctx->u.aes.block_size)
312                         return -1;
313                 blocks = len / ctx->u.aes.block_size;
314                 for (i = 0; i < blocks; i++) {
315                         for (j = 0; j < ctx->u.aes.block_size; j++)
316                                 ctx->u.aes.cbc[j] ^= plain[j];
317                         aes_encrypt(ctx->u.aes.ctx_enc, ctx->u.aes.cbc,
318                                     ctx->u.aes.cbc);
319                         os_memcpy(crypt, ctx->u.aes.cbc,
320                                   ctx->u.aes.block_size);
321                         plain += ctx->u.aes.block_size;
322                         crypt += ctx->u.aes.block_size;
323                 }
324                 break;
325         case CRYPTO_CIPHER_ALG_3DES:
326                 if (len % 8)
327                         return -1;
328                 blocks = len / 8;
329                 for (i = 0; i < blocks; i++) {
330                         for (j = 0; j < 8; j++)
331                                 ctx->u.des3.cbc[j] ^= plain[j];
332                         des3_encrypt(ctx->u.des3.cbc, &ctx->u.des3.key,
333                                      ctx->u.des3.cbc);
334                         os_memcpy(crypt, ctx->u.des3.cbc, 8);
335                         plain += 8;
336                         crypt += 8;
337                 }
338                 break;
339         case CRYPTO_CIPHER_ALG_DES:
340                 if (len % 8)
341                         return -1;
342                 blocks = len / 8;
343                 for (i = 0; i < blocks; i++) {
344                         for (j = 0; j < 8; j++)
345                                 ctx->u.des3.cbc[j] ^= plain[j];
346                         des_block_encrypt(ctx->u.des.cbc, ctx->u.des.ek,
347                                           ctx->u.des.cbc);
348                         os_memcpy(crypt, ctx->u.des.cbc, 8);
349                         plain += 8;
350                         crypt += 8;
351                 }
352                 break;
353         default:
354                 return -1;
355         }
356
357         return 0;
358 }
359
360
361 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
362                           u8 *plain, size_t len)
363 {
364         size_t i, j, blocks;
365         u8 tmp[32];
366
367         switch (ctx->alg) {
368         case CRYPTO_CIPHER_ALG_RC4:
369                 if (plain != crypt)
370                         os_memcpy(plain, crypt, len);
371                 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
372                          ctx->u.rc4.used_bytes, plain, len);
373                 ctx->u.rc4.used_bytes += len;
374                 break;
375         case CRYPTO_CIPHER_ALG_AES:
376                 if (len % ctx->u.aes.block_size)
377                         return -1;
378                 blocks = len / ctx->u.aes.block_size;
379                 for (i = 0; i < blocks; i++) {
380                         os_memcpy(tmp, crypt, ctx->u.aes.block_size);
381                         aes_decrypt(ctx->u.aes.ctx_dec, crypt, plain);
382                         for (j = 0; j < ctx->u.aes.block_size; j++)
383                                 plain[j] ^= ctx->u.aes.cbc[j];
384                         os_memcpy(ctx->u.aes.cbc, tmp, ctx->u.aes.block_size);
385                         plain += ctx->u.aes.block_size;
386                         crypt += ctx->u.aes.block_size;
387                 }
388                 break;
389         case CRYPTO_CIPHER_ALG_3DES:
390                 if (len % 8)
391                         return -1;
392                 blocks = len / 8;
393                 for (i = 0; i < blocks; i++) {
394                         os_memcpy(tmp, crypt, 8);
395                         des3_decrypt(crypt, &ctx->u.des3.key, plain);
396                         for (j = 0; j < 8; j++)
397                                 plain[j] ^= ctx->u.des3.cbc[j];
398                         os_memcpy(ctx->u.des3.cbc, tmp, 8);
399                         plain += 8;
400                         crypt += 8;
401                 }
402                 break;
403         case CRYPTO_CIPHER_ALG_DES:
404                 if (len % 8)
405                         return -1;
406                 blocks = len / 8;
407                 for (i = 0; i < blocks; i++) {
408                         os_memcpy(tmp, crypt, 8);
409                         des_block_decrypt(crypt, ctx->u.des.dk, plain);
410                         for (j = 0; j < 8; j++)
411                                 plain[j] ^= ctx->u.des.cbc[j];
412                         os_memcpy(ctx->u.des.cbc, tmp, 8);
413                         plain += 8;
414                         crypt += 8;
415                 }
416                 break;
417         default:
418                 return -1;
419         }
420
421         return 0;
422 }
423
424
425 void crypto_cipher_deinit(struct crypto_cipher *ctx)
426 {
427         switch (ctx->alg) {
428         case CRYPTO_CIPHER_ALG_AES:
429                 aes_encrypt_deinit(ctx->u.aes.ctx_enc);
430                 aes_decrypt_deinit(ctx->u.aes.ctx_dec);
431                 break;
432         case CRYPTO_CIPHER_ALG_3DES:
433                 break;
434         default:
435                 break;
436         }
437         os_free(ctx);
438 }
439
440
441 /* Dummy structures; these are just typecast to struct crypto_rsa_key */
442 struct crypto_public_key;
443 struct crypto_private_key;
444
445
446 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
447 {
448         return (struct crypto_public_key *)
449                 crypto_rsa_import_public_key(key, len);
450 }
451
452
453 struct crypto_private_key * crypto_private_key_import(const u8 *key,
454                                                       size_t len,
455                                                       const char *passwd)
456 {
457         struct crypto_private_key *res;
458
459         /* First, check for possible PKCS #8 encoding */
460         res = pkcs8_key_import(key, len);
461         if (res)
462                 return res;
463
464         if (passwd) {
465                 /* Try to parse as encrypted PKCS #8 */
466                 res = pkcs8_enc_key_import(key, len, passwd);
467                 if (res)
468                         return res;
469         }
470
471         /* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
472         wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
473                    "key");
474         return (struct crypto_private_key *)
475                 crypto_rsa_import_private_key(key, len);
476 }
477
478
479 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
480                                                        size_t len)
481 {
482         /* No X.509 support in crypto_internal.c */
483         return NULL;
484 }
485
486
487 int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
488                                         const u8 *in, size_t inlen,
489                                         u8 *out, size_t *outlen)
490 {
491         return pkcs1_encrypt(2, (struct crypto_rsa_key *) key,
492                              0, in, inlen, out, outlen);
493 }
494
495
496 int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
497                                          const u8 *in, size_t inlen,
498                                          u8 *out, size_t *outlen)
499 {
500         return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key *) key,
501                                              in, inlen, out, outlen);
502 }
503
504
505 int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
506                                   const u8 *in, size_t inlen,
507                                   u8 *out, size_t *outlen)
508 {
509         return pkcs1_encrypt(1, (struct crypto_rsa_key *) key,
510                              1, in, inlen, out, outlen);
511 }
512
513
514 void crypto_public_key_free(struct crypto_public_key *key)
515 {
516         crypto_rsa_free((struct crypto_rsa_key *) key);
517 }
518
519
520 void crypto_private_key_free(struct crypto_private_key *key)
521 {
522         crypto_rsa_free((struct crypto_rsa_key *) key);
523 }
524
525
526 int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
527                                     const u8 *crypt, size_t crypt_len,
528                                     u8 *plain, size_t *plain_len)
529 {
530         return pkcs1_decrypt_public_key((struct crypto_rsa_key *) key,
531                                         crypt, crypt_len, plain, plain_len);
532 }
533
534
535 int crypto_global_init(void)
536 {
537         return 0;
538 }
539
540
541 void crypto_global_deinit(void)
542 {
543 }
544
545
546 #ifdef CONFIG_MODEXP
547
548 int crypto_mod_exp(const u8 *base, size_t base_len,
549                    const u8 *power, size_t power_len,
550                    const u8 *modulus, size_t modulus_len,
551                    u8 *result, size_t *result_len)
552 {
553         struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result;
554         int ret = -1;
555
556         bn_base = bignum_init();
557         bn_exp = bignum_init();
558         bn_modulus = bignum_init();
559         bn_result = bignum_init();
560
561         if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
562             bn_result == NULL)
563                 goto error;
564
565         if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 ||
566             bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 ||
567             bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0)
568                 goto error;
569
570         if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0)
571                 goto error;
572
573         ret = bignum_get_unsigned_bin(bn_result, result, result_len);
574
575 error:
576         bignum_deinit(bn_base);
577         bignum_deinit(bn_exp);
578         bignum_deinit(bn_modulus);
579         bignum_deinit(bn_result);
580         return ret;
581 }
582
583 #endif /* CONFIG_MODEXP */