cipher_wrap.c 54 KB

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