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