summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp/pmeth_gn.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/evp/pmeth_gn.c')
-rw-r--r--src/lib/libcrypto/evp/pmeth_gn.c133
1 files changed, 69 insertions, 64 deletions
diff --git a/src/lib/libcrypto/evp/pmeth_gn.c b/src/lib/libcrypto/evp/pmeth_gn.c
index 4651c81370..c6683ab89b 100644
--- a/src/lib/libcrypto/evp/pmeth_gn.c
+++ b/src/lib/libcrypto/evp/pmeth_gn.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -64,15 +64,16 @@
64#include <openssl/bn.h> 64#include <openssl/bn.h>
65#include "evp_locl.h" 65#include "evp_locl.h"
66 66
67int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx) 67int
68 { 68EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
69{
69 int ret; 70 int ret;
70 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) 71
71 { 72 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
72 EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT, 73 EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
73 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 74 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
74 return -2; 75 return -2;
75 } 76 }
76 ctx->operation = EVP_PKEY_OP_PARAMGEN; 77 ctx->operation = EVP_PKEY_OP_PARAMGEN;
77 if (!ctx->pmeth->paramgen_init) 78 if (!ctx->pmeth->paramgen_init)
78 return 1; 79 return 1;
@@ -80,23 +81,23 @@ int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
80 if (ret <= 0) 81 if (ret <= 0)
81 ctx->operation = EVP_PKEY_OP_UNDEFINED; 82 ctx->operation = EVP_PKEY_OP_UNDEFINED;
82 return ret; 83 return ret;
83 } 84}
84 85
85int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) 86int
86 { 87EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
88{
87 int ret; 89 int ret;
88 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) 90
89 { 91 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
90 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, 92 EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
91 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 93 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
92 return -2; 94 return -2;
93 } 95 }
94 96
95 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) 97 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
96 {
97 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED); 98 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
98 return -1; 99 return -1;
99 } 100 }
100 101
101 if (!ppkey) 102 if (!ppkey)
102 return -1; 103 return -1;
@@ -105,23 +106,23 @@ int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
105 *ppkey = EVP_PKEY_new(); 106 *ppkey = EVP_PKEY_new();
106 107
107 ret = ctx->pmeth->paramgen(ctx, *ppkey); 108 ret = ctx->pmeth->paramgen(ctx, *ppkey);
108 if (ret <= 0) 109 if (ret <= 0) {
109 {
110 EVP_PKEY_free(*ppkey); 110 EVP_PKEY_free(*ppkey);
111 *ppkey = NULL; 111 *ppkey = NULL;
112 }
113 return ret;
114 } 112 }
113 return ret;
114}
115 115
116int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) 116int
117 { 117EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
118{
118 int ret; 119 int ret;
119 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) 120
120 { 121 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
121 EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT, 122 EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
122 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 123 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
123 return -2; 124 return -2;
124 } 125 }
125 ctx->operation = EVP_PKEY_OP_KEYGEN; 126 ctx->operation = EVP_PKEY_OP_KEYGEN;
126 if (!ctx->pmeth->keygen_init) 127 if (!ctx->pmeth->keygen_init)
127 return 1; 128 return 1;
@@ -129,23 +130,22 @@ int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
129 if (ret <= 0) 130 if (ret <= 0)
130 ctx->operation = EVP_PKEY_OP_UNDEFINED; 131 ctx->operation = EVP_PKEY_OP_UNDEFINED;
131 return ret; 132 return ret;
132 } 133}
133 134
134int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) 135int
135 { 136EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
137{
136 int ret; 138 int ret;
137 139
138 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) 140 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
139 {
140 EVPerr(EVP_F_EVP_PKEY_KEYGEN, 141 EVPerr(EVP_F_EVP_PKEY_KEYGEN,
141 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 142 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
142 return -2; 143 return -2;
143 } 144 }
144 if (ctx->operation != EVP_PKEY_OP_KEYGEN) 145 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
145 {
146 EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED); 146 EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
147 return -1; 147 return -1;
148 } 148 }
149 149
150 if (!ppkey) 150 if (!ppkey)
151 return -1; 151 return -1;
@@ -154,68 +154,73 @@ int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
154 *ppkey = EVP_PKEY_new(); 154 *ppkey = EVP_PKEY_new();
155 155
156 ret = ctx->pmeth->keygen(ctx, *ppkey); 156 ret = ctx->pmeth->keygen(ctx, *ppkey);
157 if (ret <= 0) 157 if (ret <= 0) {
158 {
159 EVP_PKEY_free(*ppkey); 158 EVP_PKEY_free(*ppkey);
160 *ppkey = NULL; 159 *ppkey = NULL;
161 }
162 return ret;
163 } 160 }
161 return ret;
162}
164 163
165void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb) 164void
166 { 165EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
166{
167 ctx->pkey_gencb = cb; 167 ctx->pkey_gencb = cb;
168 } 168}
169 169
170EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx) 170EVP_PKEY_gen_cb *
171 { 171EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
172{
172 return ctx->pkey_gencb; 173 return ctx->pkey_gencb;
173 } 174}
174 175
175/* "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB 176/* "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB
176 * style callbacks. 177 * style callbacks.
177 */ 178 */
178 179
179static int trans_cb(int a, int b, BN_GENCB *gcb) 180static int
180 { 181trans_cb(int a, int b, BN_GENCB *gcb)
182{
181 EVP_PKEY_CTX *ctx = gcb->arg; 183 EVP_PKEY_CTX *ctx = gcb->arg;
182 ctx->keygen_info[0] = a; 184 ctx->keygen_info[0] = a;
183 ctx->keygen_info[1] = b; 185 ctx->keygen_info[1] = b;
184 return ctx->pkey_gencb(ctx); 186 return ctx->pkey_gencb(ctx);
185 } 187}
186 188
187void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx) 189void
188 { 190evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
191{
189 BN_GENCB_set(cb, trans_cb, ctx) 192 BN_GENCB_set(cb, trans_cb, ctx)
190 } 193}
191 194
192int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx) 195int
193 { 196EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
197{
194 if (idx == -1) 198 if (idx == -1)
195 return ctx->keygen_info_count; 199 return ctx->keygen_info_count;
196 if (idx < 0 || idx > ctx->keygen_info_count) 200 if (idx < 0 || idx > ctx->keygen_info_count)
197 return 0; 201 return 0;
198 return ctx->keygen_info[idx]; 202 return ctx->keygen_info[idx];
199 } 203}
200 204
201EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, 205EVP_PKEY *
202 const unsigned char *key, int keylen) 206EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key, int keylen)
203 { 207{
204 EVP_PKEY_CTX *mac_ctx = NULL; 208 EVP_PKEY_CTX *mac_ctx = NULL;
205 EVP_PKEY *mac_key = NULL; 209 EVP_PKEY *mac_key = NULL;
210
206 mac_ctx = EVP_PKEY_CTX_new_id(type, e); 211 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
207 if (!mac_ctx) 212 if (!mac_ctx)
208 return NULL; 213 return NULL;
209 if (EVP_PKEY_keygen_init(mac_ctx) <= 0) 214 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
210 goto merr; 215 goto merr;
211 if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN, 216 if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN,
212 EVP_PKEY_CTRL_SET_MAC_KEY, 217 EVP_PKEY_CTRL_SET_MAC_KEY, keylen, (void *)key) <= 0)
213 keylen, (void *)key) <= 0)
214 goto merr; 218 goto merr;
215 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) 219 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
216 goto merr; 220 goto merr;
217 merr: 221
222merr:
218 if (mac_ctx) 223 if (mac_ctx)
219 EVP_PKEY_CTX_free(mac_ctx); 224 EVP_PKEY_CTX_free(mac_ctx);
220 return mac_key; 225 return mac_key;
221 } 226}