Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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