mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 19:50:30 2017 +0100
Revision:
2:bbdeda018a3c
Parent:
0:cdf462088d13
Update to mbedtls 2.6.0, many changes.

Changes to mbedtls sources made:

in include/mbedtls/config.h comment out:

#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C

uncomment:

#define MBEDTLS_NO_PLATFORM_ENTROPY

remove the following directorys:

programs
yotta
visualc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /**
markrad 0:cdf462088d13 2 * \file mbedtls_md.c
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * \brief Generic message digest wrapper for mbed TLS
markrad 0:cdf462088d13 5 *
markrad 0:cdf462088d13 6 * \author Adriaan de Jong <dejong@fox-it.com>
markrad 0:cdf462088d13 7 *
markrad 0:cdf462088d13 8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 9 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 10 *
markrad 0:cdf462088d13 11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 12 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 13 * You may obtain a copy of the License at
markrad 0:cdf462088d13 14 *
markrad 0:cdf462088d13 15 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 16 *
markrad 0:cdf462088d13 17 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 20 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 21 * limitations under the License.
markrad 0:cdf462088d13 22 *
markrad 0:cdf462088d13 23 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 24 */
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 #if !defined(MBEDTLS_CONFIG_FILE)
markrad 0:cdf462088d13 27 #include "mbedtls/config.h"
markrad 0:cdf462088d13 28 #else
markrad 0:cdf462088d13 29 #include MBEDTLS_CONFIG_FILE
markrad 0:cdf462088d13 30 #endif
markrad 0:cdf462088d13 31
markrad 0:cdf462088d13 32 #if defined(MBEDTLS_MD_C)
markrad 0:cdf462088d13 33
markrad 0:cdf462088d13 34 #include "mbedtls/md.h"
markrad 0:cdf462088d13 35 #include "mbedtls/md_internal.h"
markrad 0:cdf462088d13 36
markrad 0:cdf462088d13 37 #if defined(MBEDTLS_PLATFORM_C)
markrad 0:cdf462088d13 38 #include "mbedtls/platform.h"
markrad 0:cdf462088d13 39 #else
markrad 0:cdf462088d13 40 #include <stdlib.h>
markrad 0:cdf462088d13 41 #define mbedtls_calloc calloc
markrad 0:cdf462088d13 42 #define mbedtls_free free
markrad 0:cdf462088d13 43 #endif
markrad 0:cdf462088d13 44
markrad 0:cdf462088d13 45 #include <string.h>
markrad 0:cdf462088d13 46
markrad 0:cdf462088d13 47 #if defined(MBEDTLS_FS_IO)
markrad 0:cdf462088d13 48 #include <stdio.h>
markrad 0:cdf462088d13 49 #endif
markrad 0:cdf462088d13 50
markrad 0:cdf462088d13 51 /* Implementation that should never be optimized out by the compiler */
markrad 0:cdf462088d13 52 static void mbedtls_zeroize( void *v, size_t n ) {
markrad 0:cdf462088d13 53 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
markrad 0:cdf462088d13 54 }
markrad 0:cdf462088d13 55
markrad 0:cdf462088d13 56 /*
markrad 0:cdf462088d13 57 * Reminder: update profiles in x509_crt.c when adding a new hash!
markrad 0:cdf462088d13 58 */
markrad 0:cdf462088d13 59 static const int supported_digests[] = {
markrad 0:cdf462088d13 60
markrad 0:cdf462088d13 61 #if defined(MBEDTLS_SHA512_C)
markrad 0:cdf462088d13 62 MBEDTLS_MD_SHA512,
markrad 0:cdf462088d13 63 MBEDTLS_MD_SHA384,
markrad 0:cdf462088d13 64 #endif
markrad 0:cdf462088d13 65
markrad 0:cdf462088d13 66 #if defined(MBEDTLS_SHA256_C)
markrad 0:cdf462088d13 67 MBEDTLS_MD_SHA256,
markrad 0:cdf462088d13 68 MBEDTLS_MD_SHA224,
markrad 0:cdf462088d13 69 #endif
markrad 0:cdf462088d13 70
markrad 0:cdf462088d13 71 #if defined(MBEDTLS_SHA1_C)
markrad 0:cdf462088d13 72 MBEDTLS_MD_SHA1,
markrad 0:cdf462088d13 73 #endif
markrad 0:cdf462088d13 74
markrad 0:cdf462088d13 75 #if defined(MBEDTLS_RIPEMD160_C)
markrad 0:cdf462088d13 76 MBEDTLS_MD_RIPEMD160,
markrad 0:cdf462088d13 77 #endif
markrad 0:cdf462088d13 78
markrad 0:cdf462088d13 79 #if defined(MBEDTLS_MD5_C)
markrad 0:cdf462088d13 80 MBEDTLS_MD_MD5,
markrad 0:cdf462088d13 81 #endif
markrad 0:cdf462088d13 82
markrad 0:cdf462088d13 83 #if defined(MBEDTLS_MD4_C)
markrad 0:cdf462088d13 84 MBEDTLS_MD_MD4,
markrad 0:cdf462088d13 85 #endif
markrad 0:cdf462088d13 86
markrad 0:cdf462088d13 87 #if defined(MBEDTLS_MD2_C)
markrad 0:cdf462088d13 88 MBEDTLS_MD_MD2,
markrad 0:cdf462088d13 89 #endif
markrad 0:cdf462088d13 90
markrad 0:cdf462088d13 91 MBEDTLS_MD_NONE
markrad 0:cdf462088d13 92 };
markrad 0:cdf462088d13 93
markrad 0:cdf462088d13 94 const int *mbedtls_md_list( void )
markrad 0:cdf462088d13 95 {
markrad 0:cdf462088d13 96 return( supported_digests );
markrad 0:cdf462088d13 97 }
markrad 0:cdf462088d13 98
markrad 0:cdf462088d13 99 const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
markrad 0:cdf462088d13 100 {
markrad 0:cdf462088d13 101 if( NULL == md_name )
markrad 0:cdf462088d13 102 return( NULL );
markrad 0:cdf462088d13 103
markrad 0:cdf462088d13 104 /* Get the appropriate digest information */
markrad 0:cdf462088d13 105 #if defined(MBEDTLS_MD2_C)
markrad 0:cdf462088d13 106 if( !strcmp( "MD2", md_name ) )
markrad 0:cdf462088d13 107 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
markrad 0:cdf462088d13 108 #endif
markrad 0:cdf462088d13 109 #if defined(MBEDTLS_MD4_C)
markrad 0:cdf462088d13 110 if( !strcmp( "MD4", md_name ) )
markrad 0:cdf462088d13 111 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
markrad 0:cdf462088d13 112 #endif
markrad 0:cdf462088d13 113 #if defined(MBEDTLS_MD5_C)
markrad 0:cdf462088d13 114 if( !strcmp( "MD5", md_name ) )
markrad 0:cdf462088d13 115 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
markrad 0:cdf462088d13 116 #endif
markrad 0:cdf462088d13 117 #if defined(MBEDTLS_RIPEMD160_C)
markrad 0:cdf462088d13 118 if( !strcmp( "RIPEMD160", md_name ) )
markrad 0:cdf462088d13 119 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
markrad 0:cdf462088d13 120 #endif
markrad 0:cdf462088d13 121 #if defined(MBEDTLS_SHA1_C)
markrad 0:cdf462088d13 122 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
markrad 0:cdf462088d13 123 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
markrad 0:cdf462088d13 124 #endif
markrad 0:cdf462088d13 125 #if defined(MBEDTLS_SHA256_C)
markrad 0:cdf462088d13 126 if( !strcmp( "SHA224", md_name ) )
markrad 0:cdf462088d13 127 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
markrad 0:cdf462088d13 128 if( !strcmp( "SHA256", md_name ) )
markrad 0:cdf462088d13 129 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
markrad 0:cdf462088d13 130 #endif
markrad 0:cdf462088d13 131 #if defined(MBEDTLS_SHA512_C)
markrad 0:cdf462088d13 132 if( !strcmp( "SHA384", md_name ) )
markrad 0:cdf462088d13 133 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
markrad 0:cdf462088d13 134 if( !strcmp( "SHA512", md_name ) )
markrad 0:cdf462088d13 135 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
markrad 0:cdf462088d13 136 #endif
markrad 0:cdf462088d13 137 return( NULL );
markrad 0:cdf462088d13 138 }
markrad 0:cdf462088d13 139
markrad 0:cdf462088d13 140 const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
markrad 0:cdf462088d13 141 {
markrad 0:cdf462088d13 142 switch( md_type )
markrad 0:cdf462088d13 143 {
markrad 0:cdf462088d13 144 #if defined(MBEDTLS_MD2_C)
markrad 0:cdf462088d13 145 case MBEDTLS_MD_MD2:
markrad 0:cdf462088d13 146 return( &mbedtls_md2_info );
markrad 0:cdf462088d13 147 #endif
markrad 0:cdf462088d13 148 #if defined(MBEDTLS_MD4_C)
markrad 0:cdf462088d13 149 case MBEDTLS_MD_MD4:
markrad 0:cdf462088d13 150 return( &mbedtls_md4_info );
markrad 0:cdf462088d13 151 #endif
markrad 0:cdf462088d13 152 #if defined(MBEDTLS_MD5_C)
markrad 0:cdf462088d13 153 case MBEDTLS_MD_MD5:
markrad 0:cdf462088d13 154 return( &mbedtls_md5_info );
markrad 0:cdf462088d13 155 #endif
markrad 0:cdf462088d13 156 #if defined(MBEDTLS_RIPEMD160_C)
markrad 0:cdf462088d13 157 case MBEDTLS_MD_RIPEMD160:
markrad 0:cdf462088d13 158 return( &mbedtls_ripemd160_info );
markrad 0:cdf462088d13 159 #endif
markrad 0:cdf462088d13 160 #if defined(MBEDTLS_SHA1_C)
markrad 0:cdf462088d13 161 case MBEDTLS_MD_SHA1:
markrad 0:cdf462088d13 162 return( &mbedtls_sha1_info );
markrad 0:cdf462088d13 163 #endif
markrad 0:cdf462088d13 164 #if defined(MBEDTLS_SHA256_C)
markrad 0:cdf462088d13 165 case MBEDTLS_MD_SHA224:
markrad 0:cdf462088d13 166 return( &mbedtls_sha224_info );
markrad 0:cdf462088d13 167 case MBEDTLS_MD_SHA256:
markrad 0:cdf462088d13 168 return( &mbedtls_sha256_info );
markrad 0:cdf462088d13 169 #endif
markrad 0:cdf462088d13 170 #if defined(MBEDTLS_SHA512_C)
markrad 0:cdf462088d13 171 case MBEDTLS_MD_SHA384:
markrad 0:cdf462088d13 172 return( &mbedtls_sha384_info );
markrad 0:cdf462088d13 173 case MBEDTLS_MD_SHA512:
markrad 0:cdf462088d13 174 return( &mbedtls_sha512_info );
markrad 0:cdf462088d13 175 #endif
markrad 0:cdf462088d13 176 default:
markrad 0:cdf462088d13 177 return( NULL );
markrad 0:cdf462088d13 178 }
markrad 0:cdf462088d13 179 }
markrad 0:cdf462088d13 180
markrad 0:cdf462088d13 181 void mbedtls_md_init( mbedtls_md_context_t *ctx )
markrad 0:cdf462088d13 182 {
markrad 0:cdf462088d13 183 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
markrad 0:cdf462088d13 184 }
markrad 0:cdf462088d13 185
markrad 0:cdf462088d13 186 void mbedtls_md_free( mbedtls_md_context_t *ctx )
markrad 0:cdf462088d13 187 {
markrad 0:cdf462088d13 188 if( ctx == NULL || ctx->md_info == NULL )
markrad 0:cdf462088d13 189 return;
markrad 0:cdf462088d13 190
markrad 0:cdf462088d13 191 if( ctx->md_ctx != NULL )
markrad 0:cdf462088d13 192 ctx->md_info->ctx_free_func( ctx->md_ctx );
markrad 0:cdf462088d13 193
markrad 0:cdf462088d13 194 if( ctx->hmac_ctx != NULL )
markrad 0:cdf462088d13 195 {
markrad 0:cdf462088d13 196 mbedtls_zeroize( ctx->hmac_ctx, 2 * ctx->md_info->block_size );
markrad 0:cdf462088d13 197 mbedtls_free( ctx->hmac_ctx );
markrad 0:cdf462088d13 198 }
markrad 0:cdf462088d13 199
markrad 0:cdf462088d13 200 mbedtls_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
markrad 0:cdf462088d13 201 }
markrad 0:cdf462088d13 202
markrad 0:cdf462088d13 203 int mbedtls_md_clone( mbedtls_md_context_t *dst,
markrad 0:cdf462088d13 204 const mbedtls_md_context_t *src )
markrad 0:cdf462088d13 205 {
markrad 0:cdf462088d13 206 if( dst == NULL || dst->md_info == NULL ||
markrad 0:cdf462088d13 207 src == NULL || src->md_info == NULL ||
markrad 0:cdf462088d13 208 dst->md_info != src->md_info )
markrad 0:cdf462088d13 209 {
markrad 0:cdf462088d13 210 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 211 }
markrad 0:cdf462088d13 212
markrad 0:cdf462088d13 213 dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
markrad 0:cdf462088d13 214
markrad 0:cdf462088d13 215 return( 0 );
markrad 0:cdf462088d13 216 }
markrad 0:cdf462088d13 217
markrad 0:cdf462088d13 218 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
markrad 0:cdf462088d13 219 int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
markrad 0:cdf462088d13 220 {
markrad 0:cdf462088d13 221 return mbedtls_md_setup( ctx, md_info, 1 );
markrad 0:cdf462088d13 222 }
markrad 0:cdf462088d13 223 #endif
markrad 0:cdf462088d13 224
markrad 0:cdf462088d13 225 int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
markrad 0:cdf462088d13 226 {
markrad 0:cdf462088d13 227 if( md_info == NULL || ctx == NULL )
markrad 0:cdf462088d13 228 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 229
markrad 0:cdf462088d13 230 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
markrad 0:cdf462088d13 231 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
markrad 0:cdf462088d13 232
markrad 0:cdf462088d13 233 if( hmac != 0 )
markrad 0:cdf462088d13 234 {
markrad 0:cdf462088d13 235 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
markrad 0:cdf462088d13 236 if( ctx->hmac_ctx == NULL )
markrad 0:cdf462088d13 237 {
markrad 0:cdf462088d13 238 md_info->ctx_free_func( ctx->md_ctx );
markrad 0:cdf462088d13 239 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
markrad 0:cdf462088d13 240 }
markrad 0:cdf462088d13 241 }
markrad 0:cdf462088d13 242
markrad 0:cdf462088d13 243 ctx->md_info = md_info;
markrad 0:cdf462088d13 244
markrad 0:cdf462088d13 245 return( 0 );
markrad 0:cdf462088d13 246 }
markrad 0:cdf462088d13 247
markrad 0:cdf462088d13 248 int mbedtls_md_starts( mbedtls_md_context_t *ctx )
markrad 0:cdf462088d13 249 {
markrad 0:cdf462088d13 250 if( ctx == NULL || ctx->md_info == NULL )
markrad 0:cdf462088d13 251 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 252
markrad 0:cdf462088d13 253 ctx->md_info->starts_func( ctx->md_ctx );
markrad 0:cdf462088d13 254
markrad 0:cdf462088d13 255 return( 0 );
markrad 0:cdf462088d13 256 }
markrad 0:cdf462088d13 257
markrad 0:cdf462088d13 258 int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
markrad 0:cdf462088d13 259 {
markrad 0:cdf462088d13 260 if( ctx == NULL || ctx->md_info == NULL )
markrad 0:cdf462088d13 261 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 262
markrad 0:cdf462088d13 263 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
markrad 0:cdf462088d13 264
markrad 0:cdf462088d13 265 return( 0 );
markrad 0:cdf462088d13 266 }
markrad 0:cdf462088d13 267
markrad 0:cdf462088d13 268 int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
markrad 0:cdf462088d13 269 {
markrad 0:cdf462088d13 270 if( ctx == NULL || ctx->md_info == NULL )
markrad 0:cdf462088d13 271 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 272
markrad 0:cdf462088d13 273 ctx->md_info->finish_func( ctx->md_ctx, output );
markrad 0:cdf462088d13 274
markrad 0:cdf462088d13 275 return( 0 );
markrad 0:cdf462088d13 276 }
markrad 0:cdf462088d13 277
markrad 0:cdf462088d13 278 int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
markrad 0:cdf462088d13 279 unsigned char *output )
markrad 0:cdf462088d13 280 {
markrad 0:cdf462088d13 281 if( md_info == NULL )
markrad 0:cdf462088d13 282 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 283
markrad 0:cdf462088d13 284 md_info->digest_func( input, ilen, output );
markrad 0:cdf462088d13 285
markrad 0:cdf462088d13 286 return( 0 );
markrad 0:cdf462088d13 287 }
markrad 0:cdf462088d13 288
markrad 0:cdf462088d13 289 #if defined(MBEDTLS_FS_IO)
markrad 0:cdf462088d13 290 int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
markrad 0:cdf462088d13 291 {
markrad 0:cdf462088d13 292 int ret;
markrad 0:cdf462088d13 293 FILE *f;
markrad 0:cdf462088d13 294 size_t n;
markrad 0:cdf462088d13 295 mbedtls_md_context_t ctx;
markrad 0:cdf462088d13 296 unsigned char buf[1024];
markrad 0:cdf462088d13 297
markrad 0:cdf462088d13 298 if( md_info == NULL )
markrad 0:cdf462088d13 299 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 300
markrad 0:cdf462088d13 301 if( ( f = fopen( path, "rb" ) ) == NULL )
markrad 0:cdf462088d13 302 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
markrad 0:cdf462088d13 303
markrad 0:cdf462088d13 304 mbedtls_md_init( &ctx );
markrad 0:cdf462088d13 305
markrad 0:cdf462088d13 306 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
markrad 0:cdf462088d13 307 goto cleanup;
markrad 0:cdf462088d13 308
markrad 0:cdf462088d13 309 md_info->starts_func( ctx.md_ctx );
markrad 0:cdf462088d13 310
markrad 0:cdf462088d13 311 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
markrad 0:cdf462088d13 312 md_info->update_func( ctx.md_ctx, buf, n );
markrad 0:cdf462088d13 313
markrad 0:cdf462088d13 314 if( ferror( f ) != 0 )
markrad 0:cdf462088d13 315 {
markrad 0:cdf462088d13 316 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
markrad 0:cdf462088d13 317 goto cleanup;
markrad 0:cdf462088d13 318 }
markrad 0:cdf462088d13 319
markrad 0:cdf462088d13 320 md_info->finish_func( ctx.md_ctx, output );
markrad 0:cdf462088d13 321
markrad 0:cdf462088d13 322 cleanup:
markrad 0:cdf462088d13 323 fclose( f );
markrad 0:cdf462088d13 324 mbedtls_md_free( &ctx );
markrad 0:cdf462088d13 325
markrad 0:cdf462088d13 326 return( ret );
markrad 0:cdf462088d13 327 }
markrad 0:cdf462088d13 328 #endif /* MBEDTLS_FS_IO */
markrad 0:cdf462088d13 329
markrad 0:cdf462088d13 330 int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
markrad 0:cdf462088d13 331 {
markrad 0:cdf462088d13 332 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
markrad 0:cdf462088d13 333 unsigned char *ipad, *opad;
markrad 0:cdf462088d13 334 size_t i;
markrad 0:cdf462088d13 335
markrad 0:cdf462088d13 336 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
markrad 0:cdf462088d13 337 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 338
markrad 0:cdf462088d13 339 if( keylen > (size_t) ctx->md_info->block_size )
markrad 0:cdf462088d13 340 {
markrad 0:cdf462088d13 341 ctx->md_info->starts_func( ctx->md_ctx );
markrad 0:cdf462088d13 342 ctx->md_info->update_func( ctx->md_ctx, key, keylen );
markrad 0:cdf462088d13 343 ctx->md_info->finish_func( ctx->md_ctx, sum );
markrad 0:cdf462088d13 344
markrad 0:cdf462088d13 345 keylen = ctx->md_info->size;
markrad 0:cdf462088d13 346 key = sum;
markrad 0:cdf462088d13 347 }
markrad 0:cdf462088d13 348
markrad 0:cdf462088d13 349 ipad = (unsigned char *) ctx->hmac_ctx;
markrad 0:cdf462088d13 350 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
markrad 0:cdf462088d13 351
markrad 0:cdf462088d13 352 memset( ipad, 0x36, ctx->md_info->block_size );
markrad 0:cdf462088d13 353 memset( opad, 0x5C, ctx->md_info->block_size );
markrad 0:cdf462088d13 354
markrad 0:cdf462088d13 355 for( i = 0; i < keylen; i++ )
markrad 0:cdf462088d13 356 {
markrad 0:cdf462088d13 357 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
markrad 0:cdf462088d13 358 opad[i] = (unsigned char)( opad[i] ^ key[i] );
markrad 0:cdf462088d13 359 }
markrad 0:cdf462088d13 360
markrad 0:cdf462088d13 361 mbedtls_zeroize( sum, sizeof( sum ) );
markrad 0:cdf462088d13 362
markrad 0:cdf462088d13 363 ctx->md_info->starts_func( ctx->md_ctx );
markrad 0:cdf462088d13 364 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
markrad 0:cdf462088d13 365
markrad 0:cdf462088d13 366 return( 0 );
markrad 0:cdf462088d13 367 }
markrad 0:cdf462088d13 368
markrad 0:cdf462088d13 369 int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
markrad 0:cdf462088d13 370 {
markrad 0:cdf462088d13 371 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
markrad 0:cdf462088d13 372 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 373
markrad 0:cdf462088d13 374 ctx->md_info->update_func( ctx->md_ctx, input, ilen );
markrad 0:cdf462088d13 375
markrad 0:cdf462088d13 376 return( 0 );
markrad 0:cdf462088d13 377 }
markrad 0:cdf462088d13 378
markrad 0:cdf462088d13 379 int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
markrad 0:cdf462088d13 380 {
markrad 0:cdf462088d13 381 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
markrad 0:cdf462088d13 382 unsigned char *opad;
markrad 0:cdf462088d13 383
markrad 0:cdf462088d13 384 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
markrad 0:cdf462088d13 385 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 386
markrad 0:cdf462088d13 387 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
markrad 0:cdf462088d13 388
markrad 0:cdf462088d13 389 ctx->md_info->finish_func( ctx->md_ctx, tmp );
markrad 0:cdf462088d13 390 ctx->md_info->starts_func( ctx->md_ctx );
markrad 0:cdf462088d13 391 ctx->md_info->update_func( ctx->md_ctx, opad, ctx->md_info->block_size );
markrad 0:cdf462088d13 392 ctx->md_info->update_func( ctx->md_ctx, tmp, ctx->md_info->size );
markrad 0:cdf462088d13 393 ctx->md_info->finish_func( ctx->md_ctx, output );
markrad 0:cdf462088d13 394
markrad 0:cdf462088d13 395 return( 0 );
markrad 0:cdf462088d13 396 }
markrad 0:cdf462088d13 397
markrad 0:cdf462088d13 398 int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
markrad 0:cdf462088d13 399 {
markrad 0:cdf462088d13 400 unsigned char *ipad;
markrad 0:cdf462088d13 401
markrad 0:cdf462088d13 402 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
markrad 0:cdf462088d13 403 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 404
markrad 0:cdf462088d13 405 ipad = (unsigned char *) ctx->hmac_ctx;
markrad 0:cdf462088d13 406
markrad 0:cdf462088d13 407 ctx->md_info->starts_func( ctx->md_ctx );
markrad 0:cdf462088d13 408 ctx->md_info->update_func( ctx->md_ctx, ipad, ctx->md_info->block_size );
markrad 0:cdf462088d13 409
markrad 0:cdf462088d13 410 return( 0 );
markrad 0:cdf462088d13 411 }
markrad 0:cdf462088d13 412
markrad 0:cdf462088d13 413 int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
markrad 0:cdf462088d13 414 const unsigned char *input, size_t ilen,
markrad 0:cdf462088d13 415 unsigned char *output )
markrad 0:cdf462088d13 416 {
markrad 0:cdf462088d13 417 mbedtls_md_context_t ctx;
markrad 0:cdf462088d13 418 int ret;
markrad 0:cdf462088d13 419
markrad 0:cdf462088d13 420 if( md_info == NULL )
markrad 0:cdf462088d13 421 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 422
markrad 0:cdf462088d13 423 mbedtls_md_init( &ctx );
markrad 0:cdf462088d13 424
markrad 0:cdf462088d13 425 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
markrad 0:cdf462088d13 426 return( ret );
markrad 0:cdf462088d13 427
markrad 0:cdf462088d13 428 mbedtls_md_hmac_starts( &ctx, key, keylen );
markrad 0:cdf462088d13 429 mbedtls_md_hmac_update( &ctx, input, ilen );
markrad 0:cdf462088d13 430 mbedtls_md_hmac_finish( &ctx, output );
markrad 0:cdf462088d13 431
markrad 0:cdf462088d13 432 mbedtls_md_free( &ctx );
markrad 0:cdf462088d13 433
markrad 0:cdf462088d13 434 return( 0 );
markrad 0:cdf462088d13 435 }
markrad 0:cdf462088d13 436
markrad 0:cdf462088d13 437 int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
markrad 0:cdf462088d13 438 {
markrad 0:cdf462088d13 439 if( ctx == NULL || ctx->md_info == NULL )
markrad 0:cdf462088d13 440 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
markrad 0:cdf462088d13 441
markrad 0:cdf462088d13 442 ctx->md_info->process_func( ctx->md_ctx, data );
markrad 0:cdf462088d13 443
markrad 0:cdf462088d13 444 return( 0 );
markrad 0:cdf462088d13 445 }
markrad 0:cdf462088d13 446
markrad 0:cdf462088d13 447 unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
markrad 0:cdf462088d13 448 {
markrad 0:cdf462088d13 449 if( md_info == NULL )
markrad 0:cdf462088d13 450 return( 0 );
markrad 0:cdf462088d13 451
markrad 0:cdf462088d13 452 return md_info->size;
markrad 0:cdf462088d13 453 }
markrad 0:cdf462088d13 454
markrad 0:cdf462088d13 455 mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
markrad 0:cdf462088d13 456 {
markrad 0:cdf462088d13 457 if( md_info == NULL )
markrad 0:cdf462088d13 458 return( MBEDTLS_MD_NONE );
markrad 0:cdf462088d13 459
markrad 0:cdf462088d13 460 return md_info->type;
markrad 0:cdf462088d13 461 }
markrad 0:cdf462088d13 462
markrad 0:cdf462088d13 463 const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
markrad 0:cdf462088d13 464 {
markrad 0:cdf462088d13 465 if( md_info == NULL )
markrad 0:cdf462088d13 466 return( NULL );
markrad 0:cdf462088d13 467
markrad 0:cdf462088d13 468 return md_info->name;
markrad 0:cdf462088d13 469 }
markrad 0:cdf462088d13 470
markrad 0:cdf462088d13 471 #endif /* MBEDTLS_MD_C */