crypto_libtomcrypt.c revision 189251
1129590Smarius/* 2129590Smarius * WPA Supplicant / Crypto wrapper for LibTomCrypt (for internal TLSv1) 3129590Smarius * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi> 4129590Smarius * 5129590Smarius * This program is free software; you can redistribute it and/or modify 6129590Smarius * it under the terms of the GNU General Public License version 2 as 7129590Smarius * published by the Free Software Foundation. 8129590Smarius * 9129590Smarius * Alternatively, this software may be distributed under the terms of BSD 10129590Smarius * license. 11129590Smarius * 12129590Smarius * See README and COPYING for more details. 13129590Smarius */ 14129590Smarius 15129590Smarius#include "includes.h" 16129590Smarius#include <tomcrypt.h> 17129590Smarius 18129590Smarius#include "common.h" 19129590Smarius#include "rc4.h" 20129590Smarius#include "crypto.h" 21129590Smarius 22129590Smarius#ifndef mp_init_multi 23129590Smarius#define mp_init_multi ltc_init_multi 24129590Smarius#define mp_clear_multi ltc_deinit_multi 25129590Smarius#define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a) 26129590Smarius#define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b) 27129590Smarius#define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c) 28129590Smarius#define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d) 29129590Smarius#endif 30129590Smarius 31129590Smarius 32129590Smariusvoid md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) 33129590Smarius{ 34129590Smarius hash_state md; 35129590Smarius size_t i; 36129590Smarius 37129590Smarius md4_init(&md); 38129590Smarius for (i = 0; i < num_elem; i++) 39129590Smarius md4_process(&md, addr[i], len[i]); 40129590Smarius md4_done(&md, mac); 41129590Smarius} 42129590Smarius 43129590Smarius 44129590Smariusvoid des_encrypt(const u8 *clear, const u8 *key, u8 *cypher) 45129590Smarius{ 46129590Smarius u8 pkey[8], next, tmp; 47129590Smarius int i; 48129590Smarius symmetric_key skey; 49129590Smarius 50129590Smarius /* Add parity bits to the key */ 51129590Smarius next = 0; 52129590Smarius for (i = 0; i < 7; i++) { 53129590Smarius tmp = key[i]; 54129590Smarius pkey[i] = (tmp >> i) | next | 1; 55190097Smarius next = tmp << (7 - i); 56190097Smarius } 57129590Smarius pkey[i] = next | 1; 58129590Smarius 59190097Smarius des_setup(pkey, 8, 0, &skey); 60190097Smarius des_ecb_encrypt(clear, cypher, &skey); 61190097Smarius des_done(&skey); 62190097Smarius} 63190097Smarius 64190097Smarius 65129590Smarius#ifdef EAP_TLS_FUNCS 66263763Sdimvoid md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) 67129590Smarius{ 68129590Smarius hash_state md; 69129590Smarius size_t i; 70129590Smarius 71129590Smarius md5_init(&md); 72129590Smarius for (i = 0; i < num_elem; i++) 73161834Smarius md5_process(&md, addr[i], len[i]); 74161834Smarius md5_done(&md, mac); 75129590Smarius} 76132788Skan 77132788Skan 78132788Skanvoid sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) 79132788Skan{ 80132788Skan hash_state md; 81132788Skan size_t i; 82132788Skan 83129590Smarius sha1_init(&md); 84190097Smarius for (i = 0; i < num_elem; i++) 85190097Smarius sha1_process(&md, addr[i], len[i]); 86129590Smarius sha1_done(&md, mac); 87129590Smarius} 88129590Smarius 89129590Smarius 90129590Smariusvoid * aes_encrypt_init(const u8 *key, size_t len) 91129590Smarius{ 92129590Smarius symmetric_key *skey; 93129590Smarius skey = os_malloc(sizeof(*skey)); 94129590Smarius if (skey == NULL) 95129590Smarius return NULL; 96129590Smarius if (aes_setup(key, len, 0, skey) != CRYPT_OK) { 97129590Smarius os_free(skey); 98129590Smarius return NULL; 99129590Smarius } 100129590Smarius return skey; 101129590Smarius} 102129590Smarius 103129590Smarius 104129590Smariusvoid aes_encrypt(void *ctx, const u8 *plain, u8 *crypt) 105129590Smarius{ 106129590Smarius symmetric_key *skey = ctx; 107129590Smarius aes_ecb_encrypt(plain, crypt, skey); 108129590Smarius} 109129590Smarius 110129590Smarius 111129590Smariusvoid aes_encrypt_deinit(void *ctx) 112129590Smarius{ 113129590Smarius symmetric_key *skey = ctx; 114129590Smarius aes_done(skey); 115129590Smarius os_free(skey); 116129590Smarius} 117129590Smarius 118129590Smarius 119129590Smariusvoid * aes_decrypt_init(const u8 *key, size_t len) 120190097Smarius{ 121190097Smarius symmetric_key *skey; 122129590Smarius skey = os_malloc(sizeof(*skey)); 123129590Smarius if (skey == NULL) 124129590Smarius return NULL; 125129590Smarius if (aes_setup(key, len, 0, skey) != CRYPT_OK) { 126129590Smarius os_free(skey); 127129590Smarius return NULL; 128129590Smarius } 129129590Smarius return skey; 130129590Smarius} 131129590Smarius 132129590Smarius 133129590Smariusvoid aes_decrypt(void *ctx, const u8 *crypt, u8 *plain) 134129590Smarius{ 135129590Smarius symmetric_key *skey = ctx; 136129590Smarius aes_ecb_encrypt(plain, (u8 *) crypt, skey); 137129590Smarius} 138129590Smarius 139129590Smarius 140129590Smariusvoid aes_decrypt_deinit(void *ctx) 141129590Smarius{ 142129590Smarius symmetric_key *skey = ctx; 143129590Smarius aes_done(skey); 144129590Smarius os_free(skey); 145129590Smarius} 146129590Smarius 147129590Smarius 148190097Smarius#ifdef CONFIG_TLS_INTERNAL 149190097Smarius 150129590Smariusstruct crypto_hash { 151129590Smarius enum crypto_hash_alg alg; 152129590Smarius int error; 153129590Smarius union { 154129590Smarius hash_state md; 155129590Smarius hmac_state hmac; 156129590Smarius } u; 157129590Smarius}; 158129590Smarius 159129590Smarius 160129590Smariusstruct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key, 161129590Smarius size_t key_len) 162129590Smarius{ 163129590Smarius struct crypto_hash *ctx; 164129590Smarius 165129590Smarius ctx = os_zalloc(sizeof(*ctx)); 166129590Smarius if (ctx == NULL) 167129590Smarius return NULL; 168129590Smarius 169129590Smarius ctx->alg = alg; 170129590Smarius 171129590Smarius switch (alg) { 172129590Smarius case CRYPTO_HASH_ALG_MD5: 173129590Smarius if (md5_init(&ctx->u.md) != CRYPT_OK) 174129590Smarius goto fail; 175129590Smarius break; 176129590Smarius case CRYPTO_HASH_ALG_SHA1: 177129590Smarius if (sha1_init(&ctx->u.md) != CRYPT_OK) 178129590Smarius goto fail; 179129590Smarius break; 180129590Smarius case CRYPTO_HASH_ALG_HMAC_MD5: 181129590Smarius if (hmac_init(&ctx->u.hmac, find_hash("md5"), key, key_len) != 182129590Smarius CRYPT_OK) 183129590Smarius goto fail; 184129590Smarius break; 185129590Smarius case CRYPTO_HASH_ALG_HMAC_SHA1: 186129590Smarius if (hmac_init(&ctx->u.hmac, find_hash("sha1"), key, key_len) != 187129590Smarius CRYPT_OK) 188129590Smarius goto fail; 189129590Smarius break; 190129590Smarius default: 191129590Smarius goto fail; 192129590Smarius } 193129590Smarius 194129590Smarius return ctx; 195129590Smarius 196129590Smariusfail: 197129590Smarius os_free(ctx); 198129590Smarius return NULL; 199129590Smarius} 200129590Smarius 201129590Smariusvoid crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len) 202129590Smarius{ 203129590Smarius if (ctx == NULL || ctx->error) 204129590Smarius return; 205129590Smarius 206129590Smarius switch (ctx->alg) { 207129590Smarius case CRYPTO_HASH_ALG_MD5: 208129590Smarius ctx->error = md5_process(&ctx->u.md, data, len) != CRYPT_OK; 209129590Smarius break; 210129590Smarius case CRYPTO_HASH_ALG_SHA1: 211129590Smarius ctx->error = sha1_process(&ctx->u.md, data, len) != CRYPT_OK; 212129590Smarius break; 213129590Smarius case CRYPTO_HASH_ALG_HMAC_MD5: 214129590Smarius case CRYPTO_HASH_ALG_HMAC_SHA1: 215129590Smarius ctx->error = hmac_process(&ctx->u.hmac, data, len) != CRYPT_OK; 216129590Smarius break; 217129590Smarius } 218129590Smarius} 219129590Smarius 220129590Smarius 221129590Smariusint crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len) 222129590Smarius{ 223129590Smarius int ret = 0; 224129590Smarius unsigned long clen; 225129590Smarius 226129590Smarius if (ctx == NULL) 227129590Smarius return -2; 228129590Smarius 229129590Smarius if (mac == NULL || len == NULL) { 230129590Smarius os_free(ctx); 231129590Smarius return 0; 232129590Smarius } 233129590Smarius 234129590Smarius if (ctx->error) { 235129590Smarius os_free(ctx); 236129590Smarius return -2; 237129590Smarius } 238129590Smarius 239129590Smarius switch (ctx->alg) { 240129590Smarius case CRYPTO_HASH_ALG_MD5: 241129590Smarius if (*len < 16) { 242129590Smarius *len = 16; 243129590Smarius os_free(ctx); 244129590Smarius return -1; 245129590Smarius } 246129590Smarius *len = 16; 247129590Smarius if (md5_done(&ctx->u.md, mac) != CRYPT_OK) 248129590Smarius ret = -2; 249190097Smarius break; 250129590Smarius case CRYPTO_HASH_ALG_SHA1: 251129590Smarius if (*len < 20) { 252129590Smarius *len = 20; 253129590Smarius os_free(ctx); 254129590Smarius return -1; 255129590Smarius } 256190097Smarius *len = 20; 257129590Smarius if (sha1_done(&ctx->u.md, mac) != CRYPT_OK) 258129590Smarius ret = -2; 259129590Smarius break; 260129590Smarius case CRYPTO_HASH_ALG_HMAC_SHA1: 261129590Smarius if (*len < 20) { 262129590Smarius *len = 20; 263129590Smarius os_free(ctx); 264129590Smarius return -1; 265129590Smarius } 266129590Smarius /* continue */ 267129590Smarius case CRYPTO_HASH_ALG_HMAC_MD5: 268129590Smarius if (*len < 16) { 269129590Smarius *len = 16; 270129590Smarius os_free(ctx); 271129590Smarius return -1; 272129590Smarius } 273129590Smarius clen = *len; 274129590Smarius if (hmac_done(&ctx->u.hmac, mac, &clen) != CRYPT_OK) { 275129590Smarius os_free(ctx); 276129590Smarius return -1; 277129590Smarius } 278129590Smarius *len = clen; 279129590Smarius break; 280190097Smarius default: 281129590Smarius ret = -2; 282129590Smarius break; 283129590Smarius } 284129590Smarius 285129590Smarius os_free(ctx); 286129590Smarius 287129590Smarius return ret; 288129590Smarius} 289129590Smarius 290129590Smarius 291133768Smariusstruct crypto_cipher { 292129590Smarius int rc4; 293129590Smarius union { 294129590Smarius symmetric_CBC cbc; 295129590Smarius struct { 296129590Smarius size_t used_bytes; 297129590Smarius u8 key[16]; 298129590Smarius size_t keylen; 299129590Smarius } rc4; 300129590Smarius } u; 301129590Smarius}; 302129590Smarius 303190097Smarius 304129590Smariusstruct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg, 305129590Smarius const u8 *iv, const u8 *key, 306129590Smarius size_t key_len) 307129590Smarius{ 308129590Smarius struct crypto_cipher *ctx; 309129590Smarius int idx, res, rc4 = 0; 310129590Smarius 311 switch (alg) { 312 case CRYPTO_CIPHER_ALG_AES: 313 idx = find_cipher("aes"); 314 break; 315 case CRYPTO_CIPHER_ALG_3DES: 316 idx = find_cipher("3des"); 317 break; 318 case CRYPTO_CIPHER_ALG_DES: 319 idx = find_cipher("des"); 320 break; 321 case CRYPTO_CIPHER_ALG_RC2: 322 idx = find_cipher("rc2"); 323 break; 324 case CRYPTO_CIPHER_ALG_RC4: 325 idx = -1; 326 rc4 = 1; 327 break; 328 default: 329 return NULL; 330 } 331 332 ctx = os_zalloc(sizeof(*ctx)); 333 if (ctx == NULL) 334 return NULL; 335 336 if (rc4) { 337 ctx->rc4 = 1; 338 if (key_len > sizeof(ctx->u.rc4.key)) { 339 os_free(ctx); 340 return NULL; 341 } 342 ctx->u.rc4.keylen = key_len; 343 os_memcpy(ctx->u.rc4.key, key, key_len); 344 } else { 345 res = cbc_start(idx, iv, key, key_len, 0, &ctx->u.cbc); 346 if (res != CRYPT_OK) { 347 wpa_printf(MSG_DEBUG, "LibTomCrypt: Cipher start " 348 "failed: %s", error_to_string(res)); 349 os_free(ctx); 350 return NULL; 351 } 352 } 353 354 return ctx; 355} 356 357int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain, 358 u8 *crypt, size_t len) 359{ 360 int res; 361 362 if (ctx->rc4) { 363 if (plain != crypt) 364 os_memcpy(crypt, plain, len); 365 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen, 366 ctx->u.rc4.used_bytes, crypt, len); 367 ctx->u.rc4.used_bytes += len; 368 return 0; 369 } 370 371 res = cbc_encrypt(plain, crypt, len, &ctx->u.cbc); 372 if (res != CRYPT_OK) { 373 wpa_printf(MSG_DEBUG, "LibTomCrypt: CBC encryption " 374 "failed: %s", error_to_string(res)); 375 return -1; 376 } 377 return 0; 378} 379 380 381int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt, 382 u8 *plain, size_t len) 383{ 384 int res; 385 386 if (ctx->rc4) { 387 if (plain != crypt) 388 os_memcpy(plain, crypt, len); 389 rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen, 390 ctx->u.rc4.used_bytes, plain, len); 391 ctx->u.rc4.used_bytes += len; 392 return 0; 393 } 394 395 res = cbc_decrypt(crypt, plain, len, &ctx->u.cbc); 396 if (res != CRYPT_OK) { 397 wpa_printf(MSG_DEBUG, "LibTomCrypt: CBC decryption " 398 "failed: %s", error_to_string(res)); 399 return -1; 400 } 401 402 return 0; 403} 404 405 406void crypto_cipher_deinit(struct crypto_cipher *ctx) 407{ 408 if (!ctx->rc4) 409 cbc_done(&ctx->u.cbc); 410 os_free(ctx); 411} 412 413 414struct crypto_public_key { 415 rsa_key rsa; 416}; 417 418struct crypto_private_key { 419 rsa_key rsa; 420}; 421 422 423struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len) 424{ 425 int res; 426 struct crypto_public_key *pk; 427 428 pk = os_zalloc(sizeof(*pk)); 429 if (pk == NULL) 430 return NULL; 431 432 res = rsa_import(key, len, &pk->rsa); 433 if (res != CRYPT_OK) { 434 wpa_printf(MSG_ERROR, "LibTomCrypt: Failed to import " 435 "public key (res=%d '%s')", 436 res, error_to_string(res)); 437 os_free(pk); 438 return NULL; 439 } 440 441 if (pk->rsa.type != PK_PUBLIC) { 442 wpa_printf(MSG_ERROR, "LibTomCrypt: Public key was not of " 443 "correct type"); 444 rsa_free(&pk->rsa); 445 os_free(pk); 446 return NULL; 447 } 448 449 return pk; 450} 451 452 453struct crypto_private_key * crypto_private_key_import(const u8 *key, 454 size_t len) 455{ 456 int res; 457 struct crypto_private_key *pk; 458 459 pk = os_zalloc(sizeof(*pk)); 460 if (pk == NULL) 461 return NULL; 462 463 res = rsa_import(key, len, &pk->rsa); 464 if (res != CRYPT_OK) { 465 wpa_printf(MSG_ERROR, "LibTomCrypt: Failed to import " 466 "private key (res=%d '%s')", 467 res, error_to_string(res)); 468 os_free(pk); 469 return NULL; 470 } 471 472 if (pk->rsa.type != PK_PRIVATE) { 473 wpa_printf(MSG_ERROR, "LibTomCrypt: Private key was not of " 474 "correct type"); 475 rsa_free(&pk->rsa); 476 os_free(pk); 477 return NULL; 478 } 479 480 return pk; 481} 482 483 484struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf, 485 size_t len) 486{ 487 /* No X.509 support in LibTomCrypt */ 488 return NULL; 489} 490 491 492static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen, 493 const u8 *in, size_t inlen, 494 u8 *out, size_t *outlen) 495{ 496 size_t ps_len; 497 u8 *pos; 498 499 /* 500 * PKCS #1 v1.5, 8.1: 501 * 502 * EB = 00 || BT || PS || 00 || D 503 * BT = 00 or 01 for private-key operation; 02 for public-key operation 504 * PS = k-3-||D||; at least eight octets 505 * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero) 506 * k = length of modulus in octets (modlen) 507 */ 508 509 if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) { 510 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer " 511 "lengths (modlen=%lu outlen=%lu inlen=%lu)", 512 __func__, (unsigned long) modlen, 513 (unsigned long) *outlen, 514 (unsigned long) inlen); 515 return -1; 516 } 517 518 pos = out; 519 *pos++ = 0x00; 520 *pos++ = block_type; /* BT */ 521 ps_len = modlen - inlen - 3; 522 switch (block_type) { 523 case 0: 524 os_memset(pos, 0x00, ps_len); 525 pos += ps_len; 526 break; 527 case 1: 528 os_memset(pos, 0xff, ps_len); 529 pos += ps_len; 530 break; 531 case 2: 532 if (os_get_random(pos, ps_len) < 0) { 533 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get " 534 "random data for PS", __func__); 535 return -1; 536 } 537 while (ps_len--) { 538 if (*pos == 0x00) 539 *pos = 0x01; 540 pos++; 541 } 542 break; 543 default: 544 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type " 545 "%d", __func__, block_type); 546 return -1; 547 } 548 *pos++ = 0x00; 549 os_memcpy(pos, in, inlen); /* D */ 550 551 return 0; 552} 553 554 555static int crypto_rsa_encrypt_pkcs1(int block_type, rsa_key *key, int key_type, 556 const u8 *in, size_t inlen, 557 u8 *out, size_t *outlen) 558{ 559 unsigned long len, modlen; 560 int res; 561 562 modlen = mp_unsigned_bin_size(key->N); 563 564 if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen, 565 out, outlen) < 0) 566 return -1; 567 568 len = *outlen; 569 res = rsa_exptmod(out, modlen, out, &len, key_type, key); 570 if (res != CRYPT_OK) { 571 wpa_printf(MSG_DEBUG, "LibTomCrypt: rsa_exptmod failed: %s", 572 error_to_string(res)); 573 return -1; 574 } 575 *outlen = len; 576 577 return 0; 578} 579 580 581int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key, 582 const u8 *in, size_t inlen, 583 u8 *out, size_t *outlen) 584{ 585 return crypto_rsa_encrypt_pkcs1(2, &key->rsa, PK_PUBLIC, in, inlen, 586 out, outlen); 587} 588 589 590int crypto_private_key_sign_pkcs1(struct crypto_private_key *key, 591 const u8 *in, size_t inlen, 592 u8 *out, size_t *outlen) 593{ 594 return crypto_rsa_encrypt_pkcs1(1, &key->rsa, PK_PRIVATE, in, inlen, 595 out, outlen); 596} 597 598 599void crypto_public_key_free(struct crypto_public_key *key) 600{ 601 if (key) { 602 rsa_free(&key->rsa); 603 os_free(key); 604 } 605} 606 607 608void crypto_private_key_free(struct crypto_private_key *key) 609{ 610 if (key) { 611 rsa_free(&key->rsa); 612 os_free(key); 613 } 614} 615 616 617int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key, 618 const u8 *crypt, size_t crypt_len, 619 u8 *plain, size_t *plain_len) 620{ 621 int res; 622 unsigned long len; 623 u8 *pos; 624 625 len = *plain_len; 626 res = rsa_exptmod(crypt, crypt_len, plain, &len, PK_PUBLIC, 627 &key->rsa); 628 if (res != CRYPT_OK) { 629 wpa_printf(MSG_DEBUG, "LibTomCrypt: rsa_exptmod failed: %s", 630 error_to_string(res)); 631 return -1; 632 } 633 634 /* 635 * PKCS #1 v1.5, 8.1: 636 * 637 * EB = 00 || BT || PS || 00 || D 638 * BT = 01 639 * PS = k-3-||D|| times FF 640 * k = length of modulus in octets 641 */ 642 643 if (len < 3 + 8 + 16 /* min hash len */ || 644 plain[0] != 0x00 || plain[1] != 0x01 || plain[2] != 0xff) { 645 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB " 646 "structure"); 647 return -1; 648 } 649 650 pos = plain + 3; 651 while (pos < plain + len && *pos == 0xff) 652 pos++; 653 if (pos - plain - 2 < 8) { 654 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */ 655 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature " 656 "padding"); 657 return -1; 658 } 659 660 if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) { 661 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB " 662 "structure (2)"); 663 return -1; 664 } 665 pos++; 666 len -= pos - plain; 667 668 /* Strip PKCS #1 header */ 669 os_memmove(plain, pos, len); 670 *plain_len = len; 671 672 return 0; 673} 674 675 676int crypto_global_init(void) 677{ 678 ltc_mp = tfm_desc; 679 /* TODO: only register algorithms that are really needed */ 680 if (register_hash(&md4_desc) < 0 || 681 register_hash(&md5_desc) < 0 || 682 register_hash(&sha1_desc) < 0 || 683 register_cipher(&aes_desc) < 0 || 684 register_cipher(&des_desc) < 0 || 685 register_cipher(&des3_desc) < 0) { 686 wpa_printf(MSG_ERROR, "TLSv1: Failed to register " 687 "hash/cipher functions"); 688 return -1; 689 } 690 691 return 0; 692} 693 694 695void crypto_global_deinit(void) 696{ 697} 698 699 700#ifdef EAP_FAST 701 702int crypto_mod_exp(const u8 *base, size_t base_len, 703 const u8 *power, size_t power_len, 704 const u8 *modulus, size_t modulus_len, 705 u8 *result, size_t *result_len) 706{ 707 void *b, *p, *m, *r; 708 709 if (mp_init_multi(&b, &p, &m, &r, NULL) != CRYPT_OK) 710 return -1; 711 712 if (mp_read_unsigned_bin(b, (u8 *) base, base_len) != CRYPT_OK || 713 mp_read_unsigned_bin(p, (u8 *) power, power_len) != CRYPT_OK || 714 mp_read_unsigned_bin(m, (u8 *) modulus, modulus_len) != CRYPT_OK) 715 goto fail; 716 717 if (mp_exptmod(b, p, m, r) != CRYPT_OK) 718 goto fail; 719 720 *result_len = mp_unsigned_bin_size(r); 721 if (mp_to_unsigned_bin(r, result) != CRYPT_OK) 722 goto fail; 723 724 mp_clear_multi(b, p, m, r, NULL); 725 return 0; 726 727fail: 728 mp_clear_multi(b, p, m, r, NULL); 729 return -1; 730} 731 732#endif /* EAP_FAST */ 733 734#endif /* CONFIG_TLS_INTERNAL */ 735 736#endif /* EAP_TLS_FUNCS */ 737