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