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