mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

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