blob: dfbe481f39ac4b52bf131c9c104813f259129510 [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
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020025#if !defined(MBEDTLS_SHA1_ALT)
26
Gilles Peskine449bd832023-01-11 14:50:10 +010027void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +020028{
Gilles Peskine449bd832023-01-11 14:50:10 +010029 memset(ctx, 0, sizeof(mbedtls_sha1_context));
Paul Bakker5b4af392014-06-26 12:09:34 +020030}
31
Gilles Peskine449bd832023-01-11 14:50:10 +010032void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +020033{
Gilles Peskine449bd832023-01-11 14:50:10 +010034 if (ctx == NULL) {
Paul Bakker5b4af392014-06-26 12:09:34 +020035 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010036 }
Paul Bakker5b4af392014-06-26 12:09:34 +020037
Gilles Peskine449bd832023-01-11 14:50:10 +010038 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha1_context));
Paul Bakker5b4af392014-06-26 12:09:34 +020039}
40
Gilles Peskine449bd832023-01-11 14:50:10 +010041void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
42 const mbedtls_sha1_context *src)
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020043{
44 *dst = *src;
45}
46
Paul Bakker5121ce52009-01-03 21:22:43 +000047/*
48 * SHA-1 context setup
49 */
Gilles Peskine449bd832023-01-11 14:50:10 +010050int mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +000051{
52 ctx->total[0] = 0;
53 ctx->total[1] = 0;
54
55 ctx->state[0] = 0x67452301;
56 ctx->state[1] = 0xEFCDAB89;
57 ctx->state[2] = 0x98BADCFE;
58 ctx->state[3] = 0x10325476;
59 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010060
Gilles Peskine449bd832023-01-11 14:50:10 +010061 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000062}
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +010065int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
66 const unsigned char data[64])
Paul Bakker5121ce52009-01-03 21:22:43 +000067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 struct {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +020069 uint32_t temp, W[16], A, B, C, D, E;
70 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 local.W[0] = MBEDTLS_GET_UINT32_BE(data, 0);
73 local.W[1] = MBEDTLS_GET_UINT32_BE(data, 4);
74 local.W[2] = MBEDTLS_GET_UINT32_BE(data, 8);
75 local.W[3] = MBEDTLS_GET_UINT32_BE(data, 12);
76 local.W[4] = MBEDTLS_GET_UINT32_BE(data, 16);
77 local.W[5] = MBEDTLS_GET_UINT32_BE(data, 20);
78 local.W[6] = MBEDTLS_GET_UINT32_BE(data, 24);
79 local.W[7] = MBEDTLS_GET_UINT32_BE(data, 28);
80 local.W[8] = MBEDTLS_GET_UINT32_BE(data, 32);
81 local.W[9] = MBEDTLS_GET_UINT32_BE(data, 36);
82 local.W[10] = MBEDTLS_GET_UINT32_BE(data, 40);
83 local.W[11] = MBEDTLS_GET_UINT32_BE(data, 44);
84 local.W[12] = MBEDTLS_GET_UINT32_BE(data, 48);
85 local.W[13] = MBEDTLS_GET_UINT32_BE(data, 52);
86 local.W[14] = MBEDTLS_GET_UINT32_BE(data, 56);
87 local.W[15] = MBEDTLS_GET_UINT32_BE(data, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Gilles Peskine449bd832023-01-11 14:50:10 +010089#define S(x, n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Hanno Becker1eeca412018-10-15 12:01:35 +010091#define R(t) \
92 ( \
Gilles Peskine449bd832023-01-11 14:50:10 +010093 local.temp = local.W[((t) - 3) & 0x0F] ^ \
94 local.W[((t) - 8) & 0x0F] ^ \
95 local.W[((t) - 14) & 0x0F] ^ \
96 local.W[(t) & 0x0F], \
97 (local.W[(t) & 0x0F] = S(local.temp, 1)) \
Hanno Becker1eeca412018-10-15 12:01:35 +010098 )
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100#define P(a, b, c, d, e, x) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100101 do \
102 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 (e) += S((a), 5) + F((b), (c), (d)) + K + (x); \
104 (b) = S((b), 30); \
105 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200107 local.A = ctx->state[0];
108 local.B = ctx->state[1];
109 local.C = ctx->state[2];
110 local.D = ctx->state[3];
111 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000114#define K 0x5A827999
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 P(local.A, local.B, local.C, local.D, local.E, local.W[0]);
117 P(local.E, local.A, local.B, local.C, local.D, local.W[1]);
118 P(local.D, local.E, local.A, local.B, local.C, local.W[2]);
119 P(local.C, local.D, local.E, local.A, local.B, local.W[3]);
120 P(local.B, local.C, local.D, local.E, local.A, local.W[4]);
121 P(local.A, local.B, local.C, local.D, local.E, local.W[5]);
122 P(local.E, local.A, local.B, local.C, local.D, local.W[6]);
123 P(local.D, local.E, local.A, local.B, local.C, local.W[7]);
124 P(local.C, local.D, local.E, local.A, local.B, local.W[8]);
125 P(local.B, local.C, local.D, local.E, local.A, local.W[9]);
126 P(local.A, local.B, local.C, local.D, local.E, local.W[10]);
127 P(local.E, local.A, local.B, local.C, local.D, local.W[11]);
128 P(local.D, local.E, local.A, local.B, local.C, local.W[12]);
129 P(local.C, local.D, local.E, local.A, local.B, local.W[13]);
130 P(local.B, local.C, local.D, local.E, local.A, local.W[14]);
131 P(local.A, local.B, local.C, local.D, local.E, local.W[15]);
132 P(local.E, local.A, local.B, local.C, local.D, R(16));
133 P(local.D, local.E, local.A, local.B, local.C, R(17));
134 P(local.C, local.D, local.E, local.A, local.B, R(18));
135 P(local.B, local.C, local.D, local.E, local.A, R(19));
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137#undef K
138#undef F
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000141#define K 0x6ED9EBA1
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 P(local.A, local.B, local.C, local.D, local.E, R(20));
144 P(local.E, local.A, local.B, local.C, local.D, R(21));
145 P(local.D, local.E, local.A, local.B, local.C, R(22));
146 P(local.C, local.D, local.E, local.A, local.B, R(23));
147 P(local.B, local.C, local.D, local.E, local.A, R(24));
148 P(local.A, local.B, local.C, local.D, local.E, R(25));
149 P(local.E, local.A, local.B, local.C, local.D, R(26));
150 P(local.D, local.E, local.A, local.B, local.C, R(27));
151 P(local.C, local.D, local.E, local.A, local.B, R(28));
152 P(local.B, local.C, local.D, local.E, local.A, R(29));
153 P(local.A, local.B, local.C, local.D, local.E, R(30));
154 P(local.E, local.A, local.B, local.C, local.D, R(31));
155 P(local.D, local.E, local.A, local.B, local.C, R(32));
156 P(local.C, local.D, local.E, local.A, local.B, R(33));
157 P(local.B, local.C, local.D, local.E, local.A, R(34));
158 P(local.A, local.B, local.C, local.D, local.E, R(35));
159 P(local.E, local.A, local.B, local.C, local.D, R(36));
160 P(local.D, local.E, local.A, local.B, local.C, R(37));
161 P(local.C, local.D, local.E, local.A, local.B, R(38));
162 P(local.B, local.C, local.D, local.E, local.A, R(39));
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164#undef K
165#undef F
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167#define F(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000168#define K 0x8F1BBCDC
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 P(local.A, local.B, local.C, local.D, local.E, R(40));
171 P(local.E, local.A, local.B, local.C, local.D, R(41));
172 P(local.D, local.E, local.A, local.B, local.C, R(42));
173 P(local.C, local.D, local.E, local.A, local.B, R(43));
174 P(local.B, local.C, local.D, local.E, local.A, R(44));
175 P(local.A, local.B, local.C, local.D, local.E, R(45));
176 P(local.E, local.A, local.B, local.C, local.D, R(46));
177 P(local.D, local.E, local.A, local.B, local.C, R(47));
178 P(local.C, local.D, local.E, local.A, local.B, R(48));
179 P(local.B, local.C, local.D, local.E, local.A, R(49));
180 P(local.A, local.B, local.C, local.D, local.E, R(50));
181 P(local.E, local.A, local.B, local.C, local.D, R(51));
182 P(local.D, local.E, local.A, local.B, local.C, R(52));
183 P(local.C, local.D, local.E, local.A, local.B, R(53));
184 P(local.B, local.C, local.D, local.E, local.A, R(54));
185 P(local.A, local.B, local.C, local.D, local.E, R(55));
186 P(local.E, local.A, local.B, local.C, local.D, R(56));
187 P(local.D, local.E, local.A, local.B, local.C, R(57));
188 P(local.C, local.D, local.E, local.A, local.B, R(58));
189 P(local.B, local.C, local.D, local.E, local.A, R(59));
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191#undef K
192#undef F
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000195#define K 0xCA62C1D6
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 P(local.A, local.B, local.C, local.D, local.E, R(60));
198 P(local.E, local.A, local.B, local.C, local.D, R(61));
199 P(local.D, local.E, local.A, local.B, local.C, R(62));
200 P(local.C, local.D, local.E, local.A, local.B, R(63));
201 P(local.B, local.C, local.D, local.E, local.A, R(64));
202 P(local.A, local.B, local.C, local.D, local.E, R(65));
203 P(local.E, local.A, local.B, local.C, local.D, R(66));
204 P(local.D, local.E, local.A, local.B, local.C, R(67));
205 P(local.C, local.D, local.E, local.A, local.B, R(68));
206 P(local.B, local.C, local.D, local.E, local.A, R(69));
207 P(local.A, local.B, local.C, local.D, local.E, R(70));
208 P(local.E, local.A, local.B, local.C, local.D, R(71));
209 P(local.D, local.E, local.A, local.B, local.C, R(72));
210 P(local.C, local.D, local.E, local.A, local.B, R(73));
211 P(local.B, local.C, local.D, local.E, local.A, R(74));
212 P(local.A, local.B, local.C, local.D, local.E, R(75));
213 P(local.E, local.A, local.B, local.C, local.D, R(76));
214 P(local.D, local.E, local.A, local.B, local.C, R(77));
215 P(local.C, local.D, local.E, local.A, local.B, R(78));
216 P(local.B, local.C, local.D, local.E, local.A, R(79));
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218#undef K
219#undef F
220
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200221 ctx->state[0] += local.A;
222 ctx->state[1] += local.B;
223 ctx->state[2] += local.C;
224 ctx->state[3] += local.D;
225 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100226
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200227 /* Zeroise buffers and variables to clear sensitive data from memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_platform_zeroize(&local, sizeof(local));
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000231}
Jaeden Amero041039f2018-02-19 15:28:08 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235/*
236 * SHA-1 process buffer
237 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238int mbedtls_sha1_update(mbedtls_sha1_context *ctx,
239 const unsigned char *input,
240 size_t ilen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000241{
Janos Follath24eed8d2019-11-22 13:21:35 +0000242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000243 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000244 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if (ilen == 0) {
247 return 0;
248 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250 left = ctx->total[0] & 0x3F;
251 fill = 64 - left;
252
Paul Bakker5c2364c2012-10-01 14:41:15 +0000253 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 ctx->total[0] &= 0xFFFFFFFF;
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (ctx->total[0] < (uint32_t) ilen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 ctx->total[1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (left && ilen >= fill) {
261 memcpy((void *) (ctx->buffer + left), input, fill);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
264 return ret;
265 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100266
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 input += fill;
268 ilen -= fill;
269 left = 0;
270 }
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 while (ilen >= 64) {
273 if ((ret = mbedtls_internal_sha1_process(ctx, input)) != 0) {
274 return ret;
275 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100276
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 input += 64;
278 ilen -= 64;
279 }
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (ilen > 0) {
282 memcpy((void *) (ctx->buffer + left), input, ilen);
283 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000286}
287
Paul Bakker5121ce52009-01-03 21:22:43 +0000288/*
289 * SHA-1 final digest
290 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291int mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
292 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000293{
Janos Follath24eed8d2019-11-22 13:21:35 +0000294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200295 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000296 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200298 /*
299 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
300 */
301 used = ctx->total[0] & 0x3F;
302
303 ctx->buffer[used++] = 0x80;
304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (used <= 56) {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200306 /* Enough room for padding + length in current block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 memset(ctx->buffer + used, 0, 56 - used);
308 } else {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200309 /* We'll need an extra block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 memset(ctx->buffer + used, 0, 64 - used);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100313 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 }
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 memset(ctx->buffer, 0, 56);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200317 }
318
319 /*
320 * Add message length
321 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 high = (ctx->total[0] >> 29)
323 | (ctx->total[1] << 3);
324 low = (ctx->total[0] << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
327 MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100330 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200333 /*
334 * Output final state
335 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
337 MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
338 MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
339 MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
340 MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100341
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100342 ret = 0;
343
344exit:
345 mbedtls_sha1_free(ctx);
346 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000347}
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200350
Paul Bakker5121ce52009-01-03 21:22:43 +0000351/*
352 * output = SHA-1( input buffer )
353 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354int mbedtls_sha1(const unsigned char *input,
355 size_t ilen,
356 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000357{
Janos Follath24eed8d2019-11-22 13:21:35 +0000358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 mbedtls_sha1_init(&ctx);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if ((ret = mbedtls_sha1_starts(&ctx)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100364 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if ((ret = mbedtls_sha1_update(&ctx, input, ilen)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100368 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if ((ret = mbedtls_sha1_finish(&ctx, output)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100372 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100374
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100375exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_sha1_free(&ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000378}
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000381/*
382 * FIPS-180-1 test vectors
383 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000384static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000385{
386 { "abc" },
387 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
388 { "" }
389};
390
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100391static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000392{
393 3, 56, 1000
394};
395
396static const unsigned char sha1_test_sum[3][20] =
397{
398 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
399 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
400 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
401 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
402 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
403 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
404};
405
406/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 * Checkup routine
408 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409int mbedtls_sha1_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +0000410{
Paul Bakker5b4af392014-06-26 12:09:34 +0200411 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 unsigned char buf[1024];
413 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 mbedtls_sha1_init(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200417
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 /*
419 * SHA-1
420 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 for (i = 0; i < 3; i++) {
422 if (verbose != 0) {
423 mbedtls_printf(" SHA-1 test #%d: ", i + 1);
424 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if ((ret = mbedtls_sha1_starts(&ctx)) != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100427 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (i == 2) {
431 memset(buf, 'a', buflen = 1000);
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 for (j = 0; j < 1000; j++) {
434 ret = mbedtls_sha1_update(&ctx, buf, buflen);
435 if (ret != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100436 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 }
438 }
439 } else {
440 ret = mbedtls_sha1_update(&ctx, sha1_test_buf[i],
441 sha1_test_buflen[i]);
442 if (ret != 0) {
443 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100444 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100446
447 if ((ret = mbedtls_sha1_finish(&ctx, sha1sum)) != 0) {
448 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100449 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (memcmp(sha1sum, sha1_test_sum[i], 20) != 0) {
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100452 ret = 1;
453 goto fail;
454 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (verbose != 0) {
457 mbedtls_printf("passed\n");
458 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 }
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (verbose != 0) {
462 mbedtls_printf("\n");
463 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100465 goto exit;
466
467fail:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (verbose != 0) {
469 mbedtls_printf("failed\n");
470 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100471
Paul Bakker5b4af392014-06-26 12:09:34 +0200472exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 mbedtls_sha1_free(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#endif /* MBEDTLS_SHA1_C */