1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2010-2022. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#include "common.h"
#include "algorithms.h"
#include "cipher.h"
#include "mac.h"
#ifdef HAS_3_0_API
#include "digest.h"
#endif
#ifdef HAS_3_0_API
#else
static unsigned int algo_hash_cnt, algo_hash_fips_cnt;
static ERL_NIF_TERM algo_hash[14]; /* increase when extending the list */
void init_hash_types(ErlNifEnv* env);
#endif
static unsigned int algo_pubkey_cnt, algo_pubkey_fips_cnt;
static ERL_NIF_TERM algo_pubkey[12]; /* increase when extending the list */
void init_pubkey_types(ErlNifEnv* env);
static ERL_NIF_TERM algo_curve[2][89]; /* increase when extending the list */
static ErlNifMutex* mtx_init_curve_types;
void init_curve_types(ErlNifEnv* env);
int get_curve_cnt(ErlNifEnv* env, int fips);
static unsigned int algo_rsa_opts_cnt, algo_rsa_opts_fips_cnt;
static ERL_NIF_TERM algo_rsa_opts[11]; /* increase when extending the list */
void init_rsa_opts_types(ErlNifEnv* env);
void init_algorithms_types(ErlNifEnv* env)
{
mtx_init_curve_types = enif_mutex_create("init_curve_types");
#ifdef HAS_3_0_API
#else
init_hash_types(env);
#endif
init_pubkey_types(env);
init_curve_types(env);
init_rsa_opts_types(env);
/* ciphers and macs are initiated statically */
}
void cleanup_algorithms_types(ErlNifEnv* env)
{
enif_mutex_destroy(mtx_init_curve_types);
}
/*================================================================
Hash algorithms
*/
ERL_NIF_TERM hash_algorithms(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
#ifdef HAS_3_0_API
return digest_types_as_list(env);
#else
unsigned int cnt =
FIPS_MODE() ? algo_hash_fips_cnt : algo_hash_cnt;
return enif_make_list_from_array(env, algo_hash, cnt);
#endif
}
#ifdef HAS_3_0_API
#else
void init_hash_types(ErlNifEnv* env) {
// Validated algorithms first
algo_hash_cnt = 0;
algo_hash[algo_hash_cnt++] = atom_sha;
#ifdef HAVE_SHA224
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "sha224");
#endif
#ifdef HAVE_SHA256
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "sha256");
#endif
#ifdef HAVE_SHA384
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "sha384");
#endif
#ifdef HAVE_SHA512
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "sha512");
#endif
#ifdef HAVE_SHA3_224
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "sha3_224");
#endif
#ifdef HAVE_SHA3_256
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "sha3_256");
#endif
#ifdef HAVE_SHA3_384
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "sha3_384");
#endif
#ifdef HAVE_SHA3_512
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "sha3_512");
#endif
#ifdef HAVE_BLAKE2
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "blake2b");
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "blake2s");
#endif
// Non-validated algorithms follow
algo_hash_fips_cnt = algo_hash_cnt;
#ifdef HAVE_MD4
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "md4");
#endif
#ifdef HAVE_MD5
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "md5");
#endif
#ifdef HAVE_RIPEMD160
algo_hash[algo_hash_cnt++] = enif_make_atom(env, "ripemd160");
#endif
ASSERT(algo_hash_cnt <= sizeof(algo_hash)/sizeof(ERL_NIF_TERM));
}
#endif
/*================================================================
Public key algorithms
*/
ERL_NIF_TERM pubkey_algorithms(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
unsigned int cnt =
FIPS_MODE() ? algo_pubkey_fips_cnt : algo_pubkey_cnt;
return enif_make_list_from_array(env, algo_pubkey, cnt);
}
void init_pubkey_types(ErlNifEnv* env) {
// Validated algorithms first
algo_pubkey_cnt = 0;
algo_pubkey[algo_pubkey_cnt++] = enif_make_atom(env, "rsa");
#ifdef HAVE_DSA
algo_pubkey[algo_pubkey_cnt++] = enif_make_atom(env, "dss");
#endif
#ifdef HAVE_DH
algo_pubkey[algo_pubkey_cnt++] = enif_make_atom(env, "dh");
#endif
#if defined(HAVE_EC)
#if !defined(OPENSSL_NO_EC2M)
algo_pubkey[algo_pubkey_cnt++] = enif_make_atom(env, "ec_gf2m");
#endif
algo_pubkey[algo_pubkey_cnt++] = enif_make_atom(env, "ecdsa");
algo_pubkey[algo_pubkey_cnt++] = enif_make_atom(env, "ecdh");
#endif
// Non-validated algorithms follow
algo_pubkey_fips_cnt = algo_pubkey_cnt;
// Don't know if Edward curves are fips validated
#if defined(HAVE_EDDSA)
algo_pubkey[algo_pubkey_cnt++] = enif_make_atom(env, "eddsa");
#endif
#if defined(HAVE_EDDH)
algo_pubkey[algo_pubkey_cnt++] = enif_make_atom(env, "eddh");
#endif
algo_pubkey[algo_pubkey_cnt++] = enif_make_atom(env, "srp");
ASSERT(algo_pubkey_cnt <= sizeof(algo_pubkey)/sizeof(ERL_NIF_TERM));
}
/*================================================================
Cipher key algorithms
*/
ERL_NIF_TERM cipher_algorithms(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
return cipher_types_as_list(env); /* Exclude old api ciphers */
}
/*================================================================
MAC key algorithms
*/
ERL_NIF_TERM mac_algorithms(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
return mac_types_as_list(env);
}
/*================================================================
Curves
*/
ERL_NIF_TERM curve_algorithms(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
int fips_mode;
int algo_curve_cnt;
fips_mode = FIPS_MODE();
algo_curve_cnt = get_curve_cnt(env, fips_mode);
return enif_make_list_from_array(env, algo_curve[fips_mode], algo_curve_cnt);
}
int init_curves(ErlNifEnv* env, int fips);
#if defined(HAVE_EC)
int valid_curve(int nid);
#endif
int get_curve_cnt(ErlNifEnv* env, int fips) {
static int algo_curve_cnt = -1;
static int algo_curve_fips_cnt = -1;
int cnt = 0;
if (0 == fips && algo_curve_cnt >= 0) {
return algo_curve_cnt;
}
if (1 == fips && algo_curve_fips_cnt >= 0) {
return algo_curve_fips_cnt;
}
enif_mutex_lock(mtx_init_curve_types);
if (1 == fips) {
if (algo_curve_fips_cnt >= 0) {
return algo_curve_fips_cnt;
}
algo_curve_fips_cnt = init_curves(env, 1);
cnt = algo_curve_fips_cnt;
} else {
if (algo_curve_cnt >= 0) {
return algo_curve_cnt;
}
algo_curve_cnt = init_curves(env, 0);
cnt = algo_curve_cnt;
}
enif_mutex_unlock(mtx_init_curve_types);
return cnt;
}
void init_curve_types(ErlNifEnv* env) {
/* Initialize the curve counters and curve's lists
by calling get_curve_cnt
*/
#ifdef FIPS_SUPPORT
if (FIPS_MODE()) {
// FIPS enabled
get_curve_cnt(env, 1);
FIPS_mode_set(0); // disable
get_curve_cnt(env, 0);
FIPS_mode_set(1); // re-enable
} else {
// FIPS disabled but available
get_curve_cnt(env, 0);
FIPS_mode_set(1); // enable
get_curve_cnt(env, 1);
FIPS_mode_set(0); // re-disable
}
#else
// FIPS mode is not available
get_curve_cnt(env, 0);
#endif
# ifdef DEBUG
{
int curve_cnt = get_curve_cnt(env, 0);
ASSERT(curve_cnt <= sizeof(algo_curve[0])/sizeof(ERL_NIF_TERM));
}
# endif
}
int init_curves(ErlNifEnv* env, int fips) {
#if defined(HAVE_EC)
int cnt = 0;
#ifdef NID_secp160k1
if (valid_curve(NID_secp160k1)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp160k1");
#else
#endif
#ifdef NID_secp160r1
if (valid_curve(NID_secp160r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp160r1");
#else
#endif
#ifdef NID_secp160r2
if (valid_curve(NID_secp160r2)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp160r2");
#else
#endif
#ifdef NID_secp192k1
if (valid_curve(NID_secp192k1)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp192k1");
#else
#endif
#ifdef NID_secp224k1
if (valid_curve(NID_secp224k1)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp224k1");
#else
#endif
#ifdef NID_secp224r1
if (valid_curve(NID_secp224r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp224r1");
#else
#endif
#ifdef NID_secp256k1
if (valid_curve(NID_secp256k1)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp256k1");
#else
#endif
#ifdef NID_secp384r1
if (valid_curve(NID_secp384r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp384r1");
#else
#endif
#ifdef NID_secp521r1
if (valid_curve(NID_secp521r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp521r1");
#else
#endif
#ifdef NID_X9_62_prime192v1
if (valid_curve(NID_X9_62_prime192v1)) {
algo_curve[fips][cnt++] = enif_make_atom(env,"secp192r1");
algo_curve[fips][cnt++] = enif_make_atom(env,"prime192v1");
}
#else
#endif
#ifdef NID_X9_62_prime192v2
if (valid_curve(NID_X9_62_prime192v2)) algo_curve[fips][cnt++] = enif_make_atom(env,"prime192v2");
#else
#endif
#ifdef NID_X9_62_prime192v3
if (valid_curve(NID_X9_62_prime192v3)) algo_curve[fips][cnt++] = enif_make_atom(env,"prime192v3");
#else
#endif
#ifdef NID_X9_62_prime239v1
if (valid_curve(NID_X9_62_prime239v1)) algo_curve[fips][cnt++] = enif_make_atom(env,"prime239v1");
#else
#endif
#ifdef NID_X9_62_prime239v2
if (valid_curve(NID_X9_62_prime239v2)) algo_curve[fips][cnt++] = enif_make_atom(env,"prime239v2");
#else
#endif
#ifdef NID_X9_62_prime239v3
if (valid_curve(NID_X9_62_prime239v3)) algo_curve[fips][cnt++] = enif_make_atom(env,"prime239v3");
#else
#endif
#ifdef NID_X9_62_prime256v1
if (valid_curve(NID_X9_62_prime256v1)) {
algo_curve[fips][cnt++] = enif_make_atom(env,"secp256r1");
algo_curve[fips][cnt++] = enif_make_atom(env,"prime256v1");
}
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls7
if (valid_curve(NID_wap_wsg_idm_ecid_wtls7)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls7");
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls9
if (valid_curve(NID_wap_wsg_idm_ecid_wtls9)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls9");
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls12
if (valid_curve(NID_wap_wsg_idm_ecid_wtls12)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls12");
#else
#endif
#ifdef NID_brainpoolP160r1
if (valid_curve(NID_brainpoolP160r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP160r1");
#else
#endif
#ifdef NID_brainpoolP160t1
if (valid_curve(NID_brainpoolP160t1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP160t1");
#else
#endif
#ifdef NID_brainpoolP192r1
if (valid_curve(NID_brainpoolP192r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP192r1");
#else
#endif
#ifdef NID_brainpoolP192t1
if (valid_curve(NID_brainpoolP192t1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP192t1");
#else
#endif
#ifdef NID_brainpoolP224r1
if (valid_curve(NID_brainpoolP224r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP224r1");
#else
#endif
#ifdef NID_brainpoolP224t1
if (valid_curve(NID_brainpoolP224t1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP224t1");
#else
#endif
#ifdef NID_brainpoolP256r1
if (valid_curve(NID_brainpoolP256r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP256r1");
#else
#endif
#ifdef NID_brainpoolP256t1
if (valid_curve(NID_brainpoolP256t1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP256t1");
#else
#endif
#ifdef NID_brainpoolP320r1
if (valid_curve(NID_brainpoolP320r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP320r1");
#else
#endif
#ifdef NID_brainpoolP320t1
if (valid_curve(NID_brainpoolP320t1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP320t1");
#else
#endif
#ifdef NID_brainpoolP384r1
if (valid_curve(NID_brainpoolP384r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP384r1");
#else
#endif
#ifdef NID_brainpoolP384t1
if (valid_curve(NID_brainpoolP384t1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP384t1");
#else
#endif
#ifdef NID_brainpoolP512r1
if (valid_curve(NID_brainpoolP512r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP512r1");
#else
#endif
#ifdef NID_brainpoolP512t1
if (valid_curve(NID_brainpoolP512t1)) algo_curve[fips][cnt++] = enif_make_atom(env,"brainpoolP512t1");
#else
#endif
//#if !defined(OPENSSL_NO_EC2M)
#ifdef NID_sect163k1
if (valid_curve(NID_sect163k1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect163k1");
#else
#endif
#ifdef NID_sect163r1
if (valid_curve(NID_sect163r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect163r1");
#else
#endif
#ifdef NID_sect163r2
if (valid_curve(NID_sect163r2)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect163r2");
#else
#endif
#ifdef NID_sect193r1
if (valid_curve(NID_sect193r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect193r1");
#else
#endif
#ifdef NID_sect193r2
if (valid_curve(NID_sect193r2)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect193r2");
#else
#endif
#ifdef NID_sect233k1
if (valid_curve(NID_sect233k1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect233k1");
#else
#endif
#ifdef NID_sect233r1
if (valid_curve(NID_sect233r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect233r1");
#else
#endif
#ifdef NID_sect239k1
if (valid_curve(NID_sect239k1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect239k1");
#else
#endif
#ifdef NID_sect283k1
if (valid_curve(NID_sect283k1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect283k1");
#else
#endif
#ifdef NID_sect283r1
if (valid_curve(NID_sect283r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect283r1");
#else
#endif
#ifdef NID_sect409k1
if (valid_curve(NID_sect409k1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect409k1");
#else
#endif
#ifdef NID_sect409r1
if (valid_curve(NID_sect409r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect409r1");
#else
#endif
#ifdef NID_sect571k1
if (valid_curve(NID_sect571k1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect571k1");
#else
#endif
#ifdef NID_sect571r1
if (valid_curve(NID_sect571r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect571r1");
#else
#endif
#ifdef NID_X9_62_c2pnb163v1
if (valid_curve(NID_X9_62_c2pnb163v1)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2pnb163v1");
#else
#endif
#ifdef NID_X9_62_c2pnb163v2
if (valid_curve(NID_X9_62_c2pnb163v2)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2pnb163v2");
#else
#endif
#ifdef NID_X9_62_c2pnb163v3
if (valid_curve(NID_X9_62_c2pnb163v3)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2pnb163v3");
#else
#endif
#ifdef NID_X9_62_c2pnb176v1
if (valid_curve(NID_X9_62_c2pnb176v1)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2pnb176v1");
#else
#endif
#ifdef NID_X9_62_c2tnb191v1
if (valid_curve(NID_X9_62_c2tnb191v1)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2tnb191v1");
#else
#endif
#ifdef NID_X9_62_c2tnb191v2
if (valid_curve(NID_X9_62_c2tnb191v2)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2tnb191v2");
#else
#endif
#ifdef NID_X9_62_c2tnb191v3
if (valid_curve(NID_X9_62_c2tnb191v3)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2tnb191v3");
#else
#endif
#ifdef NID_X9_62_c2pnb208w1
if (valid_curve(NID_X9_62_c2pnb208w1)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2pnb208w1");
#else
#endif
#ifdef NID_X9_62_c2tnb239v1
if (valid_curve(NID_X9_62_c2tnb239v1)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2tnb239v1");
#else
#endif
#ifdef NID_X9_62_c2tnb239v2
if (valid_curve(NID_X9_62_c2tnb239v2)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2tnb239v2");
#else
#endif
#ifdef NID_X9_62_c2tnb239v3
if (valid_curve(NID_X9_62_c2tnb239v3)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2tnb239v3");
#else
#endif
#ifdef NID_X9_62_c2pnb272w1
if (valid_curve(NID_X9_62_c2pnb272w1)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2pnb272w1");
#else
#endif
#ifdef NID_X9_62_c2pnb304w1
if (valid_curve(NID_X9_62_c2pnb304w1)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2pnb304w1");
#else
#endif
#ifdef NID_X9_62_c2tnb359v1
if (valid_curve(NID_X9_62_c2tnb359v1)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2tnb359v1");
#else
#endif
#ifdef NID_X9_62_c2pnb368w1
if (valid_curve(NID_X9_62_c2pnb368w1)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2pnb368w1");
#else
#endif
#ifdef NID_X9_62_c2tnb431r1
if (valid_curve(NID_X9_62_c2tnb431r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"c2tnb431r1");
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls3
if (valid_curve(NID_wap_wsg_idm_ecid_wtls3)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls3");
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls5
if (valid_curve(NID_wap_wsg_idm_ecid_wtls5)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls5");
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls10
if (valid_curve(NID_wap_wsg_idm_ecid_wtls10)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls10");
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls11
if (valid_curve(NID_wap_wsg_idm_ecid_wtls11)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls11");
#else
#endif
// Non-validated algorithms follow
#ifdef NID_secp112r1
if (valid_curve(NID_secp112r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp112r1");
#else
#endif
#ifdef NID_secp112r2
if (valid_curve(NID_secp112r2)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp112r2");
#else
#endif
#ifdef NID_secp128r1
if (valid_curve(NID_secp128r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp128r1");
#else
#endif
#ifdef NID_secp128r2
if (valid_curve(NID_secp128r2)) algo_curve[fips][cnt++] = enif_make_atom(env,"secp128r2");
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls6
if (valid_curve(NID_wap_wsg_idm_ecid_wtls6)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls6");
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls8
if (valid_curve(NID_wap_wsg_idm_ecid_wtls8)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls8");
#else
#endif
//#if !defined(OPENSSL_NO_EC2M)
#ifdef NID_sect113r1
if (valid_curve(NID_sect113r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect113r1");
#else
#endif
#ifdef NID_sect113r2
if (valid_curve(NID_sect113r2)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect113r2");
#else
#endif
#ifdef NID_sect131r1
if (valid_curve(NID_sect131r1)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect131r1");
#else
#endif
#ifdef NID_sect131r2
if (valid_curve(NID_sect131r2)) algo_curve[fips][cnt++] = enif_make_atom(env,"sect131r2");
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls1
if (valid_curve(NID_wap_wsg_idm_ecid_wtls1)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls1");
#else
#endif
#ifdef NID_wap_wsg_idm_ecid_wtls4
if (valid_curve(NID_wap_wsg_idm_ecid_wtls4)) algo_curve[fips][cnt++] = enif_make_atom(env,"wtls4");
#else
#endif
#ifdef NID_ipsec3
if (valid_curve(NID_ipsec3)) algo_curve[fips][cnt++] = enif_make_atom(env,"ipsec3");
#else
#endif
#ifdef NID_ipsec4
if (valid_curve(NID_ipsec4)) algo_curve[fips][cnt++] = enif_make_atom(env,"ipsec4");
#else
#endif
if (!fips) {
#ifdef HAVE_EDDSA
algo_curve[fips][cnt++] = enif_make_atom(env,"ed25519");
algo_curve[fips][cnt++] = enif_make_atom(env,"ed448");
#endif
#ifdef HAVE_EDDH
algo_curve[fips][cnt++] = enif_make_atom(env,"x25519");
algo_curve[fips][cnt++] = enif_make_atom(env,"x448");
#endif
}
return cnt;
#else /* if not HAVE_EC */
return 0;
#endif
}
#if defined(HAVE_EC)
/* Check if the curve in nid is supported by the
current cryptolib and current FIPS state.
*/
int valid_curve(int nid) {
int ret = 0;
#if defined(HAVE_DH)
# if defined(HAS_EVP_PKEY_CTX) && (! DISABLE_EVP_DH)
EVP_PKEY_CTX *pctx = NULL, *kctx = NULL;
EVP_PKEY *pkey = NULL, *params = NULL;
if (NULL == (pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)))
goto out;
if (1 != EVP_PKEY_paramgen_init(pctx))
goto out;
if (1 != EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, nid))
goto out;
if (!EVP_PKEY_paramgen(pctx, ¶ms))
goto out;
if (NULL == (kctx = EVP_PKEY_CTX_new(params, NULL)))
goto out;
if(1 != EVP_PKEY_keygen_init(kctx))
goto out;
if (1 != EVP_PKEY_keygen(kctx, &pkey))
goto out;
ret = 1;
out:
if (pkey) EVP_PKEY_free(pkey);
if (kctx) EVP_PKEY_CTX_free(kctx);
if (params) EVP_PKEY_free(params);
if (pctx) EVP_PKEY_CTX_free(pctx);
# else
EC_KEY *key;
if (NULL == (key = EC_KEY_new_by_curve_name(nid)))
goto out;
if(1 != EC_KEY_generate_key(key))
goto out;
ret = 1;
out:
if (key) EC_KEY_free(key);
# endif
#endif /* HAVE_DH etc */
return ret;
}
#endif /* HAVE_EC */
/*================================================================
RSA Options
*/
ERL_NIF_TERM rsa_opts_algorithms(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
unsigned int cnt =
FIPS_MODE() ? algo_rsa_opts_fips_cnt : algo_rsa_opts_cnt;
return enif_make_list_from_array(env, algo_rsa_opts, cnt);
}
void init_rsa_opts_types(ErlNifEnv* env) {
// Validated algorithms first
algo_rsa_opts_cnt = 0;
#ifdef HAS_EVP_PKEY_CTX
# ifdef HAVE_RSA_PKCS1_PSS_PADDING
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"rsa_pkcs1_pss_padding");
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"rsa_pss_saltlen");
# endif
# ifdef HAVE_RSA_MGF1_MD
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"rsa_mgf1_md");
# endif
# ifdef HAVE_RSA_OAEP_PADDING
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"rsa_pkcs1_oaep_padding");
# endif
# ifdef HAVE_RSA_OAEP_MD
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"rsa_oaep_label");
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"rsa_oaep_md");
# endif
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"signature_md");
#endif
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"rsa_pkcs1_padding");
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"rsa_x931_padding");
#ifdef HAVE_RSA_SSLV23_PADDING
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"rsa_sslv23_padding");
#endif
algo_rsa_opts[algo_rsa_opts_cnt++] = enif_make_atom(env,"rsa_no_padding");
algo_rsa_opts_fips_cnt = algo_rsa_opts_cnt;
ASSERT(algo_rsa_opts_cnt <= sizeof(algo_rsa_opts)/sizeof(ERL_NIF_TERM));
}
|