chachapoly.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /**
  2. * \file chachapoly.c
  3. *
  4. * \brief ChaCha20-Poly1305 AEAD construction based on RFC 7539.
  5. *
  6. * Copyright The Mbed TLS Contributors
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. #include "common.h"
  22. #if defined(MBEDTLS_CHACHAPOLY_C)
  23. #include "mbedtls/chachapoly.h"
  24. #include "mbedtls/platform_util.h"
  25. #include "mbedtls/error.h"
  26. #include <string.h>
  27. #include "mbedtls/platform.h"
  28. #if !defined(MBEDTLS_CHACHAPOLY_ALT)
  29. #define CHACHAPOLY_STATE_INIT (0)
  30. #define CHACHAPOLY_STATE_AAD (1)
  31. #define CHACHAPOLY_STATE_CIPHERTEXT (2) /* Encrypting or decrypting */
  32. #define CHACHAPOLY_STATE_FINISHED (3)
  33. /**
  34. * \brief Adds nul bytes to pad the AAD for Poly1305.
  35. *
  36. * \param ctx The ChaCha20-Poly1305 context.
  37. */
  38. static int chachapoly_pad_aad(mbedtls_chachapoly_context *ctx)
  39. {
  40. uint32_t partial_block_len = (uint32_t) (ctx->aad_len % 16U);
  41. unsigned char zeroes[15];
  42. if (partial_block_len == 0U) {
  43. return 0;
  44. }
  45. memset(zeroes, 0, sizeof(zeroes));
  46. return mbedtls_poly1305_update(&ctx->poly1305_ctx,
  47. zeroes,
  48. 16U - partial_block_len);
  49. }
  50. /**
  51. * \brief Adds nul bytes to pad the ciphertext for Poly1305.
  52. *
  53. * \param ctx The ChaCha20-Poly1305 context.
  54. */
  55. static int chachapoly_pad_ciphertext(mbedtls_chachapoly_context *ctx)
  56. {
  57. uint32_t partial_block_len = (uint32_t) (ctx->ciphertext_len % 16U);
  58. unsigned char zeroes[15];
  59. if (partial_block_len == 0U) {
  60. return 0;
  61. }
  62. memset(zeroes, 0, sizeof(zeroes));
  63. return mbedtls_poly1305_update(&ctx->poly1305_ctx,
  64. zeroes,
  65. 16U - partial_block_len);
  66. }
  67. void mbedtls_chachapoly_init(mbedtls_chachapoly_context *ctx)
  68. {
  69. mbedtls_chacha20_init(&ctx->chacha20_ctx);
  70. mbedtls_poly1305_init(&ctx->poly1305_ctx);
  71. ctx->aad_len = 0U;
  72. ctx->ciphertext_len = 0U;
  73. ctx->state = CHACHAPOLY_STATE_INIT;
  74. ctx->mode = MBEDTLS_CHACHAPOLY_ENCRYPT;
  75. }
  76. void mbedtls_chachapoly_free(mbedtls_chachapoly_context *ctx)
  77. {
  78. if (ctx == NULL) {
  79. return;
  80. }
  81. mbedtls_chacha20_free(&ctx->chacha20_ctx);
  82. mbedtls_poly1305_free(&ctx->poly1305_ctx);
  83. ctx->aad_len = 0U;
  84. ctx->ciphertext_len = 0U;
  85. ctx->state = CHACHAPOLY_STATE_INIT;
  86. ctx->mode = MBEDTLS_CHACHAPOLY_ENCRYPT;
  87. }
  88. int mbedtls_chachapoly_setkey(mbedtls_chachapoly_context *ctx,
  89. const unsigned char key[32])
  90. {
  91. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  92. ret = mbedtls_chacha20_setkey(&ctx->chacha20_ctx, key);
  93. return ret;
  94. }
  95. int mbedtls_chachapoly_starts(mbedtls_chachapoly_context *ctx,
  96. const unsigned char nonce[12],
  97. mbedtls_chachapoly_mode_t mode)
  98. {
  99. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  100. unsigned char poly1305_key[64];
  101. /* Set counter = 0, will be update to 1 when generating Poly1305 key */
  102. ret = mbedtls_chacha20_starts(&ctx->chacha20_ctx, nonce, 0U);
  103. if (ret != 0) {
  104. goto cleanup;
  105. }
  106. /* Generate the Poly1305 key by getting the ChaCha20 keystream output with
  107. * counter = 0. This is the same as encrypting a buffer of zeroes.
  108. * Only the first 256-bits (32 bytes) of the key is used for Poly1305.
  109. * The other 256 bits are discarded.
  110. */
  111. memset(poly1305_key, 0, sizeof(poly1305_key));
  112. ret = mbedtls_chacha20_update(&ctx->chacha20_ctx, sizeof(poly1305_key),
  113. poly1305_key, poly1305_key);
  114. if (ret != 0) {
  115. goto cleanup;
  116. }
  117. ret = mbedtls_poly1305_starts(&ctx->poly1305_ctx, poly1305_key);
  118. if (ret == 0) {
  119. ctx->aad_len = 0U;
  120. ctx->ciphertext_len = 0U;
  121. ctx->state = CHACHAPOLY_STATE_AAD;
  122. ctx->mode = mode;
  123. }
  124. cleanup:
  125. mbedtls_platform_zeroize(poly1305_key, 64U);
  126. return ret;
  127. }
  128. int mbedtls_chachapoly_update_aad(mbedtls_chachapoly_context *ctx,
  129. const unsigned char *aad,
  130. size_t aad_len)
  131. {
  132. if (ctx->state != CHACHAPOLY_STATE_AAD) {
  133. return MBEDTLS_ERR_CHACHAPOLY_BAD_STATE;
  134. }
  135. ctx->aad_len += aad_len;
  136. return mbedtls_poly1305_update(&ctx->poly1305_ctx, aad, aad_len);
  137. }
  138. int mbedtls_chachapoly_update(mbedtls_chachapoly_context *ctx,
  139. size_t len,
  140. const unsigned char *input,
  141. unsigned char *output)
  142. {
  143. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  144. if ((ctx->state != CHACHAPOLY_STATE_AAD) &&
  145. (ctx->state != CHACHAPOLY_STATE_CIPHERTEXT)) {
  146. return MBEDTLS_ERR_CHACHAPOLY_BAD_STATE;
  147. }
  148. if (ctx->state == CHACHAPOLY_STATE_AAD) {
  149. ctx->state = CHACHAPOLY_STATE_CIPHERTEXT;
  150. ret = chachapoly_pad_aad(ctx);
  151. if (ret != 0) {
  152. return ret;
  153. }
  154. }
  155. ctx->ciphertext_len += len;
  156. if (ctx->mode == MBEDTLS_CHACHAPOLY_ENCRYPT) {
  157. ret = mbedtls_chacha20_update(&ctx->chacha20_ctx, len, input, output);
  158. if (ret != 0) {
  159. return ret;
  160. }
  161. ret = mbedtls_poly1305_update(&ctx->poly1305_ctx, output, len);
  162. if (ret != 0) {
  163. return ret;
  164. }
  165. } else { /* DECRYPT */
  166. ret = mbedtls_poly1305_update(&ctx->poly1305_ctx, input, len);
  167. if (ret != 0) {
  168. return ret;
  169. }
  170. ret = mbedtls_chacha20_update(&ctx->chacha20_ctx, len, input, output);
  171. if (ret != 0) {
  172. return ret;
  173. }
  174. }
  175. return 0;
  176. }
  177. int mbedtls_chachapoly_finish(mbedtls_chachapoly_context *ctx,
  178. unsigned char mac[16])
  179. {
  180. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  181. unsigned char len_block[16];
  182. if (ctx->state == CHACHAPOLY_STATE_INIT) {
  183. return MBEDTLS_ERR_CHACHAPOLY_BAD_STATE;
  184. }
  185. if (ctx->state == CHACHAPOLY_STATE_AAD) {
  186. ret = chachapoly_pad_aad(ctx);
  187. if (ret != 0) {
  188. return ret;
  189. }
  190. } else if (ctx->state == CHACHAPOLY_STATE_CIPHERTEXT) {
  191. ret = chachapoly_pad_ciphertext(ctx);
  192. if (ret != 0) {
  193. return ret;
  194. }
  195. }
  196. ctx->state = CHACHAPOLY_STATE_FINISHED;
  197. /* The lengths of the AAD and ciphertext are processed by
  198. * Poly1305 as the final 128-bit block, encoded as little-endian integers.
  199. */
  200. MBEDTLS_PUT_UINT64_LE(ctx->aad_len, len_block, 0);
  201. MBEDTLS_PUT_UINT64_LE(ctx->ciphertext_len, len_block, 8);
  202. ret = mbedtls_poly1305_update(&ctx->poly1305_ctx, len_block, 16U);
  203. if (ret != 0) {
  204. return ret;
  205. }
  206. ret = mbedtls_poly1305_finish(&ctx->poly1305_ctx, mac);
  207. return ret;
  208. }
  209. static int chachapoly_crypt_and_tag(mbedtls_chachapoly_context *ctx,
  210. mbedtls_chachapoly_mode_t mode,
  211. size_t length,
  212. const unsigned char nonce[12],
  213. const unsigned char *aad,
  214. size_t aad_len,
  215. const unsigned char *input,
  216. unsigned char *output,
  217. unsigned char tag[16])
  218. {
  219. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  220. ret = mbedtls_chachapoly_starts(ctx, nonce, mode);
  221. if (ret != 0) {
  222. goto cleanup;
  223. }
  224. ret = mbedtls_chachapoly_update_aad(ctx, aad, aad_len);
  225. if (ret != 0) {
  226. goto cleanup;
  227. }
  228. ret = mbedtls_chachapoly_update(ctx, length, input, output);
  229. if (ret != 0) {
  230. goto cleanup;
  231. }
  232. ret = mbedtls_chachapoly_finish(ctx, tag);
  233. cleanup:
  234. return ret;
  235. }
  236. int mbedtls_chachapoly_encrypt_and_tag(mbedtls_chachapoly_context *ctx,
  237. size_t length,
  238. const unsigned char nonce[12],
  239. const unsigned char *aad,
  240. size_t aad_len,
  241. const unsigned char *input,
  242. unsigned char *output,
  243. unsigned char tag[16])
  244. {
  245. return chachapoly_crypt_and_tag(ctx, MBEDTLS_CHACHAPOLY_ENCRYPT,
  246. length, nonce, aad, aad_len,
  247. input, output, tag);
  248. }
  249. int mbedtls_chachapoly_auth_decrypt(mbedtls_chachapoly_context *ctx,
  250. size_t length,
  251. const unsigned char nonce[12],
  252. const unsigned char *aad,
  253. size_t aad_len,
  254. const unsigned char tag[16],
  255. const unsigned char *input,
  256. unsigned char *output)
  257. {
  258. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  259. unsigned char check_tag[16];
  260. size_t i;
  261. int diff;
  262. if ((ret = chachapoly_crypt_and_tag(ctx,
  263. MBEDTLS_CHACHAPOLY_DECRYPT, length, nonce,
  264. aad, aad_len, input, output, check_tag)) != 0) {
  265. return ret;
  266. }
  267. /* Check tag in "constant-time" */
  268. for (diff = 0, i = 0; i < sizeof(check_tag); i++) {
  269. diff |= tag[i] ^ check_tag[i];
  270. }
  271. if (diff != 0) {
  272. mbedtls_platform_zeroize(output, length);
  273. return MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED;
  274. }
  275. return 0;
  276. }
  277. #endif /* MBEDTLS_CHACHAPOLY_ALT */
  278. #if defined(MBEDTLS_SELF_TEST)
  279. static const unsigned char test_key[1][32] =
  280. {
  281. {
  282. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  283. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  284. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  285. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  286. }
  287. };
  288. static const unsigned char test_nonce[1][12] =
  289. {
  290. {
  291. 0x07, 0x00, 0x00, 0x00, /* 32-bit common part */
  292. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 /* 64-bit IV */
  293. }
  294. };
  295. static const unsigned char test_aad[1][12] =
  296. {
  297. {
  298. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  299. 0xc4, 0xc5, 0xc6, 0xc7
  300. }
  301. };
  302. static const size_t test_aad_len[1] =
  303. {
  304. 12U
  305. };
  306. static const unsigned char test_input[1][114] =
  307. {
  308. {
  309. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  310. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  311. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  312. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  313. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  314. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  315. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  316. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  317. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  318. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  319. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  320. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  321. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  322. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  323. 0x74, 0x2e
  324. }
  325. };
  326. static const unsigned char test_output[1][114] =
  327. {
  328. {
  329. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  330. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  331. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  332. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  333. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  334. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  335. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  336. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  337. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  338. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  339. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  340. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  341. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  342. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  343. 0x61, 0x16
  344. }
  345. };
  346. static const size_t test_input_len[1] =
  347. {
  348. 114U
  349. };
  350. static const unsigned char test_mac[1][16] =
  351. {
  352. {
  353. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  354. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  355. }
  356. };
  357. /* Make sure no other definition is already present. */
  358. #undef ASSERT
  359. #define ASSERT(cond, args) \
  360. do \
  361. { \
  362. if (!(cond)) \
  363. { \
  364. if (verbose != 0) \
  365. mbedtls_printf args; \
  366. \
  367. return -1; \
  368. } \
  369. } \
  370. while (0)
  371. int mbedtls_chachapoly_self_test(int verbose)
  372. {
  373. mbedtls_chachapoly_context ctx;
  374. unsigned i;
  375. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  376. unsigned char output[200];
  377. unsigned char mac[16];
  378. for (i = 0U; i < 1U; i++) {
  379. if (verbose != 0) {
  380. mbedtls_printf(" ChaCha20-Poly1305 test %u ", i);
  381. }
  382. mbedtls_chachapoly_init(&ctx);
  383. ret = mbedtls_chachapoly_setkey(&ctx, test_key[i]);
  384. ASSERT(0 == ret, ("setkey() error code: %i\n", ret));
  385. ret = mbedtls_chachapoly_encrypt_and_tag(&ctx,
  386. test_input_len[i],
  387. test_nonce[i],
  388. test_aad[i],
  389. test_aad_len[i],
  390. test_input[i],
  391. output,
  392. mac);
  393. ASSERT(0 == ret, ("crypt_and_tag() error code: %i\n", ret));
  394. ASSERT(0 == memcmp(output, test_output[i], test_input_len[i]),
  395. ("failure (wrong output)\n"));
  396. ASSERT(0 == memcmp(mac, test_mac[i], 16U),
  397. ("failure (wrong MAC)\n"));
  398. mbedtls_chachapoly_free(&ctx);
  399. if (verbose != 0) {
  400. mbedtls_printf("passed\n");
  401. }
  402. }
  403. if (verbose != 0) {
  404. mbedtls_printf("\n");
  405. }
  406. return 0;
  407. }
  408. #endif /* MBEDTLS_SELF_TEST */
  409. #endif /* MBEDTLS_CHACHAPOLY_C */