ffc_backend.c 4.1 KB

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