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