cipher_wrap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /**
  2. * \file md_wrap.c
  3. *
  4. * \brief Generic message digest wrapper for PolarSSL
  5. *
  6. * \author Adriaan de Jong <dejong@fox-it.com>
  7. *
  8. * Copyright (C) 2006-2010, Brainspark B.V.
  9. *
  10. * This file is part of PolarSSL (http://www.polarssl.org)
  11. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  12. *
  13. * All rights reserved.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  28. */
  29. #include "config.h"
  30. #if defined(POLARSSL_CIPHER_C)
  31. #include "polarssl/cipher_wrap.h"
  32. #include "polarssl/aes.h"
  33. #include "polarssl/camellia.h"
  34. #include "polarssl/des.h"
  35. #include <stdlib.h>
  36. #if defined(POLARSSL_AES_C)
  37. int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
  38. unsigned char *iv, const unsigned char *input, unsigned char *output )
  39. {
  40. return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input, output );
  41. }
  42. int aes_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
  43. size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
  44. {
  45. #if defined(POLARSSL_CIPHER_MODE_CFB)
  46. return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv, input, output );
  47. #else
  48. ((void) ctx);
  49. ((void) operation);
  50. ((void) length);
  51. ((void) iv_off);
  52. ((void) iv);
  53. ((void) input);
  54. ((void) output);
  55. return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
  56. #endif
  57. }
  58. int aes_crypt_ctr_wrap( void *ctx, size_t length,
  59. size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
  60. const unsigned char *input, unsigned char *output )
  61. {
  62. #if defined(POLARSSL_CIPHER_MODE_CTR)
  63. return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
  64. stream_block, input, output );
  65. #else
  66. ((void) ctx);
  67. ((void) length);
  68. ((void) nc_off);
  69. ((void) nonce_counter);
  70. ((void) stream_block);
  71. ((void) input);
  72. ((void) output);
  73. return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
  74. #endif
  75. }
  76. int aes_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
  77. {
  78. return aes_setkey_dec( (aes_context *) ctx, key, key_length );
  79. }
  80. int aes_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
  81. {
  82. return aes_setkey_enc( (aes_context *) ctx, key, key_length );
  83. }
  84. static void * aes_ctx_alloc( void )
  85. {
  86. return malloc( sizeof( aes_context ) );
  87. }
  88. static void aes_ctx_free( void *ctx )
  89. {
  90. free( ctx );
  91. }
  92. const cipher_base_t aes_info = {
  93. POLARSSL_CIPHER_ID_AES,
  94. aes_crypt_cbc_wrap,
  95. aes_crypt_cfb128_wrap,
  96. aes_crypt_ctr_wrap,
  97. aes_setkey_enc_wrap,
  98. aes_setkey_dec_wrap,
  99. aes_ctx_alloc,
  100. aes_ctx_free
  101. };
  102. const cipher_info_t aes_128_cbc_info = {
  103. POLARSSL_CIPHER_AES_128_CBC,
  104. POLARSSL_MODE_CBC,
  105. 128,
  106. "AES-128-CBC",
  107. 16,
  108. 16,
  109. &aes_info
  110. };
  111. const cipher_info_t aes_192_cbc_info = {
  112. POLARSSL_CIPHER_AES_192_CBC,
  113. POLARSSL_MODE_CBC,
  114. 192,
  115. "AES-192-CBC",
  116. 16,
  117. 16,
  118. &aes_info
  119. };
  120. const cipher_info_t aes_256_cbc_info = {
  121. POLARSSL_CIPHER_AES_256_CBC,
  122. POLARSSL_MODE_CBC,
  123. 256,
  124. "AES-256-CBC",
  125. 16,
  126. 16,
  127. &aes_info
  128. };
  129. #if defined(POLARSSL_CIPHER_MODE_CFB)
  130. const cipher_info_t aes_128_cfb128_info = {
  131. POLARSSL_CIPHER_AES_128_CFB128,
  132. POLARSSL_MODE_CFB128,
  133. 128,
  134. "AES-128-CFB128",
  135. 16,
  136. 16,
  137. &aes_info
  138. };
  139. const cipher_info_t aes_192_cfb128_info = {
  140. POLARSSL_CIPHER_AES_192_CFB128,
  141. POLARSSL_MODE_CFB128,
  142. 192,
  143. "AES-192-CFB128",
  144. 16,
  145. 16,
  146. &aes_info
  147. };
  148. const cipher_info_t aes_256_cfb128_info = {
  149. POLARSSL_CIPHER_AES_256_CFB128,
  150. POLARSSL_MODE_CFB128,
  151. 256,
  152. "AES-256-CFB128",
  153. 16,
  154. 16,
  155. &aes_info
  156. };
  157. #endif /* POLARSSL_CIPHER_MODE_CFB */
  158. #if defined(POLARSSL_CIPHER_MODE_CTR)
  159. const cipher_info_t aes_128_ctr_info = {
  160. POLARSSL_CIPHER_AES_128_CTR,
  161. POLARSSL_MODE_CTR,
  162. 128,
  163. "AES-128-CTR",
  164. 16,
  165. 16,
  166. &aes_info
  167. };
  168. const cipher_info_t aes_192_ctr_info = {
  169. POLARSSL_CIPHER_AES_192_CTR,
  170. POLARSSL_MODE_CTR,
  171. 192,
  172. "AES-192-CTR",
  173. 16,
  174. 16,
  175. &aes_info
  176. };
  177. const cipher_info_t aes_256_ctr_info = {
  178. POLARSSL_CIPHER_AES_256_CTR,
  179. POLARSSL_MODE_CTR,
  180. 256,
  181. "AES-256-CTR",
  182. 16,
  183. 16,
  184. &aes_info
  185. };
  186. #endif /* POLARSSL_CIPHER_MODE_CTR */
  187. #endif
  188. #if defined(POLARSSL_CAMELLIA_C)
  189. int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
  190. unsigned char *iv, const unsigned char *input, unsigned char *output )
  191. {
  192. return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv, input, output );
  193. }
  194. int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
  195. size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
  196. {
  197. #if defined(POLARSSL_CIPHER_MODE_CFB)
  198. return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length, iv_off, iv, input, output );
  199. #else
  200. ((void) ctx);
  201. ((void) operation);
  202. ((void) length);
  203. ((void) iv_off);
  204. ((void) iv);
  205. ((void) input);
  206. ((void) output);
  207. return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
  208. #endif
  209. }
  210. int camellia_crypt_ctr_wrap( void *ctx, size_t length,
  211. size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
  212. const unsigned char *input, unsigned char *output )
  213. {
  214. #if defined(POLARSSL_CIPHER_MODE_CTR)
  215. return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off, nonce_counter,
  216. stream_block, input, output );
  217. #else
  218. ((void) ctx);
  219. ((void) length);
  220. ((void) nc_off);
  221. ((void) nonce_counter);
  222. ((void) stream_block);
  223. ((void) input);
  224. ((void) output);
  225. return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
  226. #endif
  227. }
  228. int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
  229. {
  230. return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
  231. }
  232. int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
  233. {
  234. return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
  235. }
  236. static void * camellia_ctx_alloc( void )
  237. {
  238. return malloc( sizeof( camellia_context ) );
  239. }
  240. static void camellia_ctx_free( void *ctx )
  241. {
  242. free( ctx );
  243. }
  244. const cipher_base_t camellia_info = {
  245. POLARSSL_CIPHER_ID_CAMELLIA,
  246. camellia_crypt_cbc_wrap,
  247. camellia_crypt_cfb128_wrap,
  248. camellia_crypt_ctr_wrap,
  249. camellia_setkey_enc_wrap,
  250. camellia_setkey_dec_wrap,
  251. camellia_ctx_alloc,
  252. camellia_ctx_free
  253. };
  254. const cipher_info_t camellia_128_cbc_info = {
  255. POLARSSL_CIPHER_CAMELLIA_128_CBC,
  256. POLARSSL_MODE_CBC,
  257. 128,
  258. "CAMELLIA-128-CBC",
  259. 16,
  260. 16,
  261. &camellia_info
  262. };
  263. const cipher_info_t camellia_192_cbc_info = {
  264. POLARSSL_CIPHER_CAMELLIA_192_CBC,
  265. POLARSSL_MODE_CBC,
  266. 192,
  267. "CAMELLIA-192-CBC",
  268. 16,
  269. 16,
  270. &camellia_info
  271. };
  272. const cipher_info_t camellia_256_cbc_info = {
  273. POLARSSL_CIPHER_CAMELLIA_256_CBC,
  274. POLARSSL_MODE_CBC,
  275. 256,
  276. "CAMELLIA-256-CBC",
  277. 16,
  278. 16,
  279. &camellia_info
  280. };
  281. #if defined(POLARSSL_CIPHER_MODE_CFB)
  282. const cipher_info_t camellia_128_cfb128_info = {
  283. POLARSSL_CIPHER_CAMELLIA_128_CFB128,
  284. POLARSSL_MODE_CFB128,
  285. 128,
  286. "CAMELLIA-128-CFB128",
  287. 16,
  288. 16,
  289. &camellia_info
  290. };
  291. const cipher_info_t camellia_192_cfb128_info = {
  292. POLARSSL_CIPHER_CAMELLIA_192_CFB128,
  293. POLARSSL_MODE_CFB128,
  294. 192,
  295. "CAMELLIA-192-CFB128",
  296. 16,
  297. 16,
  298. &camellia_info
  299. };
  300. const cipher_info_t camellia_256_cfb128_info = {
  301. POLARSSL_CIPHER_CAMELLIA_256_CFB128,
  302. POLARSSL_MODE_CFB128,
  303. 256,
  304. "CAMELLIA-256-CFB128",
  305. 16,
  306. 16,
  307. &camellia_info
  308. };
  309. #endif /* POLARSSL_CIPHER_MODE_CFB */
  310. #if defined(POLARSSL_CIPHER_MODE_CTR)
  311. const cipher_info_t camellia_128_ctr_info = {
  312. POLARSSL_CIPHER_CAMELLIA_128_CTR,
  313. POLARSSL_MODE_CTR,
  314. 128,
  315. "CAMELLIA-128-CTR",
  316. 16,
  317. 16,
  318. &camellia_info
  319. };
  320. const cipher_info_t camellia_192_ctr_info = {
  321. POLARSSL_CIPHER_CAMELLIA_192_CTR,
  322. POLARSSL_MODE_CTR,
  323. 192,
  324. "CAMELLIA-192-CTR",
  325. 16,
  326. 16,
  327. &camellia_info
  328. };
  329. const cipher_info_t camellia_256_ctr_info = {
  330. POLARSSL_CIPHER_CAMELLIA_256_CTR,
  331. POLARSSL_MODE_CTR,
  332. 256,
  333. "CAMELLIA-256-CTR",
  334. 16,
  335. 16,
  336. &camellia_info
  337. };
  338. #endif /* POLARSSL_CIPHER_MODE_CTR */
  339. #endif
  340. #if defined(POLARSSL_DES_C)
  341. int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
  342. unsigned char *iv, const unsigned char *input, unsigned char *output )
  343. {
  344. return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input, output );
  345. }
  346. int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
  347. unsigned char *iv, const unsigned char *input, unsigned char *output )
  348. {
  349. return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input, output );
  350. }
  351. int des_crypt_cfb128_wrap( void *ctx, operation_t operation, size_t length,
  352. size_t *iv_off, unsigned char *iv, const unsigned char *input, unsigned char *output )
  353. {
  354. ((void) ctx);
  355. ((void) operation);
  356. ((void) length);
  357. ((void) iv_off);
  358. ((void) iv);
  359. ((void) input);
  360. ((void) output);
  361. return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
  362. }
  363. int des_crypt_ctr_wrap( void *ctx, size_t length,
  364. size_t *nc_off, unsigned char *nonce_counter, unsigned char *stream_block,
  365. const unsigned char *input, unsigned char *output )
  366. {
  367. ((void) ctx);
  368. ((void) length);
  369. ((void) nc_off);
  370. ((void) nonce_counter);
  371. ((void) stream_block);
  372. ((void) input);
  373. ((void) output);
  374. return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
  375. }
  376. int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
  377. {
  378. ((void) key_length);
  379. return des_setkey_dec( (des_context *) ctx, key );
  380. }
  381. int des_setkey_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
  382. {
  383. ((void) key_length);
  384. return des_setkey_enc( (des_context *) ctx, key );
  385. }
  386. int des3_set2key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
  387. {
  388. ((void) key_length);
  389. return des3_set2key_dec( (des3_context *) ctx, key );
  390. }
  391. int des3_set2key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
  392. {
  393. ((void) key_length);
  394. return des3_set2key_enc( (des3_context *) ctx, key );
  395. }
  396. int des3_set3key_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
  397. {
  398. ((void) key_length);
  399. return des3_set3key_dec( (des3_context *) ctx, key );
  400. }
  401. int des3_set3key_enc_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
  402. {
  403. ((void) key_length);
  404. return des3_set3key_enc( (des3_context *) ctx, key );
  405. }
  406. static void * des_ctx_alloc( void )
  407. {
  408. return malloc( sizeof( des_context ) );
  409. }
  410. static void * des3_ctx_alloc( void )
  411. {
  412. return malloc( sizeof( des3_context ) );
  413. }
  414. static void des_ctx_free( void *ctx )
  415. {
  416. free( ctx );
  417. }
  418. const cipher_base_t des_info = {
  419. POLARSSL_CIPHER_ID_DES,
  420. des_crypt_cbc_wrap,
  421. des_crypt_cfb128_wrap,
  422. des_crypt_ctr_wrap,
  423. des_setkey_enc_wrap,
  424. des_setkey_dec_wrap,
  425. des_ctx_alloc,
  426. des_ctx_free
  427. };
  428. const cipher_info_t des_cbc_info = {
  429. POLARSSL_CIPHER_DES_CBC,
  430. POLARSSL_MODE_CBC,
  431. POLARSSL_KEY_LENGTH_DES,
  432. "DES-CBC",
  433. 8,
  434. 8,
  435. &des_info
  436. };
  437. const cipher_base_t des_ede_info = {
  438. POLARSSL_CIPHER_ID_DES,
  439. des3_crypt_cbc_wrap,
  440. des_crypt_cfb128_wrap,
  441. des_crypt_ctr_wrap,
  442. des3_set2key_enc_wrap,
  443. des3_set2key_dec_wrap,
  444. des3_ctx_alloc,
  445. des_ctx_free
  446. };
  447. const cipher_info_t des_ede_cbc_info = {
  448. POLARSSL_CIPHER_DES_EDE_CBC,
  449. POLARSSL_MODE_CBC,
  450. POLARSSL_KEY_LENGTH_DES_EDE,
  451. "DES-EDE-CBC",
  452. 16,
  453. 16,
  454. &des_ede_info
  455. };
  456. const cipher_base_t des_ede3_info = {
  457. POLARSSL_CIPHER_ID_DES,
  458. des3_crypt_cbc_wrap,
  459. des_crypt_cfb128_wrap,
  460. des_crypt_ctr_wrap,
  461. des3_set3key_enc_wrap,
  462. des3_set3key_dec_wrap,
  463. des3_ctx_alloc,
  464. des_ctx_free
  465. };
  466. const cipher_info_t des_ede3_cbc_info = {
  467. POLARSSL_CIPHER_DES_EDE3_CBC,
  468. POLARSSL_MODE_CBC,
  469. POLARSSL_KEY_LENGTH_DES_EDE3,
  470. "DES-EDE3-CBC",
  471. 8,
  472. 8,
  473. &des_ede3_info
  474. };
  475. #endif
  476. #endif