cipher_wrap.c 54 KB

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