pmeth_gn.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdio.h>
  10. #include <stdlib.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/objects.h>
  13. #include <openssl/evp.h>
  14. #include "internal/bn_int.h"
  15. #include "internal/evp_int.h"
  16. int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
  17. {
  18. int ret;
  19. if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
  20. EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
  21. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  22. return -2;
  23. }
  24. ctx->operation = EVP_PKEY_OP_PARAMGEN;
  25. if (!ctx->pmeth->paramgen_init)
  26. return 1;
  27. ret = ctx->pmeth->paramgen_init(ctx);
  28. if (ret <= 0)
  29. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  30. return ret;
  31. }
  32. int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  33. {
  34. int ret;
  35. if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
  36. EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
  37. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  38. return -2;
  39. }
  40. if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
  41. EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
  42. return -1;
  43. }
  44. if (ppkey == NULL)
  45. return -1;
  46. if (*ppkey == NULL)
  47. *ppkey = EVP_PKEY_new();
  48. if (*ppkey == NULL) {
  49. EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
  50. return -1;
  51. }
  52. ret = ctx->pmeth->paramgen(ctx, *ppkey);
  53. if (ret <= 0) {
  54. EVP_PKEY_free(*ppkey);
  55. *ppkey = NULL;
  56. }
  57. return ret;
  58. }
  59. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
  60. {
  61. int ret;
  62. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  63. EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
  64. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  65. return -2;
  66. }
  67. ctx->operation = EVP_PKEY_OP_KEYGEN;
  68. if (!ctx->pmeth->keygen_init)
  69. return 1;
  70. ret = ctx->pmeth->keygen_init(ctx);
  71. if (ret <= 0)
  72. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  73. return ret;
  74. }
  75. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  76. {
  77. int ret;
  78. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  79. EVPerr(EVP_F_EVP_PKEY_KEYGEN,
  80. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  81. return -2;
  82. }
  83. if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
  84. EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
  85. return -1;
  86. }
  87. if (ppkey == NULL)
  88. return -1;
  89. if (*ppkey == NULL)
  90. *ppkey = EVP_PKEY_new();
  91. if (*ppkey == NULL)
  92. return -1;
  93. ret = ctx->pmeth->keygen(ctx, *ppkey);
  94. if (ret <= 0) {
  95. EVP_PKEY_free(*ppkey);
  96. *ppkey = NULL;
  97. }
  98. return ret;
  99. }
  100. void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
  101. {
  102. ctx->pkey_gencb = cb;
  103. }
  104. EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
  105. {
  106. return ctx->pkey_gencb;
  107. }
  108. /*
  109. * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
  110. * callbacks.
  111. */
  112. static int trans_cb(int a, int b, BN_GENCB *gcb)
  113. {
  114. EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
  115. ctx->keygen_info[0] = a;
  116. ctx->keygen_info[1] = b;
  117. return ctx->pkey_gencb(ctx);
  118. }
  119. void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
  120. {
  121. BN_GENCB_set(cb, trans_cb, ctx);
  122. }
  123. int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
  124. {
  125. if (idx == -1)
  126. return ctx->keygen_info_count;
  127. if (idx < 0 || idx > ctx->keygen_info_count)
  128. return 0;
  129. return ctx->keygen_info[idx];
  130. }
  131. EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
  132. const unsigned char *key, int keylen)
  133. {
  134. EVP_PKEY_CTX *mac_ctx = NULL;
  135. EVP_PKEY *mac_key = NULL;
  136. mac_ctx = EVP_PKEY_CTX_new_id(type, e);
  137. if (!mac_ctx)
  138. return NULL;
  139. if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
  140. goto merr;
  141. if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
  142. goto merr;
  143. if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
  144. goto merr;
  145. merr:
  146. EVP_PKEY_CTX_free(mac_ctx);
  147. return mac_key;
  148. }