Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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