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