cipher_wrap.c 35 KB

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