cipher_chacha20_poly1305_hw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright 2019-2023 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. /* chacha20_poly1305 cipher implementation */
  10. #include "internal/endian.h"
  11. #include "cipher_chacha20_poly1305.h"
  12. static int chacha_poly1305_tls_init(PROV_CIPHER_CTX *bctx,
  13. unsigned char *aad, size_t alen)
  14. {
  15. unsigned int len;
  16. PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
  17. if (alen != EVP_AEAD_TLS1_AAD_LEN)
  18. return 0;
  19. memcpy(ctx->tls_aad, aad, EVP_AEAD_TLS1_AAD_LEN);
  20. len = aad[EVP_AEAD_TLS1_AAD_LEN - 2] << 8 | aad[EVP_AEAD_TLS1_AAD_LEN - 1];
  21. aad = ctx->tls_aad;
  22. if (!bctx->enc) {
  23. if (len < POLY1305_BLOCK_SIZE)
  24. return 0;
  25. len -= POLY1305_BLOCK_SIZE; /* discount attached tag */
  26. aad[EVP_AEAD_TLS1_AAD_LEN - 2] = (unsigned char)(len >> 8);
  27. aad[EVP_AEAD_TLS1_AAD_LEN - 1] = (unsigned char)len;
  28. }
  29. ctx->tls_payload_length = len;
  30. /* merge record sequence number as per RFC7905 */
  31. ctx->chacha.counter[1] = ctx->nonce[0];
  32. ctx->chacha.counter[2] = ctx->nonce[1] ^ CHACHA_U8TOU32(aad);
  33. ctx->chacha.counter[3] = ctx->nonce[2] ^ CHACHA_U8TOU32(aad+4);
  34. ctx->mac_inited = 0;
  35. return POLY1305_BLOCK_SIZE; /* tag length */
  36. }
  37. static int chacha_poly1305_tls_iv_set_fixed(PROV_CIPHER_CTX *bctx,
  38. unsigned char *fixed, size_t flen)
  39. {
  40. PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
  41. if (flen != CHACHA20_POLY1305_IVLEN)
  42. return 0;
  43. ctx->nonce[0] = ctx->chacha.counter[1] = CHACHA_U8TOU32(fixed);
  44. ctx->nonce[1] = ctx->chacha.counter[2] = CHACHA_U8TOU32(fixed + 4);
  45. ctx->nonce[2] = ctx->chacha.counter[3] = CHACHA_U8TOU32(fixed + 8);
  46. return 1;
  47. }
  48. static int chacha20_poly1305_initkey(PROV_CIPHER_CTX *bctx,
  49. const unsigned char *key, size_t keylen)
  50. {
  51. PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
  52. ctx->len.aad = 0;
  53. ctx->len.text = 0;
  54. ctx->aad = 0;
  55. ctx->mac_inited = 0;
  56. ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
  57. if (bctx->enc)
  58. return ossl_chacha20_einit(&ctx->chacha, key, keylen, NULL, 0, NULL);
  59. else
  60. return ossl_chacha20_dinit(&ctx->chacha, key, keylen, NULL, 0, NULL);
  61. }
  62. static int chacha20_poly1305_initiv(PROV_CIPHER_CTX *bctx)
  63. {
  64. PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
  65. unsigned char tempiv[CHACHA_CTR_SIZE] = { 0 };
  66. int ret = 1;
  67. size_t noncelen = CHACHA20_POLY1305_IVLEN;
  68. ctx->len.aad = 0;
  69. ctx->len.text = 0;
  70. ctx->aad = 0;
  71. ctx->mac_inited = 0;
  72. ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
  73. /* pad on the left */
  74. memcpy(tempiv + CHACHA_CTR_SIZE - noncelen, bctx->oiv,
  75. noncelen);
  76. if (bctx->enc)
  77. ret = ossl_chacha20_einit(&ctx->chacha, NULL, 0,
  78. tempiv, sizeof(tempiv), NULL);
  79. else
  80. ret = ossl_chacha20_dinit(&ctx->chacha, NULL, 0,
  81. tempiv, sizeof(tempiv), NULL);
  82. ctx->nonce[0] = ctx->chacha.counter[1];
  83. ctx->nonce[1] = ctx->chacha.counter[2];
  84. ctx->nonce[2] = ctx->chacha.counter[3];
  85. bctx->iv_set = 1;
  86. return ret;
  87. }
  88. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  89. # if defined(POLY1305_ASM) && (defined(__x86_64) || defined(__x86_64__) \
  90. || defined(_M_AMD64) || defined(_M_X64))
  91. # define XOR128_HELPERS
  92. void *xor128_encrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
  93. void *xor128_decrypt_n_pad(void *out, const void *inp, void *otp, size_t len);
  94. static const unsigned char zero[4 * CHACHA_BLK_SIZE] = { 0 };
  95. # else
  96. static const unsigned char zero[2 * CHACHA_BLK_SIZE] = { 0 };
  97. # endif
  98. static int chacha20_poly1305_tls_cipher(PROV_CIPHER_CTX *bctx,
  99. unsigned char *out,
  100. size_t *out_padlen,
  101. const unsigned char *in, size_t len)
  102. {
  103. PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
  104. POLY1305 *poly = &ctx->poly1305;
  105. size_t tail, tohash_len, buf_len, plen = ctx->tls_payload_length;
  106. unsigned char *buf, *tohash, *ctr, storage[sizeof(zero) + 32];
  107. DECLARE_IS_ENDIAN;
  108. buf = storage + ((0 - (size_t)storage) & 15); /* align */
  109. ctr = buf + CHACHA_BLK_SIZE;
  110. tohash = buf + CHACHA_BLK_SIZE - POLY1305_BLOCK_SIZE;
  111. # ifdef XOR128_HELPERS
  112. if (plen <= 3 * CHACHA_BLK_SIZE) {
  113. ctx->chacha.counter[0] = 0;
  114. buf_len = (plen + 2 * CHACHA_BLK_SIZE - 1) & (0 - CHACHA_BLK_SIZE);
  115. ChaCha20_ctr32(buf, zero, buf_len, ctx->chacha.key.d, ctx->chacha.counter);
  116. Poly1305_Init(poly, buf);
  117. ctx->chacha.partial_len = 0;
  118. memcpy(tohash, ctx->tls_aad, POLY1305_BLOCK_SIZE);
  119. tohash_len = POLY1305_BLOCK_SIZE;
  120. ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
  121. ctx->len.text = plen;
  122. if (plen) {
  123. if (bctx->enc)
  124. ctr = xor128_encrypt_n_pad(out, in, ctr, plen);
  125. else
  126. ctr = xor128_decrypt_n_pad(out, in, ctr, plen);
  127. in += plen;
  128. out += plen;
  129. tohash_len = (size_t)(ctr - tohash);
  130. }
  131. }
  132. # else
  133. if (plen <= CHACHA_BLK_SIZE) {
  134. size_t i;
  135. ctx->chacha.counter[0] = 0;
  136. ChaCha20_ctr32(buf, zero, (buf_len = 2 * CHACHA_BLK_SIZE),
  137. ctx->chacha.key.d, ctx->chacha.counter);
  138. Poly1305_Init(poly, buf);
  139. ctx->chacha.partial_len = 0;
  140. memcpy(tohash, ctx->tls_aad, POLY1305_BLOCK_SIZE);
  141. tohash_len = POLY1305_BLOCK_SIZE;
  142. ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
  143. ctx->len.text = plen;
  144. if (bctx->enc) {
  145. for (i = 0; i < plen; i++)
  146. out[i] = ctr[i] ^= in[i];
  147. } else {
  148. for (i = 0; i < plen; i++) {
  149. unsigned char c = in[i];
  150. out[i] = ctr[i] ^ c;
  151. ctr[i] = c;
  152. }
  153. }
  154. in += i;
  155. out += i;
  156. tail = (0 - i) & (POLY1305_BLOCK_SIZE - 1);
  157. memset(ctr + i, 0, tail);
  158. ctr += i + tail;
  159. tohash_len += i + tail;
  160. }
  161. # endif
  162. else {
  163. ctx->chacha.counter[0] = 0;
  164. ChaCha20_ctr32(buf, zero, (buf_len = CHACHA_BLK_SIZE),
  165. ctx->chacha.key.d, ctx->chacha.counter);
  166. Poly1305_Init(poly, buf);
  167. ctx->chacha.counter[0] = 1;
  168. ctx->chacha.partial_len = 0;
  169. Poly1305_Update(poly, ctx->tls_aad, POLY1305_BLOCK_SIZE);
  170. tohash = ctr;
  171. tohash_len = 0;
  172. ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
  173. ctx->len.text = plen;
  174. if (bctx->enc) {
  175. ChaCha20_ctr32(out, in, plen, ctx->chacha.key.d, ctx->chacha.counter);
  176. Poly1305_Update(poly, out, plen);
  177. } else {
  178. Poly1305_Update(poly, in, plen);
  179. ChaCha20_ctr32(out, in, plen, ctx->chacha.key.d, ctx->chacha.counter);
  180. }
  181. in += plen;
  182. out += plen;
  183. tail = (0 - plen) & (POLY1305_BLOCK_SIZE - 1);
  184. Poly1305_Update(poly, zero, tail);
  185. }
  186. if (IS_LITTLE_ENDIAN) {
  187. memcpy(ctr, (unsigned char *)&ctx->len, POLY1305_BLOCK_SIZE);
  188. } else {
  189. ctr[0] = (unsigned char)(ctx->len.aad);
  190. ctr[1] = (unsigned char)(ctx->len.aad>>8);
  191. ctr[2] = (unsigned char)(ctx->len.aad>>16);
  192. ctr[3] = (unsigned char)(ctx->len.aad>>24);
  193. ctr[4] = (unsigned char)(ctx->len.aad>>32);
  194. ctr[5] = (unsigned char)(ctx->len.aad>>40);
  195. ctr[6] = (unsigned char)(ctx->len.aad>>48);
  196. ctr[7] = (unsigned char)(ctx->len.aad>>56);
  197. ctr[8] = (unsigned char)(ctx->len.text);
  198. ctr[9] = (unsigned char)(ctx->len.text>>8);
  199. ctr[10] = (unsigned char)(ctx->len.text>>16);
  200. ctr[11] = (unsigned char)(ctx->len.text>>24);
  201. ctr[12] = (unsigned char)(ctx->len.text>>32);
  202. ctr[13] = (unsigned char)(ctx->len.text>>40);
  203. ctr[14] = (unsigned char)(ctx->len.text>>48);
  204. ctr[15] = (unsigned char)(ctx->len.text>>56);
  205. }
  206. tohash_len += POLY1305_BLOCK_SIZE;
  207. Poly1305_Update(poly, tohash, tohash_len);
  208. OPENSSL_cleanse(buf, buf_len);
  209. Poly1305_Final(poly, bctx->enc ? ctx->tag : tohash);
  210. ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
  211. if (bctx->enc) {
  212. memcpy(out, ctx->tag, POLY1305_BLOCK_SIZE);
  213. } else {
  214. if (CRYPTO_memcmp(tohash, in, POLY1305_BLOCK_SIZE)) {
  215. if (len > POLY1305_BLOCK_SIZE)
  216. memset(out - (len - POLY1305_BLOCK_SIZE), 0,
  217. len - POLY1305_BLOCK_SIZE);
  218. return 0;
  219. }
  220. /* Strip the tag */
  221. len -= POLY1305_BLOCK_SIZE;
  222. }
  223. *out_padlen = len;
  224. return 1;
  225. }
  226. #else
  227. static const unsigned char zero[CHACHA_BLK_SIZE] = { 0 };
  228. #endif /* OPENSSL_SMALL_FOOTPRINT */
  229. static int chacha20_poly1305_aead_cipher(PROV_CIPHER_CTX *bctx,
  230. unsigned char *out, size_t *outl,
  231. const unsigned char *in, size_t inl)
  232. {
  233. PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)bctx;
  234. POLY1305 *poly = &ctx->poly1305;
  235. size_t rem, plen = ctx->tls_payload_length;
  236. size_t olen = 0;
  237. int rv = 0;
  238. DECLARE_IS_ENDIAN;
  239. if (!ctx->mac_inited) {
  240. if (plen != NO_TLS_PAYLOAD_LENGTH && out != NULL) {
  241. if (inl != plen + POLY1305_BLOCK_SIZE)
  242. return 0;
  243. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  244. return chacha20_poly1305_tls_cipher(bctx, out, outl, in, inl);
  245. #endif
  246. }
  247. ctx->chacha.counter[0] = 0;
  248. ChaCha20_ctr32(ctx->chacha.buf, zero, CHACHA_BLK_SIZE,
  249. ctx->chacha.key.d, ctx->chacha.counter);
  250. Poly1305_Init(poly, ctx->chacha.buf);
  251. ctx->chacha.counter[0] = 1;
  252. ctx->chacha.partial_len = 0;
  253. ctx->len.aad = ctx->len.text = 0;
  254. ctx->mac_inited = 1;
  255. if (plen != NO_TLS_PAYLOAD_LENGTH) {
  256. Poly1305_Update(poly, ctx->tls_aad, EVP_AEAD_TLS1_AAD_LEN);
  257. ctx->len.aad = EVP_AEAD_TLS1_AAD_LEN;
  258. ctx->aad = 1;
  259. }
  260. }
  261. if (in != NULL) { /* aad or text */
  262. if (out == NULL) { /* aad */
  263. Poly1305_Update(poly, in, inl);
  264. ctx->len.aad += inl;
  265. ctx->aad = 1;
  266. goto finish;
  267. } else { /* plain- or ciphertext */
  268. if (ctx->aad) { /* wrap up aad */
  269. if ((rem = (size_t)ctx->len.aad % POLY1305_BLOCK_SIZE))
  270. Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
  271. ctx->aad = 0;
  272. }
  273. ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
  274. if (plen == NO_TLS_PAYLOAD_LENGTH)
  275. plen = inl;
  276. else if (inl != plen + POLY1305_BLOCK_SIZE)
  277. goto err;
  278. if (bctx->enc) { /* plaintext */
  279. ctx->chacha.base.hw->cipher(&ctx->chacha.base, out, in, plen);
  280. Poly1305_Update(poly, out, plen);
  281. in += plen;
  282. out += plen;
  283. ctx->len.text += plen;
  284. } else { /* ciphertext */
  285. Poly1305_Update(poly, in, plen);
  286. ctx->chacha.base.hw->cipher(&ctx->chacha.base, out, in, plen);
  287. in += plen;
  288. out += plen;
  289. ctx->len.text += plen;
  290. }
  291. }
  292. }
  293. /* explicit final, or tls mode */
  294. if (in == NULL || inl != plen) {
  295. unsigned char temp[POLY1305_BLOCK_SIZE];
  296. if (ctx->aad) { /* wrap up aad */
  297. if ((rem = (size_t)ctx->len.aad % POLY1305_BLOCK_SIZE))
  298. Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
  299. ctx->aad = 0;
  300. }
  301. if ((rem = (size_t)ctx->len.text % POLY1305_BLOCK_SIZE))
  302. Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
  303. if (IS_LITTLE_ENDIAN) {
  304. Poly1305_Update(poly, (unsigned char *)&ctx->len,
  305. POLY1305_BLOCK_SIZE);
  306. } else {
  307. temp[0] = (unsigned char)(ctx->len.aad);
  308. temp[1] = (unsigned char)(ctx->len.aad>>8);
  309. temp[2] = (unsigned char)(ctx->len.aad>>16);
  310. temp[3] = (unsigned char)(ctx->len.aad>>24);
  311. temp[4] = (unsigned char)(ctx->len.aad>>32);
  312. temp[5] = (unsigned char)(ctx->len.aad>>40);
  313. temp[6] = (unsigned char)(ctx->len.aad>>48);
  314. temp[7] = (unsigned char)(ctx->len.aad>>56);
  315. temp[8] = (unsigned char)(ctx->len.text);
  316. temp[9] = (unsigned char)(ctx->len.text>>8);
  317. temp[10] = (unsigned char)(ctx->len.text>>16);
  318. temp[11] = (unsigned char)(ctx->len.text>>24);
  319. temp[12] = (unsigned char)(ctx->len.text>>32);
  320. temp[13] = (unsigned char)(ctx->len.text>>40);
  321. temp[14] = (unsigned char)(ctx->len.text>>48);
  322. temp[15] = (unsigned char)(ctx->len.text>>56);
  323. Poly1305_Update(poly, temp, POLY1305_BLOCK_SIZE);
  324. }
  325. Poly1305_Final(poly, bctx->enc ? ctx->tag : temp);
  326. ctx->mac_inited = 0;
  327. if (in != NULL && inl != plen) {
  328. if (bctx->enc) {
  329. memcpy(out, ctx->tag, POLY1305_BLOCK_SIZE);
  330. } else {
  331. if (CRYPTO_memcmp(temp, in, POLY1305_BLOCK_SIZE)) {
  332. memset(out - plen, 0, plen);
  333. goto err;
  334. }
  335. /* Strip the tag */
  336. inl -= POLY1305_BLOCK_SIZE;
  337. }
  338. }
  339. else if (!bctx->enc) {
  340. if (CRYPTO_memcmp(temp, ctx->tag, ctx->tag_len))
  341. goto err;
  342. }
  343. }
  344. finish:
  345. olen = inl;
  346. rv = 1;
  347. err:
  348. *outl = olen;
  349. return rv;
  350. }
  351. static const PROV_CIPHER_HW_CHACHA20_POLY1305 chacha20poly1305_hw =
  352. {
  353. { chacha20_poly1305_initkey, NULL },
  354. chacha20_poly1305_aead_cipher,
  355. chacha20_poly1305_initiv,
  356. chacha_poly1305_tls_init,
  357. chacha_poly1305_tls_iv_set_fixed
  358. };
  359. const PROV_CIPHER_HW *ossl_prov_cipher_hw_chacha20_poly1305(size_t keybits)
  360. {
  361. return (PROV_CIPHER_HW *)&chacha20poly1305_hw;
  362. }