dsa_check.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 1995-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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/bn.h>
  12. #include "dsa_local.h"
  13. #include "crypto/dsa.h"
  14. int dsa_check_params(const DSA *dsa, int *ret)
  15. {
  16. /*
  17. * (2b) FFC domain params conform to FIPS-186-4 explicit domain param
  18. * validity tests.
  19. */
  20. return ffc_params_FIPS186_4_validate(dsa->libctx, &dsa->params,
  21. FFC_PARAM_TYPE_DSA, ret, NULL);
  22. }
  23. /*
  24. * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
  25. */
  26. int dsa_check_pub_key(const DSA *dsa, const BIGNUM *pub_key, int *ret)
  27. {
  28. return ffc_validate_public_key(&dsa->params, pub_key, ret);
  29. }
  30. /*
  31. * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
  32. * To only be used with ephemeral FFC public keys generated using the approved
  33. * safe-prime groups.
  34. */
  35. int dsa_check_pub_key_partial(const DSA *dsa, const BIGNUM *pub_key, int *ret)
  36. {
  37. return ffc_validate_public_key_partial(&dsa->params, pub_key, ret);
  38. }
  39. int dsa_check_priv_key(const DSA *dsa, const BIGNUM *priv_key, int *ret)
  40. {
  41. *ret = 0;
  42. return (dsa->params.q != NULL
  43. && ffc_validate_private_key(dsa->params.q, priv_key, ret));
  44. }
  45. /*
  46. * FFC pairwise check from SP800-56A R3.
  47. * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
  48. */
  49. int dsa_check_pairwise(const DSA *dsa)
  50. {
  51. int ret = 0;
  52. BN_CTX *ctx = NULL;
  53. BIGNUM *pub_key = NULL;
  54. if (dsa->params.p == NULL
  55. || dsa->params.g == NULL
  56. || dsa->priv_key == NULL
  57. || dsa->pub_key == NULL)
  58. return 0;
  59. ctx = BN_CTX_new_ex(dsa->libctx);
  60. if (ctx == NULL)
  61. goto err;
  62. pub_key = BN_new();
  63. if (pub_key == NULL)
  64. goto err;
  65. /* recalculate the public key = (g ^ priv) mod p */
  66. if (!dsa_generate_public_key(ctx, dsa, dsa->priv_key, pub_key))
  67. goto err;
  68. /* check it matches the existing pubic_key */
  69. ret = BN_cmp(pub_key, dsa->pub_key) == 0;
  70. err:
  71. BN_free(pub_key);
  72. BN_CTX_free(ctx);
  73. return ret;
  74. }