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