nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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