cipher_wrap.c 34 KB

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