md_wrap.c 12 KB

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