blob: b13381d30ac250f6c192b8b8d595cecbf039e397 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * FIPS-180-1 compliant SHA-1 implementation
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7/*
8 * The SHA-1 standard was published by NIST in 1993.
9 *
10 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
11 */
12
Gilles Peskinedb09ef62020-06-03 01:43:33 +020013#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000016
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/sha1.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050018#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000019#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000020
Rich Evans00ab4702015-02-06 13:43:58 +000021#include <string.h>
22
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010024
Gilles Peskine449bd832023-01-11 14:50:10 +010025void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +020026{
Gilles Peskine449bd832023-01-11 14:50:10 +010027 memset(ctx, 0, sizeof(mbedtls_sha1_context));
Paul Bakker5b4af392014-06-26 12:09:34 +020028}
29
Gilles Peskine449bd832023-01-11 14:50:10 +010030void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +020031{
Gilles Peskine449bd832023-01-11 14:50:10 +010032 if (ctx == NULL) {
Paul Bakker5b4af392014-06-26 12:09:34 +020033 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010034 }
Paul Bakker5b4af392014-06-26 12:09:34 +020035
Gilles Peskine449bd832023-01-11 14:50:10 +010036 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha1_context));
Paul Bakker5b4af392014-06-26 12:09:34 +020037}
38
Gilles Peskine449bd832023-01-11 14:50:10 +010039void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
40 const mbedtls_sha1_context *src)
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020041{
42 *dst = *src;
43}
44
Paul Bakker5121ce52009-01-03 21:22:43 +000045/*
46 * SHA-1 context setup
47 */
Gilles Peskine449bd832023-01-11 14:50:10 +010048int mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +000049{
50 ctx->total[0] = 0;
51 ctx->total[1] = 0;
52
53 ctx->state[0] = 0x67452301;
54 ctx->state[1] = 0xEFCDAB89;
55 ctx->state[2] = 0x98BADCFE;
56 ctx->state[3] = 0x10325476;
57 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010058
Gilles Peskine449bd832023-01-11 14:50:10 +010059 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000060}
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
63 const unsigned char data[64])
Paul Bakker5121ce52009-01-03 21:22:43 +000064{
Gilles Peskine449bd832023-01-11 14:50:10 +010065 struct {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +020066 uint32_t temp, W[16], A, B, C, D, E;
67 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 local.W[0] = MBEDTLS_GET_UINT32_BE(data, 0);
70 local.W[1] = MBEDTLS_GET_UINT32_BE(data, 4);
71 local.W[2] = MBEDTLS_GET_UINT32_BE(data, 8);
72 local.W[3] = MBEDTLS_GET_UINT32_BE(data, 12);
73 local.W[4] = MBEDTLS_GET_UINT32_BE(data, 16);
74 local.W[5] = MBEDTLS_GET_UINT32_BE(data, 20);
75 local.W[6] = MBEDTLS_GET_UINT32_BE(data, 24);
76 local.W[7] = MBEDTLS_GET_UINT32_BE(data, 28);
77 local.W[8] = MBEDTLS_GET_UINT32_BE(data, 32);
78 local.W[9] = MBEDTLS_GET_UINT32_BE(data, 36);
79 local.W[10] = MBEDTLS_GET_UINT32_BE(data, 40);
80 local.W[11] = MBEDTLS_GET_UINT32_BE(data, 44);
81 local.W[12] = MBEDTLS_GET_UINT32_BE(data, 48);
82 local.W[13] = MBEDTLS_GET_UINT32_BE(data, 52);
83 local.W[14] = MBEDTLS_GET_UINT32_BE(data, 56);
84 local.W[15] = MBEDTLS_GET_UINT32_BE(data, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Gilles Peskine449bd832023-01-11 14:50:10 +010086#define S(x, n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Hanno Becker1eeca412018-10-15 12:01:35 +010088#define R(t) \
89 ( \
Gilles Peskine449bd832023-01-11 14:50:10 +010090 local.temp = local.W[((t) - 3) & 0x0F] ^ \
91 local.W[((t) - 8) & 0x0F] ^ \
92 local.W[((t) - 14) & 0x0F] ^ \
93 local.W[(t) & 0x0F], \
94 (local.W[(t) & 0x0F] = S(local.temp, 1)) \
Hanno Becker1eeca412018-10-15 12:01:35 +010095 )
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Gilles Peskine449bd832023-01-11 14:50:10 +010097#define P(a, b, c, d, e, x) \
Hanno Becker1eeca412018-10-15 12:01:35 +010098 do \
99 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 (e) += S((a), 5) + F((b), (c), (d)) + K + (x); \
101 (b) = S((b), 30); \
102 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200104 local.A = ctx->state[0];
105 local.B = ctx->state[1];
106 local.C = ctx->state[2];
107 local.D = ctx->state[3];
108 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000111#define K 0x5A827999
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 P(local.A, local.B, local.C, local.D, local.E, local.W[0]);
114 P(local.E, local.A, local.B, local.C, local.D, local.W[1]);
115 P(local.D, local.E, local.A, local.B, local.C, local.W[2]);
116 P(local.C, local.D, local.E, local.A, local.B, local.W[3]);
117 P(local.B, local.C, local.D, local.E, local.A, local.W[4]);
118 P(local.A, local.B, local.C, local.D, local.E, local.W[5]);
119 P(local.E, local.A, local.B, local.C, local.D, local.W[6]);
120 P(local.D, local.E, local.A, local.B, local.C, local.W[7]);
121 P(local.C, local.D, local.E, local.A, local.B, local.W[8]);
122 P(local.B, local.C, local.D, local.E, local.A, local.W[9]);
123 P(local.A, local.B, local.C, local.D, local.E, local.W[10]);
124 P(local.E, local.A, local.B, local.C, local.D, local.W[11]);
125 P(local.D, local.E, local.A, local.B, local.C, local.W[12]);
126 P(local.C, local.D, local.E, local.A, local.B, local.W[13]);
127 P(local.B, local.C, local.D, local.E, local.A, local.W[14]);
128 P(local.A, local.B, local.C, local.D, local.E, local.W[15]);
129 P(local.E, local.A, local.B, local.C, local.D, R(16));
130 P(local.D, local.E, local.A, local.B, local.C, R(17));
131 P(local.C, local.D, local.E, local.A, local.B, R(18));
132 P(local.B, local.C, local.D, local.E, local.A, R(19));
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134#undef K
135#undef F
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000138#define K 0x6ED9EBA1
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 P(local.A, local.B, local.C, local.D, local.E, R(20));
141 P(local.E, local.A, local.B, local.C, local.D, R(21));
142 P(local.D, local.E, local.A, local.B, local.C, R(22));
143 P(local.C, local.D, local.E, local.A, local.B, R(23));
144 P(local.B, local.C, local.D, local.E, local.A, R(24));
145 P(local.A, local.B, local.C, local.D, local.E, R(25));
146 P(local.E, local.A, local.B, local.C, local.D, R(26));
147 P(local.D, local.E, local.A, local.B, local.C, R(27));
148 P(local.C, local.D, local.E, local.A, local.B, R(28));
149 P(local.B, local.C, local.D, local.E, local.A, R(29));
150 P(local.A, local.B, local.C, local.D, local.E, R(30));
151 P(local.E, local.A, local.B, local.C, local.D, R(31));
152 P(local.D, local.E, local.A, local.B, local.C, R(32));
153 P(local.C, local.D, local.E, local.A, local.B, R(33));
154 P(local.B, local.C, local.D, local.E, local.A, R(34));
155 P(local.A, local.B, local.C, local.D, local.E, R(35));
156 P(local.E, local.A, local.B, local.C, local.D, R(36));
157 P(local.D, local.E, local.A, local.B, local.C, R(37));
158 P(local.C, local.D, local.E, local.A, local.B, R(38));
159 P(local.B, local.C, local.D, local.E, local.A, R(39));
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161#undef K
162#undef F
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164#define F(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000165#define K 0x8F1BBCDC
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 P(local.A, local.B, local.C, local.D, local.E, R(40));
168 P(local.E, local.A, local.B, local.C, local.D, R(41));
169 P(local.D, local.E, local.A, local.B, local.C, R(42));
170 P(local.C, local.D, local.E, local.A, local.B, R(43));
171 P(local.B, local.C, local.D, local.E, local.A, R(44));
172 P(local.A, local.B, local.C, local.D, local.E, R(45));
173 P(local.E, local.A, local.B, local.C, local.D, R(46));
174 P(local.D, local.E, local.A, local.B, local.C, R(47));
175 P(local.C, local.D, local.E, local.A, local.B, R(48));
176 P(local.B, local.C, local.D, local.E, local.A, R(49));
177 P(local.A, local.B, local.C, local.D, local.E, R(50));
178 P(local.E, local.A, local.B, local.C, local.D, R(51));
179 P(local.D, local.E, local.A, local.B, local.C, R(52));
180 P(local.C, local.D, local.E, local.A, local.B, R(53));
181 P(local.B, local.C, local.D, local.E, local.A, R(54));
182 P(local.A, local.B, local.C, local.D, local.E, R(55));
183 P(local.E, local.A, local.B, local.C, local.D, R(56));
184 P(local.D, local.E, local.A, local.B, local.C, R(57));
185 P(local.C, local.D, local.E, local.A, local.B, R(58));
186 P(local.B, local.C, local.D, local.E, local.A, R(59));
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
188#undef K
189#undef F
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000192#define K 0xCA62C1D6
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 P(local.A, local.B, local.C, local.D, local.E, R(60));
195 P(local.E, local.A, local.B, local.C, local.D, R(61));
196 P(local.D, local.E, local.A, local.B, local.C, R(62));
197 P(local.C, local.D, local.E, local.A, local.B, R(63));
198 P(local.B, local.C, local.D, local.E, local.A, R(64));
199 P(local.A, local.B, local.C, local.D, local.E, R(65));
200 P(local.E, local.A, local.B, local.C, local.D, R(66));
201 P(local.D, local.E, local.A, local.B, local.C, R(67));
202 P(local.C, local.D, local.E, local.A, local.B, R(68));
203 P(local.B, local.C, local.D, local.E, local.A, R(69));
204 P(local.A, local.B, local.C, local.D, local.E, R(70));
205 P(local.E, local.A, local.B, local.C, local.D, R(71));
206 P(local.D, local.E, local.A, local.B, local.C, R(72));
207 P(local.C, local.D, local.E, local.A, local.B, R(73));
208 P(local.B, local.C, local.D, local.E, local.A, R(74));
209 P(local.A, local.B, local.C, local.D, local.E, R(75));
210 P(local.E, local.A, local.B, local.C, local.D, R(76));
211 P(local.D, local.E, local.A, local.B, local.C, R(77));
212 P(local.C, local.D, local.E, local.A, local.B, R(78));
213 P(local.B, local.C, local.D, local.E, local.A, R(79));
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215#undef K
216#undef F
217
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200218 ctx->state[0] += local.A;
219 ctx->state[1] += local.B;
220 ctx->state[2] += local.C;
221 ctx->state[3] += local.D;
222 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100223
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200224 /* Zeroise buffers and variables to clear sensitive data from memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 mbedtls_platform_zeroize(&local, sizeof(local));
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000228}
Jaeden Amero041039f2018-02-19 15:28:08 +0000229
Paul Bakker5121ce52009-01-03 21:22:43 +0000230/*
231 * SHA-1 process buffer
232 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233int mbedtls_sha1_update(mbedtls_sha1_context *ctx,
234 const unsigned char *input,
235 size_t ilen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000236{
Janos Follath24eed8d2019-11-22 13:21:35 +0000237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000238 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000239 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (ilen == 0) {
242 return 0;
243 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245 left = ctx->total[0] & 0x3F;
246 fill = 64 - left;
247
Paul Bakker5c2364c2012-10-01 14:41:15 +0000248 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 ctx->total[0] &= 0xFFFFFFFF;
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (ctx->total[0] < (uint32_t) ilen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 ctx->total[1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (left && ilen >= fill) {
256 memcpy((void *) (ctx->buffer + left), input, fill);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
259 return ret;
260 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100261
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 input += fill;
263 ilen -= fill;
264 left = 0;
265 }
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 while (ilen >= 64) {
268 if ((ret = mbedtls_internal_sha1_process(ctx, input)) != 0) {
269 return ret;
270 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100271
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 input += 64;
273 ilen -= 64;
274 }
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (ilen > 0) {
277 memcpy((void *) (ctx->buffer + left), input, ilen);
278 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000281}
282
Paul Bakker5121ce52009-01-03 21:22:43 +0000283/*
284 * SHA-1 final digest
285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286int mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
287 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000288{
Janos Follath24eed8d2019-11-22 13:21:35 +0000289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200290 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000291 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200293 /*
294 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
295 */
296 used = ctx->total[0] & 0x3F;
297
298 ctx->buffer[used++] = 0x80;
299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (used <= 56) {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200301 /* Enough room for padding + length in current block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 memset(ctx->buffer + used, 0, 56 - used);
303 } else {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200304 /* We'll need an extra block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 memset(ctx->buffer + used, 0, 64 - used);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100308 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 }
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 memset(ctx->buffer, 0, 56);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200312 }
313
314 /*
315 * Add message length
316 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 high = (ctx->total[0] >> 29)
318 | (ctx->total[1] << 3);
319 low = (ctx->total[0] << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
322 MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100325 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200328 /*
329 * Output final state
330 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
332 MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
333 MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
334 MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
335 MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100336
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100337 ret = 0;
338
339exit:
340 mbedtls_sha1_free(ctx);
341 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000342}
343
344/*
345 * output = SHA-1( input buffer )
346 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347int mbedtls_sha1(const unsigned char *input,
348 size_t ilen,
349 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000350{
Janos Follath24eed8d2019-11-22 13:21:35 +0000351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 mbedtls_sha1_init(&ctx);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if ((ret = mbedtls_sha1_starts(&ctx)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100357 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if ((ret = mbedtls_sha1_update(&ctx, input, ilen)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100361 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if ((ret = mbedtls_sha1_finish(&ctx, output)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100367
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100368exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 mbedtls_sha1_free(&ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000371}
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000374/*
375 * FIPS-180-1 test vectors
376 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000377static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000378{
379 { "abc" },
380 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
381 { "" }
382};
383
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100384static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000385{
386 3, 56, 1000
387};
388
389static const unsigned char sha1_test_sum[3][20] =
390{
391 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
392 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
393 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
394 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
395 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
396 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
397};
398
399/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 * Checkup routine
401 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402int mbedtls_sha1_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +0000403{
Paul Bakker5b4af392014-06-26 12:09:34 +0200404 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 unsigned char buf[1024];
406 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 mbedtls_sha1_init(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
412 * SHA-1
413 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 for (i = 0; i < 3; i++) {
415 if (verbose != 0) {
416 mbedtls_printf(" SHA-1 test #%d: ", i + 1);
417 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if ((ret = mbedtls_sha1_starts(&ctx)) != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100420 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (i == 2) {
424 memset(buf, 'a', buflen = 1000);
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 for (j = 0; j < 1000; j++) {
427 ret = mbedtls_sha1_update(&ctx, buf, buflen);
428 if (ret != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100429 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 }
431 }
432 } else {
433 ret = mbedtls_sha1_update(&ctx, sha1_test_buf[i],
434 sha1_test_buflen[i]);
435 if (ret != 0) {
436 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100437 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100439
440 if ((ret = mbedtls_sha1_finish(&ctx, sha1sum)) != 0) {
441 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100442 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (memcmp(sha1sum, sha1_test_sum[i], 20) != 0) {
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100445 ret = 1;
446 goto fail;
447 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (verbose != 0) {
450 mbedtls_printf("passed\n");
451 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 }
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (verbose != 0) {
455 mbedtls_printf("\n");
456 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100458 goto exit;
459
460fail:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (verbose != 0) {
462 mbedtls_printf("failed\n");
463 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100464
Paul Bakker5b4af392014-06-26 12:09:34 +0200465exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 mbedtls_sha1_free(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000469}
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473#endif /* MBEDTLS_SHA1_C */