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 chacha20.c
kenjiArai 0:5b88d5760320 3 *
kenjiArai 0:5b88d5760320 4 * \brief ChaCha20 cipher.
kenjiArai 0:5b88d5760320 5 *
kenjiArai 0:5b88d5760320 6 * \author Daniel King <damaki.gh@gmail.com>
kenjiArai 0:5b88d5760320 7 *
kenjiArai 0:5b88d5760320 8 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
kenjiArai 0:5b88d5760320 9 * SPDX-License-Identifier: Apache-2.0
kenjiArai 0:5b88d5760320 10 *
kenjiArai 0:5b88d5760320 11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
kenjiArai 0:5b88d5760320 12 * not use this file except in compliance with the License.
kenjiArai 0:5b88d5760320 13 * You may obtain a copy of the License at
kenjiArai 0:5b88d5760320 14 *
kenjiArai 0:5b88d5760320 15 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 0:5b88d5760320 16 *
kenjiArai 0:5b88d5760320 17 * Unless required by applicable law or agreed to in writing, software
kenjiArai 0:5b88d5760320 18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
kenjiArai 0:5b88d5760320 19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 0:5b88d5760320 20 * See the License for the specific language governing permissions and
kenjiArai 0:5b88d5760320 21 * limitations under the License.
kenjiArai 0:5b88d5760320 22 *
kenjiArai 0:5b88d5760320 23 * This file is part of mbed TLS (https://tls.mbed.org)
kenjiArai 0:5b88d5760320 24 */
kenjiArai 0:5b88d5760320 25
kenjiArai 0:5b88d5760320 26 #if !defined(MBEDTLS_CONFIG_FILE)
kenjiArai 0:5b88d5760320 27 #include "mbedtls/config.h"
kenjiArai 0:5b88d5760320 28 #else
kenjiArai 0:5b88d5760320 29 #include MBEDTLS_CONFIG_FILE
kenjiArai 0:5b88d5760320 30 #endif
kenjiArai 0:5b88d5760320 31
kenjiArai 0:5b88d5760320 32 #if defined(MBEDTLS_CHACHA20_C)
kenjiArai 0:5b88d5760320 33
kenjiArai 0:5b88d5760320 34 #include "mbedtls/chacha20.h"
kenjiArai 0:5b88d5760320 35 #include "mbedtls/platform_util.h"
kenjiArai 0:5b88d5760320 36
kenjiArai 0:5b88d5760320 37 #include <stddef.h>
kenjiArai 0:5b88d5760320 38 #include <string.h>
kenjiArai 0:5b88d5760320 39
kenjiArai 0:5b88d5760320 40 #if defined(MBEDTLS_SELF_TEST)
kenjiArai 0:5b88d5760320 41 #if defined(MBEDTLS_PLATFORM_C)
kenjiArai 0:5b88d5760320 42 #include "mbedtls/platform.h"
kenjiArai 0:5b88d5760320 43 #else
kenjiArai 0:5b88d5760320 44 #include <stdio.h>
kenjiArai 0:5b88d5760320 45 #define mbedtls_printf printf
kenjiArai 0:5b88d5760320 46 #endif /* MBEDTLS_PLATFORM_C */
kenjiArai 0:5b88d5760320 47 #endif /* MBEDTLS_SELF_TEST */
kenjiArai 0:5b88d5760320 48
kenjiArai 0:5b88d5760320 49 #if !defined(MBEDTLS_CHACHA20_ALT)
kenjiArai 0:5b88d5760320 50
kenjiArai 0:5b88d5760320 51 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
kenjiArai 0:5b88d5760320 52 !defined(inline) && !defined(__cplusplus)
kenjiArai 0:5b88d5760320 53 #define inline __inline
kenjiArai 0:5b88d5760320 54 #endif
kenjiArai 0:5b88d5760320 55
kenjiArai 0:5b88d5760320 56 /* Parameter validation macros */
kenjiArai 0:5b88d5760320 57 #define CHACHA20_VALIDATE_RET( cond ) \
kenjiArai 0:5b88d5760320 58 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA )
kenjiArai 0:5b88d5760320 59 #define CHACHA20_VALIDATE( cond ) \
kenjiArai 0:5b88d5760320 60 MBEDTLS_INTERNAL_VALIDATE( cond )
kenjiArai 0:5b88d5760320 61
kenjiArai 0:5b88d5760320 62 #define BYTES_TO_U32_LE( data, offset ) \
kenjiArai 0:5b88d5760320 63 ( (uint32_t) (data)[offset] \
kenjiArai 0:5b88d5760320 64 | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \
kenjiArai 0:5b88d5760320 65 | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \
kenjiArai 0:5b88d5760320 66 | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \
kenjiArai 0:5b88d5760320 67 )
kenjiArai 0:5b88d5760320 68
kenjiArai 0:5b88d5760320 69 #define ROTL32( value, amount ) \
kenjiArai 0:5b88d5760320 70 ( (uint32_t) ( (value) << (amount) ) | ( (value) >> ( 32 - (amount) ) ) )
kenjiArai 0:5b88d5760320 71
kenjiArai 0:5b88d5760320 72 #define CHACHA20_CTR_INDEX ( 12U )
kenjiArai 0:5b88d5760320 73
kenjiArai 0:5b88d5760320 74 #define CHACHA20_BLOCK_SIZE_BYTES ( 4U * 16U )
kenjiArai 0:5b88d5760320 75
kenjiArai 0:5b88d5760320 76 /**
kenjiArai 0:5b88d5760320 77 * \brief ChaCha20 quarter round operation.
kenjiArai 0:5b88d5760320 78 *
kenjiArai 0:5b88d5760320 79 * The quarter round is defined as follows (from RFC 7539):
kenjiArai 0:5b88d5760320 80 * 1. a += b; d ^= a; d <<<= 16;
kenjiArai 0:5b88d5760320 81 * 2. c += d; b ^= c; b <<<= 12;
kenjiArai 0:5b88d5760320 82 * 3. a += b; d ^= a; d <<<= 8;
kenjiArai 0:5b88d5760320 83 * 4. c += d; b ^= c; b <<<= 7;
kenjiArai 0:5b88d5760320 84 *
kenjiArai 0:5b88d5760320 85 * \param state ChaCha20 state to modify.
kenjiArai 0:5b88d5760320 86 * \param a The index of 'a' in the state.
kenjiArai 0:5b88d5760320 87 * \param b The index of 'b' in the state.
kenjiArai 0:5b88d5760320 88 * \param c The index of 'c' in the state.
kenjiArai 0:5b88d5760320 89 * \param d The index of 'd' in the state.
kenjiArai 0:5b88d5760320 90 */
kenjiArai 0:5b88d5760320 91 static inline void chacha20_quarter_round( uint32_t state[16],
kenjiArai 0:5b88d5760320 92 size_t a,
kenjiArai 0:5b88d5760320 93 size_t b,
kenjiArai 0:5b88d5760320 94 size_t c,
kenjiArai 0:5b88d5760320 95 size_t d )
kenjiArai 0:5b88d5760320 96 {
kenjiArai 0:5b88d5760320 97 /* a += b; d ^= a; d <<<= 16; */
kenjiArai 0:5b88d5760320 98 state[a] += state[b];
kenjiArai 0:5b88d5760320 99 state[d] ^= state[a];
kenjiArai 0:5b88d5760320 100 state[d] = ROTL32( state[d], 16 );
kenjiArai 0:5b88d5760320 101
kenjiArai 0:5b88d5760320 102 /* c += d; b ^= c; b <<<= 12 */
kenjiArai 0:5b88d5760320 103 state[c] += state[d];
kenjiArai 0:5b88d5760320 104 state[b] ^= state[c];
kenjiArai 0:5b88d5760320 105 state[b] = ROTL32( state[b], 12 );
kenjiArai 0:5b88d5760320 106
kenjiArai 0:5b88d5760320 107 /* a += b; d ^= a; d <<<= 8; */
kenjiArai 0:5b88d5760320 108 state[a] += state[b];
kenjiArai 0:5b88d5760320 109 state[d] ^= state[a];
kenjiArai 0:5b88d5760320 110 state[d] = ROTL32( state[d], 8 );
kenjiArai 0:5b88d5760320 111
kenjiArai 0:5b88d5760320 112 /* c += d; b ^= c; b <<<= 7; */
kenjiArai 0:5b88d5760320 113 state[c] += state[d];
kenjiArai 0:5b88d5760320 114 state[b] ^= state[c];
kenjiArai 0:5b88d5760320 115 state[b] = ROTL32( state[b], 7 );
kenjiArai 0:5b88d5760320 116 }
kenjiArai 0:5b88d5760320 117
kenjiArai 0:5b88d5760320 118 /**
kenjiArai 0:5b88d5760320 119 * \brief Perform the ChaCha20 inner block operation.
kenjiArai 0:5b88d5760320 120 *
kenjiArai 0:5b88d5760320 121 * This function performs two rounds: the column round and the
kenjiArai 0:5b88d5760320 122 * diagonal round.
kenjiArai 0:5b88d5760320 123 *
kenjiArai 0:5b88d5760320 124 * \param state The ChaCha20 state to update.
kenjiArai 0:5b88d5760320 125 */
kenjiArai 0:5b88d5760320 126 static void chacha20_inner_block( uint32_t state[16] )
kenjiArai 0:5b88d5760320 127 {
kenjiArai 0:5b88d5760320 128 chacha20_quarter_round( state, 0, 4, 8, 12 );
kenjiArai 0:5b88d5760320 129 chacha20_quarter_round( state, 1, 5, 9, 13 );
kenjiArai 0:5b88d5760320 130 chacha20_quarter_round( state, 2, 6, 10, 14 );
kenjiArai 0:5b88d5760320 131 chacha20_quarter_round( state, 3, 7, 11, 15 );
kenjiArai 0:5b88d5760320 132
kenjiArai 0:5b88d5760320 133 chacha20_quarter_round( state, 0, 5, 10, 15 );
kenjiArai 0:5b88d5760320 134 chacha20_quarter_round( state, 1, 6, 11, 12 );
kenjiArai 0:5b88d5760320 135 chacha20_quarter_round( state, 2, 7, 8, 13 );
kenjiArai 0:5b88d5760320 136 chacha20_quarter_round( state, 3, 4, 9, 14 );
kenjiArai 0:5b88d5760320 137 }
kenjiArai 0:5b88d5760320 138
kenjiArai 0:5b88d5760320 139 /**
kenjiArai 0:5b88d5760320 140 * \brief Generates a keystream block.
kenjiArai 0:5b88d5760320 141 *
kenjiArai 0:5b88d5760320 142 * \param initial_state The initial ChaCha20 state (key, nonce, counter).
kenjiArai 0:5b88d5760320 143 * \param keystream Generated keystream bytes are written to this buffer.
kenjiArai 0:5b88d5760320 144 */
kenjiArai 0:5b88d5760320 145 static void chacha20_block( const uint32_t initial_state[16],
kenjiArai 0:5b88d5760320 146 unsigned char keystream[64] )
kenjiArai 0:5b88d5760320 147 {
kenjiArai 0:5b88d5760320 148 uint32_t working_state[16];
kenjiArai 0:5b88d5760320 149 size_t i;
kenjiArai 0:5b88d5760320 150
kenjiArai 0:5b88d5760320 151 memcpy( working_state,
kenjiArai 0:5b88d5760320 152 initial_state,
kenjiArai 0:5b88d5760320 153 CHACHA20_BLOCK_SIZE_BYTES );
kenjiArai 0:5b88d5760320 154
kenjiArai 0:5b88d5760320 155 for( i = 0U; i < 10U; i++ )
kenjiArai 0:5b88d5760320 156 chacha20_inner_block( working_state );
kenjiArai 0:5b88d5760320 157
kenjiArai 0:5b88d5760320 158 working_state[ 0] += initial_state[ 0];
kenjiArai 0:5b88d5760320 159 working_state[ 1] += initial_state[ 1];
kenjiArai 0:5b88d5760320 160 working_state[ 2] += initial_state[ 2];
kenjiArai 0:5b88d5760320 161 working_state[ 3] += initial_state[ 3];
kenjiArai 0:5b88d5760320 162 working_state[ 4] += initial_state[ 4];
kenjiArai 0:5b88d5760320 163 working_state[ 5] += initial_state[ 5];
kenjiArai 0:5b88d5760320 164 working_state[ 6] += initial_state[ 6];
kenjiArai 0:5b88d5760320 165 working_state[ 7] += initial_state[ 7];
kenjiArai 0:5b88d5760320 166 working_state[ 8] += initial_state[ 8];
kenjiArai 0:5b88d5760320 167 working_state[ 9] += initial_state[ 9];
kenjiArai 0:5b88d5760320 168 working_state[10] += initial_state[10];
kenjiArai 0:5b88d5760320 169 working_state[11] += initial_state[11];
kenjiArai 0:5b88d5760320 170 working_state[12] += initial_state[12];
kenjiArai 0:5b88d5760320 171 working_state[13] += initial_state[13];
kenjiArai 0:5b88d5760320 172 working_state[14] += initial_state[14];
kenjiArai 0:5b88d5760320 173 working_state[15] += initial_state[15];
kenjiArai 0:5b88d5760320 174
kenjiArai 0:5b88d5760320 175 for( i = 0U; i < 16; i++ )
kenjiArai 0:5b88d5760320 176 {
kenjiArai 0:5b88d5760320 177 size_t offset = i * 4U;
kenjiArai 0:5b88d5760320 178
kenjiArai 0:5b88d5760320 179 keystream[offset ] = (unsigned char)( working_state[i] );
kenjiArai 0:5b88d5760320 180 keystream[offset + 1U] = (unsigned char)( working_state[i] >> 8 );
kenjiArai 0:5b88d5760320 181 keystream[offset + 2U] = (unsigned char)( working_state[i] >> 16 );
kenjiArai 0:5b88d5760320 182 keystream[offset + 3U] = (unsigned char)( working_state[i] >> 24 );
kenjiArai 0:5b88d5760320 183 }
kenjiArai 0:5b88d5760320 184
kenjiArai 0:5b88d5760320 185 mbedtls_platform_zeroize( working_state, sizeof( working_state ) );
kenjiArai 0:5b88d5760320 186 }
kenjiArai 0:5b88d5760320 187
kenjiArai 0:5b88d5760320 188 void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx )
kenjiArai 0:5b88d5760320 189 {
kenjiArai 0:5b88d5760320 190 CHACHA20_VALIDATE( ctx != NULL );
kenjiArai 0:5b88d5760320 191
kenjiArai 0:5b88d5760320 192 mbedtls_platform_zeroize( ctx->state, sizeof( ctx->state ) );
kenjiArai 0:5b88d5760320 193 mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) );
kenjiArai 0:5b88d5760320 194
kenjiArai 0:5b88d5760320 195 /* Initially, there's no keystream bytes available */
kenjiArai 0:5b88d5760320 196 ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES;
kenjiArai 0:5b88d5760320 197 }
kenjiArai 0:5b88d5760320 198
kenjiArai 0:5b88d5760320 199 void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx )
kenjiArai 0:5b88d5760320 200 {
kenjiArai 0:5b88d5760320 201 if( ctx != NULL )
kenjiArai 0:5b88d5760320 202 {
kenjiArai 0:5b88d5760320 203 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_chacha20_context ) );
kenjiArai 0:5b88d5760320 204 }
kenjiArai 0:5b88d5760320 205 }
kenjiArai 0:5b88d5760320 206
kenjiArai 0:5b88d5760320 207 int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
kenjiArai 0:5b88d5760320 208 const unsigned char key[32] )
kenjiArai 0:5b88d5760320 209 {
kenjiArai 0:5b88d5760320 210 CHACHA20_VALIDATE_RET( ctx != NULL );
kenjiArai 0:5b88d5760320 211 CHACHA20_VALIDATE_RET( key != NULL );
kenjiArai 0:5b88d5760320 212
kenjiArai 0:5b88d5760320 213 /* ChaCha20 constants - the string "expand 32-byte k" */
kenjiArai 0:5b88d5760320 214 ctx->state[0] = 0x61707865;
kenjiArai 0:5b88d5760320 215 ctx->state[1] = 0x3320646e;
kenjiArai 0:5b88d5760320 216 ctx->state[2] = 0x79622d32;
kenjiArai 0:5b88d5760320 217 ctx->state[3] = 0x6b206574;
kenjiArai 0:5b88d5760320 218
kenjiArai 0:5b88d5760320 219 /* Set key */
kenjiArai 0:5b88d5760320 220 ctx->state[4] = BYTES_TO_U32_LE( key, 0 );
kenjiArai 0:5b88d5760320 221 ctx->state[5] = BYTES_TO_U32_LE( key, 4 );
kenjiArai 0:5b88d5760320 222 ctx->state[6] = BYTES_TO_U32_LE( key, 8 );
kenjiArai 0:5b88d5760320 223 ctx->state[7] = BYTES_TO_U32_LE( key, 12 );
kenjiArai 0:5b88d5760320 224 ctx->state[8] = BYTES_TO_U32_LE( key, 16 );
kenjiArai 0:5b88d5760320 225 ctx->state[9] = BYTES_TO_U32_LE( key, 20 );
kenjiArai 0:5b88d5760320 226 ctx->state[10] = BYTES_TO_U32_LE( key, 24 );
kenjiArai 0:5b88d5760320 227 ctx->state[11] = BYTES_TO_U32_LE( key, 28 );
kenjiArai 0:5b88d5760320 228
kenjiArai 0:5b88d5760320 229 return( 0 );
kenjiArai 0:5b88d5760320 230 }
kenjiArai 0:5b88d5760320 231
kenjiArai 0:5b88d5760320 232 int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
kenjiArai 0:5b88d5760320 233 const unsigned char nonce[12],
kenjiArai 0:5b88d5760320 234 uint32_t counter )
kenjiArai 0:5b88d5760320 235 {
kenjiArai 0:5b88d5760320 236 CHACHA20_VALIDATE_RET( ctx != NULL );
kenjiArai 0:5b88d5760320 237 CHACHA20_VALIDATE_RET( nonce != NULL );
kenjiArai 0:5b88d5760320 238
kenjiArai 0:5b88d5760320 239 /* Counter */
kenjiArai 0:5b88d5760320 240 ctx->state[12] = counter;
kenjiArai 0:5b88d5760320 241
kenjiArai 0:5b88d5760320 242 /* Nonce */
kenjiArai 0:5b88d5760320 243 ctx->state[13] = BYTES_TO_U32_LE( nonce, 0 );
kenjiArai 0:5b88d5760320 244 ctx->state[14] = BYTES_TO_U32_LE( nonce, 4 );
kenjiArai 0:5b88d5760320 245 ctx->state[15] = BYTES_TO_U32_LE( nonce, 8 );
kenjiArai 0:5b88d5760320 246
kenjiArai 0:5b88d5760320 247 mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) );
kenjiArai 0:5b88d5760320 248
kenjiArai 0:5b88d5760320 249 /* Initially, there's no keystream bytes available */
kenjiArai 0:5b88d5760320 250 ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES;
kenjiArai 0:5b88d5760320 251
kenjiArai 0:5b88d5760320 252 return( 0 );
kenjiArai 0:5b88d5760320 253 }
kenjiArai 0:5b88d5760320 254
kenjiArai 0:5b88d5760320 255 int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
kenjiArai 0:5b88d5760320 256 size_t size,
kenjiArai 0:5b88d5760320 257 const unsigned char *input,
kenjiArai 0:5b88d5760320 258 unsigned char *output )
kenjiArai 0:5b88d5760320 259 {
kenjiArai 0:5b88d5760320 260 size_t offset = 0U;
kenjiArai 0:5b88d5760320 261 size_t i;
kenjiArai 0:5b88d5760320 262
kenjiArai 0:5b88d5760320 263 CHACHA20_VALIDATE_RET( ctx != NULL );
kenjiArai 0:5b88d5760320 264 CHACHA20_VALIDATE_RET( size == 0 || input != NULL );
kenjiArai 0:5b88d5760320 265 CHACHA20_VALIDATE_RET( size == 0 || output != NULL );
kenjiArai 0:5b88d5760320 266
kenjiArai 0:5b88d5760320 267 /* Use leftover keystream bytes, if available */
kenjiArai 0:5b88d5760320 268 while( size > 0U && ctx->keystream_bytes_used < CHACHA20_BLOCK_SIZE_BYTES )
kenjiArai 0:5b88d5760320 269 {
kenjiArai 0:5b88d5760320 270 output[offset] = input[offset]
kenjiArai 0:5b88d5760320 271 ^ ctx->keystream8[ctx->keystream_bytes_used];
kenjiArai 0:5b88d5760320 272
kenjiArai 0:5b88d5760320 273 ctx->keystream_bytes_used++;
kenjiArai 0:5b88d5760320 274 offset++;
kenjiArai 0:5b88d5760320 275 size--;
kenjiArai 0:5b88d5760320 276 }
kenjiArai 0:5b88d5760320 277
kenjiArai 0:5b88d5760320 278 /* Process full blocks */
kenjiArai 0:5b88d5760320 279 while( size >= CHACHA20_BLOCK_SIZE_BYTES )
kenjiArai 0:5b88d5760320 280 {
kenjiArai 0:5b88d5760320 281 /* Generate new keystream block and increment counter */
kenjiArai 0:5b88d5760320 282 chacha20_block( ctx->state, ctx->keystream8 );
kenjiArai 0:5b88d5760320 283 ctx->state[CHACHA20_CTR_INDEX]++;
kenjiArai 0:5b88d5760320 284
kenjiArai 0:5b88d5760320 285 for( i = 0U; i < 64U; i += 8U )
kenjiArai 0:5b88d5760320 286 {
kenjiArai 0:5b88d5760320 287 output[offset + i ] = input[offset + i ] ^ ctx->keystream8[i ];
kenjiArai 0:5b88d5760320 288 output[offset + i+1] = input[offset + i+1] ^ ctx->keystream8[i+1];
kenjiArai 0:5b88d5760320 289 output[offset + i+2] = input[offset + i+2] ^ ctx->keystream8[i+2];
kenjiArai 0:5b88d5760320 290 output[offset + i+3] = input[offset + i+3] ^ ctx->keystream8[i+3];
kenjiArai 0:5b88d5760320 291 output[offset + i+4] = input[offset + i+4] ^ ctx->keystream8[i+4];
kenjiArai 0:5b88d5760320 292 output[offset + i+5] = input[offset + i+5] ^ ctx->keystream8[i+5];
kenjiArai 0:5b88d5760320 293 output[offset + i+6] = input[offset + i+6] ^ ctx->keystream8[i+6];
kenjiArai 0:5b88d5760320 294 output[offset + i+7] = input[offset + i+7] ^ ctx->keystream8[i+7];
kenjiArai 0:5b88d5760320 295 }
kenjiArai 0:5b88d5760320 296
kenjiArai 0:5b88d5760320 297 offset += CHACHA20_BLOCK_SIZE_BYTES;
kenjiArai 0:5b88d5760320 298 size -= CHACHA20_BLOCK_SIZE_BYTES;
kenjiArai 0:5b88d5760320 299 }
kenjiArai 0:5b88d5760320 300
kenjiArai 0:5b88d5760320 301 /* Last (partial) block */
kenjiArai 0:5b88d5760320 302 if( size > 0U )
kenjiArai 0:5b88d5760320 303 {
kenjiArai 0:5b88d5760320 304 /* Generate new keystream block and increment counter */
kenjiArai 0:5b88d5760320 305 chacha20_block( ctx->state, ctx->keystream8 );
kenjiArai 0:5b88d5760320 306 ctx->state[CHACHA20_CTR_INDEX]++;
kenjiArai 0:5b88d5760320 307
kenjiArai 0:5b88d5760320 308 for( i = 0U; i < size; i++)
kenjiArai 0:5b88d5760320 309 {
kenjiArai 0:5b88d5760320 310 output[offset + i] = input[offset + i] ^ ctx->keystream8[i];
kenjiArai 0:5b88d5760320 311 }
kenjiArai 0:5b88d5760320 312
kenjiArai 0:5b88d5760320 313 ctx->keystream_bytes_used = size;
kenjiArai 0:5b88d5760320 314
kenjiArai 0:5b88d5760320 315 }
kenjiArai 0:5b88d5760320 316
kenjiArai 0:5b88d5760320 317 return( 0 );
kenjiArai 0:5b88d5760320 318 }
kenjiArai 0:5b88d5760320 319
kenjiArai 0:5b88d5760320 320 int mbedtls_chacha20_crypt( const unsigned char key[32],
kenjiArai 0:5b88d5760320 321 const unsigned char nonce[12],
kenjiArai 0:5b88d5760320 322 uint32_t counter,
kenjiArai 0:5b88d5760320 323 size_t data_len,
kenjiArai 0:5b88d5760320 324 const unsigned char* input,
kenjiArai 0:5b88d5760320 325 unsigned char* output )
kenjiArai 0:5b88d5760320 326 {
kenjiArai 0:5b88d5760320 327 mbedtls_chacha20_context ctx;
kenjiArai 0:5b88d5760320 328 int ret;
kenjiArai 0:5b88d5760320 329
kenjiArai 0:5b88d5760320 330 CHACHA20_VALIDATE_RET( key != NULL );
kenjiArai 0:5b88d5760320 331 CHACHA20_VALIDATE_RET( nonce != NULL );
kenjiArai 0:5b88d5760320 332 CHACHA20_VALIDATE_RET( data_len == 0 || input != NULL );
kenjiArai 0:5b88d5760320 333 CHACHA20_VALIDATE_RET( data_len == 0 || output != NULL );
kenjiArai 0:5b88d5760320 334
kenjiArai 0:5b88d5760320 335 mbedtls_chacha20_init( &ctx );
kenjiArai 0:5b88d5760320 336
kenjiArai 0:5b88d5760320 337 ret = mbedtls_chacha20_setkey( &ctx, key );
kenjiArai 0:5b88d5760320 338 if( ret != 0 )
kenjiArai 0:5b88d5760320 339 goto cleanup;
kenjiArai 0:5b88d5760320 340
kenjiArai 0:5b88d5760320 341 ret = mbedtls_chacha20_starts( &ctx, nonce, counter );
kenjiArai 0:5b88d5760320 342 if( ret != 0 )
kenjiArai 0:5b88d5760320 343 goto cleanup;
kenjiArai 0:5b88d5760320 344
kenjiArai 0:5b88d5760320 345 ret = mbedtls_chacha20_update( &ctx, data_len, input, output );
kenjiArai 0:5b88d5760320 346
kenjiArai 0:5b88d5760320 347 cleanup:
kenjiArai 0:5b88d5760320 348 mbedtls_chacha20_free( &ctx );
kenjiArai 0:5b88d5760320 349 return( ret );
kenjiArai 0:5b88d5760320 350 }
kenjiArai 0:5b88d5760320 351
kenjiArai 0:5b88d5760320 352 #endif /* !MBEDTLS_CHACHA20_ALT */
kenjiArai 0:5b88d5760320 353
kenjiArai 0:5b88d5760320 354 #if defined(MBEDTLS_SELF_TEST)
kenjiArai 0:5b88d5760320 355
kenjiArai 0:5b88d5760320 356 static const unsigned char test_keys[2][32] =
kenjiArai 0:5b88d5760320 357 {
kenjiArai 0:5b88d5760320 358 {
kenjiArai 0:5b88d5760320 359 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 360 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 361 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 362 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
kenjiArai 0:5b88d5760320 363 },
kenjiArai 0:5b88d5760320 364 {
kenjiArai 0:5b88d5760320 365 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 366 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 367 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 368 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
kenjiArai 0:5b88d5760320 369 }
kenjiArai 0:5b88d5760320 370 };
kenjiArai 0:5b88d5760320 371
kenjiArai 0:5b88d5760320 372 static const unsigned char test_nonces[2][12] =
kenjiArai 0:5b88d5760320 373 {
kenjiArai 0:5b88d5760320 374 {
kenjiArai 0:5b88d5760320 375 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 376 0x00, 0x00, 0x00, 0x00
kenjiArai 0:5b88d5760320 377 },
kenjiArai 0:5b88d5760320 378 {
kenjiArai 0:5b88d5760320 379 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 380 0x00, 0x00, 0x00, 0x02
kenjiArai 0:5b88d5760320 381 }
kenjiArai 0:5b88d5760320 382 };
kenjiArai 0:5b88d5760320 383
kenjiArai 0:5b88d5760320 384 static const uint32_t test_counters[2] =
kenjiArai 0:5b88d5760320 385 {
kenjiArai 0:5b88d5760320 386 0U,
kenjiArai 0:5b88d5760320 387 1U
kenjiArai 0:5b88d5760320 388 };
kenjiArai 0:5b88d5760320 389
kenjiArai 0:5b88d5760320 390 static const unsigned char test_input[2][375] =
kenjiArai 0:5b88d5760320 391 {
kenjiArai 0:5b88d5760320 392 {
kenjiArai 0:5b88d5760320 393 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 394 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 395 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 396 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 397 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 398 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 399 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
kenjiArai 0:5b88d5760320 400 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
kenjiArai 0:5b88d5760320 401 },
kenjiArai 0:5b88d5760320 402 {
kenjiArai 0:5b88d5760320 403 0x41, 0x6e, 0x79, 0x20, 0x73, 0x75, 0x62, 0x6d,
kenjiArai 0:5b88d5760320 404 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x74,
kenjiArai 0:5b88d5760320 405 0x6f, 0x20, 0x74, 0x68, 0x65, 0x20, 0x49, 0x45,
kenjiArai 0:5b88d5760320 406 0x54, 0x46, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x6e,
kenjiArai 0:5b88d5760320 407 0x64, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x74,
kenjiArai 0:5b88d5760320 408 0x68, 0x65, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x72,
kenjiArai 0:5b88d5760320 409 0x69, 0x62, 0x75, 0x74, 0x6f, 0x72, 0x20, 0x66,
kenjiArai 0:5b88d5760320 410 0x6f, 0x72, 0x20, 0x70, 0x75, 0x62, 0x6c, 0x69,
kenjiArai 0:5b88d5760320 411 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x61,
kenjiArai 0:5b88d5760320 412 0x73, 0x20, 0x61, 0x6c, 0x6c, 0x20, 0x6f, 0x72,
kenjiArai 0:5b88d5760320 413 0x20, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6f, 0x66,
kenjiArai 0:5b88d5760320 414 0x20, 0x61, 0x6e, 0x20, 0x49, 0x45, 0x54, 0x46,
kenjiArai 0:5b88d5760320 415 0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
kenjiArai 0:5b88d5760320 416 0x74, 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x20,
kenjiArai 0:5b88d5760320 417 0x6f, 0x72, 0x20, 0x52, 0x46, 0x43, 0x20, 0x61,
kenjiArai 0:5b88d5760320 418 0x6e, 0x64, 0x20, 0x61, 0x6e, 0x79, 0x20, 0x73,
kenjiArai 0:5b88d5760320 419 0x74, 0x61, 0x74, 0x65, 0x6d, 0x65, 0x6e, 0x74,
kenjiArai 0:5b88d5760320 420 0x20, 0x6d, 0x61, 0x64, 0x65, 0x20, 0x77, 0x69,
kenjiArai 0:5b88d5760320 421 0x74, 0x68, 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65,
kenjiArai 0:5b88d5760320 422 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74,
kenjiArai 0:5b88d5760320 423 0x20, 0x6f, 0x66, 0x20, 0x61, 0x6e, 0x20, 0x49,
kenjiArai 0:5b88d5760320 424 0x45, 0x54, 0x46, 0x20, 0x61, 0x63, 0x74, 0x69,
kenjiArai 0:5b88d5760320 425 0x76, 0x69, 0x74, 0x79, 0x20, 0x69, 0x73, 0x20,
kenjiArai 0:5b88d5760320 426 0x63, 0x6f, 0x6e, 0x73, 0x69, 0x64, 0x65, 0x72,
kenjiArai 0:5b88d5760320 427 0x65, 0x64, 0x20, 0x61, 0x6e, 0x20, 0x22, 0x49,
kenjiArai 0:5b88d5760320 428 0x45, 0x54, 0x46, 0x20, 0x43, 0x6f, 0x6e, 0x74,
kenjiArai 0:5b88d5760320 429 0x72, 0x69, 0x62, 0x75, 0x74, 0x69, 0x6f, 0x6e,
kenjiArai 0:5b88d5760320 430 0x22, 0x2e, 0x20, 0x53, 0x75, 0x63, 0x68, 0x20,
kenjiArai 0:5b88d5760320 431 0x73, 0x74, 0x61, 0x74, 0x65, 0x6d, 0x65, 0x6e,
kenjiArai 0:5b88d5760320 432 0x74, 0x73, 0x20, 0x69, 0x6e, 0x63, 0x6c, 0x75,
kenjiArai 0:5b88d5760320 433 0x64, 0x65, 0x20, 0x6f, 0x72, 0x61, 0x6c, 0x20,
kenjiArai 0:5b88d5760320 434 0x73, 0x74, 0x61, 0x74, 0x65, 0x6d, 0x65, 0x6e,
kenjiArai 0:5b88d5760320 435 0x74, 0x73, 0x20, 0x69, 0x6e, 0x20, 0x49, 0x45,
kenjiArai 0:5b88d5760320 436 0x54, 0x46, 0x20, 0x73, 0x65, 0x73, 0x73, 0x69,
kenjiArai 0:5b88d5760320 437 0x6f, 0x6e, 0x73, 0x2c, 0x20, 0x61, 0x73, 0x20,
kenjiArai 0:5b88d5760320 438 0x77, 0x65, 0x6c, 0x6c, 0x20, 0x61, 0x73, 0x20,
kenjiArai 0:5b88d5760320 439 0x77, 0x72, 0x69, 0x74, 0x74, 0x65, 0x6e, 0x20,
kenjiArai 0:5b88d5760320 440 0x61, 0x6e, 0x64, 0x20, 0x65, 0x6c, 0x65, 0x63,
kenjiArai 0:5b88d5760320 441 0x74, 0x72, 0x6f, 0x6e, 0x69, 0x63, 0x20, 0x63,
kenjiArai 0:5b88d5760320 442 0x6f, 0x6d, 0x6d, 0x75, 0x6e, 0x69, 0x63, 0x61,
kenjiArai 0:5b88d5760320 443 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x6d, 0x61,
kenjiArai 0:5b88d5760320 444 0x64, 0x65, 0x20, 0x61, 0x74, 0x20, 0x61, 0x6e,
kenjiArai 0:5b88d5760320 445 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20, 0x6f,
kenjiArai 0:5b88d5760320 446 0x72, 0x20, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x2c,
kenjiArai 0:5b88d5760320 447 0x20, 0x77, 0x68, 0x69, 0x63, 0x68, 0x20, 0x61,
kenjiArai 0:5b88d5760320 448 0x72, 0x65, 0x20, 0x61, 0x64, 0x64, 0x72, 0x65,
kenjiArai 0:5b88d5760320 449 0x73, 0x73, 0x65, 0x64, 0x20, 0x74, 0x6f
kenjiArai 0:5b88d5760320 450 }
kenjiArai 0:5b88d5760320 451 };
kenjiArai 0:5b88d5760320 452
kenjiArai 0:5b88d5760320 453 static const unsigned char test_output[2][375] =
kenjiArai 0:5b88d5760320 454 {
kenjiArai 0:5b88d5760320 455 {
kenjiArai 0:5b88d5760320 456 0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90,
kenjiArai 0:5b88d5760320 457 0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
kenjiArai 0:5b88d5760320 458 0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a,
kenjiArai 0:5b88d5760320 459 0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
kenjiArai 0:5b88d5760320 460 0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d,
kenjiArai 0:5b88d5760320 461 0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
kenjiArai 0:5b88d5760320 462 0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c,
kenjiArai 0:5b88d5760320 463 0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86
kenjiArai 0:5b88d5760320 464 },
kenjiArai 0:5b88d5760320 465 {
kenjiArai 0:5b88d5760320 466 0xa3, 0xfb, 0xf0, 0x7d, 0xf3, 0xfa, 0x2f, 0xde,
kenjiArai 0:5b88d5760320 467 0x4f, 0x37, 0x6c, 0xa2, 0x3e, 0x82, 0x73, 0x70,
kenjiArai 0:5b88d5760320 468 0x41, 0x60, 0x5d, 0x9f, 0x4f, 0x4f, 0x57, 0xbd,
kenjiArai 0:5b88d5760320 469 0x8c, 0xff, 0x2c, 0x1d, 0x4b, 0x79, 0x55, 0xec,
kenjiArai 0:5b88d5760320 470 0x2a, 0x97, 0x94, 0x8b, 0xd3, 0x72, 0x29, 0x15,
kenjiArai 0:5b88d5760320 471 0xc8, 0xf3, 0xd3, 0x37, 0xf7, 0xd3, 0x70, 0x05,
kenjiArai 0:5b88d5760320 472 0x0e, 0x9e, 0x96, 0xd6, 0x47, 0xb7, 0xc3, 0x9f,
kenjiArai 0:5b88d5760320 473 0x56, 0xe0, 0x31, 0xca, 0x5e, 0xb6, 0x25, 0x0d,
kenjiArai 0:5b88d5760320 474 0x40, 0x42, 0xe0, 0x27, 0x85, 0xec, 0xec, 0xfa,
kenjiArai 0:5b88d5760320 475 0x4b, 0x4b, 0xb5, 0xe8, 0xea, 0xd0, 0x44, 0x0e,
kenjiArai 0:5b88d5760320 476 0x20, 0xb6, 0xe8, 0xdb, 0x09, 0xd8, 0x81, 0xa7,
kenjiArai 0:5b88d5760320 477 0xc6, 0x13, 0x2f, 0x42, 0x0e, 0x52, 0x79, 0x50,
kenjiArai 0:5b88d5760320 478 0x42, 0xbd, 0xfa, 0x77, 0x73, 0xd8, 0xa9, 0x05,
kenjiArai 0:5b88d5760320 479 0x14, 0x47, 0xb3, 0x29, 0x1c, 0xe1, 0x41, 0x1c,
kenjiArai 0:5b88d5760320 480 0x68, 0x04, 0x65, 0x55, 0x2a, 0xa6, 0xc4, 0x05,
kenjiArai 0:5b88d5760320 481 0xb7, 0x76, 0x4d, 0x5e, 0x87, 0xbe, 0xa8, 0x5a,
kenjiArai 0:5b88d5760320 482 0xd0, 0x0f, 0x84, 0x49, 0xed, 0x8f, 0x72, 0xd0,
kenjiArai 0:5b88d5760320 483 0xd6, 0x62, 0xab, 0x05, 0x26, 0x91, 0xca, 0x66,
kenjiArai 0:5b88d5760320 484 0x42, 0x4b, 0xc8, 0x6d, 0x2d, 0xf8, 0x0e, 0xa4,
kenjiArai 0:5b88d5760320 485 0x1f, 0x43, 0xab, 0xf9, 0x37, 0xd3, 0x25, 0x9d,
kenjiArai 0:5b88d5760320 486 0xc4, 0xb2, 0xd0, 0xdf, 0xb4, 0x8a, 0x6c, 0x91,
kenjiArai 0:5b88d5760320 487 0x39, 0xdd, 0xd7, 0xf7, 0x69, 0x66, 0xe9, 0x28,
kenjiArai 0:5b88d5760320 488 0xe6, 0x35, 0x55, 0x3b, 0xa7, 0x6c, 0x5c, 0x87,
kenjiArai 0:5b88d5760320 489 0x9d, 0x7b, 0x35, 0xd4, 0x9e, 0xb2, 0xe6, 0x2b,
kenjiArai 0:5b88d5760320 490 0x08, 0x71, 0xcd, 0xac, 0x63, 0x89, 0x39, 0xe2,
kenjiArai 0:5b88d5760320 491 0x5e, 0x8a, 0x1e, 0x0e, 0xf9, 0xd5, 0x28, 0x0f,
kenjiArai 0:5b88d5760320 492 0xa8, 0xca, 0x32, 0x8b, 0x35, 0x1c, 0x3c, 0x76,
kenjiArai 0:5b88d5760320 493 0x59, 0x89, 0xcb, 0xcf, 0x3d, 0xaa, 0x8b, 0x6c,
kenjiArai 0:5b88d5760320 494 0xcc, 0x3a, 0xaf, 0x9f, 0x39, 0x79, 0xc9, 0x2b,
kenjiArai 0:5b88d5760320 495 0x37, 0x20, 0xfc, 0x88, 0xdc, 0x95, 0xed, 0x84,
kenjiArai 0:5b88d5760320 496 0xa1, 0xbe, 0x05, 0x9c, 0x64, 0x99, 0xb9, 0xfd,
kenjiArai 0:5b88d5760320 497 0xa2, 0x36, 0xe7, 0xe8, 0x18, 0xb0, 0x4b, 0x0b,
kenjiArai 0:5b88d5760320 498 0xc3, 0x9c, 0x1e, 0x87, 0x6b, 0x19, 0x3b, 0xfe,
kenjiArai 0:5b88d5760320 499 0x55, 0x69, 0x75, 0x3f, 0x88, 0x12, 0x8c, 0xc0,
kenjiArai 0:5b88d5760320 500 0x8a, 0xaa, 0x9b, 0x63, 0xd1, 0xa1, 0x6f, 0x80,
kenjiArai 0:5b88d5760320 501 0xef, 0x25, 0x54, 0xd7, 0x18, 0x9c, 0x41, 0x1f,
kenjiArai 0:5b88d5760320 502 0x58, 0x69, 0xca, 0x52, 0xc5, 0xb8, 0x3f, 0xa3,
kenjiArai 0:5b88d5760320 503 0x6f, 0xf2, 0x16, 0xb9, 0xc1, 0xd3, 0x00, 0x62,
kenjiArai 0:5b88d5760320 504 0xbe, 0xbc, 0xfd, 0x2d, 0xc5, 0xbc, 0xe0, 0x91,
kenjiArai 0:5b88d5760320 505 0x19, 0x34, 0xfd, 0xa7, 0x9a, 0x86, 0xf6, 0xe6,
kenjiArai 0:5b88d5760320 506 0x98, 0xce, 0xd7, 0x59, 0xc3, 0xff, 0x9b, 0x64,
kenjiArai 0:5b88d5760320 507 0x77, 0x33, 0x8f, 0x3d, 0xa4, 0xf9, 0xcd, 0x85,
kenjiArai 0:5b88d5760320 508 0x14, 0xea, 0x99, 0x82, 0xcc, 0xaf, 0xb3, 0x41,
kenjiArai 0:5b88d5760320 509 0xb2, 0x38, 0x4d, 0xd9, 0x02, 0xf3, 0xd1, 0xab,
kenjiArai 0:5b88d5760320 510 0x7a, 0xc6, 0x1d, 0xd2, 0x9c, 0x6f, 0x21, 0xba,
kenjiArai 0:5b88d5760320 511 0x5b, 0x86, 0x2f, 0x37, 0x30, 0xe3, 0x7c, 0xfd,
kenjiArai 0:5b88d5760320 512 0xc4, 0xfd, 0x80, 0x6c, 0x22, 0xf2, 0x21
kenjiArai 0:5b88d5760320 513 }
kenjiArai 0:5b88d5760320 514 };
kenjiArai 0:5b88d5760320 515
kenjiArai 0:5b88d5760320 516 static const size_t test_lengths[2] =
kenjiArai 0:5b88d5760320 517 {
kenjiArai 0:5b88d5760320 518 64U,
kenjiArai 0:5b88d5760320 519 375U
kenjiArai 0:5b88d5760320 520 };
kenjiArai 0:5b88d5760320 521
kenjiArai 0:5b88d5760320 522 #define ASSERT( cond, args ) \
kenjiArai 0:5b88d5760320 523 do \
kenjiArai 0:5b88d5760320 524 { \
kenjiArai 0:5b88d5760320 525 if( ! ( cond ) ) \
kenjiArai 0:5b88d5760320 526 { \
kenjiArai 0:5b88d5760320 527 if( verbose != 0 ) \
kenjiArai 0:5b88d5760320 528 mbedtls_printf args; \
kenjiArai 0:5b88d5760320 529 \
kenjiArai 0:5b88d5760320 530 return( -1 ); \
kenjiArai 0:5b88d5760320 531 } \
kenjiArai 0:5b88d5760320 532 } \
kenjiArai 0:5b88d5760320 533 while( 0 )
kenjiArai 0:5b88d5760320 534
kenjiArai 0:5b88d5760320 535 int mbedtls_chacha20_self_test( int verbose )
kenjiArai 0:5b88d5760320 536 {
kenjiArai 0:5b88d5760320 537 unsigned char output[381];
kenjiArai 0:5b88d5760320 538 unsigned i;
kenjiArai 0:5b88d5760320 539 int ret;
kenjiArai 0:5b88d5760320 540
kenjiArai 0:5b88d5760320 541 for( i = 0U; i < 2U; i++ )
kenjiArai 0:5b88d5760320 542 {
kenjiArai 0:5b88d5760320 543 if( verbose != 0 )
kenjiArai 0:5b88d5760320 544 mbedtls_printf( " ChaCha20 test %u ", i );
kenjiArai 0:5b88d5760320 545
kenjiArai 0:5b88d5760320 546 ret = mbedtls_chacha20_crypt( test_keys[i],
kenjiArai 0:5b88d5760320 547 test_nonces[i],
kenjiArai 0:5b88d5760320 548 test_counters[i],
kenjiArai 0:5b88d5760320 549 test_lengths[i],
kenjiArai 0:5b88d5760320 550 test_input[i],
kenjiArai 0:5b88d5760320 551 output );
kenjiArai 0:5b88d5760320 552
kenjiArai 0:5b88d5760320 553 ASSERT( 0 == ret, ( "error code: %i\n", ret ) );
kenjiArai 0:5b88d5760320 554
kenjiArai 0:5b88d5760320 555 ASSERT( 0 == memcmp( output, test_output[i], test_lengths[i] ),
kenjiArai 0:5b88d5760320 556 ( "failed (output)\n" ) );
kenjiArai 0:5b88d5760320 557
kenjiArai 0:5b88d5760320 558 if( verbose != 0 )
kenjiArai 0:5b88d5760320 559 mbedtls_printf( "passed\n" );
kenjiArai 0:5b88d5760320 560 }
kenjiArai 0:5b88d5760320 561
kenjiArai 0:5b88d5760320 562 if( verbose != 0 )
kenjiArai 0:5b88d5760320 563 mbedtls_printf( "\n" );
kenjiArai 0:5b88d5760320 564
kenjiArai 0:5b88d5760320 565 return( 0 );
kenjiArai 0:5b88d5760320 566 }
kenjiArai 0:5b88d5760320 567
kenjiArai 0:5b88d5760320 568 #endif /* MBEDTLS_SELF_TEST */
kenjiArai 0:5b88d5760320 569
kenjiArai 0:5b88d5760320 570 #endif /* !MBEDTLS_CHACHA20_C */