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