md_wrap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "mbedtls/config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #if defined(MBEDTLS_MD_C)
  33. #include "mbedtls/md_internal.h"
  34. #if defined(MBEDTLS_MD2_C)
  35. #include "mbedtls/md2.h"
  36. #endif
  37. #if defined(MBEDTLS_MD4_C)
  38. #include "mbedtls/md4.h"
  39. #endif
  40. #if defined(MBEDTLS_MD5_C)
  41. #include "mbedtls/md5.h"
  42. #endif
  43. #if defined(MBEDTLS_RIPEMD160_C)
  44. #include "mbedtls/ripemd160.h"
  45. #endif
  46. #if defined(MBEDTLS_SHA1_C)
  47. #include "mbedtls/sha1.h"
  48. #endif
  49. #if defined(MBEDTLS_SHA256_C)
  50. #include "mbedtls/sha256.h"
  51. #endif
  52. #if defined(MBEDTLS_SHA512_C)
  53. #include "mbedtls/sha512.h"
  54. #endif
  55. #if defined(MBEDTLS_PLATFORM_C)
  56. #include "mbedtls/platform.h"
  57. #else
  58. #include <stdlib.h>
  59. #define mbedtls_calloc calloc
  60. #define mbedtls_free free
  61. #endif
  62. #if defined(MBEDTLS_MD2_C)
  63. static void md2_starts_wrap( void *ctx )
  64. {
  65. mbedtls_md2_starts( (mbedtls_md2_context *) ctx );
  66. }
  67. static void md2_update_wrap( void *ctx, const unsigned char *input,
  68. size_t ilen )
  69. {
  70. mbedtls_md2_update( (mbedtls_md2_context *) ctx, input, ilen );
  71. }
  72. static void md2_finish_wrap( void *ctx, unsigned char *output )
  73. {
  74. mbedtls_md2_finish( (mbedtls_md2_context *) ctx, output );
  75. }
  76. static void *md2_ctx_alloc( void )
  77. {
  78. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
  79. if( ctx != NULL )
  80. mbedtls_md2_init( (mbedtls_md2_context *) ctx );
  81. return( ctx );
  82. }
  83. static void md2_ctx_free( void *ctx )
  84. {
  85. mbedtls_md2_free( (mbedtls_md2_context *) ctx );
  86. mbedtls_free( ctx );
  87. }
  88. static void md2_clone_wrap( void *dst, const void *src )
  89. {
  90. mbedtls_md2_clone( (mbedtls_md2_context *) dst,
  91. (const mbedtls_md2_context *) src );
  92. }
  93. static void md2_process_wrap( void *ctx, const unsigned char *data )
  94. {
  95. ((void) data);
  96. mbedtls_md2_process( (mbedtls_md2_context *) ctx );
  97. }
  98. const mbedtls_md_info_t mbedtls_md2_info = {
  99. MBEDTLS_MD_MD2,
  100. "MD2",
  101. 16,
  102. 16,
  103. md2_starts_wrap,
  104. md2_update_wrap,
  105. md2_finish_wrap,
  106. mbedtls_md2,
  107. md2_ctx_alloc,
  108. md2_ctx_free,
  109. md2_clone_wrap,
  110. md2_process_wrap,
  111. };
  112. #endif /* MBEDTLS_MD2_C */
  113. #if defined(MBEDTLS_MD4_C)
  114. static void md4_starts_wrap( void *ctx )
  115. {
  116. mbedtls_md4_starts( (mbedtls_md4_context *) ctx );
  117. }
  118. static void md4_update_wrap( void *ctx, const unsigned char *input,
  119. size_t ilen )
  120. {
  121. mbedtls_md4_update( (mbedtls_md4_context *) ctx, input, ilen );
  122. }
  123. static void md4_finish_wrap( void *ctx, unsigned char *output )
  124. {
  125. mbedtls_md4_finish( (mbedtls_md4_context *) ctx, output );
  126. }
  127. static void *md4_ctx_alloc( void )
  128. {
  129. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
  130. if( ctx != NULL )
  131. mbedtls_md4_init( (mbedtls_md4_context *) ctx );
  132. return( ctx );
  133. }
  134. static void md4_ctx_free( void *ctx )
  135. {
  136. mbedtls_md4_free( (mbedtls_md4_context *) ctx );
  137. mbedtls_free( ctx );
  138. }
  139. static void md4_clone_wrap( void *dst, const void *src )
  140. {
  141. mbedtls_md4_clone( (mbedtls_md4_context *) dst,
  142. (const mbedtls_md4_context *) src );
  143. }
  144. static void md4_process_wrap( void *ctx, const unsigned char *data )
  145. {
  146. mbedtls_md4_process( (mbedtls_md4_context *) ctx, data );
  147. }
  148. const mbedtls_md_info_t mbedtls_md4_info = {
  149. MBEDTLS_MD_MD4,
  150. "MD4",
  151. 16,
  152. 64,
  153. md4_starts_wrap,
  154. md4_update_wrap,
  155. md4_finish_wrap,
  156. mbedtls_md4,
  157. md4_ctx_alloc,
  158. md4_ctx_free,
  159. md4_clone_wrap,
  160. md4_process_wrap,
  161. };
  162. #endif /* MBEDTLS_MD4_C */
  163. #if defined(MBEDTLS_MD5_C)
  164. static void md5_starts_wrap( void *ctx )
  165. {
  166. mbedtls_md5_starts( (mbedtls_md5_context *) ctx );
  167. }
  168. static void md5_update_wrap( void *ctx, const unsigned char *input,
  169. size_t ilen )
  170. {
  171. mbedtls_md5_update( (mbedtls_md5_context *) ctx, input, ilen );
  172. }
  173. static void md5_finish_wrap( void *ctx, unsigned char *output )
  174. {
  175. mbedtls_md5_finish( (mbedtls_md5_context *) ctx, output );
  176. }
  177. static void *md5_ctx_alloc( void )
  178. {
  179. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
  180. if( ctx != NULL )
  181. mbedtls_md5_init( (mbedtls_md5_context *) ctx );
  182. return( ctx );
  183. }
  184. static void md5_ctx_free( void *ctx )
  185. {
  186. mbedtls_md5_free( (mbedtls_md5_context *) ctx );
  187. mbedtls_free( ctx );
  188. }
  189. static void md5_clone_wrap( void *dst, const void *src )
  190. {
  191. mbedtls_md5_clone( (mbedtls_md5_context *) dst,
  192. (const mbedtls_md5_context *) src );
  193. }
  194. static void md5_process_wrap( void *ctx, const unsigned char *data )
  195. {
  196. mbedtls_md5_process( (mbedtls_md5_context *) ctx, data );
  197. }
  198. const mbedtls_md_info_t mbedtls_md5_info = {
  199. MBEDTLS_MD_MD5,
  200. "MD5",
  201. 16,
  202. 64,
  203. md5_starts_wrap,
  204. md5_update_wrap,
  205. md5_finish_wrap,
  206. mbedtls_md5,
  207. md5_ctx_alloc,
  208. md5_ctx_free,
  209. md5_clone_wrap,
  210. md5_process_wrap,
  211. };
  212. #endif /* MBEDTLS_MD5_C */
  213. #if defined(MBEDTLS_RIPEMD160_C)
  214. static void ripemd160_starts_wrap( void *ctx )
  215. {
  216. mbedtls_ripemd160_starts( (mbedtls_ripemd160_context *) ctx );
  217. }
  218. static void ripemd160_update_wrap( void *ctx, const unsigned char *input,
  219. size_t ilen )
  220. {
  221. mbedtls_ripemd160_update( (mbedtls_ripemd160_context *) ctx, input, ilen );
  222. }
  223. static void ripemd160_finish_wrap( void *ctx, unsigned char *output )
  224. {
  225. mbedtls_ripemd160_finish( (mbedtls_ripemd160_context *) ctx, output );
  226. }
  227. static void *ripemd160_ctx_alloc( void )
  228. {
  229. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ripemd160_context ) );
  230. if( ctx != NULL )
  231. mbedtls_ripemd160_init( (mbedtls_ripemd160_context *) ctx );
  232. return( ctx );
  233. }
  234. static void ripemd160_ctx_free( void *ctx )
  235. {
  236. mbedtls_ripemd160_free( (mbedtls_ripemd160_context *) ctx );
  237. mbedtls_free( ctx );
  238. }
  239. static void ripemd160_clone_wrap( void *dst, const void *src )
  240. {
  241. mbedtls_ripemd160_clone( (mbedtls_ripemd160_context *) dst,
  242. (const mbedtls_ripemd160_context *) src );
  243. }
  244. static void ripemd160_process_wrap( void *ctx, const unsigned char *data )
  245. {
  246. mbedtls_ripemd160_process( (mbedtls_ripemd160_context *) ctx, data );
  247. }
  248. const mbedtls_md_info_t mbedtls_ripemd160_info = {
  249. MBEDTLS_MD_RIPEMD160,
  250. "RIPEMD160",
  251. 20,
  252. 64,
  253. ripemd160_starts_wrap,
  254. ripemd160_update_wrap,
  255. ripemd160_finish_wrap,
  256. mbedtls_ripemd160,
  257. ripemd160_ctx_alloc,
  258. ripemd160_ctx_free,
  259. ripemd160_clone_wrap,
  260. ripemd160_process_wrap,
  261. };
  262. #endif /* MBEDTLS_RIPEMD160_C */
  263. #if defined(MBEDTLS_SHA1_C)
  264. static void sha1_starts_wrap( void *ctx )
  265. {
  266. mbedtls_sha1_starts( (mbedtls_sha1_context *) ctx );
  267. }
  268. static void sha1_update_wrap( void *ctx, const unsigned char *input,
  269. size_t ilen )
  270. {
  271. mbedtls_sha1_update( (mbedtls_sha1_context *) ctx, input, ilen );
  272. }
  273. static void sha1_finish_wrap( void *ctx, unsigned char *output )
  274. {
  275. mbedtls_sha1_finish( (mbedtls_sha1_context *) ctx, output );
  276. }
  277. static void *sha1_ctx_alloc( void )
  278. {
  279. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
  280. if( ctx != NULL )
  281. mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
  282. return( ctx );
  283. }
  284. static void sha1_clone_wrap( void *dst, const void *src )
  285. {
  286. mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
  287. (const mbedtls_sha1_context *) src );
  288. }
  289. static void sha1_ctx_free( void *ctx )
  290. {
  291. mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
  292. mbedtls_free( ctx );
  293. }
  294. static void sha1_process_wrap( void *ctx, const unsigned char *data )
  295. {
  296. mbedtls_sha1_process( (mbedtls_sha1_context *) ctx, data );
  297. }
  298. const mbedtls_md_info_t mbedtls_sha1_info = {
  299. MBEDTLS_MD_SHA1,
  300. "SHA1",
  301. 20,
  302. 64,
  303. sha1_starts_wrap,
  304. sha1_update_wrap,
  305. sha1_finish_wrap,
  306. mbedtls_sha1,
  307. sha1_ctx_alloc,
  308. sha1_ctx_free,
  309. sha1_clone_wrap,
  310. sha1_process_wrap,
  311. };
  312. #endif /* MBEDTLS_SHA1_C */
  313. /*
  314. * Wrappers for generic message digests
  315. */
  316. #if defined(MBEDTLS_SHA256_C)
  317. static void sha224_starts_wrap( void *ctx )
  318. {
  319. mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 1 );
  320. }
  321. static void sha224_update_wrap( void *ctx, const unsigned char *input,
  322. size_t ilen )
  323. {
  324. mbedtls_sha256_update( (mbedtls_sha256_context *) ctx, input, ilen );
  325. }
  326. static void sha224_finish_wrap( void *ctx, unsigned char *output )
  327. {
  328. mbedtls_sha256_finish( (mbedtls_sha256_context *) ctx, output );
  329. }
  330. static void sha224_wrap( const unsigned char *input, size_t ilen,
  331. unsigned char *output )
  332. {
  333. mbedtls_sha256( input, ilen, output, 1 );
  334. }
  335. static void *sha224_ctx_alloc( void )
  336. {
  337. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
  338. if( ctx != NULL )
  339. mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
  340. return( ctx );
  341. }
  342. static void sha224_ctx_free( void *ctx )
  343. {
  344. mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
  345. mbedtls_free( ctx );
  346. }
  347. static void sha224_clone_wrap( void *dst, const void *src )
  348. {
  349. mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
  350. (const mbedtls_sha256_context *) src );
  351. }
  352. static void sha224_process_wrap( void *ctx, const unsigned char *data )
  353. {
  354. mbedtls_sha256_process( (mbedtls_sha256_context *) ctx, data );
  355. }
  356. const mbedtls_md_info_t mbedtls_sha224_info = {
  357. MBEDTLS_MD_SHA224,
  358. "SHA224",
  359. 28,
  360. 64,
  361. sha224_starts_wrap,
  362. sha224_update_wrap,
  363. sha224_finish_wrap,
  364. sha224_wrap,
  365. sha224_ctx_alloc,
  366. sha224_ctx_free,
  367. sha224_clone_wrap,
  368. sha224_process_wrap,
  369. };
  370. static void sha256_starts_wrap( void *ctx )
  371. {
  372. mbedtls_sha256_starts( (mbedtls_sha256_context *) ctx, 0 );
  373. }
  374. static void sha256_wrap( const unsigned char *input, size_t ilen,
  375. unsigned char *output )
  376. {
  377. mbedtls_sha256( input, ilen, output, 0 );
  378. }
  379. const mbedtls_md_info_t mbedtls_sha256_info = {
  380. MBEDTLS_MD_SHA256,
  381. "SHA256",
  382. 32,
  383. 64,
  384. sha256_starts_wrap,
  385. sha224_update_wrap,
  386. sha224_finish_wrap,
  387. sha256_wrap,
  388. sha224_ctx_alloc,
  389. sha224_ctx_free,
  390. sha224_clone_wrap,
  391. sha224_process_wrap,
  392. };
  393. #endif /* MBEDTLS_SHA256_C */
  394. #if defined(MBEDTLS_SHA512_C)
  395. static void sha384_starts_wrap( void *ctx )
  396. {
  397. mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 1 );
  398. }
  399. static void sha384_update_wrap( void *ctx, const unsigned char *input,
  400. size_t ilen )
  401. {
  402. mbedtls_sha512_update( (mbedtls_sha512_context *) ctx, input, ilen );
  403. }
  404. static void sha384_finish_wrap( void *ctx, unsigned char *output )
  405. {
  406. mbedtls_sha512_finish( (mbedtls_sha512_context *) ctx, output );
  407. }
  408. static void sha384_wrap( const unsigned char *input, size_t ilen,
  409. unsigned char *output )
  410. {
  411. mbedtls_sha512( input, ilen, output, 1 );
  412. }
  413. static void *sha384_ctx_alloc( void )
  414. {
  415. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
  416. if( ctx != NULL )
  417. mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
  418. return( ctx );
  419. }
  420. static void sha384_ctx_free( void *ctx )
  421. {
  422. mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
  423. mbedtls_free( ctx );
  424. }
  425. static void sha384_clone_wrap( void *dst, const void *src )
  426. {
  427. mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
  428. (const mbedtls_sha512_context *) src );
  429. }
  430. static void sha384_process_wrap( void *ctx, const unsigned char *data )
  431. {
  432. mbedtls_sha512_process( (mbedtls_sha512_context *) ctx, data );
  433. }
  434. const mbedtls_md_info_t mbedtls_sha384_info = {
  435. MBEDTLS_MD_SHA384,
  436. "SHA384",
  437. 48,
  438. 128,
  439. sha384_starts_wrap,
  440. sha384_update_wrap,
  441. sha384_finish_wrap,
  442. sha384_wrap,
  443. sha384_ctx_alloc,
  444. sha384_ctx_free,
  445. sha384_clone_wrap,
  446. sha384_process_wrap,
  447. };
  448. static void sha512_starts_wrap( void *ctx )
  449. {
  450. mbedtls_sha512_starts( (mbedtls_sha512_context *) ctx, 0 );
  451. }
  452. static void sha512_wrap( const unsigned char *input, size_t ilen,
  453. unsigned char *output )
  454. {
  455. mbedtls_sha512( input, ilen, output, 0 );
  456. }
  457. const mbedtls_md_info_t mbedtls_sha512_info = {
  458. MBEDTLS_MD_SHA512,
  459. "SHA512",
  460. 64,
  461. 128,
  462. sha512_starts_wrap,
  463. sha384_update_wrap,
  464. sha384_finish_wrap,
  465. sha512_wrap,
  466. sha384_ctx_alloc,
  467. sha384_ctx_free,
  468. sha384_clone_wrap,
  469. sha384_process_wrap,
  470. };
  471. #endif /* MBEDTLS_SHA512_C */
  472. #endif /* MBEDTLS_MD_C */