2
0

ec_check.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * ECDSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "ec_local.h"
  15. #include <openssl/err.h>
  16. int EC_GROUP_check_named_curve(const EC_GROUP *group, int nist_only,
  17. BN_CTX *ctx)
  18. {
  19. int nid;
  20. BN_CTX *new_ctx = NULL;
  21. if (group == NULL) {
  22. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  23. return NID_undef;
  24. }
  25. if (ctx == NULL) {
  26. ctx = new_ctx = BN_CTX_new_ex(NULL);
  27. if (ctx == NULL) {
  28. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  29. return NID_undef;
  30. }
  31. }
  32. nid = ossl_ec_curve_nid_from_params(group, ctx);
  33. if (nid > 0 && nist_only && EC_curve_nid2nist(nid) == NULL)
  34. nid = NID_undef;
  35. BN_CTX_free(new_ctx);
  36. return nid;
  37. }
  38. int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
  39. {
  40. #ifdef FIPS_MODULE
  41. /*
  42. * ECC domain parameter validation.
  43. * See SP800-56A R3 5.5.2 "Assurances of Domain-Parameter Validity" Part 1b.
  44. */
  45. return EC_GROUP_check_named_curve(group, 1, ctx) >= 0 ? 1 : 0;
  46. #else
  47. int ret = 0;
  48. const BIGNUM *order;
  49. BN_CTX *new_ctx = NULL;
  50. EC_POINT *point = NULL;
  51. if (group == NULL || group->meth == NULL) {
  52. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  53. return 0;
  54. }
  55. /* Custom curves assumed to be correct */
  56. if ((group->meth->flags & EC_FLAGS_CUSTOM_CURVE) != 0)
  57. return 1;
  58. if (ctx == NULL) {
  59. ctx = new_ctx = BN_CTX_new();
  60. if (ctx == NULL) {
  61. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  62. goto err;
  63. }
  64. }
  65. /* check the discriminant */
  66. if (!EC_GROUP_check_discriminant(group, ctx)) {
  67. ERR_raise(ERR_LIB_EC, EC_R_DISCRIMINANT_IS_ZERO);
  68. goto err;
  69. }
  70. /* check the generator */
  71. if (group->generator == NULL) {
  72. ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
  73. goto err;
  74. }
  75. if (EC_POINT_is_on_curve(group, group->generator, ctx) <= 0) {
  76. ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
  77. goto err;
  78. }
  79. /* check the order of the generator */
  80. if ((point = EC_POINT_new(group)) == NULL)
  81. goto err;
  82. order = EC_GROUP_get0_order(group);
  83. if (order == NULL)
  84. goto err;
  85. if (BN_is_zero(order)) {
  86. ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_ORDER);
  87. goto err;
  88. }
  89. if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx))
  90. goto err;
  91. if (!EC_POINT_is_at_infinity(group, point)) {
  92. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
  93. goto err;
  94. }
  95. ret = 1;
  96. err:
  97. BN_CTX_free(new_ctx);
  98. EC_POINT_free(point);
  99. return ret;
  100. #endif /* FIPS_MODULE */
  101. }