Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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