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