blob: c6a118d92c8e552f2a5a9064284c53a9f5d03460 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-2 compliant SHA-256 implementation
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
20 * The SHA-256 Secure Hash Standard was published by NIST in 2002.
21 *
22 * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
23 */
24
Jerry Yua135dee2023-02-16 16:56:22 +080025#if defined(__aarch64__) && !defined(__ARM_FEATURE_CRYPTO) && \
Jerry Yu6f86c192023-03-13 11:03:40 +080026 defined(__clang__) && __clang_major__ >= 4
Jerry Yua135dee2023-02-16 16:56:22 +080027/* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged.
28 *
Jerry Yufc2e1282023-02-27 11:16:56 +080029 * The intrinsic declaration are guarded by predefined ACLE macros in clang:
30 * these are normally only enabled by the -march option on the command line.
31 * By defining the macros ourselves we gain access to those declarations without
32 * requiring -march on the command line.
Jerry Yu4d786a72023-02-22 11:01:07 +080033 *
Jerry Yufc2e1282023-02-27 11:16:56 +080034 * `arm_neon.h` could be included by any header file, so we put these defines
35 * at the top of this file, before any includes.
Jerry Yua135dee2023-02-16 16:56:22 +080036 */
37#define __ARM_FEATURE_CRYPTO 1
Jerry Yuae129c32023-03-03 15:55:56 +080038/* See: https://arm-software.github.io/acle/main/acle.html#cryptographic-extensions
39 *
Jerry Yu490bf082023-03-06 15:21:44 +080040 * `__ARM_FEATURE_CRYPTO` is deprecated, but we need to continue to specify it
41 * for older compilers.
Jerry Yuae129c32023-03-03 15:55:56 +080042 */
43#define __ARM_FEATURE_SHA2 1
Dave Rodgmandb6ab242023-03-14 16:03:57 +000044#define MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG
Jerry Yu490bf082023-03-06 15:21:44 +080045#endif
Jerry Yua135dee2023-02-16 16:56:22 +080046
Dave Rodgman7ed619d2023-10-05 09:39:56 +010047/* Ensure that SIG_SETMASK is defined when -std=c99 is used. */
48#define _GNU_SOURCE
49
Gilles Peskinedb09ef62020-06-03 01:43:33 +020050#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Valerio Settia3f99592022-12-14 10:56:54 +010052#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/sha256.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050055#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000056#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <string.h>
59
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010061
Dave Rodgmancc5bf492023-10-03 18:02:56 +010062#if defined(MBEDTLS_ARCH_IS_ARMV8)
Jerry Yu08933d32023-04-27 18:28:00 +080063
Dave Rodgman3ba9ce32023-10-05 09:58:33 +010064# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
65 defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
66# ifdef __ARM_NEON
67# include <arm_neon.h>
68# else
69# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
70# warning "Target does not support NEON instructions"
71# undef MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
72# else
73# error "Target does not support NEON instructions"
74# endif
75# endif
Jerry Yu6b00f5a2023-05-04 16:30:21 +080076# endif
77
Dave Rodgman3ba9ce32023-10-05 09:58:33 +010078# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
79 defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
Dave Rodgman790370b2023-10-05 11:01:31 +010080/* *INDENT-OFF* */
81
Dave Rodgman793e2642023-10-04 17:36:20 +010082# if !defined(__ARM_FEATURE_CRYPTO) || defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG)
Jerry Yub1d06bb2023-05-05 14:05:07 +080083# if defined(__ARMCOMPILER_VERSION)
84# if __ARMCOMPILER_VERSION <= 6090000
85# error "Must use minimum -march=armv8-a+crypto for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
86# endif
87# pragma clang attribute push (__attribute__((target("sha2"))), apply_to=function)
88# define MBEDTLS_POP_TARGET_PRAGMA
89# elif defined(__clang__)
Jerry Yu383cbf42023-02-16 15:16:43 +080090# if __clang_major__ < 4
91# error "A more recent Clang is required for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
Jerry Yu64e5d4a2023-02-15 11:46:57 +080092# endif
Jerry Yub1d06bb2023-05-05 14:05:07 +080093# pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function)
Jerry Yu64e5d4a2023-02-15 11:46:57 +080094# define MBEDTLS_POP_TARGET_PRAGMA
95# elif defined(__GNUC__)
Tom Cosgrovec15a2b92023-03-08 12:55:48 +000096 /* FIXME: GCC 5 claims to support Armv8 Crypto Extensions, but some
97 * intrinsics are missing. Missing intrinsics could be worked around.
Jerry Yu8ae6a012023-02-16 15:16:20 +080098 */
99# if __GNUC__ < 6
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800100# error "A more recent GCC is required for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
101# else
Jerry Yu2f2c0492023-02-16 14:24:46 +0800102# pragma GCC push_options
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800103# pragma GCC target ("arch=armv8-a+crypto")
Jerry Yu2f2c0492023-02-16 14:24:46 +0800104# define MBEDTLS_POP_TARGET_PRAGMA
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800105# endif
106# else
107# error "Only GCC and Clang supported for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
108# endif
Jerry Yu35f2b262023-02-15 11:35:55 +0800109# endif
110/* *INDENT-ON* */
Jerry Yu08933d32023-04-27 18:28:00 +0800111
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000112# endif
Tom Cosgroveb7f5b972022-03-15 11:26:55 +0000113# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
114# if defined(__unix__)
115# if defined(__linux__)
Gilles Peskine449bd832023-01-11 14:50:10 +0100116/* Our preferred method of detection is getauxval() */
Tom Cosgroveb7f5b972022-03-15 11:26:55 +0000117# include <sys/auxv.h>
118# endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100119/* Use SIGILL on Unix, and fall back to it on Linux */
Tom Cosgroveb7f5b972022-03-15 11:26:55 +0000120# include <signal.h>
121# endif
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000122# endif
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000123#elif defined(_M_ARM64)
124# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000126# include <arm64_neon.h>
127# endif
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000128#else
129# undef MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY
130# undef MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
131#endif
132
133#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
134/*
135 * Capability detection code comes early, so we can disable
136 * MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT if no detection mechanism found
137 */
138#if defined(HWCAP_SHA2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100139static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000140{
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return (getauxval(AT_HWCAP) & HWCAP_SHA2) ? 1 : 0;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000142}
143#elif defined(__APPLE__)
Gilles Peskine449bd832023-01-11 14:50:10 +0100144static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000145{
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return 1;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000147}
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000148#elif defined(_M_ARM64)
149#define WIN32_LEAN_AND_MEAN
150#include <Windows.h>
151#include <processthreadsapi.h>
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000154{
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) ?
156 1 : 0;
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000157}
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000158#elif defined(__unix__) && defined(SIG_SETMASK)
159/* Detection with SIGILL, setjmp() and longjmp() */
160#include <signal.h>
161#include <setjmp.h>
162
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000163static jmp_buf return_from_sigill;
164
165/*
166 * A64 SHA256 support detection via SIGILL
167 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168static void sigill_handler(int signal)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000169{
170 (void) signal;
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 longjmp(return_from_sigill, 1);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000172}
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000175{
176 struct sigaction old_action, new_action;
177
178 sigset_t old_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (sigprocmask(0, NULL, &old_mask)) {
180 return 0;
181 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 sigemptyset(&new_action.sa_mask);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000184 new_action.sa_flags = 0;
185 new_action.sa_handler = sigill_handler;
186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 sigaction(SIGILL, &new_action, &old_action);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000188
189 static int ret = 0;
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (setjmp(return_from_sigill) == 0) { /* First return only */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000192 /* If this traps, we will return a second time from setjmp() with 1 */
Dave Rodgman7ed619d2023-10-05 09:39:56 +0100193#if defined(MBEDTLS_ARCH_IS_ARM64)
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 asm ("sha256h q0, q0, v0.4s" : : : "v0");
Dave Rodgman7ed619d2023-10-05 09:39:56 +0100195#else
196 asm ("sha256h.32 q0, q0, q0" : : : "q0");
197#endif
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000198 ret = 1;
199 }
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 sigaction(SIGILL, &old_action, NULL);
202 sigprocmask(SIG_SETMASK, &old_mask, NULL);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return ret;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000205}
206#else
207#warning "No mechanism to detect A64_CRYPTO found, using C code only"
208#undef MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
209#endif /* HWCAP_SHA2, __APPLE__, __unix__ && SIG_SETMASK */
210
211#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT */
212
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +0200213#if !defined(MBEDTLS_SHA256_ALT)
214
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000215#define SHA256_BLOCK_SIZE 64
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +0200218{
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 memset(ctx, 0, sizeof(mbedtls_sha256_context));
Paul Bakker5b4af392014-06-26 12:09:34 +0200220}
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +0200223{
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (ctx == NULL) {
Paul Bakker5b4af392014-06-26 12:09:34 +0200225 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 }
Paul Bakker5b4af392014-06-26 12:09:34 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha256_context));
Paul Bakker5b4af392014-06-26 12:09:34 +0200229}
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
232 const mbedtls_sha256_context *src)
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200233{
234 *dst = *src;
235}
236
Paul Bakker5121ce52009-01-03 21:22:43 +0000237/*
238 * SHA-256 context setup
239 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240int mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000241{
Valerio Settia3f99592022-12-14 10:56:54 +0100242#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (is224 != 0 && is224 != 1) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100244 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 }
Valerio Settia3f99592022-12-14 10:56:54 +0100246#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (is224 != 0) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100248 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 }
Valerio Settia3f99592022-12-14 10:56:54 +0100250#else /* defined MBEDTLS_SHA224_C only */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100252 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 }
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200254#endif
255
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 ctx->total[0] = 0;
257 ctx->total[1] = 0;
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100260#if defined(MBEDTLS_SHA256_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 ctx->state[0] = 0x6A09E667;
262 ctx->state[1] = 0xBB67AE85;
263 ctx->state[2] = 0x3C6EF372;
264 ctx->state[3] = 0xA54FF53A;
265 ctx->state[4] = 0x510E527F;
266 ctx->state[5] = 0x9B05688C;
267 ctx->state[6] = 0x1F83D9AB;
268 ctx->state[7] = 0x5BE0CD19;
Valerio Settia3f99592022-12-14 10:56:54 +0100269#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 } else {
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200271#if defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 ctx->state[0] = 0xC1059ED8;
273 ctx->state[1] = 0x367CD507;
274 ctx->state[2] = 0x3070DD17;
275 ctx->state[3] = 0xF70E5939;
276 ctx->state[4] = 0xFFC00B31;
277 ctx->state[5] = 0x68581511;
278 ctx->state[6] = 0x64F98FA7;
279 ctx->state[7] = 0xBEFA4FA4;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200280#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 }
282
Valerio Settia3f99592022-12-14 10:56:54 +0100283#if defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 ctx->is224 = is224;
Valerio Settia3f99592022-12-14 10:56:54 +0100285#endif
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000288}
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#if !defined(MBEDTLS_SHA256_PROCESS_ALT)
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200291static const uint32_t K[] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000292{
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200293 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
294 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
295 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
296 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
297 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
298 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
299 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
300 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
301 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
302 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
303 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
304 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
305 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
306 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
307 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
308 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
309};
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000311#endif
312
313#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
314 defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
315
316#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
317# define mbedtls_internal_sha256_process_many_a64_crypto mbedtls_internal_sha256_process_many
318# define mbedtls_internal_sha256_process_a64_crypto mbedtls_internal_sha256_process
319#endif
320
321static size_t mbedtls_internal_sha256_process_many_a64_crypto(
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 mbedtls_sha256_context *ctx, const uint8_t *msg, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000323{
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 uint32x4_t abcd = vld1q_u32(&ctx->state[0]);
325 uint32x4_t efgh = vld1q_u32(&ctx->state[4]);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000326
327 size_t processed = 0;
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 for (;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000330 len >= SHA256_BLOCK_SIZE;
331 processed += SHA256_BLOCK_SIZE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 msg += SHA256_BLOCK_SIZE,
333 len -= SHA256_BLOCK_SIZE) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000334 uint32x4_t tmp, abcd_prev;
335
336 uint32x4_t abcd_orig = abcd;
337 uint32x4_t efgh_orig = efgh;
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 uint32x4_t sched0 = (uint32x4_t) vld1q_u8(msg + 16 * 0);
340 uint32x4_t sched1 = (uint32x4_t) vld1q_u8(msg + 16 * 1);
341 uint32x4_t sched2 = (uint32x4_t) vld1q_u8(msg + 16 * 2);
342 uint32x4_t sched3 = (uint32x4_t) vld1q_u8(msg + 16 * 3);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000343
344#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ /* Will be true if not defined */
345 /* Untested on BE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 sched0 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched0)));
347 sched1 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched1)));
348 sched2 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched2)));
349 sched3 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched3)));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000350#endif
351
352 /* Rounds 0 to 3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 tmp = vaddq_u32(sched0, vld1q_u32(&K[0]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000354 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
356 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000357
358 /* Rounds 4 to 7 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 tmp = vaddq_u32(sched1, vld1q_u32(&K[4]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000360 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
362 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000363
364 /* Rounds 8 to 11 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 tmp = vaddq_u32(sched2, vld1q_u32(&K[8]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000366 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
368 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000369
370 /* Rounds 12 to 15 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 tmp = vaddq_u32(sched3, vld1q_u32(&K[12]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000372 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
374 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 for (int t = 16; t < 64; t += 16) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000377 /* Rounds t to t + 3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 sched0 = vsha256su1q_u32(vsha256su0q_u32(sched0, sched1), sched2, sched3);
379 tmp = vaddq_u32(sched0, vld1q_u32(&K[t]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000380 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
382 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000383
384 /* Rounds t + 4 to t + 7 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 sched1 = vsha256su1q_u32(vsha256su0q_u32(sched1, sched2), sched3, sched0);
386 tmp = vaddq_u32(sched1, vld1q_u32(&K[t + 4]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000387 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
389 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000390
391 /* Rounds t + 8 to t + 11 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 sched2 = vsha256su1q_u32(vsha256su0q_u32(sched2, sched3), sched0, sched1);
393 tmp = vaddq_u32(sched2, vld1q_u32(&K[t + 8]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000394 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
396 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000397
398 /* Rounds t + 12 to t + 15 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 sched3 = vsha256su1q_u32(vsha256su0q_u32(sched3, sched0), sched1, sched2);
400 tmp = vaddq_u32(sched3, vld1q_u32(&K[t + 12]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000401 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
403 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000404 }
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 abcd = vaddq_u32(abcd, abcd_orig);
407 efgh = vaddq_u32(efgh, efgh_orig);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000408 }
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 vst1q_u32(&ctx->state[0], abcd);
411 vst1q_u32(&ctx->state[4], efgh);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return processed;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000414}
415
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100416#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
417/*
418 * This function is for internal use only if we are building both C and A64
419 * versions, otherwise it is renamed to be the public mbedtls_internal_sha256_process()
420 */
421static
422#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100423int mbedtls_internal_sha256_process_a64_crypto(mbedtls_sha256_context *ctx,
424 const unsigned char data[SHA256_BLOCK_SIZE])
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000425{
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return (mbedtls_internal_sha256_process_many_a64_crypto(ctx, data,
427 SHA256_BLOCK_SIZE) ==
428 SHA256_BLOCK_SIZE) ? 0 : -1;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000429}
430
Tom Cosgroveef2aa0e2023-06-09 11:29:50 +0100431#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT || MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
432
Jerry Yu92fc5382023-02-16 11:17:11 +0800433#if defined(MBEDTLS_POP_TARGET_PRAGMA)
Jerry Yu2f2c0492023-02-16 14:24:46 +0800434#if defined(__clang__)
Jerry Yu92fc5382023-02-16 11:17:11 +0800435#pragma clang attribute pop
Jerry Yu2f2c0492023-02-16 14:24:46 +0800436#elif defined(__GNUC__)
437#pragma GCC pop_options
438#endif
Jerry Yu92fc5382023-02-16 11:17:11 +0800439#undef MBEDTLS_POP_TARGET_PRAGMA
440#endif
441
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000442#if !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
443#define mbedtls_internal_sha256_process_many_c mbedtls_internal_sha256_process_many
444#define mbedtls_internal_sha256_process_c mbedtls_internal_sha256_process
445#endif
446
447
448#if !defined(MBEDTLS_SHA256_PROCESS_ALT) && \
449 !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451#define SHR(x, n) (((x) & 0xFFFFFFFF) >> (n))
452#define ROTR(x, n) (SHR(x, n) | ((x) << (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454#define S0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
455#define S1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457#define S2(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
458#define S3(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460#define F0(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
461#define F1(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200463#define R(t) \
464 ( \
465 local.W[t] = S1(local.W[(t) - 2]) + local.W[(t) - 7] + \
466 S0(local.W[(t) - 15]) + local.W[(t) - 16] \
Hanno Becker1eeca412018-10-15 12:01:35 +0100467 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469#define P(a, b, c, d, e, f, g, h, x, K) \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200470 do \
471 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 local.temp1 = (h) + S3(e) + F1((e), (f), (g)) + (K) + (x); \
473 local.temp2 = S2(a) + F0((a), (b), (c)); \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200474 (d) += local.temp1; (h) = local.temp1 + local.temp2; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100477#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
478/*
479 * This function is for internal use only if we are building both C and A64
480 * versions, otherwise it is renamed to be the public mbedtls_internal_sha256_process()
481 */
482static
483#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100484int mbedtls_internal_sha256_process_c(mbedtls_sha256_context *ctx,
485 const unsigned char data[SHA256_BLOCK_SIZE])
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200486{
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 struct {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200488 uint32_t temp1, temp2, W[64];
489 uint32_t A[8];
490 } local;
491
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200492 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 for (i = 0; i < 8; i++) {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200495 local.A[i] = ctx->state[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 }
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200497
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200498#if defined(MBEDTLS_SHA256_SMALLER)
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 for (i = 0; i < 64; i++) {
500 if (i < 16) {
501 local.W[i] = MBEDTLS_GET_UINT32_BE(data, 4 * i);
502 } else {
503 R(i);
504 }
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
507 local.A[5], local.A[6], local.A[7], local.W[i], K[i]);
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200508
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200509 local.temp1 = local.A[7]; local.A[7] = local.A[6];
510 local.A[6] = local.A[5]; local.A[5] = local.A[4];
511 local.A[4] = local.A[3]; local.A[3] = local.A[2];
512 local.A[2] = local.A[1]; local.A[1] = local.A[0];
513 local.A[0] = local.temp1;
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200514 }
515#else /* MBEDTLS_SHA256_SMALLER */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 for (i = 0; i < 16; i++) {
517 local.W[i] = MBEDTLS_GET_UINT32_BE(data, 4 * i);
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200518 }
519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 for (i = 0; i < 16; i += 8) {
521 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
522 local.A[5], local.A[6], local.A[7], local.W[i+0], K[i+0]);
523 P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
524 local.A[4], local.A[5], local.A[6], local.W[i+1], K[i+1]);
525 P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
526 local.A[3], local.A[4], local.A[5], local.W[i+2], K[i+2]);
527 P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
528 local.A[2], local.A[3], local.A[4], local.W[i+3], K[i+3]);
529 P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
530 local.A[1], local.A[2], local.A[3], local.W[i+4], K[i+4]);
531 P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
532 local.A[0], local.A[1], local.A[2], local.W[i+5], K[i+5]);
533 P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
534 local.A[7], local.A[0], local.A[1], local.W[i+6], K[i+6]);
535 P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
536 local.A[6], local.A[7], local.A[0], local.W[i+7], K[i+7]);
537 }
538
539 for (i = 16; i < 64; i += 8) {
540 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
541 local.A[5], local.A[6], local.A[7], R(i+0), K[i+0]);
542 P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
543 local.A[4], local.A[5], local.A[6], R(i+1), K[i+1]);
544 P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
545 local.A[3], local.A[4], local.A[5], R(i+2), K[i+2]);
546 P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
547 local.A[2], local.A[3], local.A[4], R(i+3), K[i+3]);
548 P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
549 local.A[1], local.A[2], local.A[3], R(i+4), K[i+4]);
550 P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
551 local.A[0], local.A[1], local.A[2], R(i+5), K[i+5]);
552 P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
553 local.A[7], local.A[0], local.A[1], R(i+6), K[i+6]);
554 P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
555 local.A[6], local.A[7], local.A[0], R(i+7), K[i+7]);
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200556 }
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200557#endif /* MBEDTLS_SHA256_SMALLER */
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 for (i = 0; i < 8; i++) {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200560 ctx->state[i] += local.A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100562
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200563 /* Zeroise buffers and variables to clear sensitive data from memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 mbedtls_platform_zeroize(&local, sizeof(local));
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000567}
Jaeden Amero041039f2018-02-19 15:28:08 +0000568
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000569#endif /* !MBEDTLS_SHA256_PROCESS_ALT && !MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
570
571
572#if !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
573
574static size_t mbedtls_internal_sha256_process_many_c(
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 mbedtls_sha256_context *ctx, const uint8_t *data, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000576{
577 size_t processed = 0;
578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 while (len >= SHA256_BLOCK_SIZE) {
580 if (mbedtls_internal_sha256_process_c(ctx, data) != 0) {
581 return 0;
582 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000583
584 data += SHA256_BLOCK_SIZE;
585 len -= SHA256_BLOCK_SIZE;
586
587 processed += SHA256_BLOCK_SIZE;
588 }
589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return processed;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000591}
592
593#endif /* !MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
594
595
596#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598static int mbedtls_a64_crypto_sha256_has_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000599{
600 static int done = 0;
601 static int supported = 0;
602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (!done) {
Tom Cosgrove7e7aba82022-02-24 08:33:11 +0000604 supported = mbedtls_a64_crypto_sha256_determine_support();
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000605 done = 1;
606 }
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 return supported;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000609}
610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611static size_t mbedtls_internal_sha256_process_many(mbedtls_sha256_context *ctx,
612 const uint8_t *msg, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000613{
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (mbedtls_a64_crypto_sha256_has_support()) {
615 return mbedtls_internal_sha256_process_many_a64_crypto(ctx, msg, len);
616 } else {
617 return mbedtls_internal_sha256_process_many_c(ctx, msg, len);
618 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000619}
620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
622 const unsigned char data[SHA256_BLOCK_SIZE])
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000623{
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (mbedtls_a64_crypto_sha256_has_support()) {
625 return mbedtls_internal_sha256_process_a64_crypto(ctx, data);
626 } else {
627 return mbedtls_internal_sha256_process_c(ctx, data);
628 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000629}
630
631#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT */
632
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634/*
635 * SHA-256 process buffer
636 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100637int mbedtls_sha256_update(mbedtls_sha256_context *ctx,
638 const unsigned char *input,
639 size_t ilen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000640{
Janos Follath24eed8d2019-11-22 13:21:35 +0000641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000642 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000643 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (ilen == 0) {
646 return 0;
647 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
649 left = ctx->total[0] & 0x3F;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000650 fill = SHA256_BLOCK_SIZE - left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Paul Bakker5c2364c2012-10-01 14:41:15 +0000652 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 ctx->total[0] &= 0xFFFFFFFF;
654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (ctx->total[0] < (uint32_t) ilen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 ctx->total[1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (left && ilen >= fill) {
660 memcpy((void *) (ctx->buffer + left), input, fill);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
663 return ret;
664 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100665
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 input += fill;
667 ilen -= fill;
668 left = 0;
669 }
670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 while (ilen >= SHA256_BLOCK_SIZE) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000672 size_t processed =
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 mbedtls_internal_sha256_process_many(ctx, input, ilen);
674 if (processed < SHA256_BLOCK_SIZE) {
675 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
676 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100677
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000678 input += processed;
679 ilen -= processed;
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 }
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (ilen > 0) {
683 memcpy((void *) (ctx->buffer + left), input, ilen);
684 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000687}
688
Paul Bakker5121ce52009-01-03 21:22:43 +0000689/*
690 * SHA-256 final digest
691 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100692int mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
693 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000694{
Janos Follath24eed8d2019-11-22 13:21:35 +0000695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200696 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000697 uint32_t high, low;
Dave Rodgman90330a42023-09-28 17:24:06 +0100698 int truncated = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200700 /*
701 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
702 */
703 used = ctx->total[0] & 0x3F;
704
705 ctx->buffer[used++] = 0x80;
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (used <= 56) {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200708 /* Enough room for padding + length in current block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 memset(ctx->buffer + used, 0, 56 - used);
710 } else {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200711 /* We'll need an extra block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 memset(ctx->buffer + used, 0, SHA256_BLOCK_SIZE - used);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100715 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 }
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 memset(ctx->buffer, 0, 56);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200719 }
720
721 /*
722 * Add message length
723 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 high = (ctx->total[0] >> 29)
725 | (ctx->total[1] << 3);
726 low = (ctx->total[0] << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
729 MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100732 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100734
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200735 /*
736 * Output final state
737 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
739 MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
740 MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
741 MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
742 MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
743 MBEDTLS_PUT_UINT32_BE(ctx->state[5], output, 20);
744 MBEDTLS_PUT_UINT32_BE(ctx->state[6], output, 24);
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200746#if defined(MBEDTLS_SHA224_C)
David Horstmann687262c2022-10-06 17:54:57 +0100747 truncated = ctx->is224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200748#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (!truncated) {
750 MBEDTLS_PUT_UINT32_BE(ctx->state[7], output, 28);
751 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100752
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100753 ret = 0;
754
755exit:
756 mbedtls_sha256_free(ctx);
757 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000758}
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#endif /* !MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200761
Paul Bakker5121ce52009-01-03 21:22:43 +0000762/*
763 * output = SHA-256( input buffer )
764 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765int mbedtls_sha256(const unsigned char *input,
766 size_t ilen,
767 unsigned char *output,
768 int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000769{
Janos Follath24eed8d2019-11-22 13:21:35 +0000770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_sha256_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
Valerio Settia3f99592022-12-14 10:56:54 +0100773#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (is224 != 0 && is224 != 1) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100775 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 }
Valerio Settia3f99592022-12-14 10:56:54 +0100777#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (is224 != 0) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100779 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 }
Valerio Settia3f99592022-12-14 10:56:54 +0100781#else /* defined MBEDTLS_SHA224_C only */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100783 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 }
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200785#endif
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 mbedtls_sha256_init(&ctx);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if ((ret = mbedtls_sha256_starts(&ctx, is224)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100790 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if ((ret = mbedtls_sha256_update(&ctx, input, ilen)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100794 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if ((ret = mbedtls_sha256_finish(&ctx, output)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100798 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100800
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100801exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 mbedtls_sha256_free(&ctx);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000805}
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000808/*
809 * FIPS-180-2 test vectors
810 */
Valerio Settia3f99592022-12-14 10:56:54 +0100811static const unsigned char sha_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000812{
813 { "abc" },
814 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
815 { "" }
816};
817
Valerio Settia3f99592022-12-14 10:56:54 +0100818static const size_t sha_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000819{
820 3, 56, 1000
821};
822
Valerio Settia3f99592022-12-14 10:56:54 +0100823typedef const unsigned char (sha_test_sum_t)[32];
824
825/*
826 * SHA-224 test vectors
827 */
828#if defined(MBEDTLS_SHA224_C)
829static sha_test_sum_t sha224_test_sum[] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000830{
Paul Bakker5121ce52009-01-03 21:22:43 +0000831 { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
832 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
833 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
834 0xE3, 0x6C, 0x9D, 0xA7 },
835 { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
836 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
837 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
838 0x52, 0x52, 0x25, 0x25 },
839 { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8,
840 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B,
841 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE,
Valerio Settia3f99592022-12-14 10:56:54 +0100842 0x4E, 0xE7, 0xAD, 0x67 }
843};
844#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Valerio Settia3f99592022-12-14 10:56:54 +0100846/*
847 * SHA-256 test vectors
848 */
849#if defined(MBEDTLS_SHA256_C)
850static sha_test_sum_t sha256_test_sum[] =
851{
Paul Bakker5121ce52009-01-03 21:22:43 +0000852 { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
853 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
854 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
855 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
856 { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
857 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
858 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
859 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
860 { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
861 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
862 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
863 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
864};
Valerio Settia3f99592022-12-14 10:56:54 +0100865#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000866
867/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 * Checkup routine
869 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870static int mbedtls_sha256_common_self_test(int verbose, int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000871{
Valerio Settia3f99592022-12-14 10:56:54 +0100872 int i, buflen, ret = 0;
Russ Butlerbb83b422016-10-12 17:36:50 -0500873 unsigned char *buf;
Paul Bakker9e36f042013-06-30 14:34:05 +0200874 unsigned char sha256sum[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_sha256_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000876
Valerio Settia3f99592022-12-14 10:56:54 +0100877#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 sha_test_sum_t *sha_test_sum = (is224) ? sha224_test_sum : sha256_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100879#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 sha_test_sum_t *sha_test_sum = sha256_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100881#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 sha_test_sum_t *sha_test_sum = sha224_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100883#endif
884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 buf = mbedtls_calloc(1024, sizeof(unsigned char));
886 if (NULL == buf) {
887 if (verbose != 0) {
888 mbedtls_printf("Buffer allocation failed\n");
889 }
Russ Butlerbb83b422016-10-12 17:36:50 -0500890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 return 1;
Russ Butlerbb83b422016-10-12 17:36:50 -0500892 }
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 mbedtls_sha256_init(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 for (i = 0; i < 3; i++) {
897 if (verbose != 0) {
898 mbedtls_printf(" SHA-%d test #%d: ", 256 - is224 * 32, i + 1);
899 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if ((ret = mbedtls_sha256_starts(&ctx, is224)) != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100902 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (i == 2) {
906 memset(buf, 'a', buflen = 1000);
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 for (int j = 0; j < 1000; j++) {
909 ret = mbedtls_sha256_update(&ctx, buf, buflen);
910 if (ret != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100911 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 }
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100913 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 } else {
916 ret = mbedtls_sha256_update(&ctx, sha_test_buf[i],
917 sha_test_buflen[i]);
918 if (ret != 0) {
919 goto fail;
920 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100921 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if ((ret = mbedtls_sha256_finish(&ctx, sha256sum)) != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100924 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100926
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if (memcmp(sha256sum, sha_test_sum[i], 32 - is224 * 4) != 0) {
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100929 ret = 1;
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100930 goto fail;
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100931 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (verbose != 0) {
934 mbedtls_printf("passed\n");
935 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000936 }
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (verbose != 0) {
939 mbedtls_printf("\n");
940 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100942 goto exit;
943
944fail:
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (verbose != 0) {
946 mbedtls_printf("failed\n");
947 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100948
Paul Bakker5b4af392014-06-26 12:09:34 +0200949exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 mbedtls_sha256_free(&ctx);
951 mbedtls_free(buf);
Paul Bakker5b4af392014-06-26 12:09:34 +0200952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000954}
955
Valerio Settia3f99592022-12-14 10:56:54 +0100956#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100957int mbedtls_sha256_self_test(int verbose)
Valerio Settia3f99592022-12-14 10:56:54 +0100958{
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 return mbedtls_sha256_common_self_test(verbose, 0);
Valerio Settia3f99592022-12-14 10:56:54 +0100960}
961#endif /* MBEDTLS_SHA256_C */
962
963#if defined(MBEDTLS_SHA224_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100964int mbedtls_sha224_self_test(int verbose)
Valerio Settia3f99592022-12-14 10:56:54 +0100965{
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 return mbedtls_sha256_common_self_test(verbose, 1);
Valerio Settia3f99592022-12-14 10:56:54 +0100967}
968#endif /* MBEDTLS_SHA224_C */
969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
Valerio Settia3f99592022-12-14 10:56:54 +0100972#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA224_C */