ffc_key_validate.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2019-2020 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. ctx = BN_CTX_new_ex(NULL);
  25. if (ctx == NULL)
  26. goto err;
  27. BN_CTX_start(ctx);
  28. tmp = BN_CTX_get(ctx);
  29. /* Step(1): Verify pub_key >= 2 */
  30. if (tmp == NULL
  31. || !BN_set_word(tmp, 1))
  32. goto err;
  33. if (BN_cmp(pub_key, tmp) <= 0) {
  34. *ret |= FFC_ERROR_PUBKEY_TOO_SMALL;
  35. goto err;
  36. }
  37. /* Step(1): Verify pub_key <= p-2 */
  38. if (BN_copy(tmp, params->p) == NULL
  39. || !BN_sub_word(tmp, 1))
  40. goto err;
  41. if (BN_cmp(pub_key, tmp) >= 0) {
  42. *ret |= FFC_ERROR_PUBKEY_TOO_LARGE;
  43. goto err;
  44. }
  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 (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. goto err;
  77. }
  78. }
  79. ok = 1;
  80. err:
  81. if (ctx != NULL) {
  82. BN_CTX_end(ctx);
  83. BN_CTX_free(ctx);
  84. }
  85. return ok;
  86. }
  87. /*
  88. * See SP800-56Ar3 Section 5.6.2.1.2: Owner assurance of Private key validity.
  89. * Verifies priv_key is in the range [1..upper-1]. The passed in value of upper
  90. * is normally params->q but can be 2^N for approved safe prime groups.
  91. * Note: This assumes that the domain parameters are valid.
  92. */
  93. int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv,
  94. int *ret)
  95. {
  96. int ok = 0;
  97. *ret = 0;
  98. if (BN_cmp(priv, BN_value_one()) < 0) {
  99. *ret |= FFC_ERROR_PRIVKEY_TOO_SMALL;
  100. goto err;
  101. }
  102. if (BN_cmp(priv, upper) >= 0) {
  103. *ret |= FFC_ERROR_PRIVKEY_TOO_LARGE;
  104. goto err;
  105. }
  106. ok = 1;
  107. err:
  108. return ok;
  109. }