2
0

ffc_key_validate.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2019-2023 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. #include "internal/ffc.h"
  10. /*
  11. * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
  12. * To only be used with ephemeral FFC public keys generated using the approved
  13. * safe-prime groups. (Checks that the public key is in the range [2, p - 1]
  14. *
  15. * ret contains 0 on success, or error flags (see FFC_ERROR_PUBKEY_TOO_SMALL)
  16. */
  17. int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params,
  18. const BIGNUM *pub_key, int *ret)
  19. {
  20. int ok = 0;
  21. BIGNUM *tmp = NULL;
  22. BN_CTX *ctx = NULL;
  23. *ret = 0;
  24. if (params == NULL || pub_key == NULL || params->p == NULL) {
  25. *ret = FFC_ERROR_PASSED_NULL_PARAM;
  26. return 1;
  27. }
  28. ctx = BN_CTX_new_ex(NULL);
  29. if (ctx == NULL)
  30. goto err;
  31. BN_CTX_start(ctx);
  32. tmp = BN_CTX_get(ctx);
  33. /* Step(1): Verify pub_key >= 2 */
  34. if (tmp == NULL
  35. || !BN_set_word(tmp, 1))
  36. goto err;
  37. if (BN_cmp(pub_key, tmp) <= 0)
  38. *ret |= FFC_ERROR_PUBKEY_TOO_SMALL;
  39. /* Step(1): Verify pub_key <= p-2 */
  40. if (BN_copy(tmp, params->p) == NULL
  41. || !BN_sub_word(tmp, 1))
  42. goto err;
  43. if (BN_cmp(pub_key, tmp) >= 0)
  44. *ret |= FFC_ERROR_PUBKEY_TOO_LARGE;
  45. ok = 1;
  46. err:
  47. if (ctx != NULL) {
  48. BN_CTX_end(ctx);
  49. BN_CTX_free(ctx);
  50. }
  51. return ok;
  52. }
  53. /*
  54. * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
  55. */
  56. int ossl_ffc_validate_public_key(const FFC_PARAMS *params,
  57. const BIGNUM *pub_key, int *ret)
  58. {
  59. int ok = 0;
  60. BIGNUM *tmp = NULL;
  61. BN_CTX *ctx = NULL;
  62. if (!ossl_ffc_validate_public_key_partial(params, pub_key, ret))
  63. return 0;
  64. if (*ret == 0 && params->q != NULL) {
  65. ctx = BN_CTX_new_ex(NULL);
  66. if (ctx == NULL)
  67. goto err;
  68. BN_CTX_start(ctx);
  69. tmp = BN_CTX_get(ctx);
  70. /* Check pub_key^q == 1 mod p */
  71. if (tmp == NULL
  72. || !BN_mod_exp(tmp, pub_key, params->q, params->p, ctx))
  73. goto err;
  74. if (!BN_is_one(tmp))
  75. *ret |= FFC_ERROR_PUBKEY_INVALID;
  76. }
  77. ok = 1;
  78. err:
  79. if (ctx != NULL) {
  80. BN_CTX_end(ctx);
  81. BN_CTX_free(ctx);
  82. }
  83. return ok;
  84. }
  85. /*
  86. * See SP800-56Ar3 Section 5.6.2.1.2: Owner assurance of Private key validity.
  87. * Verifies priv_key is in the range [1..upper-1]. The passed in value of upper
  88. * is normally params->q but can be 2^N for approved safe prime groups.
  89. * Note: This assumes that the domain parameters are valid.
  90. */
  91. int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv,
  92. int *ret)
  93. {
  94. int ok = 0;
  95. *ret = 0;
  96. if (priv == NULL || upper == NULL) {
  97. *ret = FFC_ERROR_PASSED_NULL_PARAM;
  98. goto err;
  99. }
  100. if (BN_cmp(priv, BN_value_one()) < 0) {
  101. *ret |= FFC_ERROR_PRIVKEY_TOO_SMALL;
  102. goto err;
  103. }
  104. if (BN_cmp(priv, upper) >= 0) {
  105. *ret |= FFC_ERROR_PRIVKEY_TOO_LARGE;
  106. goto err;
  107. }
  108. ok = 1;
  109. err:
  110. return ok;
  111. }