e_aria.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include "internal/deprecated.h"
  11. #include "internal/cryptlib.h"
  12. #ifndef OPENSSL_NO_ARIA
  13. # include <openssl/evp.h>
  14. # include <openssl/modes.h>
  15. # include <openssl/rand.h>
  16. # include "crypto/aria.h"
  17. # include "crypto/evp.h"
  18. # include "crypto/modes.h"
  19. # include "evp_local.h"
  20. /* ARIA subkey Structure */
  21. typedef struct {
  22. ARIA_KEY ks;
  23. } EVP_ARIA_KEY;
  24. /* ARIA GCM context */
  25. typedef struct {
  26. union {
  27. OSSL_UNION_ALIGN;
  28. ARIA_KEY ks;
  29. } ks; /* ARIA subkey to use */
  30. int key_set; /* Set if key initialised */
  31. int iv_set; /* Set if an iv is set */
  32. GCM128_CONTEXT gcm;
  33. unsigned char *iv; /* Temporary IV store */
  34. int ivlen; /* IV length */
  35. int taglen;
  36. int iv_gen; /* It is OK to generate IVs */
  37. int tls_aad_len; /* TLS AAD length */
  38. } EVP_ARIA_GCM_CTX;
  39. /* ARIA CCM context */
  40. typedef struct {
  41. union {
  42. OSSL_UNION_ALIGN;
  43. ARIA_KEY ks;
  44. } ks; /* ARIA key schedule to use */
  45. int key_set; /* Set if key initialised */
  46. int iv_set; /* Set if an iv is set */
  47. int tag_set; /* Set if tag is valid */
  48. int len_set; /* Set if message length set */
  49. int L, M; /* L and M parameters from RFC3610 */
  50. int tls_aad_len; /* TLS AAD length */
  51. CCM128_CONTEXT ccm;
  52. ccm128_f str;
  53. } EVP_ARIA_CCM_CTX;
  54. /* The subkey for ARIA is generated. */
  55. static int aria_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  56. const unsigned char *iv, int enc)
  57. {
  58. int ret;
  59. int mode = EVP_CIPHER_CTX_get_mode(ctx);
  60. if (enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE))
  61. ret = ossl_aria_set_encrypt_key(key,
  62. EVP_CIPHER_CTX_get_key_length(ctx) * 8,
  63. EVP_CIPHER_CTX_get_cipher_data(ctx));
  64. else
  65. ret = ossl_aria_set_decrypt_key(key,
  66. EVP_CIPHER_CTX_get_key_length(ctx) * 8,
  67. EVP_CIPHER_CTX_get_cipher_data(ctx));
  68. if (ret < 0) {
  69. ERR_raise(ERR_LIB_EVP, EVP_R_ARIA_KEY_SETUP_FAILED);
  70. return 0;
  71. }
  72. return 1;
  73. }
  74. static void aria_cbc_encrypt(const unsigned char *in, unsigned char *out,
  75. size_t len, const ARIA_KEY *key,
  76. unsigned char *ivec, const int enc)
  77. {
  78. if (enc)
  79. CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
  80. (block128_f) ossl_aria_encrypt);
  81. else
  82. CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
  83. (block128_f) ossl_aria_encrypt);
  84. }
  85. static void aria_cfb128_encrypt(const unsigned char *in, unsigned char *out,
  86. size_t length, const ARIA_KEY *key,
  87. unsigned char *ivec, int *num, const int enc)
  88. {
  89. CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
  90. (block128_f) ossl_aria_encrypt);
  91. }
  92. static void aria_cfb1_encrypt(const unsigned char *in, unsigned char *out,
  93. size_t length, const ARIA_KEY *key,
  94. unsigned char *ivec, int *num, const int enc)
  95. {
  96. CRYPTO_cfb128_1_encrypt(in, out, length, key, ivec, num, enc,
  97. (block128_f) ossl_aria_encrypt);
  98. }
  99. static void aria_cfb8_encrypt(const unsigned char *in, unsigned char *out,
  100. size_t length, const ARIA_KEY *key,
  101. unsigned char *ivec, int *num, const int enc)
  102. {
  103. CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, num, enc,
  104. (block128_f) ossl_aria_encrypt);
  105. }
  106. static void aria_ecb_encrypt(const unsigned char *in, unsigned char *out,
  107. const ARIA_KEY *key, const int enc)
  108. {
  109. ossl_aria_encrypt(in, out, key);
  110. }
  111. static void aria_ofb128_encrypt(const unsigned char *in, unsigned char *out,
  112. size_t length, const ARIA_KEY *key,
  113. unsigned char *ivec, int *num)
  114. {
  115. CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
  116. (block128_f) ossl_aria_encrypt);
  117. }
  118. IMPLEMENT_BLOCK_CIPHER(aria_128, ks, aria, EVP_ARIA_KEY,
  119. NID_aria_128, 16, 16, 16, 128,
  120. 0, aria_init_key, NULL,
  121. EVP_CIPHER_set_asn1_iv,
  122. EVP_CIPHER_get_asn1_iv,
  123. NULL)
  124. IMPLEMENT_BLOCK_CIPHER(aria_192, ks, aria, EVP_ARIA_KEY,
  125. NID_aria_192, 16, 24, 16, 128,
  126. 0, aria_init_key, NULL,
  127. EVP_CIPHER_set_asn1_iv,
  128. EVP_CIPHER_get_asn1_iv,
  129. NULL)
  130. IMPLEMENT_BLOCK_CIPHER(aria_256, ks, aria, EVP_ARIA_KEY,
  131. NID_aria_256, 16, 32, 16, 128,
  132. 0, aria_init_key, NULL,
  133. EVP_CIPHER_set_asn1_iv,
  134. EVP_CIPHER_get_asn1_iv,
  135. NULL)
  136. # define IMPLEMENT_ARIA_CFBR(ksize,cbits) \
  137. IMPLEMENT_CFBR(aria,aria,EVP_ARIA_KEY,ks,ksize,cbits,16,0)
  138. IMPLEMENT_ARIA_CFBR(128,1)
  139. IMPLEMENT_ARIA_CFBR(192,1)
  140. IMPLEMENT_ARIA_CFBR(256,1)
  141. IMPLEMENT_ARIA_CFBR(128,8)
  142. IMPLEMENT_ARIA_CFBR(192,8)
  143. IMPLEMENT_ARIA_CFBR(256,8)
  144. # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  145. static const EVP_CIPHER aria_##keylen##_##mode = { \
  146. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  147. flags|EVP_CIPH_##MODE##_MODE, \
  148. EVP_ORIG_GLOBAL, \
  149. aria_init_key, \
  150. aria_##mode##_cipher, \
  151. NULL, \
  152. sizeof(EVP_ARIA_KEY), \
  153. NULL,NULL,NULL,NULL }; \
  154. const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
  155. { return &aria_##keylen##_##mode; }
  156. static int aria_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  157. const unsigned char *in, size_t len)
  158. {
  159. int n = EVP_CIPHER_CTX_get_num(ctx);
  160. unsigned int num;
  161. EVP_ARIA_KEY *dat = EVP_C_DATA(EVP_ARIA_KEY, ctx);
  162. if (n < 0)
  163. return 0;
  164. num = (unsigned int)n;
  165. CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv,
  166. EVP_CIPHER_CTX_buf_noconst(ctx), &num,
  167. (block128_f) ossl_aria_encrypt);
  168. EVP_CIPHER_CTX_set_num(ctx, num);
  169. return 1;
  170. }
  171. BLOCK_CIPHER_generic(NID_aria, 128, 1, 16, ctr, ctr, CTR, 0)
  172. BLOCK_CIPHER_generic(NID_aria, 192, 1, 16, ctr, ctr, CTR, 0)
  173. BLOCK_CIPHER_generic(NID_aria, 256, 1, 16, ctr, ctr, CTR, 0)
  174. /* Authenticated cipher modes (GCM/CCM) */
  175. /* increment counter (64-bit int) by 1 */
  176. static void ctr64_inc(unsigned char *counter)
  177. {
  178. int n = 8;
  179. unsigned char c;
  180. do {
  181. --n;
  182. c = counter[n];
  183. ++c;
  184. counter[n] = c;
  185. if (c)
  186. return;
  187. } while (n);
  188. }
  189. static int aria_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  190. const unsigned char *iv, int enc)
  191. {
  192. int ret;
  193. EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx);
  194. if (!iv && !key)
  195. return 1;
  196. if (key) {
  197. ret = ossl_aria_set_encrypt_key(key,
  198. EVP_CIPHER_CTX_get_key_length(ctx) * 8,
  199. &gctx->ks.ks);
  200. CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
  201. (block128_f) ossl_aria_encrypt);
  202. if (ret < 0) {
  203. ERR_raise(ERR_LIB_EVP, EVP_R_ARIA_KEY_SETUP_FAILED);
  204. return 0;
  205. }
  206. /*
  207. * If we have an iv can set it directly, otherwise use saved IV.
  208. */
  209. if (iv == NULL && gctx->iv_set)
  210. iv = gctx->iv;
  211. if (iv) {
  212. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  213. gctx->iv_set = 1;
  214. }
  215. gctx->key_set = 1;
  216. } else {
  217. /* If key set use IV, otherwise copy */
  218. if (gctx->key_set)
  219. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  220. else
  221. memcpy(gctx->iv, iv, gctx->ivlen);
  222. gctx->iv_set = 1;
  223. gctx->iv_gen = 0;
  224. }
  225. return 1;
  226. }
  227. static int aria_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  228. {
  229. EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, c);
  230. switch (type) {
  231. case EVP_CTRL_INIT:
  232. gctx->key_set = 0;
  233. gctx->iv_set = 0;
  234. gctx->ivlen = EVP_CIPHER_get_iv_length(c->cipher);
  235. gctx->iv = c->iv;
  236. gctx->taglen = -1;
  237. gctx->iv_gen = 0;
  238. gctx->tls_aad_len = -1;
  239. return 1;
  240. case EVP_CTRL_GET_IVLEN:
  241. *(int *)ptr = gctx->ivlen;
  242. return 1;
  243. case EVP_CTRL_AEAD_SET_IVLEN:
  244. if (arg <= 0)
  245. return 0;
  246. /* Allocate memory for IV if needed */
  247. if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) {
  248. if (gctx->iv != c->iv)
  249. OPENSSL_free(gctx->iv);
  250. if ((gctx->iv = OPENSSL_malloc(arg)) == NULL) {
  251. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  252. return 0;
  253. }
  254. }
  255. gctx->ivlen = arg;
  256. return 1;
  257. case EVP_CTRL_AEAD_SET_TAG:
  258. if (arg <= 0 || arg > 16 || EVP_CIPHER_CTX_is_encrypting(c))
  259. return 0;
  260. memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
  261. gctx->taglen = arg;
  262. return 1;
  263. case EVP_CTRL_AEAD_GET_TAG:
  264. if (arg <= 0 || arg > 16 || !EVP_CIPHER_CTX_is_encrypting(c)
  265. || gctx->taglen < 0)
  266. return 0;
  267. memcpy(ptr, EVP_CIPHER_CTX_buf_noconst(c), arg);
  268. return 1;
  269. case EVP_CTRL_GCM_SET_IV_FIXED:
  270. /* Special case: -1 length restores whole IV */
  271. if (arg == -1) {
  272. memcpy(gctx->iv, ptr, gctx->ivlen);
  273. gctx->iv_gen = 1;
  274. return 1;
  275. }
  276. /*
  277. * Fixed field must be at least 4 bytes and invocation field at least
  278. * 8.
  279. */
  280. if ((arg < 4) || (gctx->ivlen - arg) < 8)
  281. return 0;
  282. if (arg)
  283. memcpy(gctx->iv, ptr, arg);
  284. if (EVP_CIPHER_CTX_is_encrypting(c)
  285. && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
  286. return 0;
  287. gctx->iv_gen = 1;
  288. return 1;
  289. case EVP_CTRL_GCM_IV_GEN:
  290. if (gctx->iv_gen == 0 || gctx->key_set == 0)
  291. return 0;
  292. CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
  293. if (arg <= 0 || arg > gctx->ivlen)
  294. arg = gctx->ivlen;
  295. memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
  296. /*
  297. * Invocation field will be at least 8 bytes in size and so no need
  298. * to check wrap around or increment more than last 8 bytes.
  299. */
  300. ctr64_inc(gctx->iv + gctx->ivlen - 8);
  301. gctx->iv_set = 1;
  302. return 1;
  303. case EVP_CTRL_GCM_SET_IV_INV:
  304. if (gctx->iv_gen == 0 || gctx->key_set == 0
  305. || EVP_CIPHER_CTX_is_encrypting(c))
  306. return 0;
  307. memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
  308. CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
  309. gctx->iv_set = 1;
  310. return 1;
  311. case EVP_CTRL_AEAD_TLS1_AAD:
  312. /* Save the AAD for later use */
  313. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  314. return 0;
  315. memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
  316. gctx->tls_aad_len = arg;
  317. {
  318. unsigned int len =
  319. EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
  320. | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
  321. /* Correct length for explicit IV */
  322. if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
  323. return 0;
  324. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  325. /* If decrypting correct for tag too */
  326. if (!EVP_CIPHER_CTX_is_encrypting(c)) {
  327. if (len < EVP_GCM_TLS_TAG_LEN)
  328. return 0;
  329. len -= EVP_GCM_TLS_TAG_LEN;
  330. }
  331. EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
  332. EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
  333. }
  334. /* Extra padding: tag appended to record */
  335. return EVP_GCM_TLS_TAG_LEN;
  336. case EVP_CTRL_COPY:
  337. {
  338. EVP_CIPHER_CTX *out = ptr;
  339. EVP_ARIA_GCM_CTX *gctx_out = EVP_C_DATA(EVP_ARIA_GCM_CTX, out);
  340. if (gctx->gcm.key) {
  341. if (gctx->gcm.key != &gctx->ks)
  342. return 0;
  343. gctx_out->gcm.key = &gctx_out->ks;
  344. }
  345. if (gctx->iv == c->iv)
  346. gctx_out->iv = out->iv;
  347. else {
  348. if ((gctx_out->iv = OPENSSL_malloc(gctx->ivlen)) == NULL) {
  349. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  350. return 0;
  351. }
  352. memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
  353. }
  354. return 1;
  355. }
  356. default:
  357. return -1;
  358. }
  359. }
  360. static int aria_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  361. const unsigned char *in, size_t len)
  362. {
  363. EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx);
  364. int rv = -1;
  365. /* Encrypt/decrypt must be performed in place */
  366. if (out != in
  367. || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
  368. return -1;
  369. /*
  370. * Set IV from start of buffer or generate IV and write to start of
  371. * buffer.
  372. */
  373. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CIPHER_CTX_is_encrypting(ctx) ?
  374. EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
  375. EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
  376. goto err;
  377. /* Use saved AAD */
  378. if (CRYPTO_gcm128_aad(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
  379. gctx->tls_aad_len))
  380. goto err;
  381. /* Fix buffer and length to point to payload */
  382. in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  383. out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  384. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  385. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  386. /* Encrypt payload */
  387. if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
  388. goto err;
  389. out += len;
  390. /* Finally write tag */
  391. CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
  392. rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  393. } else {
  394. /* Decrypt */
  395. if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
  396. goto err;
  397. /* Retrieve tag */
  398. CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
  399. EVP_GCM_TLS_TAG_LEN);
  400. /* If tag mismatch wipe buffer */
  401. if (CRYPTO_memcmp(EVP_CIPHER_CTX_buf_noconst(ctx), in + len,
  402. EVP_GCM_TLS_TAG_LEN)) {
  403. OPENSSL_cleanse(out, len);
  404. goto err;
  405. }
  406. rv = len;
  407. }
  408. err:
  409. gctx->iv_set = 0;
  410. gctx->tls_aad_len = -1;
  411. return rv;
  412. }
  413. static int aria_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  414. const unsigned char *in, size_t len)
  415. {
  416. EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx);
  417. /* If not set up, return error */
  418. if (!gctx->key_set)
  419. return -1;
  420. if (gctx->tls_aad_len >= 0)
  421. return aria_gcm_tls_cipher(ctx, out, in, len);
  422. if (!gctx->iv_set)
  423. return -1;
  424. if (in) {
  425. if (out == NULL) {
  426. if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
  427. return -1;
  428. } else if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  429. if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
  430. return -1;
  431. } else {
  432. if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
  433. return -1;
  434. }
  435. return len;
  436. }
  437. if (!EVP_CIPHER_CTX_is_encrypting(ctx)) {
  438. if (gctx->taglen < 0)
  439. return -1;
  440. if (CRYPTO_gcm128_finish(&gctx->gcm,
  441. EVP_CIPHER_CTX_buf_noconst(ctx),
  442. gctx->taglen) != 0)
  443. return -1;
  444. gctx->iv_set = 0;
  445. return 0;
  446. }
  447. CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), 16);
  448. gctx->taglen = 16;
  449. /* Don't reuse the IV */
  450. gctx->iv_set = 0;
  451. return 0;
  452. }
  453. static int aria_gcm_cleanup(EVP_CIPHER_CTX *ctx)
  454. {
  455. EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx);
  456. if (gctx->iv != ctx->iv)
  457. OPENSSL_free(gctx->iv);
  458. return 1;
  459. }
  460. static int aria_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  461. const unsigned char *iv, int enc)
  462. {
  463. int ret;
  464. EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, ctx);
  465. if (!iv && !key)
  466. return 1;
  467. if (key) {
  468. ret = ossl_aria_set_encrypt_key(key,
  469. EVP_CIPHER_CTX_get_key_length(ctx) * 8,
  470. &cctx->ks.ks);
  471. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  472. &cctx->ks, (block128_f) ossl_aria_encrypt);
  473. if (ret < 0) {
  474. ERR_raise(ERR_LIB_EVP, EVP_R_ARIA_KEY_SETUP_FAILED);
  475. return 0;
  476. }
  477. cctx->str = NULL;
  478. cctx->key_set = 1;
  479. }
  480. if (iv) {
  481. memcpy(ctx->iv, iv, 15 - cctx->L);
  482. cctx->iv_set = 1;
  483. }
  484. return 1;
  485. }
  486. static int aria_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  487. {
  488. EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, c);
  489. switch (type) {
  490. case EVP_CTRL_INIT:
  491. cctx->key_set = 0;
  492. cctx->iv_set = 0;
  493. cctx->L = 8;
  494. cctx->M = 12;
  495. cctx->tag_set = 0;
  496. cctx->len_set = 0;
  497. cctx->tls_aad_len = -1;
  498. return 1;
  499. case EVP_CTRL_GET_IVLEN:
  500. *(int *)ptr = 15 - cctx->L;
  501. return 1;
  502. case EVP_CTRL_AEAD_TLS1_AAD:
  503. /* Save the AAD for later use */
  504. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  505. return 0;
  506. memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
  507. cctx->tls_aad_len = arg;
  508. {
  509. uint16_t len =
  510. EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
  511. | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
  512. /* Correct length for explicit IV */
  513. if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
  514. return 0;
  515. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
  516. /* If decrypting correct for tag too */
  517. if (!EVP_CIPHER_CTX_is_encrypting(c)) {
  518. if (len < cctx->M)
  519. return 0;
  520. len -= cctx->M;
  521. }
  522. EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
  523. EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
  524. }
  525. /* Extra padding: tag appended to record */
  526. return cctx->M;
  527. case EVP_CTRL_CCM_SET_IV_FIXED:
  528. /* Sanity check length */
  529. if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
  530. return 0;
  531. /* Just copy to first part of IV */
  532. memcpy(c->iv, ptr, arg);
  533. return 1;
  534. case EVP_CTRL_AEAD_SET_IVLEN:
  535. arg = 15 - arg;
  536. /* fall thru */
  537. case EVP_CTRL_CCM_SET_L:
  538. if (arg < 2 || arg > 8)
  539. return 0;
  540. cctx->L = arg;
  541. return 1;
  542. case EVP_CTRL_AEAD_SET_TAG:
  543. if ((arg & 1) || arg < 4 || arg > 16)
  544. return 0;
  545. if (EVP_CIPHER_CTX_is_encrypting(c) && ptr)
  546. return 0;
  547. if (ptr) {
  548. cctx->tag_set = 1;
  549. memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
  550. }
  551. cctx->M = arg;
  552. return 1;
  553. case EVP_CTRL_AEAD_GET_TAG:
  554. if (!EVP_CIPHER_CTX_is_encrypting(c) || !cctx->tag_set)
  555. return 0;
  556. if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
  557. return 0;
  558. cctx->tag_set = 0;
  559. cctx->iv_set = 0;
  560. cctx->len_set = 0;
  561. return 1;
  562. case EVP_CTRL_COPY:
  563. {
  564. EVP_CIPHER_CTX *out = ptr;
  565. EVP_ARIA_CCM_CTX *cctx_out = EVP_C_DATA(EVP_ARIA_CCM_CTX, out);
  566. if (cctx->ccm.key) {
  567. if (cctx->ccm.key != &cctx->ks)
  568. return 0;
  569. cctx_out->ccm.key = &cctx_out->ks;
  570. }
  571. return 1;
  572. }
  573. default:
  574. return -1;
  575. }
  576. }
  577. static int aria_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  578. const unsigned char *in, size_t len)
  579. {
  580. EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, ctx);
  581. CCM128_CONTEXT *ccm = &cctx->ccm;
  582. /* Encrypt/decrypt must be performed in place */
  583. if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M))
  584. return -1;
  585. /* If encrypting set explicit IV from sequence number (start of AAD) */
  586. if (EVP_CIPHER_CTX_is_encrypting(ctx))
  587. memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx),
  588. EVP_CCM_TLS_EXPLICIT_IV_LEN);
  589. /* Get rest of IV from explicit IV */
  590. memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in,
  591. EVP_CCM_TLS_EXPLICIT_IV_LEN);
  592. /* Correct length value */
  593. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
  594. if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,
  595. len))
  596. return -1;
  597. /* Use saved AAD */
  598. CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx),
  599. cctx->tls_aad_len);
  600. /* Fix buffer to point to payload */
  601. in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  602. out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  603. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  604. if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str)
  605. : CRYPTO_ccm128_encrypt(ccm, in, out, len))
  606. return -1;
  607. if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M))
  608. return -1;
  609. return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
  610. } else {
  611. if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, cctx->str)
  612. : !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
  613. unsigned char tag[16];
  614. if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
  615. if (!CRYPTO_memcmp(tag, in + len, cctx->M))
  616. return len;
  617. }
  618. }
  619. OPENSSL_cleanse(out, len);
  620. return -1;
  621. }
  622. }
  623. static int aria_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  624. const unsigned char *in, size_t len)
  625. {
  626. EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, ctx);
  627. CCM128_CONTEXT *ccm = &cctx->ccm;
  628. /* If not set up, return error */
  629. if (!cctx->key_set)
  630. return -1;
  631. if (cctx->tls_aad_len >= 0)
  632. return aria_ccm_tls_cipher(ctx, out, in, len);
  633. /* EVP_*Final() doesn't return any data */
  634. if (in == NULL && out != NULL)
  635. return 0;
  636. if (!cctx->iv_set)
  637. return -1;
  638. if (!out) {
  639. if (!in) {
  640. if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
  641. return -1;
  642. cctx->len_set = 1;
  643. return len;
  644. }
  645. /* If have AAD need message length */
  646. if (!cctx->len_set && len)
  647. return -1;
  648. CRYPTO_ccm128_aad(ccm, in, len);
  649. return len;
  650. }
  651. /* The tag must be set before actually decrypting data */
  652. if (!EVP_CIPHER_CTX_is_encrypting(ctx) && !cctx->tag_set)
  653. return -1;
  654. /* If not set length yet do it */
  655. if (!cctx->len_set) {
  656. if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
  657. return -1;
  658. cctx->len_set = 1;
  659. }
  660. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  661. if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str)
  662. : CRYPTO_ccm128_encrypt(ccm, in, out, len))
  663. return -1;
  664. cctx->tag_set = 1;
  665. return len;
  666. } else {
  667. int rv = -1;
  668. if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
  669. cctx->str) :
  670. !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
  671. unsigned char tag[16];
  672. if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
  673. if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx),
  674. cctx->M))
  675. rv = len;
  676. }
  677. }
  678. if (rv == -1)
  679. OPENSSL_cleanse(out, len);
  680. cctx->iv_set = 0;
  681. cctx->tag_set = 0;
  682. cctx->len_set = 0;
  683. return rv;
  684. }
  685. }
  686. #define aria_ccm_cleanup NULL
  687. #define ARIA_AUTH_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
  688. | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
  689. | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
  690. | EVP_CIPH_CUSTOM_COPY | EVP_CIPH_FLAG_AEAD_CIPHER \
  691. | EVP_CIPH_CUSTOM_IV_LENGTH)
  692. #define BLOCK_CIPHER_aead(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  693. static const EVP_CIPHER aria_##keylen##_##mode = { \
  694. nid##_##keylen##_##nmode, \
  695. blocksize, keylen/8, ivlen, \
  696. ARIA_AUTH_FLAGS|EVP_CIPH_##MODE##_MODE, \
  697. EVP_ORIG_GLOBAL, \
  698. aria_##mode##_init_key, \
  699. aria_##mode##_cipher, \
  700. aria_##mode##_cleanup, \
  701. sizeof(EVP_ARIA_##MODE##_CTX), \
  702. NULL,NULL,aria_##mode##_ctrl,NULL }; \
  703. const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
  704. { return (EVP_CIPHER*)&aria_##keylen##_##mode; }
  705. BLOCK_CIPHER_aead(NID_aria, 128, 1, 12, gcm, gcm, GCM, 0)
  706. BLOCK_CIPHER_aead(NID_aria, 192, 1, 12, gcm, gcm, GCM, 0)
  707. BLOCK_CIPHER_aead(NID_aria, 256, 1, 12, gcm, gcm, GCM, 0)
  708. BLOCK_CIPHER_aead(NID_aria, 128, 1, 12, ccm, ccm, CCM, 0)
  709. BLOCK_CIPHER_aead(NID_aria, 192, 1, 12, ccm, ccm, CCM, 0)
  710. BLOCK_CIPHER_aead(NID_aria, 256, 1, 12, ccm, ccm, CCM, 0)
  711. #endif