rsa_acvp_test_params.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <string.h> /* memcpy */
  10. #include <openssl/core_names.h>
  11. #include <openssl/param_build.h>
  12. #include "crypto/rsa.h"
  13. #include "rsa_local.h"
  14. int ossl_rsa_acvp_test_gen_params_new(OSSL_PARAM **dst, const OSSL_PARAM src[])
  15. {
  16. const OSSL_PARAM *p, *s;
  17. OSSL_PARAM *d, *alloc = NULL;
  18. int ret = 1;
  19. static const OSSL_PARAM settable[] = {
  20. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XP, NULL, 0),
  21. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XP1, NULL, 0),
  22. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XP2, NULL, 0),
  23. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XQ, NULL, 0),
  24. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XQ1, NULL, 0),
  25. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XQ2, NULL, 0),
  26. OSSL_PARAM_END
  27. };
  28. /* Assume the first element is a required field if this feature is used */
  29. p = OSSL_PARAM_locate_const(src, settable[0].key);
  30. if (p == NULL)
  31. return 1;
  32. /* Zeroing here means the terminator is always set at the end */
  33. alloc = OPENSSL_zalloc(sizeof(settable));
  34. if (alloc == NULL)
  35. return 0;
  36. d = alloc;
  37. for (s = settable; s->key != NULL; ++s) {
  38. /* If src contains a key from settable then copy the src to the dest */
  39. p = OSSL_PARAM_locate_const(src, s->key);
  40. if (p != NULL) {
  41. *d = *s; /* shallow copy from the static settable[] */
  42. d->data_size = p->data_size;
  43. d->data = OPENSSL_memdup(p->data, p->data_size);
  44. if (d->data == NULL)
  45. ret = 0;
  46. ++d;
  47. }
  48. }
  49. if (ret == 0) {
  50. ossl_rsa_acvp_test_gen_params_free(alloc);
  51. alloc = NULL;
  52. }
  53. if (*dst != NULL)
  54. ossl_rsa_acvp_test_gen_params_free(*dst);
  55. *dst = alloc;
  56. return ret;
  57. }
  58. void ossl_rsa_acvp_test_gen_params_free(OSSL_PARAM *dst)
  59. {
  60. OSSL_PARAM *p;
  61. if (dst == NULL)
  62. return;
  63. for (p = dst; p->key != NULL; ++p) {
  64. OPENSSL_free(p->data);
  65. p->data = NULL;
  66. }
  67. OPENSSL_free(dst);
  68. }
  69. int ossl_rsa_acvp_test_set_params(RSA *r, const OSSL_PARAM params[])
  70. {
  71. RSA_ACVP_TEST *t;
  72. const OSSL_PARAM *p;
  73. if (r->acvp_test != NULL) {
  74. ossl_rsa_acvp_test_free(r->acvp_test);
  75. r->acvp_test = NULL;
  76. }
  77. t = OPENSSL_zalloc(sizeof(*t));
  78. if (t == NULL)
  79. return 0;
  80. /* Set the input parameters */
  81. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XP1)) != NULL
  82. && !OSSL_PARAM_get_BN(p, &t->Xp1))
  83. goto err;
  84. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XP2)) != NULL
  85. && !OSSL_PARAM_get_BN(p, &t->Xp2))
  86. goto err;
  87. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XP)) != NULL
  88. && !OSSL_PARAM_get_BN(p, &t->Xp))
  89. goto err;
  90. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XQ1)) != NULL
  91. && !OSSL_PARAM_get_BN(p, &t->Xq1))
  92. goto err;
  93. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XQ2)) != NULL
  94. && !OSSL_PARAM_get_BN(p, &t->Xq2))
  95. goto err;
  96. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XQ)) != NULL
  97. && !OSSL_PARAM_get_BN(p, &t->Xq))
  98. goto err;
  99. /* Setup the output parameters */
  100. t->p1 = BN_new();
  101. t->p2 = BN_new();
  102. t->q1 = BN_new();
  103. t->q2 = BN_new();
  104. r->acvp_test = t;
  105. return 1;
  106. err:
  107. ossl_rsa_acvp_test_free(t);
  108. return 0;
  109. }
  110. int ossl_rsa_acvp_test_get_params(RSA *r, OSSL_PARAM params[])
  111. {
  112. RSA_ACVP_TEST *t;
  113. OSSL_PARAM *p;
  114. if (r == NULL)
  115. return 0;
  116. t = r->acvp_test;
  117. if (t != NULL) {
  118. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_TEST_P1)) != NULL
  119. && !OSSL_PARAM_set_BN(p, t->p1))
  120. return 0;
  121. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_TEST_P2)) != NULL
  122. && !OSSL_PARAM_set_BN(p, t->p2))
  123. return 0;
  124. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_TEST_Q1)) != NULL
  125. && !OSSL_PARAM_set_BN(p, t->q1))
  126. return 0;
  127. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_TEST_Q2)) != NULL
  128. && !OSSL_PARAM_set_BN(p, t->q2))
  129. return 0;
  130. }
  131. return 1;
  132. }
  133. void ossl_rsa_acvp_test_free(RSA_ACVP_TEST *t)
  134. {
  135. if (t != NULL) {
  136. BN_free(t->Xp1);
  137. BN_free(t->Xp2);
  138. BN_free(t->Xp);
  139. BN_free(t->Xq1);
  140. BN_free(t->Xq2);
  141. BN_free(t->Xq);
  142. BN_free(t->p1);
  143. BN_free(t->p2);
  144. BN_free(t->q1);
  145. BN_free(t->q2);
  146. OPENSSL_free(t);
  147. }
  148. }