blob: 4c9cbf5e85afe37a3f77159cab804c73ff7eef80 [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
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020037#if !defined(MBEDTLS_SHA1_ALT)
38
Gilles Peskine449bd832023-01-11 14:50:10 +010039void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +020040{
Gilles Peskine449bd832023-01-11 14:50:10 +010041 memset(ctx, 0, sizeof(mbedtls_sha1_context));
Paul Bakker5b4af392014-06-26 12:09:34 +020042}
43
Gilles Peskine449bd832023-01-11 14:50:10 +010044void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
Paul Bakker5b4af392014-06-26 12:09:34 +020045{
Gilles Peskine449bd832023-01-11 14:50:10 +010046 if (ctx == NULL) {
Paul Bakker5b4af392014-06-26 12:09:34 +020047 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010048 }
Paul Bakker5b4af392014-06-26 12:09:34 +020049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha1_context));
Paul Bakker5b4af392014-06-26 12:09:34 +020051}
52
Gilles Peskine449bd832023-01-11 14:50:10 +010053void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
54 const mbedtls_sha1_context *src)
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020055{
56 *dst = *src;
57}
58
Paul Bakker5121ce52009-01-03 21:22:43 +000059/*
60 * SHA-1 context setup
61 */
Gilles Peskine449bd832023-01-11 14:50:10 +010062int mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +000063{
64 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 Peskine449bd832023-01-11 14:50:10 +010073 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000074}
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +010077int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
78 const unsigned char data[64])
Paul Bakker5121ce52009-01-03 21:22:43 +000079{
Gilles Peskine449bd832023-01-11 14:50:10 +010080 struct {
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +020081 uint32_t temp, W[16], A, B, C, D, E;
82 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 local.W[0] = MBEDTLS_GET_UINT32_BE(data, 0);
85 local.W[1] = MBEDTLS_GET_UINT32_BE(data, 4);
86 local.W[2] = MBEDTLS_GET_UINT32_BE(data, 8);
87 local.W[3] = MBEDTLS_GET_UINT32_BE(data, 12);
88 local.W[4] = MBEDTLS_GET_UINT32_BE(data, 16);
89 local.W[5] = MBEDTLS_GET_UINT32_BE(data, 20);
90 local.W[6] = MBEDTLS_GET_UINT32_BE(data, 24);
91 local.W[7] = MBEDTLS_GET_UINT32_BE(data, 28);
92 local.W[8] = MBEDTLS_GET_UINT32_BE(data, 32);
93 local.W[9] = MBEDTLS_GET_UINT32_BE(data, 36);
94 local.W[10] = MBEDTLS_GET_UINT32_BE(data, 40);
95 local.W[11] = MBEDTLS_GET_UINT32_BE(data, 44);
96 local.W[12] = MBEDTLS_GET_UINT32_BE(data, 48);
97 local.W[13] = MBEDTLS_GET_UINT32_BE(data, 52);
98 local.W[14] = MBEDTLS_GET_UINT32_BE(data, 56);
99 local.W[15] = MBEDTLS_GET_UINT32_BE(data, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101#define S(x, n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Hanno Becker1eeca412018-10-15 12:01:35 +0100103#define R(t) \
104 ( \
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 local.temp = local.W[((t) - 3) & 0x0F] ^ \
106 local.W[((t) - 8) & 0x0F] ^ \
107 local.W[((t) - 14) & 0x0F] ^ \
108 local.W[(t) & 0x0F], \
109 (local.W[(t) & 0x0F] = S(local.temp, 1)) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100110 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112#define P(a, b, c, d, e, x) \
Hanno Becker1eeca412018-10-15 12:01:35 +0100113 do \
114 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 (e) += S((a), 5) + F((b), (c), (d)) + K + (x); \
116 (b) = S((b), 30); \
117 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200119 local.A = ctx->state[0];
120 local.B = ctx->state[1];
121 local.C = ctx->state[2];
122 local.D = ctx->state[3];
123 local.E = ctx->state[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000126#define K 0x5A827999
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 P(local.A, local.B, local.C, local.D, local.E, local.W[0]);
129 P(local.E, local.A, local.B, local.C, local.D, local.W[1]);
130 P(local.D, local.E, local.A, local.B, local.C, local.W[2]);
131 P(local.C, local.D, local.E, local.A, local.B, local.W[3]);
132 P(local.B, local.C, local.D, local.E, local.A, local.W[4]);
133 P(local.A, local.B, local.C, local.D, local.E, local.W[5]);
134 P(local.E, local.A, local.B, local.C, local.D, local.W[6]);
135 P(local.D, local.E, local.A, local.B, local.C, local.W[7]);
136 P(local.C, local.D, local.E, local.A, local.B, local.W[8]);
137 P(local.B, local.C, local.D, local.E, local.A, local.W[9]);
138 P(local.A, local.B, local.C, local.D, local.E, local.W[10]);
139 P(local.E, local.A, local.B, local.C, local.D, local.W[11]);
140 P(local.D, local.E, local.A, local.B, local.C, local.W[12]);
141 P(local.C, local.D, local.E, local.A, local.B, local.W[13]);
142 P(local.B, local.C, local.D, local.E, local.A, local.W[14]);
143 P(local.A, local.B, local.C, local.D, local.E, local.W[15]);
144 P(local.E, local.A, local.B, local.C, local.D, R(16));
145 P(local.D, local.E, local.A, local.B, local.C, R(17));
146 P(local.C, local.D, local.E, local.A, local.B, R(18));
147 P(local.B, local.C, local.D, local.E, local.A, R(19));
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149#undef K
150#undef F
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000153#define K 0x6ED9EBA1
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 P(local.A, local.B, local.C, local.D, local.E, R(20));
156 P(local.E, local.A, local.B, local.C, local.D, R(21));
157 P(local.D, local.E, local.A, local.B, local.C, R(22));
158 P(local.C, local.D, local.E, local.A, local.B, R(23));
159 P(local.B, local.C, local.D, local.E, local.A, R(24));
160 P(local.A, local.B, local.C, local.D, local.E, R(25));
161 P(local.E, local.A, local.B, local.C, local.D, R(26));
162 P(local.D, local.E, local.A, local.B, local.C, R(27));
163 P(local.C, local.D, local.E, local.A, local.B, R(28));
164 P(local.B, local.C, local.D, local.E, local.A, R(29));
165 P(local.A, local.B, local.C, local.D, local.E, R(30));
166 P(local.E, local.A, local.B, local.C, local.D, R(31));
167 P(local.D, local.E, local.A, local.B, local.C, R(32));
168 P(local.C, local.D, local.E, local.A, local.B, R(33));
169 P(local.B, local.C, local.D, local.E, local.A, R(34));
170 P(local.A, local.B, local.C, local.D, local.E, R(35));
171 P(local.E, local.A, local.B, local.C, local.D, R(36));
172 P(local.D, local.E, local.A, local.B, local.C, R(37));
173 P(local.C, local.D, local.E, local.A, local.B, R(38));
174 P(local.B, local.C, local.D, local.E, local.A, R(39));
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176#undef K
177#undef F
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179#define F(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000180#define K 0x8F1BBCDC
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 P(local.A, local.B, local.C, local.D, local.E, R(40));
183 P(local.E, local.A, local.B, local.C, local.D, R(41));
184 P(local.D, local.E, local.A, local.B, local.C, R(42));
185 P(local.C, local.D, local.E, local.A, local.B, R(43));
186 P(local.B, local.C, local.D, local.E, local.A, R(44));
187 P(local.A, local.B, local.C, local.D, local.E, R(45));
188 P(local.E, local.A, local.B, local.C, local.D, R(46));
189 P(local.D, local.E, local.A, local.B, local.C, R(47));
190 P(local.C, local.D, local.E, local.A, local.B, R(48));
191 P(local.B, local.C, local.D, local.E, local.A, R(49));
192 P(local.A, local.B, local.C, local.D, local.E, R(50));
193 P(local.E, local.A, local.B, local.C, local.D, R(51));
194 P(local.D, local.E, local.A, local.B, local.C, R(52));
195 P(local.C, local.D, local.E, local.A, local.B, R(53));
196 P(local.B, local.C, local.D, local.E, local.A, R(54));
197 P(local.A, local.B, local.C, local.D, local.E, R(55));
198 P(local.E, local.A, local.B, local.C, local.D, R(56));
199 P(local.D, local.E, local.A, local.B, local.C, R(57));
200 P(local.C, local.D, local.E, local.A, local.B, R(58));
201 P(local.B, local.C, local.D, local.E, local.A, R(59));
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203#undef K
204#undef F
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206#define F(x, y, z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000207#define K 0xCA62C1D6
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 P(local.A, local.B, local.C, local.D, local.E, R(60));
210 P(local.E, local.A, local.B, local.C, local.D, R(61));
211 P(local.D, local.E, local.A, local.B, local.C, R(62));
212 P(local.C, local.D, local.E, local.A, local.B, R(63));
213 P(local.B, local.C, local.D, local.E, local.A, R(64));
214 P(local.A, local.B, local.C, local.D, local.E, R(65));
215 P(local.E, local.A, local.B, local.C, local.D, R(66));
216 P(local.D, local.E, local.A, local.B, local.C, R(67));
217 P(local.C, local.D, local.E, local.A, local.B, R(68));
218 P(local.B, local.C, local.D, local.E, local.A, R(69));
219 P(local.A, local.B, local.C, local.D, local.E, R(70));
220 P(local.E, local.A, local.B, local.C, local.D, R(71));
221 P(local.D, local.E, local.A, local.B, local.C, R(72));
222 P(local.C, local.D, local.E, local.A, local.B, R(73));
223 P(local.B, local.C, local.D, local.E, local.A, R(74));
224 P(local.A, local.B, local.C, local.D, local.E, R(75));
225 P(local.E, local.A, local.B, local.C, local.D, R(76));
226 P(local.D, local.E, local.A, local.B, local.C, R(77));
227 P(local.C, local.D, local.E, local.A, local.B, R(78));
228 P(local.B, local.C, local.D, local.E, local.A, R(79));
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
230#undef K
231#undef F
232
gabor-mezei-arm4cb56f82020-08-25 19:12:01 +0200233 ctx->state[0] += local.A;
234 ctx->state[1] += local.B;
235 ctx->state[2] += local.C;
236 ctx->state[3] += local.D;
237 ctx->state[4] += local.E;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100238
gabor-mezei-arm76749ae2020-07-30 16:41:25 +0200239 /* Zeroise buffers and variables to clear sensitive data from memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 mbedtls_platform_zeroize(&local, sizeof(local));
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000243}
Jaeden Amero041039f2018-02-19 15:28:08 +0000244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* !MBEDTLS_SHA1_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
247/*
248 * SHA-1 process buffer
249 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250int mbedtls_sha1_update(mbedtls_sha1_context *ctx,
251 const unsigned char *input,
252 size_t ilen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000253{
Janos Follath24eed8d2019-11-22 13:21:35 +0000254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000255 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000256 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (ilen == 0) {
259 return 0;
260 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262 left = ctx->total[0] & 0x3F;
263 fill = 64 - left;
264
Paul Bakker5c2364c2012-10-01 14:41:15 +0000265 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 ctx->total[0] &= 0xFFFFFFFF;
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (ctx->total[0] < (uint32_t) ilen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 ctx->total[1]++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (left && ilen >= fill) {
273 memcpy((void *) (ctx->buffer + left), input, fill);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
276 return ret;
277 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100278
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 input += fill;
280 ilen -= fill;
281 left = 0;
282 }
283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 while (ilen >= 64) {
285 if ((ret = mbedtls_internal_sha1_process(ctx, input)) != 0) {
286 return ret;
287 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100288
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 input += 64;
290 ilen -= 64;
291 }
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (ilen > 0) {
294 memcpy((void *) (ctx->buffer + left), input, ilen);
295 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000298}
299
Paul Bakker5121ce52009-01-03 21:22:43 +0000300/*
301 * SHA-1 final digest
302 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100303int mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
304 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000305{
Janos Follath24eed8d2019-11-22 13:21:35 +0000306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200307 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000308 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200310 /*
311 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
312 */
313 used = ctx->total[0] & 0x3F;
314
315 ctx->buffer[used++] = 0x80;
316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (used <= 56) {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200318 /* Enough room for padding + length in current block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 memset(ctx->buffer + used, 0, 56 - used);
320 } else {
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200321 /* We'll need an extra block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 memset(ctx->buffer + used, 0, 64 - used);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
325 return ret;
326 }
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 memset(ctx->buffer, 0, 56);
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200329 }
330
331 /*
332 * Add message length
333 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 high = (ctx->total[0] >> 29)
335 | (ctx->total[1] << 3);
336 low = (ctx->total[0] << 3);
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 MBEDTLS_PUT_UINT32_BE(high, ctx->buffer, 56);
339 MBEDTLS_PUT_UINT32_BE(low, ctx->buffer, 60);
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if ((ret = mbedtls_internal_sha1_process(ctx, ctx->buffer)) != 0) {
342 return ret;
343 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200345 /*
346 * Output final state
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 MBEDTLS_PUT_UINT32_BE(ctx->state[0], output, 0);
349 MBEDTLS_PUT_UINT32_BE(ctx->state[1], output, 4);
350 MBEDTLS_PUT_UINT32_BE(ctx->state[2], output, 8);
351 MBEDTLS_PUT_UINT32_BE(ctx->state[3], output, 12);
352 MBEDTLS_PUT_UINT32_BE(ctx->state[4], output, 16);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000355}
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#endif /* !MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200358
Paul Bakker5121ce52009-01-03 21:22:43 +0000359/*
360 * output = SHA-1( input buffer )
361 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362int mbedtls_sha1(const unsigned char *input,
363 size_t ilen,
364 unsigned char output[20])
Paul Bakker5121ce52009-01-03 21:22:43 +0000365{
Janos Follath24eed8d2019-11-22 13:21:35 +0000366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 mbedtls_sha1_init(&ctx);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if ((ret = mbedtls_sha1_starts(&ctx)) != 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if ((ret = mbedtls_sha1_update(&ctx, input, ilen)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100376 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if ((ret = mbedtls_sha1_finish(&ctx, output)) != 0) {
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100380 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100382
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100383exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 mbedtls_sha1_free(&ctx);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000387}
388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000390/*
391 * FIPS-180-1 test vectors
392 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000393static const unsigned char sha1_test_buf[3][57] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000394{
395 { "abc" },
396 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
397 { "" }
398};
399
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100400static const size_t sha1_test_buflen[3] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000401{
402 3, 56, 1000
403};
404
405static const unsigned char sha1_test_sum[3][20] =
406{
407 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
408 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
409 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
410 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
411 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
412 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
413};
414
415/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 * Checkup routine
417 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418int mbedtls_sha1_self_test(int verbose)
Paul Bakker5121ce52009-01-03 21:22:43 +0000419{
Paul Bakker5b4af392014-06-26 12:09:34 +0200420 int i, j, buflen, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 unsigned char buf[1024];
422 unsigned char sha1sum[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_sha1_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 mbedtls_sha1_init(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200426
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 /*
428 * SHA-1
429 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 for (i = 0; i < 3; i++) {
431 if (verbose != 0) {
432 mbedtls_printf(" SHA-1 test #%d: ", i + 1);
433 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if ((ret = mbedtls_sha1_starts(&ctx)) != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100436 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (i == 2) {
440 memset(buf, 'a', buflen = 1000);
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 for (j = 0; j < 1000; j++) {
443 ret = mbedtls_sha1_update(&ctx, buf, buflen);
444 if (ret != 0) {
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100445 goto fail;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 }
447 }
448 } else {
449 ret = mbedtls_sha1_update(&ctx, sha1_test_buf[i],
450 sha1_test_buflen[i]);
451 if (ret != 0) {
452 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100453 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100455
456 if ((ret = mbedtls_sha1_finish(&ctx, sha1sum)) != 0) {
457 goto fail;
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100458 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (memcmp(sha1sum, sha1_test_sum[i], 20) != 0) {
Andres Amaya Garcia6a3f3052017-07-20 14:18:54 +0100461 ret = 1;
462 goto fail;
463 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (verbose != 0) {
466 mbedtls_printf("passed\n");
467 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 }
469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (verbose != 0) {
471 mbedtls_printf("\n");
472 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100474 goto exit;
475
476fail:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (verbose != 0) {
478 mbedtls_printf("failed\n");
479 }
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100480
Paul Bakker5b4af392014-06-26 12:09:34 +0200481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 mbedtls_sha1_free(&ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000485}
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#endif /* MBEDTLS_SHA1_C */