| 
									
										
										
										
											2016-05-18 02:51:04 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-03-11 21:27:36 +08:00
										 |  |  |  * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2017-06-15 22:16:46 +08:00
										 |  |  |  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved | 
					
						
							| 
									
										
										
										
											2016-05-18 02:51:04 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:38:06 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 02:51:04 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-28 13:14:18 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * ECDH low level APIs are deprecated for public use, but still ok for | 
					
						
							|  |  |  |  * internal use. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include "internal/deprecated.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-21 08:04:14 +08:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2003-02-28 23:37:10 +08:00
										 |  |  | #include <limits.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 22:56:48 +08:00
										 |  |  | #include "internal/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											2003-03-21 01:09:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  | #include <openssl/err.h>
 | 
					
						
							| 
									
										
										
										
											2004-05-18 03:14:22 +08:00
										 |  |  | #include <openssl/bn.h>
 | 
					
						
							| 
									
										
										
										
											2015-10-23 01:47:11 +08:00
										 |  |  | #include <openssl/objects.h>
 | 
					
						
							|  |  |  | #include <openssl/ec.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:40 +08:00
										 |  |  | #include "ec_local.h"
 | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 22:12:11 +08:00
										 |  |  | int ossl_ecdh_compute_key(unsigned char **psec, size_t *pseclen, | 
					
						
							|  |  |  |                           const EC_POINT *pub_key, const EC_KEY *ecdh) | 
					
						
							| 
									
										
										
										
											2016-02-29 01:47:06 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-02-29 01:48:48 +08:00
										 |  |  |     if (ecdh->group->meth->ecdh_compute_key == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH); | 
					
						
							| 
									
										
										
										
											2016-06-17 20:55:15 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2016-02-29 01:47:06 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 22:12:11 +08:00
										 |  |  |     return ecdh->group->meth->ecdh_compute_key(psec, pseclen, pub_key, ecdh); | 
					
						
							| 
									
										
										
										
											2016-02-29 01:47:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-05 08:34:00 +08:00
										 |  |  | /*-
 | 
					
						
							| 
									
										
										
										
											2020-01-23 18:17:05 +08:00
										 |  |  |  * This implementation is based on the following primitives in the | 
					
						
							|  |  |  |  * IEEE 1363 standard: | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |  *  - ECKAS-DH1 | 
					
						
							|  |  |  |  *  - ECSVDP-DH | 
					
						
							| 
									
										
										
										
											2020-01-23 18:17:05 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * It also conforms to SP800-56A r3 | 
					
						
							|  |  |  |  * See Section 5.7.1.2 "Elliptic Curve Cryptography Cofactor Diffie-Hellman | 
					
						
							|  |  |  |  * (ECC CDH) Primitive:". The steps listed below refer to SP800-56A. | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-02-18 18:27:26 +08:00
										 |  |  | int ossl_ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen, | 
					
						
							|  |  |  |                                  const EC_POINT *pub_key, const EC_KEY *ecdh) | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     BN_CTX *ctx; | 
					
						
							|  |  |  |     EC_POINT *tmp = NULL; | 
					
						
							| 
									
										
										
										
											2018-03-07 03:00:24 +08:00
										 |  |  |     BIGNUM *x = NULL; | 
					
						
							| 
									
										
										
										
											2005-05-16 18:11:04 +08:00
										 |  |  |     const BIGNUM *priv_key; | 
					
						
							|  |  |  |     const EC_GROUP *group; | 
					
						
							| 
									
										
										
										
											2016-02-29 22:12:11 +08:00
										 |  |  |     int ret = 0; | 
					
						
							| 
									
										
										
										
											2003-06-04 17:10:43 +08:00
										 |  |  |     size_t buflen, len; | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |     unsigned char *buf = NULL; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 00:30:03 +08:00
										 |  |  |     if ((ctx = BN_CTX_new_ex(ecdh->libctx)) == NULL) | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     BN_CTX_start(ctx); | 
					
						
							|  |  |  |     x = BN_CTX_get(ctx); | 
					
						
							| 
									
										
										
										
											2018-03-07 03:00:24 +08:00
										 |  |  |     if (x == NULL) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); | 
					
						
							| 
									
										
										
										
											2017-06-14 00:08:40 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-05-16 18:11:04 +08:00
										 |  |  |     priv_key = EC_KEY_get0_private_key(ecdh); | 
					
						
							|  |  |  |     if (priv_key == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY); | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-05-16 18:11:04 +08:00
										 |  |  |     group = EC_KEY_get0_group(ecdh); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-23 18:17:05 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Step(1) - Compute the point tmp = cofactor * owners_private_key | 
					
						
							|  |  |  |      *                                   * peer_public_key. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-12-10 21:35:11 +08:00
										 |  |  |     if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |         if (!EC_GROUP_get_cofactor(group, x, NULL)) { | 
					
						
							|  |  |  |             ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!BN_mul(x, x, priv_key, ctx)) { | 
					
						
							|  |  |  |             ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); | 
					
						
							| 
									
										
										
										
											2011-12-10 21:35:11 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         priv_key = x; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-05-16 18:11:04 +08:00
										 |  |  |     if ((tmp = EC_POINT_new(group)) == NULL) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-05-16 18:11:04 +08:00
										 |  |  |     if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EC, EC_R_POINT_ARITHMETIC_FAILURE); | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-23 18:17:05 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Step(2) : If point tmp is at infinity then clear intermediate values and | 
					
						
							|  |  |  |      * exit. Note: getting affine coordinates returns 0 if point is at infinity. | 
					
						
							|  |  |  |      * Step(3a) : Get x-coordinate of point x = tmp.x | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:40:18 +08:00
										 |  |  |     if (!EC_POINT_get_affine_coordinates(group, tmp, x, NULL, ctx)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EC, EC_R_POINT_ARITHMETIC_FAILURE); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:40:18 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-23 18:17:05 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Step(3b) : convert x to a byte string, using the field-element-to-byte | 
					
						
							|  |  |  |      * string conversion routine defined in Appendix C.2 | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2005-05-16 18:11:04 +08:00
										 |  |  |     buflen = (EC_GROUP_get_degree(group) + 7) / 8; | 
					
						
							| 
									
										
										
										
											2003-02-28 23:37:10 +08:00
										 |  |  |     len = BN_num_bytes(x); | 
					
						
							|  |  |  |     if (len > buflen) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); | 
					
						
							| 
									
										
										
										
											2003-02-28 23:37:10 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |     if ((buf = OPENSSL_malloc(buflen)) == NULL) | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-28 23:37:10 +08:00
										 |  |  |     memset(buf, 0, buflen - len); | 
					
						
							| 
									
										
										
										
											2003-06-20 00:56:48 +08:00
										 |  |  |     if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-29 22:12:11 +08:00
										 |  |  |     *pout = buf; | 
					
						
							|  |  |  |     *poutlen = buflen; | 
					
						
							|  |  |  |     buf = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  |  err: | 
					
						
							| 
									
										
										
										
											2020-01-23 18:17:05 +08:00
										 |  |  |     /* Step(4) : Destroy all intermediate calculations */ | 
					
						
							|  |  |  |     BN_clear(x); | 
					
						
							| 
									
										
										
										
											2019-03-17 16:48:15 +08:00
										 |  |  |     EC_POINT_clear_free(tmp); | 
					
						
							| 
									
										
										
										
											2019-03-19 07:58:09 +08:00
										 |  |  |     BN_CTX_end(ctx); | 
					
						
							| 
									
										
										
										
											2015-05-01 09:37:06 +08:00
										 |  |  |     BN_CTX_free(ctx); | 
					
						
							| 
									
										
										
										
											2015-05-01 22:02:07 +08:00
										 |  |  |     OPENSSL_free(buf); | 
					
						
							| 
									
										
										
										
											2016-02-29 22:12:11 +08:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2002-08-09 16:43:04 +08:00
										 |  |  | } |