2
0

ffc_backend.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2020-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 <openssl/core_names.h>
  10. #include "internal/ffc.h"
  11. #include "internal/sizes.h"
  12. /*
  13. * The intention with the "backend" source file is to offer backend support
  14. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  15. * implementations alike.
  16. */
  17. int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[])
  18. {
  19. const OSSL_PARAM *prm;
  20. const OSSL_PARAM *param_p, *param_q, *param_g;
  21. BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
  22. int i;
  23. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
  24. if (prm != NULL) {
  25. /*
  26. * In a no-dh build we just go straight to err because we have no
  27. * support for this.
  28. */
  29. #ifndef OPENSSL_NO_DH
  30. const DH_NAMED_GROUP *group = NULL;
  31. if (prm->data_type != OSSL_PARAM_UTF8_STRING
  32. || prm->data == NULL
  33. || (group = ossl_ffc_name_to_dh_named_group(prm->data)) == NULL
  34. || !ossl_ffc_named_group_set(ffc, group))
  35. #endif
  36. goto err;
  37. }
  38. param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
  39. param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
  40. param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
  41. if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
  42. || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
  43. || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
  44. goto err;
  45. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
  46. if (prm != NULL) {
  47. if (!OSSL_PARAM_get_int(prm, &i))
  48. goto err;
  49. ffc->gindex = i;
  50. }
  51. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
  52. if (prm != NULL) {
  53. if (!OSSL_PARAM_get_int(prm, &i))
  54. goto err;
  55. ffc->pcounter = i;
  56. }
  57. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_COFACTOR);
  58. if (prm != NULL && !OSSL_PARAM_get_BN(prm, &j))
  59. goto err;
  60. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
  61. if (prm != NULL) {
  62. if (!OSSL_PARAM_get_int(prm, &i))
  63. goto err;
  64. ffc->h = i;
  65. }
  66. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
  67. if (prm != NULL) {
  68. if (prm->data_type != OSSL_PARAM_OCTET_STRING
  69. || !ossl_ffc_params_set_seed(ffc, prm->data, prm->data_size))
  70. goto err;
  71. }
  72. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ);
  73. if (prm != NULL) {
  74. if (!OSSL_PARAM_get_int(prm, &i))
  75. goto err;
  76. ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_PQ, i);
  77. }
  78. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_G);
  79. if (prm != NULL) {
  80. if (!OSSL_PARAM_get_int(prm, &i))
  81. goto err;
  82. ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_G, i);
  83. }
  84. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY);
  85. if (prm != NULL) {
  86. if (!OSSL_PARAM_get_int(prm, &i))
  87. goto err;
  88. ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY, i);
  89. }
  90. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
  91. if (prm != NULL) {
  92. const OSSL_PARAM *p1;
  93. const char *props = NULL;
  94. if (prm->data_type != OSSL_PARAM_UTF8_STRING)
  95. goto err;
  96. p1 = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
  97. if (p1 != NULL) {
  98. if (p1->data_type != OSSL_PARAM_UTF8_STRING)
  99. goto err;
  100. props = p1->data;
  101. }
  102. ossl_ffc_set_digest(ffc, prm->data, props);
  103. }
  104. ossl_ffc_params_set0_pqg(ffc, p, q, g);
  105. ossl_ffc_params_set0_j(ffc, j);
  106. return 1;
  107. err:
  108. BN_free(j);
  109. BN_free(p);
  110. BN_free(q);
  111. BN_free(g);
  112. return 0;
  113. }