This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

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