mbed TLS Build

Dependents:   Encrypt_Decrypt1 mbed_blink_tls encrypt encrypt

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

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