dsa_ctrl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2020-2021 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 <stdlib.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/err.h>
  12. #include <openssl/dsa.h>
  13. #include <openssl/evp.h>
  14. #include "crypto/evp.h"
  15. static int dsa_paramgen_check(EVP_PKEY_CTX *ctx)
  16. {
  17. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  18. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  19. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  20. return -2;
  21. }
  22. /* If key type not DSA return error */
  23. if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_DSA)
  24. return -1;
  25. return 1;
  26. }
  27. int EVP_PKEY_CTX_set_dsa_paramgen_type(EVP_PKEY_CTX *ctx, const char *name)
  28. {
  29. int ret;
  30. OSSL_PARAM params[2], *p = params;
  31. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  32. return ret;
  33. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
  34. (char *)name, 0);
  35. *p++ = OSSL_PARAM_construct_end();
  36. return EVP_PKEY_CTX_set_params(ctx, params);
  37. }
  38. int EVP_PKEY_CTX_set_dsa_paramgen_gindex(EVP_PKEY_CTX *ctx, int gindex)
  39. {
  40. int ret;
  41. OSSL_PARAM params[2], *p = params;
  42. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  43. return ret;
  44. *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
  45. *p++ = OSSL_PARAM_construct_end();
  46. return EVP_PKEY_CTX_set_params(ctx, params);
  47. }
  48. int EVP_PKEY_CTX_set_dsa_paramgen_seed(EVP_PKEY_CTX *ctx,
  49. const unsigned char *seed,
  50. size_t seedlen)
  51. {
  52. int ret;
  53. OSSL_PARAM params[2], *p = params;
  54. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  55. return ret;
  56. *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
  57. (void *)seed, seedlen);
  58. *p++ = OSSL_PARAM_construct_end();
  59. return EVP_PKEY_CTX_set_params(ctx, params);
  60. }
  61. int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits)
  62. {
  63. int ret;
  64. OSSL_PARAM params[2], *p = params;
  65. size_t bits = nbits;
  66. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  67. return ret;
  68. *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_PBITS, &bits);
  69. *p++ = OSSL_PARAM_construct_end();
  70. return EVP_PKEY_CTX_set_params(ctx, params);
  71. }
  72. int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx, int qbits)
  73. {
  74. int ret;
  75. OSSL_PARAM params[2], *p = params;
  76. size_t bits2 = qbits;
  77. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  78. return ret;
  79. *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_QBITS, &bits2);
  80. *p++ = OSSL_PARAM_construct_end();
  81. return EVP_PKEY_CTX_set_params(ctx, params);
  82. }
  83. int EVP_PKEY_CTX_set_dsa_paramgen_md_props(EVP_PKEY_CTX *ctx,
  84. const char *md_name,
  85. const char *md_properties)
  86. {
  87. int ret;
  88. OSSL_PARAM params[3], *p = params;
  89. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  90. return ret;
  91. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
  92. (char *)md_name, 0);
  93. if (md_properties != NULL)
  94. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
  95. (char *)md_properties, 0);
  96. *p++ = OSSL_PARAM_construct_end();
  97. return EVP_PKEY_CTX_set_params(ctx, params);
  98. }
  99. #if !defined(FIPS_MODULE)
  100. /* TODO(3.0): deprecate as this is needed only for legacy? */
  101. int EVP_PKEY_CTX_set_dsa_paramgen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  102. {
  103. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
  104. EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, (void *)(md));
  105. }
  106. #endif