e_aria.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. return 0;
  252. }
  253. gctx->ivlen = arg;
  254. return 1;
  255. case EVP_CTRL_AEAD_SET_TAG:
  256. if (arg <= 0 || arg > 16 || EVP_CIPHER_CTX_is_encrypting(c))
  257. return 0;
  258. memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
  259. gctx->taglen = arg;
  260. return 1;
  261. case EVP_CTRL_AEAD_GET_TAG:
  262. if (arg <= 0 || arg > 16 || !EVP_CIPHER_CTX_is_encrypting(c)
  263. || gctx->taglen < 0)
  264. return 0;
  265. memcpy(ptr, EVP_CIPHER_CTX_buf_noconst(c), arg);
  266. return 1;
  267. case EVP_CTRL_GCM_SET_IV_FIXED:
  268. /* Special case: -1 length restores whole IV */
  269. if (arg == -1) {
  270. memcpy(gctx->iv, ptr, gctx->ivlen);
  271. gctx->iv_gen = 1;
  272. return 1;
  273. }
  274. /*
  275. * Fixed field must be at least 4 bytes and invocation field at least
  276. * 8.
  277. */
  278. if ((arg < 4) || (gctx->ivlen - arg) < 8)
  279. return 0;
  280. if (arg)
  281. memcpy(gctx->iv, ptr, arg);
  282. if (EVP_CIPHER_CTX_is_encrypting(c)
  283. && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
  284. return 0;
  285. gctx->iv_gen = 1;
  286. return 1;
  287. case EVP_CTRL_GCM_IV_GEN:
  288. if (gctx->iv_gen == 0 || gctx->key_set == 0)
  289. return 0;
  290. CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
  291. if (arg <= 0 || arg > gctx->ivlen)
  292. arg = gctx->ivlen;
  293. memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
  294. /*
  295. * Invocation field will be at least 8 bytes in size and so no need
  296. * to check wrap around or increment more than last 8 bytes.
  297. */
  298. ctr64_inc(gctx->iv + gctx->ivlen - 8);
  299. gctx->iv_set = 1;
  300. return 1;
  301. case EVP_CTRL_GCM_SET_IV_INV:
  302. if (gctx->iv_gen == 0 || gctx->key_set == 0
  303. || EVP_CIPHER_CTX_is_encrypting(c))
  304. return 0;
  305. memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
  306. CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
  307. gctx->iv_set = 1;
  308. return 1;
  309. case EVP_CTRL_AEAD_TLS1_AAD:
  310. /* Save the AAD for later use */
  311. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  312. return 0;
  313. memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
  314. gctx->tls_aad_len = arg;
  315. {
  316. unsigned int len =
  317. EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
  318. | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
  319. /* Correct length for explicit IV */
  320. if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
  321. return 0;
  322. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  323. /* If decrypting correct for tag too */
  324. if (!EVP_CIPHER_CTX_is_encrypting(c)) {
  325. if (len < EVP_GCM_TLS_TAG_LEN)
  326. return 0;
  327. len -= EVP_GCM_TLS_TAG_LEN;
  328. }
  329. EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
  330. EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
  331. }
  332. /* Extra padding: tag appended to record */
  333. return EVP_GCM_TLS_TAG_LEN;
  334. case EVP_CTRL_COPY:
  335. {
  336. EVP_CIPHER_CTX *out = ptr;
  337. EVP_ARIA_GCM_CTX *gctx_out = EVP_C_DATA(EVP_ARIA_GCM_CTX, out);
  338. if (gctx->gcm.key) {
  339. if (gctx->gcm.key != &gctx->ks)
  340. return 0;
  341. gctx_out->gcm.key = &gctx_out->ks;
  342. }
  343. if (gctx->iv == c->iv)
  344. gctx_out->iv = out->iv;
  345. else {
  346. if ((gctx_out->iv = OPENSSL_malloc(gctx->ivlen)) == NULL)
  347. return 0;
  348. memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
  349. }
  350. return 1;
  351. }
  352. default:
  353. return -1;
  354. }
  355. }
  356. static int aria_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  357. const unsigned char *in, size_t len)
  358. {
  359. EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx);
  360. int rv = -1;
  361. /* Encrypt/decrypt must be performed in place */
  362. if (out != in
  363. || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
  364. return -1;
  365. /*
  366. * Set IV from start of buffer or generate IV and write to start of
  367. * buffer.
  368. */
  369. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CIPHER_CTX_is_encrypting(ctx) ?
  370. EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
  371. EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
  372. goto err;
  373. /* Use saved AAD */
  374. if (CRYPTO_gcm128_aad(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
  375. gctx->tls_aad_len))
  376. goto err;
  377. /* Fix buffer and length to point to payload */
  378. in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  379. out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  380. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  381. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  382. /* Encrypt payload */
  383. if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
  384. goto err;
  385. out += len;
  386. /* Finally write tag */
  387. CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
  388. rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  389. } else {
  390. /* Decrypt */
  391. if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
  392. goto err;
  393. /* Retrieve tag */
  394. CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
  395. EVP_GCM_TLS_TAG_LEN);
  396. /* If tag mismatch wipe buffer */
  397. if (CRYPTO_memcmp(EVP_CIPHER_CTX_buf_noconst(ctx), in + len,
  398. EVP_GCM_TLS_TAG_LEN)) {
  399. OPENSSL_cleanse(out, len);
  400. goto err;
  401. }
  402. rv = len;
  403. }
  404. err:
  405. gctx->iv_set = 0;
  406. gctx->tls_aad_len = -1;
  407. return rv;
  408. }
  409. static int aria_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  410. const unsigned char *in, size_t len)
  411. {
  412. EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx);
  413. /* If not set up, return error */
  414. if (!gctx->key_set)
  415. return -1;
  416. if (gctx->tls_aad_len >= 0)
  417. return aria_gcm_tls_cipher(ctx, out, in, len);
  418. if (!gctx->iv_set)
  419. return -1;
  420. if (in) {
  421. if (out == NULL) {
  422. if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
  423. return -1;
  424. } else if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  425. if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
  426. return -1;
  427. } else {
  428. if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
  429. return -1;
  430. }
  431. return len;
  432. }
  433. if (!EVP_CIPHER_CTX_is_encrypting(ctx)) {
  434. if (gctx->taglen < 0)
  435. return -1;
  436. if (CRYPTO_gcm128_finish(&gctx->gcm,
  437. EVP_CIPHER_CTX_buf_noconst(ctx),
  438. gctx->taglen) != 0)
  439. return -1;
  440. gctx->iv_set = 0;
  441. return 0;
  442. }
  443. CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), 16);
  444. gctx->taglen = 16;
  445. /* Don't reuse the IV */
  446. gctx->iv_set = 0;
  447. return 0;
  448. }
  449. static int aria_gcm_cleanup(EVP_CIPHER_CTX *ctx)
  450. {
  451. EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX, ctx);
  452. if (gctx->iv != ctx->iv)
  453. OPENSSL_free(gctx->iv);
  454. return 1;
  455. }
  456. static int aria_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  457. const unsigned char *iv, int enc)
  458. {
  459. int ret;
  460. EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, ctx);
  461. if (!iv && !key)
  462. return 1;
  463. if (key) {
  464. ret = ossl_aria_set_encrypt_key(key,
  465. EVP_CIPHER_CTX_get_key_length(ctx) * 8,
  466. &cctx->ks.ks);
  467. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  468. &cctx->ks, (block128_f) ossl_aria_encrypt);
  469. if (ret < 0) {
  470. ERR_raise(ERR_LIB_EVP, EVP_R_ARIA_KEY_SETUP_FAILED);
  471. return 0;
  472. }
  473. cctx->str = NULL;
  474. cctx->key_set = 1;
  475. }
  476. if (iv) {
  477. memcpy(ctx->iv, iv, 15 - cctx->L);
  478. cctx->iv_set = 1;
  479. }
  480. return 1;
  481. }
  482. static int aria_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  483. {
  484. EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, c);
  485. switch (type) {
  486. case EVP_CTRL_INIT:
  487. cctx->key_set = 0;
  488. cctx->iv_set = 0;
  489. cctx->L = 8;
  490. cctx->M = 12;
  491. cctx->tag_set = 0;
  492. cctx->len_set = 0;
  493. cctx->tls_aad_len = -1;
  494. return 1;
  495. case EVP_CTRL_GET_IVLEN:
  496. *(int *)ptr = 15 - cctx->L;
  497. return 1;
  498. case EVP_CTRL_AEAD_TLS1_AAD:
  499. /* Save the AAD for later use */
  500. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  501. return 0;
  502. memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
  503. cctx->tls_aad_len = arg;
  504. {
  505. uint16_t len =
  506. EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
  507. | EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
  508. /* Correct length for explicit IV */
  509. if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
  510. return 0;
  511. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
  512. /* If decrypting correct for tag too */
  513. if (!EVP_CIPHER_CTX_is_encrypting(c)) {
  514. if (len < cctx->M)
  515. return 0;
  516. len -= cctx->M;
  517. }
  518. EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
  519. EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
  520. }
  521. /* Extra padding: tag appended to record */
  522. return cctx->M;
  523. case EVP_CTRL_CCM_SET_IV_FIXED:
  524. /* Sanity check length */
  525. if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
  526. return 0;
  527. /* Just copy to first part of IV */
  528. memcpy(c->iv, ptr, arg);
  529. return 1;
  530. case EVP_CTRL_AEAD_SET_IVLEN:
  531. arg = 15 - arg;
  532. /* fall through */
  533. case EVP_CTRL_CCM_SET_L:
  534. if (arg < 2 || arg > 8)
  535. return 0;
  536. cctx->L = arg;
  537. return 1;
  538. case EVP_CTRL_AEAD_SET_TAG:
  539. if ((arg & 1) || arg < 4 || arg > 16)
  540. return 0;
  541. if (EVP_CIPHER_CTX_is_encrypting(c) && ptr)
  542. return 0;
  543. if (ptr) {
  544. cctx->tag_set = 1;
  545. memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
  546. }
  547. cctx->M = arg;
  548. return 1;
  549. case EVP_CTRL_AEAD_GET_TAG:
  550. if (!EVP_CIPHER_CTX_is_encrypting(c) || !cctx->tag_set)
  551. return 0;
  552. if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
  553. return 0;
  554. cctx->tag_set = 0;
  555. cctx->iv_set = 0;
  556. cctx->len_set = 0;
  557. return 1;
  558. case EVP_CTRL_COPY:
  559. {
  560. EVP_CIPHER_CTX *out = ptr;
  561. EVP_ARIA_CCM_CTX *cctx_out = EVP_C_DATA(EVP_ARIA_CCM_CTX, out);
  562. if (cctx->ccm.key) {
  563. if (cctx->ccm.key != &cctx->ks)
  564. return 0;
  565. cctx_out->ccm.key = &cctx_out->ks;
  566. }
  567. return 1;
  568. }
  569. default:
  570. return -1;
  571. }
  572. }
  573. static int aria_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  574. const unsigned char *in, size_t len)
  575. {
  576. EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, ctx);
  577. CCM128_CONTEXT *ccm = &cctx->ccm;
  578. /* Encrypt/decrypt must be performed in place */
  579. if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M))
  580. return -1;
  581. /* If encrypting set explicit IV from sequence number (start of AAD) */
  582. if (EVP_CIPHER_CTX_is_encrypting(ctx))
  583. memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx),
  584. EVP_CCM_TLS_EXPLICIT_IV_LEN);
  585. /* Get rest of IV from explicit IV */
  586. memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in,
  587. EVP_CCM_TLS_EXPLICIT_IV_LEN);
  588. /* Correct length value */
  589. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
  590. if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,
  591. len))
  592. return -1;
  593. /* Use saved AAD */
  594. CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx),
  595. cctx->tls_aad_len);
  596. /* Fix buffer to point to payload */
  597. in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  598. out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  599. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  600. if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str)
  601. : CRYPTO_ccm128_encrypt(ccm, in, out, len))
  602. return -1;
  603. if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M))
  604. return -1;
  605. return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
  606. } else {
  607. if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, cctx->str)
  608. : !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
  609. unsigned char tag[16];
  610. if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
  611. if (!CRYPTO_memcmp(tag, in + len, cctx->M))
  612. return len;
  613. }
  614. }
  615. OPENSSL_cleanse(out, len);
  616. return -1;
  617. }
  618. }
  619. static int aria_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  620. const unsigned char *in, size_t len)
  621. {
  622. EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX, ctx);
  623. CCM128_CONTEXT *ccm = &cctx->ccm;
  624. /* If not set up, return error */
  625. if (!cctx->key_set)
  626. return -1;
  627. if (cctx->tls_aad_len >= 0)
  628. return aria_ccm_tls_cipher(ctx, out, in, len);
  629. /* EVP_*Final() doesn't return any data */
  630. if (in == NULL && out != NULL)
  631. return 0;
  632. if (!cctx->iv_set)
  633. return -1;
  634. if (!out) {
  635. if (!in) {
  636. if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
  637. return -1;
  638. cctx->len_set = 1;
  639. return len;
  640. }
  641. /* If have AAD need message length */
  642. if (!cctx->len_set && len)
  643. return -1;
  644. CRYPTO_ccm128_aad(ccm, in, len);
  645. return len;
  646. }
  647. /* The tag must be set before actually decrypting data */
  648. if (!EVP_CIPHER_CTX_is_encrypting(ctx) && !cctx->tag_set)
  649. return -1;
  650. /* If not set length yet do it */
  651. if (!cctx->len_set) {
  652. if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
  653. return -1;
  654. cctx->len_set = 1;
  655. }
  656. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  657. if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str)
  658. : CRYPTO_ccm128_encrypt(ccm, in, out, len))
  659. return -1;
  660. cctx->tag_set = 1;
  661. return len;
  662. } else {
  663. int rv = -1;
  664. if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
  665. cctx->str) :
  666. !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
  667. unsigned char tag[16];
  668. if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
  669. if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx),
  670. cctx->M))
  671. rv = len;
  672. }
  673. }
  674. if (rv == -1)
  675. OPENSSL_cleanse(out, len);
  676. cctx->iv_set = 0;
  677. cctx->tag_set = 0;
  678. cctx->len_set = 0;
  679. return rv;
  680. }
  681. }
  682. #define aria_ccm_cleanup NULL
  683. #define ARIA_AUTH_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
  684. | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
  685. | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
  686. | EVP_CIPH_CUSTOM_COPY | EVP_CIPH_FLAG_AEAD_CIPHER \
  687. | EVP_CIPH_CUSTOM_IV_LENGTH)
  688. #define BLOCK_CIPHER_aead(keylen,mode,MODE) \
  689. static const EVP_CIPHER aria_##keylen##_##mode = { \
  690. NID_aria_##keylen##_##mode, \
  691. 1, keylen/8, 12, \
  692. ARIA_AUTH_FLAGS|EVP_CIPH_##MODE##_MODE, \
  693. EVP_ORIG_GLOBAL, \
  694. aria_##mode##_init_key, \
  695. aria_##mode##_cipher, \
  696. aria_##mode##_cleanup, \
  697. sizeof(EVP_ARIA_##MODE##_CTX), \
  698. NULL,NULL,aria_##mode##_ctrl,NULL }; \
  699. const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
  700. { return (EVP_CIPHER*)&aria_##keylen##_##mode; }
  701. BLOCK_CIPHER_aead(128, gcm, GCM)
  702. BLOCK_CIPHER_aead(192, gcm, GCM)
  703. BLOCK_CIPHER_aead(256, gcm, GCM)
  704. BLOCK_CIPHER_aead(128, ccm, CCM)
  705. BLOCK_CIPHER_aead(192, ccm, CCM)
  706. BLOCK_CIPHER_aead(256, ccm, CCM)
  707. #endif