e_aria.c 26 KB

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