blob: 0db5f4d79d1f6888bcf0fb83f122f84c64984a9c [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_SHA256_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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_SELF_TEST)
36#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010038#else
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <stdio.h>
Russ Butlerbb83b422016-10-12 17:36:50 -050040#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#define mbedtls_printf printf
Russ Butlerbb83b422016-10-12 17:36:50 -050042#define mbedtls_calloc calloc
43#define mbedtls_free free
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#endif /* MBEDTLS_PLATFORM_C */
45#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010046
Tom Cosgrovef3ebd902022-02-20 22:25:31 +000047#if defined(__aarch64__)
48# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
49 defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
50# include <arm_neon.h>
51# endif
52# if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) && defined(__linux__)
53# include <sys/auxv.h>
54# endif
55#else
56# undef MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY
57# undef MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
58#endif
59
60#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
61/*
62 * Capability detection code comes early, so we can disable
63 * MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT if no detection mechanism found
64 */
65#if defined(HWCAP_SHA2)
66static int mbedtls_a64_crypto_sha256_check_support( void )
67{
68 return( ( getauxval( AT_HWCAP ) & HWCAP_SHA2 ) ? 1 : 0 );
69}
70#elif defined(__APPLE__)
71static int mbedtls_a64_crypto_sha256_check_support( void )
72{
73 return( 1 );
74}
75#elif defined(__unix__) && defined(SIG_SETMASK)
76/* Detection with SIGILL, setjmp() and longjmp() */
77#include <signal.h>
78#include <setjmp.h>
79
80#ifndef asm
81#define asm __asm__
82#endif
83
84static jmp_buf return_from_sigill;
85
86/*
87 * A64 SHA256 support detection via SIGILL
88 */
89static void sigill_handler( int signal )
90{
91 (void) signal;
92 longjmp( return_from_sigill, 1 );
93}
94
95static int mbedtls_a64_crypto_sha256_check_support( void )
96{
97 struct sigaction old_action, new_action;
98
99 sigset_t old_mask;
100 if( sigprocmask( 0, NULL, &old_mask ) )
101 return( 0 );
102
103 sigemptyset( &new_action.sa_mask );
104 new_action.sa_flags = 0;
105 new_action.sa_handler = sigill_handler;
106
107 sigaction( SIGILL, &new_action, &old_action );
108
109 static int ret = 0;
110
111 if( setjmp( return_from_sigill ) == 0 ) /* First return only */
112 {
113 /* If this traps, we will return a second time from setjmp() with 1 */
114 asm( "sha256h q0, q0, v0.4s" : : : "v0" );
115 ret = 1;
116 }
117
118 sigaction( SIGILL, &old_action, NULL );
119 sigprocmask( SIG_SETMASK, &old_mask, NULL );
120
121 return( ret );
122}
123#else
124#warning "No mechanism to detect A64_CRYPTO found, using C code only"
125#undef MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT
126#endif /* HWCAP_SHA2, __APPLE__, __unix__ && SIG_SETMASK */
127
128#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT */
129
Hanno Becker2f6de422018-12-20 10:22:32 +0000130#define SHA256_VALIDATE_RET(cond) \
131 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA256_BAD_INPUT_DATA )
132#define SHA256_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
133
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +0200134#if !defined(MBEDTLS_SHA256_ALT)
135
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000136#define SHA256_BLOCK_SIZE 64
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200139{
Hanno Becker8d215e72018-12-18 17:53:21 +0000140 SHA256_VALIDATE( ctx != NULL );
Andres Amaya Garcia79e593f2018-12-09 20:41:20 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200143}
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200146{
147 if( ctx == NULL )
148 return;
149
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500150 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200151}
152
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200153void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
154 const mbedtls_sha256_context *src )
155{
Hanno Becker8d215e72018-12-18 17:53:21 +0000156 SHA256_VALIDATE( dst != NULL );
157 SHA256_VALIDATE( src != NULL );
Andres Amaya Garcia79e593f2018-12-09 20:41:20 +0000158
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200159 *dst = *src;
160}
161
Paul Bakker5121ce52009-01-03 21:22:43 +0000162/*
163 * SHA-256 context setup
164 */
TRodziewicz26371e42021-06-08 16:45:41 +0200165int mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000166{
Hanno Becker8d215e72018-12-18 17:53:21 +0000167 SHA256_VALIDATE_RET( ctx != NULL );
Andres Amaya Garcia79e593f2018-12-09 20:41:20 +0000168
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200169#if defined(MBEDTLS_SHA224_C)
170 SHA256_VALIDATE_RET( is224 == 0 || is224 == 1 );
171#else
172 SHA256_VALIDATE_RET( is224 == 0 );
173#endif
174
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 ctx->total[0] = 0;
176 ctx->total[1] = 0;
177
178 if( is224 == 0 )
179 {
180 /* SHA-256 */
181 ctx->state[0] = 0x6A09E667;
182 ctx->state[1] = 0xBB67AE85;
183 ctx->state[2] = 0x3C6EF372;
184 ctx->state[3] = 0xA54FF53A;
185 ctx->state[4] = 0x510E527F;
186 ctx->state[5] = 0x9B05688C;
187 ctx->state[6] = 0x1F83D9AB;
188 ctx->state[7] = 0x5BE0CD19;
189 }
190 else
191 {
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200192#if defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 /* SHA-224 */
194 ctx->state[0] = 0xC1059ED8;
195 ctx->state[1] = 0x367CD507;
196 ctx->state[2] = 0x3070DD17;
197 ctx->state[3] = 0xF70E5939;
198 ctx->state[4] = 0xFFC00B31;
199 ctx->state[5] = 0x68581511;
200 ctx->state[6] = 0x64F98FA7;
201 ctx->state[7] = 0xBEFA4FA4;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200202#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 }
204
205 ctx->is224 = is224;
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100206
207 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208}
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if !defined(MBEDTLS_SHA256_PROCESS_ALT)
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200211static const uint32_t K[] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000212{
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200213 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
214 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
215 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
216 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
217 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
218 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
219 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
220 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
221 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
222 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
223 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
224 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
225 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
226 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
227 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
228 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
229};
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000231#endif
232
233#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT) || \
234 defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
235
236#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
237# define mbedtls_internal_sha256_process_many_a64_crypto mbedtls_internal_sha256_process_many
238# define mbedtls_internal_sha256_process_a64_crypto mbedtls_internal_sha256_process
239#endif
240
241static size_t mbedtls_internal_sha256_process_many_a64_crypto(
242 mbedtls_sha256_context *ctx, const uint8_t *msg, size_t len )
243{
244 uint32x4_t abcd = vld1q_u32( &ctx->state[0] );
245 uint32x4_t efgh = vld1q_u32( &ctx->state[4] );
246
247 size_t processed = 0;
248
249 for( ;
250 len >= SHA256_BLOCK_SIZE;
251 processed += SHA256_BLOCK_SIZE,
252 msg += SHA256_BLOCK_SIZE,
253 len -= SHA256_BLOCK_SIZE )
254 {
255 uint32x4_t tmp, abcd_prev;
256
257 uint32x4_t abcd_orig = abcd;
258 uint32x4_t efgh_orig = efgh;
259
260 uint32x4_t sched0 = vld1q_u32( (const uint32_t *)( msg + 16 * 0 ) );
261 uint32x4_t sched1 = vld1q_u32( (const uint32_t *)( msg + 16 * 1 ) );
262 uint32x4_t sched2 = vld1q_u32( (const uint32_t *)( msg + 16 * 2 ) );
263 uint32x4_t sched3 = vld1q_u32( (const uint32_t *)( msg + 16 * 3 ) );
264
265#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ /* Will be true if not defined */
266 /* Untested on BE */
267 sched0 = vreinterpretq_u32_u8( vrev32q_u8( vreinterpretq_u8_u32( sched0 ) ) );
268 sched1 = vreinterpretq_u32_u8( vrev32q_u8( vreinterpretq_u8_u32( sched1 ) ) );
269 sched2 = vreinterpretq_u32_u8( vrev32q_u8( vreinterpretq_u8_u32( sched2 ) ) );
270 sched3 = vreinterpretq_u32_u8( vrev32q_u8( vreinterpretq_u8_u32( sched3 ) ) );
271#endif
272
273 /* Rounds 0 to 3 */
274 tmp = vaddq_u32( sched0, vld1q_u32( &K[0] ) );
275 abcd_prev = abcd;
276 abcd = vsha256hq_u32( abcd_prev, efgh, tmp );
277 efgh = vsha256h2q_u32( efgh, abcd_prev, tmp );
278
279 /* Rounds 4 to 7 */
280 tmp = vaddq_u32( sched1, vld1q_u32( &K[4] ) );
281 abcd_prev = abcd;
282 abcd = vsha256hq_u32( abcd_prev, efgh, tmp );
283 efgh = vsha256h2q_u32( efgh, abcd_prev, tmp );
284
285 /* Rounds 8 to 11 */
286 tmp = vaddq_u32( sched2, vld1q_u32( &K[8] ) );
287 abcd_prev = abcd;
288 abcd = vsha256hq_u32( abcd_prev, efgh, tmp );
289 efgh = vsha256h2q_u32( efgh, abcd_prev, tmp );
290
291 /* Rounds 12 to 15 */
292 tmp = vaddq_u32( sched3, vld1q_u32( &K[12] ) );
293 abcd_prev = abcd;
294 abcd = vsha256hq_u32( abcd_prev, efgh, tmp );
295 efgh = vsha256h2q_u32( efgh, abcd_prev, tmp );
296
297 for( int t = 16; t < 64; t += 16 )
298 {
299 /* Rounds t to t + 3 */
300 sched0 = vsha256su1q_u32( vsha256su0q_u32( sched0, sched1 ), sched2, sched3 );
301 tmp = vaddq_u32( sched0, vld1q_u32( &K[t] ) );
302 abcd_prev = abcd;
303 abcd = vsha256hq_u32( abcd_prev, efgh, tmp );
304 efgh = vsha256h2q_u32( efgh, abcd_prev, tmp );
305
306 /* Rounds t + 4 to t + 7 */
307 sched1 = vsha256su1q_u32( vsha256su0q_u32( sched1, sched2 ), sched3, sched0 );
308 tmp = vaddq_u32( sched1, vld1q_u32( &K[t + 4] ) );
309 abcd_prev = abcd;
310 abcd = vsha256hq_u32( abcd_prev, efgh, tmp );
311 efgh = vsha256h2q_u32( efgh, abcd_prev, tmp );
312
313 /* Rounds t + 8 to t + 11 */
314 sched2 = vsha256su1q_u32( vsha256su0q_u32( sched2, sched3 ), sched0, sched1 );
315 tmp = vaddq_u32( sched2, vld1q_u32( &K[t + 8] ) );
316 abcd_prev = abcd;
317 abcd = vsha256hq_u32( abcd_prev, efgh, tmp );
318 efgh = vsha256h2q_u32( efgh, abcd_prev, tmp );
319
320 /* Rounds t + 12 to t + 15 */
321 sched3 = vsha256su1q_u32( vsha256su0q_u32( sched3, sched0 ), sched1, sched2 );
322 tmp = vaddq_u32( sched3, vld1q_u32( &K[t + 12] ) );
323 abcd_prev = abcd;
324 abcd = vsha256hq_u32( abcd_prev, efgh, tmp );
325 efgh = vsha256h2q_u32( efgh, abcd_prev, tmp );
326 }
327
328 abcd = vaddq_u32( abcd, abcd_orig );
329 efgh = vaddq_u32( efgh, efgh_orig );
330 }
331
332 vst1q_u32( &ctx->state[0], abcd );
333 vst1q_u32( &ctx->state[4], efgh );
334
335 return( processed );
336}
337
338int mbedtls_internal_sha256_process_a64_crypto( mbedtls_sha256_context *ctx,
339 const unsigned char data[SHA256_BLOCK_SIZE] )
340{
341 return( ( mbedtls_internal_sha256_process_many_a64_crypto( ctx, data,
342 SHA256_BLOCK_SIZE ) == SHA256_BLOCK_SIZE ) ? 0 : -1 );
343}
344
345#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT || MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
346
347
348#if !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
349#define mbedtls_internal_sha256_process_many_c mbedtls_internal_sha256_process_many
350#define mbedtls_internal_sha256_process_c mbedtls_internal_sha256_process
351#endif
352
353
354#if !defined(MBEDTLS_SHA256_PROCESS_ALT) && \
355 !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
356
Hanno Becker1eeca412018-10-15 12:01:35 +0100357#define SHR(x,n) (((x) & 0xFFFFFFFF) >> (n))
358#define ROTR(x,n) (SHR(x,n) | ((x) << (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
360#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
361#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
362
363#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
364#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
365
Hanno Becker1eeca412018-10-15 12:01:35 +0100366#define F0(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
367#define F1(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000368
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200369#define R(t) \
370 ( \
371 local.W[t] = S1(local.W[(t) - 2]) + local.W[(t) - 7] + \
372 S0(local.W[(t) - 15]) + local.W[(t) - 16] \
Hanno Becker1eeca412018-10-15 12:01:35 +0100373 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200375#define P(a,b,c,d,e,f,g,h,x,K) \
376 do \
377 { \
378 local.temp1 = (h) + S3(e) + F1((e),(f),(g)) + (K) + (x); \
379 local.temp2 = S2(a) + F0((a),(b),(c)); \
380 (d) += local.temp1; (h) = local.temp1 + local.temp2; \
Hanno Becker1eeca412018-10-15 12:01:35 +0100381 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000383int mbedtls_internal_sha256_process_c( mbedtls_sha256_context *ctx,
384 const unsigned char data[SHA256_BLOCK_SIZE] )
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200385{
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200386 struct
387 {
388 uint32_t temp1, temp2, W[64];
389 uint32_t A[8];
390 } local;
391
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200392 unsigned int i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
Hanno Becker8d215e72018-12-18 17:53:21 +0000394 SHA256_VALIDATE_RET( ctx != NULL );
395 SHA256_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garcia79e593f2018-12-09 20:41:20 +0000396
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200397 for( i = 0; i < 8; i++ )
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200398 local.A[i] = ctx->state[i];
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200399
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200400#if defined(MBEDTLS_SHA256_SMALLER)
401 for( i = 0; i < 64; i++ )
402 {
403 if( i < 16 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100404 local.W[i] = MBEDTLS_GET_UINT32_BE( data, 4 * i );
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200405 else
406 R( i );
407
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200408 P( local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
409 local.A[5], local.A[6], local.A[7], local.W[i], K[i] );
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200410
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200411 local.temp1 = local.A[7]; local.A[7] = local.A[6];
412 local.A[6] = local.A[5]; local.A[5] = local.A[4];
413 local.A[4] = local.A[3]; local.A[3] = local.A[2];
414 local.A[2] = local.A[1]; local.A[1] = local.A[0];
415 local.A[0] = local.temp1;
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200416 }
417#else /* MBEDTLS_SHA256_SMALLER */
418 for( i = 0; i < 16; i++ )
Joe Subbiani6a506312021-07-07 16:56:29 +0100419 local.W[i] = MBEDTLS_GET_UINT32_BE( data, 4 * i );
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200420
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200421 for( i = 0; i < 16; i += 8 )
422 {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200423 P( local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
424 local.A[5], local.A[6], local.A[7], local.W[i+0], K[i+0] );
425 P( local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
426 local.A[4], local.A[5], local.A[6], local.W[i+1], K[i+1] );
427 P( local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
428 local.A[3], local.A[4], local.A[5], local.W[i+2], K[i+2] );
429 P( local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
430 local.A[2], local.A[3], local.A[4], local.W[i+3], K[i+3] );
431 P( local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
432 local.A[1], local.A[2], local.A[3], local.W[i+4], K[i+4] );
433 P( local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
434 local.A[0], local.A[1], local.A[2], local.W[i+5], K[i+5] );
435 P( local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
436 local.A[7], local.A[0], local.A[1], local.W[i+6], K[i+6] );
437 P( local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
438 local.A[6], local.A[7], local.A[0], local.W[i+7], K[i+7] );
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200439 }
440
441 for( i = 16; i < 64; i += 8 )
442 {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200443 P( local.A[0], local.A[1], local.A[2], local.A[3], local.A[4],
444 local.A[5], local.A[6], local.A[7], R(i+0), K[i+0] );
445 P( local.A[7], local.A[0], local.A[1], local.A[2], local.A[3],
446 local.A[4], local.A[5], local.A[6], R(i+1), K[i+1] );
447 P( local.A[6], local.A[7], local.A[0], local.A[1], local.A[2],
448 local.A[3], local.A[4], local.A[5], R(i+2), K[i+2] );
449 P( local.A[5], local.A[6], local.A[7], local.A[0], local.A[1],
450 local.A[2], local.A[3], local.A[4], R(i+3), K[i+3] );
451 P( local.A[4], local.A[5], local.A[6], local.A[7], local.A[0],
452 local.A[1], local.A[2], local.A[3], R(i+4), K[i+4] );
453 P( local.A[3], local.A[4], local.A[5], local.A[6], local.A[7],
454 local.A[0], local.A[1], local.A[2], R(i+5), K[i+5] );
455 P( local.A[2], local.A[3], local.A[4], local.A[5], local.A[6],
456 local.A[7], local.A[0], local.A[1], R(i+6), K[i+6] );
457 P( local.A[1], local.A[2], local.A[3], local.A[4], local.A[5],
458 local.A[6], local.A[7], local.A[0], R(i+7), K[i+7] );
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200459 }
Manuel Pégourié-Gonnardeb0d8702015-05-28 12:54:04 +0200460#endif /* MBEDTLS_SHA256_SMALLER */
Manuel Pégourié-Gonnarda7a3a5f2015-05-28 12:14:49 +0200461
462 for( i = 0; i < 8; i++ )
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200463 ctx->state[i] += local.A[i];
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100464
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200465 /* Zeroise buffers and variables to clear sensitive data from memory. */
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200466 mbedtls_platform_zeroize( &local, sizeof( local ) );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100467
468 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469}
Jaeden Amero041039f2018-02-19 15:28:08 +0000470
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000471#endif /* !MBEDTLS_SHA256_PROCESS_ALT && !MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
472
473
474#if !defined(MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY)
475
476static size_t mbedtls_internal_sha256_process_many_c(
477 mbedtls_sha256_context *ctx, const uint8_t *data, size_t len )
478{
479 size_t processed = 0;
480
481 while( len >= SHA256_BLOCK_SIZE )
482 {
483 if( mbedtls_internal_sha256_process_c( ctx, data ) != 0 )
484 return( 0 );
485
486 data += SHA256_BLOCK_SIZE;
487 len -= SHA256_BLOCK_SIZE;
488
489 processed += SHA256_BLOCK_SIZE;
490 }
491
492 return( processed );
493}
494
495#endif /* !MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY */
496
497
498#if defined(MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT)
499
500static int mbedtls_a64_crypto_sha256_has_support( void )
501{
502 static int done = 0;
503 static int supported = 0;
504
505 if( !done )
506 {
507 supported = mbedtls_a64_crypto_sha256_check_support();
508 done = 1;
509 }
510
511 return( supported );
512}
513
514static size_t mbedtls_internal_sha256_process_many( mbedtls_sha256_context *ctx,
515 const uint8_t *msg, size_t len )
516{
517 if( mbedtls_a64_crypto_sha256_has_support() )
518 return( mbedtls_internal_sha256_process_many_a64_crypto( ctx, msg, len ) );
519 else
520 return( mbedtls_internal_sha256_process_many_c( ctx, msg, len ) );
521}
522
523int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
524 const unsigned char data[SHA256_BLOCK_SIZE] )
525{
526 if( mbedtls_a64_crypto_sha256_has_support() )
527 return( mbedtls_internal_sha256_process_a64_crypto( ctx, data ) );
528 else
529 return( mbedtls_internal_sha256_process_c( ctx, data ) );
530}
531
532#endif /* MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT */
533
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
535/*
536 * SHA-256 process buffer
537 */
TRodziewicz26371e42021-06-08 16:45:41 +0200538int mbedtls_sha256_update( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100539 const unsigned char *input,
540 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000541{
Janos Follath24eed8d2019-11-22 13:21:35 +0000542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000543 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000544 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Hanno Becker8d215e72018-12-18 17:53:21 +0000546 SHA256_VALIDATE_RET( ctx != NULL );
547 SHA256_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Becker596e0142018-12-18 15:00:38 +0000548
Brian White12895d12014-04-11 11:29:42 -0400549 if( ilen == 0 )
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100550 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
552 left = ctx->total[0] & 0x3F;
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000553 fill = SHA256_BLOCK_SIZE - left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
Paul Bakker5c2364c2012-10-01 14:41:15 +0000555 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 ctx->total[0] &= 0xFFFFFFFF;
557
Paul Bakker5c2364c2012-10-01 14:41:15 +0000558 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 ctx->total[1]++;
560
561 if( left && ilen >= fill )
562 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200563 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100564
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100565 if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100566 return( ret );
567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 input += fill;
569 ilen -= fill;
570 left = 0;
571 }
572
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000573 while( ilen >= SHA256_BLOCK_SIZE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 {
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000575 size_t processed =
576 mbedtls_internal_sha256_process_many( ctx, input, ilen );
577 if( processed < SHA256_BLOCK_SIZE )
578 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100579
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000580 input += processed;
581 ilen -= processed;
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 }
583
584 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200585 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100586
587 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588}
589
Paul Bakker5121ce52009-01-03 21:22:43 +0000590/*
591 * SHA-256 final digest
592 */
TRodziewicz26371e42021-06-08 16:45:41 +0200593int mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
Gilles Peskined7b3d922021-05-13 00:45:25 +0200594 unsigned char *output )
Paul Bakker5121ce52009-01-03 21:22:43 +0000595{
Janos Follath24eed8d2019-11-22 13:21:35 +0000596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200597 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000598 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
Hanno Becker8d215e72018-12-18 17:53:21 +0000600 SHA256_VALIDATE_RET( ctx != NULL );
601 SHA256_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garcia79e593f2018-12-09 20:41:20 +0000602
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200603 /*
604 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
605 */
606 used = ctx->total[0] & 0x3F;
607
608 ctx->buffer[used++] = 0x80;
609
610 if( used <= 56 )
611 {
612 /* Enough room for padding + length in current block */
613 memset( ctx->buffer + used, 0, 56 - used );
614 }
615 else
616 {
617 /* We'll need an extra block */
Tom Cosgrovef3ebd902022-02-20 22:25:31 +0000618 memset( ctx->buffer + used, 0, SHA256_BLOCK_SIZE - used );
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200619
620 if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
621 return( ret );
622
623 memset( ctx->buffer, 0, 56 );
624 }
625
626 /*
627 * Add message length
628 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 high = ( ctx->total[0] >> 29 )
630 | ( ctx->total[1] << 3 );
631 low = ( ctx->total[0] << 3 );
632
Joe Subbiani5ecac212021-06-24 13:00:03 +0100633 MBEDTLS_PUT_UINT32_BE( high, ctx->buffer, 56 );
634 MBEDTLS_PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200636 if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garciaaa464ef2017-07-21 14:21:53 +0100637 return( ret );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100638
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200639 /*
640 * Output final state
641 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100642 MBEDTLS_PUT_UINT32_BE( ctx->state[0], output, 0 );
643 MBEDTLS_PUT_UINT32_BE( ctx->state[1], output, 4 );
644 MBEDTLS_PUT_UINT32_BE( ctx->state[2], output, 8 );
645 MBEDTLS_PUT_UINT32_BE( ctx->state[3], output, 12 );
646 MBEDTLS_PUT_UINT32_BE( ctx->state[4], output, 16 );
647 MBEDTLS_PUT_UINT32_BE( ctx->state[5], output, 20 );
648 MBEDTLS_PUT_UINT32_BE( ctx->state[6], output, 24 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200650#if defined(MBEDTLS_SHA224_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 if( ctx->is224 == 0 )
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200652#endif
Joe Subbiani5ecac212021-06-24 13:00:03 +0100653 MBEDTLS_PUT_UINT32_BE( ctx->state[7], output, 28 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100654
655 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656}
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* !MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200659
Paul Bakker5121ce52009-01-03 21:22:43 +0000660/*
661 * output = SHA-256( input buffer )
662 */
TRodziewicz26371e42021-06-08 16:45:41 +0200663int mbedtls_sha256( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100664 size_t ilen,
Gilles Peskined7b3d922021-05-13 00:45:25 +0200665 unsigned char *output,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100666 int is224 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000667{
Janos Follath24eed8d2019-11-22 13:21:35 +0000668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_sha256_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200671#if defined(MBEDTLS_SHA224_C)
Hanno Becker8d215e72018-12-18 17:53:21 +0000672 SHA256_VALIDATE_RET( is224 == 0 || is224 == 1 );
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200673#else
674 SHA256_VALIDATE_RET( is224 == 0 );
675#endif
676
Hanno Becker8d215e72018-12-18 17:53:21 +0000677 SHA256_VALIDATE_RET( ilen == 0 || input != NULL );
678 SHA256_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garcia79e593f2018-12-09 20:41:20 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_sha256_init( &ctx );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100681
TRodziewicz26371e42021-06-08 16:45:41 +0200682 if( ( ret = mbedtls_sha256_starts( &ctx, is224 ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100683 goto exit;
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100684
TRodziewicz26371e42021-06-08 16:45:41 +0200685 if( ( ret = mbedtls_sha256_update( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100686 goto exit;
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100687
TRodziewicz26371e42021-06-08 16:45:41 +0200688 if( ( ret = mbedtls_sha256_finish( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100689 goto exit;
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100690
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100691exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 mbedtls_sha256_free( &ctx );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100693
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100694 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000695}
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000698/*
699 * FIPS-180-2 test vectors
700 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000701static const unsigned char sha256_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000702{
703 { "abc" },
704 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
705 { "" }
706};
707
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100708static const size_t sha256_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000709{
710 3, 56, 1000
711};
712
Paul Bakker9e36f042013-06-30 14:34:05 +0200713static const unsigned char sha256_test_sum[6][32] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000714{
715 /*
716 * SHA-224 test vectors
717 */
718 { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
719 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
720 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
721 0xE3, 0x6C, 0x9D, 0xA7 },
722 { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
723 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
724 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
725 0x52, 0x52, 0x25, 0x25 },
726 { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8,
727 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B,
728 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE,
729 0x4E, 0xE7, 0xAD, 0x67 },
730
731 /*
732 * SHA-256 test vectors
733 */
734 { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
735 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
736 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
737 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
738 { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
739 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
740 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
741 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
742 { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
743 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
744 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
745 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
746};
747
748/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 * Checkup routine
750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751int mbedtls_sha256_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000752{
Paul Bakker5b4af392014-06-26 12:09:34 +0200753 int i, j, k, buflen, ret = 0;
Russ Butlerbb83b422016-10-12 17:36:50 -0500754 unsigned char *buf;
Paul Bakker9e36f042013-06-30 14:34:05 +0200755 unsigned char sha256sum[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_sha256_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000757
Russ Butlerbb83b422016-10-12 17:36:50 -0500758 buf = mbedtls_calloc( 1024, sizeof(unsigned char) );
759 if( NULL == buf )
760 {
761 if( verbose != 0 )
762 mbedtls_printf( "Buffer allocation failed\n" );
763
764 return( 1 );
765 }
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_sha256_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200768
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 for( i = 0; i < 6; i++ )
770 {
771 j = i % 3;
772 k = i < 3;
773
774 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
TRodziewicz26371e42021-06-08 16:45:41 +0200777 if( ( ret = mbedtls_sha256_starts( &ctx, k ) ) != 0 )
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100778 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
780 if( j == 2 )
781 {
782 memset( buf, 'a', buflen = 1000 );
783
784 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100785 {
TRodziewicz26371e42021-06-08 16:45:41 +0200786 ret = mbedtls_sha256_update( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100787 if( ret != 0 )
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100788 goto fail;
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100789 }
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100790
Paul Bakker5121ce52009-01-03 21:22:43 +0000791 }
792 else
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100793 {
TRodziewicz26371e42021-06-08 16:45:41 +0200794 ret = mbedtls_sha256_update( &ctx, sha256_test_buf[j],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100795 sha256_test_buflen[j] );
796 if( ret != 0 )
797 goto fail;
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100798 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000799
TRodziewicz26371e42021-06-08 16:45:41 +0200800 if( ( ret = mbedtls_sha256_finish( &ctx, sha256sum ) ) != 0 )
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100801 goto fail;
802
Paul Bakker5121ce52009-01-03 21:22:43 +0000803
Paul Bakker9e36f042013-06-30 14:34:05 +0200804 if( memcmp( sha256sum, sha256_test_sum[i], 32 - k * 4 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100805 {
806 ret = 1;
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100807 goto fail;
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100808 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000809
810 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 }
813
814 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000816
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100817 goto exit;
818
819fail:
820 if( verbose != 0 )
821 mbedtls_printf( "failed\n" );
822
Paul Bakker5b4af392014-06-26 12:09:34 +0200823exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_sha256_free( &ctx );
Russ Butlerbb83b422016-10-12 17:36:50 -0500825 mbedtls_free( buf );
Paul Bakker5b4af392014-06-26 12:09:34 +0200826
827 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000828}
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#endif /* MBEDTLS_SHA256_C */