dsa_check.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 1995-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. /*
  10. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/bn.h>
  17. #include "dsa_local.h"
  18. #include "crypto/dsa.h"
  19. int ossl_dsa_check_params(const DSA *dsa, int checktype, int *ret)
  20. {
  21. if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
  22. return ossl_ffc_params_simple_validate(dsa->libctx, &dsa->params,
  23. FFC_PARAM_TYPE_DSA, ret);
  24. else
  25. /*
  26. * Do full FFC domain params validation according to FIPS-186-4
  27. * - always in FIPS_MODULE
  28. * - only if possible (i.e., seed is set) in default provider
  29. */
  30. return ossl_ffc_params_full_validate(dsa->libctx, &dsa->params,
  31. FFC_PARAM_TYPE_DSA, ret);
  32. }
  33. /*
  34. * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
  35. */
  36. int ossl_dsa_check_pub_key(const DSA *dsa, const BIGNUM *pub_key, int *ret)
  37. {
  38. return ossl_ffc_validate_public_key(&dsa->params, pub_key, ret)
  39. && *ret == 0;
  40. }
  41. /*
  42. * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
  43. * To only be used with ephemeral FFC public keys generated using the approved
  44. * safe-prime groups.
  45. */
  46. int ossl_dsa_check_pub_key_partial(const DSA *dsa, const BIGNUM *pub_key, int *ret)
  47. {
  48. return ossl_ffc_validate_public_key_partial(&dsa->params, pub_key, ret)
  49. && *ret == 0;
  50. }
  51. int ossl_dsa_check_priv_key(const DSA *dsa, const BIGNUM *priv_key, int *ret)
  52. {
  53. *ret = 0;
  54. return (dsa->params.q != NULL
  55. && ossl_ffc_validate_private_key(dsa->params.q, priv_key, ret));
  56. }
  57. /*
  58. * FFC pairwise check from SP800-56A R3.
  59. * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
  60. */
  61. int ossl_dsa_check_pairwise(const DSA *dsa)
  62. {
  63. int ret = 0;
  64. BN_CTX *ctx = NULL;
  65. BIGNUM *pub_key = NULL;
  66. if (dsa->params.p == NULL
  67. || dsa->params.g == NULL
  68. || dsa->priv_key == NULL
  69. || dsa->pub_key == NULL)
  70. return 0;
  71. ctx = BN_CTX_new_ex(dsa->libctx);
  72. if (ctx == NULL)
  73. goto err;
  74. pub_key = BN_new();
  75. if (pub_key == NULL)
  76. goto err;
  77. /* recalculate the public key = (g ^ priv) mod p */
  78. if (!ossl_dsa_generate_public_key(ctx, dsa, dsa->priv_key, pub_key))
  79. goto err;
  80. /* check it matches the existing pubic_key */
  81. ret = BN_cmp(pub_key, dsa->pub_key) == 0;
  82. err:
  83. BN_free(pub_key);
  84. BN_CTX_free(ctx);
  85. return ret;
  86. }