mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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