blob: 6da641427c7029136247eee7e88001eb0cc21fbe [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
20 * The SHA-1 standard was published by NIST in 1993.
21 *
22 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
23 */
24
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/sha1.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010036
Hanno Beckerb3c70232018-12-20 10:18:05 +000037#define SHA1_VALIDATE_RET(cond) \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010038 MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA)
Hanno Beckerb3c70232018-12-20 10:18:05 +000039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040#define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE(cond)
Hanno Beckerb3c70232018-12-20 10:18:05 +000041
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020042#if !defined(MBEDTLS_SHA1_ALT)
43
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +020045{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046 SHA1_VALIDATE(ctx != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048 memset(ctx, 0, sizeof(mbedtls_sha1_context));
Paul Bakker5b4af392014-06-26 12:09:34 +020049}
50
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +020052{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 if (ctx == NULL) {
Paul Bakker5b4af392014-06-26 12:09:34 +020054 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 }
Paul Bakker5b4af392014-06-26 12:09:34 +020056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha1_context));
Paul Bakker5b4af392014-06-26 12:09:34 +020058}
59
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
61 const mbedtls_sha1_context *src)
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020062{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063 SHA1_VALIDATE(dst != NULL);
64 SHA1_VALIDATE(src != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000065
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020066 *dst = *src;
67}
68
Paul Bakker5121ce52009-01-03 21:22:43 +000069/*
70 * SHA-1 context setup
71 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +000073{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 SHA1_VALIDATE_RET(ctx != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000075
Paul Bakker5121ce52009-01-03 21:22:43 +000076 ctx->total[0] = 0;
77 ctx->total[1] = 0;
78
79 ctx->state[0] = 0x67452301;
80 ctx->state[1] = 0xEFCDAB89;
81 ctx->state[2] = 0x98BADCFE;
82 ctx->state[3] = 0x10325476;
83 ctx->state[4] = 0xC3D2E1F0;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000086}
87
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020088#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089void mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020090{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 mbedtls_sha1_starts_ret(ctx);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020092}
93#endif
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
97 const unsigned char data[64])
Paul Bakker5121ce52009-01-03 21:22:43 +000098{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 struct {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200100 uint32_t temp, W[16], A, B, C, D, E;
101 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 SHA1_VALIDATE_RET(ctx != NULL);
104 SHA1_VALIDATE_RET((const unsigned char *) data != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 local.W[0] = MBEDTLS_GET_UINT32_BE(data, 0);
107 local.W[1] = MBEDTLS_GET_UINT32_BE(data, 4);
108 local.W[2] = MBEDTLS_GET_UINT32_BE(data, 8);
109 local.W[3] = MBEDTLS_GET_UINT32_BE(data, 12);
110 local.W[4] = MBEDTLS_GET_UINT32_BE(data, 16);
111 local.W[5] = MBEDTLS_GET_UINT32_BE(data, 20);
112 local.W[6] = MBEDTLS_GET_UINT32_BE(data, 24);
113 local.W[7] = MBEDTLS_GET_UINT32_BE(data, 28);
114 local.W[8] = MBEDTLS_GET_UINT32_BE(data, 32);
115 local.W[9] = MBEDTLS_GET_UINT32_BE(data, 36);
116 local.W[10] = MBEDTLS_GET_UINT32_BE(data, 40);
117 local.W[11] = MBEDTLS_GET_UINT32_BE(data, 44);
118 local.W[12] = MBEDTLS_GET_UINT32_BE(data, 48);
119 local.W[13] = MBEDTLS_GET_UINT32_BE(data, 52);
120 local.W[14] = MBEDTLS_GET_UINT32_BE(data, 56);
121 local.W[15] = MBEDTLS_GET_UINT32_BE(data, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123#define S(x, n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Hanno Becker1eeca412018-10-15 12:01:35 +0100125#define R(t) \
126 ( \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 local.temp = local.W[((t) - 3) & 0x0F] ^ \
128 local.W[((t) - 8) & 0x0F] ^ \
129 local.W[((t) - 14) & 0x0F] ^ \
130 local.W[(t) & 0x0F], \
131 (local.W[(t) & 0x0F] = S(local.temp, 1)) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100132 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134#define P(a, b, c, d, e, x) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100135 do \
136 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 (e) += S((a), 5) + F((b), (c), (d)) + K + (x); \
138 (b) = S((b), 30); \
139 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200141 local.A = ctx->state[0];
142 local.B = ctx->state[1];
143 local.C = ctx->state[2];
144 local.D = ctx->state[3];
145 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000148#define K 0x5A827999
149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 P(local.A, local.B, local.C, local.D, local.E, local.W[0]);
151 P(local.E, local.A, local.B, local.C, local.D, local.W[1]);
152 P(local.D, local.E, local.A, local.B, local.C, local.W[2]);
153 P(local.C, local.D, local.E, local.A, local.B, local.W[3]);
154 P(local.B, local.C, local.D, local.E, local.A, local.W[4]);
155 P(local.A, local.B, local.C, local.D, local.E, local.W[5]);
156 P(local.E, local.A, local.B, local.C, local.D, local.W[6]);
157 P(local.D, local.E, local.A, local.B, local.C, local.W[7]);
158 P(local.C, local.D, local.E, local.A, local.B, local.W[8]);
159 P(local.B, local.C, local.D, local.E, local.A, local.W[9]);
160 P(local.A, local.B, local.C, local.D, local.E, local.W[10]);
161 P(local.E, local.A, local.B, local.C, local.D, local.W[11]);
162 P(local.D, local.E, local.A, local.B, local.C, local.W[12]);
163 P(local.C, local.D, local.E, local.A, local.B, local.W[13]);
164 P(local.B, local.C, local.D, local.E, local.A, local.W[14]);
165 P(local.A, local.B, local.C, local.D, local.E, local.W[15]);
166 P(local.E, local.A, local.B, local.C, local.D, R(16));
167 P(local.D, local.E, local.A, local.B, local.C, R(17));
168 P(local.C, local.D, local.E, local.A, local.B, R(18));
169 P(local.B, local.C, local.D, local.E, local.A, R(19));
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171#undef K
172#undef F
173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000175#define K 0x6ED9EBA1
176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 P(local.A, local.B, local.C, local.D, local.E, R(20));
178 P(local.E, local.A, local.B, local.C, local.D, R(21));
179 P(local.D, local.E, local.A, local.B, local.C, R(22));
180 P(local.C, local.D, local.E, local.A, local.B, R(23));
181 P(local.B, local.C, local.D, local.E, local.A, R(24));
182 P(local.A, local.B, local.C, local.D, local.E, R(25));
183 P(local.E, local.A, local.B, local.C, local.D, R(26));
184 P(local.D, local.E, local.A, local.B, local.C, R(27));
185 P(local.C, local.D, local.E, local.A, local.B, R(28));
186 P(local.B, local.C, local.D, local.E, local.A, R(29));
187 P(local.A, local.B, local.C, local.D, local.E, R(30));
188 P(local.E, local.A, local.B, local.C, local.D, R(31));
189 P(local.D, local.E, local.A, local.B, local.C, R(32));
190 P(local.C, local.D, local.E, local.A, local.B, R(33));
191 P(local.B, local.C, local.D, local.E, local.A, R(34));
192 P(local.A, local.B, local.C, local.D, local.E, R(35));
193 P(local.E, local.A, local.B, local.C, local.D, R(36));
194 P(local.D, local.E, local.A, local.B, local.C, R(37));
195 P(local.C, local.D, local.E, local.A, local.B, R(38));
196 P(local.B, local.C, local.D, local.E, local.A, R(39));
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
198#undef K
199#undef F
200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201#define F(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000202#define K 0x8F1BBCDC
203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 P(local.A, local.B, local.C, local.D, local.E, R(40));
205 P(local.E, local.A, local.B, local.C, local.D, R(41));
206 P(local.D, local.E, local.A, local.B, local.C, R(42));
207 P(local.C, local.D, local.E, local.A, local.B, R(43));
208 P(local.B, local.C, local.D, local.E, local.A, R(44));
209 P(local.A, local.B, local.C, local.D, local.E, R(45));
210 P(local.E, local.A, local.B, local.C, local.D, R(46));
211 P(local.D, local.E, local.A, local.B, local.C, R(47));
212 P(local.C, local.D, local.E, local.A, local.B, R(48));
213 P(local.B, local.C, local.D, local.E, local.A, R(49));
214 P(local.A, local.B, local.C, local.D, local.E, R(50));
215 P(local.E, local.A, local.B, local.C, local.D, R(51));
216 P(local.D, local.E, local.A, local.B, local.C, R(52));
217 P(local.C, local.D, local.E, local.A, local.B, R(53));
218 P(local.B, local.C, local.D, local.E, local.A, R(54));
219 P(local.A, local.B, local.C, local.D, local.E, R(55));
220 P(local.E, local.A, local.B, local.C, local.D, R(56));
221 P(local.D, local.E, local.A, local.B, local.C, R(57));
222 P(local.C, local.D, local.E, local.A, local.B, R(58));
223 P(local.B, local.C, local.D, local.E, local.A, R(59));
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
225#undef K
226#undef F
227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000229#define K 0xCA62C1D6
230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 P(local.A, local.B, local.C, local.D, local.E, R(60));
232 P(local.E, local.A, local.B, local.C, local.D, R(61));
233 P(local.D, local.E, local.A, local.B, local.C, R(62));
234 P(local.C, local.D, local.E, local.A, local.B, R(63));
235 P(local.B, local.C, local.D, local.E, local.A, R(64));
236 P(local.A, local.B, local.C, local.D, local.E, R(65));
237 P(local.E, local.A, local.B, local.C, local.D, R(66));
238 P(local.D, local.E, local.A, local.B, local.C, R(67));
239 P(local.C, local.D, local.E, local.A, local.B, R(68));
240 P(local.B, local.C, local.D, local.E, local.A, R(69));
241 P(local.A, local.B, local.C, local.D, local.E, R(70));
242 P(local.E, local.A, local.B, local.C, local.D, R(71));
243 P(local.D, local.E, local.A, local.B, local.C, R(72));
244 P(local.C, local.D, local.E, local.A, local.B, R(73));
245 P(local.B, local.C, local.D, local.E, local.A, R(74));
246 P(local.A, local.B, local.C, local.D, local.E, R(75));
247 P(local.E, local.A, local.B, local.C, local.D, R(76));
248 P(local.D, local.E, local.A, local.B, local.C, R(77));
249 P(local.C, local.D, local.E, local.A, local.B, R(78));
250 P(local.B, local.C, local.D, local.E, local.A, R(79));
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252#undef K
253#undef F
254
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200255 ctx->state[0] += local.A;
256 ctx->state[1] += local.B;
257 ctx->state[2] += local.C;
258 ctx->state[3] += local.D;
259 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100260
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200261 /* Zeroise buffers and variables to clear sensitive data from memory. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 mbedtls_platform_zeroize(&local, sizeof(local));
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000265}
Jaeden Amero041039f2018-02-19 15:28:08 +0000266
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200267#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268void mbedtls_sha1_process(mbedtls_sha1_context *ctx,
269 const unsigned char data[64])
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200270{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 mbedtls_internal_sha1_process(ctx, data);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200272}
273#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
276/*
277 * SHA-1 process buffer
278 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx,
280 const unsigned char *input,
281 size_t ilen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000282{
Janos Follath24eed8d2019-11-22 13:21:35 +0000283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000284 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000285 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 SHA1_VALIDATE_RET(ctx != NULL);
288 SHA1_VALIDATE_RET(ilen == 0 || input != NULL);
Hanno Beckerb3906d82018-12-18 11:35:00 +0000289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 if (ilen == 0) {
291 return 0;
292 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
294 left = ctx->total[0] & 0x3F;
295 fill = 64 - left;
296
Paul Bakker5c2364c2012-10-01 14:41:15 +0000297 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 ctx->total[0] &= 0xFFFFFFFF;
299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 if (ctx->total[0] < (uint32_t) ilen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 ctx->total[1]++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 if (left && ilen >= fill) {
305 memcpy((void *) (ctx->buffer + left), input, fill);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
308 return ret;
309 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100310
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 input += fill;
312 ilen -= fill;
313 left = 0;
314 }
315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 while (ilen >= 64) {
317 if ((ret = mbedtls_internal_sha1_process(ctx, input)) != 0) {
318 return ret;
319 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100320
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 input += 64;
322 ilen -= 64;
323 }
324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 if (ilen > 0) {
326 memcpy((void *) (ctx->buffer + left), input, ilen);
327 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000330}
331
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200332#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333void mbedtls_sha1_update(mbedtls_sha1_context *ctx,
334 const unsigned char *input,
335 size_t ilen)
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200336{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 mbedtls_sha1_update_ret(ctx, input, ilen);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200338}
339#endif
340
Paul Bakker5121ce52009-01-03 21:22:43 +0000341/*
342 * SHA-1 final digest
343 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx,
345 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000346{
Janos Follath24eed8d2019-11-22 13:21:35 +0000347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200348 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000349 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 SHA1_VALIDATE_RET(ctx != NULL);
352 SHA1_VALIDATE_RET((unsigned char *) output != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000353
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200354 /*
355 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
356 */
357 used = ctx->total[0] & 0x3F;
358
359 ctx->buffer[used++] = 0x80;
360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 if (used <= 56) {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200362 /* Enough room for padding + length in current block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 memset(ctx->buffer + used, 0, 56 - used);
364 } else {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200365 /* We'll need an extra block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 memset(ctx->buffer + used, 0, 64 - used);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
369 return ret;
370 }
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 memset(ctx->buffer, 0, 56);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200373 }
374
375 /*
376 * Add message length
377 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 high = (ctx->total[0] >> 29)
379 | (ctx->total[1] << 3);
380 low = (ctx->total[0] << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
383 MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
386 return ret;
387 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200389 /*
390 * Output final state
391 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
393 MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
394 MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
395 MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
396 MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000399}
400
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200401#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402void mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
403 unsigned char output[20])
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200404{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 mbedtls_sha1_finish_ret(ctx, output);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200406}
407#endif
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411/*
412 * output = SHA-1( input buffer )
413 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414int mbedtls_sha1_ret(const unsigned char *input,
415 size_t ilen,
416 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000417{
Janos Follath24eed8d2019-11-22 13:21:35 +0000418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 SHA1_VALIDATE_RET(ilen == 0 || input != NULL);
422 SHA1_VALIDATE_RET((unsigned char *) output != NULL);
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +0000423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 mbedtls_sha1_init(&ctx);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 if ((ret = mbedtls_sha1_starts_ret(&ctx)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100427 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 if ((ret = mbedtls_sha1_update_ret(&ctx, input, ilen)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100431 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 if ((ret = mbedtls_sha1_finish_ret(&ctx, output)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100435 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100437
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100438exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 mbedtls_sha1_free(&ctx);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000442}
443
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200444#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445void mbedtls_sha1(const unsigned char *input,
446 size_t ilen,
447 unsigned char output[20])
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200448{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 mbedtls_sha1_ret(input, ilen, output);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200450}
451#endif
452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000454/*
455 * FIPS-180-1 test vectors
456 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000457static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000458{
459 { "abc" },
460 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
461 { "" }
462};
463
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100464static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000465{
466 3, 56, 1000
467};
468
469static const unsigned char sha1_test_sum[3][20] =
470{
471 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
472 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
473 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
474 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
475 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
476 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
477};
478
479/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 * Checkup routine
481 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482int mbedtls_sha1_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +0000483{
Paul Bakker5b4af392014-06-26 12:09:34 +0200484 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 unsigned char buf[1024];
486 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 mbedtls_sha1_init(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200490
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 /*
492 * SHA-1
493 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 for (i = 0; i < 3; i++) {
495 if (verbose != 0) {
496 mbedtls_printf(" SHA-1 test #%d: ", i + 1);
497 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499 if ((ret = mbedtls_sha1_starts_ret(&ctx)) != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100500 goto fail;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100503 if (i == 2) {
504 memset(buf, 'a', buflen = 1000);
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 for (j = 0; j < 1000; j++) {
507 ret = mbedtls_sha1_update_ret(&ctx, buf, buflen);
508 if (ret != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100509 goto fail;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 }
511 }
512 } else {
513 ret = mbedtls_sha1_update_ret(&ctx, sha1_test_buf[i],
514 sha1_test_buflen[i]);
515 if (ret != 0) {
516 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100517 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519
520 if ((ret = mbedtls_sha1_finish_ret(&ctx, sha1sum)) != 0) {
521 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100522 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 if (memcmp(sha1sum, sha1_test_sum[i], 20) != 0) {
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100525 ret = 1;
526 goto fail;
527 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 if (verbose != 0) {
530 mbedtls_printf("passed\n");
531 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 }
533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 if (verbose != 0) {
535 mbedtls_printf("\n");
536 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100538 goto exit;
539
540fail:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 if (verbose != 0) {
542 mbedtls_printf("failed\n");
543 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100544
Paul Bakker5b4af392014-06-26 12:09:34 +0200545exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 mbedtls_sha1_free(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100548 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000549}
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#endif /* MBEDTLS_SHA1_C */