blob: 84bbd551fa666b34a4c9e4e447a5e794ac6a315a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SHA-1 standard was published by NIST in 1993.
23 *
24 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
25 */
26
Gilles Peskinedb09ef62020-06-03 01:43:33 +020027#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/sha1.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050032#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_SELF_TEST)
38#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#else
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define mbedtls_printf printf
43#endif /* MBEDTLS_PLATFORM_C */
44#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010045
Hanno Beckerb3c70232018-12-20 10:18:05 +000046#define SHA1_VALIDATE_RET(cond) \
47 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
48
49#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
50
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020051#if !defined(MBEDTLS_SHA1_ALT)
52
Paul Bakker5121ce52009-01-03 21:22:43 +000053/*
54 * 32-bit integer manipulation macros (big endian)
55 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000056#ifndef GET_UINT32_BE
57#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000058{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000059 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
60 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
61 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
62 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
64#endif
65
Paul Bakker5c2364c2012-10-01 14:41:15 +000066#ifndef PUT_UINT32_BE
67#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000068{ \
69 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
70 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
71 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
72 (b)[(i) + 3] = (unsigned char) ( (n) ); \
73}
74#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020077{
Hanno Becker039ccab2018-12-18 17:52:14 +000078 SHA1_VALIDATE( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020081}
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020084{
85 if( ctx == NULL )
86 return;
87
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050088 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020089}
90
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020091void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
92 const mbedtls_sha1_context *src )
93{
Hanno Becker039ccab2018-12-18 17:52:14 +000094 SHA1_VALIDATE( dst != NULL );
95 SHA1_VALIDATE( src != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000096
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020097 *dst = *src;
98}
99
Paul Bakker5121ce52009-01-03 21:22:43 +0000100/*
101 * SHA-1 context setup
102 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100103int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104{
Hanno Becker039ccab2018-12-18 17:52:14 +0000105 SHA1_VALIDATE_RET( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000106
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 ctx->total[0] = 0;
108 ctx->total[1] = 0;
109
110 ctx->state[0] = 0x67452301;
111 ctx->state[1] = 0xEFCDAB89;
112 ctx->state[2] = 0x98BADCFE;
113 ctx->state[3] = 0x10325476;
114 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100115
116 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117}
118
Jaeden Amero041039f2018-02-19 15:28:08 +0000119#if !defined(MBEDTLS_DEPRECATED_REMOVED)
120void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
121{
122 mbedtls_sha1_starts_ret( ctx );
123}
124#endif
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100127int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
128 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000129{
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200130 struct
131 {
132 uint32_t temp, W[16], A, B, C, D, E;
133 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Hanno Becker039ccab2018-12-18 17:52:14 +0000135 SHA1_VALIDATE_RET( ctx != NULL );
136 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000137
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200138 GET_UINT32_BE( local.W[ 0], data, 0 );
139 GET_UINT32_BE( local.W[ 1], data, 4 );
140 GET_UINT32_BE( local.W[ 2], data, 8 );
141 GET_UINT32_BE( local.W[ 3], data, 12 );
142 GET_UINT32_BE( local.W[ 4], data, 16 );
143 GET_UINT32_BE( local.W[ 5], data, 20 );
144 GET_UINT32_BE( local.W[ 6], data, 24 );
145 GET_UINT32_BE( local.W[ 7], data, 28 );
146 GET_UINT32_BE( local.W[ 8], data, 32 );
147 GET_UINT32_BE( local.W[ 9], data, 36 );
148 GET_UINT32_BE( local.W[10], data, 40 );
149 GET_UINT32_BE( local.W[11], data, 44 );
150 GET_UINT32_BE( local.W[12], data, 48 );
151 GET_UINT32_BE( local.W[13], data, 52 );
152 GET_UINT32_BE( local.W[14], data, 56 );
153 GET_UINT32_BE( local.W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Hanno Becker1eeca412018-10-15 12:01:35 +0100155#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Hanno Becker1eeca412018-10-15 12:01:35 +0100157#define R(t) \
158 ( \
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200159 local.temp = local.W[( (t) - 3 ) & 0x0F] ^ \
160 local.W[( (t) - 8 ) & 0x0F] ^ \
161 local.W[( (t) - 14 ) & 0x0F] ^ \
162 local.W[ (t) & 0x0F], \
163 ( local.W[(t) & 0x0F] = S(local.temp,1) ) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100164 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Hanno Becker1eeca412018-10-15 12:01:35 +0100166#define P(a,b,c,d,e,x) \
167 do \
168 { \
Hanno Becker818bac52018-10-26 09:13:26 +0100169 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
170 (b) = S((b),30); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100171 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200173 local.A = ctx->state[0];
174 local.B = ctx->state[1];
175 local.C = ctx->state[2];
176 local.D = ctx->state[3];
177 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
Hanno Becker1eeca412018-10-15 12:01:35 +0100179#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000180#define K 0x5A827999
181
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200182 P( local.A, local.B, local.C, local.D, local.E, local.W[0] );
183 P( local.E, local.A, local.B, local.C, local.D, local.W[1] );
184 P( local.D, local.E, local.A, local.B, local.C, local.W[2] );
185 P( local.C, local.D, local.E, local.A, local.B, local.W[3] );
186 P( local.B, local.C, local.D, local.E, local.A, local.W[4] );
187 P( local.A, local.B, local.C, local.D, local.E, local.W[5] );
188 P( local.E, local.A, local.B, local.C, local.D, local.W[6] );
189 P( local.D, local.E, local.A, local.B, local.C, local.W[7] );
190 P( local.C, local.D, local.E, local.A, local.B, local.W[8] );
191 P( local.B, local.C, local.D, local.E, local.A, local.W[9] );
192 P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
193 P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
194 P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
195 P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
196 P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
197 P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
198 P( local.E, local.A, local.B, local.C, local.D, R(16) );
199 P( local.D, local.E, local.A, local.B, local.C, R(17) );
200 P( local.C, local.D, local.E, local.A, local.B, R(18) );
201 P( local.B, local.C, local.D, local.E, local.A, R(19) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203#undef K
204#undef F
205
Hanno Becker1eeca412018-10-15 12:01:35 +0100206#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000207#define K 0x6ED9EBA1
208
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200209 P( local.A, local.B, local.C, local.D, local.E, R(20) );
210 P( local.E, local.A, local.B, local.C, local.D, R(21) );
211 P( local.D, local.E, local.A, local.B, local.C, R(22) );
212 P( local.C, local.D, local.E, local.A, local.B, R(23) );
213 P( local.B, local.C, local.D, local.E, local.A, R(24) );
214 P( local.A, local.B, local.C, local.D, local.E, R(25) );
215 P( local.E, local.A, local.B, local.C, local.D, R(26) );
216 P( local.D, local.E, local.A, local.B, local.C, R(27) );
217 P( local.C, local.D, local.E, local.A, local.B, R(28) );
218 P( local.B, local.C, local.D, local.E, local.A, R(29) );
219 P( local.A, local.B, local.C, local.D, local.E, R(30) );
220 P( local.E, local.A, local.B, local.C, local.D, R(31) );
221 P( local.D, local.E, local.A, local.B, local.C, R(32) );
222 P( local.C, local.D, local.E, local.A, local.B, R(33) );
223 P( local.B, local.C, local.D, local.E, local.A, R(34) );
224 P( local.A, local.B, local.C, local.D, local.E, R(35) );
225 P( local.E, local.A, local.B, local.C, local.D, R(36) );
226 P( local.D, local.E, local.A, local.B, local.C, R(37) );
227 P( local.C, local.D, local.E, local.A, local.B, R(38) );
228 P( local.B, local.C, local.D, local.E, local.A, R(39) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
230#undef K
231#undef F
232
Hanno Becker1eeca412018-10-15 12:01:35 +0100233#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000234#define K 0x8F1BBCDC
235
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200236 P( local.A, local.B, local.C, local.D, local.E, R(40) );
237 P( local.E, local.A, local.B, local.C, local.D, R(41) );
238 P( local.D, local.E, local.A, local.B, local.C, R(42) );
239 P( local.C, local.D, local.E, local.A, local.B, R(43) );
240 P( local.B, local.C, local.D, local.E, local.A, R(44) );
241 P( local.A, local.B, local.C, local.D, local.E, R(45) );
242 P( local.E, local.A, local.B, local.C, local.D, R(46) );
243 P( local.D, local.E, local.A, local.B, local.C, R(47) );
244 P( local.C, local.D, local.E, local.A, local.B, R(48) );
245 P( local.B, local.C, local.D, local.E, local.A, R(49) );
246 P( local.A, local.B, local.C, local.D, local.E, R(50) );
247 P( local.E, local.A, local.B, local.C, local.D, R(51) );
248 P( local.D, local.E, local.A, local.B, local.C, R(52) );
249 P( local.C, local.D, local.E, local.A, local.B, R(53) );
250 P( local.B, local.C, local.D, local.E, local.A, R(54) );
251 P( local.A, local.B, local.C, local.D, local.E, R(55) );
252 P( local.E, local.A, local.B, local.C, local.D, R(56) );
253 P( local.D, local.E, local.A, local.B, local.C, R(57) );
254 P( local.C, local.D, local.E, local.A, local.B, R(58) );
255 P( local.B, local.C, local.D, local.E, local.A, R(59) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257#undef K
258#undef F
259
Hanno Becker1eeca412018-10-15 12:01:35 +0100260#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000261#define K 0xCA62C1D6
262
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200263 P( local.A, local.B, local.C, local.D, local.E, R(60) );
264 P( local.E, local.A, local.B, local.C, local.D, R(61) );
265 P( local.D, local.E, local.A, local.B, local.C, R(62) );
266 P( local.C, local.D, local.E, local.A, local.B, R(63) );
267 P( local.B, local.C, local.D, local.E, local.A, R(64) );
268 P( local.A, local.B, local.C, local.D, local.E, R(65) );
269 P( local.E, local.A, local.B, local.C, local.D, R(66) );
270 P( local.D, local.E, local.A, local.B, local.C, R(67) );
271 P( local.C, local.D, local.E, local.A, local.B, R(68) );
272 P( local.B, local.C, local.D, local.E, local.A, R(69) );
273 P( local.A, local.B, local.C, local.D, local.E, R(70) );
274 P( local.E, local.A, local.B, local.C, local.D, R(71) );
275 P( local.D, local.E, local.A, local.B, local.C, R(72) );
276 P( local.C, local.D, local.E, local.A, local.B, R(73) );
277 P( local.B, local.C, local.D, local.E, local.A, R(74) );
278 P( local.A, local.B, local.C, local.D, local.E, R(75) );
279 P( local.E, local.A, local.B, local.C, local.D, R(76) );
280 P( local.D, local.E, local.A, local.B, local.C, R(77) );
281 P( local.C, local.D, local.E, local.A, local.B, R(78) );
282 P( local.B, local.C, local.D, local.E, local.A, R(79) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284#undef K
285#undef F
286
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200287 ctx->state[0] += local.A;
288 ctx->state[1] += local.B;
289 ctx->state[2] += local.C;
290 ctx->state[3] += local.D;
291 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100292
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200293 /* Zeroise buffers and variables to clear sensitive data from memory. */
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200294 mbedtls_platform_zeroize( &local, sizeof( local ) );
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200295
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100296 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297}
Jaeden Amero041039f2018-02-19 15:28:08 +0000298
299#if !defined(MBEDTLS_DEPRECATED_REMOVED)
300void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
301 const unsigned char data[64] )
302{
303 mbedtls_internal_sha1_process( ctx, data );
304}
305#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308/*
309 * SHA-1 process buffer
310 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100311int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100312 const unsigned char *input,
313 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000314{
Janos Follath24eed8d2019-11-22 13:21:35 +0000315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000316 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000317 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
Hanno Becker039ccab2018-12-18 17:52:14 +0000319 SHA1_VALIDATE_RET( ctx != NULL );
320 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000321
Brian White12895d12014-04-11 11:29:42 -0400322 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100323 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
325 left = ctx->total[0] & 0x3F;
326 fill = 64 - left;
327
Paul Bakker5c2364c2012-10-01 14:41:15 +0000328 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 ctx->total[0] &= 0xFFFFFFFF;
330
Paul Bakker5c2364c2012-10-01 14:41:15 +0000331 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 ctx->total[1]++;
333
334 if( left && ilen >= fill )
335 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200336 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100337
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100338 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100339 return( ret );
340
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 input += fill;
342 ilen -= fill;
343 left = 0;
344 }
345
346 while( ilen >= 64 )
347 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100348 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100349 return( ret );
350
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 input += 64;
352 ilen -= 64;
353 }
354
355 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200356 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100357
358 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359}
360
Jaeden Amero041039f2018-02-19 15:28:08 +0000361#if !defined(MBEDTLS_DEPRECATED_REMOVED)
362void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
363 const unsigned char *input,
364 size_t ilen )
365{
366 mbedtls_sha1_update_ret( ctx, input, ilen );
367}
368#endif
369
Paul Bakker5121ce52009-01-03 21:22:43 +0000370/*
371 * SHA-1 final digest
372 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100373int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100374 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000375{
Janos Follath24eed8d2019-11-22 13:21:35 +0000376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200377 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000378 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
Hanno Becker039ccab2018-12-18 17:52:14 +0000380 SHA1_VALIDATE_RET( ctx != NULL );
381 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000382
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200383 /*
384 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
385 */
386 used = ctx->total[0] & 0x3F;
387
388 ctx->buffer[used++] = 0x80;
389
390 if( used <= 56 )
391 {
392 /* Enough room for padding + length in current block */
393 memset( ctx->buffer + used, 0, 56 - used );
394 }
395 else
396 {
397 /* We'll need an extra block */
398 memset( ctx->buffer + used, 0, 64 - used );
399
400 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
401 return( ret );
402
403 memset( ctx->buffer, 0, 56 );
404 }
405
406 /*
407 * Add message length
408 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 high = ( ctx->total[0] >> 29 )
410 | ( ctx->total[1] << 3 );
411 low = ( ctx->total[0] << 3 );
412
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200413 PUT_UINT32_BE( high, ctx->buffer, 56 );
414 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200416 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100417 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200419 /*
420 * Output final state
421 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000422 PUT_UINT32_BE( ctx->state[0], output, 0 );
423 PUT_UINT32_BE( ctx->state[1], output, 4 );
424 PUT_UINT32_BE( ctx->state[2], output, 8 );
425 PUT_UINT32_BE( ctx->state[3], output, 12 );
426 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100427
428 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429}
430
Jaeden Amero041039f2018-02-19 15:28:08 +0000431#if !defined(MBEDTLS_DEPRECATED_REMOVED)
432void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
433 unsigned char output[20] )
434{
435 mbedtls_sha1_finish_ret( ctx, output );
436}
437#endif
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200440
Paul Bakker5121ce52009-01-03 21:22:43 +0000441/*
442 * output = SHA-1( input buffer )
443 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100444int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100445 size_t ilen,
446 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000447{
Janos Follath24eed8d2019-11-22 13:21:35 +0000448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
Hanno Becker039ccab2018-12-18 17:52:14 +0000451 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
452 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100455
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100456 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100457 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100458
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100460 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100461
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100462 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100463 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100464
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100465exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100467
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100468 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469}
470
Jaeden Amero041039f2018-02-19 15:28:08 +0000471#if !defined(MBEDTLS_DEPRECATED_REMOVED)
472void mbedtls_sha1( const unsigned char *input,
473 size_t ilen,
474 unsigned char output[20] )
475{
476 mbedtls_sha1_ret( input, ilen, output );
477}
478#endif
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000481/*
482 * FIPS-180-1 test vectors
483 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000484static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000485{
486 { "abc" },
487 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
488 { "" }
489};
490
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100491static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000492{
493 3, 56, 1000
494};
495
496static const unsigned char sha1_test_sum[3][20] =
497{
498 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
499 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
500 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
501 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
502 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
503 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
504};
505
506/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 * Checkup routine
508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000510{
Paul Bakker5b4af392014-06-26 12:09:34 +0200511 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 unsigned char buf[1024];
513 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200517
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 /*
519 * SHA-1
520 */
521 for( i = 0; i < 3; i++ )
522 {
523 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100526 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100527 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 if( i == 2 )
530 {
531 memset( buf, 'a', buflen = 1000 );
532
533 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100534 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100535 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100536 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100537 goto fail;
538 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 }
540 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100541 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100542 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100543 sha1_test_buflen[i] );
544 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100545 goto fail;
546 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100548 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100549 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
551 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100552 {
553 ret = 1;
554 goto fail;
555 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 }
560
561 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100564 goto exit;
565
566fail:
567 if( verbose != 0 )
568 mbedtls_printf( "failed\n" );
569
Paul Bakker5b4af392014-06-26 12:09:34 +0200570exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200572
573 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574}
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#endif /* MBEDTLS_SHA1_C */