Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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