Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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