e_chacha20_poly1305.c 21 KB

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