2
0

ffc_backend.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2020-2022 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. if (ffc == NULL)
  24. return 0;
  25. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
  26. if (prm != NULL) {
  27. /*
  28. * In a no-dh build we just go straight to err because we have no
  29. * support for this.
  30. */
  31. #ifndef OPENSSL_NO_DH
  32. const DH_NAMED_GROUP *group = NULL;
  33. if (prm->data_type != OSSL_PARAM_UTF8_STRING
  34. || prm->data == NULL
  35. || (group = ossl_ffc_name_to_dh_named_group(prm->data)) == NULL
  36. || !ossl_ffc_named_group_set(ffc, group))
  37. #endif
  38. goto err;
  39. }
  40. param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
  41. param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
  42. param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
  43. if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
  44. || (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
  45. || (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
  46. goto err;
  47. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
  48. if (prm != NULL) {
  49. if (!OSSL_PARAM_get_int(prm, &i))
  50. goto err;
  51. ffc->gindex = i;
  52. }
  53. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
  54. if (prm != NULL) {
  55. if (!OSSL_PARAM_get_int(prm, &i))
  56. goto err;
  57. ffc->pcounter = i;
  58. }
  59. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_COFACTOR);
  60. if (prm != NULL && !OSSL_PARAM_get_BN(prm, &j))
  61. goto err;
  62. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
  63. if (prm != NULL) {
  64. if (!OSSL_PARAM_get_int(prm, &i))
  65. goto err;
  66. ffc->h = i;
  67. }
  68. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
  69. if (prm != NULL) {
  70. if (prm->data_type != OSSL_PARAM_OCTET_STRING)
  71. goto err;
  72. if (!ossl_ffc_params_set_seed(ffc, prm->data, prm->data_size))
  73. goto err;
  74. }
  75. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ);
  76. if (prm != NULL) {
  77. if (!OSSL_PARAM_get_int(prm, &i))
  78. goto err;
  79. ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_PQ, i);
  80. }
  81. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_G);
  82. if (prm != NULL) {
  83. if (!OSSL_PARAM_get_int(prm, &i))
  84. goto err;
  85. ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_G, i);
  86. }
  87. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY);
  88. if (prm != NULL) {
  89. if (!OSSL_PARAM_get_int(prm, &i))
  90. goto err;
  91. ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY, i);
  92. }
  93. prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
  94. if (prm != NULL) {
  95. const OSSL_PARAM *p1;
  96. const char *props = NULL;
  97. if (prm->data_type != OSSL_PARAM_UTF8_STRING)
  98. goto err;
  99. p1 = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
  100. if (p1 != NULL) {
  101. if (p1->data_type != OSSL_PARAM_UTF8_STRING)
  102. goto err;
  103. }
  104. if (!ossl_ffc_set_digest(ffc, prm->data, props))
  105. goto err;
  106. }
  107. ossl_ffc_params_set0_pqg(ffc, p, q, g);
  108. ossl_ffc_params_set0_j(ffc, j);
  109. return 1;
  110. err:
  111. BN_free(j);
  112. BN_free(p);
  113. BN_free(q);
  114. BN_free(g);
  115. return 0;
  116. }