chachapoly.c 17 KB

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