blob: c8d2852d45d3b673c15a260d758099ae04b5d1a4 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +010063int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
64 const unsigned char data[64])
Paul Bakker5121ce52009-01-03 21:22:43 +000065{
Gilles Peskine449bd832023-01-11 14:50:10 +010066 struct {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +020067 uint32_t temp, W[16], A, B, C, D, E;
68 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 local.W[0] = MBEDTLS_GET_UINT32_BE(data, 0);
71 local.W[1] = MBEDTLS_GET_UINT32_BE(data, 4);
72 local.W[2] = MBEDTLS_GET_UINT32_BE(data, 8);
73 local.W[3] = MBEDTLS_GET_UINT32_BE(data, 12);
74 local.W[4] = MBEDTLS_GET_UINT32_BE(data, 16);
75 local.W[5] = MBEDTLS_GET_UINT32_BE(data, 20);
76 local.W[6] = MBEDTLS_GET_UINT32_BE(data, 24);
77 local.W[7] = MBEDTLS_GET_UINT32_BE(data, 28);
78 local.W[8] = MBEDTLS_GET_UINT32_BE(data, 32);
79 local.W[9] = MBEDTLS_GET_UINT32_BE(data, 36);
80 local.W[10] = MBEDTLS_GET_UINT32_BE(data, 40);
81 local.W[11] = MBEDTLS_GET_UINT32_BE(data, 44);
82 local.W[12] = MBEDTLS_GET_UINT32_BE(data, 48);
83 local.W[13] = MBEDTLS_GET_UINT32_BE(data, 52);
84 local.W[14] = MBEDTLS_GET_UINT32_BE(data, 56);
85 local.W[15] = MBEDTLS_GET_UINT32_BE(data, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define S(x, n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Hanno Becker1eeca412018-10-15 12:01:35 +010089#define R(t) \
90 ( \
Gilles Peskine449bd832023-01-11 14:50:10 +010091 local.temp = local.W[((t) - 3) & 0x0F] ^ \
92 local.W[((t) - 8) & 0x0F] ^ \
93 local.W[((t) - 14) & 0x0F] ^ \
94 local.W[(t) & 0x0F], \
95 (local.W[(t) & 0x0F] = S(local.temp, 1)) \
Hanno Becker1eeca412018-10-15 12:01:35 +010096 )
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Gilles Peskine449bd832023-01-11 14:50:10 +010098#define P(a, b, c, d, e, x) \
Hanno Becker1eeca412018-10-15 12:01:35 +010099 do \
100 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 (e) += S((a), 5) + F((b), (c), (d)) + K + (x); \
102 (b) = S((b), 30); \
103 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200105 local.A = ctx->state[0];
106 local.B = ctx->state[1];
107 local.C = ctx->state[2];
108 local.D = ctx->state[3];
109 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000112#define K 0x5A827999
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 P(local.A, local.B, local.C, local.D, local.E, local.W[0]);
115 P(local.E, local.A, local.B, local.C, local.D, local.W[1]);
116 P(local.D, local.E, local.A, local.B, local.C, local.W[2]);
117 P(local.C, local.D, local.E, local.A, local.B, local.W[3]);
118 P(local.B, local.C, local.D, local.E, local.A, local.W[4]);
119 P(local.A, local.B, local.C, local.D, local.E, local.W[5]);
120 P(local.E, local.A, local.B, local.C, local.D, local.W[6]);
121 P(local.D, local.E, local.A, local.B, local.C, local.W[7]);
122 P(local.C, local.D, local.E, local.A, local.B, local.W[8]);
123 P(local.B, local.C, local.D, local.E, local.A, local.W[9]);
124 P(local.A, local.B, local.C, local.D, local.E, local.W[10]);
125 P(local.E, local.A, local.B, local.C, local.D, local.W[11]);
126 P(local.D, local.E, local.A, local.B, local.C, local.W[12]);
127 P(local.C, local.D, local.E, local.A, local.B, local.W[13]);
128 P(local.B, local.C, local.D, local.E, local.A, local.W[14]);
129 P(local.A, local.B, local.C, local.D, local.E, local.W[15]);
130 P(local.E, local.A, local.B, local.C, local.D, R(16));
131 P(local.D, local.E, local.A, local.B, local.C, R(17));
132 P(local.C, local.D, local.E, local.A, local.B, R(18));
133 P(local.B, local.C, local.D, local.E, local.A, R(19));
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135#undef K
136#undef F
137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000139#define K 0x6ED9EBA1
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 P(local.A, local.B, local.C, local.D, local.E, R(20));
142 P(local.E, local.A, local.B, local.C, local.D, R(21));
143 P(local.D, local.E, local.A, local.B, local.C, R(22));
144 P(local.C, local.D, local.E, local.A, local.B, R(23));
145 P(local.B, local.C, local.D, local.E, local.A, R(24));
146 P(local.A, local.B, local.C, local.D, local.E, R(25));
147 P(local.E, local.A, local.B, local.C, local.D, R(26));
148 P(local.D, local.E, local.A, local.B, local.C, R(27));
149 P(local.C, local.D, local.E, local.A, local.B, R(28));
150 P(local.B, local.C, local.D, local.E, local.A, R(29));
151 P(local.A, local.B, local.C, local.D, local.E, R(30));
152 P(local.E, local.A, local.B, local.C, local.D, R(31));
153 P(local.D, local.E, local.A, local.B, local.C, R(32));
154 P(local.C, local.D, local.E, local.A, local.B, R(33));
155 P(local.B, local.C, local.D, local.E, local.A, R(34));
156 P(local.A, local.B, local.C, local.D, local.E, R(35));
157 P(local.E, local.A, local.B, local.C, local.D, R(36));
158 P(local.D, local.E, local.A, local.B, local.C, R(37));
159 P(local.C, local.D, local.E, local.A, local.B, R(38));
160 P(local.B, local.C, local.D, local.E, local.A, R(39));
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
162#undef K
163#undef F
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165#define F(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000166#define K 0x8F1BBCDC
167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 P(local.A, local.B, local.C, local.D, local.E, R(40));
169 P(local.E, local.A, local.B, local.C, local.D, R(41));
170 P(local.D, local.E, local.A, local.B, local.C, R(42));
171 P(local.C, local.D, local.E, local.A, local.B, R(43));
172 P(local.B, local.C, local.D, local.E, local.A, R(44));
173 P(local.A, local.B, local.C, local.D, local.E, R(45));
174 P(local.E, local.A, local.B, local.C, local.D, R(46));
175 P(local.D, local.E, local.A, local.B, local.C, R(47));
176 P(local.C, local.D, local.E, local.A, local.B, R(48));
177 P(local.B, local.C, local.D, local.E, local.A, R(49));
178 P(local.A, local.B, local.C, local.D, local.E, R(50));
179 P(local.E, local.A, local.B, local.C, local.D, R(51));
180 P(local.D, local.E, local.A, local.B, local.C, R(52));
181 P(local.C, local.D, local.E, local.A, local.B, R(53));
182 P(local.B, local.C, local.D, local.E, local.A, R(54));
183 P(local.A, local.B, local.C, local.D, local.E, R(55));
184 P(local.E, local.A, local.B, local.C, local.D, R(56));
185 P(local.D, local.E, local.A, local.B, local.C, R(57));
186 P(local.C, local.D, local.E, local.A, local.B, R(58));
187 P(local.B, local.C, local.D, local.E, local.A, R(59));
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189#undef K
190#undef F
191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000193#define K 0xCA62C1D6
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 P(local.A, local.B, local.C, local.D, local.E, R(60));
196 P(local.E, local.A, local.B, local.C, local.D, R(61));
197 P(local.D, local.E, local.A, local.B, local.C, R(62));
198 P(local.C, local.D, local.E, local.A, local.B, R(63));
199 P(local.B, local.C, local.D, local.E, local.A, R(64));
200 P(local.A, local.B, local.C, local.D, local.E, R(65));
201 P(local.E, local.A, local.B, local.C, local.D, R(66));
202 P(local.D, local.E, local.A, local.B, local.C, R(67));
203 P(local.C, local.D, local.E, local.A, local.B, R(68));
204 P(local.B, local.C, local.D, local.E, local.A, R(69));
205 P(local.A, local.B, local.C, local.D, local.E, R(70));
206 P(local.E, local.A, local.B, local.C, local.D, R(71));
207 P(local.D, local.E, local.A, local.B, local.C, R(72));
208 P(local.C, local.D, local.E, local.A, local.B, R(73));
209 P(local.B, local.C, local.D, local.E, local.A, R(74));
210 P(local.A, local.B, local.C, local.D, local.E, R(75));
211 P(local.E, local.A, local.B, local.C, local.D, R(76));
212 P(local.D, local.E, local.A, local.B, local.C, R(77));
213 P(local.C, local.D, local.E, local.A, local.B, R(78));
214 P(local.B, local.C, local.D, local.E, local.A, R(79));
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
216#undef K
217#undef F
218
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200219 ctx->state[0] += local.A;
220 ctx->state[1] += local.B;
221 ctx->state[2] += local.C;
222 ctx->state[3] += local.D;
223 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100224
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200225 /* Zeroise buffers and variables to clear sensitive data from memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_platform_zeroize(&local, sizeof(local));
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000229}
Jaeden Amero041039f2018-02-19 15:28:08 +0000230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233/*
234 * SHA-1 process buffer
235 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236int mbedtls_sha1_update(mbedtls_sha1_context *ctx,
237 const unsigned char *input,
238 size_t ilen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000239{
Janos Follath24eed8d2019-11-22 13:21:35 +0000240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000241 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000242 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (ilen == 0) {
245 return 0;
246 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248 left = ctx->total[0] & 0x3F;
249 fill = 64 - left;
250
Paul Bakker5c2364c2012-10-01 14:41:15 +0000251 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 ctx->total[0] &= 0xFFFFFFFF;
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (ctx->total[0] < (uint32_t) ilen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 ctx->total[1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (left && ilen >= fill) {
259 memcpy((void *) (ctx->buffer + left), input, fill);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
262 return ret;
263 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100264
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 input += fill;
266 ilen -= fill;
267 left = 0;
268 }
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 while (ilen >= 64) {
271 if ((ret = mbedtls_internal_sha1_process(ctx, input)) != 0) {
272 return ret;
273 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100274
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 input += 64;
276 ilen -= 64;
277 }
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (ilen > 0) {
280 memcpy((void *) (ctx->buffer + left), input, ilen);
281 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000284}
285
Paul Bakker5121ce52009-01-03 21:22:43 +0000286/*
287 * SHA-1 final digest
288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289int mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
290 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000291{
Janos Follath24eed8d2019-11-22 13:21:35 +0000292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200293 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000294 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200296 /*
297 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
298 */
299 used = ctx->total[0] & 0x3F;
300
301 ctx->buffer[used++] = 0x80;
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (used <= 56) {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200304 /* Enough room for padding + length in current block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 memset(ctx->buffer + used, 0, 56 - used);
306 } else {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200307 /* We'll need an extra block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 memset(ctx->buffer + used, 0, 64 - used);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100311 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 memset(ctx->buffer, 0, 56);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200315 }
316
317 /*
318 * Add message length
319 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 high = (ctx->total[0] >> 29)
321 | (ctx->total[1] << 3);
322 low = (ctx->total[0] << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
325 MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100328 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200331 /*
332 * Output final state
333 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
335 MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
336 MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
337 MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
338 MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100339
Dave Rodgmanaafd1e02023-09-11 12:59:36 +0100340 ret = 0;
341
342exit:
343 mbedtls_sha1_free(ctx);
344 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000345}
346
347/*
348 * output = SHA-1( input buffer )
349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350int mbedtls_sha1(const unsigned char *input,
351 size_t ilen,
352 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000353{
Janos Follath24eed8d2019-11-22 13:21:35 +0000354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 mbedtls_sha1_init(&ctx);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if ((ret = mbedtls_sha1_starts(&ctx)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100360 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if ((ret = mbedtls_sha1_update(&ctx, input, ilen)) != 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_finish(&ctx, output)) != 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
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100371exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 mbedtls_sha1_free(&ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000374}
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000377/*
378 * FIPS-180-1 test vectors
379 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000380static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000381{
382 { "abc" },
383 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
384 { "" }
385};
386
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100387static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000388{
389 3, 56, 1000
390};
391
392static const unsigned char sha1_test_sum[3][20] =
393{
394 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
395 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
396 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
397 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
398 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
399 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
400};
401
402/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 * Checkup routine
404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405int mbedtls_sha1_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +0000406{
Paul Bakker5b4af392014-06-26 12:09:34 +0200407 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 unsigned char buf[1024];
409 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 mbedtls_sha1_init(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200413
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 /*
415 * SHA-1
416 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 for (i = 0; i < 3; i++) {
418 if (verbose != 0) {
419 mbedtls_printf(" SHA-1 test #%d: ", i + 1);
420 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if ((ret = mbedtls_sha1_starts(&ctx)) != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100423 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (i == 2) {
427 memset(buf, 'a', buflen = 1000);
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 for (j = 0; j < 1000; j++) {
430 ret = mbedtls_sha1_update(&ctx, buf, buflen);
431 if (ret != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100432 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 }
434 }
435 } else {
436 ret = mbedtls_sha1_update(&ctx, sha1_test_buf[i],
437 sha1_test_buflen[i]);
438 if (ret != 0) {
439 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100440 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100442
443 if ((ret = mbedtls_sha1_finish(&ctx, sha1sum)) != 0) {
444 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100445 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (memcmp(sha1sum, sha1_test_sum[i], 20) != 0) {
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100448 ret = 1;
449 goto fail;
450 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (verbose != 0) {
453 mbedtls_printf("passed\n");
454 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 }
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (verbose != 0) {
458 mbedtls_printf("\n");
459 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100461 goto exit;
462
463fail:
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (verbose != 0) {
465 mbedtls_printf("failed\n");
466 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100467
Paul Bakker5b4af392014-06-26 12:09:34 +0200468exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 mbedtls_sha1_free(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000472}
473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#endif /* MBEDTLS_SHA1_C */