md_wrap.c 13 KB

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