Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /*
switches 0:5c4d7b2438d3 2 * DTLS cookie callbacks implementation
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
switches 0:5c4d7b2438d3 5 * SPDX-License-Identifier: Apache-2.0
switches 0:5c4d7b2438d3 6 *
switches 0:5c4d7b2438d3 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
switches 0:5c4d7b2438d3 8 * not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 9 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 10 *
switches 0:5c4d7b2438d3 11 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 12 *
switches 0:5c4d7b2438d3 13 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
switches 0:5c4d7b2438d3 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 16 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 17 * limitations under the License.
switches 0:5c4d7b2438d3 18 *
switches 0:5c4d7b2438d3 19 * This file is part of mbed TLS (https://tls.mbed.org)
switches 0:5c4d7b2438d3 20 */
switches 0:5c4d7b2438d3 21 /*
switches 0:5c4d7b2438d3 22 * These session callbacks use a simple chained list
switches 0:5c4d7b2438d3 23 * to store and retrieve the session information.
switches 0:5c4d7b2438d3 24 */
switches 0:5c4d7b2438d3 25
switches 0:5c4d7b2438d3 26 #if !defined(MBEDTLS_CONFIG_FILE)
switches 0:5c4d7b2438d3 27 #include "mbedtls/config.h"
switches 0:5c4d7b2438d3 28 #else
switches 0:5c4d7b2438d3 29 #include MBEDTLS_CONFIG_FILE
switches 0:5c4d7b2438d3 30 #endif
switches 0:5c4d7b2438d3 31
switches 0:5c4d7b2438d3 32 #if defined(MBEDTLS_SSL_COOKIE_C)
switches 0:5c4d7b2438d3 33
switches 0:5c4d7b2438d3 34 #if defined(MBEDTLS_PLATFORM_C)
switches 0:5c4d7b2438d3 35 #include "mbedtls/platform.h"
switches 0:5c4d7b2438d3 36 #else
switches 0:5c4d7b2438d3 37 #define mbedtls_calloc calloc
switches 0:5c4d7b2438d3 38 #define mbedtls_free free
switches 0:5c4d7b2438d3 39 #endif
switches 0:5c4d7b2438d3 40
switches 0:5c4d7b2438d3 41 #include "mbedtls/ssl_cookie.h"
switches 0:5c4d7b2438d3 42 #include "mbedtls/ssl_internal.h"
switches 0:5c4d7b2438d3 43
switches 0:5c4d7b2438d3 44 #include <string.h>
switches 0:5c4d7b2438d3 45
switches 0:5c4d7b2438d3 46 /* Implementation that should never be optimized out by the compiler */
switches 0:5c4d7b2438d3 47 static void mbedtls_zeroize( void *v, size_t n ) {
switches 0:5c4d7b2438d3 48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
switches 0:5c4d7b2438d3 49 }
switches 0:5c4d7b2438d3 50
switches 0:5c4d7b2438d3 51 /*
switches 0:5c4d7b2438d3 52 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
switches 0:5c4d7b2438d3 53 * available. Try SHA-256 first, 512 wastes resources since we need to stay
switches 0:5c4d7b2438d3 54 * with max 32 bytes of cookie for DTLS 1.0
switches 0:5c4d7b2438d3 55 */
switches 0:5c4d7b2438d3 56 #if defined(MBEDTLS_SHA256_C)
switches 0:5c4d7b2438d3 57 #define COOKIE_MD MBEDTLS_MD_SHA224
switches 0:5c4d7b2438d3 58 #define COOKIE_MD_OUTLEN 32
switches 0:5c4d7b2438d3 59 #define COOKIE_HMAC_LEN 28
switches 0:5c4d7b2438d3 60 #elif defined(MBEDTLS_SHA512_C)
switches 0:5c4d7b2438d3 61 #define COOKIE_MD MBEDTLS_MD_SHA384
switches 0:5c4d7b2438d3 62 #define COOKIE_MD_OUTLEN 48
switches 0:5c4d7b2438d3 63 #define COOKIE_HMAC_LEN 28
switches 0:5c4d7b2438d3 64 #elif defined(MBEDTLS_SHA1_C)
switches 0:5c4d7b2438d3 65 #define COOKIE_MD MBEDTLS_MD_SHA1
switches 0:5c4d7b2438d3 66 #define COOKIE_MD_OUTLEN 20
switches 0:5c4d7b2438d3 67 #define COOKIE_HMAC_LEN 20
switches 0:5c4d7b2438d3 68 #else
switches 0:5c4d7b2438d3 69 #error "DTLS hello verify needs SHA-1 or SHA-2"
switches 0:5c4d7b2438d3 70 #endif
switches 0:5c4d7b2438d3 71
switches 0:5c4d7b2438d3 72 /*
switches 0:5c4d7b2438d3 73 * Cookies are formed of a 4-bytes timestamp (or serial number) and
switches 0:5c4d7b2438d3 74 * an HMAC of timestemp and client ID.
switches 0:5c4d7b2438d3 75 */
switches 0:5c4d7b2438d3 76 #define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
switches 0:5c4d7b2438d3 77
switches 0:5c4d7b2438d3 78 void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
switches 0:5c4d7b2438d3 79 {
switches 0:5c4d7b2438d3 80 mbedtls_md_init( &ctx->hmac_ctx );
switches 0:5c4d7b2438d3 81 #if !defined(MBEDTLS_HAVE_TIME)
switches 0:5c4d7b2438d3 82 ctx->serial = 0;
switches 0:5c4d7b2438d3 83 #endif
switches 0:5c4d7b2438d3 84 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
switches 0:5c4d7b2438d3 85
switches 0:5c4d7b2438d3 86 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 87 mbedtls_mutex_init( &ctx->mutex );
switches 0:5c4d7b2438d3 88 #endif
switches 0:5c4d7b2438d3 89 }
switches 0:5c4d7b2438d3 90
switches 0:5c4d7b2438d3 91 void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
switches 0:5c4d7b2438d3 92 {
switches 0:5c4d7b2438d3 93 ctx->timeout = delay;
switches 0:5c4d7b2438d3 94 }
switches 0:5c4d7b2438d3 95
switches 0:5c4d7b2438d3 96 void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
switches 0:5c4d7b2438d3 97 {
switches 0:5c4d7b2438d3 98 mbedtls_md_free( &ctx->hmac_ctx );
switches 0:5c4d7b2438d3 99
switches 0:5c4d7b2438d3 100 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 101 mbedtls_mutex_init( &ctx->mutex );
switches 0:5c4d7b2438d3 102 #endif
switches 0:5c4d7b2438d3 103
switches 0:5c4d7b2438d3 104 mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
switches 0:5c4d7b2438d3 105 }
switches 0:5c4d7b2438d3 106
switches 0:5c4d7b2438d3 107 int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
switches 0:5c4d7b2438d3 108 int (*f_rng)(void *, unsigned char *, size_t),
switches 0:5c4d7b2438d3 109 void *p_rng )
switches 0:5c4d7b2438d3 110 {
switches 0:5c4d7b2438d3 111 int ret;
switches 0:5c4d7b2438d3 112 unsigned char key[COOKIE_MD_OUTLEN];
switches 0:5c4d7b2438d3 113
switches 0:5c4d7b2438d3 114 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
switches 0:5c4d7b2438d3 115 return( ret );
switches 0:5c4d7b2438d3 116
switches 0:5c4d7b2438d3 117 ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
switches 0:5c4d7b2438d3 118 if( ret != 0 )
switches 0:5c4d7b2438d3 119 return( ret );
switches 0:5c4d7b2438d3 120
switches 0:5c4d7b2438d3 121 ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
switches 0:5c4d7b2438d3 122 if( ret != 0 )
switches 0:5c4d7b2438d3 123 return( ret );
switches 0:5c4d7b2438d3 124
switches 0:5c4d7b2438d3 125 mbedtls_zeroize( key, sizeof( key ) );
switches 0:5c4d7b2438d3 126
switches 0:5c4d7b2438d3 127 return( 0 );
switches 0:5c4d7b2438d3 128 }
switches 0:5c4d7b2438d3 129
switches 0:5c4d7b2438d3 130 /*
switches 0:5c4d7b2438d3 131 * Generate the HMAC part of a cookie
switches 0:5c4d7b2438d3 132 */
switches 0:5c4d7b2438d3 133 static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
switches 0:5c4d7b2438d3 134 const unsigned char time[4],
switches 0:5c4d7b2438d3 135 unsigned char **p, unsigned char *end,
switches 0:5c4d7b2438d3 136 const unsigned char *cli_id, size_t cli_id_len )
switches 0:5c4d7b2438d3 137 {
switches 0:5c4d7b2438d3 138 unsigned char hmac_out[COOKIE_MD_OUTLEN];
switches 0:5c4d7b2438d3 139
switches 0:5c4d7b2438d3 140 if( (size_t)( end - *p ) < COOKIE_HMAC_LEN )
switches 0:5c4d7b2438d3 141 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
switches 0:5c4d7b2438d3 142
switches 0:5c4d7b2438d3 143 if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
switches 0:5c4d7b2438d3 144 mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
switches 0:5c4d7b2438d3 145 mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
switches 0:5c4d7b2438d3 146 mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
switches 0:5c4d7b2438d3 147 {
switches 0:5c4d7b2438d3 148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
switches 0:5c4d7b2438d3 149 }
switches 0:5c4d7b2438d3 150
switches 0:5c4d7b2438d3 151 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
switches 0:5c4d7b2438d3 152 *p += COOKIE_HMAC_LEN;
switches 0:5c4d7b2438d3 153
switches 0:5c4d7b2438d3 154 return( 0 );
switches 0:5c4d7b2438d3 155 }
switches 0:5c4d7b2438d3 156
switches 0:5c4d7b2438d3 157 /*
switches 0:5c4d7b2438d3 158 * Generate cookie for DTLS ClientHello verification
switches 0:5c4d7b2438d3 159 */
switches 0:5c4d7b2438d3 160 int mbedtls_ssl_cookie_write( void *p_ctx,
switches 0:5c4d7b2438d3 161 unsigned char **p, unsigned char *end,
switches 0:5c4d7b2438d3 162 const unsigned char *cli_id, size_t cli_id_len )
switches 0:5c4d7b2438d3 163 {
switches 0:5c4d7b2438d3 164 int ret;
switches 0:5c4d7b2438d3 165 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
switches 0:5c4d7b2438d3 166 unsigned long t;
switches 0:5c4d7b2438d3 167
switches 0:5c4d7b2438d3 168 if( ctx == NULL || cli_id == NULL )
switches 0:5c4d7b2438d3 169 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
switches 0:5c4d7b2438d3 170
switches 0:5c4d7b2438d3 171 if( (size_t)( end - *p ) < COOKIE_LEN )
switches 0:5c4d7b2438d3 172 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
switches 0:5c4d7b2438d3 173
switches 0:5c4d7b2438d3 174 #if defined(MBEDTLS_HAVE_TIME)
switches 0:5c4d7b2438d3 175 t = (unsigned long) mbedtls_time( NULL );
switches 0:5c4d7b2438d3 176 #else
switches 0:5c4d7b2438d3 177 t = ctx->serial++;
switches 0:5c4d7b2438d3 178 #endif
switches 0:5c4d7b2438d3 179
switches 0:5c4d7b2438d3 180 (*p)[0] = (unsigned char)( t >> 24 );
switches 0:5c4d7b2438d3 181 (*p)[1] = (unsigned char)( t >> 16 );
switches 0:5c4d7b2438d3 182 (*p)[2] = (unsigned char)( t >> 8 );
switches 0:5c4d7b2438d3 183 (*p)[3] = (unsigned char)( t );
switches 0:5c4d7b2438d3 184 *p += 4;
switches 0:5c4d7b2438d3 185
switches 0:5c4d7b2438d3 186 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 187 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
switches 0:5c4d7b2438d3 188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
switches 0:5c4d7b2438d3 189 #endif
switches 0:5c4d7b2438d3 190
switches 0:5c4d7b2438d3 191 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
switches 0:5c4d7b2438d3 192 p, end, cli_id, cli_id_len );
switches 0:5c4d7b2438d3 193
switches 0:5c4d7b2438d3 194 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 195 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
switches 0:5c4d7b2438d3 196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
switches 0:5c4d7b2438d3 197 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
switches 0:5c4d7b2438d3 198 #endif
switches 0:5c4d7b2438d3 199
switches 0:5c4d7b2438d3 200 return( ret );
switches 0:5c4d7b2438d3 201 }
switches 0:5c4d7b2438d3 202
switches 0:5c4d7b2438d3 203 /*
switches 0:5c4d7b2438d3 204 * Check a cookie
switches 0:5c4d7b2438d3 205 */
switches 0:5c4d7b2438d3 206 int mbedtls_ssl_cookie_check( void *p_ctx,
switches 0:5c4d7b2438d3 207 const unsigned char *cookie, size_t cookie_len,
switches 0:5c4d7b2438d3 208 const unsigned char *cli_id, size_t cli_id_len )
switches 0:5c4d7b2438d3 209 {
switches 0:5c4d7b2438d3 210 unsigned char ref_hmac[COOKIE_HMAC_LEN];
switches 0:5c4d7b2438d3 211 int ret = 0;
switches 0:5c4d7b2438d3 212 unsigned char *p = ref_hmac;
switches 0:5c4d7b2438d3 213 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
switches 0:5c4d7b2438d3 214 unsigned long cur_time, cookie_time;
switches 0:5c4d7b2438d3 215
switches 0:5c4d7b2438d3 216 if( ctx == NULL || cli_id == NULL )
switches 0:5c4d7b2438d3 217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
switches 0:5c4d7b2438d3 218
switches 0:5c4d7b2438d3 219 if( cookie_len != COOKIE_LEN )
switches 0:5c4d7b2438d3 220 return( -1 );
switches 0:5c4d7b2438d3 221
switches 0:5c4d7b2438d3 222 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 223 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
switches 0:5c4d7b2438d3 224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
switches 0:5c4d7b2438d3 225 #endif
switches 0:5c4d7b2438d3 226
switches 0:5c4d7b2438d3 227 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
switches 0:5c4d7b2438d3 228 &p, p + sizeof( ref_hmac ),
switches 0:5c4d7b2438d3 229 cli_id, cli_id_len ) != 0 )
switches 0:5c4d7b2438d3 230 ret = -1;
switches 0:5c4d7b2438d3 231
switches 0:5c4d7b2438d3 232 #if defined(MBEDTLS_THREADING_C)
switches 0:5c4d7b2438d3 233 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
switches 0:5c4d7b2438d3 234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
switches 0:5c4d7b2438d3 235 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
switches 0:5c4d7b2438d3 236 #endif
switches 0:5c4d7b2438d3 237
switches 0:5c4d7b2438d3 238 if( ret != 0 )
switches 0:5c4d7b2438d3 239 return( ret );
switches 0:5c4d7b2438d3 240
switches 0:5c4d7b2438d3 241 if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
switches 0:5c4d7b2438d3 242 return( -1 );
switches 0:5c4d7b2438d3 243
switches 0:5c4d7b2438d3 244 #if defined(MBEDTLS_HAVE_TIME)
switches 0:5c4d7b2438d3 245 cur_time = (unsigned long) mbedtls_time( NULL );
switches 0:5c4d7b2438d3 246 #else
switches 0:5c4d7b2438d3 247 cur_time = ctx->serial;
switches 0:5c4d7b2438d3 248 #endif
switches 0:5c4d7b2438d3 249
switches 0:5c4d7b2438d3 250 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
switches 0:5c4d7b2438d3 251 ( (unsigned long) cookie[1] << 16 ) |
switches 0:5c4d7b2438d3 252 ( (unsigned long) cookie[2] << 8 ) |
switches 0:5c4d7b2438d3 253 ( (unsigned long) cookie[3] );
switches 0:5c4d7b2438d3 254
switches 0:5c4d7b2438d3 255 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
switches 0:5c4d7b2438d3 256 return( -1 );
switches 0:5c4d7b2438d3 257
switches 0:5c4d7b2438d3 258 return( 0 );
switches 0:5c4d7b2438d3 259 }
switches 0:5c4d7b2438d3 260 #endif /* MBEDTLS_SSL_COOKIE_C */