blob: 4321765519cf17ec29cbd546861b668c50b16e9f [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Valerio Settia3f99592022-12-14 10:56:54 +010027#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/sha256.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010036
Tom Cosgrovef3ebd902022-02-20 22:25:31 +000037#if defined(__aarch64__)
38# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +010039 defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
Jerry Yu35f2b262023-02-15 11:35:55 +080040/* *INDENT-OFF* */
41# if !defined(__ARM_FEATURE_CRYPTO)
Jerry Yu64e5d4a2023-02-15 11:46:57 +080042# if defined(__clang__)
43# if __clang_major__ < 18
44 /* TODO: Re-consider above after https://reviews.llvm.org/D131064
45 * merged.
46 *
47 * The intrinsic declaration are guarded with ACLE predefined macros
48 * in clang, and those macros are only enabled with command line.
49 * Define the macros can enable those declaration and avoid compile
50 * error on it.
51 */
52# define __ARM_FEATURE_CRYPTO 1
53# endif
54# pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function)
55# define MBEDTLS_POP_TARGET_PRAGMA
56# elif defined(__GNUC__)
57# if __GNUC__ < 6 /* TODO: check sha256 compatible for GCC */
58# error "A more recent GCC is required for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
59# else
60# pragma GCC target ("arch=armv8-a+crypto")
61# endif
62# else
63# error "Only GCC and Clang supported for MBEDTLS_SHA256_USE_A64_CRYPTO_*"
64# endif
Jerry Yu35f2b262023-02-15 11:35:55 +080065# endif
66/* *INDENT-ON* */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +000067# include <arm_neon.h>
68# endif
Tom Cosgroveb7f5b972022-03-15 11:26:55 +000069# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
70# if defined(__unix__)
71# if defined(__linux__)
Gilles Peskine449bd832023-01-11 14:50:10 +010072/* Our preferred method of detection is getauxval() */
Tom Cosgroveb7f5b972022-03-15 11:26:55 +000073# include <sys/auxv.h>
74# endif
Gilles Peskine449bd832023-01-11 14:50:10 +010075/* Use SIGILL on Unix, and fall back to it on Linux */
Tom Cosgroveb7f5b972022-03-15 11:26:55 +000076# include <signal.h>
77# endif
Tom Cosgrovef3ebd902022-02-20 22:25:31 +000078# endif
Tom Cosgroveb9987fc2022-02-21 12:26:11 +000079#elif defined(_M_ARM64)
80# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +010081 defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
Tom Cosgroveb9987fc2022-02-21 12:26:11 +000082# include <arm64_neon.h>
83# endif
Tom Cosgrovef3ebd902022-02-20 22:25:31 +000084#else
85# undef MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY
86# undef MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
87#endif
88
89#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
90/*
91 * Capability detection code comes early, so we can disable
92 * MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT if no detection mechanism found
93 */
94#if defined(HWCAP_SHA2)
Gilles Peskine449bd832023-01-11 14:50:10 +010095static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +000096{
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return (getauxval(AT_HWCAP) & HWCAP_SHA2) ? 1 : 0;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +000098}
99#elif defined(__APPLE__)
Gilles Peskine449bd832023-01-11 14:50:10 +0100100static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000101{
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return 1;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000103}
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000104#elif defined(_M_ARM64)
105#define WIN32_LEAN_AND_MEAN
106#include <Windows.h>
107#include <processthreadsapi.h>
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000110{
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 return IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) ?
112 1 : 0;
Tom Cosgroveb9987fc2022-02-21 12:26:11 +0000113}
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000114#elif defined(__unix__) && defined(SIG_SETMASK)
115/* Detection with SIGILL, setjmp() and longjmp() */
116#include <signal.h>
117#include <setjmp.h>
118
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000119static jmp_buf return_from_sigill;
120
121/*
122 * A64 SHA256 support detection via SIGILL
123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124static void sigill_handler(int signal)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000125{
126 (void) signal;
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 longjmp(return_from_sigill, 1);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000128}
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130static int mbedtls_a64_crypto_sha256_determine_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000131{
132 struct sigaction old_action, new_action;
133
134 sigset_t old_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if (sigprocmask(0, NULL, &old_mask)) {
136 return 0;
137 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 sigemptyset(&new_action.sa_mask);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000140 new_action.sa_flags = 0;
141 new_action.sa_handler = sigill_handler;
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 sigaction(SIGILL, &new_action, &old_action);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000144
145 static int ret = 0;
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (setjmp(return_from_sigill) == 0) { /* First return only */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000148 /* If this traps, we will return a second time from setjmp() with 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 asm ("sha256h q0, q0, v0.4s" : : : "v0");
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000150 ret = 1;
151 }
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 sigaction(SIGILL, &old_action, NULL);
154 sigprocmask(SIG_SETMASK, &old_mask, NULL);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return ret;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000157}
158#else
159#warning "No mechanism to detect A64_CRYPTO found, using C code only"
160#undef MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
161#endif /* HWCAP_SHA2, __APPLE__, __unix__ && SIG_SETMASK */
162
163#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT */
164
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +0200165#if !defined(MBEDTLS_SHA256_ALT)
166
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000167#define SHA256_BLOCK_SIZE 64
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +0200170{
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 memset(ctx, 0, sizeof(mbedtls_sha256_context));
Paul Bakker5b4af392014-06-26 12:09:34 +0200172}
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +0200175{
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (ctx == NULL) {
Paul Bakker5b4af392014-06-26 12:09:34 +0200177 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 }
Paul Bakker5b4af392014-06-26 12:09:34 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha256_context));
Paul Bakker5b4af392014-06-26 12:09:34 +0200181}
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
184 const mbedtls_sha256_context *src)
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200185{
186 *dst = *src;
187}
188
Paul Bakker5121ce52009-01-03 21:22:43 +0000189/*
190 * SHA-256 context setup
191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192int mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
Valerio Settia3f99592022-12-14 10:56:54 +0100194#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (is224 != 0 && is224 != 1) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100196 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 }
Valerio Settia3f99592022-12-14 10:56:54 +0100198#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (is224 != 0) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100200 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 }
Valerio Settia3f99592022-12-14 10:56:54 +0100202#else /* defined MBEDTLS_SHA224_C only */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100204 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 }
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200206#endif
207
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 ctx->total[0] = 0;
209 ctx->total[1] = 0;
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100212#if defined(MBEDTLS_SHA256_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 ctx->state[0] = 0x6A09E667;
214 ctx->state[1] = 0xBB67AE85;
215 ctx->state[2] = 0x3C6EF372;
216 ctx->state[3] = 0xA54FF53A;
217 ctx->state[4] = 0x510E527F;
218 ctx->state[5] = 0x9B05688C;
219 ctx->state[6] = 0x1F83D9AB;
220 ctx->state[7] = 0x5BE0CD19;
Valerio Settia3f99592022-12-14 10:56:54 +0100221#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 } else {
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200223#if defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 ctx->state[0] = 0xC1059ED8;
225 ctx->state[1] = 0x367CD507;
226 ctx->state[2] = 0x3070DD17;
227 ctx->state[3] = 0xF70E5939;
228 ctx->state[4] = 0xFFC00B31;
229 ctx->state[5] = 0x68581511;
230 ctx->state[6] = 0x64F98FA7;
231 ctx->state[7] = 0xBEFA4FA4;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200232#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 }
234
Valerio Settia3f99592022-12-14 10:56:54 +0100235#if defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 ctx->is224 = is224;
Valerio Settia3f99592022-12-14 10:56:54 +0100237#endif
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000240}
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if !defined(MBEDTLS_SHA256_PROCESS_ALT)
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200243static const uint32_t K[] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000244{
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200245 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
246 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
247 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
248 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
249 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
250 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
251 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
252 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
253 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
254 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
255 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
256 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
257 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
258 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
259 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
260 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
261};
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000263#endif
264
265#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
266 defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
267
268#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
269# define mbedtls_internal_sha256_process_many_a64_crypto mbedtls_internal_sha256_process_many
270# define mbedtls_internal_sha256_process_a64_crypto mbedtls_internal_sha256_process
271#endif
272
273static size_t mbedtls_internal_sha256_process_many_a64_crypto(
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_sha256_context *ctx, const uint8_t *msg, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000275{
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 uint32x4_t abcd = vld1q_u32(&ctx->state[0]);
277 uint32x4_t efgh = vld1q_u32(&ctx->state[4]);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000278
279 size_t processed = 0;
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 for (;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000282 len >= SHA256_BLOCK_SIZE;
283 processed += SHA256_BLOCK_SIZE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 msg += SHA256_BLOCK_SIZE,
285 len -= SHA256_BLOCK_SIZE) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000286 uint32x4_t tmp, abcd_prev;
287
288 uint32x4_t abcd_orig = abcd;
289 uint32x4_t efgh_orig = efgh;
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 uint32x4_t sched0 = (uint32x4_t) vld1q_u8(msg + 16 * 0);
292 uint32x4_t sched1 = (uint32x4_t) vld1q_u8(msg + 16 * 1);
293 uint32x4_t sched2 = (uint32x4_t) vld1q_u8(msg + 16 * 2);
294 uint32x4_t sched3 = (uint32x4_t) vld1q_u8(msg + 16 * 3);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000295
296#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ /* Will be true if not defined */
297 /* Untested on BE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 sched0 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched0)));
299 sched1 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched1)));
300 sched2 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched2)));
301 sched3 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(sched3)));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000302#endif
303
304 /* Rounds 0 to 3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 tmp = vaddq_u32(sched0, vld1q_u32(&K[0]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000306 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
308 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000309
310 /* Rounds 4 to 7 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 tmp = vaddq_u32(sched1, vld1q_u32(&K[4]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000312 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
314 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000315
316 /* Rounds 8 to 11 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 tmp = vaddq_u32(sched2, vld1q_u32(&K[8]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000318 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
320 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000321
322 /* Rounds 12 to 15 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 tmp = vaddq_u32(sched3, vld1q_u32(&K[12]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000324 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
326 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 for (int t = 16; t < 64; t += 16) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000329 /* Rounds t to t + 3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 sched0 = vsha256su1q_u32(vsha256su0q_u32(sched0, sched1), sched2, sched3);
331 tmp = vaddq_u32(sched0, vld1q_u32(&K[t]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000332 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
334 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000335
336 /* Rounds t + 4 to t + 7 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 sched1 = vsha256su1q_u32(vsha256su0q_u32(sched1, sched2), sched3, sched0);
338 tmp = vaddq_u32(sched1, vld1q_u32(&K[t + 4]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000339 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
341 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000342
343 /* Rounds t + 8 to t + 11 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 sched2 = vsha256su1q_u32(vsha256su0q_u32(sched2, sched3), sched0, sched1);
345 tmp = vaddq_u32(sched2, vld1q_u32(&K[t + 8]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000346 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
348 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000349
350 /* Rounds t + 12 to t + 15 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 sched3 = vsha256su1q_u32(vsha256su0q_u32(sched3, sched0), sched1, sched2);
352 tmp = vaddq_u32(sched3, vld1q_u32(&K[t + 12]));
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000353 abcd_prev = abcd;
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 abcd = vsha256hq_u32(abcd_prev, efgh, tmp);
355 efgh = vsha256h2q_u32(efgh, abcd_prev, tmp);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000356 }
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 abcd = vaddq_u32(abcd, abcd_orig);
359 efgh = vaddq_u32(efgh, efgh_orig);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000360 }
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 vst1q_u32(&ctx->state[0], abcd);
363 vst1q_u32(&ctx->state[4], efgh);
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return processed;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000366}
367
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100368#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
369/*
370 * This function is for internal use only if we are building both C and A64
371 * versions, otherwise it is renamed to be the public mbedtls_internal_sha256_process()
372 */
373static
374#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100375int mbedtls_internal_sha256_process_a64_crypto(mbedtls_sha256_context *ctx,
376 const unsigned char data[SHA256_BLOCK_SIZE])
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000377{
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return (mbedtls_internal_sha256_process_many_a64_crypto(ctx, data,
379 SHA256_BLOCK_SIZE) ==
380 SHA256_BLOCK_SIZE) ? 0 : -1;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000381}
382
383#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT || MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
384
385
386#if !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
387#define mbedtls_internal_sha256_process_many_c mbedtls_internal_sha256_process_many
388#define mbedtls_internal_sha256_process_c mbedtls_internal_sha256_process
389#endif
390
391
392#if !defined(MBEDTLS_SHA256_PROCESS_ALT) && \
393 !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395#define SHR(x, n) (((x) & 0xFFFFFFFF) >> (n))
396#define ROTR(x, n) (SHR(x, n) | ((x) << (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398#define S0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
399#define S1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401#define S2(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
402#define S3(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404#define F0(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
405#define F1(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200407#define R(t) \
408 ( \
409 local.W[t] = S1(local.W[(t) - 2]) + local.W[(t) - 7] + \
410 S0(local.W[(t) - 15]) + local.W[(t) - 16] \
Hanno Becker1eeca412018-10-15 12:01:35 +0100411 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413#define P(a, b, c, d, e, f, g, h, x, K) \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200414 do \
415 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 local.temp1 = (h) + S3(e) + F1((e), (f), (g)) + (K) + (x); \
417 local.temp2 = S2(a) + F0((a), (b), (c)); \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200418 (d) += local.temp1; (h) = local.temp1 + local.temp2; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
Tom Cosgrovec144ca62022-04-19 13:52:24 +0100421#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
422/*
423 * This function is for internal use only if we are building both C and A64
424 * versions, otherwise it is renamed to be the public mbedtls_internal_sha256_process()
425 */
426static
427#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100428int mbedtls_internal_sha256_process_c(mbedtls_sha256_context *ctx,
429 const unsigned char data[SHA256_BLOCK_SIZE])
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200430{
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 struct {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200432 uint32_t temp1, temp2, W[64];
433 uint32_t A[8];
434 } local;
435
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200436 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 for (i = 0; i < 8; i++) {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200439 local.A[i] = ctx->state[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 }
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200441
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200442#if defined(MBEDTLS_SHA256_SMALLER)
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 for (i = 0; i < 64; i++) {
444 if (i < 16) {
445 local.W[i] = MBEDTLS_GET_UINT32_BE(data, 4 * i);
446 } else {
447 R(i);
448 }
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
451 local.A[5], local.A[6], local.A[7], local.W[i], K[i]);
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200452
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200453 local.temp1 = local.A[7]; local.A[7] = local.A[6];
454 local.A[6] = local.A[5]; local.A[5] = local.A[4];
455 local.A[4] = local.A[3]; local.A[3] = local.A[2];
456 local.A[2] = local.A[1]; local.A[1] = local.A[0];
457 local.A[0] = local.temp1;
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200458 }
459#else /* MBEDTLS_SHA256_SMALLER */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 for (i = 0; i < 16; i++) {
461 local.W[i] = MBEDTLS_GET_UINT32_BE(data, 4 * i);
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200462 }
463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 for (i = 0; i < 16; i += 8) {
465 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
466 local.A[5], local.A[6], local.A[7], local.W[i+0], K[i+0]);
467 P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
468 local.A[4], local.A[5], local.A[6], local.W[i+1], K[i+1]);
469 P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
470 local.A[3], local.A[4], local.A[5], local.W[i+2], K[i+2]);
471 P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
472 local.A[2], local.A[3], local.A[4], local.W[i+3], K[i+3]);
473 P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
474 local.A[1], local.A[2], local.A[3], local.W[i+4], K[i+4]);
475 P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
476 local.A[0], local.A[1], local.A[2], local.W[i+5], K[i+5]);
477 P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
478 local.A[7], local.A[0], local.A[1], local.W[i+6], K[i+6]);
479 P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
480 local.A[6], local.A[7], local.A[0], local.W[i+7], K[i+7]);
481 }
482
483 for (i = 16; i < 64; i += 8) {
484 P(local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
485 local.A[5], local.A[6], local.A[7], R(i+0), K[i+0]);
486 P(local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
487 local.A[4], local.A[5], local.A[6], R(i+1), K[i+1]);
488 P(local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
489 local.A[3], local.A[4], local.A[5], R(i+2), K[i+2]);
490 P(local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
491 local.A[2], local.A[3], local.A[4], R(i+3), K[i+3]);
492 P(local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
493 local.A[1], local.A[2], local.A[3], R(i+4), K[i+4]);
494 P(local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
495 local.A[0], local.A[1], local.A[2], R(i+5), K[i+5]);
496 P(local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
497 local.A[7], local.A[0], local.A[1], R(i+6), K[i+6]);
498 P(local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
499 local.A[6], local.A[7], local.A[0], R(i+7), K[i+7]);
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200500 }
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200501#endif /* MBEDTLS_SHA256_SMALLER */
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 for (i = 0; i < 8; i++) {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200504 ctx->state[i] += local.A[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100506
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200507 /* Zeroise buffers and variables to clear sensitive data from memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 mbedtls_platform_zeroize(&local, sizeof(local));
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000511}
Jaeden Amero041039f2018-02-19 15:28:08 +0000512
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000513#endif /* !MBEDTLS_SHA256_PROCESS_ALT && !MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
514
515
516#if !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
517
518static size_t mbedtls_internal_sha256_process_many_c(
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 mbedtls_sha256_context *ctx, const uint8_t *data, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000520{
521 size_t processed = 0;
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 while (len >= SHA256_BLOCK_SIZE) {
524 if (mbedtls_internal_sha256_process_c(ctx, data) != 0) {
525 return 0;
526 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000527
528 data += SHA256_BLOCK_SIZE;
529 len -= SHA256_BLOCK_SIZE;
530
531 processed += SHA256_BLOCK_SIZE;
532 }
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return processed;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000535}
536
537#endif /* !MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
538
539
540#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542static int mbedtls_a64_crypto_sha256_has_support(void)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000543{
544 static int done = 0;
545 static int supported = 0;
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (!done) {
Tom Cosgrove7e7aba82022-02-24 08:33:11 +0000548 supported = mbedtls_a64_crypto_sha256_determine_support();
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000549 done = 1;
550 }
551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return supported;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000553}
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555static size_t mbedtls_internal_sha256_process_many(mbedtls_sha256_context *ctx,
556 const uint8_t *msg, size_t len)
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000557{
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (mbedtls_a64_crypto_sha256_has_support()) {
559 return mbedtls_internal_sha256_process_many_a64_crypto(ctx, msg, len);
560 } else {
561 return mbedtls_internal_sha256_process_many_c(ctx, msg, len);
562 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000563}
564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
566 const unsigned char data[SHA256_BLOCK_SIZE])
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000567{
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (mbedtls_a64_crypto_sha256_has_support()) {
569 return mbedtls_internal_sha256_process_a64_crypto(ctx, data);
570 } else {
571 return mbedtls_internal_sha256_process_c(ctx, data);
572 }
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000573}
574
575#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT */
576
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578/*
579 * SHA-256 process buffer
580 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581int mbedtls_sha256_update(mbedtls_sha256_context *ctx,
582 const unsigned char *input,
583 size_t ilen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000584{
Janos Follath24eed8d2019-11-22 13:21:35 +0000585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000586 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000587 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (ilen == 0) {
590 return 0;
591 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000592
593 left = ctx->total[0] & 0x3F;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000594 fill = SHA256_BLOCK_SIZE - left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
Paul Bakker5c2364c2012-10-01 14:41:15 +0000596 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 ctx->total[0] &= 0xFFFFFFFF;
598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (ctx->total[0] < (uint32_t) ilen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 ctx->total[1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (left && ilen >= fill) {
604 memcpy((void *) (ctx->buffer + left), input, fill);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
607 return ret;
608 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100609
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 input += fill;
611 ilen -= fill;
612 left = 0;
613 }
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 while (ilen >= SHA256_BLOCK_SIZE) {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000616 size_t processed =
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 mbedtls_internal_sha256_process_many(ctx, input, ilen);
618 if (processed < SHA256_BLOCK_SIZE) {
619 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
620 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100621
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000622 input += processed;
623 ilen -= processed;
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 }
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (ilen > 0) {
627 memcpy((void *) (ctx->buffer + left), input, ilen);
628 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000631}
632
Paul Bakker5121ce52009-01-03 21:22:43 +0000633/*
634 * SHA-256 final digest
635 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100636int mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
637 unsigned char *output)
Paul Bakker5121ce52009-01-03 21:22:43 +0000638{
Janos Follath24eed8d2019-11-22 13:21:35 +0000639 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200640 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000641 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200643 /*
644 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
645 */
646 used = ctx->total[0] & 0x3F;
647
648 ctx->buffer[used++] = 0x80;
649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (used <= 56) {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200651 /* Enough room for padding + length in current block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 memset(ctx->buffer + used, 0, 56 - used);
653 } else {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200654 /* We'll need an extra block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 memset(ctx->buffer + used, 0, SHA256_BLOCK_SIZE - used);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
658 return ret;
659 }
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 memset(ctx->buffer, 0, 56);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200662 }
663
664 /*
665 * Add message length
666 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 high = (ctx->total[0] >> 29)
668 | (ctx->total[1] << 3);
669 low = (ctx->total[0] << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
672 MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if ((ret = mbedtls_internal_sha256_process(ctx, ctx->buffer)) != 0) {
675 return ret;
676 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100677
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200678 /*
679 * Output final state
680 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
682 MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
683 MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
684 MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
685 MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
686 MBEDTLS_PUT_UINT32_BE(ctx->state[5], output, 20);
687 MBEDTLS_PUT_UINT32_BE(ctx->state[6], output, 24);
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
David Horstmann687262c2022-10-06 17:54:57 +0100689 int truncated = 0;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200690#if defined(MBEDTLS_SHA224_C)
David Horstmann687262c2022-10-06 17:54:57 +0100691 truncated = ctx->is224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200692#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (!truncated) {
694 MBEDTLS_PUT_UINT32_BE(ctx->state[7], output, 28);
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#endif /* !MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200701
Paul Bakker5121ce52009-01-03 21:22:43 +0000702/*
703 * output = SHA-256( input buffer )
704 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705int mbedtls_sha256(const unsigned char *input,
706 size_t ilen,
707 unsigned char *output,
708 int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000709{
Janos Follath24eed8d2019-11-22 13:21:35 +0000710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_sha256_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000712
Valerio Settia3f99592022-12-14 10:56:54 +0100713#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (is224 != 0 && is224 != 1) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100715 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 }
Valerio Settia3f99592022-12-14 10:56:54 +0100717#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (is224 != 0) {
Tuvshinzaya Erdenekhuu696dfb62022-08-05 15:59:19 +0100719 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 }
Valerio Settia3f99592022-12-14 10:56:54 +0100721#else /* defined MBEDTLS_SHA224_C only */
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if (is224 == 0) {
Valerio Settia3f99592022-12-14 10:56:54 +0100723 return MBEDTLS_ERR_SHA256_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 }
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200725#endif
726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 mbedtls_sha256_init(&ctx);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if ((ret = mbedtls_sha256_starts(&ctx, is224)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100730 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if ((ret = mbedtls_sha256_update(&ctx, input, ilen)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100734 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if ((ret = mbedtls_sha256_finish(&ctx, output)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100738 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100740
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100741exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 mbedtls_sha256_free(&ctx);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000745}
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000748/*
749 * FIPS-180-2 test vectors
750 */
Valerio Settia3f99592022-12-14 10:56:54 +0100751static const unsigned char sha_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000752{
753 { "abc" },
754 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
755 { "" }
756};
757
Valerio Settia3f99592022-12-14 10:56:54 +0100758static const size_t sha_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000759{
760 3, 56, 1000
761};
762
Valerio Settia3f99592022-12-14 10:56:54 +0100763typedef const unsigned char (sha_test_sum_t)[32];
764
765/*
766 * SHA-224 test vectors
767 */
768#if defined(MBEDTLS_SHA224_C)
769static sha_test_sum_t sha224_test_sum[] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000770{
Paul Bakker5121ce52009-01-03 21:22:43 +0000771 { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
772 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
773 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
774 0xE3, 0x6C, 0x9D, 0xA7 },
775 { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
776 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
777 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
778 0x52, 0x52, 0x25, 0x25 },
779 { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8,
780 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B,
781 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE,
Valerio Settia3f99592022-12-14 10:56:54 +0100782 0x4E, 0xE7, 0xAD, 0x67 }
783};
784#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000785
Valerio Settia3f99592022-12-14 10:56:54 +0100786/*
787 * SHA-256 test vectors
788 */
789#if defined(MBEDTLS_SHA256_C)
790static sha_test_sum_t sha256_test_sum[] =
791{
Paul Bakker5121ce52009-01-03 21:22:43 +0000792 { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
793 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
794 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
795 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
796 { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
797 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
798 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
799 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
800 { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
801 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
802 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
803 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
804};
Valerio Settia3f99592022-12-14 10:56:54 +0100805#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
807/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000808 * Checkup routine
809 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810static int mbedtls_sha256_common_self_test(int verbose, int is224)
Paul Bakker5121ce52009-01-03 21:22:43 +0000811{
Valerio Settia3f99592022-12-14 10:56:54 +0100812 int i, buflen, ret = 0;
Russ Butlerbb83b422016-10-12 17:36:50 -0500813 unsigned char *buf;
Paul Bakker9e36f042013-06-30 14:34:05 +0200814 unsigned char sha256sum[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_sha256_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000816
Valerio Settia3f99592022-12-14 10:56:54 +0100817#if defined(MBEDTLS_SHA224_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 sha_test_sum_t *sha_test_sum = (is224) ? sha224_test_sum : sha256_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100819#elif defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 sha_test_sum_t *sha_test_sum = sha256_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100821#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 sha_test_sum_t *sha_test_sum = sha224_test_sum;
Valerio Settia3f99592022-12-14 10:56:54 +0100823#endif
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 buf = mbedtls_calloc(1024, sizeof(unsigned char));
826 if (NULL == buf) {
827 if (verbose != 0) {
828 mbedtls_printf("Buffer allocation failed\n");
829 }
Russ Butlerbb83b422016-10-12 17:36:50 -0500830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 return 1;
Russ Butlerbb83b422016-10-12 17:36:50 -0500832 }
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 mbedtls_sha256_init(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 for (i = 0; i < 3; i++) {
837 if (verbose != 0) {
838 mbedtls_printf(" SHA-%d test #%d: ", 256 - is224 * 32, i + 1);
839 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if ((ret = mbedtls_sha256_starts(&ctx, is224)) != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100842 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (i == 2) {
846 memset(buf, 'a', buflen = 1000);
Paul Bakker5121ce52009-01-03 21:22:43 +0000847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 for (int j = 0; j < 1000; j++) {
849 ret = mbedtls_sha256_update(&ctx, buf, buflen);
850 if (ret != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100851 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 }
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100853 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 } else {
856 ret = mbedtls_sha256_update(&ctx, sha_test_buf[i],
857 sha_test_buflen[i]);
858 if (ret != 0) {
859 goto fail;
860 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100861 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if ((ret = mbedtls_sha256_finish(&ctx, sha256sum)) != 0) {
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100864 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100866
Paul Bakker5121ce52009-01-03 21:22:43 +0000867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if (memcmp(sha256sum, sha_test_sum[i], 32 - is224 * 4) != 0) {
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100869 ret = 1;
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100870 goto fail;
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100871 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (verbose != 0) {
874 mbedtls_printf("passed\n");
875 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 }
877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (verbose != 0) {
879 mbedtls_printf("\n");
880 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100882 goto exit;
883
884fail:
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (verbose != 0) {
886 mbedtls_printf("failed\n");
887 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100888
Paul Bakker5b4af392014-06-26 12:09:34 +0200889exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 mbedtls_sha256_free(&ctx);
891 mbedtls_free(buf);
Paul Bakker5b4af392014-06-26 12:09:34 +0200892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000894}
895
Valerio Settia3f99592022-12-14 10:56:54 +0100896#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100897int mbedtls_sha256_self_test(int verbose)
Valerio Settia3f99592022-12-14 10:56:54 +0100898{
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 return mbedtls_sha256_common_self_test(verbose, 0);
Valerio Settia3f99592022-12-14 10:56:54 +0100900}
901#endif /* MBEDTLS_SHA256_C */
902
903#if defined(MBEDTLS_SHA224_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100904int mbedtls_sha224_self_test(int verbose)
Valerio Settia3f99592022-12-14 10:56:54 +0100905{
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 return mbedtls_sha256_common_self_test(verbose, 1);
Valerio Settia3f99592022-12-14 10:56:54 +0100907}
908#endif /* MBEDTLS_SHA224_C */
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Valerio Settia3f99592022-12-14 10:56:54 +0100912#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA224_C */