1 /* $OpenBSD: pmeth_gn.c,v 1.10 2022/01/10 12:10:26 tb Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 2006. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #include <stdio.h> 60 #include <stdlib.h> 61 62 #include <openssl/bn.h> 63 #include <openssl/err.h> 64 #include <openssl/evp.h> 65 #include <openssl/objects.h> 66 67 #include "asn1_locl.h" 68 #include "bn_lcl.h" 69 #include "evp_locl.h" 70 71 int 72 EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx) 73 { 74 int ret; 75 76 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) { 77 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 78 return -2; 79 } 80 ctx->operation = EVP_PKEY_OP_PARAMGEN; 81 if (!ctx->pmeth->paramgen_init) 82 return 1; 83 ret = ctx->pmeth->paramgen_init(ctx); 84 if (ret <= 0) 85 ctx->operation = EVP_PKEY_OP_UNDEFINED; 86 return ret; 87 } 88 89 int 90 EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) 91 { 92 int ret; 93 94 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) { 95 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 96 return -2; 97 } 98 99 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) { 100 EVPerror(EVP_R_OPERATON_NOT_INITIALIZED); 101 return -1; 102 } 103 104 if (!ppkey) 105 return -1; 106 107 if (!*ppkey) 108 *ppkey = EVP_PKEY_new(); 109 110 ret = ctx->pmeth->paramgen(ctx, *ppkey); 111 if (ret <= 0) { 112 EVP_PKEY_free(*ppkey); 113 *ppkey = NULL; 114 } 115 return ret; 116 } 117 118 int 119 EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) 120 { 121 int ret; 122 123 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) { 124 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 125 return -2; 126 } 127 ctx->operation = EVP_PKEY_OP_KEYGEN; 128 if (!ctx->pmeth->keygen_init) 129 return 1; 130 ret = ctx->pmeth->keygen_init(ctx); 131 if (ret <= 0) 132 ctx->operation = EVP_PKEY_OP_UNDEFINED; 133 return ret; 134 } 135 136 int 137 EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) 138 { 139 int ret; 140 141 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) { 142 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 143 return -2; 144 } 145 if (ctx->operation != EVP_PKEY_OP_KEYGEN) { 146 EVPerror(EVP_R_OPERATON_NOT_INITIALIZED); 147 return -1; 148 } 149 150 if (!ppkey) 151 return -1; 152 153 if (!*ppkey) 154 *ppkey = EVP_PKEY_new(); 155 156 ret = ctx->pmeth->keygen(ctx, *ppkey); 157 if (ret <= 0) { 158 EVP_PKEY_free(*ppkey); 159 *ppkey = NULL; 160 } 161 return ret; 162 } 163 164 void 165 EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb) 166 { 167 ctx->pkey_gencb = cb; 168 } 169 170 EVP_PKEY_gen_cb * 171 EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx) 172 { 173 return ctx->pkey_gencb; 174 } 175 176 /* "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB 177 * style callbacks. 178 */ 179 180 static int 181 trans_cb(int a, int b, BN_GENCB *gcb) 182 { 183 EVP_PKEY_CTX *ctx = gcb->arg; 184 ctx->keygen_info[0] = a; 185 ctx->keygen_info[1] = b; 186 return ctx->pkey_gencb(ctx); 187 } 188 189 void 190 evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx) 191 { 192 BN_GENCB_set(cb, trans_cb, ctx); 193 } 194 195 int 196 EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx) 197 { 198 if (idx == -1) 199 return ctx->keygen_info_count; 200 if (idx < 0 || idx > ctx->keygen_info_count) 201 return 0; 202 return ctx->keygen_info[idx]; 203 } 204 205 EVP_PKEY * 206 EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key, int keylen) 207 { 208 EVP_PKEY_CTX *mac_ctx = NULL; 209 EVP_PKEY *mac_key = NULL; 210 211 mac_ctx = EVP_PKEY_CTX_new_id(type, e); 212 if (!mac_ctx) 213 return NULL; 214 if (EVP_PKEY_keygen_init(mac_ctx) <= 0) 215 goto merr; 216 if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN, 217 EVP_PKEY_CTRL_SET_MAC_KEY, keylen, (void *)key) <= 0) 218 goto merr; 219 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) 220 goto merr; 221 222 merr: 223 EVP_PKEY_CTX_free(mac_ctx); 224 return mac_key; 225 } 226 227 int 228 EVP_PKEY_check(EVP_PKEY_CTX *ctx) 229 { 230 EVP_PKEY *pkey; 231 232 if ((pkey = ctx->pkey) == NULL) { 233 EVPerror(EVP_R_NO_KEY_SET); 234 return 0; 235 } 236 237 if (ctx->pmeth->check != NULL) 238 return ctx->pmeth->check(pkey); 239 240 if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) { 241 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 242 return -2; 243 } 244 245 return pkey->ameth->pkey_check(pkey); 246 } 247 248 int 249 EVP_PKEY_public_check(EVP_PKEY_CTX *ctx) 250 { 251 EVP_PKEY *pkey; 252 253 if ((pkey = ctx->pkey) == NULL) { 254 EVPerror(EVP_R_NO_KEY_SET); 255 return 0; 256 } 257 258 if (ctx->pmeth->public_check != NULL) 259 return ctx->pmeth->public_check(pkey); 260 261 if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) { 262 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 263 return -2; 264 } 265 266 return pkey->ameth->pkey_public_check(pkey); 267 } 268 269 int 270 EVP_PKEY_param_check(EVP_PKEY_CTX *ctx) 271 { 272 EVP_PKEY *pkey; 273 274 if ((pkey = ctx->pkey) == NULL) { 275 EVPerror(EVP_R_NO_KEY_SET); 276 return 0; 277 } 278 279 if (ctx->pmeth->param_check != NULL) 280 return ctx->pmeth->param_check(pkey); 281 282 if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) { 283 EVPerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 284 return -2; 285 } 286 287 return pkey->ameth->pkey_param_check(pkey); 288 } 289