mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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