mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers md_wrap.c Source File

md_wrap.c

Go to the documentation of this file.
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 void md2_starts_wrap( void *ctx )
00075 {
00076     mbedtls_md2_starts( (mbedtls_md2_context *) ctx );
00077 }
00078 
00079 static void md2_update_wrap( void *ctx, const unsigned char *input,
00080                              size_t ilen )
00081 {
00082     mbedtls_md2_update( (mbedtls_md2_context *) ctx, input, ilen );
00083 }
00084 
00085 static void md2_finish_wrap( void *ctx, unsigned char *output )
00086 {
00087     mbedtls_md2_finish( (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 void md2_process_wrap( void *ctx, const unsigned char *data )
00113 {
00114     ((void) data);
00115 
00116     mbedtls_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,
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 void md4_starts_wrap( void *ctx )
00139 {
00140     mbedtls_md4_starts( (mbedtls_md4_context *) ctx );
00141 }
00142 
00143 static void md4_update_wrap( void *ctx, const unsigned char *input,
00144                              size_t ilen )
00145 {
00146     mbedtls_md4_update( (mbedtls_md4_context *) ctx, input, ilen );
00147 }
00148 
00149 static void md4_finish_wrap( void *ctx, unsigned char *output )
00150 {
00151     mbedtls_md4_finish( (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 void md4_process_wrap( void *ctx, const unsigned char *data )
00177 {
00178     mbedtls_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,
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 void md5_starts_wrap( void *ctx )
00201 {
00202     mbedtls_md5_starts( (mbedtls_md5_context *) ctx );
00203 }
00204 
00205 static void md5_update_wrap( void *ctx, const unsigned char *input,
00206                              size_t ilen )
00207 {
00208     mbedtls_md5_update( (mbedtls_md5_context *) ctx, input, ilen );
00209 }
00210 
00211 static void md5_finish_wrap( void *ctx, unsigned char *output )
00212 {
00213     mbedtls_md5_finish( (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 void md5_process_wrap( void *ctx, const unsigned char *data )
00239 {
00240     mbedtls_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,
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 void ripemd160_starts_wrap( void *ctx )
00263 {
00264     mbedtls_ripemd160_starts( (mbedtls_ripemd160_context *) ctx );
00265 }
00266 
00267 static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
00268                                    size_t ilen )
00269 {
00270     mbedtls_ripemd160_update( (mbedtls_ripemd160_context *) ctx, input, ilen );
00271 }
00272 
00273 static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
00274 {
00275     mbedtls_ripemd160_finish( (mbedtls_ripemd160_context *) ctx, output );
00276 }
00277 
00278 static void *ripemd160_ctx_alloc( void )
00279 {
00280     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
00281 
00282     if( ctx != NULL )
00283         mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
00284 
00285     return( ctx );
00286 }
00287 
00288 static void ripemd160_ctx_free( void *ctx )
00289 {
00290     mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
00291     mbedtls_free( ctx );
00292 }
00293 
00294 static void ripemd160_clone_wrap( void *dst, const void *src )
00295 {
00296     mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
00297                        (const mbedtls_ripemd160_context *) src );
00298 }
00299 
00300 static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
00301 {
00302     mbedtls_ripemd160_process( (mbedtls_ripemd160_context *) ctx, data );
00303 }
00304 
00305 const mbedtls_md_info_t mbedtls_ripemd160_info = {
00306     MBEDTLS_MD_RIPEMD160,
00307     "RIPEMD160",
00308     20,
00309     64,
00310     ripemd160_starts_wrap,
00311     ripemd160_update_wrap,
00312     ripemd160_finish_wrap,
00313     mbedtls_ripemd160,
00314     ripemd160_ctx_alloc,
00315     ripemd160_ctx_free,
00316     ripemd160_clone_wrap,
00317     ripemd160_process_wrap,
00318 };
00319 
00320 #endif /* MBEDTLS_RIPEMD160_C */
00321 
00322 #if defined(MBEDTLS_SHA1_C)
00323 
00324 static void sha1_starts_wrap( void *ctx )
00325 {
00326     mbedtls_sha1_starts( (mbedtls_sha1_context *) ctx );
00327 }
00328 
00329 static void sha1_update_wrap( void *ctx, const unsigned char *input,
00330                               size_t ilen )
00331 {
00332     mbedtls_sha1_update( (mbedtls_sha1_context *) ctx, input, ilen );
00333 }
00334 
00335 static void sha1_finish_wrap( void *ctx, unsigned char *output )
00336 {
00337     mbedtls_sha1_finish( (mbedtls_sha1_context *) ctx, output );
00338 }
00339 
00340 static void *sha1_ctx_alloc( void )
00341 {
00342     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
00343 
00344     if( ctx != NULL )
00345         mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
00346 
00347     return( ctx );
00348 }
00349 
00350 static void sha1_clone_wrap( void *dst, const void *src )
00351 {
00352     mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
00353                   (const mbedtls_sha1_context *) src );
00354 }
00355 
00356 static void sha1_ctx_free( void *ctx )
00357 {
00358     mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
00359     mbedtls_free( ctx );
00360 }
00361 
00362 static void sha1_process_wrap( void *ctx, const unsigned char *data )
00363 {
00364     mbedtls_sha1_process( (mbedtls_sha1_context *) ctx, data );
00365 }
00366 
00367 const mbedtls_md_info_t mbedtls_sha1_info = {
00368     MBEDTLS_MD_SHA1,
00369     "SHA1",
00370     20,
00371     64,
00372     sha1_starts_wrap,
00373     sha1_update_wrap,
00374     sha1_finish_wrap,
00375     mbedtls_sha1,
00376     sha1_ctx_alloc,
00377     sha1_ctx_free,
00378     sha1_clone_wrap,
00379     sha1_process_wrap,
00380 };
00381 
00382 #endif /* MBEDTLS_SHA1_C */
00383 
00384 /*
00385  * Wrappers for generic message digests
00386  */
00387 #if defined(MBEDTLS_SHA256_C)
00388 
00389 static void sha224_starts_wrap( void *ctx )
00390 {
00391     mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 1 );
00392 }
00393 
00394 static void sha224_update_wrap( void *ctx, const unsigned char *input,
00395                                 size_t ilen )
00396 {
00397     mbedtls_sha256_update( (mbedtls_sha256_context *) ctx, input, ilen );
00398 }
00399 
00400 static void sha224_finish_wrap( void *ctx, unsigned char *output )
00401 {
00402     mbedtls_sha256_finish( (mbedtls_sha256_context *) ctx, output );
00403 }
00404 
00405 static void sha224_wrap( const unsigned char *input, size_t ilen,
00406                     unsigned char *output )
00407 {
00408     mbedtls_sha256( input, ilen, output, 1 );
00409 }
00410 
00411 static void *sha224_ctx_alloc( void )
00412 {
00413     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
00414 
00415     if( ctx != NULL )
00416         mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
00417 
00418     return( ctx );
00419 }
00420 
00421 static void sha224_ctx_free( void *ctx )
00422 {
00423     mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
00424     mbedtls_free( ctx );
00425 }
00426 
00427 static void sha224_clone_wrap( void *dst, const void *src )
00428 {
00429     mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
00430                     (const mbedtls_sha256_context *) src );
00431 }
00432 
00433 static void sha224_process_wrap( void *ctx, const unsigned char *data )
00434 {
00435     mbedtls_sha256_process( (mbedtls_sha256_context *) ctx, data );
00436 }
00437 
00438 const mbedtls_md_info_t mbedtls_sha224_info = {
00439     MBEDTLS_MD_SHA224,
00440     "SHA224",
00441     28,
00442     64,
00443     sha224_starts_wrap,
00444     sha224_update_wrap,
00445     sha224_finish_wrap,
00446     sha224_wrap,
00447     sha224_ctx_alloc,
00448     sha224_ctx_free,
00449     sha224_clone_wrap,
00450     sha224_process_wrap,
00451 };
00452 
00453 static void sha256_starts_wrap( void *ctx )
00454 {
00455     mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 0 );
00456 }
00457 
00458 static void sha256_wrap( const unsigned char *input, size_t ilen,
00459                     unsigned char *output )
00460 {
00461     mbedtls_sha256( input, ilen, output, 0 );
00462 }
00463 
00464 const mbedtls_md_info_t mbedtls_sha256_info = {
00465     MBEDTLS_MD_SHA256,
00466     "SHA256",
00467     32,
00468     64,
00469     sha256_starts_wrap,
00470     sha224_update_wrap,
00471     sha224_finish_wrap,
00472     sha256_wrap,
00473     sha224_ctx_alloc,
00474     sha224_ctx_free,
00475     sha224_clone_wrap,
00476     sha224_process_wrap,
00477 };
00478 
00479 #endif /* MBEDTLS_SHA256_C */
00480 
00481 #if defined(MBEDTLS_SHA512_C)
00482 
00483 static void sha384_starts_wrap( void *ctx )
00484 {
00485     mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 1 );
00486 }
00487 
00488 static void sha384_update_wrap( void *ctx, const unsigned char *input,
00489                                 size_t ilen )
00490 {
00491     mbedtls_sha512_update( (mbedtls_sha512_context *) ctx, input, ilen );
00492 }
00493 
00494 static void sha384_finish_wrap( void *ctx, unsigned char *output )
00495 {
00496     mbedtls_sha512_finish( (mbedtls_sha512_context *) ctx, output );
00497 }
00498 
00499 static void sha384_wrap( const unsigned char *input, size_t ilen,
00500                     unsigned char *output )
00501 {
00502     mbedtls_sha512( input, ilen, output, 1 );
00503 }
00504 
00505 static void *sha384_ctx_alloc( void )
00506 {
00507     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
00508 
00509     if( ctx != NULL )
00510         mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
00511 
00512     return( ctx );
00513 }
00514 
00515 static void sha384_ctx_free( void *ctx )
00516 {
00517     mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
00518     mbedtls_free( ctx );
00519 }
00520 
00521 static void sha384_clone_wrap( void *dst, const void *src )
00522 {
00523     mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
00524                     (const mbedtls_sha512_context *) src );
00525 }
00526 
00527 static void sha384_process_wrap( void *ctx, const unsigned char *data )
00528 {
00529     mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
00530 }
00531 
00532 const mbedtls_md_info_t mbedtls_sha384_info = {
00533     MBEDTLS_MD_SHA384,
00534     "SHA384",
00535     48,
00536     128,
00537     sha384_starts_wrap,
00538     sha384_update_wrap,
00539     sha384_finish_wrap,
00540     sha384_wrap,
00541     sha384_ctx_alloc,
00542     sha384_ctx_free,
00543     sha384_clone_wrap,
00544     sha384_process_wrap,
00545 };
00546 
00547 static void sha512_starts_wrap( void *ctx )
00548 {
00549     mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 0 );
00550 }
00551 
00552 static void sha512_wrap( const unsigned char *input, size_t ilen,
00553                     unsigned char *output )
00554 {
00555     mbedtls_sha512( input, ilen, output, 0 );
00556 }
00557 
00558 const mbedtls_md_info_t mbedtls_sha512_info = {
00559     MBEDTLS_MD_SHA512,
00560     "SHA512",
00561     64,
00562     128,
00563     sha512_starts_wrap,
00564     sha384_update_wrap,
00565     sha384_finish_wrap,
00566     sha512_wrap,
00567     sha384_ctx_alloc,
00568     sha384_ctx_free,
00569     sha384_clone_wrap,
00570     sha384_process_wrap,
00571 };
00572 
00573 #endif /* MBEDTLS_SHA512_C */
00574 
00575 #endif /* MBEDTLS_MD_C */