md_wrap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 int md2_starts_wrap( void *ctx )
  62. {
  63. return( mbedtls_md2_starts_ret( (mbedtls_md2_context *) ctx ) );
  64. }
  65. static int md2_update_wrap( void *ctx, const unsigned char *input,
  66. size_t ilen )
  67. {
  68. return( mbedtls_md2_update_ret( (mbedtls_md2_context *) ctx, input, ilen ) );
  69. }
  70. static int md2_finish_wrap( void *ctx, unsigned char *output )
  71. {
  72. return( mbedtls_md2_finish_ret( (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 int md2_process_wrap( void *ctx, const unsigned char *data )
  92. {
  93. ((void) data);
  94. return( mbedtls_internal_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_ret,
  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 int md4_starts_wrap( void *ctx )
  113. {
  114. return( mbedtls_md4_starts_ret( (mbedtls_md4_context *) ctx ) );
  115. }
  116. static int md4_update_wrap( void *ctx, const unsigned char *input,
  117. size_t ilen )
  118. {
  119. return( mbedtls_md4_update_ret( (mbedtls_md4_context *) ctx, input, ilen ) );
  120. }
  121. static int md4_finish_wrap( void *ctx, unsigned char *output )
  122. {
  123. return( mbedtls_md4_finish_ret( (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 int md4_process_wrap( void *ctx, const unsigned char *data )
  143. {
  144. return( mbedtls_internal_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_ret,
  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 int md5_starts_wrap( void *ctx )
  163. {
  164. return( mbedtls_md5_starts_ret( (mbedtls_md5_context *) ctx ) );
  165. }
  166. static int md5_update_wrap( void *ctx, const unsigned char *input,
  167. size_t ilen )
  168. {
  169. return( mbedtls_md5_update_ret( (mbedtls_md5_context *) ctx, input, ilen ) );
  170. }
  171. static int md5_finish_wrap( void *ctx, unsigned char *output )
  172. {
  173. return( mbedtls_md5_finish_ret( (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 int md5_process_wrap( void *ctx, const unsigned char *data )
  193. {
  194. return( mbedtls_internal_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_ret,
  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 int ripemd160_starts_wrap( void *ctx )
  213. {
  214. return( mbedtls_ripemd160_starts_ret( (mbedtls_ripemd160_context *) ctx ) );
  215. }
  216. static int ripemd160_update_wrap( void *ctx, const unsigned char *input,
  217. size_t ilen )
  218. {
  219. return( mbedtls_ripemd160_update_ret( (mbedtls_ripemd160_context *) ctx,
  220. input, ilen ) );
  221. }
  222. static int ripemd160_finish_wrap( void *ctx, unsigned char *output )
  223. {
  224. return( mbedtls_ripemd160_finish_ret( (mbedtls_ripemd160_context *) ctx,
  225. 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 int ripemd160_process_wrap( void *ctx, const unsigned char *data )
  245. {
  246. return( mbedtls_internal_ripemd160_process(
  247. (mbedtls_ripemd160_context *) ctx, data ) );
  248. }
  249. const mbedtls_md_info_t mbedtls_ripemd160_info = {
  250. MBEDTLS_MD_RIPEMD160,
  251. "RIPEMD160",
  252. 20,
  253. 64,
  254. ripemd160_starts_wrap,
  255. ripemd160_update_wrap,
  256. ripemd160_finish_wrap,
  257. mbedtls_ripemd160_ret,
  258. ripemd160_ctx_alloc,
  259. ripemd160_ctx_free,
  260. ripemd160_clone_wrap,
  261. ripemd160_process_wrap,
  262. };
  263. #endif /* MBEDTLS_RIPEMD160_C */
  264. #if defined(MBEDTLS_SHA1_C)
  265. static int sha1_starts_wrap( void *ctx )
  266. {
  267. return( mbedtls_sha1_starts_ret( (mbedtls_sha1_context *) ctx ) );
  268. }
  269. static int sha1_update_wrap( void *ctx, const unsigned char *input,
  270. size_t ilen )
  271. {
  272. return( mbedtls_sha1_update_ret( (mbedtls_sha1_context *) ctx,
  273. input, ilen ) );
  274. }
  275. static int sha1_finish_wrap( void *ctx, unsigned char *output )
  276. {
  277. return( mbedtls_sha1_finish_ret( (mbedtls_sha1_context *) ctx, output ) );
  278. }
  279. static void *sha1_ctx_alloc( void )
  280. {
  281. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha1_context ) );
  282. if( ctx != NULL )
  283. mbedtls_sha1_init( (mbedtls_sha1_context *) ctx );
  284. return( ctx );
  285. }
  286. static void sha1_clone_wrap( void *dst, const void *src )
  287. {
  288. mbedtls_sha1_clone( (mbedtls_sha1_context *) dst,
  289. (const mbedtls_sha1_context *) src );
  290. }
  291. static void sha1_ctx_free( void *ctx )
  292. {
  293. mbedtls_sha1_free( (mbedtls_sha1_context *) ctx );
  294. mbedtls_free( ctx );
  295. }
  296. static int sha1_process_wrap( void *ctx, const unsigned char *data )
  297. {
  298. return( mbedtls_internal_sha1_process( (mbedtls_sha1_context *) ctx,
  299. data ) );
  300. }
  301. const mbedtls_md_info_t mbedtls_sha1_info = {
  302. MBEDTLS_MD_SHA1,
  303. "SHA1",
  304. 20,
  305. 64,
  306. sha1_starts_wrap,
  307. sha1_update_wrap,
  308. sha1_finish_wrap,
  309. mbedtls_sha1_ret,
  310. sha1_ctx_alloc,
  311. sha1_ctx_free,
  312. sha1_clone_wrap,
  313. sha1_process_wrap,
  314. };
  315. #endif /* MBEDTLS_SHA1_C */
  316. /*
  317. * Wrappers for generic message digests
  318. */
  319. #if defined(MBEDTLS_SHA256_C)
  320. static int sha224_starts_wrap( void *ctx )
  321. {
  322. return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 1 ) );
  323. }
  324. static int sha224_update_wrap( void *ctx, const unsigned char *input,
  325. size_t ilen )
  326. {
  327. return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
  328. input, ilen ) );
  329. }
  330. static int sha224_finish_wrap( void *ctx, unsigned char *output )
  331. {
  332. return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
  333. output ) );
  334. }
  335. static int sha224_wrap( const unsigned char *input, size_t ilen,
  336. unsigned char *output )
  337. {
  338. return( mbedtls_sha256_ret( input, ilen, output, 1 ) );
  339. }
  340. static void *sha224_ctx_alloc( void )
  341. {
  342. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
  343. if( ctx != NULL )
  344. mbedtls_sha256_init( (mbedtls_sha256_context *) ctx );
  345. return( ctx );
  346. }
  347. static void sha224_ctx_free( void *ctx )
  348. {
  349. mbedtls_sha256_free( (mbedtls_sha256_context *) ctx );
  350. mbedtls_free( ctx );
  351. }
  352. static void sha224_clone_wrap( void *dst, const void *src )
  353. {
  354. mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
  355. (const mbedtls_sha256_context *) src );
  356. }
  357. static int sha224_process_wrap( void *ctx, const unsigned char *data )
  358. {
  359. return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
  360. data ) );
  361. }
  362. const mbedtls_md_info_t mbedtls_sha224_info = {
  363. MBEDTLS_MD_SHA224,
  364. "SHA224",
  365. 28,
  366. 64,
  367. sha224_starts_wrap,
  368. sha224_update_wrap,
  369. sha224_finish_wrap,
  370. sha224_wrap,
  371. sha224_ctx_alloc,
  372. sha224_ctx_free,
  373. sha224_clone_wrap,
  374. sha224_process_wrap,
  375. };
  376. static int sha256_starts_wrap( void *ctx )
  377. {
  378. return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
  379. }
  380. static int sha256_wrap( const unsigned char *input, size_t ilen,
  381. unsigned char *output )
  382. {
  383. return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
  384. }
  385. const mbedtls_md_info_t mbedtls_sha256_info = {
  386. MBEDTLS_MD_SHA256,
  387. "SHA256",
  388. 32,
  389. 64,
  390. sha256_starts_wrap,
  391. sha224_update_wrap,
  392. sha224_finish_wrap,
  393. sha256_wrap,
  394. sha224_ctx_alloc,
  395. sha224_ctx_free,
  396. sha224_clone_wrap,
  397. sha224_process_wrap,
  398. };
  399. #endif /* MBEDTLS_SHA256_C */
  400. #if defined(MBEDTLS_SHA512_C)
  401. static int sha384_starts_wrap( void *ctx )
  402. {
  403. return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 1 ) );
  404. }
  405. static int sha384_update_wrap( void *ctx, const unsigned char *input,
  406. size_t ilen )
  407. {
  408. return( mbedtls_sha512_update_ret( (mbedtls_sha512_context *) ctx,
  409. input, ilen ) );
  410. }
  411. static int sha384_finish_wrap( void *ctx, unsigned char *output )
  412. {
  413. return( mbedtls_sha512_finish_ret( (mbedtls_sha512_context *) ctx,
  414. output ) );
  415. }
  416. static int sha384_wrap( const unsigned char *input, size_t ilen,
  417. unsigned char *output )
  418. {
  419. return( mbedtls_sha512_ret( input, ilen, output, 1 ) );
  420. }
  421. static void *sha384_ctx_alloc( void )
  422. {
  423. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
  424. if( ctx != NULL )
  425. mbedtls_sha512_init( (mbedtls_sha512_context *) ctx );
  426. return( ctx );
  427. }
  428. static void sha384_ctx_free( void *ctx )
  429. {
  430. mbedtls_sha512_free( (mbedtls_sha512_context *) ctx );
  431. mbedtls_free( ctx );
  432. }
  433. static void sha384_clone_wrap( void *dst, const void *src )
  434. {
  435. mbedtls_sha512_clone( (mbedtls_sha512_context *) dst,
  436. (const mbedtls_sha512_context *) src );
  437. }
  438. static int sha384_process_wrap( void *ctx, const unsigned char *data )
  439. {
  440. return( mbedtls_internal_sha512_process( (mbedtls_sha512_context *) ctx,
  441. data ) );
  442. }
  443. const mbedtls_md_info_t mbedtls_sha384_info = {
  444. MBEDTLS_MD_SHA384,
  445. "SHA384",
  446. 48,
  447. 128,
  448. sha384_starts_wrap,
  449. sha384_update_wrap,
  450. sha384_finish_wrap,
  451. sha384_wrap,
  452. sha384_ctx_alloc,
  453. sha384_ctx_free,
  454. sha384_clone_wrap,
  455. sha384_process_wrap,
  456. };
  457. static int sha512_starts_wrap( void *ctx )
  458. {
  459. return( mbedtls_sha512_starts_ret( (mbedtls_sha512_context *) ctx, 0 ) );
  460. }
  461. static int sha512_wrap( const unsigned char *input, size_t ilen,
  462. unsigned char *output )
  463. {
  464. return( mbedtls_sha512_ret( input, ilen, output, 0 ) );
  465. }
  466. const mbedtls_md_info_t mbedtls_sha512_info = {
  467. MBEDTLS_MD_SHA512,
  468. "SHA512",
  469. 64,
  470. 128,
  471. sha512_starts_wrap,
  472. sha384_update_wrap,
  473. sha384_finish_wrap,
  474. sha512_wrap,
  475. sha384_ctx_alloc,
  476. sha384_ctx_free,
  477. sha384_clone_wrap,
  478. sha384_process_wrap,
  479. };
  480. #endif /* MBEDTLS_SHA512_C */
  481. #endif /* MBEDTLS_MD_C */