e_aria.c 25 KB

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