ec_check.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include "ec_lcl.h"
  10. #include <openssl/err.h>
  11. int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
  12. {
  13. int ret = 0;
  14. const BIGNUM *order;
  15. BN_CTX *new_ctx = NULL;
  16. EC_POINT *point = NULL;
  17. /* Custom curves assumed to be correct */
  18. if ((group->meth->flags & EC_FLAGS_CUSTOM_CURVE) != 0)
  19. return 1;
  20. if (ctx == NULL) {
  21. ctx = new_ctx = BN_CTX_new();
  22. if (ctx == NULL) {
  23. ECerr(EC_F_EC_GROUP_CHECK, ERR_R_MALLOC_FAILURE);
  24. goto err;
  25. }
  26. }
  27. /* check the discriminant */
  28. if (!EC_GROUP_check_discriminant(group, ctx)) {
  29. ECerr(EC_F_EC_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);
  30. goto err;
  31. }
  32. /* check the generator */
  33. if (group->generator == NULL) {
  34. ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR);
  35. goto err;
  36. }
  37. if (EC_POINT_is_on_curve(group, group->generator, ctx) <= 0) {
  38. ECerr(EC_F_EC_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE);
  39. goto err;
  40. }
  41. /* check the order of the generator */
  42. if ((point = EC_POINT_new(group)) == NULL)
  43. goto err;
  44. order = EC_GROUP_get0_order(group);
  45. if (order == NULL)
  46. goto err;
  47. if (BN_is_zero(order)) {
  48. ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_ORDER);
  49. goto err;
  50. }
  51. if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx))
  52. goto err;
  53. if (!EC_POINT_is_at_infinity(group, point)) {
  54. ECerr(EC_F_EC_GROUP_CHECK, EC_R_INVALID_GROUP_ORDER);
  55. goto err;
  56. }
  57. ret = 1;
  58. err:
  59. BN_CTX_free(new_ctx);
  60. EC_POINT_free(point);
  61. return ret;
  62. }