blob: 96aa25e591334b780c10e1f57b2c88129e803bb2 [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
Dave Rodgman8ba9f422023-10-08 10:46:25 +010025#if defined(__clang__) && (__clang_major__ >= 4)
26
27/* Ideally, we would simply use MBEDTLS_ARCH_IS_ARMV8 in the following #if,
28 * but that is defined by build_info.h, and we need this block to happen first. */
29#if defined(__ARM_ARCH)
30#if __ARM_ARCH >= 8
31#define MBEDTLS_SHA256_ARCH_IS_ARMV8
32#endif
33#endif
34
35#if defined(MBEDTLS_SHA256_ARCH_IS_ARMV8) && !defined(__ARM_FEATURE_CRYPTO)
Jerry Yua135dee2023-02-16 16:56:22 +080036/* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged.
37 *
Jerry Yufc2e1282023-02-27 11:16:56 +080038 * The intrinsic declaration are guarded by predefined ACLE macros in clang:
39 * these are normally only enabled by the -march option on the command line.
40 * By defining the macros ourselves we gain access to those declarations without
41 * requiring -march on the command line.
Jerry Yu4d786a72023-02-22 11:01:07 +080042 *
Jerry Yufc2e1282023-02-27 11:16:56 +080043 * `arm_neon.h` could be included by any header file, so we put these defines
44 * at the top of this file, before any includes.
Jerry Yua135dee2023-02-16 16:56:22 +080045 */
46#define __ARM_FEATURE_CRYPTO 1
Jerry Yuae129c32023-03-03 15:55:56 +080047/* See: https://arm-software.github.io/acle/main/acle.html#cryptographic-extensions
48 *
Jerry Yu490bf082023-03-06 15:21:44 +080049 * `__ARM_FEATURE_CRYPTO` is deprecated, but we need to continue to specify it
50 * for older compilers.
Jerry Yuae129c32023-03-03 15:55:56 +080051 */
52#define __ARM_FEATURE_SHA2 1
Dave Rodgmandb6ab242023-03-14 16:03:57 +000053#define MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG
Jerry Yu490bf082023-03-06 15:21:44 +080054#endif
Jerry Yua135dee2023-02-16 16:56:22 +080055
Dave Rodgman8ba9f422023-10-08 10:46:25 +010056#endif /* defined(__clang__) && (__clang_major__ >= 4) */
57
Dave Rodgman7ed619d2023-10-05 09:39:56 +010058/* Ensure that SIG_SETMASK is defined when -std=c99 is used. */
59#define _GNU_SOURCE
60
Gilles Peskinedb09ef62020-06-03 01:43:33 +020061#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Valerio Settia3f99592022-12-14 10:56:54 +010063#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/sha256.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050066#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000067#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Rich Evans00ab4702015-02-06 13:43:58 +000069#include <string.h>
70
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010072
Dave Rodgmancc5bf492023-10-03 18:02:56 +010073#if defined(MBEDTLS_ARCH_IS_ARMV8)
Jerry Yu08933d32023-04-27 18:28:00 +080074
Dave Rodgman94a634d2023-10-10 12:59:29 +010075# if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT) || \
76 defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY)
Dave Rodgman3ba9ce32023-10-05 09:58:33 +010077# ifdef __ARM_NEON
78# include <arm_neon.h>
79# else
Dave Rodgman94a634d2023-10-10 12:59:29 +010080# if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT)
Dave Rodgman3ba9ce32023-10-05 09:58:33 +010081# warning "Target does not support NEON instructions"
Dave Rodgman94a634d2023-10-10 12:59:29 +010082# undef MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT
Dave Rodgman3ba9ce32023-10-05 09:58:33 +010083# else
84# error "Target does not support NEON instructions"
85# endif
86# endif
Jerry Yu6b00f5a2023-05-04 16:30:21 +080087# endif
88
Dave Rodgman94a634d2023-10-10 12:59:29 +010089# if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT) || \
90 defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY)
Dave Rodgman790370b2023-10-05 11:01:31 +010091/* *INDENT-OFF* */
92
Dave Rodgman793e2642023-10-04 17:36:20 +010093# if !defined(__ARM_FEATURE_CRYPTO) || defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG)
Jerry Yub1d06bb2023-05-05 14:05:07 +080094# if defined(__ARMCOMPILER_VERSION)
95# if __ARMCOMPILER_VERSION <= 6090000
Dave Rodgman94a634d2023-10-10 12:59:29 +010096# error "Must use minimum -march=armv8-a+crypto for MBEDTLS_SHA256_USE_ARMV8_CRYPTO_*"
Jerry Yub1d06bb2023-05-05 14:05:07 +080097# endif
98# pragma clang attribute push (__attribute__((target("sha2"))), apply_to=function)
99# define MBEDTLS_POP_TARGET_PRAGMA
100# elif defined(__clang__)
Jerry Yu383cbf42023-02-16 15:16:43 +0800101# if __clang_major__ < 4
Dave Rodgman94a634d2023-10-10 12:59:29 +0100102# error "A more recent Clang is required for MBEDTLS_SHA256_USE_ARMV8_CRYPTO_*"
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800103# endif
Jerry Yub1d06bb2023-05-05 14:05:07 +0800104# pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function)
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800105# define MBEDTLS_POP_TARGET_PRAGMA
106# elif defined(__GNUC__)
Tom Cosgrovec15a2b92023-03-08 12:55:48 +0000107 /* FIXME: GCC 5 claims to support Armv8 Crypto Extensions, but some
108 * intrinsics are missing. Missing intrinsics could be worked around.
Jerry Yu8ae6a012023-02-16 15:16:20 +0800109 */
110# if __GNUC__ < 6
Dave Rodgman94a634d2023-10-10 12:59:29 +0100111# error "A more recent GCC is required for MBEDTLS_SHA256_USE_ARMV8_CRYPTO_*"
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800112# else
Jerry Yu2f2c0492023-02-16 14:24:46 +0800113# pragma GCC push_options
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800114# pragma GCC target ("arch=armv8-a+crypto")
Jerry Yu2f2c0492023-02-16 14:24:46 +0800115# define MBEDTLS_POP_TARGET_PRAGMA
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800116# endif
117# else
Dave Rodgman94a634d2023-10-10 12:59:29 +0100118# error "Only GCC and Clang supported for MBEDTLS_SHA256_USE_ARMV8_CRYPTO_*"
Jerry Yu64e5d4a2023-02-15 11:46:57 +0800119# endif
Jerry Yu35f2b262023-02-15 11:35:55 +0800120# endif
121/* *INDENT-ON* */
Jerry Yu08933d32023-04-27 18:28:00 +0800122
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000123# endif
Dave Rodgman94a634d2023-10-10 12:59:29 +0100124# if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT)
Tom Cosgroveb7f5b972022-03-15 11:26:55 +0000125# if defined(__unix__)
126# if defined(__linux__)
Gilles Peskine449bd832023-01-11 14:50:10 +0100127/* Our preferred method of detection is getauxval() */
Tom Cosgroveb7f5b972022-03-15 11:26:55 +0000128# include <sys/auxv.h>
129# endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100130/* Use SIGILL on Unix, and fall back to it on Linux */
Tom Cosgroveb7f5b972022-03-15 11:26:55 +0000131# include <signal.h>
132# endif
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000133# endif
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000134#elif defined(_M_ARM64)
Dave Rodgman94a634d2023-10-10 12:59:29 +0100135# if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT) || \
136 defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY)
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000137# include <arm64_neon.h>
138# endif
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000139#else
Dave Rodgman94a634d2023-10-10 12:59:29 +0100140# undef MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY
141# undef MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000142#endif
143
Dave Rodgman94a634d2023-10-10 12:59:29 +0100144#if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000145/*
146 * Capability detection code comes early, so we can disable
Dave Rodgman94a634d2023-10-10 12:59:29 +0100147 * MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT if no detection mechanism found
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000148 */
149#if defined(HWCAP_SHA2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100150static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000151{
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return (getauxval(AT_HWCAP) & HWCAP_SHA2) ? 1 : 0;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000153}
154#elif defined(__APPLE__)
Gilles Peskine449bd832023-01-11 14:50:10 +0100155static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000156{
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return 1;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000158}
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000159#elif defined(_M_ARM64)
160#define WIN32_LEAN_AND_MEAN
161#include <Windows.h>
162#include <processthreadsapi.h>
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000165{
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) ?
167 1 : 0;
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000168}
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000169#elif defined(__unix__) && defined(SIG_SETMASK)
170/* Detection with SIGILL, setjmp() and longjmp() */
171#include <signal.h>
172#include <setjmp.h>
173
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000174static jmp_buf return_from_sigill;
175
176/*
Dave Rodgman94a634d2023-10-10 12:59:29 +0100177 * Armv8 SHA256 support detection via SIGILL
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000178 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179static void sigill_handler(int signal)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000180{
181 (void) signal;
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 longjmp(return_from_sigill, 1);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000183}
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000186{
187 struct sigaction old_action, new_action;
188
189 sigset_t old_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (sigprocmask(0, NULL, &old_mask)) {
191 return 0;
192 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 sigemptyset(&new_action.sa_mask);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000195 new_action.sa_flags = 0;
196 new_action.sa_handler = sigill_handler;
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 sigaction(SIGILL, &new_action, &old_action);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000199
200 static int ret = 0;
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (setjmp(return_from_sigill) == 0) { /* First return only */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000203 /* If this traps, we will return a second time from setjmp() with 1 */
Dave Rodgman7ed619d2023-10-05 09:39:56 +0100204#if defined(MBEDTLS_ARCH_IS_ARM64)
Dave Rodgman78d78462023-10-10 09:53:44 +0100205 asm volatile ("sha256h q0, q0, v0.4s" : : : "v0");
Dave Rodgman7ed619d2023-10-05 09:39:56 +0100206#else
Dave Rodgman78d78462023-10-10 09:53:44 +0100207 asm volatile ("sha256h.32 q0, q0, q0" : : : "q0");
Dave Rodgman7ed619d2023-10-05 09:39:56 +0100208#endif
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000209 ret = 1;
210 }
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 sigaction(SIGILL, &old_action, NULL);
213 sigprocmask(SIG_SETMASK, &old_mask, NULL);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return ret;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000216}
217#else
Dave Rodgman94a634d2023-10-10 12:59:29 +0100218#warning "No mechanism to detect ARMV8_CRYPTO found, using C code only"
219#undef MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000220#endif /* HWCAP_SHA2, __APPLE__, __unix__ && SIG_SETMASK */
221
Dave Rodgman94a634d2023-10-10 12:59:29 +0100222#endif /* MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000223
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +0200224#if !defined(MBEDTLS_SHA256_ALT)
225
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000226#define SHA256_BLOCK_SIZE 64
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +0200229{
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 memset(ctx, 0, sizeof(mbedtls_sha256_context));
Paul Bakker5b4af392014-06-26 12:09:34 +0200231}
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +0200234{
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (ctx == NULL) {
Paul Bakker5b4af392014-06-26 12:09:34 +0200236 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 }
Paul Bakker5b4af392014-06-26 12:09:34 +0200238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha256_context));
Paul Bakker5b4af392014-06-26 12:09:34 +0200240}
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
243 const mbedtls_sha256_context *src)
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200244{
245 *dst = *src;
246}
247
Paul Bakker5121ce52009-01-03 21:22:43 +0000248/*
249 * SHA-256 context setup
250 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251int mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000252{
Valerio Settia3f99592022-12-14 10:56:54 +0100253#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (is224 != 0 && is224 != 1) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100255 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 }
Valerio Settia3f99592022-12-14 10:56:54 +0100257#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (is224 != 0) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100259 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 }
Valerio Settia3f99592022-12-14 10:56:54 +0100261#else /* defined MBEDTLS_SHA224_C only */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100263 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 }
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200265#endif
266
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 ctx->total[0] = 0;
268 ctx->total[1] = 0;
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100271#if defined(MBEDTLS_SHA256_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 ctx->state[0] = 0x6A09E667;
273 ctx->state[1] = 0xBB67AE85;
274 ctx->state[2] = 0x3C6EF372;
275 ctx->state[3] = 0xA54FF53A;
276 ctx->state[4] = 0x510E527F;
277 ctx->state[5] = 0x9B05688C;
278 ctx->state[6] = 0x1F83D9AB;
279 ctx->state[7] = 0x5BE0CD19;
Valerio Settia3f99592022-12-14 10:56:54 +0100280#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 } else {
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200282#if defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 ctx->state[0] = 0xC1059ED8;
284 ctx->state[1] = 0x367CD507;
285 ctx->state[2] = 0x3070DD17;
286 ctx->state[3] = 0xF70E5939;
287 ctx->state[4] = 0xFFC00B31;
288 ctx->state[5] = 0x68581511;
289 ctx->state[6] = 0x64F98FA7;
290 ctx->state[7] = 0xBEFA4FA4;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200291#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 }
293
Valerio Settia3f99592022-12-14 10:56:54 +0100294#if defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 ctx->is224 = is224;
Valerio Settia3f99592022-12-14 10:56:54 +0100296#endif
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000299}
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if !defined(MBEDTLS_SHA256_PROCESS_ALT)
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200302static const uint32_t K[] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000303{
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200304 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
305 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
306 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
307 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
308 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
309 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
310 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
311 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
312 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
313 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
314 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
315 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
316 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
317 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
318 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
319 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
320};
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000322#endif
323
Dave Rodgman94a634d2023-10-10 12:59:29 +0100324#if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT) || \
325 defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000326
Dave Rodgman94a634d2023-10-10 12:59:29 +0100327#if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000328# define mbedtls_internal_sha256_process_many_a64_crypto mbedtls_internal_sha256_process_many
329# define mbedtls_internal_sha256_process_a64_crypto mbedtls_internal_sha256_process
330#endif
331
332static size_t mbedtls_internal_sha256_process_many_a64_crypto(
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 mbedtls_sha256_context *ctx, const uint8_t *msg, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000334{
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 uint32x4_t abcd = vld1q_u32(&ctx->state[0]);
336 uint32x4_t efgh = vld1q_u32(&ctx->state[4]);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000337
338 size_t processed = 0;
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 for (;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000341 len >= SHA256_BLOCK_SIZE;
342 processed += SHA256_BLOCK_SIZE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 msg += SHA256_BLOCK_SIZE,
344 len -= SHA256_BLOCK_SIZE) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000345 uint32x4_t tmp, abcd_prev;
346
347 uint32x4_t abcd_orig = abcd;
348 uint32x4_t efgh_orig = efgh;
349
Dave Rodgman9a36f4c2023-10-05 11:25:52 +0100350 uint32x4_t sched0 = vreinterpretq_u32_u8(vld1q_u8(msg + 16 * 0));
351 uint32x4_t sched1 = vreinterpretq_u32_u8(vld1q_u8(msg + 16 * 1));
352 uint32x4_t sched2 = vreinterpretq_u32_u8(vld1q_u8(msg + 16 * 2));
353 uint32x4_t sched3 = vreinterpretq_u32_u8(vld1q_u8(msg + 16 * 3));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000354
355#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ /* Will be true if not defined */
356 /* Untested on BE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 sched0 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched0)));
358 sched1 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched1)));
359 sched2 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched2)));
360 sched3 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched3)));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000361#endif
362
363 /* Rounds 0 to 3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 tmp = vaddq_u32(sched0, vld1q_u32(&K[0]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000365 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
367 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000368
369 /* Rounds 4 to 7 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 tmp = vaddq_u32(sched1, vld1q_u32(&K[4]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000371 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
373 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000374
375 /* Rounds 8 to 11 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 tmp = vaddq_u32(sched2, vld1q_u32(&K[8]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000377 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
379 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000380
381 /* Rounds 12 to 15 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 tmp = vaddq_u32(sched3, vld1q_u32(&K[12]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000383 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
385 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 for (int t = 16; t < 64; t += 16) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000388 /* Rounds t to t + 3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 sched0 = vsha256su1q_u32(vsha256su0q_u32(sched0, sched1), sched2, sched3);
390 tmp = vaddq_u32(sched0, vld1q_u32(&K[t]));
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 + 4 to t + 7 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 sched1 = vsha256su1q_u32(vsha256su0q_u32(sched1, sched2), sched3, sched0);
397 tmp = vaddq_u32(sched1, vld1q_u32(&K[t + 4]));
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 + 8 to t + 11 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 sched2 = vsha256su1q_u32(vsha256su0q_u32(sched2, sched3), sched0, sched1);
404 tmp = vaddq_u32(sched2, vld1q_u32(&K[t + 8]));
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 /* Rounds t + 12 to t + 15 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 sched3 = vsha256su1q_u32(vsha256su0q_u32(sched3, sched0), sched1, sched2);
411 tmp = vaddq_u32(sched3, vld1q_u32(&K[t + 12]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000412 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
414 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000415 }
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 abcd = vaddq_u32(abcd, abcd_orig);
418 efgh = vaddq_u32(efgh, efgh_orig);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000419 }
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 vst1q_u32(&ctx->state[0], abcd);
422 vst1q_u32(&ctx->state[4], efgh);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 return processed;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000425}
426
Dave Rodgman94a634d2023-10-10 12:59:29 +0100427#if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT)
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100428/*
Dave Rodgman94a634d2023-10-10 12:59:29 +0100429 * This function is for internal use only if we are building both C and Armv8
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100430 * versions, otherwise it is renamed to be the public mbedtls_internal_sha256_process()
431 */
432static
433#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100434int mbedtls_internal_sha256_process_a64_crypto(mbedtls_sha256_context *ctx,
435 const unsigned char data[SHA256_BLOCK_SIZE])
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000436{
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return (mbedtls_internal_sha256_process_many_a64_crypto(ctx, data,
438 SHA256_BLOCK_SIZE) ==
439 SHA256_BLOCK_SIZE) ? 0 : -1;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000440}
441
Dave Rodgman94a634d2023-10-10 12:59:29 +0100442#endif /* MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT || MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY */
Tom Cosgroveef2aa0e2023-06-09 11:29:50 +0100443
Jerry Yu92fc5382023-02-16 11:17:11 +0800444#if defined(MBEDTLS_POP_TARGET_PRAGMA)
Jerry Yu2f2c0492023-02-16 14:24:46 +0800445#if defined(__clang__)
Jerry Yu92fc5382023-02-16 11:17:11 +0800446#pragma clang attribute pop
Jerry Yu2f2c0492023-02-16 14:24:46 +0800447#elif defined(__GNUC__)
448#pragma GCC pop_options
449#endif
Jerry Yu92fc5382023-02-16 11:17:11 +0800450#undef MBEDTLS_POP_TARGET_PRAGMA
451#endif
452
Dave Rodgman94a634d2023-10-10 12:59:29 +0100453#if !defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000454#define mbedtls_internal_sha256_process_many_c mbedtls_internal_sha256_process_many
455#define mbedtls_internal_sha256_process_c mbedtls_internal_sha256_process
456#endif
457
458
459#if !defined(MBEDTLS_SHA256_PROCESS_ALT) && \
Dave Rodgman94a634d2023-10-10 12:59:29 +0100460 !defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462#define SHR(x, n) (((x) & 0xFFFFFFFF) >> (n))
463#define ROTR(x, n) (SHR(x, n) | ((x) << (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465#define S0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
466#define S1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468#define S2(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
469#define S3(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471#define F0(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
472#define F1(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200474#define R(t) \
475 ( \
476 local.W[t] = S1(local.W[(t) - 2]) + local.W[(t) - 7] + \
477 S0(local.W[(t) - 15]) + local.W[(t) - 16] \
Hanno Becker1eeca412018-10-15 12:01:35 +0100478 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480#define P(a, b, c, d, e, f, g, h, x, K) \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200481 do \
482 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 local.temp1 = (h) + S3(e) + F1((e), (f), (g)) + (K) + (x); \
484 local.temp2 = S2(a) + F0((a), (b), (c)); \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200485 (d) += local.temp1; (h) = local.temp1 + local.temp2; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
Dave Rodgman94a634d2023-10-10 12:59:29 +0100488#if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT)
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100489/*
Dave Rodgman94a634d2023-10-10 12:59:29 +0100490 * This function is for internal use only if we are building both C and Armv8
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100491 * versions, otherwise it is renamed to be the public mbedtls_internal_sha256_process()
492 */
493static
494#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100495int mbedtls_internal_sha256_process_c(mbedtls_sha256_context *ctx,
496 const unsigned char data[SHA256_BLOCK_SIZE])
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200497{
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 struct {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200499 uint32_t temp1, temp2, W[64];
500 uint32_t A[8];
501 } local;
502
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200503 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 for (i = 0; i < 8; i++) {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200506 local.A[i] = ctx->state[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 }
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200508
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200509#if defined(MBEDTLS_SHA256_SMALLER)
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 for (i = 0; i < 64; i++) {
511 if (i < 16) {
512 local.W[i] = MBEDTLS_GET_UINT32_BE(data, 4 * i);
513 } else {
514 R(i);
515 }
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
518 local.A[5], local.A[6], local.A[7], local.W[i], K[i]);
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200519
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200520 local.temp1 = local.A[7]; local.A[7] = local.A[6];
521 local.A[6] = local.A[5]; local.A[5] = local.A[4];
522 local.A[4] = local.A[3]; local.A[3] = local.A[2];
523 local.A[2] = local.A[1]; local.A[1] = local.A[0];
524 local.A[0] = local.temp1;
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200525 }
526#else /* MBEDTLS_SHA256_SMALLER */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 for (i = 0; i < 16; i++) {
528 local.W[i] = MBEDTLS_GET_UINT32_BE(data, 4 * i);
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200529 }
530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 for (i = 0; i < 16; i += 8) {
532 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
533 local.A[5], local.A[6], local.A[7], local.W[i+0], K[i+0]);
534 P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
535 local.A[4], local.A[5], local.A[6], local.W[i+1], K[i+1]);
536 P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
537 local.A[3], local.A[4], local.A[5], local.W[i+2], K[i+2]);
538 P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
539 local.A[2], local.A[3], local.A[4], local.W[i+3], K[i+3]);
540 P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
541 local.A[1], local.A[2], local.A[3], local.W[i+4], K[i+4]);
542 P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
543 local.A[0], local.A[1], local.A[2], local.W[i+5], K[i+5]);
544 P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
545 local.A[7], local.A[0], local.A[1], local.W[i+6], K[i+6]);
546 P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
547 local.A[6], local.A[7], local.A[0], local.W[i+7], K[i+7]);
548 }
549
550 for (i = 16; i < 64; i += 8) {
551 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
552 local.A[5], local.A[6], local.A[7], R(i+0), K[i+0]);
553 P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
554 local.A[4], local.A[5], local.A[6], R(i+1), K[i+1]);
555 P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
556 local.A[3], local.A[4], local.A[5], R(i+2), K[i+2]);
557 P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
558 local.A[2], local.A[3], local.A[4], R(i+3), K[i+3]);
559 P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
560 local.A[1], local.A[2], local.A[3], R(i+4), K[i+4]);
561 P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
562 local.A[0], local.A[1], local.A[2], R(i+5), K[i+5]);
563 P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
564 local.A[7], local.A[0], local.A[1], R(i+6), K[i+6]);
565 P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
566 local.A[6], local.A[7], local.A[0], R(i+7), K[i+7]);
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200567 }
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200568#endif /* MBEDTLS_SHA256_SMALLER */
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 for (i = 0; i < 8; i++) {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200571 ctx->state[i] += local.A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100573
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200574 /* Zeroise buffers and variables to clear sensitive data from memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 mbedtls_platform_zeroize(&local, sizeof(local));
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000578}
Jaeden Amero041039f2018-02-19 15:28:08 +0000579
Dave Rodgman94a634d2023-10-10 12:59:29 +0100580#endif /* !MBEDTLS_SHA256_PROCESS_ALT && !MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000581
582
Dave Rodgman94a634d2023-10-10 12:59:29 +0100583#if !defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000584
585static size_t mbedtls_internal_sha256_process_many_c(
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 mbedtls_sha256_context *ctx, const uint8_t *data, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000587{
588 size_t processed = 0;
589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 while (len >= SHA256_BLOCK_SIZE) {
591 if (mbedtls_internal_sha256_process_c(ctx, data) != 0) {
592 return 0;
593 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000594
595 data += SHA256_BLOCK_SIZE;
596 len -= SHA256_BLOCK_SIZE;
597
598 processed += SHA256_BLOCK_SIZE;
599 }
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return processed;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000602}
603
Dave Rodgman94a634d2023-10-10 12:59:29 +0100604#endif /* !MBEDTLS_SHA256_USE_ARMV8_CRYPTO_ONLY */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000605
606
Dave Rodgman94a634d2023-10-10 12:59:29 +0100607#if defined(MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609static int mbedtls_a64_crypto_sha256_has_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000610{
611 static int done = 0;
612 static int supported = 0;
613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (!done) {
Tom Cosgrove7e7aba82022-02-24 08:33:11 +0000615 supported = mbedtls_a64_crypto_sha256_determine_support();
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000616 done = 1;
617 }
618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return supported;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000620}
621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622static size_t mbedtls_internal_sha256_process_many(mbedtls_sha256_context *ctx,
623 const uint8_t *msg, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000624{
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 if (mbedtls_a64_crypto_sha256_has_support()) {
626 return mbedtls_internal_sha256_process_many_a64_crypto(ctx, msg, len);
627 } else {
628 return mbedtls_internal_sha256_process_many_c(ctx, msg, len);
629 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000630}
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
633 const unsigned char data[SHA256_BLOCK_SIZE])
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000634{
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (mbedtls_a64_crypto_sha256_has_support()) {
636 return mbedtls_internal_sha256_process_a64_crypto(ctx, data);
637 } else {
638 return mbedtls_internal_sha256_process_c(ctx, data);
639 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000640}
641
Dave Rodgman94a634d2023-10-10 12:59:29 +0100642#endif /* MBEDTLS_SHA256_USE_ARMV8_CRYPTO_IF_PRESENT */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000643
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645/*
646 * SHA-256 process buffer
647 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648int mbedtls_sha256_update(mbedtls_sha256_context *ctx,
649 const unsigned char *input,
650 size_t ilen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000651{
Janos Follath24eed8d2019-11-22 13:21:35 +0000652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000653 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000654 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (ilen == 0) {
657 return 0;
658 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000659
660 left = ctx->total[0] & 0x3F;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000661 fill = SHA256_BLOCK_SIZE - left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
Paul Bakker5c2364c2012-10-01 14:41:15 +0000663 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 ctx->total[0] &= 0xFFFFFFFF;
665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (ctx->total[0] < (uint32_t) ilen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 ctx->total[1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (left && ilen >= fill) {
671 memcpy((void *) (ctx->buffer + left), input, fill);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
674 return ret;
675 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100676
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 input += fill;
678 ilen -= fill;
679 left = 0;
680 }
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 while (ilen >= SHA256_BLOCK_SIZE) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000683 size_t processed =
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 mbedtls_internal_sha256_process_many(ctx, input, ilen);
685 if (processed < SHA256_BLOCK_SIZE) {
686 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
687 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100688
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000689 input += processed;
690 ilen -= processed;
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 }
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (ilen > 0) {
694 memcpy((void *) (ctx->buffer + left), input, ilen);
695 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000698}
699
Paul Bakker5121ce52009-01-03 21:22:43 +0000700/*
701 * SHA-256 final digest
702 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703int mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
704 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000705{
Janos Follath24eed8d2019-11-22 13:21:35 +0000706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200707 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000708 uint32_t high, low;
Dave Rodgman90330a42023-09-28 17:24:06 +0100709 int truncated = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200711 /*
712 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
713 */
714 used = ctx->total[0] & 0x3F;
715
716 ctx->buffer[used++] = 0x80;
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (used <= 56) {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200719 /* Enough room for padding + length in current block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 memset(ctx->buffer + used, 0, 56 - used);
721 } else {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200722 /* We'll need an extra block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 memset(ctx->buffer + used, 0, SHA256_BLOCK_SIZE - used);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100726 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 }
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 memset(ctx->buffer, 0, 56);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200730 }
731
732 /*
733 * Add message length
734 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 high = (ctx->total[0] >> 29)
736 | (ctx->total[1] << 3);
737 low = (ctx->total[0] << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
740 MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100743 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100745
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200746 /*
747 * Output final state
748 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
750 MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
751 MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
752 MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
753 MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
754 MBEDTLS_PUT_UINT32_BE(ctx->state[5], output, 20);
755 MBEDTLS_PUT_UINT32_BE(ctx->state[6], output, 24);
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200757#if defined(MBEDTLS_SHA224_C)
David Horstmann687262c2022-10-06 17:54:57 +0100758 truncated = ctx->is224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200759#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (!truncated) {
761 MBEDTLS_PUT_UINT32_BE(ctx->state[7], output, 28);
762 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100763
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100764 ret = 0;
765
766exit:
767 mbedtls_sha256_free(ctx);
768 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000769}
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#endif /* !MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200772
Paul Bakker5121ce52009-01-03 21:22:43 +0000773/*
774 * output = SHA-256( input buffer )
775 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776int mbedtls_sha256(const unsigned char *input,
777 size_t ilen,
778 unsigned char *output,
779 int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000780{
Janos Follath24eed8d2019-11-22 13:21:35 +0000781 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_sha256_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
Valerio Settia3f99592022-12-14 10:56:54 +0100784#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (is224 != 0 && is224 != 1) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100786 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 }
Valerio Settia3f99592022-12-14 10:56:54 +0100788#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if (is224 != 0) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100790 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 }
Valerio Settia3f99592022-12-14 10:56:54 +0100792#else /* defined MBEDTLS_SHA224_C only */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100794 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 }
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200796#endif
797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 mbedtls_sha256_init(&ctx);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if ((ret = mbedtls_sha256_starts(&ctx, is224)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100801 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if ((ret = mbedtls_sha256_update(&ctx, input, ilen)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100805 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if ((ret = mbedtls_sha256_finish(&ctx, output)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100809 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100811
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100812exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 mbedtls_sha256_free(&ctx);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000816}
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000819/*
820 * FIPS-180-2 test vectors
821 */
Valerio Settia3f99592022-12-14 10:56:54 +0100822static const unsigned char sha_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000823{
824 { "abc" },
825 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
826 { "" }
827};
828
Valerio Settia3f99592022-12-14 10:56:54 +0100829static const size_t sha_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000830{
831 3, 56, 1000
832};
833
Valerio Settia3f99592022-12-14 10:56:54 +0100834typedef const unsigned char (sha_test_sum_t)[32];
835
836/*
837 * SHA-224 test vectors
838 */
839#if defined(MBEDTLS_SHA224_C)
840static sha_test_sum_t sha224_test_sum[] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000841{
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
843 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
844 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
845 0xE3, 0x6C, 0x9D, 0xA7 },
846 { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
847 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
848 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
849 0x52, 0x52, 0x25, 0x25 },
850 { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8,
851 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B,
852 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE,
Valerio Settia3f99592022-12-14 10:56:54 +0100853 0x4E, 0xE7, 0xAD, 0x67 }
854};
855#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000856
Valerio Settia3f99592022-12-14 10:56:54 +0100857/*
858 * SHA-256 test vectors
859 */
860#if defined(MBEDTLS_SHA256_C)
861static sha_test_sum_t sha256_test_sum[] =
862{
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
864 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
865 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
866 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
867 { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
868 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
869 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
870 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
871 { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
872 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
873 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
874 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
875};
Valerio Settia3f99592022-12-14 10:56:54 +0100876#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000877
878/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000879 * Checkup routine
880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881static int mbedtls_sha256_common_self_test(int verbose, int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000882{
Valerio Settia3f99592022-12-14 10:56:54 +0100883 int i, buflen, ret = 0;
Russ Butlerbb83b422016-10-12 17:36:50 -0500884 unsigned char *buf;
Paul Bakker9e36f042013-06-30 14:34:05 +0200885 unsigned char sha256sum[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_sha256_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000887
Valerio Settia3f99592022-12-14 10:56:54 +0100888#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 sha_test_sum_t *sha_test_sum = (is224) ? sha224_test_sum : sha256_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100890#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 sha_test_sum_t *sha_test_sum = sha256_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100892#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 sha_test_sum_t *sha_test_sum = sha224_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100894#endif
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 buf = mbedtls_calloc(1024, sizeof(unsigned char));
897 if (NULL == buf) {
898 if (verbose != 0) {
899 mbedtls_printf("Buffer allocation failed\n");
900 }
Russ Butlerbb83b422016-10-12 17:36:50 -0500901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 return 1;
Russ Butlerbb83b422016-10-12 17:36:50 -0500903 }
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 mbedtls_sha256_init(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 for (i = 0; i < 3; i++) {
908 if (verbose != 0) {
909 mbedtls_printf(" SHA-%d test #%d: ", 256 - is224 * 32, i + 1);
910 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if ((ret = mbedtls_sha256_starts(&ctx, is224)) != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100913 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (i == 2) {
917 memset(buf, 'a', buflen = 1000);
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 for (int j = 0; j < 1000; j++) {
920 ret = mbedtls_sha256_update(&ctx, buf, buflen);
921 if (ret != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100922 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 }
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100924 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 } else {
927 ret = mbedtls_sha256_update(&ctx, sha_test_buf[i],
928 sha_test_buflen[i]);
929 if (ret != 0) {
930 goto fail;
931 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100932 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if ((ret = mbedtls_sha256_finish(&ctx, sha256sum)) != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100935 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100937
Paul Bakker5121ce52009-01-03 21:22:43 +0000938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (memcmp(sha256sum, sha_test_sum[i], 32 - is224 * 4) != 0) {
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100940 ret = 1;
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100941 goto fail;
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100942 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (verbose != 0) {
945 mbedtls_printf("passed\n");
946 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 }
948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (verbose != 0) {
950 mbedtls_printf("\n");
951 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100953 goto exit;
954
955fail:
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (verbose != 0) {
957 mbedtls_printf("failed\n");
958 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100959
Paul Bakker5b4af392014-06-26 12:09:34 +0200960exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 mbedtls_sha256_free(&ctx);
962 mbedtls_free(buf);
Paul Bakker5b4af392014-06-26 12:09:34 +0200963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000965}
966
Valerio Settia3f99592022-12-14 10:56:54 +0100967#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100968int mbedtls_sha256_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, 0);
Valerio Settia3f99592022-12-14 10:56:54 +0100971}
972#endif /* MBEDTLS_SHA256_C */
973
974#if defined(MBEDTLS_SHA224_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100975int mbedtls_sha224_self_test(int verbose)
Valerio Settia3f99592022-12-14 10:56:54 +0100976{
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return mbedtls_sha256_common_self_test(verbose, 1);
Valerio Settia3f99592022-12-14 10:56:54 +0100978}
979#endif /* MBEDTLS_SHA224_C */
980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Valerio Settia3f99592022-12-14 10:56:54 +0100983#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA224_C */