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 | * |
| 6 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 22 | */ |
| 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 24 | #include "mbedtls/config.h" |
| 25 | #else |
| 26 | #include MBEDTLS_CONFIG_FILE |
| 27 | #endif |
| 28 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 31 | #include "mbedtls/chachapoly.h" |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 32 | #include <string.h> |
| 33 | |
| 34 | #if defined(MBEDTLS_SELF_TEST) |
| 35 | #if defined(MBEDTLS_PLATFORM_C) |
| 36 | #include "mbedtls/platform.h" |
| 37 | #else |
| 38 | #include <stdio.h> |
| 39 | #define mbedtls_printf printf |
| 40 | #endif /* MBEDTLS_PLATFORM_C */ |
| 41 | #endif /* MBEDTLS_SELF_TEST */ |
| 42 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 43 | #if !defined(MBEDTLS_CHACHAPOLY_ALT) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 45 | #define CHACHAPOLY_STATE_INIT ( 0 ) |
| 46 | #define CHACHAPOLY_STATE_AAD ( 1 ) |
| 47 | #define CHACHAPOLY_STATE_CIPHERTEXT ( 2 ) /* Encrypting or decrypting */ |
| 48 | #define CHACHAPOLY_STATE_FINISHED ( 3 ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 49 | |
| 50 | /* Implementation that should never be optimized out by the compiler */ |
| 51 | static void mbedtls_zeroize( void *v, size_t n ) { |
| 52 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * \brief Adds padding bytes (zeroes) to pad the AAD for Poly1305. |
| 57 | * |
| 58 | * \param ctx The ChaCha20-Poly1305 context. |
| 59 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 60 | static void mbedtls_chachapoly_pad_aad( mbedtls_chachapoly_context *ctx ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 61 | { |
Daniel King | e6e7968 | 2016-05-24 11:16:17 -0300 | [diff] [blame] | 62 | uint32_t partial_block_len = (uint32_t) ( ctx->aad_len % 16U ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 63 | unsigned char zeroes[15]; |
| 64 | |
| 65 | if ( partial_block_len > 0U ) |
| 66 | { |
Daniel King | e6e7968 | 2016-05-24 11:16:17 -0300 | [diff] [blame] | 67 | memset( zeroes, 0, sizeof( zeroes ) ); |
| 68 | (void) mbedtls_poly1305_update( &ctx->poly1305_ctx, |
| 69 | 16U - partial_block_len, |
| 70 | zeroes ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * \brief Adds padding bytes (zeroes) to pad the ciphertext for Poly1305. |
| 76 | * |
| 77 | * \param ctx The ChaCha20-Poly1305 context. |
| 78 | */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 79 | static void mbedtls_chachapoly_pad_ciphertext( mbedtls_chachapoly_context *ctx ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 80 | { |
Daniel King | e6e7968 | 2016-05-24 11:16:17 -0300 | [diff] [blame] | 81 | uint32_t partial_block_len = (uint32_t) ( ctx->ciphertext_len % 16U ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 82 | unsigned char zeroes[15]; |
| 83 | |
| 84 | if ( partial_block_len > 0U ) |
| 85 | { |
Daniel King | e6e7968 | 2016-05-24 11:16:17 -0300 | [diff] [blame] | 86 | memset( zeroes, 0, sizeof( zeroes ) ); |
| 87 | (void) mbedtls_poly1305_update( &ctx->poly1305_ctx, |
| 88 | 16U - partial_block_len, |
| 89 | zeroes ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 93 | void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 94 | { |
| 95 | if ( ctx != NULL ) |
| 96 | { |
| 97 | mbedtls_chacha20_init( &ctx->chacha20_ctx ); |
| 98 | mbedtls_poly1305_init( &ctx->poly1305_ctx ); |
| 99 | ctx->aad_len = 0U; |
| 100 | ctx->ciphertext_len = 0U; |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 101 | ctx->state = CHACHAPOLY_STATE_INIT; |
| 102 | ctx->mode = MBEDTLS_CHACHAPOLY_ENCRYPT; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 106 | void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 107 | { |
| 108 | if ( ctx != NULL ) |
| 109 | { |
| 110 | mbedtls_chacha20_free( &ctx->chacha20_ctx ); |
| 111 | mbedtls_poly1305_free( &ctx->poly1305_ctx ); |
| 112 | ctx->aad_len = 0U; |
| 113 | ctx->ciphertext_len = 0U; |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 114 | ctx->state = CHACHAPOLY_STATE_INIT; |
| 115 | ctx->mode = MBEDTLS_CHACHAPOLY_ENCRYPT; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 119 | int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx, |
| 120 | const unsigned char key[32] ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 121 | { |
| 122 | int result; |
| 123 | |
| 124 | if ( ( ctx == NULL ) || ( key == NULL ) ) |
| 125 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 126 | return( MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | result = mbedtls_chacha20_setkey( &ctx->chacha20_ctx, key ); |
| 130 | |
| 131 | return( result ); |
| 132 | } |
| 133 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 134 | int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx, |
| 135 | const unsigned char nonce[12], |
| 136 | mbedtls_chachapoly_mode_t mode ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 137 | { |
| 138 | int result; |
| 139 | unsigned char poly1305_key[64]; |
| 140 | |
| 141 | if ( ( ctx == NULL ) || ( nonce == NULL ) ) |
| 142 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 143 | return( MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 144 | } |
| 145 | |
Manuel Pégourié-Gonnard | 56206c4 | 2018-05-07 12:18:34 +0200 | [diff] [blame^] | 146 | /* Set counter = 0, will be update to 1 when generating Poly1305 key */ |
| 147 | result = mbedtls_chacha20_starts( &ctx->chacha20_ctx, nonce, 0U ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 148 | if ( result != 0 ) |
| 149 | goto cleanup; |
| 150 | |
| 151 | /* Generate the Poly1305 key by getting the ChaCha20 keystream output with counter = 0. |
Manuel Pégourié-Gonnard | 56206c4 | 2018-05-07 12:18:34 +0200 | [diff] [blame^] | 152 | * This is the same as encrypting a buffer of zeroes. |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 153 | * Only the first 256-bits (32 bytes) of the key is used for Poly1305. |
| 154 | * The other 256 bits are discarded. |
| 155 | */ |
Manuel Pégourié-Gonnard | 56206c4 | 2018-05-07 12:18:34 +0200 | [diff] [blame^] | 156 | memset( poly1305_key, 0, sizeof( poly1305_key ) ); |
| 157 | result = mbedtls_chacha20_update( &ctx->chacha20_ctx, sizeof( poly1305_key ), |
| 158 | poly1305_key, poly1305_key ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 159 | if ( result != 0 ) |
| 160 | goto cleanup; |
| 161 | |
Manuel Pégourié-Gonnard | 4edd51b | 2018-05-07 10:21:56 +0200 | [diff] [blame] | 162 | result = mbedtls_poly1305_starts( &ctx->poly1305_ctx, poly1305_key ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 163 | |
| 164 | if ( result == 0 ) |
| 165 | { |
| 166 | ctx->aad_len = 0U; |
| 167 | ctx->ciphertext_len = 0U; |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 168 | ctx->state = CHACHAPOLY_STATE_AAD; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 169 | ctx->mode = mode; |
| 170 | } |
| 171 | |
| 172 | cleanup: |
| 173 | mbedtls_zeroize( poly1305_key, 64U ); |
| 174 | return( result ); |
| 175 | } |
| 176 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 177 | int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx, |
| 178 | size_t aad_len, |
| 179 | const unsigned char *aad ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 180 | { |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 181 | if ( ctx == NULL ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 182 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 183 | return( MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 184 | } |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 185 | else if ( ( aad_len > 0U ) && ( aad == NULL ) ) |
| 186 | { |
| 187 | /* aad pointer is allowed to be NULL if aad_len == 0 */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 188 | return( MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 189 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 190 | else if ( ctx->state != CHACHAPOLY_STATE_AAD ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 191 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 192 | return(MBEDTLS_ERR_CHACHAPOLY_BAD_STATE ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | ctx->aad_len += aad_len; |
| 196 | |
Daniel King | e6e7968 | 2016-05-24 11:16:17 -0300 | [diff] [blame] | 197 | return( mbedtls_poly1305_update( &ctx->poly1305_ctx, aad_len, aad ) ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 198 | } |
| 199 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 200 | int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx, |
| 201 | size_t len, |
| 202 | const unsigned char *input, |
| 203 | unsigned char *output ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 204 | { |
| 205 | if ( ( ctx == NULL ) || ( input == NULL ) || ( output == NULL ) ) |
| 206 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 207 | return( MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 208 | } |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 209 | else if ( ( len > 0U ) && ( ( input == NULL ) || ( output == NULL ) ) ) |
| 210 | { |
| 211 | /* input and output pointers are allowed to be NULL if len == 0 */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 212 | return( MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 213 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 214 | else if ( ( ctx->state != CHACHAPOLY_STATE_AAD ) && |
| 215 | ( ctx->state != CHACHAPOLY_STATE_CIPHERTEXT ) ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 216 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 217 | return( MBEDTLS_ERR_CHACHAPOLY_BAD_STATE ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 218 | } |
| 219 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 220 | if ( ctx->state == CHACHAPOLY_STATE_AAD ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 221 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 222 | ctx->state = CHACHAPOLY_STATE_CIPHERTEXT; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 223 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 224 | mbedtls_chachapoly_pad_aad( ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | ctx->ciphertext_len += len; |
| 228 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 229 | if ( ctx->mode == MBEDTLS_CHACHAPOLY_ENCRYPT ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 230 | { |
| 231 | /* Note: the following functions return an error only if one or more of |
| 232 | * the input pointers are NULL. Since we have checked their validity |
| 233 | * above, we can safety ignore the return value. |
| 234 | */ |
Daniel King | e6e7968 | 2016-05-24 11:16:17 -0300 | [diff] [blame] | 235 | (void) mbedtls_chacha20_update( &ctx->chacha20_ctx, len, input, output ); |
| 236 | (void) mbedtls_poly1305_update( &ctx->poly1305_ctx, len, output ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 237 | } |
| 238 | else /* DECRYPT */ |
| 239 | { |
Daniel King | e6e7968 | 2016-05-24 11:16:17 -0300 | [diff] [blame] | 240 | (void) mbedtls_poly1305_update( &ctx->poly1305_ctx, len, input ); |
| 241 | (void) mbedtls_chacha20_update( &ctx->chacha20_ctx, len, input, output ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | return( 0 ); |
| 245 | } |
| 246 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 247 | int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx, |
| 248 | unsigned char mac[16] ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 249 | { |
| 250 | unsigned char len_block[16]; |
| 251 | |
| 252 | if ( ( ctx == NULL ) || ( mac == NULL ) ) |
| 253 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 254 | return( MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 255 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 256 | else if ( ctx->state == CHACHAPOLY_STATE_INIT ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 257 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 258 | return( MBEDTLS_ERR_CHACHAPOLY_BAD_STATE ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 259 | } |
| 260 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 261 | if ( ctx->state == CHACHAPOLY_STATE_AAD ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 262 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 263 | mbedtls_chachapoly_pad_aad( ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 264 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 265 | else if ( ctx->state == CHACHAPOLY_STATE_CIPHERTEXT ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 266 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 267 | mbedtls_chachapoly_pad_ciphertext( ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 268 | } |
| 269 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 270 | ctx->state = CHACHAPOLY_STATE_FINISHED; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 271 | |
| 272 | /* The lengths of the AAD and ciphertext are processed by |
| 273 | * Poly1305 as the final 128-bit block, encoded as little-endian integers. |
| 274 | */ |
Daniel King | e6e7968 | 2016-05-24 11:16:17 -0300 | [diff] [blame] | 275 | len_block[0] = (unsigned char) ctx->aad_len; |
| 276 | len_block[1] = (unsigned char) ( ctx->aad_len >> 8 ); |
| 277 | len_block[2] = (unsigned char) ( ctx->aad_len >> 16 ); |
| 278 | len_block[3] = (unsigned char) ( ctx->aad_len >> 24 ); |
| 279 | len_block[4] = (unsigned char) ( ctx->aad_len >> 32 ); |
| 280 | len_block[5] = (unsigned char) ( ctx->aad_len >> 40 ); |
| 281 | len_block[6] = (unsigned char) ( ctx->aad_len >> 48 ); |
| 282 | len_block[7] = (unsigned char) ( ctx->aad_len >> 56 ); |
| 283 | len_block[8] = (unsigned char) ctx->ciphertext_len; |
| 284 | len_block[9] = (unsigned char) ( ctx->ciphertext_len >> 8 ); |
| 285 | len_block[10] = (unsigned char) ( ctx->ciphertext_len >> 16 ); |
| 286 | len_block[11] = (unsigned char) ( ctx->ciphertext_len >> 24 ); |
| 287 | len_block[12] = (unsigned char) ( ctx->ciphertext_len >> 32 ); |
| 288 | len_block[13] = (unsigned char) ( ctx->ciphertext_len >> 40 ); |
| 289 | len_block[14] = (unsigned char) ( ctx->ciphertext_len >> 48 ); |
| 290 | len_block[15] = (unsigned char) ( ctx->ciphertext_len >> 56 ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 291 | |
Daniel King | e6e7968 | 2016-05-24 11:16:17 -0300 | [diff] [blame] | 292 | (void) mbedtls_poly1305_update( &ctx->poly1305_ctx, 16U, len_block ); |
| 293 | (void) mbedtls_poly1305_finish( &ctx->poly1305_ctx, mac ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 294 | |
| 295 | return( 0 ); |
| 296 | } |
| 297 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 298 | int mbedtls_chachapoly_crypt_and_mac ( const unsigned char key[32], |
| 299 | const unsigned char nonce[12], |
| 300 | mbedtls_chachapoly_mode_t mode, |
| 301 | size_t aad_len, |
| 302 | const unsigned char *aad, |
| 303 | size_t ilen, |
| 304 | const unsigned char *input, |
| 305 | unsigned char *output, |
| 306 | unsigned char mac[16] ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 307 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 308 | mbedtls_chachapoly_context ctx; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 309 | int result; |
| 310 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 311 | mbedtls_chachapoly_init( &ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 312 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 313 | result = mbedtls_chachapoly_setkey( &ctx, key ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 314 | if ( result != 0 ) |
| 315 | goto cleanup; |
| 316 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 317 | result = mbedtls_chachapoly_starts( &ctx, nonce, mode ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 318 | if ( result != 0 ) |
| 319 | goto cleanup; |
| 320 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 321 | result = mbedtls_chachapoly_update_aad( &ctx, aad_len, aad ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 322 | if ( result != 0 ) |
| 323 | goto cleanup; |
| 324 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 325 | result = mbedtls_chachapoly_update( &ctx, ilen, input, output ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 326 | if ( result != 0 ) |
| 327 | goto cleanup; |
| 328 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 329 | result = mbedtls_chachapoly_finish( &ctx, mac ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 330 | |
| 331 | cleanup: |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 332 | mbedtls_chachapoly_free( &ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 333 | return( result ); |
| 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 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 426 | int mbedtls_chachapoly_self_test( int verbose ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 427 | { |
Manuel Pégourié-Gonnard | b7e9900 | 2018-05-07 10:14:18 +0200 | [diff] [blame] | 428 | unsigned i; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 429 | int result; |
| 430 | unsigned char output[200]; |
| 431 | unsigned char mac[16]; |
| 432 | |
| 433 | for ( i = 0U; i < 1U; i++ ) |
| 434 | { |
Daniel King | dedf4a3 | 2016-05-18 10:07:53 -0300 | [diff] [blame] | 435 | if ( verbose != 0 ) |
| 436 | { |
Manuel Pégourié-Gonnard | b7e9900 | 2018-05-07 10:14:18 +0200 | [diff] [blame] | 437 | mbedtls_printf( " ChaCha20-Poly1305 test %u ", i ); |
Daniel King | dedf4a3 | 2016-05-18 10:07:53 -0300 | [diff] [blame] | 438 | } |
| 439 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 440 | result = mbedtls_chachapoly_crypt_and_mac( test_key[i], |
| 441 | test_nonce[i], |
| 442 | MBEDTLS_CHACHAPOLY_ENCRYPT, |
| 443 | test_aad_len[i], |
| 444 | test_aad[i], |
| 445 | test_input_len[i], |
| 446 | test_input[i], |
| 447 | output, |
| 448 | mac ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 449 | if ( result != 0 ) |
| 450 | { |
| 451 | if ( verbose != 0 ) |
| 452 | { |
Daniel King | dedf4a3 | 2016-05-18 10:07:53 -0300 | [diff] [blame] | 453 | mbedtls_printf( "error code: %i\n", result ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 454 | } |
| 455 | return( -1 ); |
| 456 | } |
| 457 | |
| 458 | if ( memcmp( output, test_output[i], test_input_len[i] ) != 0 ) |
| 459 | { |
| 460 | if ( verbose != 0 ) |
| 461 | { |
Daniel King | dedf4a3 | 2016-05-18 10:07:53 -0300 | [diff] [blame] | 462 | mbedtls_printf( "failure (wrong output)\n" ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 463 | } |
| 464 | return( -1 ); |
| 465 | } |
| 466 | |
| 467 | if ( memcmp( mac, test_mac[i], 16U ) != 0 ) |
| 468 | { |
| 469 | if ( verbose != 0 ) |
| 470 | { |
Daniel King | dedf4a3 | 2016-05-18 10:07:53 -0300 | [diff] [blame] | 471 | mbedtls_printf( "failure (wrong MAC)\n" ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 472 | } |
| 473 | return( -1 ); |
| 474 | } |
Daniel King | dedf4a3 | 2016-05-18 10:07:53 -0300 | [diff] [blame] | 475 | |
| 476 | if ( verbose != 0 ) |
| 477 | { |
| 478 | mbedtls_printf( "passed\n" ); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if( verbose != 0 ) |
| 483 | { |
| 484 | mbedtls_printf( "\n" ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | return( 0 ); |
| 488 | } |
| 489 | |
| 490 | #endif /* MBEDTLS_SELF_TEST */ |
| 491 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 492 | #endif /* MBEDTLS_CHACHAPOLY_C */ |