mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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