1 /**
2  * \file md_wrap.c
3  *
4  * \brief Generic message digest wrapper for mbed TLS
5  *
6  * \author Adriaan de Jong <dejong@fox-it.com>
7  *
8  *  Copyright The Mbed TLS Contributors
9  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10  *
11  *  This file is provided under the Apache License 2.0, or the
12  *  GNU General Public License v2.0 or later.
13  *
14  *  **********
15  *  Apache License 2.0:
16  *
17  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
18  *  not use this file except in compliance with the License.
19  *  You may obtain a copy of the License at
20  *
21  *  http://www.apache.org/licenses/LICENSE-2.0
22  *
23  *  Unless required by applicable law or agreed to in writing, software
24  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  *  See the License for the specific language governing permissions and
27  *  limitations under the License.
28  *
29  *  **********
30  *
31  *  **********
32  *  GNU General Public License v2.0 or later:
33  *
34  *  This program is free software; you can redistribute it and/or modify
35  *  it under the terms of the GNU General Public License as published by
36  *  the Free Software Foundation; either version 2 of the License, or
37  *  (at your option) any later version.
38  *
39  *  This program is distributed in the hope that it will be useful,
40  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
41  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42  *  GNU General Public License for more details.
43  *
44  *  You should have received a copy of the GNU General Public License along
45  *  with this program; if not, write to the Free Software Foundation, Inc.,
46  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47  *
48  *  **********
49  */
50 
51 #if !defined(MBEDTLS_CONFIG_FILE)
52 #include "mbedtls/config.h"
53 #else
54 #include MBEDTLS_CONFIG_FILE
55 #endif
56 
57 #if defined(MBEDTLS_MD_C)
58 
59 #include "mbedtls/md_internal.h"
60 
61 #if defined(MBEDTLS_MD2_C)
62 #include "mbedtls/md2.h"
63 #endif
64 
65 #if defined(MBEDTLS_MD4_C)
66 #include "mbedtls/md4.h"
67 #endif
68 
69 #if defined(MBEDTLS_MD5_C)
70 #include "mbedtls/md5.h"
71 #endif
72 
73 #if defined(MBEDTLS_RIPEMD160_C)
74 #include "mbedtls/ripemd160.h"
75 #endif
76 
77 #if defined(MBEDTLS_SHA1_C)
78 #include "mbedtls/sha1.h"
79 #endif
80 
81 #if defined(MBEDTLS_SHA256_C)
82 #include "mbedtls/sha256.h"
83 #endif
84 
85 #if defined(MBEDTLS_SHA512_C)
86 #include "mbedtls/sha512.h"
87 #endif
88 
89 #if defined(MBEDTLS_PLATFORM_C)
90 #include "mbedtls/platform.h"
91 #else
92 #include <stdlib.h>
93 #define mbedtls_calloc    calloc
94 #define mbedtls_free       free
95 #endif
96 
97 #if defined(MBEDTLS_MD2_C)
98 
md2_starts_wrap(void * ctx)99 static int md2_starts_wrap( void *ctx )
100 {
101     return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
102 }
103 
md2_update_wrap(void * ctx,const unsigned char * input,size_t ilen)104 static int md2_update_wrap( void *ctx, const unsigned char *input,
105                              size_t ilen )
106 {
107     return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
108 }
109 
md2_finish_wrap(void * ctx,unsigned char * output)110 static int md2_finish_wrap( void *ctx, unsigned char *output )
111 {
112     return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) );
113 }
114 
md2_ctx_alloc(void)115 static void *md2_ctx_alloc( void )
116 {
117     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
118 
119     if( ctx != NULL )
120         mbedtls_md2_init( (mbedtls_md2_context *) ctx );
121 
122     return( ctx );
123 }
124 
md2_ctx_free(void * ctx)125 static void md2_ctx_free( void *ctx )
126 {
127     mbedtls_md2_free( (mbedtls_md2_context *) ctx );
128     mbedtls_free( ctx );
129 }
130 
md2_clone_wrap(void * dst,const void * src)131 static void md2_clone_wrap( void *dst, const void *src )
132 {
133     mbedtls_md2_clone( (mbedtls_md2_context *) dst,
134                  (const mbedtls_md2_context *) src );
135 }
136 
md2_process_wrap(void * ctx,const unsigned char * data)137 static int md2_process_wrap( void *ctx, const unsigned char *data )
138 {
139     ((void) data);
140 
141     return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) );
142 }
143 
144 const mbedtls_md_info_t mbedtls_md2_info = {
145     MBEDTLS_MD_MD2,
146     "MD2",
147     16,
148     16,
149     md2_starts_wrap,
150     md2_update_wrap,
151     md2_finish_wrap,
152     mbedtls_md2_ret,
153     md2_ctx_alloc,
154     md2_ctx_free,
155     md2_clone_wrap,
156     md2_process_wrap,
157 };
158 
159 #endif /* MBEDTLS_MD2_C */
160 
161 #if defined(MBEDTLS_MD4_C)
162 
md4_starts_wrap(void * ctx)163 static int md4_starts_wrap( void *ctx )
164 {
165     return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
166 }
167 
md4_update_wrap(void * ctx,const unsigned char * input,size_t ilen)168 static int md4_update_wrap( void *ctx, const unsigned char *input,
169                              size_t ilen )
170 {
171     return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
172 }
173 
md4_finish_wrap(void * ctx,unsigned char * output)174 static int md4_finish_wrap( void *ctx, unsigned char *output )
175 {
176     return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) );
177 }
178 
md4_ctx_alloc(void)179 static void *md4_ctx_alloc( void )
180 {
181     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
182 
183     if( ctx != NULL )
184         mbedtls_md4_init( (mbedtls_md4_context *) ctx );
185 
186     return( ctx );
187 }
188 
md4_ctx_free(void * ctx)189 static void md4_ctx_free( void *ctx )
190 {
191     mbedtls_md4_free( (mbedtls_md4_context *) ctx );
192     mbedtls_free( ctx );
193 }
194 
md4_clone_wrap(void * dst,const void * src)195 static void md4_clone_wrap( void *dst, const void *src )
196 {
197     mbedtls_md4_clone( (mbedtls_md4_context *) dst,
198                        (const mbedtls_md4_context *) src );
199 }
200 
md4_process_wrap(void * ctx,const unsigned char * data)201 static int md4_process_wrap( void *ctx, const unsigned char *data )
202 {
203     return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) );
204 }
205 
206 const mbedtls_md_info_t mbedtls_md4_info = {
207     MBEDTLS_MD_MD4,
208     "MD4",
209     16,
210     64,
211     md4_starts_wrap,
212     md4_update_wrap,
213     md4_finish_wrap,
214     mbedtls_md4_ret,
215     md4_ctx_alloc,
216     md4_ctx_free,
217     md4_clone_wrap,
218     md4_process_wrap,
219 };
220 
221 #endif /* MBEDTLS_MD4_C */
222 
223 #if defined(MBEDTLS_MD5_C)
224 
md5_starts_wrap(void * ctx)225 static int md5_starts_wrap( void *ctx )
226 {
227     return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
228 }
229 
md5_update_wrap(void * ctx,const unsigned char * input,size_t ilen)230 static int md5_update_wrap( void *ctx, const unsigned char *input,
231                              size_t ilen )
232 {
233     return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
234 }
235 
md5_finish_wrap(void * ctx,unsigned char * output)236 static int md5_finish_wrap( void *ctx, unsigned char *output )
237 {
238     return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) );
239 }
240 
md5_ctx_alloc(void)241 static void *md5_ctx_alloc( void )
242 {
243     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
244 
245     if( ctx != NULL )
246         mbedtls_md5_init( (mbedtls_md5_context *) ctx );
247 
248     return( ctx );
249 }
250 
md5_ctx_free(void * ctx)251 static void md5_ctx_free( void *ctx )
252 {
253     mbedtls_md5_free( (mbedtls_md5_context *) ctx );
254     mbedtls_free( ctx );
255 }
256 
md5_clone_wrap(void * dst,const void * src)257 static void md5_clone_wrap( void *dst, const void *src )
258 {
259     mbedtls_md5_clone( (mbedtls_md5_context *) dst,
260                        (const mbedtls_md5_context *) src );
261 }
262 
md5_process_wrap(void * ctx,const unsigned char * data)263 static int md5_process_wrap( void *ctx, const unsigned char *data )
264 {
265     return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) );
266 }
267 
268 const mbedtls_md_info_t mbedtls_md5_info = {
269     MBEDTLS_MD_MD5,
270     "MD5",
271     16,
272     64,
273     md5_starts_wrap,
274     md5_update_wrap,
275     md5_finish_wrap,
276     mbedtls_md5_ret,
277     md5_ctx_alloc,
278     md5_ctx_free,
279     md5_clone_wrap,
280     md5_process_wrap,
281 };
282 
283 #endif /* MBEDTLS_MD5_C */
284 
285 #if defined(MBEDTLS_RIPEMD160_C)
286 
ripemd160_starts_wrap(void * ctx)287 static int ripemd160_starts_wrap( void *ctx )
288 {
289     return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
290 }
291 
ripemd160_update_wrap(void * ctx,const unsigned char * input,size_t ilen)292 static int ripemd160_update_wrap( void *ctx, const unsigned char *input,
293                                    size_t ilen )
294 {
295     return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
296                                           input, ilen ) );
297 }
298 
ripemd160_finish_wrap(void * ctx,unsigned char * output)299 static int ripemd160_finish_wrap( void *ctx, unsigned char *output )
300 {
301     return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
302                                           output ) );
303 }
304 
ripemd160_ctx_alloc(void)305 static void *ripemd160_ctx_alloc( void )
306 {
307     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
308 
309     if( ctx != NULL )
310         mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
311 
312     return( ctx );
313 }
314 
ripemd160_ctx_free(void * ctx)315 static void ripemd160_ctx_free( void *ctx )
316 {
317     mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
318     mbedtls_free( ctx );
319 }
320 
ripemd160_clone_wrap(void * dst,const void * src)321 static void ripemd160_clone_wrap( void *dst, const void *src )
322 {
323     mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
324                        (const mbedtls_ripemd160_context *) src );
325 }
326 
ripemd160_process_wrap(void * ctx,const unsigned char * data)327 static int ripemd160_process_wrap( void *ctx, const unsigned char *data )
328 {
329     return( mbedtls_internal_ripemd160_process(
330                                 (mbedtls_ripemd160_context *) ctx, data ) );
331 }
332 
333 const mbedtls_md_info_t mbedtls_ripemd160_info = {
334     MBEDTLS_MD_RIPEMD160,
335     "RIPEMD160",
336     20,
337     64,
338     ripemd160_starts_wrap,
339     ripemd160_update_wrap,
340     ripemd160_finish_wrap,
341     mbedtls_ripemd160_ret,
342     ripemd160_ctx_alloc,
343     ripemd160_ctx_free,
344     ripemd160_clone_wrap,
345     ripemd160_process_wrap,
346 };
347 
348 #endif /* MBEDTLS_RIPEMD160_C */
349 
350 #if defined(MBEDTLS_SHA1_C)
351 
sha1_starts_wrap(void * ctx)352 static int sha1_starts_wrap( void *ctx )
353 {
354     return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
355 }
356 
sha1_update_wrap(void * ctx,const unsigned char * input,size_t ilen)357 static int sha1_update_wrap( void *ctx, const unsigned char *input,
358                               size_t ilen )
359 {
360     return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
361                                      input, ilen ) );
362 }
363 
sha1_finish_wrap(void * ctx,unsigned char * output)364 static int sha1_finish_wrap( void *ctx, unsigned char *output )
365 {
366     return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
367 }
368 
sha1_ctx_alloc(void)369 static void *sha1_ctx_alloc( void )
370 {
371     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
372 
373     if( ctx != NULL )
374         mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
375 
376     return( ctx );
377 }
378 
sha1_clone_wrap(void * dst,const void * src)379 static void sha1_clone_wrap( void *dst, const void *src )
380 {
381     mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
382                   (const mbedtls_sha1_context *) src );
383 }
384 
sha1_ctx_free(void * ctx)385 static void sha1_ctx_free( void *ctx )
386 {
387     mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
388     mbedtls_free( ctx );
389 }
390 
sha1_process_wrap(void * ctx,const unsigned char * data)391 static int sha1_process_wrap( void *ctx, const unsigned char *data )
392 {
393     return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
394                                            data ) );
395 }
396 
397 const mbedtls_md_info_t mbedtls_sha1_info = {
398     MBEDTLS_MD_SHA1,
399     "SHA1",
400     20,
401     64,
402     sha1_starts_wrap,
403     sha1_update_wrap,
404     sha1_finish_wrap,
405     mbedtls_sha1_ret,
406     sha1_ctx_alloc,
407     sha1_ctx_free,
408     sha1_clone_wrap,
409     sha1_process_wrap,
410 };
411 
412 #endif /* MBEDTLS_SHA1_C */
413 
414 /*
415  * Wrappers for generic message digests
416  */
417 #if defined(MBEDTLS_SHA256_C)
418 
sha224_starts_wrap(void * ctx)419 static int sha224_starts_wrap( void *ctx )
420 {
421     return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
422 }
423 
sha224_update_wrap(void * ctx,const unsigned char * input,size_t ilen)424 static int sha224_update_wrap( void *ctx, const unsigned char *input,
425                                 size_t ilen )
426 {
427     return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
428                                        input, ilen ) );
429 }
430 
sha224_finish_wrap(void * ctx,unsigned char * output)431 static int sha224_finish_wrap( void *ctx, unsigned char *output )
432 {
433     return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
434                                        output ) );
435 }
436 
sha224_wrap(const unsigned char * input,size_t ilen,unsigned char * output)437 static int sha224_wrap( const unsigned char *input, size_t ilen,
438                         unsigned char *output )
439 {
440     return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
441 }
442 
sha224_ctx_alloc(void)443 static void *sha224_ctx_alloc( void )
444 {
445     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
446 
447     if( ctx != NULL )
448         mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
449 
450     return( ctx );
451 }
452 
sha224_ctx_free(void * ctx)453 static void sha224_ctx_free( void *ctx )
454 {
455     mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
456     mbedtls_free( ctx );
457 }
458 
sha224_clone_wrap(void * dst,const void * src)459 static void sha224_clone_wrap( void *dst, const void *src )
460 {
461     mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
462                     (const mbedtls_sha256_context *) src );
463 }
464 
sha224_process_wrap(void * ctx,const unsigned char * data)465 static int sha224_process_wrap( void *ctx, const unsigned char *data )
466 {
467     return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
468                                              data ) );
469 }
470 
471 const mbedtls_md_info_t mbedtls_sha224_info = {
472     MBEDTLS_MD_SHA224,
473     "SHA224",
474     28,
475     64,
476     sha224_starts_wrap,
477     sha224_update_wrap,
478     sha224_finish_wrap,
479     sha224_wrap,
480     sha224_ctx_alloc,
481     sha224_ctx_free,
482     sha224_clone_wrap,
483     sha224_process_wrap,
484 };
485 
sha256_starts_wrap(void * ctx)486 static int sha256_starts_wrap( void *ctx )
487 {
488     return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
489 }
490 
sha256_wrap(const unsigned char * input,size_t ilen,unsigned char * output)491 static int sha256_wrap( const unsigned char *input, size_t ilen,
492                         unsigned char *output )
493 {
494     return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
495 }
496 
497 const mbedtls_md_info_t mbedtls_sha256_info = {
498     MBEDTLS_MD_SHA256,
499     "SHA256",
500     32,
501     64,
502     sha256_starts_wrap,
503     sha224_update_wrap,
504     sha224_finish_wrap,
505     sha256_wrap,
506     sha224_ctx_alloc,
507     sha224_ctx_free,
508     sha224_clone_wrap,
509     sha224_process_wrap,
510 };
511 
512 #endif /* MBEDTLS_SHA256_C */
513 
514 #if defined(MBEDTLS_SHA512_C)
515 
sha384_starts_wrap(void * ctx)516 static int sha384_starts_wrap( void *ctx )
517 {
518     return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
519 }
520 
sha384_update_wrap(void * ctx,const unsigned char * input,size_t ilen)521 static int sha384_update_wrap( void *ctx, const unsigned char *input,
522                                size_t ilen )
523 {
524     return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
525                                        input, ilen ) );
526 }
527 
sha384_finish_wrap(void * ctx,unsigned char * output)528 static int sha384_finish_wrap( void *ctx, unsigned char *output )
529 {
530     return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
531                                        output ) );
532 }
533 
sha384_wrap(const unsigned char * input,size_t ilen,unsigned char * output)534 static int sha384_wrap( const unsigned char *input, size_t ilen,
535                         unsigned char *output )
536 {
537     return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
538 }
539 
sha384_ctx_alloc(void)540 static void *sha384_ctx_alloc( void )
541 {
542     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
543 
544     if( ctx != NULL )
545         mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
546 
547     return( ctx );
548 }
549 
sha384_ctx_free(void * ctx)550 static void sha384_ctx_free( void *ctx )
551 {
552     mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
553     mbedtls_free( ctx );
554 }
555 
sha384_clone_wrap(void * dst,const void * src)556 static void sha384_clone_wrap( void *dst, const void *src )
557 {
558     mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
559                     (const mbedtls_sha512_context *) src );
560 }
561 
sha384_process_wrap(void * ctx,const unsigned char * data)562 static int sha384_process_wrap( void *ctx, const unsigned char *data )
563 {
564     return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
565                                              data ) );
566 }
567 
568 const mbedtls_md_info_t mbedtls_sha384_info = {
569     MBEDTLS_MD_SHA384,
570     "SHA384",
571     48,
572     128,
573     sha384_starts_wrap,
574     sha384_update_wrap,
575     sha384_finish_wrap,
576     sha384_wrap,
577     sha384_ctx_alloc,
578     sha384_ctx_free,
579     sha384_clone_wrap,
580     sha384_process_wrap,
581 };
582 
sha512_starts_wrap(void * ctx)583 static int sha512_starts_wrap( void *ctx )
584 {
585     return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
586 }
587 
sha512_wrap(const unsigned char * input,size_t ilen,unsigned char * output)588 static int sha512_wrap( const unsigned char *input, size_t ilen,
589                         unsigned char *output )
590 {
591     return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
592 }
593 
594 const mbedtls_md_info_t mbedtls_sha512_info = {
595     MBEDTLS_MD_SHA512,
596     "SHA512",
597     64,
598     128,
599     sha512_starts_wrap,
600     sha384_update_wrap,
601     sha384_finish_wrap,
602     sha512_wrap,
603     sha384_ctx_alloc,
604     sha384_ctx_free,
605     sha384_clone_wrap,
606     sha384_process_wrap,
607 };
608 
609 #endif /* MBEDTLS_SHA512_C */
610 
611 #endif /* MBEDTLS_MD_C */
612