1 /* Written by Dr Stephen N Henson (steve (at) openssl.org) for the OpenSSL 2 * project 2006. 3 */ 4 /* ==================================================================== 5 * Copyright (c) 2006 The OpenSSL Project. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. All advertising materials mentioning features or use of this 20 * software must display the following acknowledgment: 21 * "This product includes software developed by the OpenSSL Project 22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 23 * 24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 25 * endorse or promote products derived from this software without 26 * prior written permission. For written permission, please contact 27 * licensing (at) OpenSSL.org. 28 * 29 * 5. Products derived from this software may not be called "OpenSSL" 30 * nor may "OpenSSL" appear in their names without prior written 31 * permission of the OpenSSL Project. 32 * 33 * 6. Redistributions of any form whatsoever must retain the following 34 * acknowledgment: 35 * "This product includes software developed by the OpenSSL Project 36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 37 * 38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 49 * OF THE POSSIBILITY OF SUCH DAMAGE. 50 * ==================================================================== 51 * 52 * This product includes cryptographic software written by Eric Young 53 * (eay (at) cryptsoft.com). This product includes software written by Tim 54 * Hudson (tjh (at) cryptsoft.com). */ 55 56 #include <openssl/evp.h> 57 58 #include <string.h> 59 60 #include <openssl/bn.h> 61 #include <openssl/buf.h> 62 #include <openssl/digest.h> 63 #include <openssl/ec.h> 64 #include <openssl/ec_key.h> 65 #include <openssl/ecdh.h> 66 #include <openssl/ecdsa.h> 67 #include <openssl/err.h> 68 #include <openssl/mem.h> 69 #include <openssl/nid.h> 70 71 #include "internal.h" 72 #include "../fipsmodule/ec/internal.h" 73 #include "../internal.h" 74 75 76 typedef struct { 77 // message digest 78 const EVP_MD *md; 79 EC_GROUP *gen_group; 80 } EC_PKEY_CTX; 81 82 83 static int pkey_ec_init(EVP_PKEY_CTX *ctx) { 84 EC_PKEY_CTX *dctx; 85 dctx = OPENSSL_malloc(sizeof(EC_PKEY_CTX)); 86 if (!dctx) { 87 return 0; 88 } 89 OPENSSL_memset(dctx, 0, sizeof(EC_PKEY_CTX)); 90 91 ctx->data = dctx; 92 93 return 1; 94 } 95 96 static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { 97 EC_PKEY_CTX *dctx, *sctx; 98 if (!pkey_ec_init(dst)) { 99 return 0; 100 } 101 sctx = src->data; 102 dctx = dst->data; 103 104 dctx->md = sctx->md; 105 106 return 1; 107 } 108 109 static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx) { 110 EC_PKEY_CTX *dctx = ctx->data; 111 if (!dctx) { 112 return; 113 } 114 115 EC_GROUP_free(dctx->gen_group); 116 OPENSSL_free(dctx); 117 } 118 119 static int pkey_ec_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen, 120 const uint8_t *tbs, size_t tbslen) { 121 unsigned int sltmp; 122 EC_KEY *ec = ctx->pkey->pkey.ec; 123 124 if (!sig) { 125 *siglen = ECDSA_size(ec); 126 return 1; 127 } else if (*siglen < (size_t)ECDSA_size(ec)) { 128 OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); 129 return 0; 130 } 131 132 if (!ECDSA_sign(0, tbs, tbslen, sig, &sltmp, ec)) { 133 return 0; 134 } 135 *siglen = (size_t)sltmp; 136 return 1; 137 } 138 139 static int pkey_ec_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t siglen, 140 const uint8_t *tbs, size_t tbslen) { 141 return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->pkey->pkey.ec); 142 } 143 144 static int pkey_ec_derive(EVP_PKEY_CTX *ctx, uint8_t *key, 145 size_t *keylen) { 146 int ret; 147 size_t outlen; 148 const EC_POINT *pubkey = NULL; 149 EC_KEY *eckey; 150 151 if (!ctx->pkey || !ctx->peerkey) { 152 OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET); 153 return 0; 154 } 155 156 eckey = ctx->pkey->pkey.ec; 157 158 if (!key) { 159 const EC_GROUP *group; 160 group = EC_KEY_get0_group(eckey); 161 *keylen = (EC_GROUP_get_degree(group) + 7) / 8; 162 return 1; 163 } 164 pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec); 165 166 // NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is 167 // not an error, the result is truncated. 168 169 outlen = *keylen; 170 171 ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0); 172 if (ret < 0) { 173 return 0; 174 } 175 *keylen = ret; 176 return 1; 177 } 178 179 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { 180 EC_PKEY_CTX *dctx = ctx->data; 181 182 switch (type) { 183 case EVP_PKEY_CTRL_MD: 184 if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && 185 EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 && 186 EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && 187 EVP_MD_type((const EVP_MD *)p2) != NID_sha256 && 188 EVP_MD_type((const EVP_MD *)p2) != NID_sha384 && 189 EVP_MD_type((const EVP_MD *)p2) != NID_sha512) { 190 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_TYPE); 191 return 0; 192 } 193 dctx->md = p2; 194 return 1; 195 196 case EVP_PKEY_CTRL_GET_MD: 197 *(const EVP_MD **)p2 = dctx->md; 198 return 1; 199 200 case EVP_PKEY_CTRL_PEER_KEY: 201 // Default behaviour is OK 202 return 1; 203 204 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID: { 205 EC_GROUP *group = EC_GROUP_new_by_curve_name(p1); 206 if (group == NULL) { 207 return 0; 208 } 209 EC_GROUP_free(dctx->gen_group); 210 dctx->gen_group = group; 211 return 1; 212 } 213 214 default: 215 OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); 216 return 0; 217 } 218 } 219 220 static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { 221 EC_PKEY_CTX *dctx = ctx->data; 222 const EC_GROUP *group = dctx->gen_group; 223 if (group == NULL) { 224 if (ctx->pkey == NULL) { 225 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_PARAMETERS_SET); 226 return 0; 227 } 228 group = EC_KEY_get0_group(ctx->pkey->pkey.ec); 229 } 230 EC_KEY *ec = EC_KEY_new(); 231 if (ec == NULL || 232 !EC_KEY_set_group(ec, group) || 233 !EC_KEY_generate_key(ec)) { 234 EC_KEY_free(ec); 235 return 0; 236 } 237 EVP_PKEY_assign_EC_KEY(pkey, ec); 238 return 1; 239 } 240 241 static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { 242 EC_PKEY_CTX *dctx = ctx->data; 243 if (dctx->gen_group == NULL) { 244 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_PARAMETERS_SET); 245 return 0; 246 } 247 EC_KEY *ec = EC_KEY_new(); 248 if (ec == NULL || 249 !EC_KEY_set_group(ec, dctx->gen_group)) { 250 EC_KEY_free(ec); 251 return 0; 252 } 253 EVP_PKEY_assign_EC_KEY(pkey, ec); 254 return 1; 255 } 256 257 const EVP_PKEY_METHOD ec_pkey_meth = { 258 EVP_PKEY_EC, 259 pkey_ec_init, 260 pkey_ec_copy, 261 pkey_ec_cleanup, 262 pkey_ec_keygen, 263 pkey_ec_sign, 264 NULL /* sign_message */, 265 pkey_ec_verify, 266 NULL /* verify_message */, 267 NULL /* verify_recover */, 268 NULL /* encrypt */, 269 NULL /* decrypt */, 270 pkey_ec_derive, 271 pkey_ec_paramgen, 272 pkey_ec_ctrl, 273 }; 274 275 int EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX *ctx, int nid) { 276 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, EVP_PKEY_OP_TYPE_GEN, 277 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL); 278 } 279 280 int EVP_PKEY_CTX_set_ec_param_enc(EVP_PKEY_CTX *ctx, int encoding) { 281 // BoringSSL only supports named curve syntax. 282 if (encoding != OPENSSL_EC_NAMED_CURVE) { 283 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PARAMETERS); 284 return 0; 285 } 286 return 1; 287 } 288