e_chacha20_poly1305.c 20 KB

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