blob: 87889817a4eac6d143bc3318ae314183020d4724 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7/*
8 * The SHA-256 Secure Hash Standard was published by NIST in 2002.
9 *
10 * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
11 */
12
Dave Rodgman8ba9f422023-10-08 10:46:25 +010013#if defined(__clang__) && (__clang_major__ >= 4)
14
Dave Rodgmanfe9fda82023-10-10 14:51:06 +010015/* Ideally, we would simply use MBEDTLS_ARCH_IS_ARMV8_A in the following #if,
Dave Rodgman8ba9f422023-10-08 10:46:25 +010016 * but that is defined by build_info.h, and we need this block to happen first. */
Dave Rodgmanfe9fda82023-10-10 14:51:06 +010017#if defined(__ARM_ARCH) && (__ARM_ARCH_PROFILE == 'A')
Dave Rodgman8ba9f422023-10-08 10:46:25 +010018#if __ARM_ARCH >= 8
Dave Rodgmanfe9fda82023-10-10 14:51:06 +010019#define MBEDTLS_SHA256_ARCH_IS_ARMV8_A
Dave Rodgman8ba9f422023-10-08 10:46:25 +010020#endif
21#endif
22
Dave Rodgmanfe9fda82023-10-10 14:51:06 +010023#if defined(MBEDTLS_SHA256_ARCH_IS_ARMV8_A) && !defined(__ARM_FEATURE_CRYPTO)
Jerry Yua135dee2023-02-16 16:56:22 +080024/* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged.
25 *
Jerry Yufc2e1282023-02-27 11:16:56 +080026 * The intrinsic declaration are guarded by predefined ACLE macros in clang:
27 * these are normally only enabled by the -march option on the command line.
28 * By defining the macros ourselves we gain access to those declarations without
29 * requiring -march on the command line.
Jerry Yu4d786a72023-02-22 11:01:07 +080030 *
Dave Rodgmana0f10da2023-09-05 11:43:17 +010031 * `arm_neon.h` is included by common.h, so we put these defines
Jerry Yufc2e1282023-02-27 11:16:56 +080032 * at the top of this file, before any includes.
Jerry Yua135dee2023-02-16 16:56:22 +080033 */
34#define __ARM_FEATURE_CRYPTO 1
Jerry Yuae129c32023-03-03 15:55:56 +080035/* See: https://arm-software.github.io/acle/main/acle.html#cryptographic-extensions
36 *
Jerry Yu490bf082023-03-06 15:21:44 +080037 * `__ARM_FEATURE_CRYPTO` is deprecated, but we need to continue to specify it
38 * for older compilers.
Jerry Yuae129c32023-03-03 15:55:56 +080039 */
40#define __ARM_FEATURE_SHA2 1
Dave Rodgmandb6ab242023-03-14 16:03:57 +000041#define MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG
Jerry Yu490bf082023-03-06 15:21:44 +080042#endif
Jerry Yua135dee2023-02-16 16:56:22 +080043
Dave Rodgman8ba9f422023-10-08 10:46:25 +010044#endif /* defined(__clang__) && (__clang_major__ >= 4) */
45
Dave Rodgman7ed619d2023-10-05 09:39:56 +010046/* Ensure that SIG_SETMASK is defined when -std=c99 is used. */
47#define _GNU_SOURCE
48
Gilles Peskinedb09ef62020-06-03 01:43:33 +020049#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Valerio Settia3f99592022-12-14 10:56:54 +010051#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/sha256.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050054#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000055#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <string.h>
58
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010060
Dave Rodgmanfe9fda82023-10-10 14:51:06 +010061#if defined(MBEDTLS_ARCH_IS_ARMV8_A)
Jerry Yu08933d32023-04-27 18:28:00 +080062
Dave Rodgman5b89c552023-10-10 14:59:02 +010063# if defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT) || \
64 defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY)
Dave Rodgman6e51abf2023-10-25 15:17:11 +010065# if !defined(MBEDTLS_HAVE_NEON_INTRINSICS)
Dave Rodgman5b89c552023-10-10 14:59:02 +010066# if defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT)
Dave Rodgman3ba9ce32023-10-05 09:58:33 +010067# warning "Target does not support NEON instructions"
Dave Rodgman5b89c552023-10-10 14:59:02 +010068# undef MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
Dave Rodgman3ba9ce32023-10-05 09:58:33 +010069# else
70# error "Target does not support NEON instructions"
71# endif
72# endif
Jerry Yu6b00f5a2023-05-04 16:30:21 +080073# endif
74
Dave Rodgman5b89c552023-10-10 14:59:02 +010075# if defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT) || \
76 defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY)
Dave Rodgman790370b2023-10-05 11:01:31 +010077/* *INDENT-OFF* */
78
Dave Rodgman793e2642023-10-04 17:36:20 +010079# if !defined(__ARM_FEATURE_CRYPTO) || defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG)
Jerry Yub1d06bb2023-05-05 14:05:07 +080080# if defined(__ARMCOMPILER_VERSION)
81# if __ARMCOMPILER_VERSION <= 6090000
Dave Rodgman5b89c552023-10-10 14:59:02 +010082# error "Must use minimum -march=armv8-a+crypto for MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_*"
Jerry Yub1d06bb2023-05-05 14:05:07 +080083# endif
84# pragma clang attribute push (__attribute__((target("sha2"))), apply_to=function)
85# define MBEDTLS_POP_TARGET_PRAGMA
86# elif defined(__clang__)
Jerry Yu383cbf42023-02-16 15:16:43 +080087# if __clang_major__ < 4
Dave Rodgman5b89c552023-10-10 14:59:02 +010088# error "A more recent Clang is required for MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_*"
Jerry Yu64e5d4a2023-02-15 11:46:57 +080089# endif
Jerry Yub1d06bb2023-05-05 14:05:07 +080090# pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function)
Jerry Yu64e5d4a2023-02-15 11:46:57 +080091# define MBEDTLS_POP_TARGET_PRAGMA
92# elif defined(__GNUC__)
Tom Cosgrovec15a2b92023-03-08 12:55:48 +000093 /* FIXME: GCC 5 claims to support Armv8 Crypto Extensions, but some
94 * intrinsics are missing. Missing intrinsics could be worked around.
Jerry Yu8ae6a012023-02-16 15:16:20 +080095 */
96# if __GNUC__ < 6
Dave Rodgman5b89c552023-10-10 14:59:02 +010097# error "A more recent GCC is required for MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_*"
Jerry Yu64e5d4a2023-02-15 11:46:57 +080098# else
Jerry Yu2f2c0492023-02-16 14:24:46 +080099# pragma GCC push_options
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800100# pragma GCC target ("arch=armv8-a+crypto")
Jerry Yu2f2c0492023-02-16 14:24:46 +0800101# define MBEDTLS_POP_TARGET_PRAGMA
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800102# endif
103# else
Dave Rodgman5b89c552023-10-10 14:59:02 +0100104# error "Only GCC and Clang supported for MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_*"
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800105# endif
Jerry Yu35f2b262023-02-15 11:35:55 +0800106# endif
107/* *INDENT-ON* */
Jerry Yu08933d32023-04-27 18:28:00 +0800108
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000109# endif
Dave Rodgman5b89c552023-10-10 14:59:02 +0100110# if defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT)
Tom Cosgroveb7f5b972022-03-15 11:26:55 +0000111# if defined(__unix__)
112# if defined(__linux__)
Gilles Peskine449bd832023-01-11 14:50:10 +0100113/* Our preferred method of detection is getauxval() */
Tom Cosgroveb7f5b972022-03-15 11:26:55 +0000114# include <sys/auxv.h>
Dave Rodgman66d55122023-10-23 15:12:32 +0100115/* These are not always defined via sys/auxv.h */
116# if !defined(HWCAP_SHA2)
117# define HWCAP_SHA2 (1 << 6)
118# endif
119# if !defined(HWCAP2_SHA2)
120# define HWCAP2_SHA2 (1 << 3)
121# endif
Tom Cosgroveb7f5b972022-03-15 11:26:55 +0000122# endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100123/* Use SIGILL on Unix, and fall back to it on Linux */
Tom Cosgroveb7f5b972022-03-15 11:26:55 +0000124# include <signal.h>
125# endif
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000126# endif
Dave Rodgman0a487172023-09-15 11:52:06 +0100127#elif !defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64)
Dave Rodgman5b89c552023-10-10 14:59:02 +0100128# undef MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY
129# undef MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000130#endif
131
Dave Rodgman5b89c552023-10-10 14:59:02 +0100132#if defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000133/*
134 * Capability detection code comes early, so we can disable
Dave Rodgman5b89c552023-10-10 14:59:02 +0100135 * MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT if no detection mechanism found
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000136 */
Dave Rodgman5d4ef832023-10-10 13:04:07 +0100137#if defined(MBEDTLS_ARCH_IS_ARM64) && defined(HWCAP_SHA2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100138static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000139{
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return (getauxval(AT_HWCAP) & HWCAP_SHA2) ? 1 : 0;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000141}
Dave Rodgman5d4ef832023-10-10 13:04:07 +0100142#elif defined(MBEDTLS_ARCH_IS_ARM32) && defined(HWCAP2_SHA2)
143static int mbedtls_a64_crypto_sha256_determine_support(void)
144{
145 return (getauxval(AT_HWCAP2) & HWCAP2_SHA2) ? 1 : 0;
146}
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000147#elif defined(__APPLE__)
Gilles Peskine449bd832023-01-11 14:50:10 +0100148static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000149{
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return 1;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000151}
Dave Rodgman0a487172023-09-15 11:52:06 +0100152#elif defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64)
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000153#define WIN32_LEAN_AND_MEAN
154#include <Windows.h>
155#include <processthreadsapi.h>
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000158{
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) ?
160 1 : 0;
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000161}
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000162#elif defined(__unix__) && defined(SIG_SETMASK)
163/* Detection with SIGILL, setjmp() and longjmp() */
164#include <signal.h>
165#include <setjmp.h>
166
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000167static jmp_buf return_from_sigill;
168
169/*
Dave Rodgman4fd868e2023-10-12 09:09:42 +0100170 * Armv8-A SHA256 support detection via SIGILL
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000171 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172static void sigill_handler(int signal)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000173{
174 (void) signal;
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 longjmp(return_from_sigill, 1);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000176}
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000179{
180 struct sigaction old_action, new_action;
181
182 sigset_t old_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (sigprocmask(0, NULL, &old_mask)) {
184 return 0;
185 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 sigemptyset(&new_action.sa_mask);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000188 new_action.sa_flags = 0;
189 new_action.sa_handler = sigill_handler;
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 sigaction(SIGILL, &new_action, &old_action);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000192
193 static int ret = 0;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (setjmp(return_from_sigill) == 0) { /* First return only */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000196 /* If this traps, we will return a second time from setjmp() with 1 */
Dave Rodgman7ed619d2023-10-05 09:39:56 +0100197#if defined(MBEDTLS_ARCH_IS_ARM64)
Dave Rodgman78d78462023-10-10 09:53:44 +0100198 asm volatile ("sha256h q0, q0, v0.4s" : : : "v0");
Dave Rodgman7ed619d2023-10-05 09:39:56 +0100199#else
Dave Rodgman78d78462023-10-10 09:53:44 +0100200 asm volatile ("sha256h.32 q0, q0, q0" : : : "q0");
Dave Rodgman7ed619d2023-10-05 09:39:56 +0100201#endif
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000202 ret = 1;
203 }
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 sigaction(SIGILL, &old_action, NULL);
206 sigprocmask(SIG_SETMASK, &old_mask, NULL);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return ret;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000209}
210#else
Dave Rodgman94a634d2023-10-10 12:59:29 +0100211#warning "No mechanism to detect ARMV8_CRYPTO found, using C code only"
Dave Rodgman5b89c552023-10-10 14:59:02 +0100212#undef MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000213#endif /* HWCAP_SHA2, __APPLE__, __unix__ && SIG_SETMASK */
214
Dave Rodgman5b89c552023-10-10 14:59:02 +0100215#endif /* MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000216
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +0200217#if !defined(MBEDTLS_SHA256_ALT)
218
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000219#define SHA256_BLOCK_SIZE 64
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +0200222{
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 memset(ctx, 0, sizeof(mbedtls_sha256_context));
Paul Bakker5b4af392014-06-26 12:09:34 +0200224}
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +0200227{
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (ctx == NULL) {
Paul Bakker5b4af392014-06-26 12:09:34 +0200229 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 }
Paul Bakker5b4af392014-06-26 12:09:34 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha256_context));
Paul Bakker5b4af392014-06-26 12:09:34 +0200233}
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
236 const mbedtls_sha256_context *src)
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200237{
238 *dst = *src;
239}
240
Paul Bakker5121ce52009-01-03 21:22:43 +0000241/*
242 * SHA-256 context setup
243 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244int mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000245{
Valerio Settia3f99592022-12-14 10:56:54 +0100246#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (is224 != 0 && is224 != 1) {
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#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (is224 != 0) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100252 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 }
Valerio Settia3f99592022-12-14 10:56:54 +0100254#else /* defined MBEDTLS_SHA224_C only */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100256 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 }
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200258#endif
259
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 ctx->total[0] = 0;
261 ctx->total[1] = 0;
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100264#if defined(MBEDTLS_SHA256_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 ctx->state[0] = 0x6A09E667;
266 ctx->state[1] = 0xBB67AE85;
267 ctx->state[2] = 0x3C6EF372;
268 ctx->state[3] = 0xA54FF53A;
269 ctx->state[4] = 0x510E527F;
270 ctx->state[5] = 0x9B05688C;
271 ctx->state[6] = 0x1F83D9AB;
272 ctx->state[7] = 0x5BE0CD19;
Valerio Settia3f99592022-12-14 10:56:54 +0100273#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 } else {
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200275#if defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 ctx->state[0] = 0xC1059ED8;
277 ctx->state[1] = 0x367CD507;
278 ctx->state[2] = 0x3070DD17;
279 ctx->state[3] = 0xF70E5939;
280 ctx->state[4] = 0xFFC00B31;
281 ctx->state[5] = 0x68581511;
282 ctx->state[6] = 0x64F98FA7;
283 ctx->state[7] = 0xBEFA4FA4;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200284#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 }
286
Valerio Settia3f99592022-12-14 10:56:54 +0100287#if defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 ctx->is224 = is224;
Valerio Settia3f99592022-12-14 10:56:54 +0100289#endif
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000292}
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if !defined(MBEDTLS_SHA256_PROCESS_ALT)
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200295static const uint32_t K[] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000296{
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200297 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
298 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
299 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
300 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
301 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
302 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
303 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
304 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
305 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
306 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
307 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
308 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
309 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
310 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
311 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
312 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
313};
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000315#endif
316
Dave Rodgman5b89c552023-10-10 14:59:02 +0100317#if defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT) || \
318 defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000319
Dave Rodgman5b89c552023-10-10 14:59:02 +0100320#if defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000321# define mbedtls_internal_sha256_process_many_a64_crypto mbedtls_internal_sha256_process_many
322# define mbedtls_internal_sha256_process_a64_crypto mbedtls_internal_sha256_process
323#endif
324
325static size_t mbedtls_internal_sha256_process_many_a64_crypto(
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 mbedtls_sha256_context *ctx, const uint8_t *msg, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000327{
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 uint32x4_t abcd = vld1q_u32(&ctx->state[0]);
329 uint32x4_t efgh = vld1q_u32(&ctx->state[4]);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000330
331 size_t processed = 0;
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 for (;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000334 len >= SHA256_BLOCK_SIZE;
335 processed += SHA256_BLOCK_SIZE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 msg += SHA256_BLOCK_SIZE,
337 len -= SHA256_BLOCK_SIZE) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000338 uint32x4_t tmp, abcd_prev;
339
340 uint32x4_t abcd_orig = abcd;
341 uint32x4_t efgh_orig = efgh;
342
Dave Rodgman9a36f4c2023-10-05 11:25:52 +0100343 uint32x4_t sched0 = vreinterpretq_u32_u8(vld1q_u8(msg + 16 * 0));
344 uint32x4_t sched1 = vreinterpretq_u32_u8(vld1q_u8(msg + 16 * 1));
345 uint32x4_t sched2 = vreinterpretq_u32_u8(vld1q_u8(msg + 16 * 2));
346 uint32x4_t sched3 = vreinterpretq_u32_u8(vld1q_u8(msg + 16 * 3));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000347
348#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ /* Will be true if not defined */
349 /* Untested on BE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 sched0 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched0)));
351 sched1 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched1)));
352 sched2 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched2)));
353 sched3 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched3)));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000354#endif
355
356 /* Rounds 0 to 3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 tmp = vaddq_u32(sched0, vld1q_u32(&K[0]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000358 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
360 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000361
362 /* Rounds 4 to 7 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 tmp = vaddq_u32(sched1, vld1q_u32(&K[4]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000364 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
366 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000367
368 /* Rounds 8 to 11 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 tmp = vaddq_u32(sched2, vld1q_u32(&K[8]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000370 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
372 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000373
374 /* Rounds 12 to 15 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 tmp = vaddq_u32(sched3, vld1q_u32(&K[12]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000376 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
378 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 for (int t = 16; t < 64; t += 16) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000381 /* Rounds t to t + 3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 sched0 = vsha256su1q_u32(vsha256su0q_u32(sched0, sched1), sched2, sched3);
383 tmp = vaddq_u32(sched0, vld1q_u32(&K[t]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000384 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
386 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000387
388 /* Rounds t + 4 to t + 7 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 sched1 = vsha256su1q_u32(vsha256su0q_u32(sched1, sched2), sched3, sched0);
390 tmp = vaddq_u32(sched1, vld1q_u32(&K[t + 4]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000391 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
393 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000394
395 /* Rounds t + 8 to t + 11 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 sched2 = vsha256su1q_u32(vsha256su0q_u32(sched2, sched3), sched0, sched1);
397 tmp = vaddq_u32(sched2, vld1q_u32(&K[t + 8]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000398 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
400 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000401
402 /* Rounds t + 12 to t + 15 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 sched3 = vsha256su1q_u32(vsha256su0q_u32(sched3, sched0), sched1, sched2);
404 tmp = vaddq_u32(sched3, vld1q_u32(&K[t + 12]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000405 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
407 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000408 }
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 abcd = vaddq_u32(abcd, abcd_orig);
411 efgh = vaddq_u32(efgh, efgh_orig);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000412 }
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 vst1q_u32(&ctx->state[0], abcd);
415 vst1q_u32(&ctx->state[4], efgh);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return processed;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000418}
419
Dave Rodgman5b89c552023-10-10 14:59:02 +0100420#if defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT)
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100421/*
Dave Rodgman4fd868e2023-10-12 09:09:42 +0100422 * This function is for internal use only if we are building both C and Armv8-A
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100423 * versions, otherwise it is renamed to be the public mbedtls_internal_sha256_process()
424 */
425static
426#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100427int mbedtls_internal_sha256_process_a64_crypto(mbedtls_sha256_context *ctx,
428 const unsigned char data[SHA256_BLOCK_SIZE])
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000429{
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return (mbedtls_internal_sha256_process_many_a64_crypto(ctx, data,
431 SHA256_BLOCK_SIZE) ==
432 SHA256_BLOCK_SIZE) ? 0 : -1;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000433}
434
Dave Rodgman5b89c552023-10-10 14:59:02 +0100435#endif /* MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT || MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY */
Tom Cosgroveef2aa0e2023-06-09 11:29:50 +0100436
Jerry Yu92fc5382023-02-16 11:17:11 +0800437#if defined(MBEDTLS_POP_TARGET_PRAGMA)
Jerry Yu2f2c0492023-02-16 14:24:46 +0800438#if defined(__clang__)
Jerry Yu92fc5382023-02-16 11:17:11 +0800439#pragma clang attribute pop
Jerry Yu2f2c0492023-02-16 14:24:46 +0800440#elif defined(__GNUC__)
441#pragma GCC pop_options
442#endif
Jerry Yu92fc5382023-02-16 11:17:11 +0800443#undef MBEDTLS_POP_TARGET_PRAGMA
444#endif
445
Dave Rodgman5b89c552023-10-10 14:59:02 +0100446#if !defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000447#define mbedtls_internal_sha256_process_many_c mbedtls_internal_sha256_process_many
448#define mbedtls_internal_sha256_process_c mbedtls_internal_sha256_process
449#endif
450
451
452#if !defined(MBEDTLS_SHA256_PROCESS_ALT) && \
Dave Rodgman5b89c552023-10-10 14:59:02 +0100453 !defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455#define SHR(x, n) (((x) & 0xFFFFFFFF) >> (n))
456#define ROTR(x, n) (SHR(x, n) | ((x) << (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458#define S0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
459#define S1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461#define S2(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
462#define S3(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464#define F0(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
465#define F1(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200467#define R(t) \
468 ( \
469 local.W[t] = S1(local.W[(t) - 2]) + local.W[(t) - 7] + \
470 S0(local.W[(t) - 15]) + local.W[(t) - 16] \
Hanno Becker1eeca412018-10-15 12:01:35 +0100471 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473#define P(a, b, c, d, e, f, g, h, x, K) \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200474 do \
475 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 local.temp1 = (h) + S3(e) + F1((e), (f), (g)) + (K) + (x); \
477 local.temp2 = S2(a) + F0((a), (b), (c)); \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200478 (d) += local.temp1; (h) = local.temp1 + local.temp2; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
Dave Rodgman5b89c552023-10-10 14:59:02 +0100481#if defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT)
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100482/*
Dave Rodgman94a634d2023-10-10 12:59:29 +0100483 * This function is for internal use only if we are building both C and Armv8
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100484 * versions, otherwise it is renamed to be the public mbedtls_internal_sha256_process()
485 */
486static
487#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100488int mbedtls_internal_sha256_process_c(mbedtls_sha256_context *ctx,
489 const unsigned char data[SHA256_BLOCK_SIZE])
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200490{
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 struct {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200492 uint32_t temp1, temp2, W[64];
493 uint32_t A[8];
494 } local;
495
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200496 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 for (i = 0; i < 8; i++) {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200499 local.A[i] = ctx->state[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 }
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200501
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200502#if defined(MBEDTLS_SHA256_SMALLER)
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 for (i = 0; i < 64; i++) {
504 if (i < 16) {
505 local.W[i] = MBEDTLS_GET_UINT32_BE(data, 4 * i);
506 } else {
507 R(i);
508 }
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
511 local.A[5], local.A[6], local.A[7], local.W[i], K[i]);
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200512
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200513 local.temp1 = local.A[7]; local.A[7] = local.A[6];
514 local.A[6] = local.A[5]; local.A[5] = local.A[4];
515 local.A[4] = local.A[3]; local.A[3] = local.A[2];
516 local.A[2] = local.A[1]; local.A[1] = local.A[0];
517 local.A[0] = local.temp1;
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200518 }
519#else /* MBEDTLS_SHA256_SMALLER */
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 for (i = 0; i < 16; i++) {
521 local.W[i] = MBEDTLS_GET_UINT32_BE(data, 4 * i);
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200522 }
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 for (i = 0; i < 16; i += 8) {
525 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
526 local.A[5], local.A[6], local.A[7], local.W[i+0], K[i+0]);
527 P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
528 local.A[4], local.A[5], local.A[6], local.W[i+1], K[i+1]);
529 P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
530 local.A[3], local.A[4], local.A[5], local.W[i+2], K[i+2]);
531 P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
532 local.A[2], local.A[3], local.A[4], local.W[i+3], K[i+3]);
533 P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
534 local.A[1], local.A[2], local.A[3], local.W[i+4], K[i+4]);
535 P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
536 local.A[0], local.A[1], local.A[2], local.W[i+5], K[i+5]);
537 P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
538 local.A[7], local.A[0], local.A[1], local.W[i+6], K[i+6]);
539 P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
540 local.A[6], local.A[7], local.A[0], local.W[i+7], K[i+7]);
541 }
542
543 for (i = 16; i < 64; i += 8) {
544 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
545 local.A[5], local.A[6], local.A[7], R(i+0), K[i+0]);
546 P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
547 local.A[4], local.A[5], local.A[6], R(i+1), K[i+1]);
548 P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
549 local.A[3], local.A[4], local.A[5], R(i+2), K[i+2]);
550 P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
551 local.A[2], local.A[3], local.A[4], R(i+3), K[i+3]);
552 P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
553 local.A[1], local.A[2], local.A[3], R(i+4), K[i+4]);
554 P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
555 local.A[0], local.A[1], local.A[2], R(i+5), K[i+5]);
556 P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
557 local.A[7], local.A[0], local.A[1], R(i+6), K[i+6]);
558 P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
559 local.A[6], local.A[7], local.A[0], R(i+7), K[i+7]);
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200560 }
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200561#endif /* MBEDTLS_SHA256_SMALLER */
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 for (i = 0; i < 8; i++) {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200564 ctx->state[i] += local.A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100566
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200567 /* Zeroise buffers and variables to clear sensitive data from memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 mbedtls_platform_zeroize(&local, sizeof(local));
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000571}
Jaeden Amero041039f2018-02-19 15:28:08 +0000572
Dave Rodgman5b89c552023-10-10 14:59:02 +0100573#endif /* !MBEDTLS_SHA256_PROCESS_ALT && !MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000574
575
Dave Rodgman5b89c552023-10-10 14:59:02 +0100576#if !defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000577
578static size_t mbedtls_internal_sha256_process_many_c(
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 mbedtls_sha256_context *ctx, const uint8_t *data, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000580{
581 size_t processed = 0;
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 while (len >= SHA256_BLOCK_SIZE) {
584 if (mbedtls_internal_sha256_process_c(ctx, data) != 0) {
585 return 0;
586 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000587
588 data += SHA256_BLOCK_SIZE;
589 len -= SHA256_BLOCK_SIZE;
590
591 processed += SHA256_BLOCK_SIZE;
592 }
593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return processed;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000595}
596
Dave Rodgman5b89c552023-10-10 14:59:02 +0100597#endif /* !MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000598
599
Dave Rodgman5b89c552023-10-10 14:59:02 +0100600#if defined(MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602static int mbedtls_a64_crypto_sha256_has_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000603{
604 static int done = 0;
605 static int supported = 0;
606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (!done) {
Tom Cosgrove7e7aba82022-02-24 08:33:11 +0000608 supported = mbedtls_a64_crypto_sha256_determine_support();
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000609 done = 1;
610 }
611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return supported;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000613}
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615static size_t mbedtls_internal_sha256_process_many(mbedtls_sha256_context *ctx,
616 const uint8_t *msg, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000617{
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (mbedtls_a64_crypto_sha256_has_support()) {
619 return mbedtls_internal_sha256_process_many_a64_crypto(ctx, msg, len);
620 } else {
621 return mbedtls_internal_sha256_process_many_c(ctx, msg, len);
622 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000623}
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
626 const unsigned char data[SHA256_BLOCK_SIZE])
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000627{
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (mbedtls_a64_crypto_sha256_has_support()) {
629 return mbedtls_internal_sha256_process_a64_crypto(ctx, data);
630 } else {
631 return mbedtls_internal_sha256_process_c(ctx, data);
632 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000633}
634
Dave Rodgman5b89c552023-10-10 14:59:02 +0100635#endif /* MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000636
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
638/*
639 * SHA-256 process buffer
640 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100641int mbedtls_sha256_update(mbedtls_sha256_context *ctx,
642 const unsigned char *input,
643 size_t ilen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000644{
Janos Follath24eed8d2019-11-22 13:21:35 +0000645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000646 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000647 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (ilen == 0) {
650 return 0;
651 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
653 left = ctx->total[0] & 0x3F;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000654 fill = SHA256_BLOCK_SIZE - left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Paul Bakker5c2364c2012-10-01 14:41:15 +0000656 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 ctx->total[0] &= 0xFFFFFFFF;
658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (ctx->total[0] < (uint32_t) ilen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 ctx->total[1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (left && ilen >= fill) {
664 memcpy((void *) (ctx->buffer + left), input, fill);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
667 return ret;
668 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100669
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 input += fill;
671 ilen -= fill;
672 left = 0;
673 }
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 while (ilen >= SHA256_BLOCK_SIZE) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000676 size_t processed =
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 mbedtls_internal_sha256_process_many(ctx, input, ilen);
678 if (processed < SHA256_BLOCK_SIZE) {
679 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
680 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100681
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000682 input += processed;
683 ilen -= processed;
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 }
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (ilen > 0) {
687 memcpy((void *) (ctx->buffer + left), input, ilen);
688 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000691}
692
Paul Bakker5121ce52009-01-03 21:22:43 +0000693/*
694 * SHA-256 final digest
695 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696int mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
697 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000698{
Janos Follath24eed8d2019-11-22 13:21:35 +0000699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200700 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000701 uint32_t high, low;
Dave Rodgman90330a42023-09-28 17:24:06 +0100702 int truncated = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200704 /*
705 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
706 */
707 used = ctx->total[0] & 0x3F;
708
709 ctx->buffer[used++] = 0x80;
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (used <= 56) {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200712 /* Enough room for padding + length in current block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 memset(ctx->buffer + used, 0, 56 - used);
714 } else {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200715 /* We'll need an extra block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 memset(ctx->buffer + used, 0, SHA256_BLOCK_SIZE - used);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100719 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 }
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 memset(ctx->buffer, 0, 56);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200723 }
724
725 /*
726 * Add message length
727 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 high = (ctx->total[0] >> 29)
729 | (ctx->total[1] << 3);
730 low = (ctx->total[0] << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
733 MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100736 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100738
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200739 /*
740 * Output final state
741 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
743 MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
744 MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
745 MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
746 MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
747 MBEDTLS_PUT_UINT32_BE(ctx->state[5], output, 20);
748 MBEDTLS_PUT_UINT32_BE(ctx->state[6], output, 24);
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200750#if defined(MBEDTLS_SHA224_C)
David Horstmann687262c2022-10-06 17:54:57 +0100751 truncated = ctx->is224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200752#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (!truncated) {
754 MBEDTLS_PUT_UINT32_BE(ctx->state[7], output, 28);
755 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100756
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100757 ret = 0;
758
759exit:
760 mbedtls_sha256_free(ctx);
761 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000762}
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#endif /* !MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200765
Paul Bakker5121ce52009-01-03 21:22:43 +0000766/*
767 * output = SHA-256( input buffer )
768 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769int mbedtls_sha256(const unsigned char *input,
770 size_t ilen,
771 unsigned char *output,
772 int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000773{
Janos Follath24eed8d2019-11-22 13:21:35 +0000774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_sha256_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
Valerio Settia3f99592022-12-14 10:56:54 +0100777#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (is224 != 0 && is224 != 1) {
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#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (is224 != 0) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100783 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 }
Valerio Settia3f99592022-12-14 10:56:54 +0100785#else /* defined MBEDTLS_SHA224_C only */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100787 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 }
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200789#endif
790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 mbedtls_sha256_init(&ctx);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if ((ret = mbedtls_sha256_starts(&ctx, is224)) != 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_update(&ctx, input, ilen)) != 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if ((ret = mbedtls_sha256_finish(&ctx, output)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100802 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100804
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100805exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 mbedtls_sha256_free(&ctx);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000809}
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000812/*
813 * FIPS-180-2 test vectors
814 */
Valerio Settia3f99592022-12-14 10:56:54 +0100815static const unsigned char sha_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000816{
817 { "abc" },
818 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
819 { "" }
820};
821
Valerio Settia3f99592022-12-14 10:56:54 +0100822static const size_t sha_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000823{
824 3, 56, 1000
825};
826
Valerio Settia3f99592022-12-14 10:56:54 +0100827typedef const unsigned char (sha_test_sum_t)[32];
828
829/*
830 * SHA-224 test vectors
831 */
832#if defined(MBEDTLS_SHA224_C)
833static sha_test_sum_t sha224_test_sum[] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000834{
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
836 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
837 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
838 0xE3, 0x6C, 0x9D, 0xA7 },
839 { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
840 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
841 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
842 0x52, 0x52, 0x25, 0x25 },
843 { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8,
844 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B,
845 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE,
Valerio Settia3f99592022-12-14 10:56:54 +0100846 0x4E, 0xE7, 0xAD, 0x67 }
847};
848#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000849
Valerio Settia3f99592022-12-14 10:56:54 +0100850/*
851 * SHA-256 test vectors
852 */
853#if defined(MBEDTLS_SHA256_C)
854static sha_test_sum_t sha256_test_sum[] =
855{
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
857 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
858 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
859 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
860 { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
861 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
862 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
863 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
864 { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
865 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
866 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
867 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
868};
Valerio Settia3f99592022-12-14 10:56:54 +0100869#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000870
871/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 * Checkup routine
873 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874static int mbedtls_sha256_common_self_test(int verbose, int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000875{
Valerio Settia3f99592022-12-14 10:56:54 +0100876 int i, buflen, ret = 0;
Russ Butlerbb83b422016-10-12 17:36:50 -0500877 unsigned char *buf;
Paul Bakker9e36f042013-06-30 14:34:05 +0200878 unsigned char sha256sum[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_sha256_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000880
Valerio Settia3f99592022-12-14 10:56:54 +0100881#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 sha_test_sum_t *sha_test_sum = (is224) ? sha224_test_sum : sha256_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100883#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 sha_test_sum_t *sha_test_sum = sha256_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100885#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 sha_test_sum_t *sha_test_sum = sha224_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100887#endif
888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 buf = mbedtls_calloc(1024, sizeof(unsigned char));
890 if (NULL == buf) {
891 if (verbose != 0) {
892 mbedtls_printf("Buffer allocation failed\n");
893 }
Russ Butlerbb83b422016-10-12 17:36:50 -0500894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 return 1;
Russ Butlerbb83b422016-10-12 17:36:50 -0500896 }
897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 mbedtls_sha256_init(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 for (i = 0; i < 3; i++) {
901 if (verbose != 0) {
902 mbedtls_printf(" SHA-%d test #%d: ", 256 - is224 * 32, i + 1);
903 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if ((ret = mbedtls_sha256_starts(&ctx, is224)) != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100906 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (i == 2) {
910 memset(buf, 'a', buflen = 1000);
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 for (int j = 0; j < 1000; j++) {
913 ret = mbedtls_sha256_update(&ctx, buf, buflen);
914 if (ret != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100915 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 }
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100917 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 } else {
920 ret = mbedtls_sha256_update(&ctx, sha_test_buf[i],
921 sha_test_buflen[i]);
922 if (ret != 0) {
923 goto fail;
924 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100925 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if ((ret = mbedtls_sha256_finish(&ctx, sha256sum)) != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100928 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100930
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (memcmp(sha256sum, sha_test_sum[i], 32 - is224 * 4) != 0) {
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100933 ret = 1;
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100934 goto fail;
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100935 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (verbose != 0) {
938 mbedtls_printf("passed\n");
939 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000940 }
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (verbose != 0) {
943 mbedtls_printf("\n");
944 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100946 goto exit;
947
948fail:
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (verbose != 0) {
950 mbedtls_printf("failed\n");
951 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100952
Paul Bakker5b4af392014-06-26 12:09:34 +0200953exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 mbedtls_sha256_free(&ctx);
955 mbedtls_free(buf);
Paul Bakker5b4af392014-06-26 12:09:34 +0200956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000958}
959
Valerio Settia3f99592022-12-14 10:56:54 +0100960#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100961int mbedtls_sha256_self_test(int verbose)
Valerio Settia3f99592022-12-14 10:56:54 +0100962{
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return mbedtls_sha256_common_self_test(verbose, 0);
Valerio Settia3f99592022-12-14 10:56:54 +0100964}
965#endif /* MBEDTLS_SHA256_C */
966
967#if defined(MBEDTLS_SHA224_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100968int mbedtls_sha224_self_test(int verbose)
Valerio Settia3f99592022-12-14 10:56:54 +0100969{
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return mbedtls_sha256_common_self_test(verbose, 1);
Valerio Settia3f99592022-12-14 10:56:54 +0100971}
972#endif /* MBEDTLS_SHA224_C */
973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
Valerio Settia3f99592022-12-14 10:56:54 +0100976#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA224_C */