pmeth_gn.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* pmeth_gn.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2006.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include "cryptlib.h"
  62. #include <openssl/objects.h>
  63. #include <openssl/evp.h>
  64. #include <openssl/bn.h>
  65. #include "evp_locl.h"
  66. int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
  67. {
  68. int ret;
  69. if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
  70. EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
  71. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  72. return -2;
  73. }
  74. ctx->operation = EVP_PKEY_OP_PARAMGEN;
  75. if (!ctx->pmeth->paramgen_init)
  76. return 1;
  77. ret = ctx->pmeth->paramgen_init(ctx);
  78. if (ret <= 0)
  79. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  80. return ret;
  81. }
  82. int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  83. {
  84. int ret;
  85. if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
  86. EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
  87. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  88. return -2;
  89. }
  90. if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
  91. EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
  92. return -1;
  93. }
  94. if (ppkey == NULL)
  95. return -1;
  96. if (*ppkey == NULL)
  97. *ppkey = EVP_PKEY_new();
  98. if (*ppkey == NULL) {
  99. EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
  100. return -1;
  101. }
  102. ret = ctx->pmeth->paramgen(ctx, *ppkey);
  103. if (ret <= 0) {
  104. EVP_PKEY_free(*ppkey);
  105. *ppkey = NULL;
  106. }
  107. return ret;
  108. }
  109. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
  110. {
  111. int ret;
  112. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  113. EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
  114. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  115. return -2;
  116. }
  117. ctx->operation = EVP_PKEY_OP_KEYGEN;
  118. if (!ctx->pmeth->keygen_init)
  119. return 1;
  120. ret = ctx->pmeth->keygen_init(ctx);
  121. if (ret <= 0)
  122. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  123. return ret;
  124. }
  125. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  126. {
  127. int ret;
  128. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  129. EVPerr(EVP_F_EVP_PKEY_KEYGEN,
  130. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  131. return -2;
  132. }
  133. if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
  134. EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
  135. return -1;
  136. }
  137. if (!ppkey)
  138. return -1;
  139. if (*ppkey == NULL)
  140. *ppkey = EVP_PKEY_new();
  141. if (*ppkey == NULL)
  142. return -1;
  143. ret = ctx->pmeth->keygen(ctx, *ppkey);
  144. if (ret <= 0) {
  145. EVP_PKEY_free(*ppkey);
  146. *ppkey = NULL;
  147. }
  148. return ret;
  149. }
  150. void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
  151. {
  152. ctx->pkey_gencb = cb;
  153. }
  154. EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
  155. {
  156. return ctx->pkey_gencb;
  157. }
  158. /*
  159. * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
  160. * callbacks.
  161. */
  162. static int trans_cb(int a, int b, BN_GENCB *gcb)
  163. {
  164. EVP_PKEY_CTX *ctx = gcb->arg;
  165. ctx->keygen_info[0] = a;
  166. ctx->keygen_info[1] = b;
  167. return ctx->pkey_gencb(ctx);
  168. }
  169. void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
  170. {
  171. BN_GENCB_set(cb, trans_cb, ctx)
  172. }
  173. int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
  174. {
  175. if (idx == -1)
  176. return ctx->keygen_info_count;
  177. if (idx < 0 || idx > ctx->keygen_info_count)
  178. return 0;
  179. return ctx->keygen_info[idx];
  180. }
  181. EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
  182. const unsigned char *key, int keylen)
  183. {
  184. EVP_PKEY_CTX *mac_ctx = NULL;
  185. EVP_PKEY *mac_key = NULL;
  186. mac_ctx = EVP_PKEY_CTX_new_id(type, e);
  187. if (!mac_ctx)
  188. return NULL;
  189. if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
  190. goto merr;
  191. if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN,
  192. EVP_PKEY_CTRL_SET_MAC_KEY,
  193. keylen, (void *)key) <= 0)
  194. goto merr;
  195. if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
  196. goto merr;
  197. merr:
  198. if (mac_ctx)
  199. EVP_PKEY_CTX_free(mac_ctx);
  200. return mac_key;
  201. }