blob: 7e6981d5a5ea0897906b29e1a40b60bea4493b9c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46/*
47 * The SHA-1 standard was published by NIST in 1993.
48 *
49 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
50 */
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020054#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <string.h>
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_SELF_TEST)
65#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#else
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#define mbedtls_printf printf
70#endif /* MBEDTLS_PLATFORM_C */
71#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010072
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020073#if !defined(MBEDTLS_SHA1_ALT)
74
Paul Bakker34617722014-06-13 17:20:13 +020075/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void mbedtls_zeroize( void *v, size_t n ) {
Simon Butcher88ffc082016-05-20 00:00:37 +010077 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
Paul Bakker34617722014-06-13 17:20:13 +020078}
79
Paul Bakker5121ce52009-01-03 21:22:43 +000080/*
81 * 32-bit integer manipulation macros (big endian)
82 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000083#ifndef GET_UINT32_BE
84#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000085{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000086 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
87 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
88 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
89 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000090}
91#endif
92
Paul Bakker5c2364c2012-10-01 14:41:15 +000093#ifndef PUT_UINT32_BE
94#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000095{ \
96 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
97 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
98 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
99 (b)[(i) + 3] = (unsigned char) ( (n) ); \
100}
101#endif
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200106}
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200109{
110 if( ctx == NULL )
111 return;
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200114}
115
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200116void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
117 const mbedtls_sha1_context *src )
118{
119 *dst = *src;
120}
121
Paul Bakker5121ce52009-01-03 21:22:43 +0000122/*
123 * SHA-1 context setup
124 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100125int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000126{
127 ctx->total[0] = 0;
128 ctx->total[1] = 0;
129
130 ctx->state[0] = 0x67452301;
131 ctx->state[1] = 0xEFCDAB89;
132 ctx->state[2] = 0x98BADCFE;
133 ctx->state[3] = 0x10325476;
134 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100135
136 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137}
138
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000139#if !defined(MBEDTLS_DEPRECATED_REMOVED)
140void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
141{
142 mbedtls_sha1_starts_ret( ctx );
143}
144#endif
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100147int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
148 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000149{
gabor-mezei-armbfdbd432020-08-25 19:12:01 +0200150 struct
151 {
152 uint32_t temp, W[16], A, B, C, D, E;
153 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
gabor-mezei-armbfdbd432020-08-25 19:12:01 +0200155 GET_UINT32_BE( local.W[ 0], data, 0 );
156 GET_UINT32_BE( local.W[ 1], data, 4 );
157 GET_UINT32_BE( local.W[ 2], data, 8 );
158 GET_UINT32_BE( local.W[ 3], data, 12 );
159 GET_UINT32_BE( local.W[ 4], data, 16 );
160 GET_UINT32_BE( local.W[ 5], data, 20 );
161 GET_UINT32_BE( local.W[ 6], data, 24 );
162 GET_UINT32_BE( local.W[ 7], data, 28 );
163 GET_UINT32_BE( local.W[ 8], data, 32 );
164 GET_UINT32_BE( local.W[ 9], data, 36 );
165 GET_UINT32_BE( local.W[10], data, 40 );
166 GET_UINT32_BE( local.W[11], data, 44 );
167 GET_UINT32_BE( local.W[12], data, 48 );
168 GET_UINT32_BE( local.W[13], data, 52 );
169 GET_UINT32_BE( local.W[14], data, 56 );
170 GET_UINT32_BE( local.W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
173
174#define R(t) \
175( \
gabor-mezei-armbfdbd432020-08-25 19:12:01 +0200176 local.temp = local.W[( t - 3 ) & 0x0F] ^ \
177 local.W[( t - 8 ) & 0x0F] ^ \
178 local.W[( t - 14 ) & 0x0F] ^ \
179 local.W[ t & 0x0F], \
180 ( local.W[t & 0x0F] = S(local.temp,1) ) \
Paul Bakker5121ce52009-01-03 21:22:43 +0000181)
182
183#define P(a,b,c,d,e,x) \
184{ \
185 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
186}
187
gabor-mezei-armbfdbd432020-08-25 19:12:01 +0200188 local.A = ctx->state[0];
189 local.B = ctx->state[1];
190 local.C = ctx->state[2];
191 local.D = ctx->state[3];
192 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194#define F(x,y,z) (z ^ (x & (y ^ z)))
195#define K 0x5A827999
196
gabor-mezei-armbfdbd432020-08-25 19:12:01 +0200197 P( local.A, local.B, local.C, local.D, local.E, local.W[0] );
198 P( local.E, local.A, local.B, local.C, local.D, local.W[1] );
199 P( local.D, local.E, local.A, local.B, local.C, local.W[2] );
200 P( local.C, local.D, local.E, local.A, local.B, local.W[3] );
201 P( local.B, local.C, local.D, local.E, local.A, local.W[4] );
202 P( local.A, local.B, local.C, local.D, local.E, local.W[5] );
203 P( local.E, local.A, local.B, local.C, local.D, local.W[6] );
204 P( local.D, local.E, local.A, local.B, local.C, local.W[7] );
205 P( local.C, local.D, local.E, local.A, local.B, local.W[8] );
206 P( local.B, local.C, local.D, local.E, local.A, local.W[9] );
207 P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
208 P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
209 P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
210 P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
211 P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
212 P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
213 P( local.E, local.A, local.B, local.C, local.D, R(16) );
214 P( local.D, local.E, local.A, local.B, local.C, R(17) );
215 P( local.C, local.D, local.E, local.A, local.B, R(18) );
216 P( local.B, local.C, local.D, local.E, local.A, R(19) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218#undef K
219#undef F
220
221#define F(x,y,z) (x ^ y ^ z)
222#define K 0x6ED9EBA1
223
gabor-mezei-armbfdbd432020-08-25 19:12:01 +0200224 P( local.A, local.B, local.C, local.D, local.E, R(20) );
225 P( local.E, local.A, local.B, local.C, local.D, R(21) );
226 P( local.D, local.E, local.A, local.B, local.C, R(22) );
227 P( local.C, local.D, local.E, local.A, local.B, R(23) );
228 P( local.B, local.C, local.D, local.E, local.A, R(24) );
229 P( local.A, local.B, local.C, local.D, local.E, R(25) );
230 P( local.E, local.A, local.B, local.C, local.D, R(26) );
231 P( local.D, local.E, local.A, local.B, local.C, R(27) );
232 P( local.C, local.D, local.E, local.A, local.B, R(28) );
233 P( local.B, local.C, local.D, local.E, local.A, R(29) );
234 P( local.A, local.B, local.C, local.D, local.E, R(30) );
235 P( local.E, local.A, local.B, local.C, local.D, R(31) );
236 P( local.D, local.E, local.A, local.B, local.C, R(32) );
237 P( local.C, local.D, local.E, local.A, local.B, R(33) );
238 P( local.B, local.C, local.D, local.E, local.A, R(34) );
239 P( local.A, local.B, local.C, local.D, local.E, R(35) );
240 P( local.E, local.A, local.B, local.C, local.D, R(36) );
241 P( local.D, local.E, local.A, local.B, local.C, R(37) );
242 P( local.C, local.D, local.E, local.A, local.B, R(38) );
243 P( local.B, local.C, local.D, local.E, local.A, R(39) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245#undef K
246#undef F
247
248#define F(x,y,z) ((x & y) | (z & (x | y)))
249#define K 0x8F1BBCDC
250
gabor-mezei-armbfdbd432020-08-25 19:12:01 +0200251 P( local.A, local.B, local.C, local.D, local.E, R(40) );
252 P( local.E, local.A, local.B, local.C, local.D, R(41) );
253 P( local.D, local.E, local.A, local.B, local.C, R(42) );
254 P( local.C, local.D, local.E, local.A, local.B, R(43) );
255 P( local.B, local.C, local.D, local.E, local.A, R(44) );
256 P( local.A, local.B, local.C, local.D, local.E, R(45) );
257 P( local.E, local.A, local.B, local.C, local.D, R(46) );
258 P( local.D, local.E, local.A, local.B, local.C, R(47) );
259 P( local.C, local.D, local.E, local.A, local.B, R(48) );
260 P( local.B, local.C, local.D, local.E, local.A, R(49) );
261 P( local.A, local.B, local.C, local.D, local.E, R(50) );
262 P( local.E, local.A, local.B, local.C, local.D, R(51) );
263 P( local.D, local.E, local.A, local.B, local.C, R(52) );
264 P( local.C, local.D, local.E, local.A, local.B, R(53) );
265 P( local.B, local.C, local.D, local.E, local.A, R(54) );
266 P( local.A, local.B, local.C, local.D, local.E, R(55) );
267 P( local.E, local.A, local.B, local.C, local.D, R(56) );
268 P( local.D, local.E, local.A, local.B, local.C, R(57) );
269 P( local.C, local.D, local.E, local.A, local.B, R(58) );
270 P( local.B, local.C, local.D, local.E, local.A, R(59) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272#undef K
273#undef F
274
275#define F(x,y,z) (x ^ y ^ z)
276#define K 0xCA62C1D6
277
gabor-mezei-armbfdbd432020-08-25 19:12:01 +0200278 P( local.A, local.B, local.C, local.D, local.E, R(60) );
279 P( local.E, local.A, local.B, local.C, local.D, R(61) );
280 P( local.D, local.E, local.A, local.B, local.C, R(62) );
281 P( local.C, local.D, local.E, local.A, local.B, R(63) );
282 P( local.B, local.C, local.D, local.E, local.A, R(64) );
283 P( local.A, local.B, local.C, local.D, local.E, R(65) );
284 P( local.E, local.A, local.B, local.C, local.D, R(66) );
285 P( local.D, local.E, local.A, local.B, local.C, R(67) );
286 P( local.C, local.D, local.E, local.A, local.B, R(68) );
287 P( local.B, local.C, local.D, local.E, local.A, R(69) );
288 P( local.A, local.B, local.C, local.D, local.E, R(70) );
289 P( local.E, local.A, local.B, local.C, local.D, R(71) );
290 P( local.D, local.E, local.A, local.B, local.C, R(72) );
291 P( local.C, local.D, local.E, local.A, local.B, R(73) );
292 P( local.B, local.C, local.D, local.E, local.A, R(74) );
293 P( local.A, local.B, local.C, local.D, local.E, R(75) );
294 P( local.E, local.A, local.B, local.C, local.D, R(76) );
295 P( local.D, local.E, local.A, local.B, local.C, R(77) );
296 P( local.C, local.D, local.E, local.A, local.B, R(78) );
297 P( local.B, local.C, local.D, local.E, local.A, R(79) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
299#undef K
300#undef F
301
gabor-mezei-armbfdbd432020-08-25 19:12:01 +0200302 ctx->state[0] += local.A;
303 ctx->state[1] += local.B;
304 ctx->state[2] += local.C;
305 ctx->state[3] += local.D;
306 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100307
gabor-mezei-arm461c5a82020-07-30 16:41:25 +0200308 /* Zeroise buffers and variables to clear sensitive data from memory. */
gabor-mezei-armbfdbd432020-08-25 19:12:01 +0200309 mbedtls_zeroize( &local, sizeof( local ) );
gabor-mezei-arm461c5a82020-07-30 16:41:25 +0200310
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100311 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312}
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000313
314#if !defined(MBEDTLS_DEPRECATED_REMOVED)
315void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
316 const unsigned char data[64] )
317{
318 mbedtls_internal_sha1_process( ctx, data );
319}
320#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323/*
324 * SHA-1 process buffer
325 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100326int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100327 const unsigned char *input,
328 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000329{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100330 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000331 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000332 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
Brian White12895d12014-04-11 11:29:42 -0400334 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100335 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 left = ctx->total[0] & 0x3F;
338 fill = 64 - left;
339
Paul Bakker5c2364c2012-10-01 14:41:15 +0000340 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 ctx->total[0] &= 0xFFFFFFFF;
342
Paul Bakker5c2364c2012-10-01 14:41:15 +0000343 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 ctx->total[1]++;
345
346 if( left && ilen >= fill )
347 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200348 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100349
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100350 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100351 return( ret );
352
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 input += fill;
354 ilen -= fill;
355 left = 0;
356 }
357
358 while( ilen >= 64 )
359 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100360 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100361 return( ret );
362
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 input += 64;
364 ilen -= 64;
365 }
366
367 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200368 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100369
370 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371}
372
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000373#if !defined(MBEDTLS_DEPRECATED_REMOVED)
374void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
375 const unsigned char *input,
376 size_t ilen )
377{
378 mbedtls_sha1_update_ret( ctx, input, ilen );
379}
380#endif
381
Paul Bakker5121ce52009-01-03 21:22:43 +0000382/*
383 * SHA-1 final digest
384 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100385int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100386 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000387{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100388 int ret;
Manuel Pégourié-Gonnard5fcfd032018-06-28 12:10:27 +0200389 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000390 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Manuel Pégourié-Gonnard5fcfd032018-06-28 12:10:27 +0200392 /*
393 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
394 */
395 used = ctx->total[0] & 0x3F;
396
397 ctx->buffer[used++] = 0x80;
398
399 if( used <= 56 )
400 {
401 /* Enough room for padding + length in current block */
402 memset( ctx->buffer + used, 0, 56 - used );
403 }
404 else
405 {
406 /* We'll need an extra block */
407 memset( ctx->buffer + used, 0, 64 - used );
408
409 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
410 return( ret );
411
412 memset( ctx->buffer, 0, 56 );
413 }
414
415 /*
416 * Add message length
417 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 high = ( ctx->total[0] >> 29 )
419 | ( ctx->total[1] << 3 );
420 low = ( ctx->total[0] << 3 );
421
Manuel Pégourié-Gonnard5fcfd032018-06-28 12:10:27 +0200422 PUT_UINT32_BE( high, ctx->buffer, 56 );
423 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
Manuel Pégourié-Gonnard5fcfd032018-06-28 12:10:27 +0200425 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100426 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
Manuel Pégourié-Gonnard5fcfd032018-06-28 12:10:27 +0200428 /*
429 * Output final state
430 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000431 PUT_UINT32_BE( ctx->state[0], output, 0 );
432 PUT_UINT32_BE( ctx->state[1], output, 4 );
433 PUT_UINT32_BE( ctx->state[2], output, 8 );
434 PUT_UINT32_BE( ctx->state[3], output, 12 );
435 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100436
437 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438}
439
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000440#if !defined(MBEDTLS_DEPRECATED_REMOVED)
441void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
442 unsigned char output[20] )
443{
444 mbedtls_sha1_finish_ret( ctx, output );
445}
446#endif
447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200449
Paul Bakker5121ce52009-01-03 21:22:43 +0000450/*
451 * output = SHA-1( input buffer )
452 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100453int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100454 size_t ilen,
455 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000456{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100457 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100461
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100462 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100463 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100464
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100465 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100466 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100467
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100469 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100470
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100471exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100473
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100474 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475}
476
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000477#if !defined(MBEDTLS_DEPRECATED_REMOVED)
478void mbedtls_sha1( const unsigned char *input,
479 size_t ilen,
480 unsigned char output[20] )
481{
482 mbedtls_sha1_ret( input, ilen, output );
483}
484#endif
485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000487/*
488 * FIPS-180-1 test vectors
489 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000490static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000491{
492 { "abc" },
493 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
494 { "" }
495};
496
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100497static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000498{
499 3, 56, 1000
500};
501
502static const unsigned char sha1_test_sum[3][20] =
503{
504 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
505 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
506 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
507 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
508 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
509 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
510};
511
512/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 * Checkup routine
514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000516{
Paul Bakker5b4af392014-06-26 12:09:34 +0200517 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 unsigned char buf[1024];
519 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200523
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 /*
525 * SHA-1
526 */
527 for( i = 0; i < 3; i++ )
528 {
529 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100532 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100533 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
535 if( i == 2 )
536 {
537 memset( buf, 'a', buflen = 1000 );
538
539 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100540 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100541 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100542 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100543 goto fail;
544 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 }
546 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100547 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100548 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100549 sha1_test_buflen[i] );
550 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100551 goto fail;
552 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100554 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100555 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100558 {
559 ret = 1;
560 goto fail;
561 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
563 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 }
566
567 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100570 goto exit;
571
572fail:
573 if( verbose != 0 )
574 mbedtls_printf( "failed\n" );
575
Paul Bakker5b4af392014-06-26 12:09:34 +0200576exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200578
579 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580}
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#endif /* MBEDTLS_SHA1_C */