early-access version 1503
This commit is contained in:
114
externals/mbedtls/library/aria.c
vendored
114
externals/mbedtls/library/aria.c
vendored
@@ -1,8 +1,31 @@
|
||||
/*
|
||||
* ARIA implementation
|
||||
*
|
||||
* Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: GPL-2.0
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||
*
|
||||
* This file is provided under the Apache License 2.0, or the
|
||||
* GNU General Public License v2.0 or later.
|
||||
*
|
||||
* **********
|
||||
* Apache License 2.0:
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* **********
|
||||
*
|
||||
* **********
|
||||
* GNU General Public License v2.0 or later:
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -18,7 +41,7 @@
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
* **********
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ -57,6 +80,12 @@
|
||||
#define inline __inline
|
||||
#endif
|
||||
|
||||
/* Parameter validation macros */
|
||||
#define ARIA_VALIDATE_RET( cond ) \
|
||||
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ARIA_BAD_INPUT_DATA )
|
||||
#define ARIA_VALIDATE( cond ) \
|
||||
MBEDTLS_INTERNAL_VALIDATE( cond )
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (little endian)
|
||||
*/
|
||||
@@ -451,9 +480,11 @@ int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx,
|
||||
|
||||
int i;
|
||||
uint32_t w[4][4], *w2;
|
||||
ARIA_VALIDATE_RET( ctx != NULL );
|
||||
ARIA_VALIDATE_RET( key != NULL );
|
||||
|
||||
if( keybits != 128 && keybits != 192 && keybits != 256 )
|
||||
return( MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH );
|
||||
return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA );
|
||||
|
||||
/* Copy key to W0 (and potential remainder to W1) */
|
||||
GET_UINT32_LE( w[0][0], key, 0 );
|
||||
@@ -505,6 +536,8 @@ int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx,
|
||||
const unsigned char *key, unsigned int keybits )
|
||||
{
|
||||
int i, j, k, ret;
|
||||
ARIA_VALIDATE_RET( ctx != NULL );
|
||||
ARIA_VALIDATE_RET( key != NULL );
|
||||
|
||||
ret = mbedtls_aria_setkey_enc( ctx, key, keybits );
|
||||
if( ret != 0 )
|
||||
@@ -541,6 +574,9 @@ int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
|
||||
int i;
|
||||
|
||||
uint32_t a, b, c, d;
|
||||
ARIA_VALIDATE_RET( ctx != NULL );
|
||||
ARIA_VALIDATE_RET( input != NULL );
|
||||
ARIA_VALIDATE_RET( output != NULL );
|
||||
|
||||
GET_UINT32_LE( a, input, 0 );
|
||||
GET_UINT32_LE( b, input, 4 );
|
||||
@@ -588,6 +624,7 @@ int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx,
|
||||
/* Initialize context */
|
||||
void mbedtls_aria_init( mbedtls_aria_context *ctx )
|
||||
{
|
||||
ARIA_VALIDATE( ctx != NULL );
|
||||
memset( ctx, 0, sizeof( mbedtls_aria_context ) );
|
||||
}
|
||||
|
||||
@@ -614,6 +651,13 @@ int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx,
|
||||
int i;
|
||||
unsigned char temp[MBEDTLS_ARIA_BLOCKSIZE];
|
||||
|
||||
ARIA_VALIDATE_RET( ctx != NULL );
|
||||
ARIA_VALIDATE_RET( mode == MBEDTLS_ARIA_ENCRYPT ||
|
||||
mode == MBEDTLS_ARIA_DECRYPT );
|
||||
ARIA_VALIDATE_RET( length == 0 || input != NULL );
|
||||
ARIA_VALIDATE_RET( length == 0 || output != NULL );
|
||||
ARIA_VALIDATE_RET( iv != NULL );
|
||||
|
||||
if( length % MBEDTLS_ARIA_BLOCKSIZE )
|
||||
return( MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH );
|
||||
|
||||
@@ -667,7 +711,23 @@ int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx,
|
||||
unsigned char *output )
|
||||
{
|
||||
unsigned char c;
|
||||
size_t n = *iv_off;
|
||||
size_t n;
|
||||
|
||||
ARIA_VALIDATE_RET( ctx != NULL );
|
||||
ARIA_VALIDATE_RET( mode == MBEDTLS_ARIA_ENCRYPT ||
|
||||
mode == MBEDTLS_ARIA_DECRYPT );
|
||||
ARIA_VALIDATE_RET( length == 0 || input != NULL );
|
||||
ARIA_VALIDATE_RET( length == 0 || output != NULL );
|
||||
ARIA_VALIDATE_RET( iv != NULL );
|
||||
ARIA_VALIDATE_RET( iv_off != NULL );
|
||||
|
||||
n = *iv_off;
|
||||
|
||||
/* An overly large value of n can lead to an unlimited
|
||||
* buffer overflow. Therefore, guard against this
|
||||
* outside of parameter validation. */
|
||||
if( n >= MBEDTLS_ARIA_BLOCKSIZE )
|
||||
return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA );
|
||||
|
||||
if( mode == MBEDTLS_ARIA_DECRYPT )
|
||||
{
|
||||
@@ -715,7 +775,21 @@ int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx,
|
||||
unsigned char *output )
|
||||
{
|
||||
int c, i;
|
||||
size_t n = *nc_off;
|
||||
size_t n;
|
||||
|
||||
ARIA_VALIDATE_RET( ctx != NULL );
|
||||
ARIA_VALIDATE_RET( length == 0 || input != NULL );
|
||||
ARIA_VALIDATE_RET( length == 0 || output != NULL );
|
||||
ARIA_VALIDATE_RET( nonce_counter != NULL );
|
||||
ARIA_VALIDATE_RET( stream_block != NULL );
|
||||
ARIA_VALIDATE_RET( nc_off != NULL );
|
||||
|
||||
n = *nc_off;
|
||||
/* An overly large value of n can lead to an unlimited
|
||||
* buffer overflow. Therefore, guard against this
|
||||
* outside of parameter validation. */
|
||||
if( n >= MBEDTLS_ARIA_BLOCKSIZE )
|
||||
return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA );
|
||||
|
||||
while( length-- )
|
||||
{
|
||||
@@ -877,11 +951,11 @@ static const uint8_t aria_test2_ctr_ct[3][48] = // CTR ciphertext
|
||||
#define ARIA_SELF_TEST_IF_FAIL \
|
||||
{ \
|
||||
if( verbose ) \
|
||||
printf( "failed\n" ); \
|
||||
mbedtls_printf( "failed\n" ); \
|
||||
return( 1 ); \
|
||||
} else { \
|
||||
if( verbose ) \
|
||||
printf( "passed\n" ); \
|
||||
mbedtls_printf( "passed\n" ); \
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -910,7 +984,7 @@ int mbedtls_aria_self_test( int verbose )
|
||||
{
|
||||
/* test ECB encryption */
|
||||
if( verbose )
|
||||
printf( " ARIA-ECB-%d (enc): ", 128 + 64 * i );
|
||||
mbedtls_printf( " ARIA-ECB-%d (enc): ", 128 + 64 * i );
|
||||
mbedtls_aria_setkey_enc( &ctx, aria_test1_ecb_key, 128 + 64 * i );
|
||||
mbedtls_aria_crypt_ecb( &ctx, aria_test1_ecb_pt, blk );
|
||||
if( memcmp( blk, aria_test1_ecb_ct[i], MBEDTLS_ARIA_BLOCKSIZE ) != 0 )
|
||||
@@ -918,14 +992,14 @@ int mbedtls_aria_self_test( int verbose )
|
||||
|
||||
/* test ECB decryption */
|
||||
if( verbose )
|
||||
printf( " ARIA-ECB-%d (dec): ", 128 + 64 * i );
|
||||
mbedtls_printf( " ARIA-ECB-%d (dec): ", 128 + 64 * i );
|
||||
mbedtls_aria_setkey_dec( &ctx, aria_test1_ecb_key, 128 + 64 * i );
|
||||
mbedtls_aria_crypt_ecb( &ctx, aria_test1_ecb_ct[i], blk );
|
||||
if( memcmp( blk, aria_test1_ecb_pt, MBEDTLS_ARIA_BLOCKSIZE ) != 0 )
|
||||
ARIA_SELF_TEST_IF_FAIL;
|
||||
}
|
||||
if( verbose )
|
||||
printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
|
||||
/*
|
||||
* Test set 2
|
||||
@@ -935,7 +1009,7 @@ int mbedtls_aria_self_test( int verbose )
|
||||
{
|
||||
/* Test CBC encryption */
|
||||
if( verbose )
|
||||
printf( " ARIA-CBC-%d (enc): ", 128 + 64 * i );
|
||||
mbedtls_printf( " ARIA-CBC-%d (enc): ", 128 + 64 * i );
|
||||
mbedtls_aria_setkey_enc( &ctx, aria_test2_key, 128 + 64 * i );
|
||||
memcpy( iv, aria_test2_iv, MBEDTLS_ARIA_BLOCKSIZE );
|
||||
memset( buf, 0x55, sizeof( buf ) );
|
||||
@@ -946,7 +1020,7 @@ int mbedtls_aria_self_test( int verbose )
|
||||
|
||||
/* Test CBC decryption */
|
||||
if( verbose )
|
||||
printf( " ARIA-CBC-%d (dec): ", 128 + 64 * i );
|
||||
mbedtls_printf( " ARIA-CBC-%d (dec): ", 128 + 64 * i );
|
||||
mbedtls_aria_setkey_dec( &ctx, aria_test2_key, 128 + 64 * i );
|
||||
memcpy( iv, aria_test2_iv, MBEDTLS_ARIA_BLOCKSIZE );
|
||||
memset( buf, 0xAA, sizeof( buf ) );
|
||||
@@ -956,7 +1030,7 @@ int mbedtls_aria_self_test( int verbose )
|
||||
ARIA_SELF_TEST_IF_FAIL;
|
||||
}
|
||||
if( verbose )
|
||||
printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
||||
|
||||
@@ -965,7 +1039,7 @@ int mbedtls_aria_self_test( int verbose )
|
||||
{
|
||||
/* Test CFB encryption */
|
||||
if( verbose )
|
||||
printf( " ARIA-CFB-%d (enc): ", 128 + 64 * i );
|
||||
mbedtls_printf( " ARIA-CFB-%d (enc): ", 128 + 64 * i );
|
||||
mbedtls_aria_setkey_enc( &ctx, aria_test2_key, 128 + 64 * i );
|
||||
memcpy( iv, aria_test2_iv, MBEDTLS_ARIA_BLOCKSIZE );
|
||||
memset( buf, 0x55, sizeof( buf ) );
|
||||
@@ -977,7 +1051,7 @@ int mbedtls_aria_self_test( int verbose )
|
||||
|
||||
/* Test CFB decryption */
|
||||
if( verbose )
|
||||
printf( " ARIA-CFB-%d (dec): ", 128 + 64 * i );
|
||||
mbedtls_printf( " ARIA-CFB-%d (dec): ", 128 + 64 * i );
|
||||
mbedtls_aria_setkey_enc( &ctx, aria_test2_key, 128 + 64 * i );
|
||||
memcpy( iv, aria_test2_iv, MBEDTLS_ARIA_BLOCKSIZE );
|
||||
memset( buf, 0xAA, sizeof( buf ) );
|
||||
@@ -988,7 +1062,7 @@ int mbedtls_aria_self_test( int verbose )
|
||||
ARIA_SELF_TEST_IF_FAIL;
|
||||
}
|
||||
if( verbose )
|
||||
printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CFB */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
||||
@@ -996,7 +1070,7 @@ int mbedtls_aria_self_test( int verbose )
|
||||
{
|
||||
/* Test CTR encryption */
|
||||
if( verbose )
|
||||
printf( " ARIA-CTR-%d (enc): ", 128 + 64 * i );
|
||||
mbedtls_printf( " ARIA-CTR-%d (enc): ", 128 + 64 * i );
|
||||
mbedtls_aria_setkey_enc( &ctx, aria_test2_key, 128 + 64 * i );
|
||||
memset( iv, 0, MBEDTLS_ARIA_BLOCKSIZE ); // IV = 0
|
||||
memset( buf, 0x55, sizeof( buf ) );
|
||||
@@ -1008,7 +1082,7 @@ int mbedtls_aria_self_test( int verbose )
|
||||
|
||||
/* Test CTR decryption */
|
||||
if( verbose )
|
||||
printf( " ARIA-CTR-%d (dec): ", 128 + 64 * i );
|
||||
mbedtls_printf( " ARIA-CTR-%d (dec): ", 128 + 64 * i );
|
||||
mbedtls_aria_setkey_enc( &ctx, aria_test2_key, 128 + 64 * i );
|
||||
memset( iv, 0, MBEDTLS_ARIA_BLOCKSIZE ); // IV = 0
|
||||
memset( buf, 0xAA, sizeof( buf ) );
|
||||
@@ -1019,7 +1093,7 @@ int mbedtls_aria_self_test( int verbose )
|
||||
ARIA_SELF_TEST_IF_FAIL;
|
||||
}
|
||||
if( verbose )
|
||||
printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CTR */
|
||||
|
||||
return( 0 );
|
||||
|
Reference in New Issue
Block a user