I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X

Dependencies:   WncControllerK64F

Committer:
JMF
Date:
Mon Nov 14 21:15:42 2016 +0000
Revision:
16:8a4105d407d3
Parent:
12:0071cb144c7a
updated to ensure it builds with TLS by correcting config defaults.

Who changed what in which revision?

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