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