e_chacha20_poly1305.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #ifndef OPENSSL_NO_CHACHA
  12. # include <openssl/evp.h>
  13. # include <openssl/objects.h>
  14. # include "evp_local.h"
  15. # include "crypto/evp.h"
  16. # include "crypto/chacha.h"
  17. typedef struct {
  18. union {
  19. double align; /* this ensures even sizeof(EVP_CHACHA_KEY)%8==0 */
  20. unsigned int d[CHACHA_KEY_SIZE / 4];
  21. } key;
  22. unsigned int counter[CHACHA_CTR_SIZE / 4];
  23. unsigned char buf[CHACHA_BLK_SIZE];
  24. unsigned int partial_len;
  25. } EVP_CHACHA_KEY;
  26. #define data(ctx) ((EVP_CHACHA_KEY *)(ctx)->cipher_data)
  27. #define CHACHA20_POLY1305_MAX_IVLEN 12
  28. static int chacha_init_key(EVP_CIPHER_CTX *ctx,
  29. const unsigned char user_key[CHACHA_KEY_SIZE],
  30. const unsigned char iv[CHACHA_CTR_SIZE], int enc)
  31. {
  32. EVP_CHACHA_KEY *key = data(ctx);
  33. unsigned int i;
  34. if (user_key)
  35. for (i = 0; i < CHACHA_KEY_SIZE; i+=4) {
  36. key->key.d[i/4] = CHACHA_U8TOU32(user_key+i);
  37. }
  38. if (iv)
  39. for (i = 0; i < CHACHA_CTR_SIZE; i+=4) {
  40. key->counter[i/4] = CHACHA_U8TOU32(iv+i);
  41. }
  42. key->partial_len = 0;
  43. return 1;
  44. }
  45. static int chacha_cipher(EVP_CIPHER_CTX * ctx, unsigned char *out,
  46. const unsigned char *inp, size_t len)
  47. {
  48. EVP_CHACHA_KEY *key = data(ctx);
  49. unsigned int n, rem, ctr32;
  50. if ((n = key->partial_len)) {
  51. while (len && n < CHACHA_BLK_SIZE) {
  52. *out++ = *inp++ ^ key->buf[n++];
  53. len--;
  54. }
  55. key->partial_len = n;
  56. if (len == 0)
  57. return 1;
  58. if (n == CHACHA_BLK_SIZE) {
  59. key->partial_len = 0;
  60. key->counter[0]++;
  61. if (key->counter[0] == 0)
  62. key->counter[1]++;
  63. }
  64. }
  65. rem = (unsigned int)(len % CHACHA_BLK_SIZE);
  66. len -= rem;
  67. ctr32 = key->counter[0];
  68. while (len >= CHACHA_BLK_SIZE) {
  69. size_t blocks = len / CHACHA_BLK_SIZE;
  70. /*
  71. * 1<<28 is just a not-so-small yet not-so-large number...
  72. * Below condition is practically never met, but it has to
  73. * be checked for code correctness.
  74. */
  75. if (sizeof(size_t)>sizeof(unsigned int) && blocks>(1U<<28))
  76. blocks = (1U<<28);
  77. /*
  78. * As ChaCha20_ctr32 operates on 32-bit counter, caller
  79. * has to handle overflow. 'if' below detects the
  80. * overflow, which is then handled by limiting the
  81. * amount of blocks to the exact overflow point...
  82. */
  83. ctr32 += (unsigned int)blocks;
  84. if (ctr32 < blocks) {
  85. blocks -= ctr32;
  86. ctr32 = 0;
  87. }
  88. blocks *= CHACHA_BLK_SIZE;
  89. ChaCha20_ctr32(out, inp, blocks, key->key.d, key->counter);
  90. len -= blocks;
  91. inp += blocks;
  92. out += blocks;
  93. key->counter[0] = ctr32;
  94. if (ctr32 == 0) key->counter[1]++;
  95. }
  96. if (rem) {
  97. memset(key->buf, 0, sizeof(key->buf));
  98. ChaCha20_ctr32(key->buf, key->buf, CHACHA_BLK_SIZE,
  99. key->key.d, key->counter);
  100. for (n = 0; n < rem; n++)
  101. out[n] = inp[n] ^ key->buf[n];
  102. key->partial_len = rem;
  103. }
  104. return 1;
  105. }
  106. static const EVP_CIPHER chacha20 = {
  107. NID_chacha20,
  108. 1, /* block_size */
  109. CHACHA_KEY_SIZE, /* key_len */
  110. CHACHA_CTR_SIZE, /* iv_len, 128-bit counter in the context */
  111. EVP_CIPH_CUSTOM_IV | EVP_CIPH_ALWAYS_CALL_INIT,
  112. chacha_init_key,
  113. chacha_cipher,
  114. NULL,
  115. sizeof(EVP_CHACHA_KEY),
  116. NULL,
  117. NULL,
  118. NULL,
  119. NULL
  120. };
  121. const EVP_CIPHER *EVP_chacha20(void)
  122. {
  123. return &chacha20;
  124. }
  125. # ifndef OPENSSL_NO_POLY1305
  126. # include "crypto/poly1305.h"
  127. typedef struct {
  128. EVP_CHACHA_KEY key;
  129. unsigned int nonce[12/4];
  130. unsigned char tag[POLY1305_BLOCK_SIZE];
  131. unsigned char tls_aad[POLY1305_BLOCK_SIZE];
  132. struct { uint64_t aad, text; } len;
  133. int aad, mac_inited, tag_len, nonce_len;
  134. size_t tls_payload_length;
  135. } EVP_CHACHA_AEAD_CTX;
  136. # define NO_TLS_PAYLOAD_LENGTH ((size_t)-1)
  137. # define aead_data(ctx) ((EVP_CHACHA_AEAD_CTX *)(ctx)->cipher_data)
  138. # define POLY1305_ctx(actx) ((POLY1305 *)(actx + 1))
  139. static int chacha20_poly1305_init_key(EVP_CIPHER_CTX *ctx,
  140. const unsigned char *inkey,
  141. const unsigned char *iv, int enc)
  142. {
  143. EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
  144. if (!inkey && !iv)
  145. return 1;
  146. actx->len.aad = 0;
  147. actx->len.text = 0;
  148. actx->aad = 0;
  149. actx->mac_inited = 0;
  150. actx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
  151. if (iv != NULL) {
  152. unsigned char temp[CHACHA_CTR_SIZE] = { 0 };
  153. /* pad on the left */
  154. if (actx->nonce_len <= CHACHA_CTR_SIZE)
  155. memcpy(temp + CHACHA_CTR_SIZE - actx->nonce_len, iv,
  156. actx->nonce_len);
  157. chacha_init_key(ctx, inkey, temp, enc);
  158. actx->nonce[0] = actx->key.counter[1];
  159. actx->nonce[1] = actx->key.counter[2];
  160. actx->nonce[2] = actx->key.counter[3];
  161. } else {
  162. chacha_init_key(ctx, inkey, NULL, enc);
  163. }
  164. return 1;
  165. }
  166. # if !defined(OPENSSL_SMALL_FOOTPRINT)
  167. # if defined(POLY1305_ASM) && (defined(__x86_64) || defined(__x86_64__) || \
  168. defined(_M_AMD64) || defined(_M_X64))
  169. # define XOR128_HELPERS
  170. void *xor128_encrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
  171. void *xor128_decrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
  172. static const unsigned char zero[4 * CHACHA_BLK_SIZE] = { 0 };
  173. # else
  174. static const unsigned char zero[2 * CHACHA_BLK_SIZE] = { 0 };
  175. # endif
  176. static int chacha20_poly1305_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  177. const unsigned char *in, size_t len)
  178. {
  179. EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
  180. size_t tail, tohash_len, buf_len, plen = actx->tls_payload_length;
  181. unsigned char *buf, *tohash, *ctr, storage[sizeof(zero) + 32];
  182. if (len != plen + POLY1305_BLOCK_SIZE)
  183. return -1;
  184. buf = storage + ((0 - (size_t)storage) & 15); /* align */
  185. ctr = buf + CHACHA_BLK_SIZE;
  186. tohash = buf + CHACHA_BLK_SIZE - POLY1305_BLOCK_SIZE;
  187. # ifdef XOR128_HELPERS
  188. if (plen <= 3 * CHACHA_BLK_SIZE) {
  189. actx->key.counter[0] = 0;
  190. buf_len = (plen + 2 * CHACHA_BLK_SIZE - 1) & (0 - CHACHA_BLK_SIZE);
  191. ChaCha20_ctr32(buf, zero, buf_len, actx->key.key.d,
  192. actx->key.counter);
  193. Poly1305_Init(POLY1305_ctx(actx), buf);
  194. actx->key.partial_len = 0;
  195. memcpy(tohash, actx->tls_aad, POLY1305_BLOCK_SIZE);
  196. tohash_len = POLY1305_BLOCK_SIZE;
  197. actx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
  198. actx->len.text = plen;
  199. if (plen) {
  200. if (ctx->encrypt)
  201. ctr = xor128_encrypt_n_pad(out, in, ctr, plen);
  202. else
  203. ctr = xor128_decrypt_n_pad(out, in, ctr, plen);
  204. in += plen;
  205. out += plen;
  206. tohash_len = (size_t)(ctr - tohash);
  207. }
  208. }
  209. # else
  210. if (plen <= CHACHA_BLK_SIZE) {
  211. size_t i;
  212. actx->key.counter[0] = 0;
  213. ChaCha20_ctr32(buf, zero, (buf_len = 2 * CHACHA_BLK_SIZE),
  214. actx->key.key.d, actx->key.counter);
  215. Poly1305_Init(POLY1305_ctx(actx), buf);
  216. actx->key.partial_len = 0;
  217. memcpy(tohash, actx->tls_aad, POLY1305_BLOCK_SIZE);
  218. tohash_len = POLY1305_BLOCK_SIZE;
  219. actx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
  220. actx->len.text = plen;
  221. if (ctx->encrypt) {
  222. for (i = 0; i < plen; i++) {
  223. out[i] = ctr[i] ^= in[i];
  224. }
  225. } else {
  226. for (i = 0; i < plen; i++) {
  227. unsigned char c = in[i];
  228. out[i] = ctr[i] ^ c;
  229. ctr[i] = c;
  230. }
  231. }
  232. in += i;
  233. out += i;
  234. tail = (0 - i) & (POLY1305_BLOCK_SIZE - 1);
  235. memset(ctr + i, 0, tail);
  236. ctr += i + tail;
  237. tohash_len += i + tail;
  238. }
  239. # endif
  240. else {
  241. actx->key.counter[0] = 0;
  242. ChaCha20_ctr32(buf, zero, (buf_len = CHACHA_BLK_SIZE),
  243. actx->key.key.d, actx->key.counter);
  244. Poly1305_Init(POLY1305_ctx(actx), buf);
  245. actx->key.counter[0] = 1;
  246. actx->key.partial_len = 0;
  247. Poly1305_Update(POLY1305_ctx(actx), actx->tls_aad, POLY1305_BLOCK_SIZE);
  248. tohash = ctr;
  249. tohash_len = 0;
  250. actx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
  251. actx->len.text = plen;
  252. if (ctx->encrypt) {
  253. ChaCha20_ctr32(out, in, plen, actx->key.key.d, actx->key.counter);
  254. Poly1305_Update(POLY1305_ctx(actx), out, plen);
  255. } else {
  256. Poly1305_Update(POLY1305_ctx(actx), in, plen);
  257. ChaCha20_ctr32(out, in, plen, actx->key.key.d, actx->key.counter);
  258. }
  259. in += plen;
  260. out += plen;
  261. tail = (0 - plen) & (POLY1305_BLOCK_SIZE - 1);
  262. Poly1305_Update(POLY1305_ctx(actx), zero, tail);
  263. }
  264. {
  265. const union {
  266. long one;
  267. char little;
  268. } is_endian = { 1 };
  269. if (is_endian.little) {
  270. memcpy(ctr, (unsigned char *)&actx->len, POLY1305_BLOCK_SIZE);
  271. } else {
  272. ctr[0] = (unsigned char)(actx->len.aad);
  273. ctr[1] = (unsigned char)(actx->len.aad>>8);
  274. ctr[2] = (unsigned char)(actx->len.aad>>16);
  275. ctr[3] = (unsigned char)(actx->len.aad>>24);
  276. ctr[4] = (unsigned char)(actx->len.aad>>32);
  277. ctr[5] = (unsigned char)(actx->len.aad>>40);
  278. ctr[6] = (unsigned char)(actx->len.aad>>48);
  279. ctr[7] = (unsigned char)(actx->len.aad>>56);
  280. ctr[8] = (unsigned char)(actx->len.text);
  281. ctr[9] = (unsigned char)(actx->len.text>>8);
  282. ctr[10] = (unsigned char)(actx->len.text>>16);
  283. ctr[11] = (unsigned char)(actx->len.text>>24);
  284. ctr[12] = (unsigned char)(actx->len.text>>32);
  285. ctr[13] = (unsigned char)(actx->len.text>>40);
  286. ctr[14] = (unsigned char)(actx->len.text>>48);
  287. ctr[15] = (unsigned char)(actx->len.text>>56);
  288. }
  289. tohash_len += POLY1305_BLOCK_SIZE;
  290. }
  291. Poly1305_Update(POLY1305_ctx(actx), tohash, tohash_len);
  292. OPENSSL_cleanse(buf, buf_len);
  293. Poly1305_Final(POLY1305_ctx(actx), ctx->encrypt ? actx->tag
  294. : tohash);
  295. actx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
  296. if (ctx->encrypt) {
  297. memcpy(out, actx->tag, POLY1305_BLOCK_SIZE);
  298. } else {
  299. if (CRYPTO_memcmp(tohash, in, POLY1305_BLOCK_SIZE)) {
  300. memset(out - (len - POLY1305_BLOCK_SIZE), 0,
  301. len - POLY1305_BLOCK_SIZE);
  302. return -1;
  303. }
  304. }
  305. return len;
  306. }
  307. # else
  308. static const unsigned char zero[CHACHA_BLK_SIZE] = { 0 };
  309. # endif
  310. static int chacha20_poly1305_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  311. const unsigned char *in, size_t len)
  312. {
  313. EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
  314. size_t rem, plen = actx->tls_payload_length;
  315. if (!actx->mac_inited) {
  316. # if !defined(OPENSSL_SMALL_FOOTPRINT)
  317. if (plen != NO_TLS_PAYLOAD_LENGTH && out != NULL)
  318. return chacha20_poly1305_tls_cipher(ctx, out, in, len);
  319. # endif
  320. actx->key.counter[0] = 0;
  321. ChaCha20_ctr32(actx->key.buf, zero, CHACHA_BLK_SIZE,
  322. actx->key.key.d, actx->key.counter);
  323. Poly1305_Init(POLY1305_ctx(actx), actx->key.buf);
  324. actx->key.counter[0] = 1;
  325. actx->key.partial_len = 0;
  326. actx->len.aad = actx->len.text = 0;
  327. actx->mac_inited = 1;
  328. if (plen != NO_TLS_PAYLOAD_LENGTH) {
  329. Poly1305_Update(POLY1305_ctx(actx), actx->tls_aad,
  330. EVP_AEAD_TLS1_AAD_LEN);
  331. actx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
  332. actx->aad = 1;
  333. }
  334. }
  335. if (in) { /* aad or text */
  336. if (out == NULL) { /* aad */
  337. Poly1305_Update(POLY1305_ctx(actx), in, len);
  338. actx->len.aad += len;
  339. actx->aad = 1;
  340. return len;
  341. } else { /* plain- or ciphertext */
  342. if (actx->aad) { /* wrap up aad */
  343. if ((rem = (size_t)actx->len.aad % POLY1305_BLOCK_SIZE))
  344. Poly1305_Update(POLY1305_ctx(actx), zero,
  345. POLY1305_BLOCK_SIZE - rem);
  346. actx->aad = 0;
  347. }
  348. actx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
  349. if (plen == NO_TLS_PAYLOAD_LENGTH)
  350. plen = len;
  351. else if (len != plen + POLY1305_BLOCK_SIZE)
  352. return -1;
  353. if (ctx->encrypt) { /* plaintext */
  354. chacha_cipher(ctx, out, in, plen);
  355. Poly1305_Update(POLY1305_ctx(actx), out, plen);
  356. in += plen;
  357. out += plen;
  358. actx->len.text += plen;
  359. } else { /* ciphertext */
  360. Poly1305_Update(POLY1305_ctx(actx), in, plen);
  361. chacha_cipher(ctx, out, in, plen);
  362. in += plen;
  363. out += plen;
  364. actx->len.text += plen;
  365. }
  366. }
  367. }
  368. if (in == NULL /* explicit final */
  369. || plen != len) { /* or tls mode */
  370. const union {
  371. long one;
  372. char little;
  373. } is_endian = { 1 };
  374. unsigned char temp[POLY1305_BLOCK_SIZE];
  375. if (actx->aad) { /* wrap up aad */
  376. if ((rem = (size_t)actx->len.aad % POLY1305_BLOCK_SIZE))
  377. Poly1305_Update(POLY1305_ctx(actx), zero,
  378. POLY1305_BLOCK_SIZE - rem);
  379. actx->aad = 0;
  380. }
  381. if ((rem = (size_t)actx->len.text % POLY1305_BLOCK_SIZE))
  382. Poly1305_Update(POLY1305_ctx(actx), zero,
  383. POLY1305_BLOCK_SIZE - rem);
  384. if (is_endian.little) {
  385. Poly1305_Update(POLY1305_ctx(actx),
  386. (unsigned char *)&actx->len, POLY1305_BLOCK_SIZE);
  387. } else {
  388. temp[0] = (unsigned char)(actx->len.aad);
  389. temp[1] = (unsigned char)(actx->len.aad>>8);
  390. temp[2] = (unsigned char)(actx->len.aad>>16);
  391. temp[3] = (unsigned char)(actx->len.aad>>24);
  392. temp[4] = (unsigned char)(actx->len.aad>>32);
  393. temp[5] = (unsigned char)(actx->len.aad>>40);
  394. temp[6] = (unsigned char)(actx->len.aad>>48);
  395. temp[7] = (unsigned char)(actx->len.aad>>56);
  396. temp[8] = (unsigned char)(actx->len.text);
  397. temp[9] = (unsigned char)(actx->len.text>>8);
  398. temp[10] = (unsigned char)(actx->len.text>>16);
  399. temp[11] = (unsigned char)(actx->len.text>>24);
  400. temp[12] = (unsigned char)(actx->len.text>>32);
  401. temp[13] = (unsigned char)(actx->len.text>>40);
  402. temp[14] = (unsigned char)(actx->len.text>>48);
  403. temp[15] = (unsigned char)(actx->len.text>>56);
  404. Poly1305_Update(POLY1305_ctx(actx), temp, POLY1305_BLOCK_SIZE);
  405. }
  406. Poly1305_Final(POLY1305_ctx(actx), ctx->encrypt ? actx->tag
  407. : temp);
  408. actx->mac_inited = 0;
  409. if (in != NULL && len != plen) { /* tls mode */
  410. if (ctx->encrypt) {
  411. memcpy(out, actx->tag, POLY1305_BLOCK_SIZE);
  412. } else {
  413. if (CRYPTO_memcmp(temp, in, POLY1305_BLOCK_SIZE)) {
  414. memset(out - plen, 0, plen);
  415. return -1;
  416. }
  417. }
  418. }
  419. else if (!ctx->encrypt) {
  420. if (CRYPTO_memcmp(temp, actx->tag, actx->tag_len))
  421. return -1;
  422. }
  423. }
  424. return len;
  425. }
  426. static int chacha20_poly1305_cleanup(EVP_CIPHER_CTX *ctx)
  427. {
  428. EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
  429. if (actx)
  430. OPENSSL_cleanse(ctx->cipher_data, sizeof(*actx) + Poly1305_ctx_size());
  431. return 1;
  432. }
  433. static int chacha20_poly1305_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  434. void *ptr)
  435. {
  436. EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
  437. switch(type) {
  438. case EVP_CTRL_INIT:
  439. if (actx == NULL)
  440. actx = ctx->cipher_data
  441. = OPENSSL_zalloc(sizeof(*actx) + Poly1305_ctx_size());
  442. if (actx == NULL) {
  443. EVPerr(EVP_F_CHACHA20_POLY1305_CTRL, EVP_R_INITIALIZATION_ERROR);
  444. return 0;
  445. }
  446. actx->len.aad = 0;
  447. actx->len.text = 0;
  448. actx->aad = 0;
  449. actx->mac_inited = 0;
  450. actx->tag_len = 0;
  451. actx->nonce_len = 12;
  452. actx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
  453. memset(actx->tls_aad, 0, POLY1305_BLOCK_SIZE);
  454. return 1;
  455. case EVP_CTRL_COPY:
  456. if (actx) {
  457. EVP_CIPHER_CTX *dst = (EVP_CIPHER_CTX *)ptr;
  458. dst->cipher_data =
  459. OPENSSL_memdup(actx, sizeof(*actx) + Poly1305_ctx_size());
  460. if (dst->cipher_data == NULL) {
  461. EVPerr(EVP_F_CHACHA20_POLY1305_CTRL, EVP_R_COPY_ERROR);
  462. return 0;
  463. }
  464. }
  465. return 1;
  466. case EVP_CTRL_GET_IVLEN:
  467. *(int *)ptr = actx->nonce_len;
  468. return 1;
  469. case EVP_CTRL_AEAD_SET_IVLEN:
  470. if (arg <= 0 || arg > CHACHA20_POLY1305_MAX_IVLEN)
  471. return 0;
  472. actx->nonce_len = arg;
  473. return 1;
  474. case EVP_CTRL_AEAD_SET_IV_FIXED:
  475. if (arg != 12)
  476. return 0;
  477. actx->nonce[0] = actx->key.counter[1]
  478. = CHACHA_U8TOU32((unsigned char *)ptr);
  479. actx->nonce[1] = actx->key.counter[2]
  480. = CHACHA_U8TOU32((unsigned char *)ptr+4);
  481. actx->nonce[2] = actx->key.counter[3]
  482. = CHACHA_U8TOU32((unsigned char *)ptr+8);
  483. return 1;
  484. case EVP_CTRL_AEAD_SET_TAG:
  485. if (arg <= 0 || arg > POLY1305_BLOCK_SIZE)
  486. return 0;
  487. if (ptr != NULL) {
  488. memcpy(actx->tag, ptr, arg);
  489. actx->tag_len = arg;
  490. }
  491. return 1;
  492. case EVP_CTRL_AEAD_GET_TAG:
  493. if (arg <= 0 || arg > POLY1305_BLOCK_SIZE || !ctx->encrypt)
  494. return 0;
  495. memcpy(ptr, actx->tag, arg);
  496. return 1;
  497. case EVP_CTRL_AEAD_TLS1_AAD:
  498. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  499. return 0;
  500. {
  501. unsigned int len;
  502. unsigned char *aad = ptr;
  503. memcpy(actx->tls_aad, ptr, EVP_AEAD_TLS1_AAD_LEN);
  504. len = aad[EVP_AEAD_TLS1_AAD_LEN - 2] << 8 |
  505. aad[EVP_AEAD_TLS1_AAD_LEN - 1];
  506. aad = actx->tls_aad;
  507. if (!ctx->encrypt) {
  508. if (len < POLY1305_BLOCK_SIZE)
  509. return 0;
  510. len -= POLY1305_BLOCK_SIZE; /* discount attached tag */
  511. aad[EVP_AEAD_TLS1_AAD_LEN - 2] = (unsigned char)(len >> 8);
  512. aad[EVP_AEAD_TLS1_AAD_LEN - 1] = (unsigned char)len;
  513. }
  514. actx->tls_payload_length = len;
  515. /*
  516. * merge record sequence number as per RFC7905
  517. */
  518. actx->key.counter[1] = actx->nonce[0];
  519. actx->key.counter[2] = actx->nonce[1] ^ CHACHA_U8TOU32(aad);
  520. actx->key.counter[3] = actx->nonce[2] ^ CHACHA_U8TOU32(aad+4);
  521. actx->mac_inited = 0;
  522. return POLY1305_BLOCK_SIZE; /* tag length */
  523. }
  524. case EVP_CTRL_AEAD_SET_MAC_KEY:
  525. /* no-op */
  526. return 1;
  527. default:
  528. return -1;
  529. }
  530. }
  531. static EVP_CIPHER chacha20_poly1305 = {
  532. NID_chacha20_poly1305,
  533. 1, /* block_size */
  534. CHACHA_KEY_SIZE, /* key_len */
  535. 12, /* iv_len, 96-bit nonce in the context */
  536. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_CUSTOM_IV |
  537. EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT |
  538. EVP_CIPH_CUSTOM_COPY | EVP_CIPH_FLAG_CUSTOM_CIPHER |
  539. EVP_CIPH_CUSTOM_IV_LENGTH,
  540. chacha20_poly1305_init_key,
  541. chacha20_poly1305_cipher,
  542. chacha20_poly1305_cleanup,
  543. 0, /* 0 moves context-specific structure allocation to ctrl */
  544. NULL, /* set_asn1_parameters */
  545. NULL, /* get_asn1_parameters */
  546. chacha20_poly1305_ctrl,
  547. NULL /* app_data */
  548. };
  549. const EVP_CIPHER *EVP_chacha20_poly1305(void)
  550. {
  551. return(&chacha20_poly1305);
  552. }
  553. # endif
  554. #endif