blob: 9dd958ef4c4f7880249c298f4cb42f6cffc344c8 [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 Rodgman7ff79652023-11-03 12:04:52 +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
Hanno Beckerb3c70232018-12-20 10:18:05 +000025#define SHA1_VALIDATE_RET(cond) \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010026 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA)
Hanno Beckerb3c70232018-12-20 10:18:05 +000027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010028#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE(cond)
Hanno Beckerb3c70232018-12-20 10:18:05 +000029
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020030#if !defined(MBEDTLS_SHA1_ALT)
31
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010032void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +020033{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010034 SHA1_VALIDATE(ctx != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036 memset(ctx, 0, sizeof(mbedtls_sha1_context));
Paul Bakker5b4af392014-06-26 12:09:34 +020037}
38
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +020040{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 if (ctx == NULL) {
Paul Bakker5b4af392014-06-26 12:09:34 +020042 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043 }
Paul Bakker5b4af392014-06-26 12:09:34 +020044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha1_context));
Paul Bakker5b4af392014-06-26 12:09:34 +020046}
47
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
49 const mbedtls_sha1_context *src)
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020050{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 SHA1_VALIDATE(dst != NULL);
52 SHA1_VALIDATE(src != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000053
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020054 *dst = *src;
55}
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057/*
58 * SHA-1 context setup
59 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +000061{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 SHA1_VALIDATE_RET(ctx != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000063
Paul Bakker5121ce52009-01-03 21:22:43 +000064 ctx->total[0] = 0;
65 ctx->total[1] = 0;
66
67 ctx->state[0] = 0x67452301;
68 ctx->state[1] = 0xEFCDAB89;
69 ctx->state[2] = 0x98BADCFE;
70 ctx->state[3] = 0x10325476;
71 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000074}
75
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020076#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077void mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020078{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 mbedtls_sha1_starts_ret(ctx);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020080}
81#endif
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
85 const unsigned char data[64])
Paul Bakker5121ce52009-01-03 21:22:43 +000086{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 struct {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +020088 uint32_t temp, W[16], A, B, C, D, E;
89 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 SHA1_VALIDATE_RET(ctx != NULL);
92 SHA1_VALIDATE_RET((const unsigned char *) data != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 local.W[0] = MBEDTLS_GET_UINT32_BE(data, 0);
95 local.W[1] = MBEDTLS_GET_UINT32_BE(data, 4);
96 local.W[2] = MBEDTLS_GET_UINT32_BE(data, 8);
97 local.W[3] = MBEDTLS_GET_UINT32_BE(data, 12);
98 local.W[4] = MBEDTLS_GET_UINT32_BE(data, 16);
99 local.W[5] = MBEDTLS_GET_UINT32_BE(data, 20);
100 local.W[6] = MBEDTLS_GET_UINT32_BE(data, 24);
101 local.W[7] = MBEDTLS_GET_UINT32_BE(data, 28);
102 local.W[8] = MBEDTLS_GET_UINT32_BE(data, 32);
103 local.W[9] = MBEDTLS_GET_UINT32_BE(data, 36);
104 local.W[10] = MBEDTLS_GET_UINT32_BE(data, 40);
105 local.W[11] = MBEDTLS_GET_UINT32_BE(data, 44);
106 local.W[12] = MBEDTLS_GET_UINT32_BE(data, 48);
107 local.W[13] = MBEDTLS_GET_UINT32_BE(data, 52);
108 local.W[14] = MBEDTLS_GET_UINT32_BE(data, 56);
109 local.W[15] = MBEDTLS_GET_UINT32_BE(data, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111#define S(x, n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Hanno Becker1eeca412018-10-15 12:01:35 +0100113#define R(t) \
114 ( \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 local.temp = local.W[((t) - 3) & 0x0F] ^ \
116 local.W[((t) - 8) & 0x0F] ^ \
117 local.W[((t) - 14) & 0x0F] ^ \
118 local.W[(t) & 0x0F], \
119 (local.W[(t) & 0x0F] = S(local.temp, 1)) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100120 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122#define P(a, b, c, d, e, x) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100123 do \
124 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 (e) += S((a), 5) + F((b), (c), (d)) + K + (x); \
126 (b) = S((b), 30); \
127 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200129 local.A = ctx->state[0];
130 local.B = ctx->state[1];
131 local.C = ctx->state[2];
132 local.D = ctx->state[3];
133 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000136#define K 0x5A827999
137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 P(local.A, local.B, local.C, local.D, local.E, local.W[0]);
139 P(local.E, local.A, local.B, local.C, local.D, local.W[1]);
140 P(local.D, local.E, local.A, local.B, local.C, local.W[2]);
141 P(local.C, local.D, local.E, local.A, local.B, local.W[3]);
142 P(local.B, local.C, local.D, local.E, local.A, local.W[4]);
143 P(local.A, local.B, local.C, local.D, local.E, local.W[5]);
144 P(local.E, local.A, local.B, local.C, local.D, local.W[6]);
145 P(local.D, local.E, local.A, local.B, local.C, local.W[7]);
146 P(local.C, local.D, local.E, local.A, local.B, local.W[8]);
147 P(local.B, local.C, local.D, local.E, local.A, local.W[9]);
148 P(local.A, local.B, local.C, local.D, local.E, local.W[10]);
149 P(local.E, local.A, local.B, local.C, local.D, local.W[11]);
150 P(local.D, local.E, local.A, local.B, local.C, local.W[12]);
151 P(local.C, local.D, local.E, local.A, local.B, local.W[13]);
152 P(local.B, local.C, local.D, local.E, local.A, local.W[14]);
153 P(local.A, local.B, local.C, local.D, local.E, local.W[15]);
154 P(local.E, local.A, local.B, local.C, local.D, R(16));
155 P(local.D, local.E, local.A, local.B, local.C, R(17));
156 P(local.C, local.D, local.E, local.A, local.B, R(18));
157 P(local.B, local.C, local.D, local.E, local.A, R(19));
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159#undef K
160#undef F
161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000163#define K 0x6ED9EBA1
164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 P(local.A, local.B, local.C, local.D, local.E, R(20));
166 P(local.E, local.A, local.B, local.C, local.D, R(21));
167 P(local.D, local.E, local.A, local.B, local.C, R(22));
168 P(local.C, local.D, local.E, local.A, local.B, R(23));
169 P(local.B, local.C, local.D, local.E, local.A, R(24));
170 P(local.A, local.B, local.C, local.D, local.E, R(25));
171 P(local.E, local.A, local.B, local.C, local.D, R(26));
172 P(local.D, local.E, local.A, local.B, local.C, R(27));
173 P(local.C, local.D, local.E, local.A, local.B, R(28));
174 P(local.B, local.C, local.D, local.E, local.A, R(29));
175 P(local.A, local.B, local.C, local.D, local.E, R(30));
176 P(local.E, local.A, local.B, local.C, local.D, R(31));
177 P(local.D, local.E, local.A, local.B, local.C, R(32));
178 P(local.C, local.D, local.E, local.A, local.B, R(33));
179 P(local.B, local.C, local.D, local.E, local.A, R(34));
180 P(local.A, local.B, local.C, local.D, local.E, R(35));
181 P(local.E, local.A, local.B, local.C, local.D, R(36));
182 P(local.D, local.E, local.A, local.B, local.C, R(37));
183 P(local.C, local.D, local.E, local.A, local.B, R(38));
184 P(local.B, local.C, local.D, local.E, local.A, R(39));
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186#undef K
187#undef F
188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189#define F(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000190#define K 0x8F1BBCDC
191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 P(local.A, local.B, local.C, local.D, local.E, R(40));
193 P(local.E, local.A, local.B, local.C, local.D, R(41));
194 P(local.D, local.E, local.A, local.B, local.C, R(42));
195 P(local.C, local.D, local.E, local.A, local.B, R(43));
196 P(local.B, local.C, local.D, local.E, local.A, R(44));
197 P(local.A, local.B, local.C, local.D, local.E, R(45));
198 P(local.E, local.A, local.B, local.C, local.D, R(46));
199 P(local.D, local.E, local.A, local.B, local.C, R(47));
200 P(local.C, local.D, local.E, local.A, local.B, R(48));
201 P(local.B, local.C, local.D, local.E, local.A, R(49));
202 P(local.A, local.B, local.C, local.D, local.E, R(50));
203 P(local.E, local.A, local.B, local.C, local.D, R(51));
204 P(local.D, local.E, local.A, local.B, local.C, R(52));
205 P(local.C, local.D, local.E, local.A, local.B, R(53));
206 P(local.B, local.C, local.D, local.E, local.A, R(54));
207 P(local.A, local.B, local.C, local.D, local.E, R(55));
208 P(local.E, local.A, local.B, local.C, local.D, R(56));
209 P(local.D, local.E, local.A, local.B, local.C, R(57));
210 P(local.C, local.D, local.E, local.A, local.B, R(58));
211 P(local.B, local.C, local.D, local.E, local.A, R(59));
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213#undef K
214#undef F
215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000217#define K 0xCA62C1D6
218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 P(local.A, local.B, local.C, local.D, local.E, R(60));
220 P(local.E, local.A, local.B, local.C, local.D, R(61));
221 P(local.D, local.E, local.A, local.B, local.C, R(62));
222 P(local.C, local.D, local.E, local.A, local.B, R(63));
223 P(local.B, local.C, local.D, local.E, local.A, R(64));
224 P(local.A, local.B, local.C, local.D, local.E, R(65));
225 P(local.E, local.A, local.B, local.C, local.D, R(66));
226 P(local.D, local.E, local.A, local.B, local.C, R(67));
227 P(local.C, local.D, local.E, local.A, local.B, R(68));
228 P(local.B, local.C, local.D, local.E, local.A, R(69));
229 P(local.A, local.B, local.C, local.D, local.E, R(70));
230 P(local.E, local.A, local.B, local.C, local.D, R(71));
231 P(local.D, local.E, local.A, local.B, local.C, R(72));
232 P(local.C, local.D, local.E, local.A, local.B, R(73));
233 P(local.B, local.C, local.D, local.E, local.A, R(74));
234 P(local.A, local.B, local.C, local.D, local.E, R(75));
235 P(local.E, local.A, local.B, local.C, local.D, R(76));
236 P(local.D, local.E, local.A, local.B, local.C, R(77));
237 P(local.C, local.D, local.E, local.A, local.B, R(78));
238 P(local.B, local.C, local.D, local.E, local.A, R(79));
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240#undef K
241#undef F
242
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200243 ctx->state[0] += local.A;
244 ctx->state[1] += local.B;
245 ctx->state[2] += local.C;
246 ctx->state[3] += local.D;
247 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100248
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200249 /* Zeroise buffers and variables to clear sensitive data from memory. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 mbedtls_platform_zeroize(&local, sizeof(local));
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000253}
Jaeden Amero041039f2018-02-19 15:28:08 +0000254
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200255#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256void mbedtls_sha1_process(mbedtls_sha1_context *ctx,
257 const unsigned char data[64])
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200258{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 mbedtls_internal_sha1_process(ctx, data);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200260}
261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
264/*
265 * SHA-1 process buffer
266 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx,
268 const unsigned char *input,
269 size_t ilen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000270{
Janos Follath24eed8d2019-11-22 13:21:35 +0000271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000272 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000273 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 SHA1_VALIDATE_RET(ctx != NULL);
276 SHA1_VALIDATE_RET(ilen == 0 || input != NULL);
Hanno Beckerb3906d82018-12-18 11:35:00 +0000277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 if (ilen == 0) {
279 return 0;
280 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282 left = ctx->total[0] & 0x3F;
283 fill = 64 - left;
284
Paul Bakker5c2364c2012-10-01 14:41:15 +0000285 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 ctx->total[0] &= 0xFFFFFFFF;
287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 if (ctx->total[0] < (uint32_t) ilen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 ctx->total[1]++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 if (left && ilen >= fill) {
293 memcpy((void *) (ctx->buffer + left), input, fill);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
296 return ret;
297 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100298
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 input += fill;
300 ilen -= fill;
301 left = 0;
302 }
303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 while (ilen >= 64) {
305 if ((ret = mbedtls_internal_sha1_process(ctx, input)) != 0) {
306 return ret;
307 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100308
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 input += 64;
310 ilen -= 64;
311 }
312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 if (ilen > 0) {
314 memcpy((void *) (ctx->buffer + left), input, ilen);
315 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000318}
319
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200320#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321void mbedtls_sha1_update(mbedtls_sha1_context *ctx,
322 const unsigned char *input,
323 size_t ilen)
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200324{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 mbedtls_sha1_update_ret(ctx, input, ilen);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200326}
327#endif
328
Paul Bakker5121ce52009-01-03 21:22:43 +0000329/*
330 * SHA-1 final digest
331 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx,
333 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000334{
Janos Follath24eed8d2019-11-22 13:21:35 +0000335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200336 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000337 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 SHA1_VALIDATE_RET(ctx != NULL);
340 SHA1_VALIDATE_RET((unsigned char *) output != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000341
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200342 /*
343 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
344 */
345 used = ctx->total[0] & 0x3F;
346
347 ctx->buffer[used++] = 0x80;
348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 if (used <= 56) {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200350 /* Enough room for padding + length in current block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 memset(ctx->buffer + used, 0, 56 - used);
352 } else {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200353 /* We'll need an extra block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 memset(ctx->buffer + used, 0, 64 - used);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
357 return ret;
358 }
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 memset(ctx->buffer, 0, 56);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200361 }
362
363 /*
364 * Add message length
365 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 high = (ctx->total[0] >> 29)
367 | (ctx->total[1] << 3);
368 low = (ctx->total[0] << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
371 MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
374 return ret;
375 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200377 /*
378 * Output final state
379 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
381 MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
382 MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
383 MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
384 MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000387}
388
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200389#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390void mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
391 unsigned char output[20])
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200392{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 mbedtls_sha1_finish_ret(ctx, output);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200394}
395#endif
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200398
Paul Bakker5121ce52009-01-03 21:22:43 +0000399/*
400 * output = SHA-1( input buffer )
401 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402int mbedtls_sha1_ret(const unsigned char *input,
403 size_t ilen,
404 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000405{
Janos Follath24eed8d2019-11-22 13:21:35 +0000406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 SHA1_VALIDATE_RET(ilen == 0 || input != NULL);
410 SHA1_VALIDATE_RET((unsigned char *) output != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412 mbedtls_sha1_init(&ctx);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 if ((ret = mbedtls_sha1_starts_ret(&ctx)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100415 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 if ((ret = mbedtls_sha1_update_ret(&ctx, input, ilen)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100419 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 if ((ret = mbedtls_sha1_finish_ret(&ctx, output)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100423 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100425
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100426exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 mbedtls_sha1_free(&ctx);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000430}
431
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200432#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433void mbedtls_sha1(const unsigned char *input,
434 size_t ilen,
435 unsigned char output[20])
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200436{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 mbedtls_sha1_ret(input, ilen, output);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200438}
439#endif
440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000442/*
443 * FIPS-180-1 test vectors
444 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000445static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000446{
447 { "abc" },
448 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
449 { "" }
450};
451
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100452static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000453{
454 3, 56, 1000
455};
456
457static const unsigned char sha1_test_sum[3][20] =
458{
459 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
460 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
461 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
462 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
463 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
464 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
465};
466
467/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 * Checkup routine
469 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470int mbedtls_sha1_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +0000471{
Paul Bakker5b4af392014-06-26 12:09:34 +0200472 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 unsigned char buf[1024];
474 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 mbedtls_sha1_init(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200478
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 /*
480 * SHA-1
481 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 for (i = 0; i < 3; i++) {
483 if (verbose != 0) {
484 mbedtls_printf(" SHA-1 test #%d: ", i + 1);
485 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 if ((ret = mbedtls_sha1_starts_ret(&ctx)) != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100488 goto fail;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 if (i == 2) {
492 memset(buf, 'a', buflen = 1000);
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 for (j = 0; j < 1000; j++) {
495 ret = mbedtls_sha1_update_ret(&ctx, buf, buflen);
496 if (ret != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100497 goto fail;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 }
499 }
500 } else {
501 ret = mbedtls_sha1_update_ret(&ctx, sha1_test_buf[i],
502 sha1_test_buflen[i]);
503 if (ret != 0) {
504 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100505 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507
508 if ((ret = mbedtls_sha1_finish_ret(&ctx, sha1sum)) != 0) {
509 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100510 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 if (memcmp(sha1sum, sha1_test_sum[i], 20) != 0) {
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100513 ret = 1;
514 goto fail;
515 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100517 if (verbose != 0) {
518 mbedtls_printf("passed\n");
519 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 }
521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522 if (verbose != 0) {
523 mbedtls_printf("\n");
524 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100526 goto exit;
527
528fail:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 if (verbose != 0) {
530 mbedtls_printf("failed\n");
531 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100532
Paul Bakker5b4af392014-06-26 12:09:34 +0200533exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 mbedtls_sha1_free(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000537}
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541#endif /* MBEDTLS_SHA1_C */