chachapoly.c 18 KB

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