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