mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew 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 1:9db0e321a9f4 38 #include "mbedtls/md2.h"
kenjiArai 1:9db0e321a9f4 39 #include "mbedtls/md4.h"
kenjiArai 1:9db0e321a9f4 40 #include "mbedtls/md5.h"
kenjiArai 1:9db0e321a9f4 41 #include "mbedtls/ripemd160.h"
kenjiArai 1:9db0e321a9f4 42 #include "mbedtls/sha1.h"
kenjiArai 1:9db0e321a9f4 43 #include "mbedtls/sha256.h"
kenjiArai 1:9db0e321a9f4 44 #include "mbedtls/sha512.h"
kenjiArai 1:9db0e321a9f4 45
kenjiArai 0:5b88d5760320 46 #if defined(MBEDTLS_PLATFORM_C)
kenjiArai 0:5b88d5760320 47 #include "mbedtls/platform.h"
kenjiArai 0:5b88d5760320 48 #else
kenjiArai 0:5b88d5760320 49 #include <stdlib.h>
kenjiArai 0:5b88d5760320 50 #define mbedtls_calloc calloc
kenjiArai 0:5b88d5760320 51 #define mbedtls_free free
kenjiArai 0:5b88d5760320 52 #endif
kenjiArai 0:5b88d5760320 53
kenjiArai 0:5b88d5760320 54 #include <string.h>
kenjiArai 0:5b88d5760320 55
kenjiArai 0:5b88d5760320 56 #if defined(MBEDTLS_FS_IO)
kenjiArai 0:5b88d5760320 57 #include <stdio.h>
kenjiArai 0:5b88d5760320 58 #endif
kenjiArai 0:5b88d5760320 59
kenjiArai 1:9db0e321a9f4 60 #if defined(MBEDTLS_MD2_C)
kenjiArai 1:9db0e321a9f4 61 const mbedtls_md_info_t mbedtls_md2_info = {
kenjiArai 1:9db0e321a9f4 62 "MD2",
kenjiArai 1:9db0e321a9f4 63 MBEDTLS_MD_MD2,
kenjiArai 1:9db0e321a9f4 64 16,
kenjiArai 1:9db0e321a9f4 65 16,
kenjiArai 1:9db0e321a9f4 66 };
kenjiArai 1:9db0e321a9f4 67 #endif
kenjiArai 1:9db0e321a9f4 68
kenjiArai 1:9db0e321a9f4 69 #if defined(MBEDTLS_MD4_C)
kenjiArai 1:9db0e321a9f4 70 const mbedtls_md_info_t mbedtls_md4_info = {
kenjiArai 1:9db0e321a9f4 71 "MD4",
kenjiArai 1:9db0e321a9f4 72 MBEDTLS_MD_MD4,
kenjiArai 1:9db0e321a9f4 73 16,
kenjiArai 1:9db0e321a9f4 74 64,
kenjiArai 1:9db0e321a9f4 75 };
kenjiArai 1:9db0e321a9f4 76 #endif
kenjiArai 1:9db0e321a9f4 77
kenjiArai 1:9db0e321a9f4 78 #if defined(MBEDTLS_MD5_C)
kenjiArai 1:9db0e321a9f4 79 const mbedtls_md_info_t mbedtls_md5_info = {
kenjiArai 1:9db0e321a9f4 80 "MD5",
kenjiArai 1:9db0e321a9f4 81 MBEDTLS_MD_MD5,
kenjiArai 1:9db0e321a9f4 82 16,
kenjiArai 1:9db0e321a9f4 83 64,
kenjiArai 1:9db0e321a9f4 84 };
kenjiArai 1:9db0e321a9f4 85 #endif
kenjiArai 1:9db0e321a9f4 86
kenjiArai 1:9db0e321a9f4 87 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 1:9db0e321a9f4 88 const mbedtls_md_info_t mbedtls_ripemd160_info = {
kenjiArai 1:9db0e321a9f4 89 "RIPEMD160",
kenjiArai 1:9db0e321a9f4 90 MBEDTLS_MD_RIPEMD160,
kenjiArai 1:9db0e321a9f4 91 20,
kenjiArai 1:9db0e321a9f4 92 64,
kenjiArai 1:9db0e321a9f4 93 };
kenjiArai 1:9db0e321a9f4 94 #endif
kenjiArai 1:9db0e321a9f4 95
kenjiArai 1:9db0e321a9f4 96 #if defined(MBEDTLS_SHA1_C)
kenjiArai 1:9db0e321a9f4 97 const mbedtls_md_info_t mbedtls_sha1_info = {
kenjiArai 1:9db0e321a9f4 98 "SHA1",
kenjiArai 1:9db0e321a9f4 99 MBEDTLS_MD_SHA1,
kenjiArai 1:9db0e321a9f4 100 20,
kenjiArai 1:9db0e321a9f4 101 64,
kenjiArai 1:9db0e321a9f4 102 };
kenjiArai 1:9db0e321a9f4 103 #endif
kenjiArai 1:9db0e321a9f4 104
kenjiArai 1:9db0e321a9f4 105 #if defined(MBEDTLS_SHA256_C)
kenjiArai 1:9db0e321a9f4 106 const mbedtls_md_info_t mbedtls_sha224_info = {
kenjiArai 1:9db0e321a9f4 107 "SHA224",
kenjiArai 1:9db0e321a9f4 108 MBEDTLS_MD_SHA224,
kenjiArai 1:9db0e321a9f4 109 28,
kenjiArai 1:9db0e321a9f4 110 64,
kenjiArai 1:9db0e321a9f4 111 };
kenjiArai 1:9db0e321a9f4 112
kenjiArai 1:9db0e321a9f4 113 const mbedtls_md_info_t mbedtls_sha256_info = {
kenjiArai 1:9db0e321a9f4 114 "SHA256",
kenjiArai 1:9db0e321a9f4 115 MBEDTLS_MD_SHA256,
kenjiArai 1:9db0e321a9f4 116 32,
kenjiArai 1:9db0e321a9f4 117 64,
kenjiArai 1:9db0e321a9f4 118 };
kenjiArai 1:9db0e321a9f4 119 #endif
kenjiArai 1:9db0e321a9f4 120
kenjiArai 1:9db0e321a9f4 121 #if defined(MBEDTLS_SHA512_C)
kenjiArai 1:9db0e321a9f4 122 const mbedtls_md_info_t mbedtls_sha384_info = {
kenjiArai 1:9db0e321a9f4 123 "SHA384",
kenjiArai 1:9db0e321a9f4 124 MBEDTLS_MD_SHA384,
kenjiArai 1:9db0e321a9f4 125 48,
kenjiArai 1:9db0e321a9f4 126 128,
kenjiArai 1:9db0e321a9f4 127 };
kenjiArai 1:9db0e321a9f4 128
kenjiArai 1:9db0e321a9f4 129 const mbedtls_md_info_t mbedtls_sha512_info = {
kenjiArai 1:9db0e321a9f4 130 "SHA512",
kenjiArai 1:9db0e321a9f4 131 MBEDTLS_MD_SHA512,
kenjiArai 1:9db0e321a9f4 132 64,
kenjiArai 1:9db0e321a9f4 133 128,
kenjiArai 1:9db0e321a9f4 134 };
kenjiArai 1:9db0e321a9f4 135 #endif
kenjiArai 1:9db0e321a9f4 136
kenjiArai 0:5b88d5760320 137 /*
kenjiArai 0:5b88d5760320 138 * Reminder: update profiles in Mbed TLS's x509_crt.c when adding a new hash!
kenjiArai 0:5b88d5760320 139 */
kenjiArai 0:5b88d5760320 140 static const int supported_digests[] = {
kenjiArai 0:5b88d5760320 141
kenjiArai 0:5b88d5760320 142 #if defined(MBEDTLS_SHA512_C)
kenjiArai 0:5b88d5760320 143 MBEDTLS_MD_SHA512,
kenjiArai 0:5b88d5760320 144 MBEDTLS_MD_SHA384,
kenjiArai 0:5b88d5760320 145 #endif
kenjiArai 0:5b88d5760320 146
kenjiArai 0:5b88d5760320 147 #if defined(MBEDTLS_SHA256_C)
kenjiArai 0:5b88d5760320 148 MBEDTLS_MD_SHA256,
kenjiArai 0:5b88d5760320 149 MBEDTLS_MD_SHA224,
kenjiArai 0:5b88d5760320 150 #endif
kenjiArai 0:5b88d5760320 151
kenjiArai 0:5b88d5760320 152 #if defined(MBEDTLS_SHA1_C)
kenjiArai 0:5b88d5760320 153 MBEDTLS_MD_SHA1,
kenjiArai 0:5b88d5760320 154 #endif
kenjiArai 0:5b88d5760320 155
kenjiArai 0:5b88d5760320 156 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 0:5b88d5760320 157 MBEDTLS_MD_RIPEMD160,
kenjiArai 0:5b88d5760320 158 #endif
kenjiArai 0:5b88d5760320 159
kenjiArai 0:5b88d5760320 160 #if defined(MBEDTLS_MD5_C)
kenjiArai 0:5b88d5760320 161 MBEDTLS_MD_MD5,
kenjiArai 0:5b88d5760320 162 #endif
kenjiArai 0:5b88d5760320 163
kenjiArai 0:5b88d5760320 164 #if defined(MBEDTLS_MD4_C)
kenjiArai 0:5b88d5760320 165 MBEDTLS_MD_MD4,
kenjiArai 0:5b88d5760320 166 #endif
kenjiArai 0:5b88d5760320 167
kenjiArai 0:5b88d5760320 168 #if defined(MBEDTLS_MD2_C)
kenjiArai 0:5b88d5760320 169 MBEDTLS_MD_MD2,
kenjiArai 0:5b88d5760320 170 #endif
kenjiArai 0:5b88d5760320 171
kenjiArai 0:5b88d5760320 172 MBEDTLS_MD_NONE
kenjiArai 0:5b88d5760320 173 };
kenjiArai 0:5b88d5760320 174
kenjiArai 0:5b88d5760320 175 const int *mbedtls_md_list( void )
kenjiArai 0:5b88d5760320 176 {
kenjiArai 0:5b88d5760320 177 return( supported_digests );
kenjiArai 0:5b88d5760320 178 }
kenjiArai 0:5b88d5760320 179
kenjiArai 0:5b88d5760320 180 const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
kenjiArai 0:5b88d5760320 181 {
kenjiArai 0:5b88d5760320 182 if( NULL == md_name )
kenjiArai 0:5b88d5760320 183 return( NULL );
kenjiArai 0:5b88d5760320 184
kenjiArai 0:5b88d5760320 185 /* Get the appropriate digest information */
kenjiArai 0:5b88d5760320 186 #if defined(MBEDTLS_MD2_C)
kenjiArai 0:5b88d5760320 187 if( !strcmp( "MD2", md_name ) )
kenjiArai 0:5b88d5760320 188 return mbedtls_md_info_from_type( MBEDTLS_MD_MD2 );
kenjiArai 0:5b88d5760320 189 #endif
kenjiArai 0:5b88d5760320 190 #if defined(MBEDTLS_MD4_C)
kenjiArai 0:5b88d5760320 191 if( !strcmp( "MD4", md_name ) )
kenjiArai 0:5b88d5760320 192 return mbedtls_md_info_from_type( MBEDTLS_MD_MD4 );
kenjiArai 0:5b88d5760320 193 #endif
kenjiArai 0:5b88d5760320 194 #if defined(MBEDTLS_MD5_C)
kenjiArai 0:5b88d5760320 195 if( !strcmp( "MD5", md_name ) )
kenjiArai 0:5b88d5760320 196 return mbedtls_md_info_from_type( MBEDTLS_MD_MD5 );
kenjiArai 0:5b88d5760320 197 #endif
kenjiArai 0:5b88d5760320 198 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 0:5b88d5760320 199 if( !strcmp( "RIPEMD160", md_name ) )
kenjiArai 0:5b88d5760320 200 return mbedtls_md_info_from_type( MBEDTLS_MD_RIPEMD160 );
kenjiArai 0:5b88d5760320 201 #endif
kenjiArai 0:5b88d5760320 202 #if defined(MBEDTLS_SHA1_C)
kenjiArai 0:5b88d5760320 203 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
kenjiArai 0:5b88d5760320 204 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
kenjiArai 0:5b88d5760320 205 #endif
kenjiArai 0:5b88d5760320 206 #if defined(MBEDTLS_SHA256_C)
kenjiArai 0:5b88d5760320 207 if( !strcmp( "SHA224", md_name ) )
kenjiArai 0:5b88d5760320 208 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA224 );
kenjiArai 0:5b88d5760320 209 if( !strcmp( "SHA256", md_name ) )
kenjiArai 0:5b88d5760320 210 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
kenjiArai 0:5b88d5760320 211 #endif
kenjiArai 0:5b88d5760320 212 #if defined(MBEDTLS_SHA512_C)
kenjiArai 0:5b88d5760320 213 if( !strcmp( "SHA384", md_name ) )
kenjiArai 0:5b88d5760320 214 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA384 );
kenjiArai 0:5b88d5760320 215 if( !strcmp( "SHA512", md_name ) )
kenjiArai 0:5b88d5760320 216 return mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
kenjiArai 0:5b88d5760320 217 #endif
kenjiArai 0:5b88d5760320 218 return( NULL );
kenjiArai 0:5b88d5760320 219 }
kenjiArai 0:5b88d5760320 220
kenjiArai 0:5b88d5760320 221 const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
kenjiArai 0:5b88d5760320 222 {
kenjiArai 0:5b88d5760320 223 switch( md_type )
kenjiArai 0:5b88d5760320 224 {
kenjiArai 0:5b88d5760320 225 #if defined(MBEDTLS_MD2_C)
kenjiArai 0:5b88d5760320 226 case MBEDTLS_MD_MD2:
kenjiArai 0:5b88d5760320 227 return( &mbedtls_md2_info );
kenjiArai 0:5b88d5760320 228 #endif
kenjiArai 0:5b88d5760320 229 #if defined(MBEDTLS_MD4_C)
kenjiArai 0:5b88d5760320 230 case MBEDTLS_MD_MD4:
kenjiArai 0:5b88d5760320 231 return( &mbedtls_md4_info );
kenjiArai 0:5b88d5760320 232 #endif
kenjiArai 0:5b88d5760320 233 #if defined(MBEDTLS_MD5_C)
kenjiArai 0:5b88d5760320 234 case MBEDTLS_MD_MD5:
kenjiArai 0:5b88d5760320 235 return( &mbedtls_md5_info );
kenjiArai 0:5b88d5760320 236 #endif
kenjiArai 0:5b88d5760320 237 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 0:5b88d5760320 238 case MBEDTLS_MD_RIPEMD160:
kenjiArai 0:5b88d5760320 239 return( &mbedtls_ripemd160_info );
kenjiArai 0:5b88d5760320 240 #endif
kenjiArai 0:5b88d5760320 241 #if defined(MBEDTLS_SHA1_C)
kenjiArai 0:5b88d5760320 242 case MBEDTLS_MD_SHA1:
kenjiArai 0:5b88d5760320 243 return( &mbedtls_sha1_info );
kenjiArai 0:5b88d5760320 244 #endif
kenjiArai 0:5b88d5760320 245 #if defined(MBEDTLS_SHA256_C)
kenjiArai 0:5b88d5760320 246 case MBEDTLS_MD_SHA224:
kenjiArai 0:5b88d5760320 247 return( &mbedtls_sha224_info );
kenjiArai 0:5b88d5760320 248 case MBEDTLS_MD_SHA256:
kenjiArai 0:5b88d5760320 249 return( &mbedtls_sha256_info );
kenjiArai 0:5b88d5760320 250 #endif
kenjiArai 0:5b88d5760320 251 #if defined(MBEDTLS_SHA512_C)
kenjiArai 0:5b88d5760320 252 case MBEDTLS_MD_SHA384:
kenjiArai 0:5b88d5760320 253 return( &mbedtls_sha384_info );
kenjiArai 0:5b88d5760320 254 case MBEDTLS_MD_SHA512:
kenjiArai 0:5b88d5760320 255 return( &mbedtls_sha512_info );
kenjiArai 0:5b88d5760320 256 #endif
kenjiArai 0:5b88d5760320 257 default:
kenjiArai 0:5b88d5760320 258 return( NULL );
kenjiArai 0:5b88d5760320 259 }
kenjiArai 0:5b88d5760320 260 }
kenjiArai 0:5b88d5760320 261
kenjiArai 0:5b88d5760320 262 void mbedtls_md_init( mbedtls_md_context_t *ctx )
kenjiArai 0:5b88d5760320 263 {
kenjiArai 0:5b88d5760320 264 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
kenjiArai 0:5b88d5760320 265 }
kenjiArai 0:5b88d5760320 266
kenjiArai 0:5b88d5760320 267 void mbedtls_md_free( mbedtls_md_context_t *ctx )
kenjiArai 0:5b88d5760320 268 {
kenjiArai 0:5b88d5760320 269 if( ctx == NULL || ctx->md_info == NULL )
kenjiArai 0:5b88d5760320 270 return;
kenjiArai 0:5b88d5760320 271
kenjiArai 0:5b88d5760320 272 if( ctx->md_ctx != NULL )
kenjiArai 1:9db0e321a9f4 273 {
kenjiArai 1:9db0e321a9f4 274 switch( ctx->md_info->type )
kenjiArai 1:9db0e321a9f4 275 {
kenjiArai 1:9db0e321a9f4 276 #if defined(MBEDTLS_MD2_C)
kenjiArai 1:9db0e321a9f4 277 case MBEDTLS_MD_MD2:
kenjiArai 1:9db0e321a9f4 278 mbedtls_md2_free( ctx->md_ctx );
kenjiArai 1:9db0e321a9f4 279 break;
kenjiArai 1:9db0e321a9f4 280 #endif
kenjiArai 1:9db0e321a9f4 281 #if defined(MBEDTLS_MD4_C)
kenjiArai 1:9db0e321a9f4 282 case MBEDTLS_MD_MD4:
kenjiArai 1:9db0e321a9f4 283 mbedtls_md4_free( ctx->md_ctx );
kenjiArai 1:9db0e321a9f4 284 break;
kenjiArai 1:9db0e321a9f4 285 #endif
kenjiArai 1:9db0e321a9f4 286 #if defined(MBEDTLS_MD5_C)
kenjiArai 1:9db0e321a9f4 287 case MBEDTLS_MD_MD5:
kenjiArai 1:9db0e321a9f4 288 mbedtls_md5_free( ctx->md_ctx );
kenjiArai 1:9db0e321a9f4 289 break;
kenjiArai 1:9db0e321a9f4 290 #endif
kenjiArai 1:9db0e321a9f4 291 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 1:9db0e321a9f4 292 case MBEDTLS_MD_RIPEMD160:
kenjiArai 1:9db0e321a9f4 293 mbedtls_ripemd160_free( ctx->md_ctx );
kenjiArai 1:9db0e321a9f4 294 break;
kenjiArai 1:9db0e321a9f4 295 #endif
kenjiArai 1:9db0e321a9f4 296 #if defined(MBEDTLS_SHA1_C)
kenjiArai 1:9db0e321a9f4 297 case MBEDTLS_MD_SHA1:
kenjiArai 1:9db0e321a9f4 298 mbedtls_sha1_free( ctx->md_ctx );
kenjiArai 1:9db0e321a9f4 299 break;
kenjiArai 1:9db0e321a9f4 300 #endif
kenjiArai 1:9db0e321a9f4 301 #if defined(MBEDTLS_SHA256_C)
kenjiArai 1:9db0e321a9f4 302 case MBEDTLS_MD_SHA224:
kenjiArai 1:9db0e321a9f4 303 case MBEDTLS_MD_SHA256:
kenjiArai 1:9db0e321a9f4 304 mbedtls_sha256_free( ctx->md_ctx );
kenjiArai 1:9db0e321a9f4 305 break;
kenjiArai 1:9db0e321a9f4 306 #endif
kenjiArai 1:9db0e321a9f4 307 #if defined(MBEDTLS_SHA512_C)
kenjiArai 1:9db0e321a9f4 308 case MBEDTLS_MD_SHA384:
kenjiArai 1:9db0e321a9f4 309 case MBEDTLS_MD_SHA512:
kenjiArai 1:9db0e321a9f4 310 mbedtls_sha512_free( ctx->md_ctx );
kenjiArai 1:9db0e321a9f4 311 break;
kenjiArai 1:9db0e321a9f4 312 #endif
kenjiArai 1:9db0e321a9f4 313 default:
kenjiArai 1:9db0e321a9f4 314 /* Shouldn't happen */
kenjiArai 1:9db0e321a9f4 315 break;
kenjiArai 1:9db0e321a9f4 316 }
kenjiArai 1:9db0e321a9f4 317 mbedtls_free( ctx->md_ctx );
kenjiArai 1:9db0e321a9f4 318 }
kenjiArai 0:5b88d5760320 319
kenjiArai 0:5b88d5760320 320 if( ctx->hmac_ctx != NULL )
kenjiArai 0:5b88d5760320 321 {
kenjiArai 0:5b88d5760320 322 mbedtls_platform_zeroize( ctx->hmac_ctx,
kenjiArai 0:5b88d5760320 323 2 * ctx->md_info->block_size );
kenjiArai 0:5b88d5760320 324 mbedtls_free( ctx->hmac_ctx );
kenjiArai 0:5b88d5760320 325 }
kenjiArai 0:5b88d5760320 326
kenjiArai 0:5b88d5760320 327 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
kenjiArai 0:5b88d5760320 328 }
kenjiArai 0:5b88d5760320 329
kenjiArai 0:5b88d5760320 330 int mbedtls_md_clone( mbedtls_md_context_t *dst,
kenjiArai 0:5b88d5760320 331 const mbedtls_md_context_t *src )
kenjiArai 0:5b88d5760320 332 {
kenjiArai 0:5b88d5760320 333 if( dst == NULL || dst->md_info == NULL ||
kenjiArai 0:5b88d5760320 334 src == NULL || src->md_info == NULL ||
kenjiArai 0:5b88d5760320 335 dst->md_info != src->md_info )
kenjiArai 0:5b88d5760320 336 {
kenjiArai 0:5b88d5760320 337 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 338 }
kenjiArai 0:5b88d5760320 339
kenjiArai 1:9db0e321a9f4 340 switch( src->md_info->type )
kenjiArai 1:9db0e321a9f4 341 {
kenjiArai 1:9db0e321a9f4 342 #if defined(MBEDTLS_MD2_C)
kenjiArai 1:9db0e321a9f4 343 case MBEDTLS_MD_MD2:
kenjiArai 1:9db0e321a9f4 344 mbedtls_md2_clone( dst->md_ctx, src->md_ctx );
kenjiArai 1:9db0e321a9f4 345 break;
kenjiArai 1:9db0e321a9f4 346 #endif
kenjiArai 1:9db0e321a9f4 347 #if defined(MBEDTLS_MD4_C)
kenjiArai 1:9db0e321a9f4 348 case MBEDTLS_MD_MD4:
kenjiArai 1:9db0e321a9f4 349 mbedtls_md4_clone( dst->md_ctx, src->md_ctx );
kenjiArai 1:9db0e321a9f4 350 break;
kenjiArai 1:9db0e321a9f4 351 #endif
kenjiArai 1:9db0e321a9f4 352 #if defined(MBEDTLS_MD5_C)
kenjiArai 1:9db0e321a9f4 353 case MBEDTLS_MD_MD5:
kenjiArai 1:9db0e321a9f4 354 mbedtls_md5_clone( dst->md_ctx, src->md_ctx );
kenjiArai 1:9db0e321a9f4 355 break;
kenjiArai 1:9db0e321a9f4 356 #endif
kenjiArai 1:9db0e321a9f4 357 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 1:9db0e321a9f4 358 case MBEDTLS_MD_RIPEMD160:
kenjiArai 1:9db0e321a9f4 359 mbedtls_ripemd160_clone( dst->md_ctx, src->md_ctx );
kenjiArai 1:9db0e321a9f4 360 break;
kenjiArai 1:9db0e321a9f4 361 #endif
kenjiArai 1:9db0e321a9f4 362 #if defined(MBEDTLS_SHA1_C)
kenjiArai 1:9db0e321a9f4 363 case MBEDTLS_MD_SHA1:
kenjiArai 1:9db0e321a9f4 364 mbedtls_sha1_clone( dst->md_ctx, src->md_ctx );
kenjiArai 1:9db0e321a9f4 365 break;
kenjiArai 1:9db0e321a9f4 366 #endif
kenjiArai 1:9db0e321a9f4 367 #if defined(MBEDTLS_SHA256_C)
kenjiArai 1:9db0e321a9f4 368 case MBEDTLS_MD_SHA224:
kenjiArai 1:9db0e321a9f4 369 case MBEDTLS_MD_SHA256:
kenjiArai 1:9db0e321a9f4 370 mbedtls_sha256_clone( dst->md_ctx, src->md_ctx );
kenjiArai 1:9db0e321a9f4 371 break;
kenjiArai 1:9db0e321a9f4 372 #endif
kenjiArai 1:9db0e321a9f4 373 #if defined(MBEDTLS_SHA512_C)
kenjiArai 1:9db0e321a9f4 374 case MBEDTLS_MD_SHA384:
kenjiArai 1:9db0e321a9f4 375 case MBEDTLS_MD_SHA512:
kenjiArai 1:9db0e321a9f4 376 mbedtls_sha512_clone( dst->md_ctx, src->md_ctx );
kenjiArai 1:9db0e321a9f4 377 break;
kenjiArai 1:9db0e321a9f4 378 #endif
kenjiArai 1:9db0e321a9f4 379 default:
kenjiArai 1:9db0e321a9f4 380 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 1:9db0e321a9f4 381 }
kenjiArai 0:5b88d5760320 382
kenjiArai 0:5b88d5760320 383 return( 0 );
kenjiArai 0:5b88d5760320 384 }
kenjiArai 0:5b88d5760320 385
kenjiArai 0:5b88d5760320 386 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
kenjiArai 0:5b88d5760320 387 int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
kenjiArai 0:5b88d5760320 388 {
kenjiArai 0:5b88d5760320 389 return mbedtls_md_setup( ctx, md_info, 1 );
kenjiArai 0:5b88d5760320 390 }
kenjiArai 0:5b88d5760320 391 #endif
kenjiArai 0:5b88d5760320 392
kenjiArai 1:9db0e321a9f4 393 #define ALLOC( type ) \
kenjiArai 1:9db0e321a9f4 394 do { \
kenjiArai 1:9db0e321a9f4 395 ctx->md_ctx = mbedtls_calloc( 1, sizeof( mbedtls_##type##_context ) ); \
kenjiArai 1:9db0e321a9f4 396 if( ctx->md_ctx == NULL ) \
kenjiArai 1:9db0e321a9f4 397 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); \
kenjiArai 1:9db0e321a9f4 398 mbedtls_##type##_init( ctx->md_ctx ); \
kenjiArai 1:9db0e321a9f4 399 } \
kenjiArai 1:9db0e321a9f4 400 while( 0 )
kenjiArai 1:9db0e321a9f4 401
kenjiArai 0:5b88d5760320 402 int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
kenjiArai 0:5b88d5760320 403 {
kenjiArai 0:5b88d5760320 404 if( md_info == NULL || ctx == NULL )
kenjiArai 0:5b88d5760320 405 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 406
kenjiArai 1:9db0e321a9f4 407 switch( md_info->type )
kenjiArai 1:9db0e321a9f4 408 {
kenjiArai 1:9db0e321a9f4 409 #if defined(MBEDTLS_MD2_C)
kenjiArai 1:9db0e321a9f4 410 case MBEDTLS_MD_MD2:
kenjiArai 1:9db0e321a9f4 411 ALLOC( md2 );
kenjiArai 1:9db0e321a9f4 412 break;
kenjiArai 1:9db0e321a9f4 413 #endif
kenjiArai 1:9db0e321a9f4 414 #if defined(MBEDTLS_MD4_C)
kenjiArai 1:9db0e321a9f4 415 case MBEDTLS_MD_MD4:
kenjiArai 1:9db0e321a9f4 416 ALLOC( md4 );
kenjiArai 1:9db0e321a9f4 417 break;
kenjiArai 1:9db0e321a9f4 418 #endif
kenjiArai 1:9db0e321a9f4 419 #if defined(MBEDTLS_MD5_C)
kenjiArai 1:9db0e321a9f4 420 case MBEDTLS_MD_MD5:
kenjiArai 1:9db0e321a9f4 421 ALLOC( md5 );
kenjiArai 1:9db0e321a9f4 422 break;
kenjiArai 1:9db0e321a9f4 423 #endif
kenjiArai 1:9db0e321a9f4 424 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 1:9db0e321a9f4 425 case MBEDTLS_MD_RIPEMD160:
kenjiArai 1:9db0e321a9f4 426 ALLOC( ripemd160 );
kenjiArai 1:9db0e321a9f4 427 break;
kenjiArai 1:9db0e321a9f4 428 #endif
kenjiArai 1:9db0e321a9f4 429 #if defined(MBEDTLS_SHA1_C)
kenjiArai 1:9db0e321a9f4 430 case MBEDTLS_MD_SHA1:
kenjiArai 1:9db0e321a9f4 431 ALLOC( sha1 );
kenjiArai 1:9db0e321a9f4 432 break;
kenjiArai 1:9db0e321a9f4 433 #endif
kenjiArai 1:9db0e321a9f4 434 #if defined(MBEDTLS_SHA256_C)
kenjiArai 1:9db0e321a9f4 435 case MBEDTLS_MD_SHA224:
kenjiArai 1:9db0e321a9f4 436 case MBEDTLS_MD_SHA256:
kenjiArai 1:9db0e321a9f4 437 ALLOC( sha256 );
kenjiArai 1:9db0e321a9f4 438 break;
kenjiArai 1:9db0e321a9f4 439 #endif
kenjiArai 1:9db0e321a9f4 440 #if defined(MBEDTLS_SHA512_C)
kenjiArai 1:9db0e321a9f4 441 case MBEDTLS_MD_SHA384:
kenjiArai 1:9db0e321a9f4 442 case MBEDTLS_MD_SHA512:
kenjiArai 1:9db0e321a9f4 443 ALLOC( sha512 );
kenjiArai 1:9db0e321a9f4 444 break;
kenjiArai 1:9db0e321a9f4 445 #endif
kenjiArai 1:9db0e321a9f4 446 default:
kenjiArai 1:9db0e321a9f4 447 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 1:9db0e321a9f4 448 }
kenjiArai 0:5b88d5760320 449
kenjiArai 0:5b88d5760320 450 if( hmac != 0 )
kenjiArai 0:5b88d5760320 451 {
kenjiArai 0:5b88d5760320 452 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
kenjiArai 0:5b88d5760320 453 if( ctx->hmac_ctx == NULL )
kenjiArai 0:5b88d5760320 454 {
kenjiArai 1:9db0e321a9f4 455 mbedtls_md_free( ctx );
kenjiArai 0:5b88d5760320 456 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
kenjiArai 0:5b88d5760320 457 }
kenjiArai 0:5b88d5760320 458 }
kenjiArai 0:5b88d5760320 459
kenjiArai 0:5b88d5760320 460 ctx->md_info = md_info;
kenjiArai 0:5b88d5760320 461
kenjiArai 0:5b88d5760320 462 return( 0 );
kenjiArai 0:5b88d5760320 463 }
kenjiArai 1:9db0e321a9f4 464 #undef ALLOC
kenjiArai 0:5b88d5760320 465
kenjiArai 0:5b88d5760320 466 int mbedtls_md_starts( mbedtls_md_context_t *ctx )
kenjiArai 0:5b88d5760320 467 {
kenjiArai 0:5b88d5760320 468 if( ctx == NULL || ctx->md_info == NULL )
kenjiArai 0:5b88d5760320 469 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 470
kenjiArai 1:9db0e321a9f4 471 switch( ctx->md_info->type )
kenjiArai 1:9db0e321a9f4 472 {
kenjiArai 1:9db0e321a9f4 473 #if defined(MBEDTLS_MD2_C)
kenjiArai 1:9db0e321a9f4 474 case MBEDTLS_MD_MD2:
kenjiArai 1:9db0e321a9f4 475 return( mbedtls_md2_starts_ret( ctx->md_ctx ) );
kenjiArai 1:9db0e321a9f4 476 #endif
kenjiArai 1:9db0e321a9f4 477 #if defined(MBEDTLS_MD4_C)
kenjiArai 1:9db0e321a9f4 478 case MBEDTLS_MD_MD4:
kenjiArai 1:9db0e321a9f4 479 return( mbedtls_md4_starts_ret( ctx->md_ctx ) );
kenjiArai 1:9db0e321a9f4 480 #endif
kenjiArai 1:9db0e321a9f4 481 #if defined(MBEDTLS_MD5_C)
kenjiArai 1:9db0e321a9f4 482 case MBEDTLS_MD_MD5:
kenjiArai 1:9db0e321a9f4 483 return( mbedtls_md5_starts_ret( ctx->md_ctx ) );
kenjiArai 1:9db0e321a9f4 484 #endif
kenjiArai 1:9db0e321a9f4 485 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 1:9db0e321a9f4 486 case MBEDTLS_MD_RIPEMD160:
kenjiArai 1:9db0e321a9f4 487 return( mbedtls_ripemd160_starts_ret( ctx->md_ctx ) );
kenjiArai 1:9db0e321a9f4 488 #endif
kenjiArai 1:9db0e321a9f4 489 #if defined(MBEDTLS_SHA1_C)
kenjiArai 1:9db0e321a9f4 490 case MBEDTLS_MD_SHA1:
kenjiArai 1:9db0e321a9f4 491 return( mbedtls_sha1_starts_ret( ctx->md_ctx ) );
kenjiArai 1:9db0e321a9f4 492 #endif
kenjiArai 1:9db0e321a9f4 493 #if defined(MBEDTLS_SHA256_C)
kenjiArai 1:9db0e321a9f4 494 case MBEDTLS_MD_SHA224:
kenjiArai 1:9db0e321a9f4 495 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 1 ) );
kenjiArai 1:9db0e321a9f4 496 case MBEDTLS_MD_SHA256:
kenjiArai 1:9db0e321a9f4 497 return( mbedtls_sha256_starts_ret( ctx->md_ctx, 0 ) );
kenjiArai 1:9db0e321a9f4 498 #endif
kenjiArai 1:9db0e321a9f4 499 #if defined(MBEDTLS_SHA512_C)
kenjiArai 1:9db0e321a9f4 500 case MBEDTLS_MD_SHA384:
kenjiArai 1:9db0e321a9f4 501 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 1 ) );
kenjiArai 1:9db0e321a9f4 502 case MBEDTLS_MD_SHA512:
kenjiArai 1:9db0e321a9f4 503 return( mbedtls_sha512_starts_ret( ctx->md_ctx, 0 ) );
kenjiArai 1:9db0e321a9f4 504 #endif
kenjiArai 1:9db0e321a9f4 505 default:
kenjiArai 1:9db0e321a9f4 506 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 1:9db0e321a9f4 507 }
kenjiArai 0:5b88d5760320 508 }
kenjiArai 0:5b88d5760320 509
kenjiArai 0:5b88d5760320 510 int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
kenjiArai 0:5b88d5760320 511 {
kenjiArai 0:5b88d5760320 512 if( ctx == NULL || ctx->md_info == NULL )
kenjiArai 0:5b88d5760320 513 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 514
kenjiArai 1:9db0e321a9f4 515 switch( ctx->md_info->type )
kenjiArai 1:9db0e321a9f4 516 {
kenjiArai 1:9db0e321a9f4 517 #if defined(MBEDTLS_MD2_C)
kenjiArai 1:9db0e321a9f4 518 case MBEDTLS_MD_MD2:
kenjiArai 1:9db0e321a9f4 519 return( mbedtls_md2_update_ret( ctx->md_ctx, input, ilen ) );
kenjiArai 1:9db0e321a9f4 520 #endif
kenjiArai 1:9db0e321a9f4 521 #if defined(MBEDTLS_MD4_C)
kenjiArai 1:9db0e321a9f4 522 case MBEDTLS_MD_MD4:
kenjiArai 1:9db0e321a9f4 523 return( mbedtls_md4_update_ret( ctx->md_ctx, input, ilen ) );
kenjiArai 1:9db0e321a9f4 524 #endif
kenjiArai 1:9db0e321a9f4 525 #if defined(MBEDTLS_MD5_C)
kenjiArai 1:9db0e321a9f4 526 case MBEDTLS_MD_MD5:
kenjiArai 1:9db0e321a9f4 527 return( mbedtls_md5_update_ret( ctx->md_ctx, input, ilen ) );
kenjiArai 1:9db0e321a9f4 528 #endif
kenjiArai 1:9db0e321a9f4 529 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 1:9db0e321a9f4 530 case MBEDTLS_MD_RIPEMD160:
kenjiArai 1:9db0e321a9f4 531 return( mbedtls_ripemd160_update_ret( ctx->md_ctx, input, ilen ) );
kenjiArai 1:9db0e321a9f4 532 #endif
kenjiArai 1:9db0e321a9f4 533 #if defined(MBEDTLS_SHA1_C)
kenjiArai 1:9db0e321a9f4 534 case MBEDTLS_MD_SHA1:
kenjiArai 1:9db0e321a9f4 535 return( mbedtls_sha1_update_ret( ctx->md_ctx, input, ilen ) );
kenjiArai 1:9db0e321a9f4 536 #endif
kenjiArai 1:9db0e321a9f4 537 #if defined(MBEDTLS_SHA256_C)
kenjiArai 1:9db0e321a9f4 538 case MBEDTLS_MD_SHA224:
kenjiArai 1:9db0e321a9f4 539 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
kenjiArai 1:9db0e321a9f4 540 case MBEDTLS_MD_SHA256:
kenjiArai 1:9db0e321a9f4 541 return( mbedtls_sha256_update_ret( ctx->md_ctx, input, ilen ) );
kenjiArai 1:9db0e321a9f4 542 #endif
kenjiArai 1:9db0e321a9f4 543 #if defined(MBEDTLS_SHA512_C)
kenjiArai 1:9db0e321a9f4 544 case MBEDTLS_MD_SHA384:
kenjiArai 1:9db0e321a9f4 545 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
kenjiArai 1:9db0e321a9f4 546 case MBEDTLS_MD_SHA512:
kenjiArai 1:9db0e321a9f4 547 return( mbedtls_sha512_update_ret( ctx->md_ctx, input, ilen ) );
kenjiArai 1:9db0e321a9f4 548 #endif
kenjiArai 1:9db0e321a9f4 549 default:
kenjiArai 1:9db0e321a9f4 550 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 1:9db0e321a9f4 551 }
kenjiArai 0:5b88d5760320 552 }
kenjiArai 0:5b88d5760320 553
kenjiArai 0:5b88d5760320 554 int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
kenjiArai 0:5b88d5760320 555 {
kenjiArai 0:5b88d5760320 556 if( ctx == NULL || ctx->md_info == NULL )
kenjiArai 0:5b88d5760320 557 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 558
kenjiArai 1:9db0e321a9f4 559 switch( ctx->md_info->type )
kenjiArai 1:9db0e321a9f4 560 {
kenjiArai 1:9db0e321a9f4 561 #if defined(MBEDTLS_MD2_C)
kenjiArai 1:9db0e321a9f4 562 case MBEDTLS_MD_MD2:
kenjiArai 1:9db0e321a9f4 563 return( mbedtls_md2_finish_ret( ctx->md_ctx, output ) );
kenjiArai 1:9db0e321a9f4 564 #endif
kenjiArai 1:9db0e321a9f4 565 #if defined(MBEDTLS_MD4_C)
kenjiArai 1:9db0e321a9f4 566 case MBEDTLS_MD_MD4:
kenjiArai 1:9db0e321a9f4 567 return( mbedtls_md4_finish_ret( ctx->md_ctx, output ) );
kenjiArai 1:9db0e321a9f4 568 #endif
kenjiArai 1:9db0e321a9f4 569 #if defined(MBEDTLS_MD5_C)
kenjiArai 1:9db0e321a9f4 570 case MBEDTLS_MD_MD5:
kenjiArai 1:9db0e321a9f4 571 return( mbedtls_md5_finish_ret( ctx->md_ctx, output ) );
kenjiArai 1:9db0e321a9f4 572 #endif
kenjiArai 1:9db0e321a9f4 573 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 1:9db0e321a9f4 574 case MBEDTLS_MD_RIPEMD160:
kenjiArai 1:9db0e321a9f4 575 return( mbedtls_ripemd160_finish_ret( ctx->md_ctx, output ) );
kenjiArai 1:9db0e321a9f4 576 #endif
kenjiArai 1:9db0e321a9f4 577 #if defined(MBEDTLS_SHA1_C)
kenjiArai 1:9db0e321a9f4 578 case MBEDTLS_MD_SHA1:
kenjiArai 1:9db0e321a9f4 579 return( mbedtls_sha1_finish_ret( ctx->md_ctx, output ) );
kenjiArai 1:9db0e321a9f4 580 #endif
kenjiArai 1:9db0e321a9f4 581 #if defined(MBEDTLS_SHA256_C)
kenjiArai 1:9db0e321a9f4 582 case MBEDTLS_MD_SHA224:
kenjiArai 1:9db0e321a9f4 583 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
kenjiArai 1:9db0e321a9f4 584 case MBEDTLS_MD_SHA256:
kenjiArai 1:9db0e321a9f4 585 return( mbedtls_sha256_finish_ret( ctx->md_ctx, output ) );
kenjiArai 1:9db0e321a9f4 586 #endif
kenjiArai 1:9db0e321a9f4 587 #if defined(MBEDTLS_SHA512_C)
kenjiArai 1:9db0e321a9f4 588 case MBEDTLS_MD_SHA384:
kenjiArai 1:9db0e321a9f4 589 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
kenjiArai 1:9db0e321a9f4 590 case MBEDTLS_MD_SHA512:
kenjiArai 1:9db0e321a9f4 591 return( mbedtls_sha512_finish_ret( ctx->md_ctx, output ) );
kenjiArai 1:9db0e321a9f4 592 #endif
kenjiArai 1:9db0e321a9f4 593 default:
kenjiArai 1:9db0e321a9f4 594 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 1:9db0e321a9f4 595 }
kenjiArai 0:5b88d5760320 596 }
kenjiArai 0:5b88d5760320 597
kenjiArai 0:5b88d5760320 598 int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
kenjiArai 0:5b88d5760320 599 unsigned char *output )
kenjiArai 0:5b88d5760320 600 {
kenjiArai 0:5b88d5760320 601 if( md_info == NULL )
kenjiArai 0:5b88d5760320 602 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 603
kenjiArai 1:9db0e321a9f4 604 switch( md_info->type )
kenjiArai 1:9db0e321a9f4 605 {
kenjiArai 1:9db0e321a9f4 606 #if defined(MBEDTLS_MD2_C)
kenjiArai 1:9db0e321a9f4 607 case MBEDTLS_MD_MD2:
kenjiArai 1:9db0e321a9f4 608 return( mbedtls_md2_ret( input, ilen, output ) );
kenjiArai 1:9db0e321a9f4 609 #endif
kenjiArai 1:9db0e321a9f4 610 #if defined(MBEDTLS_MD4_C)
kenjiArai 1:9db0e321a9f4 611 case MBEDTLS_MD_MD4:
kenjiArai 1:9db0e321a9f4 612 return( mbedtls_md4_ret( input, ilen, output ) );
kenjiArai 1:9db0e321a9f4 613 #endif
kenjiArai 1:9db0e321a9f4 614 #if defined(MBEDTLS_MD5_C)
kenjiArai 1:9db0e321a9f4 615 case MBEDTLS_MD_MD5:
kenjiArai 1:9db0e321a9f4 616 return( mbedtls_md5_ret( input, ilen, output ) );
kenjiArai 1:9db0e321a9f4 617 #endif
kenjiArai 1:9db0e321a9f4 618 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 1:9db0e321a9f4 619 case MBEDTLS_MD_RIPEMD160:
kenjiArai 1:9db0e321a9f4 620 return( mbedtls_ripemd160_ret( input, ilen, output ) );
kenjiArai 1:9db0e321a9f4 621 #endif
kenjiArai 1:9db0e321a9f4 622 #if defined(MBEDTLS_SHA1_C)
kenjiArai 1:9db0e321a9f4 623 case MBEDTLS_MD_SHA1:
kenjiArai 1:9db0e321a9f4 624 return( mbedtls_sha1_ret( input, ilen, output ) );
kenjiArai 1:9db0e321a9f4 625 #endif
kenjiArai 1:9db0e321a9f4 626 #if defined(MBEDTLS_SHA256_C)
kenjiArai 1:9db0e321a9f4 627 case MBEDTLS_MD_SHA224:
kenjiArai 1:9db0e321a9f4 628 return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
kenjiArai 1:9db0e321a9f4 629 case MBEDTLS_MD_SHA256:
kenjiArai 1:9db0e321a9f4 630 return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
kenjiArai 1:9db0e321a9f4 631 #endif
kenjiArai 1:9db0e321a9f4 632 #if defined(MBEDTLS_SHA512_C)
kenjiArai 1:9db0e321a9f4 633 case MBEDTLS_MD_SHA384:
kenjiArai 1:9db0e321a9f4 634 return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
kenjiArai 1:9db0e321a9f4 635 case MBEDTLS_MD_SHA512:
kenjiArai 1:9db0e321a9f4 636 return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
kenjiArai 1:9db0e321a9f4 637 #endif
kenjiArai 1:9db0e321a9f4 638 default:
kenjiArai 1:9db0e321a9f4 639 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 1:9db0e321a9f4 640 }
kenjiArai 0:5b88d5760320 641 }
kenjiArai 0:5b88d5760320 642
kenjiArai 0:5b88d5760320 643 #if defined(MBEDTLS_FS_IO)
kenjiArai 0:5b88d5760320 644 int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
kenjiArai 0:5b88d5760320 645 {
kenjiArai 0:5b88d5760320 646 int ret;
kenjiArai 0:5b88d5760320 647 FILE *f;
kenjiArai 0:5b88d5760320 648 size_t n;
kenjiArai 0:5b88d5760320 649 mbedtls_md_context_t ctx;
kenjiArai 0:5b88d5760320 650 unsigned char buf[1024];
kenjiArai 0:5b88d5760320 651
kenjiArai 0:5b88d5760320 652 if( md_info == NULL )
kenjiArai 0:5b88d5760320 653 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 654
kenjiArai 0:5b88d5760320 655 if( ( f = fopen( path, "rb" ) ) == NULL )
kenjiArai 0:5b88d5760320 656 return( MBEDTLS_ERR_MD_FILE_IO_ERROR );
kenjiArai 0:5b88d5760320 657
kenjiArai 0:5b88d5760320 658 mbedtls_md_init( &ctx );
kenjiArai 0:5b88d5760320 659
kenjiArai 0:5b88d5760320 660 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
kenjiArai 0:5b88d5760320 661 goto cleanup;
kenjiArai 0:5b88d5760320 662
kenjiArai 1:9db0e321a9f4 663 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
kenjiArai 0:5b88d5760320 664 goto cleanup;
kenjiArai 0:5b88d5760320 665
kenjiArai 0:5b88d5760320 666 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
kenjiArai 1:9db0e321a9f4 667 if( ( ret = mbedtls_md_update( &ctx, buf, n ) ) != 0 )
kenjiArai 0:5b88d5760320 668 goto cleanup;
kenjiArai 0:5b88d5760320 669
kenjiArai 0:5b88d5760320 670 if( ferror( f ) != 0 )
kenjiArai 0:5b88d5760320 671 ret = MBEDTLS_ERR_MD_FILE_IO_ERROR;
kenjiArai 0:5b88d5760320 672 else
kenjiArai 1:9db0e321a9f4 673 ret = mbedtls_md_finish( &ctx, output );
kenjiArai 0:5b88d5760320 674
kenjiArai 0:5b88d5760320 675 cleanup:
kenjiArai 0:5b88d5760320 676 mbedtls_platform_zeroize( buf, sizeof( buf ) );
kenjiArai 0:5b88d5760320 677 fclose( f );
kenjiArai 0:5b88d5760320 678 mbedtls_md_free( &ctx );
kenjiArai 0:5b88d5760320 679
kenjiArai 0:5b88d5760320 680 return( ret );
kenjiArai 0:5b88d5760320 681 }
kenjiArai 0:5b88d5760320 682 #endif /* MBEDTLS_FS_IO */
kenjiArai 0:5b88d5760320 683
kenjiArai 0:5b88d5760320 684 int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
kenjiArai 0:5b88d5760320 685 {
kenjiArai 0:5b88d5760320 686 int ret;
kenjiArai 0:5b88d5760320 687 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
kenjiArai 0:5b88d5760320 688 unsigned char *ipad, *opad;
kenjiArai 0:5b88d5760320 689 size_t i;
kenjiArai 0:5b88d5760320 690
kenjiArai 0:5b88d5760320 691 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
kenjiArai 0:5b88d5760320 692 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 693
kenjiArai 0:5b88d5760320 694 if( keylen > (size_t) ctx->md_info->block_size )
kenjiArai 0:5b88d5760320 695 {
kenjiArai 1:9db0e321a9f4 696 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
kenjiArai 0:5b88d5760320 697 goto cleanup;
kenjiArai 1:9db0e321a9f4 698 if( ( ret = mbedtls_md_update( ctx, key, keylen ) ) != 0 )
kenjiArai 0:5b88d5760320 699 goto cleanup;
kenjiArai 1:9db0e321a9f4 700 if( ( ret = mbedtls_md_finish( ctx, sum ) ) != 0 )
kenjiArai 0:5b88d5760320 701 goto cleanup;
kenjiArai 0:5b88d5760320 702
kenjiArai 0:5b88d5760320 703 keylen = ctx->md_info->size;
kenjiArai 0:5b88d5760320 704 key = sum;
kenjiArai 0:5b88d5760320 705 }
kenjiArai 0:5b88d5760320 706
kenjiArai 0:5b88d5760320 707 ipad = (unsigned char *) ctx->hmac_ctx;
kenjiArai 0:5b88d5760320 708 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
kenjiArai 0:5b88d5760320 709
kenjiArai 0:5b88d5760320 710 memset( ipad, 0x36, ctx->md_info->block_size );
kenjiArai 0:5b88d5760320 711 memset( opad, 0x5C, ctx->md_info->block_size );
kenjiArai 0:5b88d5760320 712
kenjiArai 0:5b88d5760320 713 for( i = 0; i < keylen; i++ )
kenjiArai 0:5b88d5760320 714 {
kenjiArai 0:5b88d5760320 715 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
kenjiArai 0:5b88d5760320 716 opad[i] = (unsigned char)( opad[i] ^ key[i] );
kenjiArai 0:5b88d5760320 717 }
kenjiArai 0:5b88d5760320 718
kenjiArai 1:9db0e321a9f4 719 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
kenjiArai 0:5b88d5760320 720 goto cleanup;
kenjiArai 1:9db0e321a9f4 721 if( ( ret = mbedtls_md_update( ctx, ipad,
kenjiArai 1:9db0e321a9f4 722 ctx->md_info->block_size ) ) != 0 )
kenjiArai 0:5b88d5760320 723 goto cleanup;
kenjiArai 0:5b88d5760320 724
kenjiArai 0:5b88d5760320 725 cleanup:
kenjiArai 0:5b88d5760320 726 mbedtls_platform_zeroize( sum, sizeof( sum ) );
kenjiArai 0:5b88d5760320 727
kenjiArai 0:5b88d5760320 728 return( ret );
kenjiArai 0:5b88d5760320 729 }
kenjiArai 0:5b88d5760320 730
kenjiArai 0:5b88d5760320 731 int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
kenjiArai 0:5b88d5760320 732 {
kenjiArai 0:5b88d5760320 733 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
kenjiArai 0:5b88d5760320 734 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 735
kenjiArai 1:9db0e321a9f4 736 return( mbedtls_md_update( ctx, input, ilen ) );
kenjiArai 0:5b88d5760320 737 }
kenjiArai 0:5b88d5760320 738
kenjiArai 0:5b88d5760320 739 int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
kenjiArai 0:5b88d5760320 740 {
kenjiArai 0:5b88d5760320 741 int ret;
kenjiArai 0:5b88d5760320 742 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
kenjiArai 0:5b88d5760320 743 unsigned char *opad;
kenjiArai 0:5b88d5760320 744
kenjiArai 0:5b88d5760320 745 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
kenjiArai 0:5b88d5760320 746 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 747
kenjiArai 0:5b88d5760320 748 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
kenjiArai 0:5b88d5760320 749
kenjiArai 1:9db0e321a9f4 750 if( ( ret = mbedtls_md_finish( ctx, tmp ) ) != 0 )
kenjiArai 0:5b88d5760320 751 return( ret );
kenjiArai 1:9db0e321a9f4 752 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
kenjiArai 0:5b88d5760320 753 return( ret );
kenjiArai 1:9db0e321a9f4 754 if( ( ret = mbedtls_md_update( ctx, opad,
kenjiArai 1:9db0e321a9f4 755 ctx->md_info->block_size ) ) != 0 )
kenjiArai 0:5b88d5760320 756 return( ret );
kenjiArai 1:9db0e321a9f4 757 if( ( ret = mbedtls_md_update( ctx, tmp,
kenjiArai 1:9db0e321a9f4 758 ctx->md_info->size ) ) != 0 )
kenjiArai 0:5b88d5760320 759 return( ret );
kenjiArai 1:9db0e321a9f4 760 return( mbedtls_md_finish( ctx, output ) );
kenjiArai 0:5b88d5760320 761 }
kenjiArai 0:5b88d5760320 762
kenjiArai 0:5b88d5760320 763 int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
kenjiArai 0:5b88d5760320 764 {
kenjiArai 0:5b88d5760320 765 int ret;
kenjiArai 0:5b88d5760320 766 unsigned char *ipad;
kenjiArai 0:5b88d5760320 767
kenjiArai 0:5b88d5760320 768 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
kenjiArai 0:5b88d5760320 769 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 770
kenjiArai 0:5b88d5760320 771 ipad = (unsigned char *) ctx->hmac_ctx;
kenjiArai 0:5b88d5760320 772
kenjiArai 1:9db0e321a9f4 773 if( ( ret = mbedtls_md_starts( ctx ) ) != 0 )
kenjiArai 0:5b88d5760320 774 return( ret );
kenjiArai 1:9db0e321a9f4 775 return( mbedtls_md_update( ctx, ipad, ctx->md_info->block_size ) );
kenjiArai 0:5b88d5760320 776 }
kenjiArai 0:5b88d5760320 777
kenjiArai 0:5b88d5760320 778 int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
kenjiArai 0:5b88d5760320 779 const unsigned char *key, size_t keylen,
kenjiArai 0:5b88d5760320 780 const unsigned char *input, size_t ilen,
kenjiArai 0:5b88d5760320 781 unsigned char *output )
kenjiArai 0:5b88d5760320 782 {
kenjiArai 0:5b88d5760320 783 mbedtls_md_context_t ctx;
kenjiArai 0:5b88d5760320 784 int ret;
kenjiArai 0:5b88d5760320 785
kenjiArai 0:5b88d5760320 786 if( md_info == NULL )
kenjiArai 0:5b88d5760320 787 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 788
kenjiArai 0:5b88d5760320 789 mbedtls_md_init( &ctx );
kenjiArai 0:5b88d5760320 790
kenjiArai 0:5b88d5760320 791 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
kenjiArai 0:5b88d5760320 792 goto cleanup;
kenjiArai 0:5b88d5760320 793
kenjiArai 0:5b88d5760320 794 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
kenjiArai 0:5b88d5760320 795 goto cleanup;
kenjiArai 0:5b88d5760320 796 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
kenjiArai 0:5b88d5760320 797 goto cleanup;
kenjiArai 0:5b88d5760320 798 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
kenjiArai 0:5b88d5760320 799 goto cleanup;
kenjiArai 0:5b88d5760320 800
kenjiArai 0:5b88d5760320 801 cleanup:
kenjiArai 0:5b88d5760320 802 mbedtls_md_free( &ctx );
kenjiArai 0:5b88d5760320 803
kenjiArai 0:5b88d5760320 804 return( ret );
kenjiArai 0:5b88d5760320 805 }
kenjiArai 0:5b88d5760320 806
kenjiArai 0:5b88d5760320 807 int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
kenjiArai 0:5b88d5760320 808 {
kenjiArai 0:5b88d5760320 809 if( ctx == NULL || ctx->md_info == NULL )
kenjiArai 0:5b88d5760320 810 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 811
kenjiArai 1:9db0e321a9f4 812 switch( ctx->md_info->type )
kenjiArai 1:9db0e321a9f4 813 {
kenjiArai 1:9db0e321a9f4 814 #if defined(MBEDTLS_MD2_C)
kenjiArai 1:9db0e321a9f4 815 case MBEDTLS_MD_MD2:
kenjiArai 1:9db0e321a9f4 816 return( mbedtls_internal_md2_process( ctx->md_ctx ) );
kenjiArai 1:9db0e321a9f4 817 #endif
kenjiArai 1:9db0e321a9f4 818 #if defined(MBEDTLS_MD4_C)
kenjiArai 1:9db0e321a9f4 819 case MBEDTLS_MD_MD4:
kenjiArai 1:9db0e321a9f4 820 return( mbedtls_internal_md4_process( ctx->md_ctx, data ) );
kenjiArai 1:9db0e321a9f4 821 #endif
kenjiArai 1:9db0e321a9f4 822 #if defined(MBEDTLS_MD5_C)
kenjiArai 1:9db0e321a9f4 823 case MBEDTLS_MD_MD5:
kenjiArai 1:9db0e321a9f4 824 return( mbedtls_internal_md5_process( ctx->md_ctx, data ) );
kenjiArai 1:9db0e321a9f4 825 #endif
kenjiArai 1:9db0e321a9f4 826 #if defined(MBEDTLS_RIPEMD160_C)
kenjiArai 1:9db0e321a9f4 827 case MBEDTLS_MD_RIPEMD160:
kenjiArai 1:9db0e321a9f4 828 return( mbedtls_internal_ripemd160_process( ctx->md_ctx, data ) );
kenjiArai 1:9db0e321a9f4 829 #endif
kenjiArai 1:9db0e321a9f4 830 #if defined(MBEDTLS_SHA1_C)
kenjiArai 1:9db0e321a9f4 831 case MBEDTLS_MD_SHA1:
kenjiArai 1:9db0e321a9f4 832 return( mbedtls_internal_sha1_process( ctx->md_ctx, data ) );
kenjiArai 1:9db0e321a9f4 833 #endif
kenjiArai 1:9db0e321a9f4 834 #if defined(MBEDTLS_SHA256_C)
kenjiArai 1:9db0e321a9f4 835 case MBEDTLS_MD_SHA224:
kenjiArai 1:9db0e321a9f4 836 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
kenjiArai 1:9db0e321a9f4 837 case MBEDTLS_MD_SHA256:
kenjiArai 1:9db0e321a9f4 838 return( mbedtls_internal_sha256_process( ctx->md_ctx, data ) );
kenjiArai 1:9db0e321a9f4 839 #endif
kenjiArai 1:9db0e321a9f4 840 #if defined(MBEDTLS_SHA512_C)
kenjiArai 1:9db0e321a9f4 841 case MBEDTLS_MD_SHA384:
kenjiArai 1:9db0e321a9f4 842 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
kenjiArai 1:9db0e321a9f4 843 case MBEDTLS_MD_SHA512:
kenjiArai 1:9db0e321a9f4 844 return( mbedtls_internal_sha512_process( ctx->md_ctx, data ) );
kenjiArai 1:9db0e321a9f4 845 #endif
kenjiArai 1:9db0e321a9f4 846 default:
kenjiArai 1:9db0e321a9f4 847 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
kenjiArai 1:9db0e321a9f4 848 }
kenjiArai 0:5b88d5760320 849 }
kenjiArai 0:5b88d5760320 850
kenjiArai 0:5b88d5760320 851 unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
kenjiArai 0:5b88d5760320 852 {
kenjiArai 0:5b88d5760320 853 if( md_info == NULL )
kenjiArai 0:5b88d5760320 854 return( 0 );
kenjiArai 0:5b88d5760320 855
kenjiArai 0:5b88d5760320 856 return md_info->size;
kenjiArai 0:5b88d5760320 857 }
kenjiArai 0:5b88d5760320 858
kenjiArai 0:5b88d5760320 859 mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
kenjiArai 0:5b88d5760320 860 {
kenjiArai 0:5b88d5760320 861 if( md_info == NULL )
kenjiArai 0:5b88d5760320 862 return( MBEDTLS_MD_NONE );
kenjiArai 0:5b88d5760320 863
kenjiArai 0:5b88d5760320 864 return md_info->type;
kenjiArai 0:5b88d5760320 865 }
kenjiArai 0:5b88d5760320 866
kenjiArai 0:5b88d5760320 867 const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
kenjiArai 0:5b88d5760320 868 {
kenjiArai 0:5b88d5760320 869 if( md_info == NULL )
kenjiArai 0:5b88d5760320 870 return( NULL );
kenjiArai 0:5b88d5760320 871
kenjiArai 0:5b88d5760320 872 return md_info->name;
kenjiArai 0:5b88d5760320 873 }
kenjiArai 0:5b88d5760320 874
kenjiArai 0:5b88d5760320 875 #endif /* MBEDTLS_MD_C */