blob: a2e1ca77add6030b248a479e3d845bb2679eab71 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * RFC 1321 compliant MD5 implementation
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46/*
47 * The MD5 algorithm was designed by Ron Rivest in 1991.
48 *
49 * http://www.ietf.org/rfc/rfc1321.txt
50 */
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020054#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/md5.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050061#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <string.h>
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_SELF_TEST)
66#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010068#else
Rich Evans00ab4702015-02-06 13:43:58 +000069#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#define mbedtls_printf printf
71#endif /* MBEDTLS_PLATFORM_C */
72#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7dc4c442014-02-01 22:50:26 +010073
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020074#if !defined(MBEDTLS_MD5_ALT)
75
Paul Bakker5121ce52009-01-03 21:22:43 +000076/*
77 * 32-bit integer manipulation macros (little endian)
78 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000079#ifndef GET_UINT32_LE
80#define GET_UINT32_LE(n,b,i) \
Paul Bakker5121ce52009-01-03 21:22:43 +000081{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000082 (n) = ( (uint32_t) (b)[(i) ] ) \
83 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
84 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
85 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000086}
87#endif
88
Paul Bakker5c2364c2012-10-01 14:41:15 +000089#ifndef PUT_UINT32_LE
Manuel Pégourié-Gonnardceedb822015-01-23 15:02:43 +000090#define PUT_UINT32_LE(n,b,i) \
91{ \
92 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
93 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
94 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
95 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
Paul Bakker5121ce52009-01-03 21:22:43 +000096}
97#endif
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099void mbedtls_md5_init( mbedtls_md5_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 memset( ctx, 0, sizeof( mbedtls_md5_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200102}
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104void mbedtls_md5_free( mbedtls_md5_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +0200105{
106 if( ctx == NULL )
107 return;
108
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500109 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md5_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +0200110}
111
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200112void mbedtls_md5_clone( mbedtls_md5_context *dst,
113 const mbedtls_md5_context *src )
114{
115 *dst = *src;
116}
117
Paul Bakker5121ce52009-01-03 21:22:43 +0000118/*
119 * MD5 context setup
120 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100121int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122{
123 ctx->total[0] = 0;
124 ctx->total[1] = 0;
125
126 ctx->state[0] = 0x67452301;
127 ctx->state[1] = 0xEFCDAB89;
128 ctx->state[2] = 0x98BADCFE;
129 ctx->state[3] = 0x10325476;
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100130
131 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132}
133
Jaeden Amero041039f2018-02-19 15:28:08 +0000134#if !defined(MBEDTLS_DEPRECATED_REMOVED)
135void mbedtls_md5_starts( mbedtls_md5_context *ctx )
136{
137 mbedtls_md5_starts_ret( ctx );
138}
139#endif
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if !defined(MBEDTLS_MD5_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100142int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
143 const unsigned char data[64] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000144{
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200145 struct
146 {
147 uint32_t X[16], A, B, C, D;
148 } local;
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200150 GET_UINT32_LE( local.X[ 0], data, 0 );
151 GET_UINT32_LE( local.X[ 1], data, 4 );
152 GET_UINT32_LE( local.X[ 2], data, 8 );
153 GET_UINT32_LE( local.X[ 3], data, 12 );
154 GET_UINT32_LE( local.X[ 4], data, 16 );
155 GET_UINT32_LE( local.X[ 5], data, 20 );
156 GET_UINT32_LE( local.X[ 6], data, 24 );
157 GET_UINT32_LE( local.X[ 7], data, 28 );
158 GET_UINT32_LE( local.X[ 8], data, 32 );
159 GET_UINT32_LE( local.X[ 9], data, 36 );
160 GET_UINT32_LE( local.X[10], data, 40 );
161 GET_UINT32_LE( local.X[11], data, 44 );
162 GET_UINT32_LE( local.X[12], data, 48 );
163 GET_UINT32_LE( local.X[13], data, 52 );
164 GET_UINT32_LE( local.X[14], data, 56 );
165 GET_UINT32_LE( local.X[15], data, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Hanno Beckerd6028a12018-10-15 12:01:35 +0100167#define S(x,n) \
168 ( ( (x) << (n) ) | ( ( (x) & 0xFFFFFFFF) >> ( 32 - (n) ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200170#define P(a,b,c,d,k,s,t) \
171 do \
172 { \
173 (a) += F((b),(c),(d)) + local.X[(k)] + (t); \
174 (a) = S((a),(s)) + (b); \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100175 } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200177 local.A = ctx->state[0];
178 local.B = ctx->state[1];
179 local.C = ctx->state[2];
180 local.D = ctx->state[3];
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Hanno Beckerd6028a12018-10-15 12:01:35 +0100182#define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200184 P( local.A, local.B, local.C, local.D, 0, 7, 0xD76AA478 );
185 P( local.D, local.A, local.B, local.C, 1, 12, 0xE8C7B756 );
186 P( local.C, local.D, local.A, local.B, 2, 17, 0x242070DB );
187 P( local.B, local.C, local.D, local.A, 3, 22, 0xC1BDCEEE );
188 P( local.A, local.B, local.C, local.D, 4, 7, 0xF57C0FAF );
189 P( local.D, local.A, local.B, local.C, 5, 12, 0x4787C62A );
190 P( local.C, local.D, local.A, local.B, 6, 17, 0xA8304613 );
191 P( local.B, local.C, local.D, local.A, 7, 22, 0xFD469501 );
192 P( local.A, local.B, local.C, local.D, 8, 7, 0x698098D8 );
193 P( local.D, local.A, local.B, local.C, 9, 12, 0x8B44F7AF );
194 P( local.C, local.D, local.A, local.B, 10, 17, 0xFFFF5BB1 );
195 P( local.B, local.C, local.D, local.A, 11, 22, 0x895CD7BE );
196 P( local.A, local.B, local.C, local.D, 12, 7, 0x6B901122 );
197 P( local.D, local.A, local.B, local.C, 13, 12, 0xFD987193 );
198 P( local.C, local.D, local.A, local.B, 14, 17, 0xA679438E );
199 P( local.B, local.C, local.D, local.A, 15, 22, 0x49B40821 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201#undef F
202
Hanno Beckerd6028a12018-10-15 12:01:35 +0100203#define F(x,y,z) ((y) ^ ((z) & ((x) ^ (y))))
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200205 P( local.A, local.B, local.C, local.D, 1, 5, 0xF61E2562 );
206 P( local.D, local.A, local.B, local.C, 6, 9, 0xC040B340 );
207 P( local.C, local.D, local.A, local.B, 11, 14, 0x265E5A51 );
208 P( local.B, local.C, local.D, local.A, 0, 20, 0xE9B6C7AA );
209 P( local.A, local.B, local.C, local.D, 5, 5, 0xD62F105D );
210 P( local.D, local.A, local.B, local.C, 10, 9, 0x02441453 );
211 P( local.C, local.D, local.A, local.B, 15, 14, 0xD8A1E681 );
212 P( local.B, local.C, local.D, local.A, 4, 20, 0xE7D3FBC8 );
213 P( local.A, local.B, local.C, local.D, 9, 5, 0x21E1CDE6 );
214 P( local.D, local.A, local.B, local.C, 14, 9, 0xC33707D6 );
215 P( local.C, local.D, local.A, local.B, 3, 14, 0xF4D50D87 );
216 P( local.B, local.C, local.D, local.A, 8, 20, 0x455A14ED );
217 P( local.A, local.B, local.C, local.D, 13, 5, 0xA9E3E905 );
218 P( local.D, local.A, local.B, local.C, 2, 9, 0xFCEFA3F8 );
219 P( local.C, local.D, local.A, local.B, 7, 14, 0x676F02D9 );
220 P( local.B, local.C, local.D, local.A, 12, 20, 0x8D2A4C8A );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222#undef F
Paul Bakker9af723c2014-05-01 13:03:14 +0200223
Hanno Beckerd6028a12018-10-15 12:01:35 +0100224#define F(x,y,z) ((x) ^ (y) ^ (z))
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200226 P( local.A, local.B, local.C, local.D, 5, 4, 0xFFFA3942 );
227 P( local.D, local.A, local.B, local.C, 8, 11, 0x8771F681 );
228 P( local.C, local.D, local.A, local.B, 11, 16, 0x6D9D6122 );
229 P( local.B, local.C, local.D, local.A, 14, 23, 0xFDE5380C );
230 P( local.A, local.B, local.C, local.D, 1, 4, 0xA4BEEA44 );
231 P( local.D, local.A, local.B, local.C, 4, 11, 0x4BDECFA9 );
232 P( local.C, local.D, local.A, local.B, 7, 16, 0xF6BB4B60 );
233 P( local.B, local.C, local.D, local.A, 10, 23, 0xBEBFBC70 );
234 P( local.A, local.B, local.C, local.D, 13, 4, 0x289B7EC6 );
235 P( local.D, local.A, local.B, local.C, 0, 11, 0xEAA127FA );
236 P( local.C, local.D, local.A, local.B, 3, 16, 0xD4EF3085 );
237 P( local.B, local.C, local.D, local.A, 6, 23, 0x04881D05 );
238 P( local.A, local.B, local.C, local.D, 9, 4, 0xD9D4D039 );
239 P( local.D, local.A, local.B, local.C, 12, 11, 0xE6DB99E5 );
240 P( local.C, local.D, local.A, local.B, 15, 16, 0x1FA27CF8 );
241 P( local.B, local.C, local.D, local.A, 2, 23, 0xC4AC5665 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
243#undef F
244
Hanno Beckerd6028a12018-10-15 12:01:35 +0100245#define F(x,y,z) ((y) ^ ((x) | ~(z)))
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200247 P( local.A, local.B, local.C, local.D, 0, 6, 0xF4292244 );
248 P( local.D, local.A, local.B, local.C, 7, 10, 0x432AFF97 );
249 P( local.C, local.D, local.A, local.B, 14, 15, 0xAB9423A7 );
250 P( local.B, local.C, local.D, local.A, 5, 21, 0xFC93A039 );
251 P( local.A, local.B, local.C, local.D, 12, 6, 0x655B59C3 );
252 P( local.D, local.A, local.B, local.C, 3, 10, 0x8F0CCC92 );
253 P( local.C, local.D, local.A, local.B, 10, 15, 0xFFEFF47D );
254 P( local.B, local.C, local.D, local.A, 1, 21, 0x85845DD1 );
255 P( local.A, local.B, local.C, local.D, 8, 6, 0x6FA87E4F );
256 P( local.D, local.A, local.B, local.C, 15, 10, 0xFE2CE6E0 );
257 P( local.C, local.D, local.A, local.B, 6, 15, 0xA3014314 );
258 P( local.B, local.C, local.D, local.A, 13, 21, 0x4E0811A1 );
259 P( local.A, local.B, local.C, local.D, 4, 6, 0xF7537E82 );
260 P( local.D, local.A, local.B, local.C, 11, 10, 0xBD3AF235 );
261 P( local.C, local.D, local.A, local.B, 2, 15, 0x2AD7D2BB );
262 P( local.B, local.C, local.D, local.A, 9, 21, 0xEB86D391 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
264#undef F
265
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200266 ctx->state[0] += local.A;
267 ctx->state[1] += local.B;
268 ctx->state[2] += local.C;
269 ctx->state[3] += local.D;
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100270
gabor-mezei-armf21639f2020-08-19 14:03:06 +0200271 /* Zeroise variables to clear sensitive data from memory. */
gabor-mezei-arm70f7f672020-08-25 19:12:01 +0200272 mbedtls_platform_zeroize( &local, sizeof( local ) );
gabor-mezei-armf21639f2020-08-19 14:03:06 +0200273
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100274 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275}
Jaeden Amero041039f2018-02-19 15:28:08 +0000276
277#if !defined(MBEDTLS_DEPRECATED_REMOVED)
278void mbedtls_md5_process( mbedtls_md5_context *ctx,
279 const unsigned char data[64] )
280{
281 mbedtls_internal_md5_process( ctx, data );
282}
283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#endif /* !MBEDTLS_MD5_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286/*
287 * MD5 process buffer
288 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100289int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100290 const unsigned char *input,
291 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000292{
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100293 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000294 size_t fill;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000295 uint32_t left;
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Brian White12895d12014-04-11 11:29:42 -0400297 if( ilen == 0 )
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100298 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 left = ctx->total[0] & 0x3F;
301 fill = 64 - left;
302
Paul Bakker5c2364c2012-10-01 14:41:15 +0000303 ctx->total[0] += (uint32_t) ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 ctx->total[0] &= 0xFFFFFFFF;
305
Paul Bakker5c2364c2012-10-01 14:41:15 +0000306 if( ctx->total[0] < (uint32_t) ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 ctx->total[1]++;
308
309 if( left && ilen >= fill )
310 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200311 memcpy( (void *) (ctx->buffer + left), input, fill );
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100312 if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100313 return( ret );
314
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 input += fill;
316 ilen -= fill;
317 left = 0;
318 }
319
320 while( ilen >= 64 )
321 {
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100322 if( ( ret = mbedtls_internal_md5_process( ctx, input ) ) != 0 )
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100323 return( ret );
324
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 input += 64;
326 ilen -= 64;
327 }
328
329 if( ilen > 0 )
330 {
Paul Bakker3c2122f2013-06-24 19:03:14 +0200331 memcpy( (void *) (ctx->buffer + left), input, ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 }
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100333
334 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335}
336
Jaeden Amero041039f2018-02-19 15:28:08 +0000337#if !defined(MBEDTLS_DEPRECATED_REMOVED)
338void mbedtls_md5_update( mbedtls_md5_context *ctx,
339 const unsigned char *input,
340 size_t ilen )
341{
342 mbedtls_md5_update_ret( ctx, input, ilen );
343}
344#endif
345
Paul Bakker5121ce52009-01-03 21:22:43 +0000346/*
347 * MD5 final digest
348 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100349int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100350 unsigned char output[16] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000351{
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100352 int ret;
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200353 uint32_t used;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000354 uint32_t high, low;
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200356 /*
357 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
358 */
359 used = ctx->total[0] & 0x3F;
360
361 ctx->buffer[used++] = 0x80;
362
363 if( used <= 56 )
364 {
365 /* Enough room for padding + length in current block */
366 memset( ctx->buffer + used, 0, 56 - used );
367 }
368 else
369 {
370 /* We'll need an extra block */
371 memset( ctx->buffer + used, 0, 64 - used );
372
373 if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
374 return( ret );
375
376 memset( ctx->buffer, 0, 56 );
377 }
378
379 /*
380 * Add message length
381 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 high = ( ctx->total[0] >> 29 )
383 | ( ctx->total[1] << 3 );
384 low = ( ctx->total[0] << 3 );
385
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200386 PUT_UINT32_LE( low, ctx->buffer, 56 );
387 PUT_UINT32_LE( high, ctx->buffer, 60 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200389 if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
390 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Manuel Pégourié-Gonnard1cc1fb02018-06-28 12:10:27 +0200392 /*
393 * Output final state
394 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000395 PUT_UINT32_LE( ctx->state[0], output, 0 );
396 PUT_UINT32_LE( ctx->state[1], output, 4 );
397 PUT_UINT32_LE( ctx->state[2], output, 8 );
398 PUT_UINT32_LE( ctx->state[3], output, 12 );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100399
400 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401}
402
Jaeden Amero041039f2018-02-19 15:28:08 +0000403#if !defined(MBEDTLS_DEPRECATED_REMOVED)
404void mbedtls_md5_finish( mbedtls_md5_context *ctx,
405 unsigned char output[16] )
406{
407 mbedtls_md5_finish_ret( ctx, output );
408}
409#endif
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#endif /* !MBEDTLS_MD5_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200412
Paul Bakker5121ce52009-01-03 21:22:43 +0000413/*
414 * output = MD5( input buffer )
415 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100416int mbedtls_md5_ret( const unsigned char *input,
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100417 size_t ilen,
418 unsigned char output[16] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000419{
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100420 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_md5_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_md5_init( &ctx );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100424
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100425 if( ( ret = mbedtls_md5_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100426 goto exit;
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100427
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100428 if( ( ret = mbedtls_md5_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100429 goto exit;
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100430
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100431 if( ( ret = mbedtls_md5_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100432 goto exit;
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100433
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100434exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_md5_free( &ctx );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100436
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100437 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438}
439
Jaeden Amero041039f2018-02-19 15:28:08 +0000440#if !defined(MBEDTLS_DEPRECATED_REMOVED)
441void mbedtls_md5( const unsigned char *input,
442 size_t ilen,
443 unsigned char output[16] )
444{
445 mbedtls_md5_ret( input, ilen, output );
446}
447#endif
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000450/*
451 * RFC 1321 test vectors
452 */
Manuel Pégourié-Gonnard28122e42015-03-11 09:13:42 +0000453static const unsigned char md5_test_buf[7][81] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000454{
Paul Bakker9af723c2014-05-01 13:03:14 +0200455 { "" },
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 { "a" },
457 { "abc" },
458 { "message digest" },
459 { "abcdefghijklmnopqrstuvwxyz" },
460 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
Guido Vranken70bdf8d2020-08-21 21:08:56 +0200461 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890" }
Paul Bakker5121ce52009-01-03 21:22:43 +0000462};
463
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100464static const size_t md5_test_buflen[7] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000465{
466 0, 1, 3, 14, 26, 62, 80
467};
468
469static const unsigned char md5_test_sum[7][16] =
470{
471 { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
472 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
473 { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
474 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
475 { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
476 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
477 { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
478 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
479 { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
480 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
481 { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
482 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
483 { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
484 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
485};
486
487/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 * Checkup routine
489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490int mbedtls_md5_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000491{
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100492 int i, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 unsigned char md5sum[16];
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495 for( i = 0; i < 7; i++ )
496 {
497 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_printf( " MD5 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100500 ret = mbedtls_md5_ret( md5_test_buf[i], md5_test_buflen[i], md5sum );
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100501 if( ret != 0 )
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100502 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100505 {
506 ret = 1;
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100507 goto fail;
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100508 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 }
513
514 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 return( 0 );
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100518
519fail:
520 if( verbose != 0 )
521 mbedtls_printf( "failed\n" );
522
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100523 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524}
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#endif /* MBEDTLS_MD5_C */