blob: f8293a1df39ba9186385a74addf96d543831c0a8 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * RFC 1115/1319 compliant MD2 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 MD2 algorithm was designed by Ron Rivest in 1989.
21 *
22 * http://www.ietf.org/rfc/rfc1115.txt
23 * http://www.ietf.org/rfc/rfc1319.txt
24 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_MD2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/md2.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050031#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000032#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Manuel Pégourié-Gonnard0c851ee2015-02-10 12:47:52 +000034#include <string.h>
35
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010037
Manuel Pégourié-Gonnard8b2641d2015-08-27 20:03:46 +020038#if !defined(MBEDTLS_MD2_ALT)
39
Paul Bakker5121ce52009-01-03 21:22:43 +000040static const unsigned char PI_SUBST[256] =
41{
42 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36,
43 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, 0x62, 0xA7, 0x05, 0xF3,
44 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C,
45 0x82, 0xCA, 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16,
46 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, 0xBE, 0x4E,
47 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E,
48 0xBB, 0x2F, 0xEE, 0x7A, 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2,
49 0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
50 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E,
51 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, 0xFF, 0x19, 0x30, 0xB3,
52 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56,
53 0xAA, 0xC6, 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6,
54 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 0x45, 0x9D,
55 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65,
56 0xE6, 0x2D, 0xA8, 0x02, 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0,
57 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
58 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C,
59 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 0x2C, 0x53, 0x0D, 0x6E,
60 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81,
61 0x4D, 0x52, 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA,
62 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, 0x78, 0x88,
63 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE,
64 0x3B, 0x00, 0x1D, 0x39, 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58,
65 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
66 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99,
67 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14
68};
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070void mbedtls_md2_init( mbedtls_md2_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 memset( ctx, 0, sizeof( mbedtls_md2_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020073}
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075void mbedtls_md2_free( mbedtls_md2_context *ctx )
Paul Bakker5b4af392014-06-26 12:09:34 +020076{
77 if( ctx == NULL )
78 return;
79
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050080 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md2_context ) );
Paul Bakker5b4af392014-06-26 12:09:34 +020081}
82
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020083void mbedtls_md2_clone( mbedtls_md2_context *dst,
84 const mbedtls_md2_context *src )
85{
86 *dst = *src;
87}
88
Paul Bakker5121ce52009-01-03 21:22:43 +000089/*
90 * MD2 context setup
91 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010092int mbedtls_md2_starts_ret( mbedtls_md2_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +000093{
Paul Bakker2fd71f02009-07-11 20:40:58 +000094 memset( ctx->cksum, 0, 16 );
95 memset( ctx->state, 0, 46 );
96 memset( ctx->buffer, 0, 16 );
97 ctx->left = 0;
Andres Amaya Garcia1d852132017-04-28 16:21:40 +010098
99 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100}
101
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200102#if !defined(MBEDTLS_DEPRECATED_REMOVED)
103void mbedtls_md2_starts( mbedtls_md2_context *ctx )
104{
105 mbedtls_md2_starts_ret( ctx );
106}
107#endif
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if !defined(MBEDTLS_MD2_PROCESS_ALT)
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100110int mbedtls_internal_md2_process( mbedtls_md2_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000111{
112 int i, j;
113 unsigned char t = 0;
114
115 for( i = 0; i < 16; i++ )
116 {
117 ctx->state[i + 16] = ctx->buffer[i];
118 ctx->state[i + 32] =
119 (unsigned char)( ctx->buffer[i] ^ ctx->state[i]);
120 }
121
122 for( i = 0; i < 18; i++ )
123 {
124 for( j = 0; j < 48; j++ )
125 {
126 ctx->state[j] = (unsigned char)
127 ( ctx->state[j] ^ PI_SUBST[t] );
128 t = ctx->state[j];
129 }
130
131 t = (unsigned char)( t + i );
132 }
133
134 t = ctx->cksum[15];
135
136 for( i = 0; i < 16; i++ )
137 {
138 ctx->cksum[i] = (unsigned char)
139 ( ctx->cksum[i] ^ PI_SUBST[ctx->buffer[i] ^ t] );
140 t = ctx->cksum[i];
141 }
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100142
gabor-mezei-armd1c98fc2020-08-19 14:03:06 +0200143 /* Zeroise variables to clear sensitive data from memory. */
144 mbedtls_platform_zeroize( &t, sizeof( t ) );
145
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100146 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147}
Jaeden Amero041039f2018-02-19 15:28:08 +0000148
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200149#if !defined(MBEDTLS_DEPRECATED_REMOVED)
150void mbedtls_md2_process( mbedtls_md2_context *ctx )
151{
152 mbedtls_internal_md2_process( ctx );
153}
154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#endif /* !MBEDTLS_MD2_PROCESS_ALT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157/*
158 * MD2 process buffer
159 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100160int mbedtls_md2_update_ret( mbedtls_md2_context *ctx,
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100161 const unsigned char *input,
162 size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000163{
Janos Follath24eed8d2019-11-22 13:21:35 +0000164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000165 size_t fill;
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167 while( ilen > 0 )
168 {
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000169 if( ilen > 16 - ctx->left )
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 fill = 16 - ctx->left;
171 else
172 fill = ilen;
173
174 memcpy( ctx->buffer + ctx->left, input, fill );
175
176 ctx->left += fill;
177 input += fill;
178 ilen -= fill;
179
180 if( ctx->left == 16 )
181 {
182 ctx->left = 0;
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100183 if( ( ret = mbedtls_internal_md2_process( ctx ) ) != 0 )
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100184 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 }
186 }
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100187
188 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189}
190
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200191#if !defined(MBEDTLS_DEPRECATED_REMOVED)
192void mbedtls_md2_update( mbedtls_md2_context *ctx,
193 const unsigned char *input,
194 size_t ilen )
195{
196 mbedtls_md2_update_ret( ctx, input, ilen );
197}
198#endif
199
Paul Bakker5121ce52009-01-03 21:22:43 +0000200/*
201 * MD2 final digest
202 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100203int mbedtls_md2_finish_ret( mbedtls_md2_context *ctx,
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100204 unsigned char output[16] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
Janos Follath24eed8d2019-11-22 13:21:35 +0000206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +0000207 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 unsigned char x;
209
210 x = (unsigned char)( 16 - ctx->left );
211
212 for( i = ctx->left; i < 16; i++ )
213 ctx->buffer[i] = x;
214
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100215 if( ( ret = mbedtls_internal_md2_process( ctx ) ) != 0 )
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100216 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218 memcpy( ctx->buffer, ctx->cksum, 16 );
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100219 if( ( ret = mbedtls_internal_md2_process( ctx ) ) != 0 )
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100220 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222 memcpy( output, ctx->state, 16 );
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100223
224 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225}
226
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200227#if !defined(MBEDTLS_DEPRECATED_REMOVED)
228void mbedtls_md2_finish( mbedtls_md2_context *ctx,
229 unsigned char output[16] )
230{
231 mbedtls_md2_finish_ret( ctx, output );
232}
233#endif
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#endif /* !MBEDTLS_MD2_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200236
Paul Bakker5121ce52009-01-03 21:22:43 +0000237/*
238 * output = MD2( input buffer )
239 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100240int mbedtls_md2_ret( const unsigned char *input,
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100241 size_t ilen,
242 unsigned char output[16] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000243{
Janos Follath24eed8d2019-11-22 13:21:35 +0000244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_md2_context ctx;
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_md2_init( &ctx );
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100248
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100249 if( ( ret = mbedtls_md2_starts_ret( &ctx ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100250 goto exit;
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100251
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100252 if( ( ret = mbedtls_md2_update_ret( &ctx, input, ilen ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100253 goto exit;
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100254
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100255 if( ( ret = mbedtls_md2_finish_ret( &ctx, output ) ) != 0 )
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100256 goto exit;
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100257
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100258exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_md2_free( &ctx );
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100260
Andres Amaya Garcia0963e6c2017-07-20 14:34:08 +0100261 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262}
263
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200264#if !defined(MBEDTLS_DEPRECATED_REMOVED)
265void mbedtls_md2( const unsigned char *input,
266 size_t ilen,
267 unsigned char output[16] )
268{
269 mbedtls_md2_ret( input, ilen, output );
270}
271#endif
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275/*
276 * RFC 1319 test vectors
277 */
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100278static const unsigned char md2_test_str[7][81] =
Paul Bakker5121ce52009-01-03 21:22:43 +0000279{
280 { "" },
281 { "a" },
282 { "abc" },
283 { "message digest" },
284 { "abcdefghijklmnopqrstuvwxyz" },
285 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
Guido Vranken962e4ee2020-08-21 21:08:56 +0200286 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890" }
Paul Bakker5121ce52009-01-03 21:22:43 +0000287};
288
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100289static const size_t md2_test_strlen[7] =
290{
291 0, 1, 3, 14, 26, 62, 80
292};
293
Paul Bakker5121ce52009-01-03 21:22:43 +0000294static const unsigned char md2_test_sum[7][16] =
295{
296 { 0x83, 0x50, 0xE5, 0xA3, 0xE2, 0x4C, 0x15, 0x3D,
297 0xF2, 0x27, 0x5C, 0x9F, 0x80, 0x69, 0x27, 0x73 },
298 { 0x32, 0xEC, 0x01, 0xEC, 0x4A, 0x6D, 0xAC, 0x72,
299 0xC0, 0xAB, 0x96, 0xFB, 0x34, 0xC0, 0xB5, 0xD1 },
300 { 0xDA, 0x85, 0x3B, 0x0D, 0x3F, 0x88, 0xD9, 0x9B,
301 0x30, 0x28, 0x3A, 0x69, 0xE6, 0xDE, 0xD6, 0xBB },
302 { 0xAB, 0x4F, 0x49, 0x6B, 0xFB, 0x2A, 0x53, 0x0B,
303 0x21, 0x9F, 0xF3, 0x30, 0x31, 0xFE, 0x06, 0xB0 },
304 { 0x4E, 0x8D, 0xDF, 0xF3, 0x65, 0x02, 0x92, 0xAB,
305 0x5A, 0x41, 0x08, 0xC3, 0xAA, 0x47, 0x94, 0x0B },
306 { 0xDA, 0x33, 0xDE, 0xF2, 0xA4, 0x2D, 0xF1, 0x39,
307 0x75, 0x35, 0x28, 0x46, 0xC3, 0x03, 0x38, 0xCD },
308 { 0xD5, 0x97, 0x6F, 0x79, 0xD8, 0x3D, 0x3A, 0x0D,
309 0xC9, 0x80, 0x6C, 0x3C, 0x66, 0xF3, 0xEF, 0xD8 }
310};
311
312/*
313 * Checkup routine
314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315int mbedtls_md2_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000316{
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100317 int i, ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 unsigned char md2sum[16];
319
320 for( i = 0; i < 7; i++ )
321 {
322 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_printf( " MD2 test #%d: ", i + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100325 ret = mbedtls_md2_ret( md2_test_str[i], md2_test_strlen[i], md2sum );
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100326 if( ret != 0 )
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100327 goto fail;
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329 if( memcmp( md2sum, md2_test_sum[i], 16 ) != 0 )
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100330 {
331 ret = 1;
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100332 goto fail;
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100333 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
335 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_printf( "passed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 }
338
339 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342 return( 0 );
Andres Amaya Garcia1d852132017-04-28 16:21:40 +0100343
344fail:
345 if( verbose != 0 )
346 mbedtls_printf( "failed\n" );
347
Andres Amaya Garcia2d0aa8b2017-07-21 14:57:26 +0100348 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349}
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* MBEDTLS_MD2_C */