Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curve Diffie-Hellman |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * References: |
| 24 | * |
| 25 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 26 | * RFC 4492 |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 27 | */ |
| 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 33 | #endif |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/ecdh.h" |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 38 | #include "mbedtls/platform_util.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 39 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 40 | #include <string.h> |
| 41 | |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 42 | /* Parameter validation macros based on platform_util.h */ |
| 43 | #define ECDH_VALIDATE_RET( cond ) \ |
| 44 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) |
| 45 | #define ECDH_VALIDATE( cond ) \ |
| 46 | MBEDTLS_INTERNAL_VALIDATE( cond ) |
| 47 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 48 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 49 | typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed; |
| 50 | #endif |
| 51 | |
Ron Eldor | a84c1cb | 2017-10-10 19:04:27 +0300 | [diff] [blame] | 52 | #if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 53 | /* |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 54 | * Generate public key (restartable version) |
Manuel Pégourié-Gonnard | c0edc96 | 2018-10-16 10:38:19 +0200 | [diff] [blame] | 55 | * |
| 56 | * Note: this internal function relies on its caller preserving the value of |
Manuel Pégourié-Gonnard | ca29fdf | 2018-10-22 09:56:53 +0200 | [diff] [blame] | 57 | * the output parameter 'd' across continuation calls. This would not be |
Manuel Pégourié-Gonnard | c0edc96 | 2018-10-16 10:38:19 +0200 | [diff] [blame] | 58 | * acceptable for a public function but is OK here as we control call sites. |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 59 | */ |
| 60 | static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp, |
| 61 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 62 | int (*f_rng)(void *, unsigned char *, size_t), |
| 63 | void *p_rng, |
| 64 | mbedtls_ecp_restart_ctx *rs_ctx ) |
| 65 | { |
| 66 | int ret; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 67 | ECDH_VALIDATE_RET( grp != NULL ); |
| 68 | ECDH_VALIDATE_RET( d != NULL ); |
| 69 | ECDH_VALIDATE_RET( Q != NULL ); |
| 70 | ECDH_VALIDATE_RET( f_rng != NULL ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 71 | |
| 72 | /* If multiplication is in progress, we already generated a privkey */ |
| 73 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 74 | if( rs_ctx == NULL || rs_ctx->rsm == NULL ) |
| 75 | #endif |
| 76 | MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) ); |
| 77 | |
| 78 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G, |
| 79 | f_rng, p_rng, rs_ctx ) ); |
| 80 | |
| 81 | cleanup: |
| 82 | return( ret ); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Generate public key |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 87 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 89 | int (*f_rng)(void *, unsigned char *, size_t), |
| 90 | void *p_rng ) |
| 91 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 92 | ECDH_VALIDATE_RET( grp != NULL ); |
| 93 | ECDH_VALIDATE_RET( d != NULL ); |
| 94 | ECDH_VALIDATE_RET( Q != NULL ); |
| 95 | ECDH_VALIDATE_RET( f_rng != NULL ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 96 | return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 97 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 98 | #endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */ |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 99 | |
Ron Eldor | a84c1cb | 2017-10-10 19:04:27 +0300 | [diff] [blame] | 100 | #if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 101 | /* |
| 102 | * Compute shared secret (SEC1 3.3.1) |
| 103 | */ |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 104 | static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp, |
| 105 | mbedtls_mpi *z, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 107 | int (*f_rng)(void *, unsigned char *, size_t), |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 108 | void *p_rng, |
| 109 | mbedtls_ecp_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 110 | { |
| 111 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | mbedtls_ecp_point P; |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | mbedtls_ecp_point_init( &P ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 115 | |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 116 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q, |
| 117 | f_rng, p_rng, rs_ctx ) ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 118 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | if( mbedtls_ecp_is_zero( &P ) ) |
Paul Bakker | b548d77 | 2013-07-26 14:21:34 +0200 | [diff] [blame] | 120 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Paul Bakker | b548d77 | 2013-07-26 14:21:34 +0200 | [diff] [blame] | 122 | goto cleanup; |
| 123 | } |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 126 | |
| 127 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | mbedtls_ecp_point_free( &P ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 129 | |
| 130 | return( ret ); |
| 131 | } |
| 132 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 133 | /* |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 134 | * Compute shared secret (SEC1 3.3.1) |
| 135 | */ |
| 136 | int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, |
| 137 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
| 138 | int (*f_rng)(void *, unsigned char *, size_t), |
| 139 | void *p_rng ) |
| 140 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 141 | ECDH_VALIDATE_RET( grp != NULL ); |
| 142 | ECDH_VALIDATE_RET( Q != NULL ); |
| 143 | ECDH_VALIDATE_RET( d != NULL ); |
| 144 | ECDH_VALIDATE_RET( z != NULL ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 145 | return( ecdh_compute_shared_restartable( grp, z, Q, d, |
| 146 | f_rng, p_rng, NULL ) ); |
| 147 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 148 | #endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */ |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 149 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 150 | static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx ) |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 151 | { |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 152 | mbedtls_ecp_group_init( &ctx->grp ); |
| 153 | mbedtls_mpi_init( &ctx->d ); |
| 154 | mbedtls_ecp_point_init( &ctx->Q ); |
| 155 | mbedtls_ecp_point_init( &ctx->Qp ); |
| 156 | mbedtls_mpi_init( &ctx->z ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 157 | |
| 158 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 159 | mbedtls_ecp_restart_init( &ctx->rs ); |
| 160 | #endif |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 163 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 164 | * Initialize context |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 165 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 166 | void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ) |
| 167 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 168 | ECDH_VALIDATE( ctx != NULL ); |
| 169 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 170 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 171 | ecdh_init_internal( ctx ); |
| 172 | mbedtls_ecp_point_init( &ctx->Vi ); |
| 173 | mbedtls_ecp_point_init( &ctx->Vf ); |
| 174 | mbedtls_mpi_init( &ctx->_d ); |
| 175 | #else |
| 176 | memset( ctx, 0, sizeof( mbedtls_ecdh_context ) ); |
| 177 | |
| 178 | ctx->var = MBEDTLS_ECDH_VARIANT_NONE; |
| 179 | #endif |
| 180 | ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 181 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 182 | ctx->restart_enabled = 0; |
| 183 | #endif |
| 184 | } |
| 185 | |
| 186 | static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx, |
| 187 | mbedtls_ecp_group_id grp_id ) |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 188 | { |
| 189 | int ret; |
| 190 | |
| 191 | ret = mbedtls_ecp_group_load( &ctx->grp, grp_id ); |
| 192 | if( ret != 0 ) |
| 193 | { |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 194 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 195 | } |
| 196 | |
| 197 | return( 0 ); |
| 198 | } |
| 199 | |
| 200 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 201 | * Setup context |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 202 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 203 | int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id ) |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 204 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 205 | ECDH_VALIDATE_RET( ctx != NULL ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 206 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 207 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 208 | return( ecdh_setup_internal( ctx, grp_id ) ); |
| 209 | #else |
| 210 | switch( grp_id ) |
| 211 | { |
| 212 | default: |
| 213 | ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 214 | ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0; |
| 215 | ctx->grp_id = grp_id; |
| 216 | ecdh_init_internal( &ctx->ctx.mbed_ecdh ); |
| 217 | return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) ); |
| 218 | } |
| 219 | #endif |
| 220 | } |
| 221 | |
| 222 | static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx ) |
| 223 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | mbedtls_ecp_group_free( &ctx->grp ); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 225 | mbedtls_mpi_free( &ctx->d ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | mbedtls_ecp_point_free( &ctx->Q ); |
| 227 | mbedtls_ecp_point_free( &ctx->Qp ); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 228 | mbedtls_mpi_free( &ctx->z ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 229 | |
| 230 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 231 | mbedtls_ecp_restart_free( &ctx->rs ); |
| 232 | #endif |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 235 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 236 | /* |
| 237 | * Enable restartable operations for context |
| 238 | */ |
| 239 | void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx ) |
| 240 | { |
Hanno Becker | a7634e8 | 2018-12-18 18:45:00 +0000 | [diff] [blame^] | 241 | ECDH_VALIDATE( ctx != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 242 | |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 243 | ctx->restart_enabled = 1; |
| 244 | } |
| 245 | #endif |
| 246 | |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 247 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 248 | * Free context |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 249 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 250 | void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ) |
| 251 | { |
| 252 | if( ctx == NULL ) |
| 253 | return; |
| 254 | |
| 255 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 256 | mbedtls_ecp_point_free( &ctx->Vi ); |
| 257 | mbedtls_ecp_point_free( &ctx->Vf ); |
| 258 | mbedtls_mpi_free( &ctx->_d ); |
| 259 | ecdh_free_internal( ctx ); |
| 260 | #else |
| 261 | switch( ctx->var ) |
| 262 | { |
| 263 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 264 | ecdh_free_internal( &ctx->ctx.mbed_ecdh ); |
| 265 | break; |
| 266 | default: |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 271 | ctx->var = MBEDTLS_ECDH_VARIANT_NONE; |
| 272 | ctx->grp_id = MBEDTLS_ECP_DP_NONE; |
| 273 | #endif |
| 274 | } |
| 275 | |
| 276 | static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx, |
| 277 | size_t *olen, int point_format, |
| 278 | unsigned char *buf, size_t blen, |
| 279 | int (*f_rng)(void *, |
| 280 | unsigned char *, |
| 281 | size_t), |
| 282 | void *p_rng, |
| 283 | int restart_enabled ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 284 | { |
| 285 | int ret; |
| 286 | size_t grp_len, pt_len; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 287 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 288 | mbedtls_ecp_restart_ctx *rs_ctx = NULL; |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 289 | #endif |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 290 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 291 | if( ctx->grp.pbits == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 293 | |
Ron Eldor | 19779c4 | 2018-11-05 16:58:13 +0200 | [diff] [blame] | 294 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 295 | if( restart_enabled ) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 296 | rs_ctx = &ctx->rs; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 297 | #else |
| 298 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 299 | #endif |
| 300 | |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 301 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 302 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 303 | if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q, |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 304 | f_rng, p_rng, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 305 | return( ret ); |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 306 | #else |
| 307 | if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, |
| 308 | f_rng, p_rng ) ) != 0 ) |
| 309 | return( ret ); |
| 310 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 311 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 312 | if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, |
| 313 | blen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 314 | return( ret ); |
| 315 | |
| 316 | buf += grp_len; |
| 317 | blen -= grp_len; |
| 318 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 319 | if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 320 | &pt_len, buf, blen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 321 | return( ret ); |
| 322 | |
| 323 | *olen = grp_len + pt_len; |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 324 | return( 0 ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 327 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 328 | * Setup and write the ServerKeyExhange parameters (RFC 4492) |
| 329 | * struct { |
| 330 | * ECParameters curve_params; |
| 331 | * ECPoint public; |
| 332 | * } ServerECDHParams; |
| 333 | */ |
| 334 | int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, |
| 335 | unsigned char *buf, size_t blen, |
| 336 | int (*f_rng)(void *, unsigned char *, size_t), |
| 337 | void *p_rng ) |
| 338 | { |
| 339 | int restart_enabled = 0; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 340 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 341 | ECDH_VALIDATE_RET( olen != NULL ); |
| 342 | ECDH_VALIDATE_RET( buf != NULL ); |
| 343 | ECDH_VALIDATE_RET( f_rng != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 344 | |
| 345 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 346 | restart_enabled = ctx->restart_enabled; |
| 347 | #else |
| 348 | (void) restart_enabled; |
| 349 | #endif |
| 350 | |
| 351 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 352 | return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen, |
| 353 | f_rng, p_rng, restart_enabled ) ); |
| 354 | #else |
| 355 | switch( ctx->var ) |
| 356 | { |
| 357 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 358 | return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen, |
| 359 | ctx->point_format, buf, blen, |
| 360 | f_rng, p_rng, |
| 361 | restart_enabled ) ); |
| 362 | default: |
| 363 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 364 | } |
| 365 | #endif |
| 366 | } |
| 367 | |
| 368 | static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx, |
| 369 | const unsigned char **buf, |
| 370 | const unsigned char *end ) |
| 371 | { |
| 372 | return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, |
| 373 | end - *buf ) ); |
| 374 | } |
| 375 | |
| 376 | /* |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 377 | * Read the ServerKeyExhange parameters (RFC 4492) |
| 378 | * struct { |
| 379 | * ECParameters curve_params; |
| 380 | * ECPoint public; |
| 381 | * } ServerECDHParams; |
| 382 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 383 | int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 384 | const unsigned char **buf, |
| 385 | const unsigned char *end ) |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 386 | { |
| 387 | int ret; |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 388 | mbedtls_ecp_group_id grp_id; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 389 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 390 | ECDH_VALIDATE_RET( buf != NULL ); |
| 391 | ECDH_VALIDATE_RET( *buf != NULL ); |
| 392 | ECDH_VALIDATE_RET( end != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 393 | |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 394 | if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) ) |
| 395 | != 0 ) |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 396 | return( ret ); |
| 397 | |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 398 | if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 ) |
| 399 | return( ret ); |
| 400 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 401 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 402 | return( ecdh_read_params_internal( ctx, buf, end ) ); |
| 403 | #else |
| 404 | switch( ctx->var ) |
| 405 | { |
| 406 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 407 | return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh, |
| 408 | buf, end ) ); |
| 409 | default: |
| 410 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 411 | } |
| 412 | #endif |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 413 | } |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 414 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 415 | static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx, |
| 416 | const mbedtls_ecp_keypair *key, |
| 417 | mbedtls_ecdh_side side ) |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 418 | { |
| 419 | int ret; |
| 420 | |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 421 | /* If it's not our key, just import the public part as Qp */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 422 | if( side == MBEDTLS_ECDH_THEIRS ) |
| 423 | return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) ); |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 424 | |
| 425 | /* Our key: import public (as Q) and private parts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | if( side != MBEDTLS_ECDH_OURS ) |
| 427 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 428 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 429 | if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 || |
| 430 | ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ) |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 431 | return( ret ); |
| 432 | |
| 433 | return( 0 ); |
| 434 | } |
| 435 | |
| 436 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 437 | * Get parameters from a keypair |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 438 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 439 | int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, |
| 440 | const mbedtls_ecp_keypair *key, |
| 441 | mbedtls_ecdh_side side ) |
| 442 | { |
| 443 | int ret; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 444 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 445 | ECDH_VALIDATE_RET( key != NULL ); |
| 446 | ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS || |
| 447 | side == MBEDTLS_ECDH_THEIRS ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 448 | |
| 449 | if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 ) |
| 450 | return( ret ); |
| 451 | |
| 452 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 453 | return( ecdh_get_params_internal( ctx, key, side ) ); |
| 454 | #else |
| 455 | switch( ctx->var ) |
| 456 | { |
| 457 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 458 | return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh, |
| 459 | key, side ) ); |
| 460 | default: |
| 461 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 462 | } |
| 463 | #endif |
| 464 | } |
| 465 | |
| 466 | static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx, |
| 467 | size_t *olen, int point_format, |
| 468 | unsigned char *buf, size_t blen, |
| 469 | int (*f_rng)(void *, |
| 470 | unsigned char *, |
| 471 | size_t), |
| 472 | void *p_rng, |
| 473 | int restart_enabled ) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 474 | { |
| 475 | int ret; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 476 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 477 | mbedtls_ecp_restart_ctx *rs_ctx = NULL; |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 478 | #endif |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 479 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 480 | if( ctx->grp.pbits == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 481 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 482 | |
Ron Eldor | b430d9f | 2018-11-05 17:18:29 +0200 | [diff] [blame] | 483 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 484 | if( restart_enabled ) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 485 | rs_ctx = &ctx->rs; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 486 | #else |
| 487 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 488 | #endif |
| 489 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 490 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 491 | if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q, |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 492 | f_rng, p_rng, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 493 | return( ret ); |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 494 | #else |
| 495 | if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, |
| 496 | f_rng, p_rng ) ) != 0 ) |
| 497 | return( ret ); |
| 498 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 499 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 500 | return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen, |
| 501 | buf, blen ); |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 505 | * Setup and export the client public value |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 506 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 507 | int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, |
| 508 | unsigned char *buf, size_t blen, |
| 509 | int (*f_rng)(void *, unsigned char *, size_t), |
| 510 | void *p_rng ) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 511 | { |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 512 | int restart_enabled = 0; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 513 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 514 | ECDH_VALIDATE_RET( olen != NULL ); |
| 515 | ECDH_VALIDATE_RET( buf != NULL ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 516 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 517 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 518 | restart_enabled = ctx->restart_enabled; |
| 519 | #endif |
| 520 | |
| 521 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 522 | return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen, |
| 523 | f_rng, p_rng, restart_enabled ) ); |
| 524 | #else |
| 525 | switch( ctx->var ) |
| 526 | { |
| 527 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 528 | return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen, |
| 529 | ctx->point_format, buf, blen, |
| 530 | f_rng, p_rng, |
| 531 | restart_enabled ) ); |
| 532 | default: |
| 533 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 534 | } |
| 535 | #endif |
| 536 | } |
| 537 | |
| 538 | static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx, |
| 539 | const unsigned char *buf, size_t blen ) |
| 540 | { |
| 541 | int ret; |
| 542 | const unsigned char *p = buf; |
| 543 | |
| 544 | if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, |
| 545 | blen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 546 | return( ret ); |
| 547 | |
| 548 | if( (size_t)( p - buf ) != blen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 549 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 550 | |
| 551 | return( 0 ); |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 554 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 555 | * Parse and import the client's public value |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 556 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 557 | int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, |
| 558 | const unsigned char *buf, size_t blen ) |
| 559 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 560 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 561 | ECDH_VALIDATE_RET( buf != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 562 | |
| 563 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 564 | return( ecdh_read_public_internal( ctx, buf, blen ) ); |
| 565 | #else |
| 566 | switch( ctx->var ) |
| 567 | { |
| 568 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 569 | return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh, |
| 570 | buf, blen ) ); |
| 571 | default: |
| 572 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 573 | } |
| 574 | #endif |
| 575 | } |
| 576 | |
| 577 | static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx, |
| 578 | size_t *olen, unsigned char *buf, |
| 579 | size_t blen, |
| 580 | int (*f_rng)(void *, |
| 581 | unsigned char *, |
| 582 | size_t), |
| 583 | void *p_rng, |
| 584 | int restart_enabled ) |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 585 | { |
| 586 | int ret; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 587 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 588 | mbedtls_ecp_restart_ctx *rs_ctx = NULL; |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 589 | #endif |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 590 | |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 591 | if( ctx == NULL || ctx->grp.pbits == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 593 | |
Ron Eldor | b430d9f | 2018-11-05 17:18:29 +0200 | [diff] [blame] | 594 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 595 | if( restart_enabled ) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 596 | rs_ctx = &ctx->rs; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 597 | #else |
| 598 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 599 | #endif |
| 600 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 601 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 602 | if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp, |
| 603 | &ctx->d, f_rng, p_rng, |
| 604 | rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 605 | { |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 606 | return( ret ); |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 607 | } |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 608 | #else |
| 609 | if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, |
| 610 | &ctx->d, f_rng, p_rng ) ) != 0 ) |
| 611 | { |
| 612 | return( ret ); |
| 613 | } |
| 614 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 616 | if( mbedtls_mpi_size( &ctx->z ) > blen ) |
| 617 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 618 | |
Manuel Pégourié-Gonnard | 0a56c2c | 2014-01-17 21:24:04 +0100 | [diff] [blame] | 619 | *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 620 | return mbedtls_mpi_write_binary( &ctx->z, buf, *olen ); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 621 | } |
| 622 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 623 | /* |
| 624 | * Derive and export the shared secret |
| 625 | */ |
| 626 | int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, |
| 627 | unsigned char *buf, size_t blen, |
| 628 | int (*f_rng)(void *, unsigned char *, size_t), |
| 629 | void *p_rng ) |
| 630 | { |
| 631 | int restart_enabled = 0; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 632 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 633 | ECDH_VALIDATE_RET( olen != NULL ); |
| 634 | ECDH_VALIDATE_RET( buf != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 635 | |
| 636 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 637 | restart_enabled = ctx->restart_enabled; |
| 638 | #endif |
| 639 | |
| 640 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 641 | return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng, |
| 642 | restart_enabled ) ); |
| 643 | #else |
| 644 | switch( ctx->var ) |
| 645 | { |
| 646 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 647 | return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf, |
| 648 | blen, f_rng, p_rng, |
| 649 | restart_enabled ) ); |
| 650 | default: |
| 651 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 652 | } |
| 653 | #endif |
| 654 | } |
| 655 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | #endif /* MBEDTLS_ECDH_C */ |