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