chacha20_poly1305.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* chacha.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*
  22. DESCRIPTION
  23. This library contains implementation for the ChaCha20 stream cipher and
  24. the Poly1305 authenticator, both as as combined-mode,
  25. or Authenticated Encryption with Additional Data (AEAD) algorithm.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30. #include <wolfssl/wolfcrypt/settings.h>
  31. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  32. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  33. #include <wolfssl/wolfcrypt/error-crypt.h>
  34. #include <wolfssl/wolfcrypt/logging.h>
  35. #ifdef NO_INLINE
  36. #include <wolfssl/wolfcrypt/misc.h>
  37. #else
  38. #define WOLFSSL_MISC_INCLUDED
  39. #include <wolfcrypt/src/misc.c>
  40. #endif
  41. #define CHACHA20_POLY1305_AEAD_INITIAL_COUNTER 0
  42. int wc_ChaCha20Poly1305_Encrypt(
  43. const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
  44. const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
  45. const byte* inAAD, const word32 inAADLen,
  46. const byte* inPlaintext, const word32 inPlaintextLen,
  47. byte* outCiphertext,
  48. byte outAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE])
  49. {
  50. int ret;
  51. ChaChaPoly_Aead aead;
  52. /* Validate function arguments */
  53. if (!inKey || !inIV ||
  54. (inPlaintextLen > 0 && inPlaintext == NULL) ||
  55. !outCiphertext ||
  56. !outAuthTag)
  57. {
  58. return BAD_FUNC_ARG;
  59. }
  60. ret = wc_ChaCha20Poly1305_Init(&aead, inKey, inIV,
  61. CHACHA20_POLY1305_AEAD_ENCRYPT);
  62. if (ret == 0)
  63. ret = wc_ChaCha20Poly1305_UpdateAad(&aead, inAAD, inAADLen);
  64. if (ret == 0)
  65. ret = wc_ChaCha20Poly1305_UpdateData(&aead, inPlaintext, outCiphertext,
  66. inPlaintextLen);
  67. if (ret == 0)
  68. ret = wc_ChaCha20Poly1305_Final(&aead, outAuthTag);
  69. return ret;
  70. }
  71. int wc_ChaCha20Poly1305_Decrypt(
  72. const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
  73. const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
  74. const byte* inAAD, const word32 inAADLen,
  75. const byte* inCiphertext, const word32 inCiphertextLen,
  76. const byte inAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE],
  77. byte* outPlaintext)
  78. {
  79. int ret;
  80. ChaChaPoly_Aead aead;
  81. byte calculatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  82. /* Validate function arguments */
  83. if (!inKey || !inIV ||
  84. (inCiphertextLen > 0 && inCiphertext == NULL) ||
  85. !inAuthTag ||
  86. !outPlaintext)
  87. {
  88. return BAD_FUNC_ARG;
  89. }
  90. XMEMSET(calculatedAuthTag, 0, sizeof(calculatedAuthTag));
  91. ret = wc_ChaCha20Poly1305_Init(&aead, inKey, inIV,
  92. CHACHA20_POLY1305_AEAD_DECRYPT);
  93. if (ret == 0)
  94. ret = wc_ChaCha20Poly1305_UpdateAad(&aead, inAAD, inAADLen);
  95. if (ret == 0)
  96. ret = wc_ChaCha20Poly1305_UpdateData(&aead, inCiphertext, outPlaintext,
  97. inCiphertextLen);
  98. if (ret == 0)
  99. ret = wc_ChaCha20Poly1305_Final(&aead, calculatedAuthTag);
  100. if (ret == 0)
  101. ret = wc_ChaCha20Poly1305_CheckTag(inAuthTag, calculatedAuthTag);
  102. return ret;
  103. }
  104. int wc_ChaCha20Poly1305_CheckTag(
  105. const byte authTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE],
  106. const byte authTagChk[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE])
  107. {
  108. int ret = 0;
  109. if (authTag == NULL || authTagChk == NULL) {
  110. return BAD_FUNC_ARG;
  111. }
  112. if (ConstantCompare(authTag, authTagChk,
  113. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) != 0) {
  114. ret = MAC_CMP_FAILED_E;
  115. }
  116. return ret;
  117. }
  118. int wc_ChaCha20Poly1305_Init(ChaChaPoly_Aead* aead,
  119. const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
  120. const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
  121. int isEncrypt)
  122. {
  123. int ret;
  124. byte authKey[CHACHA20_POLY1305_AEAD_KEYSIZE];
  125. /* check arguments */
  126. if (aead == NULL || inKey == NULL || inIV == NULL) {
  127. return BAD_FUNC_ARG;
  128. }
  129. /* setup aead context */
  130. XMEMSET(aead, 0, sizeof(ChaChaPoly_Aead));
  131. XMEMSET(authKey, 0, sizeof(authKey));
  132. aead->isEncrypt = (byte)isEncrypt;
  133. /* Initialize the ChaCha20 context (key and iv) */
  134. ret = wc_Chacha_SetKey(&aead->chacha, inKey,
  135. CHACHA20_POLY1305_AEAD_KEYSIZE);
  136. if (ret == 0) {
  137. ret = wc_Chacha_SetIV(&aead->chacha, inIV,
  138. CHACHA20_POLY1305_AEAD_INITIAL_COUNTER);
  139. }
  140. /* Create the Poly1305 key */
  141. if (ret == 0) {
  142. ret = wc_Chacha_Process(&aead->chacha, authKey, authKey,
  143. CHACHA20_POLY1305_AEAD_KEYSIZE);
  144. }
  145. /* Initialize Poly1305 context */
  146. if (ret == 0) {
  147. ret = wc_Poly1305SetKey(&aead->poly, authKey,
  148. CHACHA20_POLY1305_AEAD_KEYSIZE);
  149. }
  150. /* advance counter by 1 after creating Poly1305 key */
  151. if (ret == 0) {
  152. ret = wc_Chacha_SetIV(&aead->chacha, inIV,
  153. CHACHA20_POLY1305_AEAD_INITIAL_COUNTER + 1);
  154. }
  155. if (ret == 0) {
  156. aead->state = CHACHA20_POLY1305_STATE_READY;
  157. }
  158. return ret;
  159. }
  160. /* optional additional authentication data */
  161. int wc_ChaCha20Poly1305_UpdateAad(ChaChaPoly_Aead* aead,
  162. const byte* inAAD, word32 inAADLen)
  163. {
  164. int ret = 0;
  165. if (aead == NULL || (inAAD == NULL && inAADLen > 0)) {
  166. return BAD_FUNC_ARG;
  167. }
  168. if (aead->state != CHACHA20_POLY1305_STATE_READY &&
  169. aead->state != CHACHA20_POLY1305_STATE_AAD) {
  170. return BAD_STATE_E;
  171. }
  172. if (inAADLen > CHACHA20_POLY1305_MAX - aead->aadLen)
  173. return CHACHA_POLY_OVERFLOW;
  174. if (inAAD && inAADLen > 0) {
  175. ret = wc_Poly1305Update(&aead->poly, inAAD, inAADLen);
  176. if (ret == 0) {
  177. aead->aadLen += inAADLen;
  178. aead->state = CHACHA20_POLY1305_STATE_AAD;
  179. }
  180. }
  181. return ret;
  182. }
  183. /* inData and outData can be same pointer (inline) */
  184. int wc_ChaCha20Poly1305_UpdateData(ChaChaPoly_Aead* aead,
  185. const byte* inData, byte* outData, word32 dataLen)
  186. {
  187. int ret = 0;
  188. if (aead == NULL || inData == NULL || outData == NULL) {
  189. return BAD_FUNC_ARG;
  190. }
  191. if (aead->state != CHACHA20_POLY1305_STATE_READY &&
  192. aead->state != CHACHA20_POLY1305_STATE_AAD &&
  193. aead->state != CHACHA20_POLY1305_STATE_DATA) {
  194. return BAD_STATE_E;
  195. }
  196. if (dataLen > CHACHA20_POLY1305_MAX - aead->dataLen)
  197. return CHACHA_POLY_OVERFLOW;
  198. /* Pad the AAD */
  199. if (aead->state == CHACHA20_POLY1305_STATE_AAD) {
  200. ret = wc_Poly1305_Pad(&aead->poly, aead->aadLen);
  201. }
  202. /* advance state */
  203. aead->state = CHACHA20_POLY1305_STATE_DATA;
  204. /* Perform ChaCha20 encrypt/decrypt and Poly1305 auth calc */
  205. if (ret == 0) {
  206. if (aead->isEncrypt) {
  207. ret = wc_Chacha_Process(&aead->chacha, outData, inData, dataLen);
  208. if (ret == 0)
  209. ret = wc_Poly1305Update(&aead->poly, outData, dataLen);
  210. }
  211. else {
  212. ret = wc_Poly1305Update(&aead->poly, inData, dataLen);
  213. if (ret == 0)
  214. ret = wc_Chacha_Process(&aead->chacha, outData, inData, dataLen);
  215. }
  216. }
  217. if (ret == 0) {
  218. aead->dataLen += dataLen;
  219. }
  220. return ret;
  221. }
  222. int wc_ChaCha20Poly1305_Final(ChaChaPoly_Aead* aead,
  223. byte outAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE])
  224. {
  225. int ret = 0;
  226. if (aead == NULL || outAuthTag == NULL) {
  227. return BAD_FUNC_ARG;
  228. }
  229. if (aead->state != CHACHA20_POLY1305_STATE_AAD &&
  230. aead->state != CHACHA20_POLY1305_STATE_DATA) {
  231. return BAD_STATE_E;
  232. }
  233. /* Pad the AAD - Make sure it is done */
  234. if (aead->state == CHACHA20_POLY1305_STATE_AAD) {
  235. ret = wc_Poly1305_Pad(&aead->poly, aead->aadLen);
  236. }
  237. /* Pad the plaintext/ciphertext to 16 bytes */
  238. if (ret == 0) {
  239. ret = wc_Poly1305_Pad(&aead->poly, aead->dataLen);
  240. }
  241. /* Add the aad length and plaintext/ciphertext length */
  242. if (ret == 0) {
  243. ret = wc_Poly1305_EncodeSizes(&aead->poly, aead->aadLen,
  244. aead->dataLen);
  245. }
  246. /* Finalize the auth tag */
  247. if (ret == 0) {
  248. ret = wc_Poly1305Final(&aead->poly, outAuthTag);
  249. }
  250. /* reset and cleanup sensitive context */
  251. ForceZero(aead, sizeof(ChaChaPoly_Aead));
  252. return ret;
  253. }
  254. #ifdef HAVE_XCHACHA
  255. int wc_XChaCha20Poly1305_Init(
  256. ChaChaPoly_Aead *aead,
  257. const byte *ad, word32 ad_len,
  258. const byte *nonce, word32 nonce_len,
  259. const byte *key, word32 key_len,
  260. int isEncrypt)
  261. {
  262. byte authKey[CHACHA20_POLY1305_AEAD_KEYSIZE];
  263. int ret;
  264. if ((ad == NULL) || (nonce == NULL) || (key == NULL))
  265. return BAD_FUNC_ARG;
  266. if ((key_len != CHACHA20_POLY1305_AEAD_KEYSIZE) ||
  267. (nonce_len != XCHACHA20_POLY1305_AEAD_NONCE_SIZE))
  268. return BAD_FUNC_ARG;
  269. if ((ret = wc_XChacha_SetKey(&aead->chacha,
  270. key, key_len,
  271. nonce, nonce_len,
  272. 0 /* counter */)) < 0)
  273. return ret;
  274. XMEMSET(authKey, 0, sizeof authKey);
  275. /* Create the Poly1305 key */
  276. if ((ret = wc_Chacha_Process(&aead->chacha, authKey, authKey,
  277. (word32)sizeof authKey)) < 0)
  278. return ret;
  279. /* advance to start of the next ChaCha block. */
  280. wc_Chacha_purge_current_block(&aead->chacha);
  281. /* Initialize Poly1305 context */
  282. if ((ret = wc_Poly1305SetKey(&aead->poly, authKey,
  283. (word32)sizeof authKey)) < 0)
  284. return ret;
  285. if ((ret = wc_Poly1305Update(&aead->poly, ad, (word32)ad_len)) < 0)
  286. return ret;
  287. if ((ret = wc_Poly1305_Pad(&aead->poly, (word32)ad_len)) < 0)
  288. return ret;
  289. aead->isEncrypt = (byte)isEncrypt;
  290. aead->state = CHACHA20_POLY1305_STATE_AAD;
  291. return 0;
  292. }
  293. static WC_INLINE int wc_XChaCha20Poly1305_crypt_oneshot(
  294. byte *dst, const size_t dst_space,
  295. const byte *src, const size_t src_len,
  296. const byte *ad, const size_t ad_len,
  297. const byte *nonce, const size_t nonce_len,
  298. const byte *key, const size_t key_len,
  299. int isEncrypt)
  300. {
  301. int ret;
  302. ssize_t dst_len = isEncrypt ?
  303. (ssize_t)src_len + POLY1305_DIGEST_SIZE :
  304. (ssize_t)src_len - POLY1305_DIGEST_SIZE;
  305. const byte *src_i;
  306. byte *dst_i;
  307. size_t src_len_rem;
  308. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  309. ChaChaPoly_Aead *aead = (ChaChaPoly_Aead *)XMALLOC(sizeof *aead, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  310. if (aead == NULL)
  311. return MEMORY_E;
  312. #else
  313. ChaChaPoly_Aead aead_buf, *aead = &aead_buf;
  314. #endif
  315. if ((dst == NULL) || (src == NULL)) {
  316. ret = BAD_FUNC_ARG;
  317. goto out;
  318. }
  319. if ((ssize_t)dst_space < dst_len) {
  320. ret = BUFFER_E;
  321. goto out;
  322. }
  323. if ((ret = wc_XChaCha20Poly1305_Init(aead, ad, (word32)ad_len,
  324. nonce, (word32)nonce_len,
  325. key, (word32)key_len, 1)) < 0)
  326. goto out;
  327. #ifdef WOLFSSL_CHECK_MEM_ZERO
  328. wc_MemZero_Add("wc_XChaCha20Poly1305_crypt_oneshot aead", aead,
  329. sizeof(ChaChaPoly_Aead));
  330. #endif
  331. /* process the input in 16k pieces to accommodate src_lens that don't fit in a word32,
  332. * and to exploit hot cache for the input data.
  333. */
  334. src_i = src;
  335. src_len_rem = isEncrypt ? src_len : (size_t)dst_len;
  336. dst_i = dst;
  337. while (src_len_rem > 0) {
  338. word32 this_src_len =
  339. (src_len_rem > 16384) ?
  340. 16384 :
  341. (word32)src_len_rem;
  342. if ((ret = wc_Chacha_Process(&aead->chacha, dst_i, src_i, this_src_len)) < 0)
  343. goto out;
  344. if ((ret = wc_Poly1305Update(&aead->poly, isEncrypt ? dst_i : src_i, this_src_len)) < 0)
  345. goto out;
  346. src_len_rem -= (size_t)this_src_len;
  347. src_i += this_src_len;
  348. dst_i += this_src_len;
  349. }
  350. if (aead->poly.leftover) {
  351. if ((ret = wc_Poly1305_Pad(&aead->poly, (word32)aead->poly.leftover)) < 0)
  352. return ret;
  353. }
  354. #ifdef WORD64_AVAILABLE
  355. ret = wc_Poly1305_EncodeSizes64(&aead->poly, ad_len, isEncrypt ? src_len : (size_t)dst_len);
  356. #else
  357. ret = wc_Poly1305_EncodeSizes(&aead->poly, ad_len, isEncrypt ? src_len : (size_t)dst_len);
  358. #endif
  359. if (ret < 0)
  360. goto out;
  361. if (isEncrypt)
  362. ret = wc_Poly1305Final(&aead->poly, dst + src_len);
  363. else {
  364. byte outAuthTag[POLY1305_DIGEST_SIZE];
  365. if ((ret = wc_Poly1305Final(&aead->poly, outAuthTag)) < 0)
  366. goto out;
  367. if (ConstantCompare(outAuthTag, src + dst_len, POLY1305_DIGEST_SIZE) != 0) {
  368. ret = MAC_CMP_FAILED_E;
  369. goto out;
  370. }
  371. }
  372. out:
  373. ForceZero(aead, sizeof *aead);
  374. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  375. XFREE(aead, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  376. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  377. wc_MemZero_Check(aead, sizeof(ChaChaPoly_Aead));
  378. #endif
  379. return ret;
  380. }
  381. int wc_XChaCha20Poly1305_Encrypt(
  382. byte *dst, const size_t dst_space,
  383. const byte *src, const size_t src_len,
  384. const byte *ad, const size_t ad_len,
  385. const byte *nonce, const size_t nonce_len,
  386. const byte *key, const size_t key_len)
  387. {
  388. return wc_XChaCha20Poly1305_crypt_oneshot(dst, dst_space, src, src_len, ad, ad_len, nonce, nonce_len, key, key_len, 1);
  389. }
  390. int wc_XChaCha20Poly1305_Decrypt(
  391. byte *dst, const size_t dst_space,
  392. const byte *src, const size_t src_len,
  393. const byte *ad, const size_t ad_len,
  394. const byte *nonce, const size_t nonce_len,
  395. const byte *key, const size_t key_len)
  396. {
  397. return wc_XChaCha20Poly1305_crypt_oneshot(dst, dst_space, src, src_len, ad, ad_len, nonce, nonce_len, key, key_len, 0);
  398. }
  399. #endif /* HAVE_XCHACHA */
  400. #endif /* HAVE_CHACHA && HAVE_POLY1305 */