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