nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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