blob: e99a5e86356277031cf5f480033cfe6ed92a1c02 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-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útif744bd72020-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"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050061#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <string.h>
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_SELF_TEST)
66#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010068#else
Rich Evans00ab4702015-02-06 13:43:58 +000069#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define mbedtls_printf printf
71#endif /* MBEDTLS_PLATFORM_C */
72#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010073
Hanno Beckerb3c70232018-12-20 10:18:05 +000074#define SHA1_VALIDATE_RET(cond) \
75 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
76
77#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
78
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020079#if !defined(MBEDTLS_SHA1_ALT)
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
82 * 32-bit integer manipulation macros (big endian)
83 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000084#ifndef GET_UINT32_BE
85#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000086{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000087 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
88 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
89 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
90 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
92#endif
93
Paul Bakker5c2364c2012-10-01 14:41:15 +000094#ifndef PUT_UINT32_BE
95#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000096{ \
97 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
98 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
99 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
100 (b)[(i) + 3] = (unsigned char) ( (n) ); \
101}
102#endif
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200105{
Hanno Becker039ccab2018-12-18 17:52:14 +0000106 SHA1_VALIDATE( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200109}
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200112{
113 if( ctx == NULL )
114 return;
115
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500116 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200117}
118
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200119void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
120 const mbedtls_sha1_context *src )
121{
Hanno Becker039ccab2018-12-18 17:52:14 +0000122 SHA1_VALIDATE( dst != NULL );
123 SHA1_VALIDATE( src != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000124
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200125 *dst = *src;
126}
127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128/*
129 * SHA-1 context setup
130 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100131int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000132{
Hanno Becker039ccab2018-12-18 17:52:14 +0000133 SHA1_VALIDATE_RET( ctx != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000134
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 ctx->total[0] = 0;
136 ctx->total[1] = 0;
137
138 ctx->state[0] = 0x67452301;
139 ctx->state[1] = 0xEFCDAB89;
140 ctx->state[2] = 0x98BADCFE;
141 ctx->state[3] = 0x10325476;
142 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100143
144 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145}
146
Jaeden Amero041039f2018-02-19 15:28:08 +0000147#if !defined(MBEDTLS_DEPRECATED_REMOVED)
148void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
149{
150 mbedtls_sha1_starts_ret( ctx );
151}
152#endif
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100155int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
156 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000157{
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200158 struct
159 {
160 uint32_t temp, W[16], A, B, C, D, E;
161 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Hanno Becker039ccab2018-12-18 17:52:14 +0000163 SHA1_VALIDATE_RET( ctx != NULL );
164 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000165
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200166 GET_UINT32_BE( local.W[ 0], data, 0 );
167 GET_UINT32_BE( local.W[ 1], data, 4 );
168 GET_UINT32_BE( local.W[ 2], data, 8 );
169 GET_UINT32_BE( local.W[ 3], data, 12 );
170 GET_UINT32_BE( local.W[ 4], data, 16 );
171 GET_UINT32_BE( local.W[ 5], data, 20 );
172 GET_UINT32_BE( local.W[ 6], data, 24 );
173 GET_UINT32_BE( local.W[ 7], data, 28 );
174 GET_UINT32_BE( local.W[ 8], data, 32 );
175 GET_UINT32_BE( local.W[ 9], data, 36 );
176 GET_UINT32_BE( local.W[10], data, 40 );
177 GET_UINT32_BE( local.W[11], data, 44 );
178 GET_UINT32_BE( local.W[12], data, 48 );
179 GET_UINT32_BE( local.W[13], data, 52 );
180 GET_UINT32_BE( local.W[14], data, 56 );
181 GET_UINT32_BE( local.W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
Hanno Beckerd6028a12018-10-15 12:01:35 +0100183#define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
Hanno Beckerd6028a12018-10-15 12:01:35 +0100185#define R(t) \
186 ( \
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200187 local.temp = local.W[( (t) - 3 ) & 0x0F] ^ \
188 local.W[( (t) - 8 ) & 0x0F] ^ \
189 local.W[( (t) - 14 ) & 0x0F] ^ \
190 local.W[ (t) & 0x0F], \
191 ( local.W[(t) & 0x0F] = S(local.temp,1) ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100192 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
Hanno Beckerd6028a12018-10-15 12:01:35 +0100194#define P(a,b,c,d,e,x) \
195 do \
196 { \
Hanno Becker3ac21ac2018-10-26 09:13:26 +0100197 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
198 (b) = S((b),30); \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100199 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200201 local.A = ctx->state[0];
202 local.B = ctx->state[1];
203 local.C = ctx->state[2];
204 local.D = ctx->state[3];
205 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
Hanno Beckerd6028a12018-10-15 12:01:35 +0100207#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000208#define K 0x5A827999
209
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200210 P( local.A, local.B, local.C, local.D, local.E, local.W[0] );
211 P( local.E, local.A, local.B, local.C, local.D, local.W[1] );
212 P( local.D, local.E, local.A, local.B, local.C, local.W[2] );
213 P( local.C, local.D, local.E, local.A, local.B, local.W[3] );
214 P( local.B, local.C, local.D, local.E, local.A, local.W[4] );
215 P( local.A, local.B, local.C, local.D, local.E, local.W[5] );
216 P( local.E, local.A, local.B, local.C, local.D, local.W[6] );
217 P( local.D, local.E, local.A, local.B, local.C, local.W[7] );
218 P( local.C, local.D, local.E, local.A, local.B, local.W[8] );
219 P( local.B, local.C, local.D, local.E, local.A, local.W[9] );
220 P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
221 P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
222 P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
223 P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
224 P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
225 P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
226 P( local.E, local.A, local.B, local.C, local.D, R(16) );
227 P( local.D, local.E, local.A, local.B, local.C, R(17) );
228 P( local.C, local.D, local.E, local.A, local.B, R(18) );
229 P( local.B, local.C, local.D, local.E, local.A, R(19) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
231#undef K
232#undef F
233
Hanno Beckerd6028a12018-10-15 12:01:35 +0100234#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000235#define K 0x6ED9EBA1
236
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200237 P( local.A, local.B, local.C, local.D, local.E, R(20) );
238 P( local.E, local.A, local.B, local.C, local.D, R(21) );
239 P( local.D, local.E, local.A, local.B, local.C, R(22) );
240 P( local.C, local.D, local.E, local.A, local.B, R(23) );
241 P( local.B, local.C, local.D, local.E, local.A, R(24) );
242 P( local.A, local.B, local.C, local.D, local.E, R(25) );
243 P( local.E, local.A, local.B, local.C, local.D, R(26) );
244 P( local.D, local.E, local.A, local.B, local.C, R(27) );
245 P( local.C, local.D, local.E, local.A, local.B, R(28) );
246 P( local.B, local.C, local.D, local.E, local.A, R(29) );
247 P( local.A, local.B, local.C, local.D, local.E, R(30) );
248 P( local.E, local.A, local.B, local.C, local.D, R(31) );
249 P( local.D, local.E, local.A, local.B, local.C, R(32) );
250 P( local.C, local.D, local.E, local.A, local.B, R(33) );
251 P( local.B, local.C, local.D, local.E, local.A, R(34) );
252 P( local.A, local.B, local.C, local.D, local.E, R(35) );
253 P( local.E, local.A, local.B, local.C, local.D, R(36) );
254 P( local.D, local.E, local.A, local.B, local.C, R(37) );
255 P( local.C, local.D, local.E, local.A, local.B, R(38) );
256 P( local.B, local.C, local.D, local.E, local.A, R(39) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
258#undef K
259#undef F
260
Hanno Beckerd6028a12018-10-15 12:01:35 +0100261#define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000262#define K 0x8F1BBCDC
263
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200264 P( local.A, local.B, local.C, local.D, local.E, R(40) );
265 P( local.E, local.A, local.B, local.C, local.D, R(41) );
266 P( local.D, local.E, local.A, local.B, local.C, R(42) );
267 P( local.C, local.D, local.E, local.A, local.B, R(43) );
268 P( local.B, local.C, local.D, local.E, local.A, R(44) );
269 P( local.A, local.B, local.C, local.D, local.E, R(45) );
270 P( local.E, local.A, local.B, local.C, local.D, R(46) );
271 P( local.D, local.E, local.A, local.B, local.C, R(47) );
272 P( local.C, local.D, local.E, local.A, local.B, R(48) );
273 P( local.B, local.C, local.D, local.E, local.A, R(49) );
274 P( local.A, local.B, local.C, local.D, local.E, R(50) );
275 P( local.E, local.A, local.B, local.C, local.D, R(51) );
276 P( local.D, local.E, local.A, local.B, local.C, R(52) );
277 P( local.C, local.D, local.E, local.A, local.B, R(53) );
278 P( local.B, local.C, local.D, local.E, local.A, R(54) );
279 P( local.A, local.B, local.C, local.D, local.E, R(55) );
280 P( local.E, local.A, local.B, local.C, local.D, R(56) );
281 P( local.D, local.E, local.A, local.B, local.C, R(57) );
282 P( local.C, local.D, local.E, local.A, local.B, R(58) );
283 P( local.B, local.C, local.D, local.E, local.A, R(59) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285#undef K
286#undef F
287
Hanno Beckerd6028a12018-10-15 12:01:35 +0100288#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000289#define K 0xCA62C1D6
290
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200291 P( local.A, local.B, local.C, local.D, local.E, R(60) );
292 P( local.E, local.A, local.B, local.C, local.D, R(61) );
293 P( local.D, local.E, local.A, local.B, local.C, R(62) );
294 P( local.C, local.D, local.E, local.A, local.B, R(63) );
295 P( local.B, local.C, local.D, local.E, local.A, R(64) );
296 P( local.A, local.B, local.C, local.D, local.E, R(65) );
297 P( local.E, local.A, local.B, local.C, local.D, R(66) );
298 P( local.D, local.E, local.A, local.B, local.C, R(67) );
299 P( local.C, local.D, local.E, local.A, local.B, R(68) );
300 P( local.B, local.C, local.D, local.E, local.A, R(69) );
301 P( local.A, local.B, local.C, local.D, local.E, R(70) );
302 P( local.E, local.A, local.B, local.C, local.D, R(71) );
303 P( local.D, local.E, local.A, local.B, local.C, R(72) );
304 P( local.C, local.D, local.E, local.A, local.B, R(73) );
305 P( local.B, local.C, local.D, local.E, local.A, R(74) );
306 P( local.A, local.B, local.C, local.D, local.E, R(75) );
307 P( local.E, local.A, local.B, local.C, local.D, R(76) );
308 P( local.D, local.E, local.A, local.B, local.C, R(77) );
309 P( local.C, local.D, local.E, local.A, local.B, R(78) );
310 P( local.B, local.C, local.D, local.E, local.A, R(79) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312#undef K
313#undef F
314
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200315 ctx->state[0] += local.A;
316 ctx->state[1] += local.B;
317 ctx->state[2] += local.C;
318 ctx->state[3] += local.D;
319 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100320
gabor-mezei-armd5253bb2020-07-30 16:41:25 +0200321 /* Zeroise buffers and variables to clear sensitive data from memory. */
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200322 mbedtls_platform_zeroize( &local, sizeof( local ) );
gabor-mezei-armd5253bb2020-07-30 16:41:25 +0200323
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100324 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325}
Jaeden Amero041039f2018-02-19 15:28:08 +0000326
327#if !defined(MBEDTLS_DEPRECATED_REMOVED)
328void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
329 const unsigned char data[64] )
330{
331 mbedtls_internal_sha1_process( ctx, data );
332}
333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336/*
337 * SHA-1 process buffer
338 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100339int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100340 const unsigned char *input,
341 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000342{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100343 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000344 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000345 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
Hanno Becker039ccab2018-12-18 17:52:14 +0000347 SHA1_VALIDATE_RET( ctx != NULL );
348 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
Hanno Beckerb3906d82018-12-18 11:35:00 +0000349
Brian White12895d12014-04-11 11:29:42 -0400350 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100351 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353 left = ctx->total[0] & 0x3F;
354 fill = 64 - left;
355
Paul Bakker5c2364c2012-10-01 14:41:15 +0000356 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 ctx->total[0] &= 0xFFFFFFFF;
358
Paul Bakker5c2364c2012-10-01 14:41:15 +0000359 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 ctx->total[1]++;
361
362 if( left && ilen >= fill )
363 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200364 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100365
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100366 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100367 return( ret );
368
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 input += fill;
370 ilen -= fill;
371 left = 0;
372 }
373
374 while( ilen >= 64 )
375 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100376 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100377 return( ret );
378
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 input += 64;
380 ilen -= 64;
381 }
382
383 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200384 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100385
386 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387}
388
Jaeden Amero041039f2018-02-19 15:28:08 +0000389#if !defined(MBEDTLS_DEPRECATED_REMOVED)
390void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
391 const unsigned char *input,
392 size_t ilen )
393{
394 mbedtls_sha1_update_ret( ctx, input, ilen );
395}
396#endif
397
Paul Bakker5121ce52009-01-03 21:22:43 +0000398/*
399 * SHA-1 final digest
400 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100401int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100402 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000403{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100404 int ret;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200405 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000406 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
Hanno Becker039ccab2018-12-18 17:52:14 +0000408 SHA1_VALIDATE_RET( ctx != NULL );
409 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000410
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200411 /*
412 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
413 */
414 used = ctx->total[0] & 0x3F;
415
416 ctx->buffer[used++] = 0x80;
417
418 if( used <= 56 )
419 {
420 /* Enough room for padding + length in current block */
421 memset( ctx->buffer + used, 0, 56 - used );
422 }
423 else
424 {
425 /* We'll need an extra block */
426 memset( ctx->buffer + used, 0, 64 - used );
427
428 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
429 return( ret );
430
431 memset( ctx->buffer, 0, 56 );
432 }
433
434 /*
435 * Add message length
436 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 high = ( ctx->total[0] >> 29 )
438 | ( ctx->total[1] << 3 );
439 low = ( ctx->total[0] << 3 );
440
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200441 PUT_UINT32_BE( high, ctx->buffer, 56 );
442 PUT_UINT32_BE( low, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200444 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100445 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200447 /*
448 * Output final state
449 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000450 PUT_UINT32_BE( ctx->state[0], output, 0 );
451 PUT_UINT32_BE( ctx->state[1], output, 4 );
452 PUT_UINT32_BE( ctx->state[2], output, 8 );
453 PUT_UINT32_BE( ctx->state[3], output, 12 );
454 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100455
456 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457}
458
Jaeden Amero041039f2018-02-19 15:28:08 +0000459#if !defined(MBEDTLS_DEPRECATED_REMOVED)
460void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
461 unsigned char output[20] )
462{
463 mbedtls_sha1_finish_ret( ctx, output );
464}
465#endif
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200468
Paul Bakker5121ce52009-01-03 21:22:43 +0000469/*
470 * output = SHA-1( input buffer )
471 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100472int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100473 size_t ilen,
474 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000475{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100476 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
Hanno Becker039ccab2018-12-18 17:52:14 +0000479 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
480 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100483
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100484 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100485 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100486
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100488 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100489
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100490 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100491 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100492
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100493exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100495
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100496 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497}
498
Jaeden Amero041039f2018-02-19 15:28:08 +0000499#if !defined(MBEDTLS_DEPRECATED_REMOVED)
500void mbedtls_sha1( const unsigned char *input,
501 size_t ilen,
502 unsigned char output[20] )
503{
504 mbedtls_sha1_ret( input, ilen, output );
505}
506#endif
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000509/*
510 * FIPS-180-1 test vectors
511 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000512static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000513{
514 { "abc" },
515 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
516 { "" }
517};
518
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100519static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000520{
521 3, 56, 1000
522};
523
524static const unsigned char sha1_test_sum[3][20] =
525{
526 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
527 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
528 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
529 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
530 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
531 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
532};
533
534/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 * Checkup routine
536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000538{
Paul Bakker5b4af392014-06-26 12:09:34 +0200539 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 unsigned char buf[1024];
541 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200545
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 /*
547 * SHA-1
548 */
549 for( i = 0; i < 3; i++ )
550 {
551 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100554 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100555 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557 if( i == 2 )
558 {
559 memset( buf, 'a', buflen = 1000 );
560
561 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100562 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100563 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100564 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100565 goto fail;
566 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 }
568 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100569 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100570 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100571 sha1_test_buflen[i] );
572 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100573 goto fail;
574 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100576 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100577 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
579 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100580 {
581 ret = 1;
582 goto fail;
583 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 }
588
589 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100592 goto exit;
593
594fail:
595 if( verbose != 0 )
596 mbedtls_printf( "failed\n" );
597
Paul Bakker5b4af392014-06-26 12:09:34 +0200598exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200600
601 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602}
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#endif /* MBEDTLS_SHA1_C */