Pinned to some recent date
features/mbedtls/src/ecdh.c@0:fb7af294d5d9, 2016-11-17 (annotated)
- Committer:
- Simon Cooksey
- Date:
- Thu Nov 17 16:43:53 2016 +0000
- Revision:
- 0:fb7af294d5d9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Simon Cooksey |
0:fb7af294d5d9 | 1 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 2 | * Elliptic curve Diffie-Hellman |
Simon Cooksey |
0:fb7af294d5d9 | 3 | * |
Simon Cooksey |
0:fb7af294d5d9 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Simon Cooksey |
0:fb7af294d5d9 | 5 | * SPDX-License-Identifier: Apache-2.0 |
Simon Cooksey |
0:fb7af294d5d9 | 6 | * |
Simon Cooksey |
0:fb7af294d5d9 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Simon Cooksey |
0:fb7af294d5d9 | 8 | * not use this file except in compliance with the License. |
Simon Cooksey |
0:fb7af294d5d9 | 9 | * You may obtain a copy of the License at |
Simon Cooksey |
0:fb7af294d5d9 | 10 | * |
Simon Cooksey |
0:fb7af294d5d9 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
Simon Cooksey |
0:fb7af294d5d9 | 12 | * |
Simon Cooksey |
0:fb7af294d5d9 | 13 | * Unless required by applicable law or agreed to in writing, software |
Simon Cooksey |
0:fb7af294d5d9 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Simon Cooksey |
0:fb7af294d5d9 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Simon Cooksey |
0:fb7af294d5d9 | 16 | * See the License for the specific language governing permissions and |
Simon Cooksey |
0:fb7af294d5d9 | 17 | * limitations under the License. |
Simon Cooksey |
0:fb7af294d5d9 | 18 | * |
Simon Cooksey |
0:fb7af294d5d9 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Simon Cooksey |
0:fb7af294d5d9 | 20 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 21 | |
Simon Cooksey |
0:fb7af294d5d9 | 22 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 23 | * References: |
Simon Cooksey |
0:fb7af294d5d9 | 24 | * |
Simon Cooksey |
0:fb7af294d5d9 | 25 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
Simon Cooksey |
0:fb7af294d5d9 | 26 | * RFC 4492 |
Simon Cooksey |
0:fb7af294d5d9 | 27 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 28 | |
Simon Cooksey |
0:fb7af294d5d9 | 29 | #if !defined(MBEDTLS_CONFIG_FILE) |
Simon Cooksey |
0:fb7af294d5d9 | 30 | #include "mbedtls/config.h" |
Simon Cooksey |
0:fb7af294d5d9 | 31 | #else |
Simon Cooksey |
0:fb7af294d5d9 | 32 | #include MBEDTLS_CONFIG_FILE |
Simon Cooksey |
0:fb7af294d5d9 | 33 | #endif |
Simon Cooksey |
0:fb7af294d5d9 | 34 | |
Simon Cooksey |
0:fb7af294d5d9 | 35 | #if defined(MBEDTLS_ECDH_C) |
Simon Cooksey |
0:fb7af294d5d9 | 36 | |
Simon Cooksey |
0:fb7af294d5d9 | 37 | #include "mbedtls/ecdh.h" |
Simon Cooksey |
0:fb7af294d5d9 | 38 | |
Simon Cooksey |
0:fb7af294d5d9 | 39 | #include <string.h> |
Simon Cooksey |
0:fb7af294d5d9 | 40 | |
Simon Cooksey |
0:fb7af294d5d9 | 41 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 42 | * Generate public key: simple wrapper around mbedtls_ecp_gen_keypair |
Simon Cooksey |
0:fb7af294d5d9 | 43 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 44 | int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
Simon Cooksey |
0:fb7af294d5d9 | 45 | int (*f_rng)(void *, unsigned char *, size_t), |
Simon Cooksey |
0:fb7af294d5d9 | 46 | void *p_rng ) |
Simon Cooksey |
0:fb7af294d5d9 | 47 | { |
Simon Cooksey |
0:fb7af294d5d9 | 48 | return mbedtls_ecp_gen_keypair( grp, d, Q, f_rng, p_rng ); |
Simon Cooksey |
0:fb7af294d5d9 | 49 | } |
Simon Cooksey |
0:fb7af294d5d9 | 50 | |
Simon Cooksey |
0:fb7af294d5d9 | 51 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 52 | * Compute shared secret (SEC1 3.3.1) |
Simon Cooksey |
0:fb7af294d5d9 | 53 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 54 | int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, |
Simon Cooksey |
0:fb7af294d5d9 | 55 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
Simon Cooksey |
0:fb7af294d5d9 | 56 | int (*f_rng)(void *, unsigned char *, size_t), |
Simon Cooksey |
0:fb7af294d5d9 | 57 | void *p_rng ) |
Simon Cooksey |
0:fb7af294d5d9 | 58 | { |
Simon Cooksey |
0:fb7af294d5d9 | 59 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 60 | mbedtls_ecp_point P; |
Simon Cooksey |
0:fb7af294d5d9 | 61 | |
Simon Cooksey |
0:fb7af294d5d9 | 62 | mbedtls_ecp_point_init( &P ); |
Simon Cooksey |
0:fb7af294d5d9 | 63 | |
Simon Cooksey |
0:fb7af294d5d9 | 64 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 65 | * Make sure Q is a valid pubkey before using it |
Simon Cooksey |
0:fb7af294d5d9 | 66 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 67 | MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) ); |
Simon Cooksey |
0:fb7af294d5d9 | 68 | |
Simon Cooksey |
0:fb7af294d5d9 | 69 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &P, d, Q, f_rng, p_rng ) ); |
Simon Cooksey |
0:fb7af294d5d9 | 70 | |
Simon Cooksey |
0:fb7af294d5d9 | 71 | if( mbedtls_ecp_is_zero( &P ) ) |
Simon Cooksey |
0:fb7af294d5d9 | 72 | { |
Simon Cooksey |
0:fb7af294d5d9 | 73 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Simon Cooksey |
0:fb7af294d5d9 | 74 | goto cleanup; |
Simon Cooksey |
0:fb7af294d5d9 | 75 | } |
Simon Cooksey |
0:fb7af294d5d9 | 76 | |
Simon Cooksey |
0:fb7af294d5d9 | 77 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) ); |
Simon Cooksey |
0:fb7af294d5d9 | 78 | |
Simon Cooksey |
0:fb7af294d5d9 | 79 | cleanup: |
Simon Cooksey |
0:fb7af294d5d9 | 80 | mbedtls_ecp_point_free( &P ); |
Simon Cooksey |
0:fb7af294d5d9 | 81 | |
Simon Cooksey |
0:fb7af294d5d9 | 82 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 83 | } |
Simon Cooksey |
0:fb7af294d5d9 | 84 | |
Simon Cooksey |
0:fb7af294d5d9 | 85 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 86 | * Initialize context |
Simon Cooksey |
0:fb7af294d5d9 | 87 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 88 | void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ) |
Simon Cooksey |
0:fb7af294d5d9 | 89 | { |
Simon Cooksey |
0:fb7af294d5d9 | 90 | memset( ctx, 0, sizeof( mbedtls_ecdh_context ) ); |
Simon Cooksey |
0:fb7af294d5d9 | 91 | } |
Simon Cooksey |
0:fb7af294d5d9 | 92 | |
Simon Cooksey |
0:fb7af294d5d9 | 93 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 94 | * Free context |
Simon Cooksey |
0:fb7af294d5d9 | 95 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 96 | void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ) |
Simon Cooksey |
0:fb7af294d5d9 | 97 | { |
Simon Cooksey |
0:fb7af294d5d9 | 98 | if( ctx == NULL ) |
Simon Cooksey |
0:fb7af294d5d9 | 99 | return; |
Simon Cooksey |
0:fb7af294d5d9 | 100 | |
Simon Cooksey |
0:fb7af294d5d9 | 101 | mbedtls_ecp_group_free( &ctx->grp ); |
Simon Cooksey |
0:fb7af294d5d9 | 102 | mbedtls_ecp_point_free( &ctx->Q ); |
Simon Cooksey |
0:fb7af294d5d9 | 103 | mbedtls_ecp_point_free( &ctx->Qp ); |
Simon Cooksey |
0:fb7af294d5d9 | 104 | mbedtls_ecp_point_free( &ctx->Vi ); |
Simon Cooksey |
0:fb7af294d5d9 | 105 | mbedtls_ecp_point_free( &ctx->Vf ); |
Simon Cooksey |
0:fb7af294d5d9 | 106 | mbedtls_mpi_free( &ctx->d ); |
Simon Cooksey |
0:fb7af294d5d9 | 107 | mbedtls_mpi_free( &ctx->z ); |
Simon Cooksey |
0:fb7af294d5d9 | 108 | mbedtls_mpi_free( &ctx->_d ); |
Simon Cooksey |
0:fb7af294d5d9 | 109 | } |
Simon Cooksey |
0:fb7af294d5d9 | 110 | |
Simon Cooksey |
0:fb7af294d5d9 | 111 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 112 | * Setup and write the ServerKeyExhange parameters (RFC 4492) |
Simon Cooksey |
0:fb7af294d5d9 | 113 | * struct { |
Simon Cooksey |
0:fb7af294d5d9 | 114 | * ECParameters curve_params; |
Simon Cooksey |
0:fb7af294d5d9 | 115 | * ECPoint public; |
Simon Cooksey |
0:fb7af294d5d9 | 116 | * } ServerECDHParams; |
Simon Cooksey |
0:fb7af294d5d9 | 117 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 118 | int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, |
Simon Cooksey |
0:fb7af294d5d9 | 119 | unsigned char *buf, size_t blen, |
Simon Cooksey |
0:fb7af294d5d9 | 120 | int (*f_rng)(void *, unsigned char *, size_t), |
Simon Cooksey |
0:fb7af294d5d9 | 121 | void *p_rng ) |
Simon Cooksey |
0:fb7af294d5d9 | 122 | { |
Simon Cooksey |
0:fb7af294d5d9 | 123 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 124 | size_t grp_len, pt_len; |
Simon Cooksey |
0:fb7af294d5d9 | 125 | |
Simon Cooksey |
0:fb7af294d5d9 | 126 | if( ctx == NULL || ctx->grp.pbits == 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 127 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Simon Cooksey |
0:fb7af294d5d9 | 128 | |
Simon Cooksey |
0:fb7af294d5d9 | 129 | if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ) |
Simon Cooksey |
0:fb7af294d5d9 | 130 | != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 131 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 132 | |
Simon Cooksey |
0:fb7af294d5d9 | 133 | if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) ) |
Simon Cooksey |
0:fb7af294d5d9 | 134 | != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 135 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 136 | |
Simon Cooksey |
0:fb7af294d5d9 | 137 | buf += grp_len; |
Simon Cooksey |
0:fb7af294d5d9 | 138 | blen -= grp_len; |
Simon Cooksey |
0:fb7af294d5d9 | 139 | |
Simon Cooksey |
0:fb7af294d5d9 | 140 | if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format, |
Simon Cooksey |
0:fb7af294d5d9 | 141 | &pt_len, buf, blen ) ) != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 142 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 143 | |
Simon Cooksey |
0:fb7af294d5d9 | 144 | *olen = grp_len + pt_len; |
Simon Cooksey |
0:fb7af294d5d9 | 145 | return( 0 ); |
Simon Cooksey |
0:fb7af294d5d9 | 146 | } |
Simon Cooksey |
0:fb7af294d5d9 | 147 | |
Simon Cooksey |
0:fb7af294d5d9 | 148 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 149 | * Read the ServerKeyExhange parameters (RFC 4492) |
Simon Cooksey |
0:fb7af294d5d9 | 150 | * struct { |
Simon Cooksey |
0:fb7af294d5d9 | 151 | * ECParameters curve_params; |
Simon Cooksey |
0:fb7af294d5d9 | 152 | * ECPoint public; |
Simon Cooksey |
0:fb7af294d5d9 | 153 | * } ServerECDHParams; |
Simon Cooksey |
0:fb7af294d5d9 | 154 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 155 | int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, |
Simon Cooksey |
0:fb7af294d5d9 | 156 | const unsigned char **buf, const unsigned char *end ) |
Simon Cooksey |
0:fb7af294d5d9 | 157 | { |
Simon Cooksey |
0:fb7af294d5d9 | 158 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 159 | |
Simon Cooksey |
0:fb7af294d5d9 | 160 | if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 161 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 162 | |
Simon Cooksey |
0:fb7af294d5d9 | 163 | if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) ) |
Simon Cooksey |
0:fb7af294d5d9 | 164 | != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 165 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 166 | |
Simon Cooksey |
0:fb7af294d5d9 | 167 | return( 0 ); |
Simon Cooksey |
0:fb7af294d5d9 | 168 | } |
Simon Cooksey |
0:fb7af294d5d9 | 169 | |
Simon Cooksey |
0:fb7af294d5d9 | 170 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 171 | * Get parameters from a keypair |
Simon Cooksey |
0:fb7af294d5d9 | 172 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 173 | int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, |
Simon Cooksey |
0:fb7af294d5d9 | 174 | mbedtls_ecdh_side side ) |
Simon Cooksey |
0:fb7af294d5d9 | 175 | { |
Simon Cooksey |
0:fb7af294d5d9 | 176 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 177 | |
Simon Cooksey |
0:fb7af294d5d9 | 178 | if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 179 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 180 | |
Simon Cooksey |
0:fb7af294d5d9 | 181 | /* If it's not our key, just import the public part as Qp */ |
Simon Cooksey |
0:fb7af294d5d9 | 182 | if( side == MBEDTLS_ECDH_THEIRS ) |
Simon Cooksey |
0:fb7af294d5d9 | 183 | return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) ); |
Simon Cooksey |
0:fb7af294d5d9 | 184 | |
Simon Cooksey |
0:fb7af294d5d9 | 185 | /* Our key: import public (as Q) and private parts */ |
Simon Cooksey |
0:fb7af294d5d9 | 186 | if( side != MBEDTLS_ECDH_OURS ) |
Simon Cooksey |
0:fb7af294d5d9 | 187 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Simon Cooksey |
0:fb7af294d5d9 | 188 | |
Simon Cooksey |
0:fb7af294d5d9 | 189 | if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 || |
Simon Cooksey |
0:fb7af294d5d9 | 190 | ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 191 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 192 | |
Simon Cooksey |
0:fb7af294d5d9 | 193 | return( 0 ); |
Simon Cooksey |
0:fb7af294d5d9 | 194 | } |
Simon Cooksey |
0:fb7af294d5d9 | 195 | |
Simon Cooksey |
0:fb7af294d5d9 | 196 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 197 | * Setup and export the client public value |
Simon Cooksey |
0:fb7af294d5d9 | 198 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 199 | int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, |
Simon Cooksey |
0:fb7af294d5d9 | 200 | unsigned char *buf, size_t blen, |
Simon Cooksey |
0:fb7af294d5d9 | 201 | int (*f_rng)(void *, unsigned char *, size_t), |
Simon Cooksey |
0:fb7af294d5d9 | 202 | void *p_rng ) |
Simon Cooksey |
0:fb7af294d5d9 | 203 | { |
Simon Cooksey |
0:fb7af294d5d9 | 204 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 205 | |
Simon Cooksey |
0:fb7af294d5d9 | 206 | if( ctx == NULL || ctx->grp.pbits == 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 207 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Simon Cooksey |
0:fb7af294d5d9 | 208 | |
Simon Cooksey |
0:fb7af294d5d9 | 209 | if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ) |
Simon Cooksey |
0:fb7af294d5d9 | 210 | != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 211 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 212 | |
Simon Cooksey |
0:fb7af294d5d9 | 213 | return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format, |
Simon Cooksey |
0:fb7af294d5d9 | 214 | olen, buf, blen ); |
Simon Cooksey |
0:fb7af294d5d9 | 215 | } |
Simon Cooksey |
0:fb7af294d5d9 | 216 | |
Simon Cooksey |
0:fb7af294d5d9 | 217 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 218 | * Parse and import the client's public value |
Simon Cooksey |
0:fb7af294d5d9 | 219 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 220 | int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, |
Simon Cooksey |
0:fb7af294d5d9 | 221 | const unsigned char *buf, size_t blen ) |
Simon Cooksey |
0:fb7af294d5d9 | 222 | { |
Simon Cooksey |
0:fb7af294d5d9 | 223 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 224 | const unsigned char *p = buf; |
Simon Cooksey |
0:fb7af294d5d9 | 225 | |
Simon Cooksey |
0:fb7af294d5d9 | 226 | if( ctx == NULL ) |
Simon Cooksey |
0:fb7af294d5d9 | 227 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Simon Cooksey |
0:fb7af294d5d9 | 228 | |
Simon Cooksey |
0:fb7af294d5d9 | 229 | if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 230 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 231 | |
Simon Cooksey |
0:fb7af294d5d9 | 232 | if( (size_t)( p - buf ) != blen ) |
Simon Cooksey |
0:fb7af294d5d9 | 233 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Simon Cooksey |
0:fb7af294d5d9 | 234 | |
Simon Cooksey |
0:fb7af294d5d9 | 235 | return( 0 ); |
Simon Cooksey |
0:fb7af294d5d9 | 236 | } |
Simon Cooksey |
0:fb7af294d5d9 | 237 | |
Simon Cooksey |
0:fb7af294d5d9 | 238 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 239 | * Derive and export the shared secret |
Simon Cooksey |
0:fb7af294d5d9 | 240 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 241 | int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, |
Simon Cooksey |
0:fb7af294d5d9 | 242 | unsigned char *buf, size_t blen, |
Simon Cooksey |
0:fb7af294d5d9 | 243 | int (*f_rng)(void *, unsigned char *, size_t), |
Simon Cooksey |
0:fb7af294d5d9 | 244 | void *p_rng ) |
Simon Cooksey |
0:fb7af294d5d9 | 245 | { |
Simon Cooksey |
0:fb7af294d5d9 | 246 | int ret; |
Simon Cooksey |
0:fb7af294d5d9 | 247 | |
Simon Cooksey |
0:fb7af294d5d9 | 248 | if( ctx == NULL ) |
Simon Cooksey |
0:fb7af294d5d9 | 249 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Simon Cooksey |
0:fb7af294d5d9 | 250 | |
Simon Cooksey |
0:fb7af294d5d9 | 251 | if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d, |
Simon Cooksey |
0:fb7af294d5d9 | 252 | f_rng, p_rng ) ) != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 253 | { |
Simon Cooksey |
0:fb7af294d5d9 | 254 | return( ret ); |
Simon Cooksey |
0:fb7af294d5d9 | 255 | } |
Simon Cooksey |
0:fb7af294d5d9 | 256 | |
Simon Cooksey |
0:fb7af294d5d9 | 257 | if( mbedtls_mpi_size( &ctx->z ) > blen ) |
Simon Cooksey |
0:fb7af294d5d9 | 258 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Simon Cooksey |
0:fb7af294d5d9 | 259 | |
Simon Cooksey |
0:fb7af294d5d9 | 260 | *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 ); |
Simon Cooksey |
0:fb7af294d5d9 | 261 | return mbedtls_mpi_write_binary( &ctx->z, buf, *olen ); |
Simon Cooksey |
0:fb7af294d5d9 | 262 | } |
Simon Cooksey |
0:fb7af294d5d9 | 263 | |
Simon Cooksey |
0:fb7af294d5d9 | 264 | #endif /* MBEDTLS_ECDH_C */ |