BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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