cipher_wrap.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  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_CHACHAPOLY_C)
  33. #include "mbedtls/chachapoly.h"
  34. #endif
  35. #if defined(MBEDTLS_AES_C)
  36. #include "mbedtls/aes.h"
  37. #endif
  38. #if defined(MBEDTLS_ARC4_C)
  39. #include "mbedtls/arc4.h"
  40. #endif
  41. #if defined(MBEDTLS_CAMELLIA_C)
  42. #include "mbedtls/camellia.h"
  43. #endif
  44. #if defined(MBEDTLS_ARIA_C)
  45. #include "mbedtls/aria.h"
  46. #endif
  47. #if defined(MBEDTLS_DES_C)
  48. #include "mbedtls/des.h"
  49. #endif
  50. #if defined(MBEDTLS_BLOWFISH_C)
  51. #include "mbedtls/blowfish.h"
  52. #endif
  53. #if defined(MBEDTLS_CHACHA20_C)
  54. #include "mbedtls/chacha20.h"
  55. #endif
  56. #if defined(MBEDTLS_GCM_C)
  57. #include "mbedtls/gcm.h"
  58. #endif
  59. #if defined(MBEDTLS_CCM_C)
  60. #include "mbedtls/ccm.h"
  61. #endif
  62. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  63. #include <string.h>
  64. #endif
  65. #if defined(MBEDTLS_PLATFORM_C)
  66. #include "mbedtls/platform.h"
  67. #else
  68. #include <stdlib.h>
  69. #define mbedtls_calloc calloc
  70. #define mbedtls_free free
  71. #endif
  72. #if defined(MBEDTLS_GCM_C)
  73. /* shared by all GCM ciphers */
  74. static void *gcm_ctx_alloc( void )
  75. {
  76. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
  77. if( ctx != NULL )
  78. mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
  79. return( ctx );
  80. }
  81. static void gcm_ctx_free( void *ctx )
  82. {
  83. mbedtls_gcm_free( ctx );
  84. mbedtls_free( ctx );
  85. }
  86. #endif /* MBEDTLS_GCM_C */
  87. #if defined(MBEDTLS_CCM_C)
  88. /* shared by all CCM ciphers */
  89. static void *ccm_ctx_alloc( void )
  90. {
  91. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
  92. if( ctx != NULL )
  93. mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
  94. return( ctx );
  95. }
  96. static void ccm_ctx_free( void *ctx )
  97. {
  98. mbedtls_ccm_free( ctx );
  99. mbedtls_free( ctx );
  100. }
  101. #endif /* MBEDTLS_CCM_C */
  102. #if defined(MBEDTLS_AES_C)
  103. static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  104. const unsigned char *input, unsigned char *output )
  105. {
  106. return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
  107. }
  108. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  109. static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
  110. unsigned char *iv, const unsigned char *input, unsigned char *output )
  111. {
  112. return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
  113. output );
  114. }
  115. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  116. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  117. static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
  118. size_t length, size_t *iv_off, unsigned char *iv,
  119. const unsigned char *input, unsigned char *output )
  120. {
  121. return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
  122. input, output );
  123. }
  124. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  125. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  126. static int aes_crypt_ofb_wrap( void *ctx, size_t length, size_t *iv_off,
  127. unsigned char *iv, const unsigned char *input, unsigned char *output )
  128. {
  129. return mbedtls_aes_crypt_ofb( (mbedtls_aes_context *) ctx, length, iv_off,
  130. iv, input, output );
  131. }
  132. #endif /* MBEDTLS_CIPHER_MODE_OFB */
  133. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  134. static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
  135. unsigned char *nonce_counter, unsigned char *stream_block,
  136. const unsigned char *input, unsigned char *output )
  137. {
  138. return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
  139. stream_block, input, output );
  140. }
  141. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  142. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  143. static int aes_crypt_xts_wrap( void *ctx, mbedtls_operation_t operation,
  144. size_t length,
  145. const unsigned char data_unit[16],
  146. const unsigned char *input,
  147. unsigned char *output )
  148. {
  149. mbedtls_aes_xts_context *xts_ctx = ctx;
  150. int mode;
  151. switch( operation )
  152. {
  153. case MBEDTLS_ENCRYPT:
  154. mode = MBEDTLS_AES_ENCRYPT;
  155. break;
  156. case MBEDTLS_DECRYPT:
  157. mode = MBEDTLS_AES_DECRYPT;
  158. break;
  159. default:
  160. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  161. }
  162. return mbedtls_aes_crypt_xts( xts_ctx, mode, length,
  163. data_unit, input, output );
  164. }
  165. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  166. static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
  167. unsigned int key_bitlen )
  168. {
  169. return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
  170. }
  171. static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
  172. unsigned int key_bitlen )
  173. {
  174. return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
  175. }
  176. static void * aes_ctx_alloc( void )
  177. {
  178. mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
  179. if( aes == NULL )
  180. return( NULL );
  181. mbedtls_aes_init( aes );
  182. return( aes );
  183. }
  184. static void aes_ctx_free( void *ctx )
  185. {
  186. mbedtls_aes_free( (mbedtls_aes_context *) ctx );
  187. mbedtls_free( ctx );
  188. }
  189. static const mbedtls_cipher_base_t aes_info = {
  190. MBEDTLS_CIPHER_ID_AES,
  191. aes_crypt_ecb_wrap,
  192. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  193. aes_crypt_cbc_wrap,
  194. #endif
  195. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  196. aes_crypt_cfb128_wrap,
  197. #endif
  198. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  199. aes_crypt_ofb_wrap,
  200. #endif
  201. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  202. aes_crypt_ctr_wrap,
  203. #endif
  204. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  205. NULL,
  206. #endif
  207. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  208. NULL,
  209. #endif
  210. aes_setkey_enc_wrap,
  211. aes_setkey_dec_wrap,
  212. aes_ctx_alloc,
  213. aes_ctx_free
  214. };
  215. static const mbedtls_cipher_info_t aes_128_ecb_info = {
  216. MBEDTLS_CIPHER_AES_128_ECB,
  217. MBEDTLS_MODE_ECB,
  218. 128,
  219. "AES-128-ECB",
  220. 0,
  221. 0,
  222. 16,
  223. &aes_info
  224. };
  225. static const mbedtls_cipher_info_t aes_192_ecb_info = {
  226. MBEDTLS_CIPHER_AES_192_ECB,
  227. MBEDTLS_MODE_ECB,
  228. 192,
  229. "AES-192-ECB",
  230. 0,
  231. 0,
  232. 16,
  233. &aes_info
  234. };
  235. static const mbedtls_cipher_info_t aes_256_ecb_info = {
  236. MBEDTLS_CIPHER_AES_256_ECB,
  237. MBEDTLS_MODE_ECB,
  238. 256,
  239. "AES-256-ECB",
  240. 0,
  241. 0,
  242. 16,
  243. &aes_info
  244. };
  245. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  246. static const mbedtls_cipher_info_t aes_128_cbc_info = {
  247. MBEDTLS_CIPHER_AES_128_CBC,
  248. MBEDTLS_MODE_CBC,
  249. 128,
  250. "AES-128-CBC",
  251. 16,
  252. 0,
  253. 16,
  254. &aes_info
  255. };
  256. static const mbedtls_cipher_info_t aes_192_cbc_info = {
  257. MBEDTLS_CIPHER_AES_192_CBC,
  258. MBEDTLS_MODE_CBC,
  259. 192,
  260. "AES-192-CBC",
  261. 16,
  262. 0,
  263. 16,
  264. &aes_info
  265. };
  266. static const mbedtls_cipher_info_t aes_256_cbc_info = {
  267. MBEDTLS_CIPHER_AES_256_CBC,
  268. MBEDTLS_MODE_CBC,
  269. 256,
  270. "AES-256-CBC",
  271. 16,
  272. 0,
  273. 16,
  274. &aes_info
  275. };
  276. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  277. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  278. static const mbedtls_cipher_info_t aes_128_cfb128_info = {
  279. MBEDTLS_CIPHER_AES_128_CFB128,
  280. MBEDTLS_MODE_CFB,
  281. 128,
  282. "AES-128-CFB128",
  283. 16,
  284. 0,
  285. 16,
  286. &aes_info
  287. };
  288. static const mbedtls_cipher_info_t aes_192_cfb128_info = {
  289. MBEDTLS_CIPHER_AES_192_CFB128,
  290. MBEDTLS_MODE_CFB,
  291. 192,
  292. "AES-192-CFB128",
  293. 16,
  294. 0,
  295. 16,
  296. &aes_info
  297. };
  298. static const mbedtls_cipher_info_t aes_256_cfb128_info = {
  299. MBEDTLS_CIPHER_AES_256_CFB128,
  300. MBEDTLS_MODE_CFB,
  301. 256,
  302. "AES-256-CFB128",
  303. 16,
  304. 0,
  305. 16,
  306. &aes_info
  307. };
  308. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  309. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  310. static const mbedtls_cipher_info_t aes_128_ofb_info = {
  311. MBEDTLS_CIPHER_AES_128_OFB,
  312. MBEDTLS_MODE_OFB,
  313. 128,
  314. "AES-128-OFB",
  315. 16,
  316. 0,
  317. 16,
  318. &aes_info
  319. };
  320. static const mbedtls_cipher_info_t aes_192_ofb_info = {
  321. MBEDTLS_CIPHER_AES_192_OFB,
  322. MBEDTLS_MODE_OFB,
  323. 192,
  324. "AES-192-OFB",
  325. 16,
  326. 0,
  327. 16,
  328. &aes_info
  329. };
  330. static const mbedtls_cipher_info_t aes_256_ofb_info = {
  331. MBEDTLS_CIPHER_AES_256_OFB,
  332. MBEDTLS_MODE_OFB,
  333. 256,
  334. "AES-256-OFB",
  335. 16,
  336. 0,
  337. 16,
  338. &aes_info
  339. };
  340. #endif /* MBEDTLS_CIPHER_MODE_OFB */
  341. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  342. static const mbedtls_cipher_info_t aes_128_ctr_info = {
  343. MBEDTLS_CIPHER_AES_128_CTR,
  344. MBEDTLS_MODE_CTR,
  345. 128,
  346. "AES-128-CTR",
  347. 16,
  348. 0,
  349. 16,
  350. &aes_info
  351. };
  352. static const mbedtls_cipher_info_t aes_192_ctr_info = {
  353. MBEDTLS_CIPHER_AES_192_CTR,
  354. MBEDTLS_MODE_CTR,
  355. 192,
  356. "AES-192-CTR",
  357. 16,
  358. 0,
  359. 16,
  360. &aes_info
  361. };
  362. static const mbedtls_cipher_info_t aes_256_ctr_info = {
  363. MBEDTLS_CIPHER_AES_256_CTR,
  364. MBEDTLS_MODE_CTR,
  365. 256,
  366. "AES-256-CTR",
  367. 16,
  368. 0,
  369. 16,
  370. &aes_info
  371. };
  372. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  373. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  374. static int xts_aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
  375. unsigned int key_bitlen )
  376. {
  377. mbedtls_aes_xts_context *xts_ctx = ctx;
  378. return( mbedtls_aes_xts_setkey_enc( xts_ctx, key, key_bitlen ) );
  379. }
  380. static int xts_aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
  381. unsigned int key_bitlen )
  382. {
  383. mbedtls_aes_xts_context *xts_ctx = ctx;
  384. return( mbedtls_aes_xts_setkey_dec( xts_ctx, key, key_bitlen ) );
  385. }
  386. static void *xts_aes_ctx_alloc( void )
  387. {
  388. mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc( 1, sizeof( *xts_ctx ) );
  389. if( xts_ctx != NULL )
  390. mbedtls_aes_xts_init( xts_ctx );
  391. return( xts_ctx );
  392. }
  393. static void xts_aes_ctx_free( void *ctx )
  394. {
  395. mbedtls_aes_xts_context *xts_ctx = ctx;
  396. if( xts_ctx == NULL )
  397. return;
  398. mbedtls_aes_xts_free( xts_ctx );
  399. mbedtls_free( xts_ctx );
  400. }
  401. static const mbedtls_cipher_base_t xts_aes_info = {
  402. MBEDTLS_CIPHER_ID_AES,
  403. NULL,
  404. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  405. NULL,
  406. #endif
  407. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  408. NULL,
  409. #endif
  410. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  411. NULL,
  412. #endif
  413. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  414. NULL,
  415. #endif
  416. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  417. aes_crypt_xts_wrap,
  418. #endif
  419. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  420. NULL,
  421. #endif
  422. xts_aes_setkey_enc_wrap,
  423. xts_aes_setkey_dec_wrap,
  424. xts_aes_ctx_alloc,
  425. xts_aes_ctx_free
  426. };
  427. static const mbedtls_cipher_info_t aes_128_xts_info = {
  428. MBEDTLS_CIPHER_AES_128_XTS,
  429. MBEDTLS_MODE_XTS,
  430. 256,
  431. "AES-128-XTS",
  432. 16,
  433. 0,
  434. 16,
  435. &xts_aes_info
  436. };
  437. static const mbedtls_cipher_info_t aes_256_xts_info = {
  438. MBEDTLS_CIPHER_AES_256_XTS,
  439. MBEDTLS_MODE_XTS,
  440. 512,
  441. "AES-256-XTS",
  442. 16,
  443. 0,
  444. 16,
  445. &xts_aes_info
  446. };
  447. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  448. #if defined(MBEDTLS_GCM_C)
  449. static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
  450. unsigned int key_bitlen )
  451. {
  452. return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
  453. key, key_bitlen );
  454. }
  455. static const mbedtls_cipher_base_t gcm_aes_info = {
  456. MBEDTLS_CIPHER_ID_AES,
  457. NULL,
  458. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  459. NULL,
  460. #endif
  461. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  462. NULL,
  463. #endif
  464. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  465. NULL,
  466. #endif
  467. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  468. NULL,
  469. #endif
  470. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  471. NULL,
  472. #endif
  473. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  474. NULL,
  475. #endif
  476. gcm_aes_setkey_wrap,
  477. gcm_aes_setkey_wrap,
  478. gcm_ctx_alloc,
  479. gcm_ctx_free,
  480. };
  481. static const mbedtls_cipher_info_t aes_128_gcm_info = {
  482. MBEDTLS_CIPHER_AES_128_GCM,
  483. MBEDTLS_MODE_GCM,
  484. 128,
  485. "AES-128-GCM",
  486. 12,
  487. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  488. 16,
  489. &gcm_aes_info
  490. };
  491. static const mbedtls_cipher_info_t aes_192_gcm_info = {
  492. MBEDTLS_CIPHER_AES_192_GCM,
  493. MBEDTLS_MODE_GCM,
  494. 192,
  495. "AES-192-GCM",
  496. 12,
  497. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  498. 16,
  499. &gcm_aes_info
  500. };
  501. static const mbedtls_cipher_info_t aes_256_gcm_info = {
  502. MBEDTLS_CIPHER_AES_256_GCM,
  503. MBEDTLS_MODE_GCM,
  504. 256,
  505. "AES-256-GCM",
  506. 12,
  507. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  508. 16,
  509. &gcm_aes_info
  510. };
  511. #endif /* MBEDTLS_GCM_C */
  512. #if defined(MBEDTLS_CCM_C)
  513. static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
  514. unsigned int key_bitlen )
  515. {
  516. return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
  517. key, key_bitlen );
  518. }
  519. static const mbedtls_cipher_base_t ccm_aes_info = {
  520. MBEDTLS_CIPHER_ID_AES,
  521. NULL,
  522. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  523. NULL,
  524. #endif
  525. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  526. NULL,
  527. #endif
  528. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  529. NULL,
  530. #endif
  531. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  532. NULL,
  533. #endif
  534. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  535. NULL,
  536. #endif
  537. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  538. NULL,
  539. #endif
  540. ccm_aes_setkey_wrap,
  541. ccm_aes_setkey_wrap,
  542. ccm_ctx_alloc,
  543. ccm_ctx_free,
  544. };
  545. static const mbedtls_cipher_info_t aes_128_ccm_info = {
  546. MBEDTLS_CIPHER_AES_128_CCM,
  547. MBEDTLS_MODE_CCM,
  548. 128,
  549. "AES-128-CCM",
  550. 12,
  551. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  552. 16,
  553. &ccm_aes_info
  554. };
  555. static const mbedtls_cipher_info_t aes_192_ccm_info = {
  556. MBEDTLS_CIPHER_AES_192_CCM,
  557. MBEDTLS_MODE_CCM,
  558. 192,
  559. "AES-192-CCM",
  560. 12,
  561. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  562. 16,
  563. &ccm_aes_info
  564. };
  565. static const mbedtls_cipher_info_t aes_256_ccm_info = {
  566. MBEDTLS_CIPHER_AES_256_CCM,
  567. MBEDTLS_MODE_CCM,
  568. 256,
  569. "AES-256-CCM",
  570. 12,
  571. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  572. 16,
  573. &ccm_aes_info
  574. };
  575. #endif /* MBEDTLS_CCM_C */
  576. #endif /* MBEDTLS_AES_C */
  577. #if defined(MBEDTLS_CAMELLIA_C)
  578. static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  579. const unsigned char *input, unsigned char *output )
  580. {
  581. return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
  582. output );
  583. }
  584. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  585. static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
  586. size_t length, unsigned char *iv,
  587. const unsigned char *input, unsigned char *output )
  588. {
  589. return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
  590. input, output );
  591. }
  592. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  593. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  594. static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
  595. size_t length, size_t *iv_off, unsigned char *iv,
  596. const unsigned char *input, unsigned char *output )
  597. {
  598. return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
  599. iv_off, iv, input, output );
  600. }
  601. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  602. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  603. static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
  604. unsigned char *nonce_counter, unsigned char *stream_block,
  605. const unsigned char *input, unsigned char *output )
  606. {
  607. return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
  608. nonce_counter, stream_block, input, output );
  609. }
  610. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  611. static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
  612. unsigned int key_bitlen )
  613. {
  614. return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
  615. }
  616. static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
  617. unsigned int key_bitlen )
  618. {
  619. return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
  620. }
  621. static void * camellia_ctx_alloc( void )
  622. {
  623. mbedtls_camellia_context *ctx;
  624. ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
  625. if( ctx == NULL )
  626. return( NULL );
  627. mbedtls_camellia_init( ctx );
  628. return( ctx );
  629. }
  630. static void camellia_ctx_free( void *ctx )
  631. {
  632. mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
  633. mbedtls_free( ctx );
  634. }
  635. static const mbedtls_cipher_base_t camellia_info = {
  636. MBEDTLS_CIPHER_ID_CAMELLIA,
  637. camellia_crypt_ecb_wrap,
  638. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  639. camellia_crypt_cbc_wrap,
  640. #endif
  641. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  642. camellia_crypt_cfb128_wrap,
  643. #endif
  644. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  645. NULL,
  646. #endif
  647. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  648. camellia_crypt_ctr_wrap,
  649. #endif
  650. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  651. NULL,
  652. #endif
  653. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  654. NULL,
  655. #endif
  656. camellia_setkey_enc_wrap,
  657. camellia_setkey_dec_wrap,
  658. camellia_ctx_alloc,
  659. camellia_ctx_free
  660. };
  661. static const mbedtls_cipher_info_t camellia_128_ecb_info = {
  662. MBEDTLS_CIPHER_CAMELLIA_128_ECB,
  663. MBEDTLS_MODE_ECB,
  664. 128,
  665. "CAMELLIA-128-ECB",
  666. 16,
  667. 0,
  668. 16,
  669. &camellia_info
  670. };
  671. static const mbedtls_cipher_info_t camellia_192_ecb_info = {
  672. MBEDTLS_CIPHER_CAMELLIA_192_ECB,
  673. MBEDTLS_MODE_ECB,
  674. 192,
  675. "CAMELLIA-192-ECB",
  676. 16,
  677. 0,
  678. 16,
  679. &camellia_info
  680. };
  681. static const mbedtls_cipher_info_t camellia_256_ecb_info = {
  682. MBEDTLS_CIPHER_CAMELLIA_256_ECB,
  683. MBEDTLS_MODE_ECB,
  684. 256,
  685. "CAMELLIA-256-ECB",
  686. 16,
  687. 0,
  688. 16,
  689. &camellia_info
  690. };
  691. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  692. static const mbedtls_cipher_info_t camellia_128_cbc_info = {
  693. MBEDTLS_CIPHER_CAMELLIA_128_CBC,
  694. MBEDTLS_MODE_CBC,
  695. 128,
  696. "CAMELLIA-128-CBC",
  697. 16,
  698. 0,
  699. 16,
  700. &camellia_info
  701. };
  702. static const mbedtls_cipher_info_t camellia_192_cbc_info = {
  703. MBEDTLS_CIPHER_CAMELLIA_192_CBC,
  704. MBEDTLS_MODE_CBC,
  705. 192,
  706. "CAMELLIA-192-CBC",
  707. 16,
  708. 0,
  709. 16,
  710. &camellia_info
  711. };
  712. static const mbedtls_cipher_info_t camellia_256_cbc_info = {
  713. MBEDTLS_CIPHER_CAMELLIA_256_CBC,
  714. MBEDTLS_MODE_CBC,
  715. 256,
  716. "CAMELLIA-256-CBC",
  717. 16,
  718. 0,
  719. 16,
  720. &camellia_info
  721. };
  722. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  723. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  724. static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
  725. MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
  726. MBEDTLS_MODE_CFB,
  727. 128,
  728. "CAMELLIA-128-CFB128",
  729. 16,
  730. 0,
  731. 16,
  732. &camellia_info
  733. };
  734. static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
  735. MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
  736. MBEDTLS_MODE_CFB,
  737. 192,
  738. "CAMELLIA-192-CFB128",
  739. 16,
  740. 0,
  741. 16,
  742. &camellia_info
  743. };
  744. static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
  745. MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
  746. MBEDTLS_MODE_CFB,
  747. 256,
  748. "CAMELLIA-256-CFB128",
  749. 16,
  750. 0,
  751. 16,
  752. &camellia_info
  753. };
  754. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  755. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  756. static const mbedtls_cipher_info_t camellia_128_ctr_info = {
  757. MBEDTLS_CIPHER_CAMELLIA_128_CTR,
  758. MBEDTLS_MODE_CTR,
  759. 128,
  760. "CAMELLIA-128-CTR",
  761. 16,
  762. 0,
  763. 16,
  764. &camellia_info
  765. };
  766. static const mbedtls_cipher_info_t camellia_192_ctr_info = {
  767. MBEDTLS_CIPHER_CAMELLIA_192_CTR,
  768. MBEDTLS_MODE_CTR,
  769. 192,
  770. "CAMELLIA-192-CTR",
  771. 16,
  772. 0,
  773. 16,
  774. &camellia_info
  775. };
  776. static const mbedtls_cipher_info_t camellia_256_ctr_info = {
  777. MBEDTLS_CIPHER_CAMELLIA_256_CTR,
  778. MBEDTLS_MODE_CTR,
  779. 256,
  780. "CAMELLIA-256-CTR",
  781. 16,
  782. 0,
  783. 16,
  784. &camellia_info
  785. };
  786. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  787. #if defined(MBEDTLS_GCM_C)
  788. static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
  789. unsigned int key_bitlen )
  790. {
  791. return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
  792. key, key_bitlen );
  793. }
  794. static const mbedtls_cipher_base_t gcm_camellia_info = {
  795. MBEDTLS_CIPHER_ID_CAMELLIA,
  796. NULL,
  797. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  798. NULL,
  799. #endif
  800. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  801. NULL,
  802. #endif
  803. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  804. NULL,
  805. #endif
  806. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  807. NULL,
  808. #endif
  809. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  810. NULL,
  811. #endif
  812. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  813. NULL,
  814. #endif
  815. gcm_camellia_setkey_wrap,
  816. gcm_camellia_setkey_wrap,
  817. gcm_ctx_alloc,
  818. gcm_ctx_free,
  819. };
  820. static const mbedtls_cipher_info_t camellia_128_gcm_info = {
  821. MBEDTLS_CIPHER_CAMELLIA_128_GCM,
  822. MBEDTLS_MODE_GCM,
  823. 128,
  824. "CAMELLIA-128-GCM",
  825. 12,
  826. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  827. 16,
  828. &gcm_camellia_info
  829. };
  830. static const mbedtls_cipher_info_t camellia_192_gcm_info = {
  831. MBEDTLS_CIPHER_CAMELLIA_192_GCM,
  832. MBEDTLS_MODE_GCM,
  833. 192,
  834. "CAMELLIA-192-GCM",
  835. 12,
  836. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  837. 16,
  838. &gcm_camellia_info
  839. };
  840. static const mbedtls_cipher_info_t camellia_256_gcm_info = {
  841. MBEDTLS_CIPHER_CAMELLIA_256_GCM,
  842. MBEDTLS_MODE_GCM,
  843. 256,
  844. "CAMELLIA-256-GCM",
  845. 12,
  846. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  847. 16,
  848. &gcm_camellia_info
  849. };
  850. #endif /* MBEDTLS_GCM_C */
  851. #if defined(MBEDTLS_CCM_C)
  852. static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
  853. unsigned int key_bitlen )
  854. {
  855. return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
  856. key, key_bitlen );
  857. }
  858. static const mbedtls_cipher_base_t ccm_camellia_info = {
  859. MBEDTLS_CIPHER_ID_CAMELLIA,
  860. NULL,
  861. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  862. NULL,
  863. #endif
  864. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  865. NULL,
  866. #endif
  867. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  868. NULL,
  869. #endif
  870. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  871. NULL,
  872. #endif
  873. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  874. NULL,
  875. #endif
  876. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  877. NULL,
  878. #endif
  879. ccm_camellia_setkey_wrap,
  880. ccm_camellia_setkey_wrap,
  881. ccm_ctx_alloc,
  882. ccm_ctx_free,
  883. };
  884. static const mbedtls_cipher_info_t camellia_128_ccm_info = {
  885. MBEDTLS_CIPHER_CAMELLIA_128_CCM,
  886. MBEDTLS_MODE_CCM,
  887. 128,
  888. "CAMELLIA-128-CCM",
  889. 12,
  890. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  891. 16,
  892. &ccm_camellia_info
  893. };
  894. static const mbedtls_cipher_info_t camellia_192_ccm_info = {
  895. MBEDTLS_CIPHER_CAMELLIA_192_CCM,
  896. MBEDTLS_MODE_CCM,
  897. 192,
  898. "CAMELLIA-192-CCM",
  899. 12,
  900. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  901. 16,
  902. &ccm_camellia_info
  903. };
  904. static const mbedtls_cipher_info_t camellia_256_ccm_info = {
  905. MBEDTLS_CIPHER_CAMELLIA_256_CCM,
  906. MBEDTLS_MODE_CCM,
  907. 256,
  908. "CAMELLIA-256-CCM",
  909. 12,
  910. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  911. 16,
  912. &ccm_camellia_info
  913. };
  914. #endif /* MBEDTLS_CCM_C */
  915. #endif /* MBEDTLS_CAMELLIA_C */
  916. #if defined(MBEDTLS_ARIA_C)
  917. static int aria_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  918. const unsigned char *input, unsigned char *output )
  919. {
  920. (void) operation;
  921. return mbedtls_aria_crypt_ecb( (mbedtls_aria_context *) ctx, input,
  922. output );
  923. }
  924. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  925. static int aria_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
  926. size_t length, unsigned char *iv,
  927. const unsigned char *input, unsigned char *output )
  928. {
  929. return mbedtls_aria_crypt_cbc( (mbedtls_aria_context *) ctx, operation, length, iv,
  930. input, output );
  931. }
  932. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  933. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  934. static int aria_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
  935. size_t length, size_t *iv_off, unsigned char *iv,
  936. const unsigned char *input, unsigned char *output )
  937. {
  938. return mbedtls_aria_crypt_cfb128( (mbedtls_aria_context *) ctx, operation, length,
  939. iv_off, iv, input, output );
  940. }
  941. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  942. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  943. static int aria_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
  944. unsigned char *nonce_counter, unsigned char *stream_block,
  945. const unsigned char *input, unsigned char *output )
  946. {
  947. return mbedtls_aria_crypt_ctr( (mbedtls_aria_context *) ctx, length, nc_off,
  948. nonce_counter, stream_block, input, output );
  949. }
  950. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  951. static int aria_setkey_dec_wrap( void *ctx, const unsigned char *key,
  952. unsigned int key_bitlen )
  953. {
  954. return mbedtls_aria_setkey_dec( (mbedtls_aria_context *) ctx, key, key_bitlen );
  955. }
  956. static int aria_setkey_enc_wrap( void *ctx, const unsigned char *key,
  957. unsigned int key_bitlen )
  958. {
  959. return mbedtls_aria_setkey_enc( (mbedtls_aria_context *) ctx, key, key_bitlen );
  960. }
  961. static void * aria_ctx_alloc( void )
  962. {
  963. mbedtls_aria_context *ctx;
  964. ctx = mbedtls_calloc( 1, sizeof( mbedtls_aria_context ) );
  965. if( ctx == NULL )
  966. return( NULL );
  967. mbedtls_aria_init( ctx );
  968. return( ctx );
  969. }
  970. static void aria_ctx_free( void *ctx )
  971. {
  972. mbedtls_aria_free( (mbedtls_aria_context *) ctx );
  973. mbedtls_free( ctx );
  974. }
  975. static const mbedtls_cipher_base_t aria_info = {
  976. MBEDTLS_CIPHER_ID_ARIA,
  977. aria_crypt_ecb_wrap,
  978. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  979. aria_crypt_cbc_wrap,
  980. #endif
  981. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  982. aria_crypt_cfb128_wrap,
  983. #endif
  984. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  985. NULL,
  986. #endif
  987. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  988. aria_crypt_ctr_wrap,
  989. #endif
  990. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  991. NULL,
  992. #endif
  993. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  994. NULL,
  995. #endif
  996. aria_setkey_enc_wrap,
  997. aria_setkey_dec_wrap,
  998. aria_ctx_alloc,
  999. aria_ctx_free
  1000. };
  1001. static const mbedtls_cipher_info_t aria_128_ecb_info = {
  1002. MBEDTLS_CIPHER_ARIA_128_ECB,
  1003. MBEDTLS_MODE_ECB,
  1004. 128,
  1005. "ARIA-128-ECB",
  1006. 16,
  1007. 0,
  1008. 16,
  1009. &aria_info
  1010. };
  1011. static const mbedtls_cipher_info_t aria_192_ecb_info = {
  1012. MBEDTLS_CIPHER_ARIA_192_ECB,
  1013. MBEDTLS_MODE_ECB,
  1014. 192,
  1015. "ARIA-192-ECB",
  1016. 16,
  1017. 0,
  1018. 16,
  1019. &aria_info
  1020. };
  1021. static const mbedtls_cipher_info_t aria_256_ecb_info = {
  1022. MBEDTLS_CIPHER_ARIA_256_ECB,
  1023. MBEDTLS_MODE_ECB,
  1024. 256,
  1025. "ARIA-256-ECB",
  1026. 16,
  1027. 0,
  1028. 16,
  1029. &aria_info
  1030. };
  1031. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1032. static const mbedtls_cipher_info_t aria_128_cbc_info = {
  1033. MBEDTLS_CIPHER_ARIA_128_CBC,
  1034. MBEDTLS_MODE_CBC,
  1035. 128,
  1036. "ARIA-128-CBC",
  1037. 16,
  1038. 0,
  1039. 16,
  1040. &aria_info
  1041. };
  1042. static const mbedtls_cipher_info_t aria_192_cbc_info = {
  1043. MBEDTLS_CIPHER_ARIA_192_CBC,
  1044. MBEDTLS_MODE_CBC,
  1045. 192,
  1046. "ARIA-192-CBC",
  1047. 16,
  1048. 0,
  1049. 16,
  1050. &aria_info
  1051. };
  1052. static const mbedtls_cipher_info_t aria_256_cbc_info = {
  1053. MBEDTLS_CIPHER_ARIA_256_CBC,
  1054. MBEDTLS_MODE_CBC,
  1055. 256,
  1056. "ARIA-256-CBC",
  1057. 16,
  1058. 0,
  1059. 16,
  1060. &aria_info
  1061. };
  1062. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1063. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1064. static const mbedtls_cipher_info_t aria_128_cfb128_info = {
  1065. MBEDTLS_CIPHER_ARIA_128_CFB128,
  1066. MBEDTLS_MODE_CFB,
  1067. 128,
  1068. "ARIA-128-CFB128",
  1069. 16,
  1070. 0,
  1071. 16,
  1072. &aria_info
  1073. };
  1074. static const mbedtls_cipher_info_t aria_192_cfb128_info = {
  1075. MBEDTLS_CIPHER_ARIA_192_CFB128,
  1076. MBEDTLS_MODE_CFB,
  1077. 192,
  1078. "ARIA-192-CFB128",
  1079. 16,
  1080. 0,
  1081. 16,
  1082. &aria_info
  1083. };
  1084. static const mbedtls_cipher_info_t aria_256_cfb128_info = {
  1085. MBEDTLS_CIPHER_ARIA_256_CFB128,
  1086. MBEDTLS_MODE_CFB,
  1087. 256,
  1088. "ARIA-256-CFB128",
  1089. 16,
  1090. 0,
  1091. 16,
  1092. &aria_info
  1093. };
  1094. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  1095. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1096. static const mbedtls_cipher_info_t aria_128_ctr_info = {
  1097. MBEDTLS_CIPHER_ARIA_128_CTR,
  1098. MBEDTLS_MODE_CTR,
  1099. 128,
  1100. "ARIA-128-CTR",
  1101. 16,
  1102. 0,
  1103. 16,
  1104. &aria_info
  1105. };
  1106. static const mbedtls_cipher_info_t aria_192_ctr_info = {
  1107. MBEDTLS_CIPHER_ARIA_192_CTR,
  1108. MBEDTLS_MODE_CTR,
  1109. 192,
  1110. "ARIA-192-CTR",
  1111. 16,
  1112. 0,
  1113. 16,
  1114. &aria_info
  1115. };
  1116. static const mbedtls_cipher_info_t aria_256_ctr_info = {
  1117. MBEDTLS_CIPHER_ARIA_256_CTR,
  1118. MBEDTLS_MODE_CTR,
  1119. 256,
  1120. "ARIA-256-CTR",
  1121. 16,
  1122. 0,
  1123. 16,
  1124. &aria_info
  1125. };
  1126. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  1127. #if defined(MBEDTLS_GCM_C)
  1128. static int gcm_aria_setkey_wrap( void *ctx, const unsigned char *key,
  1129. unsigned int key_bitlen )
  1130. {
  1131. return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
  1132. key, key_bitlen );
  1133. }
  1134. static const mbedtls_cipher_base_t gcm_aria_info = {
  1135. MBEDTLS_CIPHER_ID_ARIA,
  1136. NULL,
  1137. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1138. NULL,
  1139. #endif
  1140. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1141. NULL,
  1142. #endif
  1143. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1144. NULL,
  1145. #endif
  1146. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1147. NULL,
  1148. #endif
  1149. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1150. NULL,
  1151. #endif
  1152. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1153. NULL,
  1154. #endif
  1155. gcm_aria_setkey_wrap,
  1156. gcm_aria_setkey_wrap,
  1157. gcm_ctx_alloc,
  1158. gcm_ctx_free,
  1159. };
  1160. static const mbedtls_cipher_info_t aria_128_gcm_info = {
  1161. MBEDTLS_CIPHER_ARIA_128_GCM,
  1162. MBEDTLS_MODE_GCM,
  1163. 128,
  1164. "ARIA-128-GCM",
  1165. 12,
  1166. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  1167. 16,
  1168. &gcm_aria_info
  1169. };
  1170. static const mbedtls_cipher_info_t aria_192_gcm_info = {
  1171. MBEDTLS_CIPHER_ARIA_192_GCM,
  1172. MBEDTLS_MODE_GCM,
  1173. 192,
  1174. "ARIA-192-GCM",
  1175. 12,
  1176. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  1177. 16,
  1178. &gcm_aria_info
  1179. };
  1180. static const mbedtls_cipher_info_t aria_256_gcm_info = {
  1181. MBEDTLS_CIPHER_ARIA_256_GCM,
  1182. MBEDTLS_MODE_GCM,
  1183. 256,
  1184. "ARIA-256-GCM",
  1185. 12,
  1186. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  1187. 16,
  1188. &gcm_aria_info
  1189. };
  1190. #endif /* MBEDTLS_GCM_C */
  1191. #if defined(MBEDTLS_CCM_C)
  1192. static int ccm_aria_setkey_wrap( void *ctx, const unsigned char *key,
  1193. unsigned int key_bitlen )
  1194. {
  1195. return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
  1196. key, key_bitlen );
  1197. }
  1198. static const mbedtls_cipher_base_t ccm_aria_info = {
  1199. MBEDTLS_CIPHER_ID_ARIA,
  1200. NULL,
  1201. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1202. NULL,
  1203. #endif
  1204. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1205. NULL,
  1206. #endif
  1207. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1208. NULL,
  1209. #endif
  1210. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1211. NULL,
  1212. #endif
  1213. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1214. NULL,
  1215. #endif
  1216. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1217. NULL,
  1218. #endif
  1219. ccm_aria_setkey_wrap,
  1220. ccm_aria_setkey_wrap,
  1221. ccm_ctx_alloc,
  1222. ccm_ctx_free,
  1223. };
  1224. static const mbedtls_cipher_info_t aria_128_ccm_info = {
  1225. MBEDTLS_CIPHER_ARIA_128_CCM,
  1226. MBEDTLS_MODE_CCM,
  1227. 128,
  1228. "ARIA-128-CCM",
  1229. 12,
  1230. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  1231. 16,
  1232. &ccm_aria_info
  1233. };
  1234. static const mbedtls_cipher_info_t aria_192_ccm_info = {
  1235. MBEDTLS_CIPHER_ARIA_192_CCM,
  1236. MBEDTLS_MODE_CCM,
  1237. 192,
  1238. "ARIA-192-CCM",
  1239. 12,
  1240. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  1241. 16,
  1242. &ccm_aria_info
  1243. };
  1244. static const mbedtls_cipher_info_t aria_256_ccm_info = {
  1245. MBEDTLS_CIPHER_ARIA_256_CCM,
  1246. MBEDTLS_MODE_CCM,
  1247. 256,
  1248. "ARIA-256-CCM",
  1249. 12,
  1250. MBEDTLS_CIPHER_VARIABLE_IV_LEN,
  1251. 16,
  1252. &ccm_aria_info
  1253. };
  1254. #endif /* MBEDTLS_CCM_C */
  1255. #endif /* MBEDTLS_ARIA_C */
  1256. #if defined(MBEDTLS_DES_C)
  1257. static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  1258. const unsigned char *input, unsigned char *output )
  1259. {
  1260. ((void) operation);
  1261. return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
  1262. }
  1263. static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  1264. const unsigned char *input, unsigned char *output )
  1265. {
  1266. ((void) operation);
  1267. return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
  1268. }
  1269. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1270. static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
  1271. unsigned char *iv, const unsigned char *input, unsigned char *output )
  1272. {
  1273. return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
  1274. output );
  1275. }
  1276. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1277. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1278. static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
  1279. unsigned char *iv, const unsigned char *input, unsigned char *output )
  1280. {
  1281. return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
  1282. output );
  1283. }
  1284. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1285. static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
  1286. unsigned int key_bitlen )
  1287. {
  1288. ((void) key_bitlen);
  1289. return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
  1290. }
  1291. static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
  1292. unsigned int key_bitlen )
  1293. {
  1294. ((void) key_bitlen);
  1295. return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
  1296. }
  1297. static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
  1298. unsigned int key_bitlen )
  1299. {
  1300. ((void) key_bitlen);
  1301. return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
  1302. }
  1303. static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
  1304. unsigned int key_bitlen )
  1305. {
  1306. ((void) key_bitlen);
  1307. return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
  1308. }
  1309. static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
  1310. unsigned int key_bitlen )
  1311. {
  1312. ((void) key_bitlen);
  1313. return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
  1314. }
  1315. static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
  1316. unsigned int key_bitlen )
  1317. {
  1318. ((void) key_bitlen);
  1319. return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
  1320. }
  1321. static void * des_ctx_alloc( void )
  1322. {
  1323. mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
  1324. if( des == NULL )
  1325. return( NULL );
  1326. mbedtls_des_init( des );
  1327. return( des );
  1328. }
  1329. static void des_ctx_free( void *ctx )
  1330. {
  1331. mbedtls_des_free( (mbedtls_des_context *) ctx );
  1332. mbedtls_free( ctx );
  1333. }
  1334. static void * des3_ctx_alloc( void )
  1335. {
  1336. mbedtls_des3_context *des3;
  1337. des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
  1338. if( des3 == NULL )
  1339. return( NULL );
  1340. mbedtls_des3_init( des3 );
  1341. return( des3 );
  1342. }
  1343. static void des3_ctx_free( void *ctx )
  1344. {
  1345. mbedtls_des3_free( (mbedtls_des3_context *) ctx );
  1346. mbedtls_free( ctx );
  1347. }
  1348. static const mbedtls_cipher_base_t des_info = {
  1349. MBEDTLS_CIPHER_ID_DES,
  1350. des_crypt_ecb_wrap,
  1351. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1352. des_crypt_cbc_wrap,
  1353. #endif
  1354. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1355. NULL,
  1356. #endif
  1357. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1358. NULL,
  1359. #endif
  1360. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1361. NULL,
  1362. #endif
  1363. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1364. NULL,
  1365. #endif
  1366. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1367. NULL,
  1368. #endif
  1369. des_setkey_enc_wrap,
  1370. des_setkey_dec_wrap,
  1371. des_ctx_alloc,
  1372. des_ctx_free
  1373. };
  1374. static const mbedtls_cipher_info_t des_ecb_info = {
  1375. MBEDTLS_CIPHER_DES_ECB,
  1376. MBEDTLS_MODE_ECB,
  1377. MBEDTLS_KEY_LENGTH_DES,
  1378. "DES-ECB",
  1379. 8,
  1380. 0,
  1381. 8,
  1382. &des_info
  1383. };
  1384. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1385. static const mbedtls_cipher_info_t des_cbc_info = {
  1386. MBEDTLS_CIPHER_DES_CBC,
  1387. MBEDTLS_MODE_CBC,
  1388. MBEDTLS_KEY_LENGTH_DES,
  1389. "DES-CBC",
  1390. 8,
  1391. 0,
  1392. 8,
  1393. &des_info
  1394. };
  1395. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1396. static const mbedtls_cipher_base_t des_ede_info = {
  1397. MBEDTLS_CIPHER_ID_DES,
  1398. des3_crypt_ecb_wrap,
  1399. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1400. des3_crypt_cbc_wrap,
  1401. #endif
  1402. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1403. NULL,
  1404. #endif
  1405. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1406. NULL,
  1407. #endif
  1408. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1409. NULL,
  1410. #endif
  1411. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1412. NULL,
  1413. #endif
  1414. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1415. NULL,
  1416. #endif
  1417. des3_set2key_enc_wrap,
  1418. des3_set2key_dec_wrap,
  1419. des3_ctx_alloc,
  1420. des3_ctx_free
  1421. };
  1422. static const mbedtls_cipher_info_t des_ede_ecb_info = {
  1423. MBEDTLS_CIPHER_DES_EDE_ECB,
  1424. MBEDTLS_MODE_ECB,
  1425. MBEDTLS_KEY_LENGTH_DES_EDE,
  1426. "DES-EDE-ECB",
  1427. 8,
  1428. 0,
  1429. 8,
  1430. &des_ede_info
  1431. };
  1432. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1433. static const mbedtls_cipher_info_t des_ede_cbc_info = {
  1434. MBEDTLS_CIPHER_DES_EDE_CBC,
  1435. MBEDTLS_MODE_CBC,
  1436. MBEDTLS_KEY_LENGTH_DES_EDE,
  1437. "DES-EDE-CBC",
  1438. 8,
  1439. 0,
  1440. 8,
  1441. &des_ede_info
  1442. };
  1443. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1444. static const mbedtls_cipher_base_t des_ede3_info = {
  1445. MBEDTLS_CIPHER_ID_3DES,
  1446. des3_crypt_ecb_wrap,
  1447. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1448. des3_crypt_cbc_wrap,
  1449. #endif
  1450. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1451. NULL,
  1452. #endif
  1453. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1454. NULL,
  1455. #endif
  1456. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1457. NULL,
  1458. #endif
  1459. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1460. NULL,
  1461. #endif
  1462. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1463. NULL,
  1464. #endif
  1465. des3_set3key_enc_wrap,
  1466. des3_set3key_dec_wrap,
  1467. des3_ctx_alloc,
  1468. des3_ctx_free
  1469. };
  1470. static const mbedtls_cipher_info_t des_ede3_ecb_info = {
  1471. MBEDTLS_CIPHER_DES_EDE3_ECB,
  1472. MBEDTLS_MODE_ECB,
  1473. MBEDTLS_KEY_LENGTH_DES_EDE3,
  1474. "DES-EDE3-ECB",
  1475. 8,
  1476. 0,
  1477. 8,
  1478. &des_ede3_info
  1479. };
  1480. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1481. static const mbedtls_cipher_info_t des_ede3_cbc_info = {
  1482. MBEDTLS_CIPHER_DES_EDE3_CBC,
  1483. MBEDTLS_MODE_CBC,
  1484. MBEDTLS_KEY_LENGTH_DES_EDE3,
  1485. "DES-EDE3-CBC",
  1486. 8,
  1487. 0,
  1488. 8,
  1489. &des_ede3_info
  1490. };
  1491. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1492. #endif /* MBEDTLS_DES_C */
  1493. #if defined(MBEDTLS_BLOWFISH_C)
  1494. static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
  1495. const unsigned char *input, unsigned char *output )
  1496. {
  1497. return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
  1498. output );
  1499. }
  1500. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1501. static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
  1502. size_t length, unsigned char *iv, const unsigned char *input,
  1503. unsigned char *output )
  1504. {
  1505. return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
  1506. input, output );
  1507. }
  1508. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1509. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1510. static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
  1511. size_t length, size_t *iv_off, unsigned char *iv,
  1512. const unsigned char *input, unsigned char *output )
  1513. {
  1514. return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
  1515. iv_off, iv, input, output );
  1516. }
  1517. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  1518. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1519. static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
  1520. unsigned char *nonce_counter, unsigned char *stream_block,
  1521. const unsigned char *input, unsigned char *output )
  1522. {
  1523. return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
  1524. nonce_counter, stream_block, input, output );
  1525. }
  1526. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  1527. static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
  1528. unsigned int key_bitlen )
  1529. {
  1530. return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
  1531. }
  1532. static void * blowfish_ctx_alloc( void )
  1533. {
  1534. mbedtls_blowfish_context *ctx;
  1535. ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
  1536. if( ctx == NULL )
  1537. return( NULL );
  1538. mbedtls_blowfish_init( ctx );
  1539. return( ctx );
  1540. }
  1541. static void blowfish_ctx_free( void *ctx )
  1542. {
  1543. mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
  1544. mbedtls_free( ctx );
  1545. }
  1546. static const mbedtls_cipher_base_t blowfish_info = {
  1547. MBEDTLS_CIPHER_ID_BLOWFISH,
  1548. blowfish_crypt_ecb_wrap,
  1549. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1550. blowfish_crypt_cbc_wrap,
  1551. #endif
  1552. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1553. blowfish_crypt_cfb64_wrap,
  1554. #endif
  1555. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1556. NULL,
  1557. #endif
  1558. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1559. blowfish_crypt_ctr_wrap,
  1560. #endif
  1561. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1562. NULL,
  1563. #endif
  1564. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1565. NULL,
  1566. #endif
  1567. blowfish_setkey_wrap,
  1568. blowfish_setkey_wrap,
  1569. blowfish_ctx_alloc,
  1570. blowfish_ctx_free
  1571. };
  1572. static const mbedtls_cipher_info_t blowfish_ecb_info = {
  1573. MBEDTLS_CIPHER_BLOWFISH_ECB,
  1574. MBEDTLS_MODE_ECB,
  1575. 128,
  1576. "BLOWFISH-ECB",
  1577. 8,
  1578. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  1579. 8,
  1580. &blowfish_info
  1581. };
  1582. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1583. static const mbedtls_cipher_info_t blowfish_cbc_info = {
  1584. MBEDTLS_CIPHER_BLOWFISH_CBC,
  1585. MBEDTLS_MODE_CBC,
  1586. 128,
  1587. "BLOWFISH-CBC",
  1588. 8,
  1589. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  1590. 8,
  1591. &blowfish_info
  1592. };
  1593. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1594. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1595. static const mbedtls_cipher_info_t blowfish_cfb64_info = {
  1596. MBEDTLS_CIPHER_BLOWFISH_CFB64,
  1597. MBEDTLS_MODE_CFB,
  1598. 128,
  1599. "BLOWFISH-CFB64",
  1600. 8,
  1601. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  1602. 8,
  1603. &blowfish_info
  1604. };
  1605. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  1606. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1607. static const mbedtls_cipher_info_t blowfish_ctr_info = {
  1608. MBEDTLS_CIPHER_BLOWFISH_CTR,
  1609. MBEDTLS_MODE_CTR,
  1610. 128,
  1611. "BLOWFISH-CTR",
  1612. 8,
  1613. MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
  1614. 8,
  1615. &blowfish_info
  1616. };
  1617. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  1618. #endif /* MBEDTLS_BLOWFISH_C */
  1619. #if defined(MBEDTLS_ARC4_C)
  1620. static int arc4_crypt_stream_wrap( void *ctx, size_t length,
  1621. const unsigned char *input,
  1622. unsigned char *output )
  1623. {
  1624. return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
  1625. }
  1626. static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
  1627. unsigned int key_bitlen )
  1628. {
  1629. /* we get key_bitlen in bits, arc4 expects it in bytes */
  1630. if( key_bitlen % 8 != 0 )
  1631. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1632. mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
  1633. return( 0 );
  1634. }
  1635. static void * arc4_ctx_alloc( void )
  1636. {
  1637. mbedtls_arc4_context *ctx;
  1638. ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
  1639. if( ctx == NULL )
  1640. return( NULL );
  1641. mbedtls_arc4_init( ctx );
  1642. return( ctx );
  1643. }
  1644. static void arc4_ctx_free( void *ctx )
  1645. {
  1646. mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
  1647. mbedtls_free( ctx );
  1648. }
  1649. static const mbedtls_cipher_base_t arc4_base_info = {
  1650. MBEDTLS_CIPHER_ID_ARC4,
  1651. NULL,
  1652. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1653. NULL,
  1654. #endif
  1655. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1656. NULL,
  1657. #endif
  1658. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1659. NULL,
  1660. #endif
  1661. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1662. NULL,
  1663. #endif
  1664. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1665. NULL,
  1666. #endif
  1667. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1668. arc4_crypt_stream_wrap,
  1669. #endif
  1670. arc4_setkey_wrap,
  1671. arc4_setkey_wrap,
  1672. arc4_ctx_alloc,
  1673. arc4_ctx_free
  1674. };
  1675. static const mbedtls_cipher_info_t arc4_128_info = {
  1676. MBEDTLS_CIPHER_ARC4_128,
  1677. MBEDTLS_MODE_STREAM,
  1678. 128,
  1679. "ARC4-128",
  1680. 0,
  1681. 0,
  1682. 1,
  1683. &arc4_base_info
  1684. };
  1685. #endif /* MBEDTLS_ARC4_C */
  1686. #if defined(MBEDTLS_CHACHA20_C)
  1687. static int chacha20_setkey_wrap( void *ctx, const unsigned char *key,
  1688. unsigned int key_bitlen )
  1689. {
  1690. if( key_bitlen != 256U )
  1691. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1692. if ( 0 != mbedtls_chacha20_setkey( (mbedtls_chacha20_context*)ctx, key ) )
  1693. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1694. return( 0 );
  1695. }
  1696. static int chacha20_stream_wrap( void *ctx, size_t length,
  1697. const unsigned char *input,
  1698. unsigned char *output )
  1699. {
  1700. int ret;
  1701. ret = mbedtls_chacha20_update( ctx, length, input, output );
  1702. if( ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA )
  1703. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1704. return( ret );
  1705. }
  1706. static void * chacha20_ctx_alloc( void )
  1707. {
  1708. mbedtls_chacha20_context *ctx;
  1709. ctx = mbedtls_calloc( 1, sizeof( mbedtls_chacha20_context ) );
  1710. if( ctx == NULL )
  1711. return( NULL );
  1712. mbedtls_chacha20_init( ctx );
  1713. return( ctx );
  1714. }
  1715. static void chacha20_ctx_free( void *ctx )
  1716. {
  1717. mbedtls_chacha20_free( (mbedtls_chacha20_context *) ctx );
  1718. mbedtls_free( ctx );
  1719. }
  1720. static const mbedtls_cipher_base_t chacha20_base_info = {
  1721. MBEDTLS_CIPHER_ID_CHACHA20,
  1722. NULL,
  1723. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1724. NULL,
  1725. #endif
  1726. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1727. NULL,
  1728. #endif
  1729. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1730. NULL,
  1731. #endif
  1732. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1733. NULL,
  1734. #endif
  1735. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1736. NULL,
  1737. #endif
  1738. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1739. chacha20_stream_wrap,
  1740. #endif
  1741. chacha20_setkey_wrap,
  1742. chacha20_setkey_wrap,
  1743. chacha20_ctx_alloc,
  1744. chacha20_ctx_free
  1745. };
  1746. static const mbedtls_cipher_info_t chacha20_info = {
  1747. MBEDTLS_CIPHER_CHACHA20,
  1748. MBEDTLS_MODE_STREAM,
  1749. 256,
  1750. "CHACHA20",
  1751. 12,
  1752. 0,
  1753. 1,
  1754. &chacha20_base_info
  1755. };
  1756. #endif /* MBEDTLS_CHACHA20_C */
  1757. #if defined(MBEDTLS_CHACHAPOLY_C)
  1758. static int chachapoly_setkey_wrap( void *ctx,
  1759. const unsigned char *key,
  1760. unsigned int key_bitlen )
  1761. {
  1762. if( key_bitlen != 256U )
  1763. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1764. if ( 0 != mbedtls_chachapoly_setkey( (mbedtls_chachapoly_context*)ctx, key ) )
  1765. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1766. return( 0 );
  1767. }
  1768. static void * chachapoly_ctx_alloc( void )
  1769. {
  1770. mbedtls_chachapoly_context *ctx;
  1771. ctx = mbedtls_calloc( 1, sizeof( mbedtls_chachapoly_context ) );
  1772. if( ctx == NULL )
  1773. return( NULL );
  1774. mbedtls_chachapoly_init( ctx );
  1775. return( ctx );
  1776. }
  1777. static void chachapoly_ctx_free( void *ctx )
  1778. {
  1779. mbedtls_chachapoly_free( (mbedtls_chachapoly_context *) ctx );
  1780. mbedtls_free( ctx );
  1781. }
  1782. static const mbedtls_cipher_base_t chachapoly_base_info = {
  1783. MBEDTLS_CIPHER_ID_CHACHA20,
  1784. NULL,
  1785. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1786. NULL,
  1787. #endif
  1788. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1789. NULL,
  1790. #endif
  1791. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1792. NULL,
  1793. #endif
  1794. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1795. NULL,
  1796. #endif
  1797. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1798. NULL,
  1799. #endif
  1800. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1801. NULL,
  1802. #endif
  1803. chachapoly_setkey_wrap,
  1804. chachapoly_setkey_wrap,
  1805. chachapoly_ctx_alloc,
  1806. chachapoly_ctx_free
  1807. };
  1808. static const mbedtls_cipher_info_t chachapoly_info = {
  1809. MBEDTLS_CIPHER_CHACHA20_POLY1305,
  1810. MBEDTLS_MODE_CHACHAPOLY,
  1811. 256,
  1812. "CHACHA20-POLY1305",
  1813. 12,
  1814. 0,
  1815. 1,
  1816. &chachapoly_base_info
  1817. };
  1818. #endif /* MBEDTLS_CHACHAPOLY_C */
  1819. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  1820. static int null_crypt_stream( void *ctx, size_t length,
  1821. const unsigned char *input,
  1822. unsigned char *output )
  1823. {
  1824. ((void) ctx);
  1825. memmove( output, input, length );
  1826. return( 0 );
  1827. }
  1828. static int null_setkey( void *ctx, const unsigned char *key,
  1829. unsigned int key_bitlen )
  1830. {
  1831. ((void) ctx);
  1832. ((void) key);
  1833. ((void) key_bitlen);
  1834. return( 0 );
  1835. }
  1836. static void * null_ctx_alloc( void )
  1837. {
  1838. return( (void *) 1 );
  1839. }
  1840. static void null_ctx_free( void *ctx )
  1841. {
  1842. ((void) ctx);
  1843. }
  1844. static const mbedtls_cipher_base_t null_base_info = {
  1845. MBEDTLS_CIPHER_ID_NULL,
  1846. NULL,
  1847. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1848. NULL,
  1849. #endif
  1850. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1851. NULL,
  1852. #endif
  1853. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1854. NULL,
  1855. #endif
  1856. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1857. NULL,
  1858. #endif
  1859. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1860. NULL,
  1861. #endif
  1862. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  1863. null_crypt_stream,
  1864. #endif
  1865. null_setkey,
  1866. null_setkey,
  1867. null_ctx_alloc,
  1868. null_ctx_free
  1869. };
  1870. static const mbedtls_cipher_info_t null_cipher_info = {
  1871. MBEDTLS_CIPHER_NULL,
  1872. MBEDTLS_MODE_STREAM,
  1873. 0,
  1874. "NULL",
  1875. 0,
  1876. 0,
  1877. 1,
  1878. &null_base_info
  1879. };
  1880. #endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
  1881. const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
  1882. {
  1883. #if defined(MBEDTLS_AES_C)
  1884. { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
  1885. { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
  1886. { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
  1887. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1888. { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
  1889. { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
  1890. { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
  1891. #endif
  1892. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1893. { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
  1894. { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
  1895. { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
  1896. #endif
  1897. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  1898. { MBEDTLS_CIPHER_AES_128_OFB, &aes_128_ofb_info },
  1899. { MBEDTLS_CIPHER_AES_192_OFB, &aes_192_ofb_info },
  1900. { MBEDTLS_CIPHER_AES_256_OFB, &aes_256_ofb_info },
  1901. #endif
  1902. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1903. { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
  1904. { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
  1905. { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
  1906. #endif
  1907. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  1908. { MBEDTLS_CIPHER_AES_128_XTS, &aes_128_xts_info },
  1909. { MBEDTLS_CIPHER_AES_256_XTS, &aes_256_xts_info },
  1910. #endif
  1911. #if defined(MBEDTLS_GCM_C)
  1912. { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
  1913. { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
  1914. { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
  1915. #endif
  1916. #if defined(MBEDTLS_CCM_C)
  1917. { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
  1918. { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
  1919. { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
  1920. #endif
  1921. #endif /* MBEDTLS_AES_C */
  1922. #if defined(MBEDTLS_ARC4_C)
  1923. { MBEDTLS_CIPHER_ARC4_128, &arc4_128_info },
  1924. #endif
  1925. #if defined(MBEDTLS_BLOWFISH_C)
  1926. { MBEDTLS_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
  1927. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1928. { MBEDTLS_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
  1929. #endif
  1930. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1931. { MBEDTLS_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
  1932. #endif
  1933. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1934. { MBEDTLS_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
  1935. #endif
  1936. #endif /* MBEDTLS_BLOWFISH_C */
  1937. #if defined(MBEDTLS_CAMELLIA_C)
  1938. { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
  1939. { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
  1940. { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
  1941. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1942. { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
  1943. { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
  1944. { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
  1945. #endif
  1946. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1947. { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
  1948. { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
  1949. { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
  1950. #endif
  1951. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1952. { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
  1953. { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
  1954. { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
  1955. #endif
  1956. #if defined(MBEDTLS_GCM_C)
  1957. { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
  1958. { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
  1959. { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
  1960. #endif
  1961. #if defined(MBEDTLS_CCM_C)
  1962. { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
  1963. { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
  1964. { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
  1965. #endif
  1966. #endif /* MBEDTLS_CAMELLIA_C */
  1967. #if defined(MBEDTLS_ARIA_C)
  1968. { MBEDTLS_CIPHER_ARIA_128_ECB, &aria_128_ecb_info },
  1969. { MBEDTLS_CIPHER_ARIA_192_ECB, &aria_192_ecb_info },
  1970. { MBEDTLS_CIPHER_ARIA_256_ECB, &aria_256_ecb_info },
  1971. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1972. { MBEDTLS_CIPHER_ARIA_128_CBC, &aria_128_cbc_info },
  1973. { MBEDTLS_CIPHER_ARIA_192_CBC, &aria_192_cbc_info },
  1974. { MBEDTLS_CIPHER_ARIA_256_CBC, &aria_256_cbc_info },
  1975. #endif
  1976. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  1977. { MBEDTLS_CIPHER_ARIA_128_CFB128, &aria_128_cfb128_info },
  1978. { MBEDTLS_CIPHER_ARIA_192_CFB128, &aria_192_cfb128_info },
  1979. { MBEDTLS_CIPHER_ARIA_256_CFB128, &aria_256_cfb128_info },
  1980. #endif
  1981. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  1982. { MBEDTLS_CIPHER_ARIA_128_CTR, &aria_128_ctr_info },
  1983. { MBEDTLS_CIPHER_ARIA_192_CTR, &aria_192_ctr_info },
  1984. { MBEDTLS_CIPHER_ARIA_256_CTR, &aria_256_ctr_info },
  1985. #endif
  1986. #if defined(MBEDTLS_GCM_C)
  1987. { MBEDTLS_CIPHER_ARIA_128_GCM, &aria_128_gcm_info },
  1988. { MBEDTLS_CIPHER_ARIA_192_GCM, &aria_192_gcm_info },
  1989. { MBEDTLS_CIPHER_ARIA_256_GCM, &aria_256_gcm_info },
  1990. #endif
  1991. #if defined(MBEDTLS_CCM_C)
  1992. { MBEDTLS_CIPHER_ARIA_128_CCM, &aria_128_ccm_info },
  1993. { MBEDTLS_CIPHER_ARIA_192_CCM, &aria_192_ccm_info },
  1994. { MBEDTLS_CIPHER_ARIA_256_CCM, &aria_256_ccm_info },
  1995. #endif
  1996. #endif /* MBEDTLS_ARIA_C */
  1997. #if defined(MBEDTLS_DES_C)
  1998. { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
  1999. { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
  2000. { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
  2001. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  2002. { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
  2003. { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
  2004. { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
  2005. #endif
  2006. #endif /* MBEDTLS_DES_C */
  2007. #if defined(MBEDTLS_CHACHA20_C)
  2008. { MBEDTLS_CIPHER_CHACHA20, &chacha20_info },
  2009. #endif
  2010. #if defined(MBEDTLS_CHACHAPOLY_C)
  2011. { MBEDTLS_CIPHER_CHACHA20_POLY1305, &chachapoly_info },
  2012. #endif
  2013. #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
  2014. { MBEDTLS_CIPHER_NULL, &null_cipher_info },
  2015. #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
  2016. { MBEDTLS_CIPHER_NONE, NULL }
  2017. };
  2018. #define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
  2019. int mbedtls_cipher_supported[NUM_CIPHERS];
  2020. #endif /* MBEDTLS_CIPHER_C */