9501dfd623f01a7a6b5b4980677ce64e8f70d019
[libeap.git] / src / crypto / crypto_internal.c
1 /*
2  * WPA Supplicant / Crypto wrapper for internal crypto implementation
3  * Copyright (c) 2006-2007, 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/asn1.h"
25
26 #include "sha1_i.h"
27 #include "md5_i.h"
28
29 #ifdef CONFIG_TLS_INTERNAL
30
31 /* from des.c */
32 struct des3_key_s {
33         u32 ek[3][32];
34         u32 dk[3][32];
35 };
36
37 void des3_key_setup(const u8 *key, struct des3_key_s *dkey);
38 void des3_encrypt(const u8 *plain, const struct des3_key_s *key, u8 *crypt);
39 void des3_decrypt(const u8 *crypt, const struct des3_key_s *key, u8 *plain);
40
41
42 struct MD5Context {
43         u32 buf[4];
44         u32 bits[2];
45         u8 in[64];
46 };
47
48 struct SHA1Context {
49         u32 state[5];
50         u32 count[2];
51         unsigned char buffer[64];
52 };
53
54
55 struct crypto_hash {
56         enum crypto_hash_alg alg;
57         union {
58                 struct MD5Context md5;
59                 struct SHA1Context sha1;
60         } u;
61         u8 key[64];
62         size_t key_len;
63 };
64
65
66 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
67                                       size_t key_len)
68 {
69         struct crypto_hash *ctx;
70         u8 k_pad[64];
71         u8 tk[20];
72         size_t i;
73
74         ctx = os_zalloc(sizeof(*ctx));
75         if (ctx == NULL)
76                 return NULL;
77
78         ctx->alg = alg;
79
80         switch (alg) {
81         case CRYPTO_HASH_ALG_MD5:
82                 MD5Init(&ctx->u.md5);
83                 break;
84         case CRYPTO_HASH_ALG_SHA1:
85                 SHA1Init(&ctx->u.sha1);
86                 break;
87         case CRYPTO_HASH_ALG_HMAC_MD5:
88                 if (key_len > sizeof(k_pad)) {
89                         MD5Init(&ctx->u.md5);
90                         MD5Update(&ctx->u.md5, key, key_len);
91                         MD5Final(tk, &ctx->u.md5);
92                         key = tk;
93                         key_len = 16;
94                 }
95                 os_memcpy(ctx->key, key, key_len);
96                 ctx->key_len = key_len;
97
98                 os_memcpy(k_pad, key, key_len);
99                 os_memset(k_pad + key_len, 0, sizeof(k_pad) - key_len);
100                 for (i = 0; i < sizeof(k_pad); i++)
101                         k_pad[i] ^= 0x36;
102                 MD5Init(&ctx->u.md5);
103                 MD5Update(&ctx->u.md5, k_pad, sizeof(k_pad));
104                 break;
105         case CRYPTO_HASH_ALG_HMAC_SHA1:
106                 if (key_len > sizeof(k_pad)) {
107                         SHA1Init(&ctx->u.sha1);
108                         SHA1Update(&ctx->u.sha1, key, key_len);
109                         SHA1Final(tk, &ctx->u.sha1);
110                         key = tk;
111                         key_len = 20;
112                 }
113                 os_memcpy(ctx->key, key, key_len);
114                 ctx->key_len = key_len;
115
116                 os_memcpy(k_pad, key, key_len);
117                 os_memset(k_pad + key_len, 0, sizeof(k_pad) - key_len);
118                 for (i = 0; i < sizeof(k_pad); i++)
119                         k_pad[i] ^= 0x36;
120                 SHA1Init(&ctx->u.sha1);
121                 SHA1Update(&ctx->u.sha1, k_pad, sizeof(k_pad));
122                 break;
123         default:
124                 os_free(ctx);
125                 return NULL;
126         }
127
128         return ctx;
129 }
130
131
132 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
133 {
134         if (ctx == NULL)
135                 return;
136
137         switch (ctx->alg) {
138         case CRYPTO_HASH_ALG_MD5:
139         case CRYPTO_HASH_ALG_HMAC_MD5:
140                 MD5Update(&ctx->u.md5, data, len);
141                 break;
142         case CRYPTO_HASH_ALG_SHA1:
143         case CRYPTO_HASH_ALG_HMAC_SHA1:
144                 SHA1Update(&ctx->u.sha1, data, len);
145                 break;
146         }
147 }
148
149
150 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
151 {
152         u8 k_pad[64];
153         size_t i;
154
155         if (ctx == NULL)
156                 return -2;
157
158         if (mac == NULL || len == NULL) {
159                 os_free(ctx);
160                 return 0;
161         }
162
163         switch (ctx->alg) {
164         case CRYPTO_HASH_ALG_MD5:
165                 if (*len < 16) {
166                         *len = 16;
167                         os_free(ctx);
168                         return -1;
169                 }
170                 *len = 16;
171                 MD5Final(mac, &ctx->u.md5);
172                 break;
173         case CRYPTO_HASH_ALG_SHA1:
174                 if (*len < 20) {
175                         *len = 20;
176                         os_free(ctx);
177                         return -1;
178                 }
179                 *len = 20;
180                 SHA1Final(mac, &ctx->u.sha1);
181                 break;
182         case CRYPTO_HASH_ALG_HMAC_MD5:
183                 if (*len < 16) {
184                         *len = 16;
185                         os_free(ctx);
186                         return -1;
187                 }
188                 *len = 16;
189
190                 MD5Final(mac, &ctx->u.md5);
191
192                 os_memcpy(k_pad, ctx->key, ctx->key_len);
193                 os_memset(k_pad + ctx->key_len, 0,
194                           sizeof(k_pad) - ctx->key_len);
195                 for (i = 0; i < sizeof(k_pad); i++)
196                         k_pad[i] ^= 0x5c;
197                 MD5Init(&ctx->u.md5);
198                 MD5Update(&ctx->u.md5, k_pad, sizeof(k_pad));
199                 MD5Update(&ctx->u.md5, mac, 16);
200                 MD5Final(mac, &ctx->u.md5);
201                 break;
202         case CRYPTO_HASH_ALG_HMAC_SHA1:
203                 if (*len < 20) {
204                         *len = 20;
205                         os_free(ctx);
206                         return -1;
207                 }
208                 *len = 20;
209
210                 SHA1Final(mac, &ctx->u.sha1);
211
212                 os_memcpy(k_pad, ctx->key, ctx->key_len);
213                 os_memset(k_pad + ctx->key_len, 0,
214                           sizeof(k_pad) - ctx->key_len);
215                 for (i = 0; i < sizeof(k_pad); i++)
216                         k_pad[i] ^= 0x5c;
217                 SHA1Init(&ctx->u.sha1);
218                 SHA1Update(&ctx->u.sha1, k_pad, sizeof(k_pad));
219                 SHA1Update(&ctx->u.sha1, mac, 20);
220                 SHA1Final(mac, &ctx->u.sha1);
221                 break;
222         }
223
224         os_free(ctx);
225
226         return 0;
227 }
228
229
230 struct crypto_cipher {
231         enum crypto_cipher_alg alg;
232         union {
233                 struct {
234                         size_t used_bytes;
235                         u8 key[16];
236                         size_t keylen;
237                 } rc4;
238                 struct {
239                         u8 cbc[32];
240                         size_t block_size;
241                         void *ctx_enc;
242                         void *ctx_dec;
243                 } aes;
244                 struct {
245                         struct des3_key_s key;
246                         u8 cbc[8];
247                 } des3;
248         } u;
249 };
250
251
252 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
253                                           const u8 *iv, const u8 *key,
254                                           size_t key_len)
255 {
256         struct crypto_cipher *ctx;
257
258         ctx = os_zalloc(sizeof(*ctx));
259         if (ctx == NULL)
260                 return NULL;
261
262         ctx->alg = alg;
263
264         switch (alg) {
265         case CRYPTO_CIPHER_ALG_RC4:
266                 if (key_len > sizeof(ctx->u.rc4.key)) {
267                         os_free(ctx);
268                         return NULL;
269                 }
270                 ctx->u.rc4.keylen = key_len;
271                 os_memcpy(ctx->u.rc4.key, key, key_len);
272                 break;
273         case CRYPTO_CIPHER_ALG_AES:
274                 if (key_len > sizeof(ctx->u.aes.cbc)) {
275                         os_free(ctx);
276                         return NULL;
277                 }
278                 ctx->u.aes.ctx_enc = aes_encrypt_init(key, key_len);
279                 if (ctx->u.aes.ctx_enc == NULL) {
280                         os_free(ctx);
281                         return NULL;
282                 }
283                 ctx->u.aes.ctx_dec = aes_decrypt_init(key, key_len);
284                 if (ctx->u.aes.ctx_dec == NULL) {
285                         aes_encrypt_deinit(ctx->u.aes.ctx_enc);
286                         os_free(ctx);
287                         return NULL;
288                 }
289                 ctx->u.aes.block_size = key_len;
290                 os_memcpy(ctx->u.aes.cbc, iv, ctx->u.aes.block_size);
291                 break;
292         case CRYPTO_CIPHER_ALG_3DES:
293                 if (key_len != 24) {
294                         os_free(ctx);
295                         return NULL;
296                 }
297                 des3_key_setup(key, &ctx->u.des3.key);
298                 os_memcpy(ctx->u.des3.cbc, iv, 8);
299                 break;
300         default:
301                 os_free(ctx);
302                 return NULL;
303         }
304
305         return ctx;
306 }
307
308
309 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
310                           u8 *crypt, size_t len)
311 {
312         size_t i, j, blocks;
313
314         switch (ctx->alg) {
315         case CRYPTO_CIPHER_ALG_RC4:
316                 if (plain != crypt)
317                         os_memcpy(crypt, plain, len);
318                 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
319                          ctx->u.rc4.used_bytes, crypt, len);
320                 ctx->u.rc4.used_bytes += len;
321                 break;
322         case CRYPTO_CIPHER_ALG_AES:
323                 if (len % ctx->u.aes.block_size)
324                         return -1;
325                 blocks = len / ctx->u.aes.block_size;
326                 for (i = 0; i < blocks; i++) {
327                         for (j = 0; j < ctx->u.aes.block_size; j++)
328                                 ctx->u.aes.cbc[j] ^= plain[j];
329                         aes_encrypt(ctx->u.aes.ctx_enc, ctx->u.aes.cbc,
330                                     ctx->u.aes.cbc);
331                         os_memcpy(crypt, ctx->u.aes.cbc,
332                                   ctx->u.aes.block_size);
333                         plain += ctx->u.aes.block_size;
334                         crypt += ctx->u.aes.block_size;
335                 }
336                 break;
337         case CRYPTO_CIPHER_ALG_3DES:
338                 if (len % 8)
339                         return -1;
340                 blocks = len / 8;
341                 for (i = 0; i < blocks; i++) {
342                         for (j = 0; j < 8; j++)
343                                 ctx->u.des3.cbc[j] ^= plain[j];
344                         des3_encrypt(ctx->u.des3.cbc, &ctx->u.des3.key,
345                                      ctx->u.des3.cbc);
346                         os_memcpy(crypt, ctx->u.des3.cbc, 8);
347                         plain += 8;
348                         crypt += 8;
349                 }
350                 break;
351         default:
352                 return -1;
353         }
354
355         return 0;
356 }
357
358
359 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
360                           u8 *plain, size_t len)
361 {
362         size_t i, j, blocks;
363         u8 tmp[32];
364
365         switch (ctx->alg) {
366         case CRYPTO_CIPHER_ALG_RC4:
367                 if (plain != crypt)
368                         os_memcpy(plain, crypt, len);
369                 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
370                          ctx->u.rc4.used_bytes, plain, len);
371                 ctx->u.rc4.used_bytes += len;
372                 break;
373         case CRYPTO_CIPHER_ALG_AES:
374                 if (len % ctx->u.aes.block_size)
375                         return -1;
376                 blocks = len / ctx->u.aes.block_size;
377                 for (i = 0; i < blocks; i++) {
378                         os_memcpy(tmp, crypt, ctx->u.aes.block_size);
379                         aes_decrypt(ctx->u.aes.ctx_dec, crypt, plain);
380                         for (j = 0; j < ctx->u.aes.block_size; j++)
381                                 plain[j] ^= ctx->u.aes.cbc[j];
382                         os_memcpy(ctx->u.aes.cbc, tmp, ctx->u.aes.block_size);
383                         plain += ctx->u.aes.block_size;
384                         crypt += ctx->u.aes.block_size;
385                 }
386                 break;
387         case CRYPTO_CIPHER_ALG_3DES:
388                 if (len % 8)
389                         return -1;
390                 blocks = len / 8;
391                 for (i = 0; i < blocks; i++) {
392                         os_memcpy(tmp, crypt, 8);
393                         des3_decrypt(crypt, &ctx->u.des3.key, plain);
394                         for (j = 0; j < 8; j++)
395                                 plain[j] ^= ctx->u.des3.cbc[j];
396                         os_memcpy(ctx->u.des3.cbc, tmp, 8);
397                         plain += 8;
398                         crypt += 8;
399                 }
400                 break;
401         default:
402                 return -1;
403         }
404
405         return 0;
406 }
407
408
409 void crypto_cipher_deinit(struct crypto_cipher *ctx)
410 {
411         switch (ctx->alg) {
412         case CRYPTO_CIPHER_ALG_AES:
413                 aes_encrypt_deinit(ctx->u.aes.ctx_enc);
414                 aes_decrypt_deinit(ctx->u.aes.ctx_dec);
415                 break;
416         case CRYPTO_CIPHER_ALG_3DES:
417                 break;
418         default:
419                 break;
420         }
421         os_free(ctx);
422 }
423
424
425 /* Dummy structures; these are just typecast to struct crypto_rsa_key */
426 struct crypto_public_key;
427 struct crypto_private_key;
428
429
430 struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
431 {
432         return (struct crypto_public_key *)
433                 crypto_rsa_import_public_key(key, len);
434 }
435
436
437 #ifdef EAP_TLS_FUNCS
438 static struct crypto_private_key *
439 crypto_pkcs8_key_import(const u8 *buf, size_t len)
440 {
441         struct asn1_hdr hdr;
442         const u8 *pos, *end;
443         struct bignum *zero;
444         struct asn1_oid oid;
445         char obuf[80];
446
447         /* PKCS #8, Chapter 6 */
448
449         /* PrivateKeyInfo ::= SEQUENCE */
450         if (asn1_get_next(buf, len, &hdr) < 0 ||
451             hdr.class != ASN1_CLASS_UNIVERSAL ||
452             hdr.tag != ASN1_TAG_SEQUENCE) {
453                 wpa_printf(MSG_DEBUG, "PKCS #8: Does not start with PKCS #8 "
454                            "header (SEQUENCE); assume PKCS #8 not used");
455                 return NULL;
456         }
457         pos = hdr.payload;
458         end = pos + hdr.length;
459
460         /* version Version (Version ::= INTEGER) */
461         if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
462             hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
463                 wpa_printf(MSG_DEBUG, "PKCS #8: Expected INTEGER - found "
464                            "class %d tag 0x%x; assume PKCS #8 not used",
465                            hdr.class, hdr.tag);
466                 return NULL;
467         }
468
469         zero = bignum_init();
470         if (zero == NULL)
471                 return NULL;
472
473         if (bignum_set_unsigned_bin(zero, hdr.payload, hdr.length) < 0) {
474                 wpa_printf(MSG_DEBUG, "PKCS #8: Failed to parse INTEGER");
475                 bignum_deinit(zero);
476                 return NULL;
477         }
478         pos = hdr.payload + hdr.length;
479
480         if (bignum_cmp_d(zero, 0) != 0) {
481                 wpa_printf(MSG_DEBUG, "PKCS #8: Expected zero INTEGER in the "
482                            "beginning of private key; not found; assume "
483                            "PKCS #8 not used");
484                 bignum_deinit(zero);
485                 return NULL;
486         }
487         bignum_deinit(zero);
488
489         /* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier
490          * (PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier) */
491         if (asn1_get_next(pos, len, &hdr) < 0 ||
492             hdr.class != ASN1_CLASS_UNIVERSAL ||
493             hdr.tag != ASN1_TAG_SEQUENCE) {
494                 wpa_printf(MSG_DEBUG, "PKCS #8: Expected SEQUENCE "
495                            "(AlgorithmIdentifier) - found class %d tag 0x%x; "
496                            "assume PKCS #8 not used",
497                            hdr.class, hdr.tag);
498                 return NULL;
499         }
500
501         if (asn1_get_oid(hdr.payload, hdr.length, &oid, &pos)) {
502                 wpa_printf(MSG_DEBUG, "PKCS #8: Failed to parse OID "
503                            "(algorithm); assume PKCS #8 not used");
504                 return NULL;
505         }
506
507         asn1_oid_to_str(&oid, obuf, sizeof(obuf));
508         wpa_printf(MSG_DEBUG, "PKCS #8: algorithm=%s", obuf);
509
510         if (oid.len != 7 ||
511             oid.oid[0] != 1 /* iso */ ||
512             oid.oid[1] != 2 /* member-body */ ||
513             oid.oid[2] != 840 /* us */ ||
514             oid.oid[3] != 113549 /* rsadsi */ ||
515             oid.oid[4] != 1 /* pkcs */ ||
516             oid.oid[5] != 1 /* pkcs-1 */ ||
517             oid.oid[6] != 1 /* rsaEncryption */) {
518                 wpa_printf(MSG_DEBUG, "PKCS #8: Unsupported private key "
519                            "algorithm %s", obuf);
520                 return NULL;
521         }
522
523         pos = hdr.payload + hdr.length;
524
525         /* privateKey PrivateKey (PrivateKey ::= OCTET STRING) */
526         if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
527             hdr.class != ASN1_CLASS_UNIVERSAL ||
528             hdr.tag != ASN1_TAG_OCTETSTRING) {
529                 wpa_printf(MSG_DEBUG, "PKCS #8: Expected OCTETSTRING "
530                            "(privateKey) - found class %d tag 0x%x",
531                            hdr.class, hdr.tag);
532                 return NULL;
533         }
534         wpa_printf(MSG_DEBUG, "PKCS #8: Try to parse RSAPrivateKey");
535
536         return (struct crypto_private_key *)
537                 crypto_rsa_import_private_key(hdr.payload, hdr.length);
538 }
539 #endif /* EAP_TLS_FUNCS */
540
541
542 struct crypto_private_key * crypto_private_key_import(const u8 *key,
543                                                       size_t len)
544 {
545         struct crypto_private_key *res;
546
547         /* First, check for possible PKCS #8 encoding */
548         res = crypto_pkcs8_key_import(key, len);
549         if (res)
550                 return res;
551
552         /* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
553         wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
554                    "key");
555         return (struct crypto_private_key *)
556                 crypto_rsa_import_private_key(key, len);
557 }
558
559
560 struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
561                                                        size_t len)
562 {
563         /* No X.509 support in crypto_internal.c */
564         return NULL;
565 }
566
567
568 static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
569                                            const u8 *in, size_t inlen,
570                                            u8 *out, size_t *outlen)
571 {
572         size_t ps_len;
573         u8 *pos;
574
575         /*
576          * PKCS #1 v1.5, 8.1:
577          *
578          * EB = 00 || BT || PS || 00 || D
579          * BT = 00 or 01 for private-key operation; 02 for public-key operation
580          * PS = k-3-||D||; at least eight octets
581          * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
582          * k = length of modulus in octets (modlen)
583          */
584
585         if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
586                 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
587                            "lengths (modlen=%lu outlen=%lu inlen=%lu)",
588                            __func__, (unsigned long) modlen,
589                            (unsigned long) *outlen,
590                            (unsigned long) inlen);
591                 return -1;
592         }
593
594         pos = out;
595         *pos++ = 0x00;
596         *pos++ = block_type; /* BT */
597         ps_len = modlen - inlen - 3;
598         switch (block_type) {
599         case 0:
600                 os_memset(pos, 0x00, ps_len);
601                 pos += ps_len;
602                 break;
603         case 1:
604                 os_memset(pos, 0xff, ps_len);
605                 pos += ps_len;
606                 break;
607         case 2:
608                 if (os_get_random(pos, ps_len) < 0) {
609                         wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
610                                    "random data for PS", __func__);
611                         return -1;
612                 }
613                 while (ps_len--) {
614                         if (*pos == 0x00)
615                                 *pos = 0x01;
616                         pos++;
617                 }
618                 break;
619         default:
620                 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
621                            "%d", __func__, block_type);
622                 return -1;
623         }
624         *pos++ = 0x00;
625         os_memcpy(pos, in, inlen); /* D */
626
627         return 0;
628 }
629
630
631 static int crypto_rsa_encrypt_pkcs1(int block_type, struct crypto_rsa_key *key,
632                                     int use_private,
633                                     const u8 *in, size_t inlen,
634                                     u8 *out, size_t *outlen)
635 {
636         size_t modlen;
637
638         modlen = crypto_rsa_get_modulus_len(key);
639
640         if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
641                                             out, outlen) < 0)
642                 return -1;
643
644         return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);
645 }
646
647
648 int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
649                                         const u8 *in, size_t inlen,
650                                         u8 *out, size_t *outlen)
651 {
652         return crypto_rsa_encrypt_pkcs1(2, (struct crypto_rsa_key *) key,
653                                         0, in, inlen, out, outlen);
654 }
655
656
657 int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
658                                          const u8 *in, size_t inlen,
659                                          u8 *out, size_t *outlen)
660 {
661         struct crypto_rsa_key *rkey = (struct crypto_rsa_key *) key;
662         int res;
663         u8 *pos, *end;
664
665         res = crypto_rsa_exptmod(in, inlen, out, outlen, rkey, 1);
666         if (res)
667                 return res;
668
669         if (*outlen < 2 || out[0] != 0 || out[1] != 2)
670                 return -1;
671
672         /* Skip PS (pseudorandom non-zero octets) */
673         pos = out + 2;
674         end = out + *outlen;
675         while (*pos && pos < end)
676                 pos++;
677         if (pos == end)
678                 return -1;
679         pos++;
680
681         *outlen -= pos - out;
682
683         /* Strip PKCS #1 header */
684         os_memmove(out, pos, *outlen);
685
686         return 0;
687 }
688
689
690 int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
691                                   const u8 *in, size_t inlen,
692                                   u8 *out, size_t *outlen)
693 {
694         return crypto_rsa_encrypt_pkcs1(1, (struct crypto_rsa_key *) key,
695                                         1, in, inlen, out, outlen);
696 }
697
698
699 void crypto_public_key_free(struct crypto_public_key *key)
700 {
701         crypto_rsa_free((struct crypto_rsa_key *) key);
702 }
703
704
705 void crypto_private_key_free(struct crypto_private_key *key)
706 {
707         crypto_rsa_free((struct crypto_rsa_key *) key);
708 }
709
710
711 int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
712                                     const u8 *crypt, size_t crypt_len,
713                                     u8 *plain, size_t *plain_len)
714 {
715         size_t len;
716         u8 *pos;
717
718         len = *plain_len;
719         if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len,
720                                (struct crypto_rsa_key *) key, 0) < 0)
721                 return -1;
722
723         /*
724          * PKCS #1 v1.5, 8.1:
725          *
726          * EB = 00 || BT || PS || 00 || D
727          * BT = 00 or 01
728          * PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01)
729          * k = length of modulus in octets
730          */
731
732         if (len < 3 + 8 + 16 /* min hash len */ ||
733             plain[0] != 0x00 || (plain[1] != 0x00 && plain[1] != 0x01)) {
734                 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
735                            "structure");
736                 return -1;
737         }
738
739         pos = plain + 3;
740         if (plain[1] == 0x00) {
741                 /* BT = 00 */
742                 if (plain[2] != 0x00) {
743                         wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
744                                    "PS (BT=00)");
745                         return -1;
746                 }
747                 while (pos + 1 < plain + len && *pos == 0x00 && pos[1] == 0x00)
748                         pos++;
749         } else {
750                 /* BT = 01 */
751                 if (plain[2] != 0xff) {
752                         wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
753                                    "PS (BT=01)");
754                         return -1;
755                 }
756                 while (pos < plain + len && *pos == 0xff)
757                         pos++;
758         }
759
760         if (pos - plain - 2 < 8) {
761                 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
762                 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
763                            "padding");
764                 return -1;
765         }
766
767         if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
768                 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
769                            "structure (2)");
770                 return -1;
771         }
772         pos++;
773         len -= pos - plain;
774
775         /* Strip PKCS #1 header */
776         os_memmove(plain, pos, len);
777         *plain_len = len;
778
779         return 0;
780 }
781
782
783 int crypto_global_init(void)
784 {
785         return 0;
786 }
787
788
789 void crypto_global_deinit(void)
790 {
791 }
792 #endif /* CONFIG_TLS_INTERNAL */
793
794
795 #if defined(EAP_FAST) || defined(EAP_SERVER_FAST) || defined(CONFIG_WPS)
796
797 int crypto_mod_exp(const u8 *base, size_t base_len,
798                    const u8 *power, size_t power_len,
799                    const u8 *modulus, size_t modulus_len,
800                    u8 *result, size_t *result_len)
801 {
802         struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result;
803         int ret = -1;
804
805         bn_base = bignum_init();
806         bn_exp = bignum_init();
807         bn_modulus = bignum_init();
808         bn_result = bignum_init();
809
810         if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
811             bn_result == NULL)
812                 goto error;
813
814         if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 ||
815             bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 ||
816             bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0)
817                 goto error;
818
819         if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0)
820                 goto error;
821
822         ret = bignum_get_unsigned_bin(bn_result, result, result_len);
823
824 error:
825         bignum_deinit(bn_base);
826         bignum_deinit(bn_exp);
827         bignum_deinit(bn_modulus);
828         bignum_deinit(bn_result);
829         return ret;
830 }
831
832 #endif /* EAP_FAST || EAP_SERVER_FAST || CONFIG_WPS */