md_wrap.c 15 KB

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