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