cipher_wrap.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. /**
  2. * \file cipher_wrap.c
  3. *
  4. * \brief Generic cipher 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: GPL-2.0
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. * This file is part of mbed TLS (https://tls.mbed.org)
  26. */
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "mbedtls/config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #if defined(MBEDTLS_CIPHER_C)
  33. #include "mbedtls/cipher_internal.h"
  34. #if defined(MBEDTLS_AES_C)
  35. #include "mbedtls/aes.h"
  36. #endif
  37. #if defined(MBEDTLS_ARC4_C)
  38. #include "mbedtls/arc4.h"
  39. #endif
  40. #if defined(MBEDTLS_CAMELLIA_C)
  41. #include "mbedtls/camellia.h"
  42. #endif
  43. #if defined(MBEDTLS_DES_C)
  44. #include "mbedtls/des.h"
  45. #endif
  46. #if defined(MBEDTLS_BLOWFISH_C)
  47. #include "mbedtls/blowfish.h"
  48. #endif
  49. #if defined(MBEDTLS_GCM_C)
  50. #include "mbedtls/gcm.h"
  51. #endif
  52. #if defined(MBEDTLS_CCM_C)
  53. #include "mbedtls/ccm.h"
  54. #endif
  55. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  56. #include <string.h>
  57. #endif
  58. #if defined(MBEDTLS_PLATFORM_C)
  59. #include "mbedtls/platform.h"
  60. #else
  61. #include <stdlib.h>
  62. #define mbedtls_calloc calloc
  63. #define mbedtls_free free
  64. #endif
  65. #if defined(MBEDTLS_GCM_C)
  66. /* shared by all GCM ciphers */
  67. static void *gcm_ctx_alloc( void )
  68. {
  69. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
  70. if( ctx != NULL )
  71. mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
  72. return( ctx );
  73. }
  74. static void gcm_ctx_free( void *ctx )
  75. {
  76. mbedtls_gcm_free( ctx );
  77. mbedtls_free( ctx );
  78. }
  79. #endif /* MBEDTLS_GCM_C */
  80. #if defined(MBEDTLS_CCM_C)
  81. /* shared by all CCM ciphers */
  82. static void *ccm_ctx_alloc( void )
  83. {
  84. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
  85. if( ctx != NULL )
  86. mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
  87. return( ctx );
  88. }
  89. static void ccm_ctx_free( void *ctx )
  90. {
  91. mbedtls_ccm_free( ctx );
  92. mbedtls_free( ctx );
  93. }
  94. #endif /* MBEDTLS_CCM_C */
  95. #if defined(MBEDTLS_AES_C)
  96. static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  97. const unsigned char *input, unsigned char *output )
  98. {
  99. return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
  100. }
  101. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  102. static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
  103. unsigned char *iv, const unsigned char *input, unsigned char *output )
  104. {
  105. return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
  106. output );
  107. }
  108. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  109. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  110. static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
  111. size_t length, size_t *iv_off, unsigned char *iv,
  112. const unsigned char *input, unsigned char *output )
  113. {
  114. return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
  115. input, output );
  116. }
  117. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  118. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  119. static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
  120. unsigned char *nonce_counter, unsigned char *stream_block,
  121. const unsigned char *input, unsigned char *output )
  122. {
  123. return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
  124. stream_block, input, output );
  125. }
  126. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  127. static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
  128. unsigned int key_bitlen )
  129. {
  130. return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
  131. }
  132. static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
  133. unsigned int key_bitlen )
  134. {
  135. return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
  136. }
  137. static void * aes_ctx_alloc( void )
  138. {
  139. mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
  140. if( aes == NULL )
  141. return( NULL );
  142. mbedtls_aes_init( aes );
  143. return( aes );
  144. }
  145. static void aes_ctx_free( void *ctx )
  146. {
  147. mbedtls_aes_free( (mbedtls_aes_context *) ctx );
  148. mbedtls_free( ctx );
  149. }
  150. static const mbedtls_cipher_base_t aes_info = {
  151. MBEDTLS_CIPHER_ID_AES,
  152. aes_crypt_ecb_wrap,
  153. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  154. aes_crypt_cbc_wrap,
  155. #endif
  156. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  157. aes_crypt_cfb128_wrap,
  158. #endif
  159. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  160. aes_crypt_ctr_wrap,
  161. #endif
  162. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  163. NULL,
  164. #endif
  165. aes_setkey_enc_wrap,
  166. aes_setkey_dec_wrap,
  167. aes_ctx_alloc,
  168. aes_ctx_free
  169. };
  170. static const mbedtls_cipher_info_t aes_128_ecb_info = {
  171. MBEDTLS_CIPHER_AES_128_ECB,
  172. MBEDTLS_MODE_ECB,
  173. 128,
  174. "AES-128-ECB",
  175. 16,
  176. 0,
  177. 16,
  178. &aes_info
  179. };
  180. static const mbedtls_cipher_info_t aes_192_ecb_info = {
  181. MBEDTLS_CIPHER_AES_192_ECB,
  182. MBEDTLS_MODE_ECB,
  183. 192,
  184. "AES-192-ECB",
  185. 16,
  186. 0,
  187. 16,
  188. &aes_info
  189. };
  190. static const mbedtls_cipher_info_t aes_256_ecb_info = {
  191. MBEDTLS_CIPHER_AES_256_ECB,
  192. MBEDTLS_MODE_ECB,
  193. 256,
  194. "AES-256-ECB",
  195. 16,
  196. 0,
  197. 16,
  198. &aes_info
  199. };
  200. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  201. static const mbedtls_cipher_info_t aes_128_cbc_info = {
  202. MBEDTLS_CIPHER_AES_128_CBC,
  203. MBEDTLS_MODE_CBC,
  204. 128,
  205. "AES-128-CBC",
  206. 16,
  207. 0,
  208. 16,
  209. &aes_info
  210. };
  211. static const mbedtls_cipher_info_t aes_192_cbc_info = {
  212. MBEDTLS_CIPHER_AES_192_CBC,
  213. MBEDTLS_MODE_CBC,
  214. 192,
  215. "AES-192-CBC",
  216. 16,
  217. 0,
  218. 16,
  219. &aes_info
  220. };
  221. static const mbedtls_cipher_info_t aes_256_cbc_info = {
  222. MBEDTLS_CIPHER_AES_256_CBC,
  223. MBEDTLS_MODE_CBC,
  224. 256,
  225. "AES-256-CBC",
  226. 16,
  227. 0,
  228. 16,
  229. &aes_info
  230. };
  231. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  232. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  233. static const mbedtls_cipher_info_t aes_128_cfb128_info = {
  234. MBEDTLS_CIPHER_AES_128_CFB128,
  235. MBEDTLS_MODE_CFB,
  236. 128,
  237. "AES-128-CFB128",
  238. 16,
  239. 0,
  240. 16,
  241. &aes_info
  242. };
  243. static const mbedtls_cipher_info_t aes_192_cfb128_info = {
  244. MBEDTLS_CIPHER_AES_192_CFB128,
  245. MBEDTLS_MODE_CFB,
  246. 192,
  247. "AES-192-CFB128",
  248. 16,
  249. 0,
  250. 16,
  251. &aes_info
  252. };
  253. static const mbedtls_cipher_info_t aes_256_cfb128_info = {
  254. MBEDTLS_CIPHER_AES_256_CFB128,
  255. MBEDTLS_MODE_CFB,
  256. 256,
  257. "AES-256-CFB128",
  258. 16,
  259. 0,
  260. 16,
  261. &aes_info
  262. };
  263. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  264. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  265. static const mbedtls_cipher_info_t aes_128_ctr_info = {
  266. MBEDTLS_CIPHER_AES_128_CTR,
  267. MBEDTLS_MODE_CTR,
  268. 128,
  269. "AES-128-CTR",
  270. 16,
  271. 0,
  272. 16,
  273. &aes_info
  274. };
  275. static const mbedtls_cipher_info_t aes_192_ctr_info = {
  276. MBEDTLS_CIPHER_AES_192_CTR,
  277. MBEDTLS_MODE_CTR,
  278. 192,
  279. "AES-192-CTR",
  280. 16,
  281. 0,
  282. 16,
  283. &aes_info
  284. };
  285. static const mbedtls_cipher_info_t aes_256_ctr_info = {
  286. MBEDTLS_CIPHER_AES_256_CTR,
  287. MBEDTLS_MODE_CTR,
  288. 256,
  289. "AES-256-CTR",
  290. 16,
  291. 0,
  292. 16,
  293. &aes_info
  294. };
  295. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  296. #if defined(MBEDTLS_GCM_C)
  297. static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
  298. unsigned int key_bitlen )
  299. {
  300. return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
  301. key, key_bitlen );
  302. }
  303. static const mbedtls_cipher_base_t gcm_aes_info = {
  304. MBEDTLS_CIPHER_ID_AES,
  305. NULL,
  306. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  307. NULL,
  308. #endif
  309. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  310. NULL,
  311. #endif
  312. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  313. NULL,
  314. #endif
  315. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  316. NULL,
  317. #endif
  318. gcm_aes_setkey_wrap,
  319. gcm_aes_setkey_wrap,
  320. gcm_ctx_alloc,
  321. gcm_ctx_free,
  322. };
  323. static const mbedtls_cipher_info_t aes_128_gcm_info = {
  324. MBEDTLS_CIPHER_AES_128_GCM,
  325. MBEDTLS_MODE_GCM,
  326. 128,
  327. "AES-128-GCM",
  328. 12,
  329. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  330. 16,
  331. &gcm_aes_info
  332. };
  333. static const mbedtls_cipher_info_t aes_192_gcm_info = {
  334. MBEDTLS_CIPHER_AES_192_GCM,
  335. MBEDTLS_MODE_GCM,
  336. 192,
  337. "AES-192-GCM",
  338. 12,
  339. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  340. 16,
  341. &gcm_aes_info
  342. };
  343. static const mbedtls_cipher_info_t aes_256_gcm_info = {
  344. MBEDTLS_CIPHER_AES_256_GCM,
  345. MBEDTLS_MODE_GCM,
  346. 256,
  347. "AES-256-GCM",
  348. 12,
  349. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  350. 16,
  351. &gcm_aes_info
  352. };
  353. #endif /* MBEDTLS_GCM_C */
  354. #if defined(MBEDTLS_CCM_C)
  355. static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
  356. unsigned int key_bitlen )
  357. {
  358. return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
  359. key, key_bitlen );
  360. }
  361. static const mbedtls_cipher_base_t ccm_aes_info = {
  362. MBEDTLS_CIPHER_ID_AES,
  363. NULL,
  364. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  365. NULL,
  366. #endif
  367. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  368. NULL,
  369. #endif
  370. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  371. NULL,
  372. #endif
  373. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  374. NULL,
  375. #endif
  376. ccm_aes_setkey_wrap,
  377. ccm_aes_setkey_wrap,
  378. ccm_ctx_alloc,
  379. ccm_ctx_free,
  380. };
  381. static const mbedtls_cipher_info_t aes_128_ccm_info = {
  382. MBEDTLS_CIPHER_AES_128_CCM,
  383. MBEDTLS_MODE_CCM,
  384. 128,
  385. "AES-128-CCM",
  386. 12,
  387. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  388. 16,
  389. &ccm_aes_info
  390. };
  391. static const mbedtls_cipher_info_t aes_192_ccm_info = {
  392. MBEDTLS_CIPHER_AES_192_CCM,
  393. MBEDTLS_MODE_CCM,
  394. 192,
  395. "AES-192-CCM",
  396. 12,
  397. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  398. 16,
  399. &ccm_aes_info
  400. };
  401. static const mbedtls_cipher_info_t aes_256_ccm_info = {
  402. MBEDTLS_CIPHER_AES_256_CCM,
  403. MBEDTLS_MODE_CCM,
  404. 256,
  405. "AES-256-CCM",
  406. 12,
  407. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  408. 16,
  409. &ccm_aes_info
  410. };
  411. #endif /* MBEDTLS_CCM_C */
  412. #endif /* MBEDTLS_AES_C */
  413. #if defined(MBEDTLS_CAMELLIA_C)
  414. static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  415. const unsigned char *input, unsigned char *output )
  416. {
  417. return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
  418. output );
  419. }
  420. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  421. static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
  422. size_t length, unsigned char *iv,
  423. const unsigned char *input, unsigned char *output )
  424. {
  425. return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
  426. input, output );
  427. }
  428. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  429. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  430. static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
  431. size_t length, size_t *iv_off, unsigned char *iv,
  432. const unsigned char *input, unsigned char *output )
  433. {
  434. return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
  435. iv_off, iv, input, output );
  436. }
  437. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  438. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  439. static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
  440. unsigned char *nonce_counter, unsigned char *stream_block,
  441. const unsigned char *input, unsigned char *output )
  442. {
  443. return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
  444. nonce_counter, stream_block, input, output );
  445. }
  446. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  447. static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
  448. unsigned int key_bitlen )
  449. {
  450. return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
  451. }
  452. static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
  453. unsigned int key_bitlen )
  454. {
  455. return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
  456. }
  457. static void * camellia_ctx_alloc( void )
  458. {
  459. mbedtls_camellia_context *ctx;
  460. ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
  461. if( ctx == NULL )
  462. return( NULL );
  463. mbedtls_camellia_init( ctx );
  464. return( ctx );
  465. }
  466. static void camellia_ctx_free( void *ctx )
  467. {
  468. mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
  469. mbedtls_free( ctx );
  470. }
  471. static const mbedtls_cipher_base_t camellia_info = {
  472. MBEDTLS_CIPHER_ID_CAMELLIA,
  473. camellia_crypt_ecb_wrap,
  474. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  475. camellia_crypt_cbc_wrap,
  476. #endif
  477. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  478. camellia_crypt_cfb128_wrap,
  479. #endif
  480. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  481. camellia_crypt_ctr_wrap,
  482. #endif
  483. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  484. NULL,
  485. #endif
  486. camellia_setkey_enc_wrap,
  487. camellia_setkey_dec_wrap,
  488. camellia_ctx_alloc,
  489. camellia_ctx_free
  490. };
  491. static const mbedtls_cipher_info_t camellia_128_ecb_info = {
  492. MBEDTLS_CIPHER_CAMELLIA_128_ECB,
  493. MBEDTLS_MODE_ECB,
  494. 128,
  495. "CAMELLIA-128-ECB",
  496. 16,
  497. 0,
  498. 16,
  499. &camellia_info
  500. };
  501. static const mbedtls_cipher_info_t camellia_192_ecb_info = {
  502. MBEDTLS_CIPHER_CAMELLIA_192_ECB,
  503. MBEDTLS_MODE_ECB,
  504. 192,
  505. "CAMELLIA-192-ECB",
  506. 16,
  507. 0,
  508. 16,
  509. &camellia_info
  510. };
  511. static const mbedtls_cipher_info_t camellia_256_ecb_info = {
  512. MBEDTLS_CIPHER_CAMELLIA_256_ECB,
  513. MBEDTLS_MODE_ECB,
  514. 256,
  515. "CAMELLIA-256-ECB",
  516. 16,
  517. 0,
  518. 16,
  519. &camellia_info
  520. };
  521. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  522. static const mbedtls_cipher_info_t camellia_128_cbc_info = {
  523. MBEDTLS_CIPHER_CAMELLIA_128_CBC,
  524. MBEDTLS_MODE_CBC,
  525. 128,
  526. "CAMELLIA-128-CBC",
  527. 16,
  528. 0,
  529. 16,
  530. &camellia_info
  531. };
  532. static const mbedtls_cipher_info_t camellia_192_cbc_info = {
  533. MBEDTLS_CIPHER_CAMELLIA_192_CBC,
  534. MBEDTLS_MODE_CBC,
  535. 192,
  536. "CAMELLIA-192-CBC",
  537. 16,
  538. 0,
  539. 16,
  540. &camellia_info
  541. };
  542. static const mbedtls_cipher_info_t camellia_256_cbc_info = {
  543. MBEDTLS_CIPHER_CAMELLIA_256_CBC,
  544. MBEDTLS_MODE_CBC,
  545. 256,
  546. "CAMELLIA-256-CBC",
  547. 16,
  548. 0,
  549. 16,
  550. &camellia_info
  551. };
  552. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  553. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  554. static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
  555. MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
  556. MBEDTLS_MODE_CFB,
  557. 128,
  558. "CAMELLIA-128-CFB128",
  559. 16,
  560. 0,
  561. 16,
  562. &camellia_info
  563. };
  564. static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
  565. MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
  566. MBEDTLS_MODE_CFB,
  567. 192,
  568. "CAMELLIA-192-CFB128",
  569. 16,
  570. 0,
  571. 16,
  572. &camellia_info
  573. };
  574. static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
  575. MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
  576. MBEDTLS_MODE_CFB,
  577. 256,
  578. "CAMELLIA-256-CFB128",
  579. 16,
  580. 0,
  581. 16,
  582. &camellia_info
  583. };
  584. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  585. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  586. static const mbedtls_cipher_info_t camellia_128_ctr_info = {
  587. MBEDTLS_CIPHER_CAMELLIA_128_CTR,
  588. MBEDTLS_MODE_CTR,
  589. 128,
  590. "CAMELLIA-128-CTR",
  591. 16,
  592. 0,
  593. 16,
  594. &camellia_info
  595. };
  596. static const mbedtls_cipher_info_t camellia_192_ctr_info = {
  597. MBEDTLS_CIPHER_CAMELLIA_192_CTR,
  598. MBEDTLS_MODE_CTR,
  599. 192,
  600. "CAMELLIA-192-CTR",
  601. 16,
  602. 0,
  603. 16,
  604. &camellia_info
  605. };
  606. static const mbedtls_cipher_info_t camellia_256_ctr_info = {
  607. MBEDTLS_CIPHER_CAMELLIA_256_CTR,
  608. MBEDTLS_MODE_CTR,
  609. 256,
  610. "CAMELLIA-256-CTR",
  611. 16,
  612. 0,
  613. 16,
  614. &camellia_info
  615. };
  616. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  617. #if defined(MBEDTLS_GCM_C)
  618. static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
  619. unsigned int key_bitlen )
  620. {
  621. return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
  622. key, key_bitlen );
  623. }
  624. static const mbedtls_cipher_base_t gcm_camellia_info = {
  625. MBEDTLS_CIPHER_ID_CAMELLIA,
  626. NULL,
  627. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  628. NULL,
  629. #endif
  630. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  631. NULL,
  632. #endif
  633. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  634. NULL,
  635. #endif
  636. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  637. NULL,
  638. #endif
  639. gcm_camellia_setkey_wrap,
  640. gcm_camellia_setkey_wrap,
  641. gcm_ctx_alloc,
  642. gcm_ctx_free,
  643. };
  644. static const mbedtls_cipher_info_t camellia_128_gcm_info = {
  645. MBEDTLS_CIPHER_CAMELLIA_128_GCM,
  646. MBEDTLS_MODE_GCM,
  647. 128,
  648. "CAMELLIA-128-GCM",
  649. 12,
  650. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  651. 16,
  652. &gcm_camellia_info
  653. };
  654. static const mbedtls_cipher_info_t camellia_192_gcm_info = {
  655. MBEDTLS_CIPHER_CAMELLIA_192_GCM,
  656. MBEDTLS_MODE_GCM,
  657. 192,
  658. "CAMELLIA-192-GCM",
  659. 12,
  660. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  661. 16,
  662. &gcm_camellia_info
  663. };
  664. static const mbedtls_cipher_info_t camellia_256_gcm_info = {
  665. MBEDTLS_CIPHER_CAMELLIA_256_GCM,
  666. MBEDTLS_MODE_GCM,
  667. 256,
  668. "CAMELLIA-256-GCM",
  669. 12,
  670. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  671. 16,
  672. &gcm_camellia_info
  673. };
  674. #endif /* MBEDTLS_GCM_C */
  675. #if defined(MBEDTLS_CCM_C)
  676. static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
  677. unsigned int key_bitlen )
  678. {
  679. return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
  680. key, key_bitlen );
  681. }
  682. static const mbedtls_cipher_base_t ccm_camellia_info = {
  683. MBEDTLS_CIPHER_ID_CAMELLIA,
  684. NULL,
  685. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  686. NULL,
  687. #endif
  688. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  689. NULL,
  690. #endif
  691. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  692. NULL,
  693. #endif
  694. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  695. NULL,
  696. #endif
  697. ccm_camellia_setkey_wrap,
  698. ccm_camellia_setkey_wrap,
  699. ccm_ctx_alloc,
  700. ccm_ctx_free,
  701. };
  702. static const mbedtls_cipher_info_t camellia_128_ccm_info = {
  703. MBEDTLS_CIPHER_CAMELLIA_128_CCM,
  704. MBEDTLS_MODE_CCM,
  705. 128,
  706. "CAMELLIA-128-CCM",
  707. 12,
  708. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  709. 16,
  710. &ccm_camellia_info
  711. };
  712. static const mbedtls_cipher_info_t camellia_192_ccm_info = {
  713. MBEDTLS_CIPHER_CAMELLIA_192_CCM,
  714. MBEDTLS_MODE_CCM,
  715. 192,
  716. "CAMELLIA-192-CCM",
  717. 12,
  718. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  719. 16,
  720. &ccm_camellia_info
  721. };
  722. static const mbedtls_cipher_info_t camellia_256_ccm_info = {
  723. MBEDTLS_CIPHER_CAMELLIA_256_CCM,
  724. MBEDTLS_MODE_CCM,
  725. 256,
  726. "CAMELLIA-256-CCM",
  727. 12,
  728. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  729. 16,
  730. &ccm_camellia_info
  731. };
  732. #endif /* MBEDTLS_CCM_C */
  733. #endif /* MBEDTLS_CAMELLIA_C */
  734. #if defined(MBEDTLS_DES_C)
  735. static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  736. const unsigned char *input, unsigned char *output )
  737. {
  738. ((void) operation);
  739. return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
  740. }
  741. static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  742. const unsigned char *input, unsigned char *output )
  743. {
  744. ((void) operation);
  745. return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
  746. }
  747. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  748. static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
  749. unsigned char *iv, const unsigned char *input, unsigned char *output )
  750. {
  751. return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
  752. output );
  753. }
  754. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  755. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  756. static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
  757. unsigned char *iv, const unsigned char *input, unsigned char *output )
  758. {
  759. return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
  760. output );
  761. }
  762. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  763. static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
  764. unsigned int key_bitlen )
  765. {
  766. ((void) key_bitlen);
  767. return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
  768. }
  769. static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
  770. unsigned int key_bitlen )
  771. {
  772. ((void) key_bitlen);
  773. return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
  774. }
  775. static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
  776. unsigned int key_bitlen )
  777. {
  778. ((void) key_bitlen);
  779. return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
  780. }
  781. static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
  782. unsigned int key_bitlen )
  783. {
  784. ((void) key_bitlen);
  785. return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
  786. }
  787. static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
  788. unsigned int key_bitlen )
  789. {
  790. ((void) key_bitlen);
  791. return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
  792. }
  793. static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
  794. unsigned int key_bitlen )
  795. {
  796. ((void) key_bitlen);
  797. return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
  798. }
  799. static void * des_ctx_alloc( void )
  800. {
  801. mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
  802. if( des == NULL )
  803. return( NULL );
  804. mbedtls_des_init( des );
  805. return( des );
  806. }
  807. static void des_ctx_free( void *ctx )
  808. {
  809. mbedtls_des_free( (mbedtls_des_context *) ctx );
  810. mbedtls_free( ctx );
  811. }
  812. static void * des3_ctx_alloc( void )
  813. {
  814. mbedtls_des3_context *des3;
  815. des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
  816. if( des3 == NULL )
  817. return( NULL );
  818. mbedtls_des3_init( des3 );
  819. return( des3 );
  820. }
  821. static void des3_ctx_free( void *ctx )
  822. {
  823. mbedtls_des3_free( (mbedtls_des3_context *) ctx );
  824. mbedtls_free( ctx );
  825. }
  826. static const mbedtls_cipher_base_t des_info = {
  827. MBEDTLS_CIPHER_ID_DES,
  828. des_crypt_ecb_wrap,
  829. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  830. des_crypt_cbc_wrap,
  831. #endif
  832. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  833. NULL,
  834. #endif
  835. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  836. NULL,
  837. #endif
  838. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  839. NULL,
  840. #endif
  841. des_setkey_enc_wrap,
  842. des_setkey_dec_wrap,
  843. des_ctx_alloc,
  844. des_ctx_free
  845. };
  846. static const mbedtls_cipher_info_t des_ecb_info = {
  847. MBEDTLS_CIPHER_DES_ECB,
  848. MBEDTLS_MODE_ECB,
  849. MBEDTLS_KEY_LENGTH_DES,
  850. "DES-ECB",
  851. 8,
  852. 0,
  853. 8,
  854. &des_info
  855. };
  856. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  857. static const mbedtls_cipher_info_t des_cbc_info = {
  858. MBEDTLS_CIPHER_DES_CBC,
  859. MBEDTLS_MODE_CBC,
  860. MBEDTLS_KEY_LENGTH_DES,
  861. "DES-CBC",
  862. 8,
  863. 0,
  864. 8,
  865. &des_info
  866. };
  867. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  868. static const mbedtls_cipher_base_t des_ede_info = {
  869. MBEDTLS_CIPHER_ID_DES,
  870. des3_crypt_ecb_wrap,
  871. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  872. des3_crypt_cbc_wrap,
  873. #endif
  874. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  875. NULL,
  876. #endif
  877. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  878. NULL,
  879. #endif
  880. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  881. NULL,
  882. #endif
  883. des3_set2key_enc_wrap,
  884. des3_set2key_dec_wrap,
  885. des3_ctx_alloc,
  886. des3_ctx_free
  887. };
  888. static const mbedtls_cipher_info_t des_ede_ecb_info = {
  889. MBEDTLS_CIPHER_DES_EDE_ECB,
  890. MBEDTLS_MODE_ECB,
  891. MBEDTLS_KEY_LENGTH_DES_EDE,
  892. "DES-EDE-ECB",
  893. 8,
  894. 0,
  895. 8,
  896. &des_ede_info
  897. };
  898. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  899. static const mbedtls_cipher_info_t des_ede_cbc_info = {
  900. MBEDTLS_CIPHER_DES_EDE_CBC,
  901. MBEDTLS_MODE_CBC,
  902. MBEDTLS_KEY_LENGTH_DES_EDE,
  903. "DES-EDE-CBC",
  904. 8,
  905. 0,
  906. 8,
  907. &des_ede_info
  908. };
  909. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  910. static const mbedtls_cipher_base_t des_ede3_info = {
  911. MBEDTLS_CIPHER_ID_3DES,
  912. des3_crypt_ecb_wrap,
  913. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  914. des3_crypt_cbc_wrap,
  915. #endif
  916. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  917. NULL,
  918. #endif
  919. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  920. NULL,
  921. #endif
  922. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  923. NULL,
  924. #endif
  925. des3_set3key_enc_wrap,
  926. des3_set3key_dec_wrap,
  927. des3_ctx_alloc,
  928. des3_ctx_free
  929. };
  930. static const mbedtls_cipher_info_t des_ede3_ecb_info = {
  931. MBEDTLS_CIPHER_DES_EDE3_ECB,
  932. MBEDTLS_MODE_ECB,
  933. MBEDTLS_KEY_LENGTH_DES_EDE3,
  934. "DES-EDE3-ECB",
  935. 8,
  936. 0,
  937. 8,
  938. &des_ede3_info
  939. };
  940. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  941. static const mbedtls_cipher_info_t des_ede3_cbc_info = {
  942. MBEDTLS_CIPHER_DES_EDE3_CBC,
  943. MBEDTLS_MODE_CBC,
  944. MBEDTLS_KEY_LENGTH_DES_EDE3,
  945. "DES-EDE3-CBC",
  946. 8,
  947. 0,
  948. 8,
  949. &des_ede3_info
  950. };
  951. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  952. #endif /* MBEDTLS_DES_C */
  953. #if defined(MBEDTLS_BLOWFISH_C)
  954. static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  955. const unsigned char *input, unsigned char *output )
  956. {
  957. return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
  958. output );
  959. }
  960. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  961. static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
  962. size_t length, unsigned char *iv, const unsigned char *input,
  963. unsigned char *output )
  964. {
  965. return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
  966. input, output );
  967. }
  968. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  969. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  970. static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
  971. size_t length, size_t *iv_off, unsigned char *iv,
  972. const unsigned char *input, unsigned char *output )
  973. {
  974. return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
  975. iv_off, iv, input, output );
  976. }
  977. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  978. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  979. static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
  980. unsigned char *nonce_counter, unsigned char *stream_block,
  981. const unsigned char *input, unsigned char *output )
  982. {
  983. return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
  984. nonce_counter, stream_block, input, output );
  985. }
  986. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  987. static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
  988. unsigned int key_bitlen )
  989. {
  990. return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
  991. }
  992. static void * blowfish_ctx_alloc( void )
  993. {
  994. mbedtls_blowfish_context *ctx;
  995. ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
  996. if( ctx == NULL )
  997. return( NULL );
  998. mbedtls_blowfish_init( ctx );
  999. return( ctx );
  1000. }
  1001. static void blowfish_ctx_free( void *ctx )
  1002. {
  1003. mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
  1004. mbedtls_free( ctx );
  1005. }
  1006. static const mbedtls_cipher_base_t blowfish_info = {
  1007. MBEDTLS_CIPHER_ID_BLOWFISH,
  1008. blowfish_crypt_ecb_wrap,
  1009. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1010. blowfish_crypt_cbc_wrap,
  1011. #endif
  1012. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1013. blowfish_crypt_cfb64_wrap,
  1014. #endif
  1015. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1016. blowfish_crypt_ctr_wrap,
  1017. #endif
  1018. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1019. NULL,
  1020. #endif
  1021. blowfish_setkey_wrap,
  1022. blowfish_setkey_wrap,
  1023. blowfish_ctx_alloc,
  1024. blowfish_ctx_free
  1025. };
  1026. static const mbedtls_cipher_info_t blowfish_ecb_info = {
  1027. MBEDTLS_CIPHER_BLOWFISH_ECB,
  1028. MBEDTLS_MODE_ECB,
  1029. 128,
  1030. "BLOWFISH-ECB",
  1031. 8,
  1032. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  1033. 8,
  1034. &blowfish_info
  1035. };
  1036. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1037. static const mbedtls_cipher_info_t blowfish_cbc_info = {
  1038. MBEDTLS_CIPHER_BLOWFISH_CBC,
  1039. MBEDTLS_MODE_CBC,
  1040. 128,
  1041. "BLOWFISH-CBC",
  1042. 8,
  1043. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  1044. 8,
  1045. &blowfish_info
  1046. };
  1047. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1048. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1049. static const mbedtls_cipher_info_t blowfish_cfb64_info = {
  1050. MBEDTLS_CIPHER_BLOWFISH_CFB64,
  1051. MBEDTLS_MODE_CFB,
  1052. 128,
  1053. "BLOWFISH-CFB64",
  1054. 8,
  1055. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  1056. 8,
  1057. &blowfish_info
  1058. };
  1059. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  1060. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1061. static const mbedtls_cipher_info_t blowfish_ctr_info = {
  1062. MBEDTLS_CIPHER_BLOWFISH_CTR,
  1063. MBEDTLS_MODE_CTR,
  1064. 128,
  1065. "BLOWFISH-CTR",
  1066. 8,
  1067. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  1068. 8,
  1069. &blowfish_info
  1070. };
  1071. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  1072. #endif /* MBEDTLS_BLOWFISH_C */
  1073. #if defined(MBEDTLS_ARC4_C)
  1074. static int arc4_crypt_stream_wrap( void *ctx, size_t length,
  1075. const unsigned char *input,
  1076. unsigned char *output )
  1077. {
  1078. return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
  1079. }
  1080. static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
  1081. unsigned int key_bitlen )
  1082. {
  1083. /* we get key_bitlen in bits, arc4 expects it in bytes */
  1084. if( key_bitlen % 8 != 0 )
  1085. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1086. mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
  1087. return( 0 );
  1088. }
  1089. static void * arc4_ctx_alloc( void )
  1090. {
  1091. mbedtls_arc4_context *ctx;
  1092. ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
  1093. if( ctx == NULL )
  1094. return( NULL );
  1095. mbedtls_arc4_init( ctx );
  1096. return( ctx );
  1097. }
  1098. static void arc4_ctx_free( void *ctx )
  1099. {
  1100. mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
  1101. mbedtls_free( ctx );
  1102. }
  1103. static const mbedtls_cipher_base_t arc4_base_info = {
  1104. MBEDTLS_CIPHER_ID_ARC4,
  1105. NULL,
  1106. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1107. NULL,
  1108. #endif
  1109. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1110. NULL,
  1111. #endif
  1112. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1113. NULL,
  1114. #endif
  1115. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1116. arc4_crypt_stream_wrap,
  1117. #endif
  1118. arc4_setkey_wrap,
  1119. arc4_setkey_wrap,
  1120. arc4_ctx_alloc,
  1121. arc4_ctx_free
  1122. };
  1123. static const mbedtls_cipher_info_t arc4_128_info = {
  1124. MBEDTLS_CIPHER_ARC4_128,
  1125. MBEDTLS_MODE_STREAM,
  1126. 128,
  1127. "ARC4-128",
  1128. 0,
  1129. 0,
  1130. 1,
  1131. &arc4_base_info
  1132. };
  1133. #endif /* MBEDTLS_ARC4_C */
  1134. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  1135. static int null_crypt_stream( void *ctx, size_t length,
  1136. const unsigned char *input,
  1137. unsigned char *output )
  1138. {
  1139. ((void) ctx);
  1140. memmove( output, input, length );
  1141. return( 0 );
  1142. }
  1143. static int null_setkey( void *ctx, const unsigned char *key,
  1144. unsigned int key_bitlen )
  1145. {
  1146. ((void) ctx);
  1147. ((void) key);
  1148. ((void) key_bitlen);
  1149. return( 0 );
  1150. }
  1151. static void * null_ctx_alloc( void )
  1152. {
  1153. return( (void *) 1 );
  1154. }
  1155. static void null_ctx_free( void *ctx )
  1156. {
  1157. ((void) ctx);
  1158. }
  1159. static const mbedtls_cipher_base_t null_base_info = {
  1160. MBEDTLS_CIPHER_ID_NULL,
  1161. NULL,
  1162. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1163. NULL,
  1164. #endif
  1165. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1166. NULL,
  1167. #endif
  1168. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1169. NULL,
  1170. #endif
  1171. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1172. null_crypt_stream,
  1173. #endif
  1174. null_setkey,
  1175. null_setkey,
  1176. null_ctx_alloc,
  1177. null_ctx_free
  1178. };
  1179. static const mbedtls_cipher_info_t null_cipher_info = {
  1180. MBEDTLS_CIPHER_NULL,
  1181. MBEDTLS_MODE_STREAM,
  1182. 0,
  1183. "NULL",
  1184. 0,
  1185. 0,
  1186. 1,
  1187. &null_base_info
  1188. };
  1189. #endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
  1190. const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
  1191. {
  1192. #if defined(MBEDTLS_AES_C)
  1193. { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
  1194. { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
  1195. { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
  1196. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1197. { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
  1198. { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
  1199. { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
  1200. #endif
  1201. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1202. { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
  1203. { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
  1204. { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
  1205. #endif
  1206. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1207. { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
  1208. { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
  1209. { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
  1210. #endif
  1211. #if defined(MBEDTLS_GCM_C)
  1212. { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
  1213. { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
  1214. { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
  1215. #endif
  1216. #if defined(MBEDTLS_CCM_C)
  1217. { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
  1218. { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
  1219. { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
  1220. #endif
  1221. #endif /* MBEDTLS_AES_C */
  1222. #if defined(MBEDTLS_ARC4_C)
  1223. { MBEDTLS_CIPHER_ARC4_128, &arc4_128_info },
  1224. #endif
  1225. #if defined(MBEDTLS_BLOWFISH_C)
  1226. { MBEDTLS_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
  1227. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1228. { MBEDTLS_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
  1229. #endif
  1230. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1231. { MBEDTLS_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
  1232. #endif
  1233. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1234. { MBEDTLS_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
  1235. #endif
  1236. #endif /* MBEDTLS_BLOWFISH_C */
  1237. #if defined(MBEDTLS_CAMELLIA_C)
  1238. { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
  1239. { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
  1240. { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
  1241. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1242. { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
  1243. { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
  1244. { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
  1245. #endif
  1246. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1247. { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
  1248. { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
  1249. { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
  1250. #endif
  1251. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1252. { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
  1253. { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
  1254. { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
  1255. #endif
  1256. #if defined(MBEDTLS_GCM_C)
  1257. { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
  1258. { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
  1259. { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
  1260. #endif
  1261. #if defined(MBEDTLS_CCM_C)
  1262. { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
  1263. { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
  1264. { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
  1265. #endif
  1266. #endif /* MBEDTLS_CAMELLIA_C */
  1267. #if defined(MBEDTLS_DES_C)
  1268. { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
  1269. { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
  1270. { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
  1271. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1272. { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
  1273. { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
  1274. { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
  1275. #endif
  1276. #endif /* MBEDTLS_DES_C */
  1277. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  1278. { MBEDTLS_CIPHER_NULL, &null_cipher_info },
  1279. #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
  1280. { MBEDTLS_CIPHER_NONE, NULL }
  1281. };
  1282. #define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
  1283. int mbedtls_cipher_supported[NUM_CIPHERS];
  1284. #endif /* MBEDTLS_CIPHER_C */