blob: 1587de48052decb6c27d6e971c7141e23f74b7a6 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/sha1.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050036#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_SELF_TEST)
41#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010043#else
Rich Evans00ab4702015-02-06 13:43:58 +000044#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define mbedtls_printf printf
46#endif /* MBEDTLS_PLATFORM_C */
47#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010048
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020049#if !defined(MBEDTLS_SHA1_ALT)
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/*
52 * 32-bit integer manipulation macros (big endian)
53 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000054#ifndef GET_UINT32_BE
55#define GET_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000056{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
58 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
59 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
60 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000061}
62#endif
63
Paul Bakker5c2364c2012-10-01 14:41:15 +000064#ifndef PUT_UINT32_BE
65#define PUT_UINT32_BE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000066{ \
67 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
68 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
69 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
70 (b)[(i) + 3] = (unsigned char) ( (n) ); \
71}
72#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020077}
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020080{
81 if( ctx == NULL )
82 return;
83
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050084 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020085}
86
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020087void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
88 const mbedtls_sha1_context *src )
89{
90 *dst = *src;
91}
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093/*
94 * SHA-1 context setup
95 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010096int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
98 ctx->total[0] = 0;
99 ctx->total[1] = 0;
100
101 ctx->state[0] = 0x67452301;
102 ctx->state[1] = 0xEFCDAB89;
103 ctx->state[2] = 0x98BADCFE;
104 ctx->state[3] = 0x10325476;
105 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100106
107 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108}
109
Jaeden Amero041039f2018-02-19 15:28:08 +0000110#if !defined(MBEDTLS_DEPRECATED_REMOVED)
111void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
112{
113 mbedtls_sha1_starts_ret( ctx );
114}
115#endif
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100118int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
119 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000120{
Paul Bakker5c2364c2012-10-01 14:41:15 +0000121 uint32_t temp, W[16], A, B, C, D, E;
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Paul Bakker5c2364c2012-10-01 14:41:15 +0000123 GET_UINT32_BE( W[ 0], data, 0 );
124 GET_UINT32_BE( W[ 1], data, 4 );
125 GET_UINT32_BE( W[ 2], data, 8 );
126 GET_UINT32_BE( W[ 3], data, 12 );
127 GET_UINT32_BE( W[ 4], data, 16 );
128 GET_UINT32_BE( W[ 5], data, 20 );
129 GET_UINT32_BE( W[ 6], data, 24 );
130 GET_UINT32_BE( W[ 7], data, 28 );
131 GET_UINT32_BE( W[ 8], data, 32 );
132 GET_UINT32_BE( W[ 9], data, 36 );
133 GET_UINT32_BE( W[10], data, 40 );
134 GET_UINT32_BE( W[11], data, 44 );
135 GET_UINT32_BE( W[12], data, 48 );
136 GET_UINT32_BE( W[13], data, 52 );
137 GET_UINT32_BE( W[14], data, 56 );
138 GET_UINT32_BE( W[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
141
142#define R(t) \
143( \
Paul Bakker66d5d072014-06-17 16:39:18 +0200144 temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
145 W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 ( W[t & 0x0F] = S(temp,1) ) \
147)
148
149#define P(a,b,c,d,e,x) \
150{ \
151 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
152}
153
154 A = ctx->state[0];
155 B = ctx->state[1];
156 C = ctx->state[2];
157 D = ctx->state[3];
158 E = ctx->state[4];
159
160#define F(x,y,z) (z ^ (x & (y ^ z)))
161#define K 0x5A827999
162
163 P( A, B, C, D, E, W[0] );
164 P( E, A, B, C, D, W[1] );
165 P( D, E, A, B, C, W[2] );
166 P( C, D, E, A, B, W[3] );
167 P( B, C, D, E, A, W[4] );
168 P( A, B, C, D, E, W[5] );
169 P( E, A, B, C, D, W[6] );
170 P( D, E, A, B, C, W[7] );
171 P( C, D, E, A, B, W[8] );
172 P( B, C, D, E, A, W[9] );
173 P( A, B, C, D, E, W[10] );
174 P( E, A, B, C, D, W[11] );
175 P( D, E, A, B, C, W[12] );
176 P( C, D, E, A, B, W[13] );
177 P( B, C, D, E, A, W[14] );
178 P( A, B, C, D, E, W[15] );
179 P( E, A, B, C, D, R(16) );
180 P( D, E, A, B, C, R(17) );
181 P( C, D, E, A, B, R(18) );
182 P( B, C, D, E, A, R(19) );
183
184#undef K
185#undef F
186
187#define F(x,y,z) (x ^ y ^ z)
188#define K 0x6ED9EBA1
189
190 P( A, B, C, D, E, R(20) );
191 P( E, A, B, C, D, R(21) );
192 P( D, E, A, B, C, R(22) );
193 P( C, D, E, A, B, R(23) );
194 P( B, C, D, E, A, R(24) );
195 P( A, B, C, D, E, R(25) );
196 P( E, A, B, C, D, R(26) );
197 P( D, E, A, B, C, R(27) );
198 P( C, D, E, A, B, R(28) );
199 P( B, C, D, E, A, R(29) );
200 P( A, B, C, D, E, R(30) );
201 P( E, A, B, C, D, R(31) );
202 P( D, E, A, B, C, R(32) );
203 P( C, D, E, A, B, R(33) );
204 P( B, C, D, E, A, R(34) );
205 P( A, B, C, D, E, R(35) );
206 P( E, A, B, C, D, R(36) );
207 P( D, E, A, B, C, R(37) );
208 P( C, D, E, A, B, R(38) );
209 P( B, C, D, E, A, R(39) );
210
211#undef K
212#undef F
213
214#define F(x,y,z) ((x & y) | (z & (x | y)))
215#define K 0x8F1BBCDC
216
217 P( A, B, C, D, E, R(40) );
218 P( E, A, B, C, D, R(41) );
219 P( D, E, A, B, C, R(42) );
220 P( C, D, E, A, B, R(43) );
221 P( B, C, D, E, A, R(44) );
222 P( A, B, C, D, E, R(45) );
223 P( E, A, B, C, D, R(46) );
224 P( D, E, A, B, C, R(47) );
225 P( C, D, E, A, B, R(48) );
226 P( B, C, D, E, A, R(49) );
227 P( A, B, C, D, E, R(50) );
228 P( E, A, B, C, D, R(51) );
229 P( D, E, A, B, C, R(52) );
230 P( C, D, E, A, B, R(53) );
231 P( B, C, D, E, A, R(54) );
232 P( A, B, C, D, E, R(55) );
233 P( E, A, B, C, D, R(56) );
234 P( D, E, A, B, C, R(57) );
235 P( C, D, E, A, B, R(58) );
236 P( B, C, D, E, A, R(59) );
237
238#undef K
239#undef F
240
241#define F(x,y,z) (x ^ y ^ z)
242#define K 0xCA62C1D6
243
244 P( A, B, C, D, E, R(60) );
245 P( E, A, B, C, D, R(61) );
246 P( D, E, A, B, C, R(62) );
247 P( C, D, E, A, B, R(63) );
248 P( B, C, D, E, A, R(64) );
249 P( A, B, C, D, E, R(65) );
250 P( E, A, B, C, D, R(66) );
251 P( D, E, A, B, C, R(67) );
252 P( C, D, E, A, B, R(68) );
253 P( B, C, D, E, A, R(69) );
254 P( A, B, C, D, E, R(70) );
255 P( E, A, B, C, D, R(71) );
256 P( D, E, A, B, C, R(72) );
257 P( C, D, E, A, B, R(73) );
258 P( B, C, D, E, A, R(74) );
259 P( A, B, C, D, E, R(75) );
260 P( E, A, B, C, D, R(76) );
261 P( D, E, A, B, C, R(77) );
262 P( C, D, E, A, B, R(78) );
263 P( B, C, D, E, A, R(79) );
264
265#undef K
266#undef F
267
268 ctx->state[0] += A;
269 ctx->state[1] += B;
270 ctx->state[2] += C;
271 ctx->state[3] += D;
272 ctx->state[4] += E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100273
274 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275}
Jaeden Amero041039f2018-02-19 15:28:08 +0000276
277#if !defined(MBEDTLS_DEPRECATED_REMOVED)
278void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
279 const unsigned char data[64] )
280{
281 mbedtls_internal_sha1_process( ctx, data );
282}
283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286/*
287 * SHA-1 process buffer
288 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100289int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100290 const unsigned char *input,
291 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000292{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100293 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000294 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000295 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Brian White12895d12014-04-11 11:29:42 -0400297 if( ilen == 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100298 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 left = ctx->total[0] & 0x3F;
301 fill = 64 - left;
302
Paul Bakker5c2364c2012-10-01 14:41:15 +0000303 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 ctx->total[0] &= 0xFFFFFFFF;
305
Paul Bakker5c2364c2012-10-01 14:41:15 +0000306 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 ctx->total[1]++;
308
309 if( left && ilen >= fill )
310 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200311 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100312
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100313 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100314 return( ret );
315
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 input += fill;
317 ilen -= fill;
318 left = 0;
319 }
320
321 while( ilen >= 64 )
322 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100323 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100324 return( ret );
325
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 input += 64;
327 ilen -= 64;
328 }
329
330 if( ilen > 0 )
Paul Bakker3c2122f2013-06-24 19:03:14 +0200331 memcpy( (void *) (ctx->buffer + left), input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100332
333 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334}
335
Jaeden Amero041039f2018-02-19 15:28:08 +0000336#if !defined(MBEDTLS_DEPRECATED_REMOVED)
337void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
338 const unsigned char *input,
339 size_t ilen )
340{
341 mbedtls_sha1_update_ret( ctx, input, ilen );
342}
343#endif
344
Paul Bakker5121ce52009-01-03 21:22:43 +0000345static const unsigned char sha1_padding[64] =
346{
347 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
348 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
349 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
350 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
351};
352
353/*
354 * SHA-1 final digest
355 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100356int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100357 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000358{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100359 int ret;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000360 uint32_t last, padn;
361 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 unsigned char msglen[8];
363
364 high = ( ctx->total[0] >> 29 )
365 | ( ctx->total[1] << 3 );
366 low = ( ctx->total[0] << 3 );
367
Paul Bakker5c2364c2012-10-01 14:41:15 +0000368 PUT_UINT32_BE( high, msglen, 0 );
369 PUT_UINT32_BE( low, msglen, 4 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
371 last = ctx->total[0] & 0x3F;
372 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
373
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100374 if( ( ret = mbedtls_sha1_update_ret( ctx, sha1_padding, padn ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100375 return( ret );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100376 if( ( ret = mbedtls_sha1_update_ret( ctx, msglen, 8 ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100377 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
Paul Bakker5c2364c2012-10-01 14:41:15 +0000379 PUT_UINT32_BE( ctx->state[0], output, 0 );
380 PUT_UINT32_BE( ctx->state[1], output, 4 );
381 PUT_UINT32_BE( ctx->state[2], output, 8 );
382 PUT_UINT32_BE( ctx->state[3], output, 12 );
383 PUT_UINT32_BE( ctx->state[4], output, 16 );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100384
385 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386}
387
Jaeden Amero041039f2018-02-19 15:28:08 +0000388#if !defined(MBEDTLS_DEPRECATED_REMOVED)
389void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
390 unsigned char output[20] )
391{
392 mbedtls_sha1_finish_ret( ctx, output );
393}
394#endif
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200397
Paul Bakker5121ce52009-01-03 21:22:43 +0000398/*
399 * output = SHA-1( input buffer )
400 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100401int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100402 size_t ilen,
403 unsigned char output[20] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000404{
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100405 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_sha1_init( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100409
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100410 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100411 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100412
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100413 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100414 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100415
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100416 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100417 goto exit;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100418
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100419exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_sha1_free( &ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100421
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100422 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423}
424
Jaeden Amero041039f2018-02-19 15:28:08 +0000425#if !defined(MBEDTLS_DEPRECATED_REMOVED)
426void mbedtls_sha1( const unsigned char *input,
427 size_t ilen,
428 unsigned char output[20] )
429{
430 mbedtls_sha1_ret( input, ilen, output );
431}
432#endif
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000435/*
436 * FIPS-180-1 test vectors
437 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000438static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000439{
440 { "abc" },
441 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
442 { "" }
443};
444
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100445static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000446{
447 3, 56, 1000
448};
449
450static const unsigned char sha1_test_sum[3][20] =
451{
452 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
453 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
454 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
455 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
456 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
457 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
458};
459
460/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 * Checkup routine
462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463int mbedtls_sha1_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000464{
Paul Bakker5b4af392014-06-26 12:09:34 +0200465 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 unsigned char buf[1024];
467 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_sha1_init( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200471
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 /*
473 * SHA-1
474 */
475 for( i = 0; i < 3; i++ )
476 {
477 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100480 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100481 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483 if( i == 2 )
484 {
485 memset( buf, 'a', buflen = 1000 );
486
487 for( j = 0; j < 1000; j++ )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100488 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100489 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100490 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100491 goto fail;
492 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 }
494 else
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100495 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100496 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100497 sha1_test_buflen[i] );
498 if( ret != 0 )
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100499 goto fail;
500 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100502 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100503 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100506 {
507 ret = 1;
508 goto fail;
509 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 }
514
515 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100518 goto exit;
519
520fail:
521 if( verbose != 0 )
522 mbedtls_printf( "failed\n" );
523
Paul Bakker5b4af392014-06-26 12:09:34 +0200524exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_sha1_free( &ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200526
527 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528}
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#endif /* MBEDTLS_SHA1_C */