Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
md_wrap.c
00001 /** 00002 * \file md_wrap.c 00003 * 00004 * \brief Generic message digest wrapper for mbed TLS 00005 * 00006 * \author Adriaan de Jong <dejong@fox-it.com> 00007 * 00008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00009 * SPDX-License-Identifier: Apache-2.0 00010 * 00011 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00012 * not use this file except in compliance with the License. 00013 * You may obtain a copy of the License at 00014 * 00015 * http://www.apache.org/licenses/LICENSE-2.0 00016 * 00017 * Unless required by applicable law or agreed to in writing, software 00018 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00019 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 * See the License for the specific language governing permissions and 00021 * limitations under the License. 00022 * 00023 * This file is part of mbed TLS (https://tls.mbed.org) 00024 */ 00025 00026 #if !defined(MBEDTLS_CONFIG_FILE) 00027 #include "mbedtls/config.h" 00028 #else 00029 #include MBEDTLS_CONFIG_FILE 00030 #endif 00031 00032 #if defined(MBEDTLS_MD_C) 00033 00034 #include "mbedtls/md_internal.h" 00035 00036 #if defined(MBEDTLS_MD2_C) 00037 #include "mbedtls/md2.h" 00038 #endif 00039 00040 #if defined(MBEDTLS_MD4_C) 00041 #include "mbedtls/md4.h" 00042 #endif 00043 00044 #if defined(MBEDTLS_MD5_C) 00045 #include "mbedtls/md5.h" 00046 #endif 00047 00048 #if defined(MBEDTLS_RIPEMD160_C) 00049 #include "mbedtls/ripemd160.h" 00050 #endif 00051 00052 #if defined(MBEDTLS_SHA1_C) 00053 #include "mbedtls/sha1.h" 00054 #endif 00055 00056 #if defined(MBEDTLS_SHA256_C) 00057 #include "mbedtls/sha256.h" 00058 #endif 00059 00060 #if defined(MBEDTLS_SHA512_C) 00061 #include "mbedtls/sha512.h" 00062 #endif 00063 00064 #if defined(MBEDTLS_PLATFORM_C) 00065 #include "mbedtls/platform.h" 00066 #else 00067 #include <stdlib.h> 00068 #define mbedtls_calloc calloc 00069 #define mbedtls_free free 00070 #endif 00071 00072 #if defined(MBEDTLS_MD2_C) 00073 00074 static int md2_starts_wrap( void *ctx ) 00075 { 00076 return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) ); 00077 } 00078 00079 static int md2_update_wrap( void *ctx, const unsigned char *input, 00080 size_t ilen ) 00081 { 00082 return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) ); 00083 } 00084 00085 static int md2_finish_wrap( void *ctx, unsigned char *output ) 00086 { 00087 return( mbedtls_md2_finish_ret( (mbedtls_md2_context *) ctx, output ) ); 00088 } 00089 00090 static void *md2_ctx_alloc( void ) 00091 { 00092 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) ); 00093 00094 if( ctx != NULL ) 00095 mbedtls_md2_init( (mbedtls_md2_context *) ctx ); 00096 00097 return( ctx ); 00098 } 00099 00100 static void md2_ctx_free( void *ctx ) 00101 { 00102 mbedtls_md2_free( (mbedtls_md2_context *) ctx ); 00103 mbedtls_free( ctx ); 00104 } 00105 00106 static void md2_clone_wrap( void *dst, const void *src ) 00107 { 00108 mbedtls_md2_clone( (mbedtls_md2_context *) dst, 00109 (const mbedtls_md2_context *) src ); 00110 } 00111 00112 static int md2_process_wrap( void *ctx, const unsigned char *data ) 00113 { 00114 ((void) data); 00115 00116 return( mbedtls_internal_md2_process( (mbedtls_md2_context *) ctx ) ); 00117 } 00118 00119 const mbedtls_md_info_t mbedtls_md2_info = { 00120 MBEDTLS_MD_MD2, 00121 "MD2", 00122 16, 00123 16, 00124 md2_starts_wrap, 00125 md2_update_wrap, 00126 md2_finish_wrap, 00127 mbedtls_md2_ret, 00128 md2_ctx_alloc, 00129 md2_ctx_free, 00130 md2_clone_wrap, 00131 md2_process_wrap, 00132 }; 00133 00134 #endif /* MBEDTLS_MD2_C */ 00135 00136 #if defined(MBEDTLS_MD4_C) 00137 00138 static int md4_starts_wrap( void *ctx ) 00139 { 00140 return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) ); 00141 } 00142 00143 static int md4_update_wrap( void *ctx, const unsigned char *input, 00144 size_t ilen ) 00145 { 00146 return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) ); 00147 } 00148 00149 static int md4_finish_wrap( void *ctx, unsigned char *output ) 00150 { 00151 return( mbedtls_md4_finish_ret( (mbedtls_md4_context *) ctx, output ) ); 00152 } 00153 00154 static void *md4_ctx_alloc( void ) 00155 { 00156 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) ); 00157 00158 if( ctx != NULL ) 00159 mbedtls_md4_init( (mbedtls_md4_context *) ctx ); 00160 00161 return( ctx ); 00162 } 00163 00164 static void md4_ctx_free( void *ctx ) 00165 { 00166 mbedtls_md4_free( (mbedtls_md4_context *) ctx ); 00167 mbedtls_free( ctx ); 00168 } 00169 00170 static void md4_clone_wrap( void *dst, const void *src ) 00171 { 00172 mbedtls_md4_clone( (mbedtls_md4_context *) dst, 00173 (const mbedtls_md4_context *) src ); 00174 } 00175 00176 static int md4_process_wrap( void *ctx, const unsigned char *data ) 00177 { 00178 return( mbedtls_internal_md4_process( (mbedtls_md4_context *) ctx, data ) ); 00179 } 00180 00181 const mbedtls_md_info_t mbedtls_md4_info = { 00182 MBEDTLS_MD_MD4, 00183 "MD4", 00184 16, 00185 64, 00186 md4_starts_wrap, 00187 md4_update_wrap, 00188 md4_finish_wrap, 00189 mbedtls_md4_ret, 00190 md4_ctx_alloc, 00191 md4_ctx_free, 00192 md4_clone_wrap, 00193 md4_process_wrap, 00194 }; 00195 00196 #endif /* MBEDTLS_MD4_C */ 00197 00198 #if defined(MBEDTLS_MD5_C) 00199 00200 static int md5_starts_wrap( void *ctx ) 00201 { 00202 return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) ); 00203 } 00204 00205 static int md5_update_wrap( void *ctx, const unsigned char *input, 00206 size_t ilen ) 00207 { 00208 return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) ); 00209 } 00210 00211 static int md5_finish_wrap( void *ctx, unsigned char *output ) 00212 { 00213 return( mbedtls_md5_finish_ret( (mbedtls_md5_context *) ctx, output ) ); 00214 } 00215 00216 static void *md5_ctx_alloc( void ) 00217 { 00218 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) ); 00219 00220 if( ctx != NULL ) 00221 mbedtls_md5_init( (mbedtls_md5_context *) ctx ); 00222 00223 return( ctx ); 00224 } 00225 00226 static void md5_ctx_free( void *ctx ) 00227 { 00228 mbedtls_md5_free( (mbedtls_md5_context *) ctx ); 00229 mbedtls_free( ctx ); 00230 } 00231 00232 static void md5_clone_wrap( void *dst, const void *src ) 00233 { 00234 mbedtls_md5_clone( (mbedtls_md5_context *) dst, 00235 (const mbedtls_md5_context *) src ); 00236 } 00237 00238 static int md5_process_wrap( void *ctx, const unsigned char *data ) 00239 { 00240 return( mbedtls_internal_md5_process( (mbedtls_md5_context *) ctx, data ) ); 00241 } 00242 00243 const mbedtls_md_info_t mbedtls_md5_info = { 00244 MBEDTLS_MD_MD5, 00245 "MD5", 00246 16, 00247 64, 00248 md5_starts_wrap, 00249 md5_update_wrap, 00250 md5_finish_wrap, 00251 mbedtls_md5_ret, 00252 md5_ctx_alloc, 00253 md5_ctx_free, 00254 md5_clone_wrap, 00255 md5_process_wrap, 00256 }; 00257 00258 #endif /* MBEDTLS_MD5_C */ 00259 00260 #if defined(MBEDTLS_RIPEMD160_C) 00261 00262 static int ripemd160_starts_wrap( void *ctx ) 00263 { 00264 return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) ); 00265 } 00266 00267 static int ripemd160_update_wrap( void *ctx, const unsigned char *input, 00268 size_t ilen ) 00269 { 00270 return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx, 00271 input, ilen ) ); 00272 } 00273 00274 static int ripemd160_finish_wrap( void *ctx, unsigned char *output ) 00275 { 00276 return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx, 00277 output ) ); 00278 } 00279 00280 static void *ripemd160_ctx_alloc( void ) 00281 { 00282 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) ); 00283 00284 if( ctx != NULL ) 00285 mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx ); 00286 00287 return( ctx ); 00288 } 00289 00290 static void ripemd160_ctx_free( void *ctx ) 00291 { 00292 mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx ); 00293 mbedtls_free( ctx ); 00294 } 00295 00296 static void ripemd160_clone_wrap( void *dst, const void *src ) 00297 { 00298 mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst, 00299 (const mbedtls_ripemd160_context *) src ); 00300 } 00301 00302 static int ripemd160_process_wrap( void *ctx, const unsigned char *data ) 00303 { 00304 return( mbedtls_internal_ripemd160_process( 00305 (mbedtls_ripemd160_context *) ctx, data ) ); 00306 } 00307 00308 const mbedtls_md_info_t mbedtls_ripemd160_info = { 00309 MBEDTLS_MD_RIPEMD160, 00310 "RIPEMD160", 00311 20, 00312 64, 00313 ripemd160_starts_wrap, 00314 ripemd160_update_wrap, 00315 ripemd160_finish_wrap, 00316 mbedtls_ripemd160_ret, 00317 ripemd160_ctx_alloc, 00318 ripemd160_ctx_free, 00319 ripemd160_clone_wrap, 00320 ripemd160_process_wrap, 00321 }; 00322 00323 #endif /* MBEDTLS_RIPEMD160_C */ 00324 00325 #if defined(MBEDTLS_SHA1_C) 00326 00327 static int sha1_starts_wrap( void *ctx ) 00328 { 00329 return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) ); 00330 } 00331 00332 static int sha1_update_wrap( void *ctx, const unsigned char *input, 00333 size_t ilen ) 00334 { 00335 return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx, 00336 input, ilen ) ); 00337 } 00338 00339 static int sha1_finish_wrap( void *ctx, unsigned char *output ) 00340 { 00341 return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) ); 00342 } 00343 00344 static void *sha1_ctx_alloc( void ) 00345 { 00346 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) ); 00347 00348 if( ctx != NULL ) 00349 mbedtls_sha1_init( (mbedtls_sha1_context *) ctx ); 00350 00351 return( ctx ); 00352 } 00353 00354 static void sha1_clone_wrap( void *dst, const void *src ) 00355 { 00356 mbedtls_sha1_clone( (mbedtls_sha1_context *) dst, 00357 (const mbedtls_sha1_context *) src ); 00358 } 00359 00360 static void sha1_ctx_free( void *ctx ) 00361 { 00362 mbedtls_sha1_free( (mbedtls_sha1_context *) ctx ); 00363 mbedtls_free( ctx ); 00364 } 00365 00366 static int sha1_process_wrap( void *ctx, const unsigned char *data ) 00367 { 00368 return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx, 00369 data ) ); 00370 } 00371 00372 const mbedtls_md_info_t mbedtls_sha1_info = { 00373 MBEDTLS_MD_SHA1, 00374 "SHA1", 00375 20, 00376 64, 00377 sha1_starts_wrap, 00378 sha1_update_wrap, 00379 sha1_finish_wrap, 00380 mbedtls_sha1_ret, 00381 sha1_ctx_alloc, 00382 sha1_ctx_free, 00383 sha1_clone_wrap, 00384 sha1_process_wrap, 00385 }; 00386 00387 #endif /* MBEDTLS_SHA1_C */ 00388 00389 /* 00390 * Wrappers for generic message digests 00391 */ 00392 #if defined(MBEDTLS_SHA256_C) 00393 00394 static int sha224_starts_wrap( void *ctx ) 00395 { 00396 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) ); 00397 } 00398 00399 static int sha224_update_wrap( void *ctx, const unsigned char *input, 00400 size_t ilen ) 00401 { 00402 return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx, 00403 input, ilen ) ); 00404 } 00405 00406 static int sha224_finish_wrap( void *ctx, unsigned char *output ) 00407 { 00408 return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx, 00409 output ) ); 00410 } 00411 00412 static int sha224_wrap( const unsigned char *input, size_t ilen, 00413 unsigned char *output ) 00414 { 00415 return( mbedtls_sha256_ret( input, ilen, output, 1 ) ); 00416 } 00417 00418 static void *sha224_ctx_alloc( void ) 00419 { 00420 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) ); 00421 00422 if( ctx != NULL ) 00423 mbedtls_sha256_init( (mbedtls_sha256_context *) ctx ); 00424 00425 return( ctx ); 00426 } 00427 00428 static void sha224_ctx_free( void *ctx ) 00429 { 00430 mbedtls_sha256_free( (mbedtls_sha256_context *) ctx ); 00431 mbedtls_free( ctx ); 00432 } 00433 00434 static void sha224_clone_wrap( void *dst, const void *src ) 00435 { 00436 mbedtls_sha256_clone( (mbedtls_sha256_context *) dst, 00437 (const mbedtls_sha256_context *) src ); 00438 } 00439 00440 static int sha224_process_wrap( void *ctx, const unsigned char *data ) 00441 { 00442 return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx, 00443 data ) ); 00444 } 00445 00446 const mbedtls_md_info_t mbedtls_sha224_info = { 00447 MBEDTLS_MD_SHA224, 00448 "SHA224", 00449 28, 00450 64, 00451 sha224_starts_wrap, 00452 sha224_update_wrap, 00453 sha224_finish_wrap, 00454 sha224_wrap, 00455 sha224_ctx_alloc, 00456 sha224_ctx_free, 00457 sha224_clone_wrap, 00458 sha224_process_wrap, 00459 }; 00460 00461 static int sha256_starts_wrap( void *ctx ) 00462 { 00463 return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) ); 00464 } 00465 00466 static int sha256_wrap( const unsigned char *input, size_t ilen, 00467 unsigned char *output ) 00468 { 00469 return( mbedtls_sha256_ret( input, ilen, output, 0 ) ); 00470 } 00471 00472 const mbedtls_md_info_t mbedtls_sha256_info = { 00473 MBEDTLS_MD_SHA256, 00474 "SHA256", 00475 32, 00476 64, 00477 sha256_starts_wrap, 00478 sha224_update_wrap, 00479 sha224_finish_wrap, 00480 sha256_wrap, 00481 sha224_ctx_alloc, 00482 sha224_ctx_free, 00483 sha224_clone_wrap, 00484 sha224_process_wrap, 00485 }; 00486 00487 #endif /* MBEDTLS_SHA256_C */ 00488 00489 #if defined(MBEDTLS_SHA512_C) 00490 00491 static int sha384_starts_wrap( void *ctx ) 00492 { 00493 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) ); 00494 } 00495 00496 static int sha384_update_wrap( void *ctx, const unsigned char *input, 00497 size_t ilen ) 00498 { 00499 return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx, 00500 input, ilen ) ); 00501 } 00502 00503 static int sha384_finish_wrap( void *ctx, unsigned char *output ) 00504 { 00505 return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx, 00506 output ) ); 00507 } 00508 00509 static int sha384_wrap( const unsigned char *input, size_t ilen, 00510 unsigned char *output ) 00511 { 00512 return( mbedtls_sha512_ret( input, ilen, output, 1 ) ); 00513 } 00514 00515 static void *sha384_ctx_alloc( void ) 00516 { 00517 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) ); 00518 00519 if( ctx != NULL ) 00520 mbedtls_sha512_init( (mbedtls_sha512_context *) ctx ); 00521 00522 return( ctx ); 00523 } 00524 00525 static void sha384_ctx_free( void *ctx ) 00526 { 00527 mbedtls_sha512_free( (mbedtls_sha512_context *) ctx ); 00528 mbedtls_free( ctx ); 00529 } 00530 00531 static void sha384_clone_wrap( void *dst, const void *src ) 00532 { 00533 mbedtls_sha512_clone( (mbedtls_sha512_context *) dst, 00534 (const mbedtls_sha512_context *) src ); 00535 } 00536 00537 static int sha384_process_wrap( void *ctx, const unsigned char *data ) 00538 { 00539 return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx, 00540 data ) ); 00541 } 00542 00543 const mbedtls_md_info_t mbedtls_sha384_info = { 00544 MBEDTLS_MD_SHA384, 00545 "SHA384", 00546 48, 00547 128, 00548 sha384_starts_wrap, 00549 sha384_update_wrap, 00550 sha384_finish_wrap, 00551 sha384_wrap, 00552 sha384_ctx_alloc, 00553 sha384_ctx_free, 00554 sha384_clone_wrap, 00555 sha384_process_wrap, 00556 }; 00557 00558 static int sha512_starts_wrap( void *ctx ) 00559 { 00560 return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) ); 00561 } 00562 00563 static int sha512_wrap( const unsigned char *input, size_t ilen, 00564 unsigned char *output ) 00565 { 00566 return( mbedtls_sha512_ret( input, ilen, output, 0 ) ); 00567 } 00568 00569 const mbedtls_md_info_t mbedtls_sha512_info = { 00570 MBEDTLS_MD_SHA512, 00571 "SHA512", 00572 64, 00573 128, 00574 sha512_starts_wrap, 00575 sha384_update_wrap, 00576 sha384_finish_wrap, 00577 sha512_wrap, 00578 sha384_ctx_alloc, 00579 sha384_ctx_free, 00580 sha384_clone_wrap, 00581 sha384_process_wrap, 00582 }; 00583 00584 #endif /* MBEDTLS_SHA512_C */ 00585 00586 #endif /* MBEDTLS_MD_C */
Generated on Tue Jul 12 2022 12:45:31 by
