1 /* 2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 3 * 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 "cryptlib.h" 61 #include <openssl/asn1t.h> 62 #include <openssl/x509.h> 63 #include <openssl/ec.h> 64 #include "ec_lcl.h" 65 #include <openssl/ecdsa.h> 66 #include <openssl/evp.h> 67 #include "evp_locl.h" 68 69 /* EC pkey context structure */ 70 71 typedef struct { 72 /* Key and paramgen group */ 73 EC_GROUP *gen_group; 74 /* message digest */ 75 const EVP_MD *md; 76 /* Duplicate key if custom cofactor needed */ 77 EC_KEY *co_key; 78 /* Cofactor mode */ 79 signed char cofactor_mode; 80 /* KDF (if any) to use for ECDH */ 81 char kdf_type; 82 /* Message digest to use for key derivation */ 83 const EVP_MD *kdf_md; 84 /* User key material */ 85 unsigned char *kdf_ukm; 86 size_t kdf_ukmlen; 87 /* KDF output length */ 88 size_t kdf_outlen; 89 } EC_PKEY_CTX; 90 91 static int pkey_ec_init(EVP_PKEY_CTX *ctx) 92 { 93 EC_PKEY_CTX *dctx; 94 dctx = OPENSSL_malloc(sizeof(EC_PKEY_CTX)); 95 if (!dctx) 96 return 0; 97 dctx->gen_group = NULL; 98 dctx->md = NULL; 99 100 dctx->cofactor_mode = -1; 101 dctx->co_key = NULL; 102 dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE; 103 dctx->kdf_md = NULL; 104 dctx->kdf_outlen = 0; 105 dctx->kdf_ukm = NULL; 106 dctx->kdf_ukmlen = 0; 107 108 ctx->data = dctx; 109 110 return 1; 111 } 112 113 static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) 114 { 115 EC_PKEY_CTX *dctx, *sctx; 116 if (!pkey_ec_init(dst)) 117 return 0; 118 sctx = src->data; 119 dctx = dst->data; 120 if (sctx->gen_group) { 121 dctx->gen_group = EC_GROUP_dup(sctx->gen_group); 122 if (!dctx->gen_group) 123 return 0; 124 } 125 dctx->md = sctx->md; 126 127 if (sctx->co_key) { 128 dctx->co_key = EC_KEY_dup(sctx->co_key); 129 if (!dctx->co_key) 130 return 0; 131 } 132 dctx->kdf_type = sctx->kdf_type; 133 dctx->kdf_md = sctx->kdf_md; 134 dctx->kdf_outlen = sctx->kdf_outlen; 135 if (sctx->kdf_ukm) { 136 dctx->kdf_ukm = BUF_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen); 137 if (!dctx->kdf_ukm) 138 return 0; 139 } else 140 dctx->kdf_ukm = NULL; 141 dctx->kdf_ukmlen = sctx->kdf_ukmlen; 142 return 1; 143 } 144 145 static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx) 146 { 147 EC_PKEY_CTX *dctx = ctx->data; 148 if (dctx) { 149 if (dctx->gen_group) 150 EC_GROUP_free(dctx->gen_group); 151 if (dctx->co_key) 152 EC_KEY_free(dctx->co_key); 153 if (dctx->kdf_ukm) 154 OPENSSL_free(dctx->kdf_ukm); 155 OPENSSL_free(dctx); 156 } 157 } 158 159 static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, 160 const unsigned char *tbs, size_t tbslen) 161 { 162 int ret, type; 163 unsigned int sltmp; 164 EC_PKEY_CTX *dctx = ctx->data; 165 EC_KEY *ec = ctx->pkey->pkey.ec; 166 167 if (!sig) { 168 *siglen = ECDSA_size(ec); 169 return 1; 170 } else if (*siglen < (size_t)ECDSA_size(ec)) { 171 ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL); 172 return 0; 173 } 174 175 if (dctx->md) 176 type = EVP_MD_type(dctx->md); 177 else 178 type = NID_sha1; 179 180 ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec); 181 182 if (ret <= 0) 183 return ret; 184 *siglen = (size_t)sltmp; 185 return 1; 186 } 187 188 static int pkey_ec_verify(EVP_PKEY_CTX *ctx, 189 const unsigned char *sig, size_t siglen, 190 const unsigned char *tbs, size_t tbslen) 191 { 192 int ret, type; 193 EC_PKEY_CTX *dctx = ctx->data; 194 EC_KEY *ec = ctx->pkey->pkey.ec; 195 196 if (dctx->md) 197 type = EVP_MD_type(dctx->md); 198 else 199 type = NID_sha1; 200 201 ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec); 202 203 return ret; 204 } 205 206 #ifndef OPENSSL_NO_ECDH 207 static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, 208 size_t *keylen) 209 { 210 int ret; 211 size_t outlen; 212 const EC_POINT *pubkey = NULL; 213 EC_KEY *eckey; 214 EC_PKEY_CTX *dctx = ctx->data; 215 if (!ctx->pkey || !ctx->peerkey) { 216 ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET); 217 return 0; 218 } 219 220 eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec; 221 222 if (!key) { 223 const EC_GROUP *group; 224 group = EC_KEY_get0_group(eckey); 225 *keylen = (EC_GROUP_get_degree(group) + 7) / 8; 226 return 1; 227 } 228 pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec); 229 230 /* 231 * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not 232 * an error, the result is truncated. 233 */ 234 235 outlen = *keylen; 236 237 ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0); 238 if (ret <= 0) 239 return 0; 240 *keylen = ret; 241 return 1; 242 } 243 244 static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx, 245 unsigned char *key, size_t *keylen) 246 { 247 EC_PKEY_CTX *dctx = ctx->data; 248 unsigned char *ktmp = NULL; 249 size_t ktmplen; 250 int rv = 0; 251 if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE) 252 return pkey_ec_derive(ctx, key, keylen); 253 if (!key) { 254 *keylen = dctx->kdf_outlen; 255 return 1; 256 } 257 if (*keylen != dctx->kdf_outlen) 258 return 0; 259 if (!pkey_ec_derive(ctx, NULL, &ktmplen)) 260 return 0; 261 ktmp = OPENSSL_malloc(ktmplen); 262 if (!ktmp) 263 return 0; 264 if (!pkey_ec_derive(ctx, ktmp, &ktmplen)) 265 goto err; 266 /* Do KDF stuff */ 267 if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen, 268 dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md)) 269 goto err; 270 rv = 1; 271 272 err: 273 if (ktmp) { 274 OPENSSL_cleanse(ktmp, ktmplen); 275 OPENSSL_free(ktmp); 276 } 277 return rv; 278 } 279 #endif 280 281 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) 282 { 283 EC_PKEY_CTX *dctx = ctx->data; 284 EC_GROUP *group; 285 switch (type) { 286 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID: 287 group = EC_GROUP_new_by_curve_name(p1); 288 if (group == NULL) { 289 ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE); 290 return 0; 291 } 292 if (dctx->gen_group) 293 EC_GROUP_free(dctx->gen_group); 294 dctx->gen_group = group; 295 return 1; 296 297 case EVP_PKEY_CTRL_EC_PARAM_ENC: 298 if (!dctx->gen_group) { 299 ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET); 300 return 0; 301 } 302 EC_GROUP_set_asn1_flag(dctx->gen_group, p1); 303 return 1; 304 305 #ifndef OPENSSL_NO_ECDH 306 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR: 307 if (p1 == -2) { 308 if (dctx->cofactor_mode != -1) 309 return dctx->cofactor_mode; 310 else { 311 EC_KEY *ec_key = ctx->pkey->pkey.ec; 312 return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 313 0; 314 } 315 } else if (p1 < -1 || p1 > 1) 316 return -2; 317 dctx->cofactor_mode = p1; 318 if (p1 != -1) { 319 EC_KEY *ec_key = ctx->pkey->pkey.ec; 320 if (!ec_key->group) 321 return -2; 322 /* If cofactor is 1 cofactor mode does nothing */ 323 if (BN_is_one(&ec_key->group->cofactor)) 324 return 1; 325 if (!dctx->co_key) { 326 dctx->co_key = EC_KEY_dup(ec_key); 327 if (!dctx->co_key) 328 return 0; 329 } 330 if (p1) 331 EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH); 332 else 333 EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH); 334 } else if (dctx->co_key) { 335 EC_KEY_free(dctx->co_key); 336 dctx->co_key = NULL; 337 } 338 return 1; 339 #endif 340 341 case EVP_PKEY_CTRL_EC_KDF_TYPE: 342 if (p1 == -2) 343 return dctx->kdf_type; 344 if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62) 345 return -2; 346 dctx->kdf_type = p1; 347 return 1; 348 349 case EVP_PKEY_CTRL_EC_KDF_MD: 350 dctx->kdf_md = p2; 351 return 1; 352 353 case EVP_PKEY_CTRL_GET_EC_KDF_MD: 354 *(const EVP_MD **)p2 = dctx->kdf_md; 355 return 1; 356 357 case EVP_PKEY_CTRL_EC_KDF_OUTLEN: 358 if (p1 <= 0) 359 return -2; 360 dctx->kdf_outlen = (size_t)p1; 361 return 1; 362 363 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN: 364 *(int *)p2 = dctx->kdf_outlen; 365 return 1; 366 367 case EVP_PKEY_CTRL_EC_KDF_UKM: 368 if (dctx->kdf_ukm) 369 OPENSSL_free(dctx->kdf_ukm); 370 dctx->kdf_ukm = p2; 371 if (p2) 372 dctx->kdf_ukmlen = p1; 373 else 374 dctx->kdf_ukmlen = 0; 375 return 1; 376 377 case EVP_PKEY_CTRL_GET_EC_KDF_UKM: 378 *(unsigned char **)p2 = dctx->kdf_ukm; 379 return dctx->kdf_ukmlen; 380 381 case EVP_PKEY_CTRL_MD: 382 if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && 383 EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 && 384 EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && 385 EVP_MD_type((const EVP_MD *)p2) != NID_sha256 && 386 EVP_MD_type((const EVP_MD *)p2) != NID_sha384 && 387 EVP_MD_type((const EVP_MD *)p2) != NID_sha512) { 388 ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE); 389 return 0; 390 } 391 dctx->md = p2; 392 return 1; 393 394 case EVP_PKEY_CTRL_GET_MD: 395 *(const EVP_MD **)p2 = dctx->md; 396 return 1; 397 398 case EVP_PKEY_CTRL_PEER_KEY: 399 /* Default behaviour is OK */ 400 case EVP_PKEY_CTRL_DIGESTINIT: 401 case EVP_PKEY_CTRL_PKCS7_SIGN: 402 case EVP_PKEY_CTRL_CMS_SIGN: 403 return 1; 404 405 default: 406 return -2; 407 408 } 409 } 410 411 static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx, 412 const char *type, const char *value) 413 { 414 if (!strcmp(type, "ec_paramgen_curve")) { 415 int nid; 416 nid = EC_curve_nist2nid(value); 417 if (nid == NID_undef) 418 nid = OBJ_sn2nid(value); 419 if (nid == NID_undef) 420 nid = OBJ_ln2nid(value); 421 if (nid == NID_undef) { 422 ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE); 423 return 0; 424 } 425 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid); 426 } else if (!strcmp(type, "ec_param_enc")) { 427 int param_enc; 428 if (!strcmp(value, "explicit")) 429 param_enc = 0; 430 else if (!strcmp(value, "named_curve")) 431 param_enc = OPENSSL_EC_NAMED_CURVE; 432 else 433 return -2; 434 return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc); 435 } else if (!strcmp(type, "ecdh_kdf_md")) { 436 const EVP_MD *md; 437 if (!(md = EVP_get_digestbyname(value))) { 438 ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST); 439 return 0; 440 } 441 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md); 442 } else if (!strcmp(type, "ecdh_cofactor_mode")) { 443 int co_mode; 444 co_mode = atoi(value); 445 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode); 446 } 447 448 return -2; 449 } 450 451 static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) 452 { 453 EC_KEY *ec = NULL; 454 EC_PKEY_CTX *dctx = ctx->data; 455 int ret = 0; 456 if (dctx->gen_group == NULL) { 457 ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET); 458 return 0; 459 } 460 ec = EC_KEY_new(); 461 if (!ec) 462 return 0; 463 ret = EC_KEY_set_group(ec, dctx->gen_group); 464 if (ret) 465 EVP_PKEY_assign_EC_KEY(pkey, ec); 466 else 467 EC_KEY_free(ec); 468 return ret; 469 } 470 471 static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) 472 { 473 EC_KEY *ec = NULL; 474 EC_PKEY_CTX *dctx = ctx->data; 475 if (ctx->pkey == NULL && dctx->gen_group == NULL) { 476 ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET); 477 return 0; 478 } 479 ec = EC_KEY_new(); 480 if (!ec) 481 return 0; 482 EVP_PKEY_assign_EC_KEY(pkey, ec); 483 if (ctx->pkey) { 484 /* Note: if error return, pkey is freed by parent routine */ 485 if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey)) 486 return 0; 487 } else { 488 if (!EC_KEY_set_group(ec, dctx->gen_group)) 489 return 0; 490 } 491 return EC_KEY_generate_key(pkey->pkey.ec); 492 } 493 494 const EVP_PKEY_METHOD ec_pkey_meth = { 495 EVP_PKEY_EC, 496 0, 497 pkey_ec_init, 498 pkey_ec_copy, 499 pkey_ec_cleanup, 500 501 0, 502 pkey_ec_paramgen, 503 504 0, 505 pkey_ec_keygen, 506 507 0, 508 pkey_ec_sign, 509 510 0, 511 pkey_ec_verify, 512 513 0, 0, 514 515 0, 0, 0, 0, 516 517 0, 0, 518 519 0, 0, 520 521 0, 522 #ifndef OPENSSL_NO_ECDH 523 pkey_ec_kdf_derive, 524 #else 525 0, 526 #endif 527 528 pkey_ec_ctrl, 529 pkey_ec_ctrl_str 530 }; 531