md_wrap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /**
  2. * \file md_wrap.c
  3. * \brief Generic message digest wrapper for PolarSSL
  4. *
  5. * \author Adriaan de Jong <dejong@fox-it.com>
  6. *
  7. * Copyright (C) 2006-2010, Brainspark B.V.
  8. *
  9. * This file is part of PolarSSL (http://www.polarssl.org)
  10. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  11. *
  12. * All rights reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  27. */
  28. #include "config.h"
  29. #if defined(POLARSSL_MD_C)
  30. #include "polarssl/md_wrap.h"
  31. #include "polarssl/md2.h"
  32. #include "polarssl/md4.h"
  33. #include "polarssl/md5.h"
  34. #include "polarssl/sha1.h"
  35. #include "polarssl/sha2.h"
  36. #include "polarssl/sha4.h"
  37. #include <stdlib.h>
  38. #if defined(POLARSSL_MD2_C)
  39. static void md2_starts_wrap( void *ctx )
  40. {
  41. md2_starts( (md2_context *) ctx );
  42. }
  43. static void md2_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  44. {
  45. md2_update( (md2_context *) ctx, input, ilen );
  46. }
  47. static void md2_finish_wrap( void *ctx, unsigned char *output )
  48. {
  49. md2_finish( (md2_context *) ctx, output );
  50. }
  51. int md2_file_wrap( const char *path, unsigned char *output )
  52. {
  53. #if defined(POLARSSL_FS_IO)
  54. return md2_file( path, output );
  55. #else
  56. ((void) path);
  57. ((void) output);
  58. return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
  59. #endif
  60. }
  61. static void md2_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
  62. {
  63. md2_hmac_starts( (md2_context *) ctx, key, keylen );
  64. }
  65. static void md2_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  66. {
  67. md2_hmac_update( (md2_context *) ctx, input, ilen );
  68. }
  69. static void md2_hmac_finish_wrap( void *ctx, unsigned char *output )
  70. {
  71. md2_hmac_finish( (md2_context *) ctx, output );
  72. }
  73. static void md2_hmac_reset_wrap( void *ctx )
  74. {
  75. md2_hmac_reset( (md2_context *) ctx );
  76. }
  77. static void * md2_ctx_alloc( void )
  78. {
  79. return malloc( sizeof( md2_context ) );
  80. }
  81. static void md2_ctx_free( void *ctx )
  82. {
  83. free( ctx );
  84. }
  85. const md_info_t md2_info = {
  86. POLARSSL_MD_MD2,
  87. "MD2",
  88. 16,
  89. md2_starts_wrap,
  90. md2_update_wrap,
  91. md2_finish_wrap,
  92. md2,
  93. md2_file_wrap,
  94. md2_hmac_starts_wrap,
  95. md2_hmac_update_wrap,
  96. md2_hmac_finish_wrap,
  97. md2_hmac_reset_wrap,
  98. md2_hmac,
  99. md2_ctx_alloc,
  100. md2_ctx_free,
  101. };
  102. #endif
  103. #if defined(POLARSSL_MD4_C)
  104. void md4_starts_wrap( void *ctx )
  105. {
  106. md4_starts( (md4_context *) ctx );
  107. }
  108. void md4_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  109. {
  110. md4_update( (md4_context *) ctx, input, ilen );
  111. }
  112. void md4_finish_wrap( void *ctx, unsigned char *output )
  113. {
  114. md4_finish( (md4_context *) ctx, output );
  115. }
  116. int md4_file_wrap( const char *path, unsigned char *output )
  117. {
  118. #if defined(POLARSSL_FS_IO)
  119. return md4_file( path, output );
  120. #else
  121. ((void) path);
  122. ((void) output);
  123. return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
  124. #endif
  125. }
  126. void md4_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
  127. {
  128. md4_hmac_starts( (md4_context *) ctx, key, keylen );
  129. }
  130. void md4_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  131. {
  132. md4_hmac_update( (md4_context *) ctx, input, ilen );
  133. }
  134. void md4_hmac_finish_wrap( void *ctx, unsigned char *output )
  135. {
  136. md4_hmac_finish( (md4_context *) ctx, output );
  137. }
  138. void md4_hmac_reset_wrap( void *ctx )
  139. {
  140. md4_hmac_reset( (md4_context *) ctx );
  141. }
  142. void *md4_ctx_alloc( void )
  143. {
  144. return malloc( sizeof( md4_context ) );
  145. }
  146. void md4_ctx_free( void *ctx )
  147. {
  148. free( ctx );
  149. }
  150. const md_info_t md4_info = {
  151. POLARSSL_MD_MD4,
  152. "MD4",
  153. 16,
  154. md4_starts_wrap,
  155. md4_update_wrap,
  156. md4_finish_wrap,
  157. md4,
  158. md4_file_wrap,
  159. md4_hmac_starts_wrap,
  160. md4_hmac_update_wrap,
  161. md4_hmac_finish_wrap,
  162. md4_hmac_reset_wrap,
  163. md4_hmac,
  164. md4_ctx_alloc,
  165. md4_ctx_free,
  166. };
  167. #endif
  168. #if defined(POLARSSL_MD5_C)
  169. static void md5_starts_wrap( void *ctx )
  170. {
  171. md5_starts( (md5_context *) ctx );
  172. }
  173. static void md5_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  174. {
  175. md5_update( (md5_context *) ctx, input, ilen );
  176. }
  177. static void md5_finish_wrap( void *ctx, unsigned char *output )
  178. {
  179. md5_finish( (md5_context *) ctx, output );
  180. }
  181. int md5_file_wrap( const char *path, unsigned char *output )
  182. {
  183. #if defined(POLARSSL_FS_IO)
  184. return md5_file( path, output );
  185. #else
  186. ((void) path);
  187. ((void) output);
  188. return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
  189. #endif
  190. }
  191. static void md5_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
  192. {
  193. md5_hmac_starts( (md5_context *) ctx, key, keylen );
  194. }
  195. static void md5_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  196. {
  197. md5_hmac_update( (md5_context *) ctx, input, ilen );
  198. }
  199. static void md5_hmac_finish_wrap( void *ctx, unsigned char *output )
  200. {
  201. md5_hmac_finish( (md5_context *) ctx, output );
  202. }
  203. static void md5_hmac_reset_wrap( void *ctx )
  204. {
  205. md5_hmac_reset( (md5_context *) ctx );
  206. }
  207. static void * md5_ctx_alloc( void )
  208. {
  209. return malloc( sizeof( md5_context ) );
  210. }
  211. static void md5_ctx_free( void *ctx )
  212. {
  213. free( ctx );
  214. }
  215. const md_info_t md5_info = {
  216. POLARSSL_MD_MD5,
  217. "MD5",
  218. 16,
  219. md5_starts_wrap,
  220. md5_update_wrap,
  221. md5_finish_wrap,
  222. md5,
  223. md5_file_wrap,
  224. md5_hmac_starts_wrap,
  225. md5_hmac_update_wrap,
  226. md5_hmac_finish_wrap,
  227. md5_hmac_reset_wrap,
  228. md5_hmac,
  229. md5_ctx_alloc,
  230. md5_ctx_free,
  231. };
  232. #endif
  233. #if defined(POLARSSL_SHA1_C)
  234. void sha1_starts_wrap( void *ctx )
  235. {
  236. sha1_starts( (sha1_context *) ctx );
  237. }
  238. void sha1_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  239. {
  240. sha1_update( (sha1_context *) ctx, input, ilen );
  241. }
  242. void sha1_finish_wrap( void *ctx, unsigned char *output )
  243. {
  244. sha1_finish( (sha1_context *) ctx, output );
  245. }
  246. int sha1_file_wrap( const char *path, unsigned char *output )
  247. {
  248. #if defined(POLARSSL_FS_IO)
  249. return sha1_file( path, output );
  250. #else
  251. ((void) path);
  252. ((void) output);
  253. return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
  254. #endif
  255. }
  256. void sha1_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
  257. {
  258. sha1_hmac_starts( (sha1_context *) ctx, key, keylen );
  259. }
  260. void sha1_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  261. {
  262. sha1_hmac_update( (sha1_context *) ctx, input, ilen );
  263. }
  264. void sha1_hmac_finish_wrap( void *ctx, unsigned char *output )
  265. {
  266. sha1_hmac_finish( (sha1_context *) ctx, output );
  267. }
  268. void sha1_hmac_reset_wrap( void *ctx )
  269. {
  270. sha1_hmac_reset( (sha1_context *) ctx );
  271. }
  272. void * sha1_ctx_alloc( void )
  273. {
  274. return malloc( sizeof( sha1_context ) );
  275. }
  276. void sha1_ctx_free( void *ctx )
  277. {
  278. free( ctx );
  279. }
  280. const md_info_t sha1_info = {
  281. POLARSSL_MD_SHA1,
  282. "SHA1",
  283. 20,
  284. sha1_starts_wrap,
  285. sha1_update_wrap,
  286. sha1_finish_wrap,
  287. sha1,
  288. sha1_file_wrap,
  289. sha1_hmac_starts_wrap,
  290. sha1_hmac_update_wrap,
  291. sha1_hmac_finish_wrap,
  292. sha1_hmac_reset_wrap,
  293. sha1_hmac,
  294. sha1_ctx_alloc,
  295. sha1_ctx_free,
  296. };
  297. #endif
  298. /*
  299. * Wrappers for generic message digests
  300. */
  301. #if defined(POLARSSL_SHA2_C)
  302. void sha224_starts_wrap( void *ctx )
  303. {
  304. sha2_starts( (sha2_context *) ctx, 1 );
  305. }
  306. void sha224_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  307. {
  308. sha2_update( (sha2_context *) ctx, input, ilen );
  309. }
  310. void sha224_finish_wrap( void *ctx, unsigned char *output )
  311. {
  312. sha2_finish( (sha2_context *) ctx, output );
  313. }
  314. void sha224_wrap( const unsigned char *input, size_t ilen,
  315. unsigned char *output )
  316. {
  317. sha2( input, ilen, output, 1 );
  318. }
  319. int sha224_file_wrap( const char *path, unsigned char *output )
  320. {
  321. #if defined(POLARSSL_FS_IO)
  322. return sha2_file( path, output, 1 );
  323. #else
  324. ((void) path);
  325. ((void) output);
  326. return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
  327. #endif
  328. }
  329. void sha224_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
  330. {
  331. sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 1 );
  332. }
  333. void sha224_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  334. {
  335. sha2_hmac_update( (sha2_context *) ctx, input, ilen );
  336. }
  337. void sha224_hmac_finish_wrap( void *ctx, unsigned char *output )
  338. {
  339. sha2_hmac_finish( (sha2_context *) ctx, output );
  340. }
  341. void sha224_hmac_reset_wrap( void *ctx )
  342. {
  343. sha2_hmac_reset( (sha2_context *) ctx );
  344. }
  345. void sha224_hmac_wrap( const unsigned char *key, size_t keylen,
  346. const unsigned char *input, size_t ilen,
  347. unsigned char *output )
  348. {
  349. sha2_hmac( key, keylen, input, ilen, output, 1 );
  350. }
  351. void * sha224_ctx_alloc( void )
  352. {
  353. return malloc( sizeof( sha2_context ) );
  354. }
  355. void sha224_ctx_free( void *ctx )
  356. {
  357. free( ctx );
  358. }
  359. const md_info_t sha224_info = {
  360. POLARSSL_MD_SHA224,
  361. "SHA224",
  362. 28,
  363. sha224_starts_wrap,
  364. sha224_update_wrap,
  365. sha224_finish_wrap,
  366. sha224_wrap,
  367. sha224_file_wrap,
  368. sha224_hmac_starts_wrap,
  369. sha224_hmac_update_wrap,
  370. sha224_hmac_finish_wrap,
  371. sha224_hmac_reset_wrap,
  372. sha224_hmac_wrap,
  373. sha224_ctx_alloc,
  374. sha224_ctx_free,
  375. };
  376. void sha256_starts_wrap( void *ctx )
  377. {
  378. sha2_starts( (sha2_context *) ctx, 0 );
  379. }
  380. void sha256_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  381. {
  382. sha2_update( (sha2_context *) ctx, input, ilen );
  383. }
  384. void sha256_finish_wrap( void *ctx, unsigned char *output )
  385. {
  386. sha2_finish( (sha2_context *) ctx, output );
  387. }
  388. void sha256_wrap( const unsigned char *input, size_t ilen,
  389. unsigned char *output )
  390. {
  391. sha2( input, ilen, output, 0 );
  392. }
  393. int sha256_file_wrap( const char *path, unsigned char *output )
  394. {
  395. #if defined(POLARSSL_FS_IO)
  396. return sha2_file( path, output, 0 );
  397. #else
  398. ((void) path);
  399. ((void) output);
  400. return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
  401. #endif
  402. }
  403. void sha256_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
  404. {
  405. sha2_hmac_starts( (sha2_context *) ctx, key, keylen, 0 );
  406. }
  407. void sha256_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  408. {
  409. sha2_hmac_update( (sha2_context *) ctx, input, ilen );
  410. }
  411. void sha256_hmac_finish_wrap( void *ctx, unsigned char *output )
  412. {
  413. sha2_hmac_finish( (sha2_context *) ctx, output );
  414. }
  415. void sha256_hmac_reset_wrap( void *ctx )
  416. {
  417. sha2_hmac_reset( (sha2_context *) ctx );
  418. }
  419. void sha256_hmac_wrap( const unsigned char *key, size_t keylen,
  420. const unsigned char *input, size_t ilen,
  421. unsigned char *output )
  422. {
  423. sha2_hmac( key, keylen, input, ilen, output, 0 );
  424. }
  425. void * sha256_ctx_alloc( void )
  426. {
  427. return malloc( sizeof( sha2_context ) );
  428. }
  429. void sha256_ctx_free( void *ctx )
  430. {
  431. free( ctx );
  432. }
  433. const md_info_t sha256_info = {
  434. POLARSSL_MD_SHA256,
  435. "SHA256",
  436. 32,
  437. sha256_starts_wrap,
  438. sha256_update_wrap,
  439. sha256_finish_wrap,
  440. sha256_wrap,
  441. sha256_file_wrap,
  442. sha256_hmac_starts_wrap,
  443. sha256_hmac_update_wrap,
  444. sha256_hmac_finish_wrap,
  445. sha256_hmac_reset_wrap,
  446. sha256_hmac_wrap,
  447. sha256_ctx_alloc,
  448. sha256_ctx_free,
  449. };
  450. #endif
  451. #if defined(POLARSSL_SHA4_C)
  452. void sha384_starts_wrap( void *ctx )
  453. {
  454. sha4_starts( (sha4_context *) ctx, 1 );
  455. }
  456. void sha384_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  457. {
  458. sha4_update( (sha4_context *) ctx, input, ilen );
  459. }
  460. void sha384_finish_wrap( void *ctx, unsigned char *output )
  461. {
  462. sha4_finish( (sha4_context *) ctx, output );
  463. }
  464. void sha384_wrap( const unsigned char *input, size_t ilen,
  465. unsigned char *output )
  466. {
  467. sha4( input, ilen, output, 1 );
  468. }
  469. int sha384_file_wrap( const char *path, unsigned char *output )
  470. {
  471. #if defined(POLARSSL_FS_IO)
  472. return sha4_file( path, output, 1 );
  473. #else
  474. ((void) path);
  475. ((void) output);
  476. return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
  477. #endif
  478. }
  479. void sha384_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
  480. {
  481. sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 1 );
  482. }
  483. void sha384_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  484. {
  485. sha4_hmac_update( (sha4_context *) ctx, input, ilen );
  486. }
  487. void sha384_hmac_finish_wrap( void *ctx, unsigned char *output )
  488. {
  489. sha4_hmac_finish( (sha4_context *) ctx, output );
  490. }
  491. void sha384_hmac_reset_wrap( void *ctx )
  492. {
  493. sha4_hmac_reset( (sha4_context *) ctx );
  494. }
  495. void sha384_hmac_wrap( const unsigned char *key, size_t keylen,
  496. const unsigned char *input, size_t ilen,
  497. unsigned char *output )
  498. {
  499. sha4_hmac( key, keylen, input, ilen, output, 1 );
  500. }
  501. void * sha384_ctx_alloc( void )
  502. {
  503. return malloc( sizeof( sha4_context ) );
  504. }
  505. void sha384_ctx_free( void *ctx )
  506. {
  507. free( ctx );
  508. }
  509. const md_info_t sha384_info = {
  510. POLARSSL_MD_SHA384,
  511. "SHA384",
  512. 48,
  513. sha384_starts_wrap,
  514. sha384_update_wrap,
  515. sha384_finish_wrap,
  516. sha384_wrap,
  517. sha384_file_wrap,
  518. sha384_hmac_starts_wrap,
  519. sha384_hmac_update_wrap,
  520. sha384_hmac_finish_wrap,
  521. sha384_hmac_reset_wrap,
  522. sha384_hmac_wrap,
  523. sha384_ctx_alloc,
  524. sha384_ctx_free,
  525. };
  526. void sha512_starts_wrap( void *ctx )
  527. {
  528. sha4_starts( (sha4_context *) ctx, 0 );
  529. }
  530. void sha512_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  531. {
  532. sha4_update( (sha4_context *) ctx, input, ilen );
  533. }
  534. void sha512_finish_wrap( void *ctx, unsigned char *output )
  535. {
  536. sha4_finish( (sha4_context *) ctx, output );
  537. }
  538. void sha512_wrap( const unsigned char *input, size_t ilen,
  539. unsigned char *output )
  540. {
  541. sha4( input, ilen, output, 0 );
  542. }
  543. int sha512_file_wrap( const char *path, unsigned char *output )
  544. {
  545. #if defined(POLARSSL_FS_IO)
  546. return sha4_file( path, output, 0 );
  547. #else
  548. ((void) path);
  549. ((void) output);
  550. return POLARSSL_ERR_MD_FEATURE_UNAVAILABLE;
  551. #endif
  552. }
  553. void sha512_hmac_starts_wrap( void *ctx, const unsigned char *key, size_t keylen )
  554. {
  555. sha4_hmac_starts( (sha4_context *) ctx, key, keylen, 0 );
  556. }
  557. void sha512_hmac_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
  558. {
  559. sha4_hmac_update( (sha4_context *) ctx, input, ilen );
  560. }
  561. void sha512_hmac_finish_wrap( void *ctx, unsigned char *output )
  562. {
  563. sha4_hmac_finish( (sha4_context *) ctx, output );
  564. }
  565. void sha512_hmac_reset_wrap( void *ctx )
  566. {
  567. sha4_hmac_reset( (sha4_context *) ctx );
  568. }
  569. void sha512_hmac_wrap( const unsigned char *key, size_t keylen,
  570. const unsigned char *input, size_t ilen,
  571. unsigned char *output )
  572. {
  573. sha4_hmac( key, keylen, input, ilen, output, 0 );
  574. }
  575. void * sha512_ctx_alloc( void )
  576. {
  577. return malloc( sizeof( sha4_context ) );
  578. }
  579. void sha512_ctx_free( void *ctx )
  580. {
  581. free( ctx );
  582. }
  583. const md_info_t sha512_info = {
  584. POLARSSL_MD_SHA512,
  585. "SHA512",
  586. 64,
  587. sha512_starts_wrap,
  588. sha512_update_wrap,
  589. sha512_finish_wrap,
  590. sha512_wrap,
  591. sha512_file_wrap,
  592. sha512_hmac_starts_wrap,
  593. sha512_hmac_update_wrap,
  594. sha512_hmac_finish_wrap,
  595. sha512_hmac_reset_wrap,
  596. sha512_hmac_wrap,
  597. sha512_ctx_alloc,
  598. sha512_ctx_free,
  599. };
  600. #endif
  601. #endif