early-access version 1503

This commit is contained in:
pineappleEA
2021-03-06 01:41:47 +01:00
parent a37fdd48d5
commit 3fd627d0ba
558 changed files with 55823 additions and 15727 deletions

View File

@@ -30,7 +30,7 @@ void mbedtls_nist_kw_mix_contexts( )
memset( key, 0, sizeof( key ) );
/*
* 1. Check wrap and unwrap with two seperate contexts
* 1. Check wrap and unwrap with two separate contexts
*/
mbedtls_nist_kw_init( &ctx1 );
mbedtls_nist_kw_init( &ctx2 );
@@ -158,23 +158,17 @@ void nist_kw_plaintext_lengths( int in_len, int out_len, int mode, int res )
memset( key, 0, sizeof( key ) );
if (in_len == 0)
{
/* mbedtls_calloc can return NULL for zero-length buffers. Make sure we
* always have a plaintext buffer, even if the length is 0. */
plaintext = mbedtls_calloc( 1, 1 );
}
else
if( in_len != 0 )
{
plaintext = mbedtls_calloc( 1, in_len );
TEST_ASSERT( plaintext != NULL );
}
TEST_ASSERT( plaintext != NULL );
ciphertext = mbedtls_calloc( 1, output_len );
TEST_ASSERT( ciphertext != NULL );
memset( plaintext, 0, in_len );
memset( ciphertext, 0, output_len );
if( out_len != 0 )
{
ciphertext = mbedtls_calloc( 1, output_len );
TEST_ASSERT( ciphertext != NULL );
}
TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
key, 8 * sizeof( key ), 1 ) == 0 );
@@ -216,14 +210,16 @@ void nist_kw_ciphertext_lengths( int in_len, int out_len, int mode, int res )
memset( key, 0, sizeof( key ) );
plaintext = mbedtls_calloc( 1, output_len );
TEST_ASSERT( plaintext != NULL );
ciphertext = mbedtls_calloc( 1, in_len );
TEST_ASSERT( ciphertext != NULL );
memset( plaintext, 0, output_len );
memset( ciphertext, 0, in_len );
if( out_len != 0 )
{
plaintext = mbedtls_calloc( 1, output_len );
TEST_ASSERT( plaintext != NULL );
}
if( in_len != 0 )
{
ciphertext = mbedtls_calloc( 1, in_len );
TEST_ASSERT( ciphertext != NULL );
}
TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
key, 8 * sizeof( key ), 0 ) == 0 );
@@ -246,42 +242,31 @@ exit:
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_nist_kw_wrap( int cipher_id, int mode,
char *key_hex, char *msg_hex,
char *result_hex )
void mbedtls_nist_kw_wrap( int cipher_id, int mode, data_t *key, data_t *msg,
data_t *expected_result )
{
unsigned char key[32];
unsigned char msg[512];
unsigned char result[528];
unsigned char expected_result[528];
mbedtls_nist_kw_context ctx;
size_t key_len, msg_len, output_len, result_len, i, padlen;
size_t result_len, i, padlen;
mbedtls_nist_kw_init( &ctx );
memset( key, 0x00, sizeof( key ) );
memset( msg, 0x00, sizeof( msg ) );
memset( result, '+', sizeof( result ) );
key_len = unhexify( key, key_hex );
msg_len = unhexify( msg, msg_hex );
result_len = unhexify( expected_result, result_hex );
output_len = sizeof( result );
TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_len * 8, 1 )
== 0 );
TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, cipher_id,
key->x, key->len * 8, 1 ) == 0 );
/* Test with input == output */
TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx, mode, msg, msg_len,
result, &output_len, sizeof( result ) ) == 0 );
TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx, mode, msg->x, msg->len,
result, &result_len, sizeof( result ) ) == 0 );
TEST_ASSERT( output_len == result_len );
TEST_ASSERT( result_len == expected_result->len );
TEST_ASSERT( memcmp( expected_result, result, result_len ) == 0 );
TEST_ASSERT( memcmp( expected_result->x, result, result_len ) == 0 );
padlen = ( msg_len % 8 != 0 ) ? 8 - (msg_len % 8 ) : 0;
padlen = ( msg->len % 8 != 0 ) ? 8 - (msg->len % 8 ) : 0;
/* Check that the function didn't write beyond the end of the buffer. */
for( i = msg_len + 8 + padlen; i < sizeof( result ); i++ )
for( i = msg->len + 8 + padlen; i < sizeof( result ); i++ )
{
TEST_ASSERT( result[i] == '+' );
}
@@ -292,47 +277,35 @@ exit:
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_nist_kw_unwrap( int cipher_id, int mode,
char *key_hex, char *msg_hex,
char *result_hex, int expected_ret )
void mbedtls_nist_kw_unwrap( int cipher_id, int mode, data_t *key, data_t *msg,
data_t *expected_result, int expected_ret )
{
unsigned char key[32];
unsigned char msg[528];
unsigned char result[528];
unsigned char expected_result[528];
mbedtls_nist_kw_context ctx;
size_t key_len, msg_len, output_len, result_len, i;
size_t result_len, i;
mbedtls_nist_kw_init( &ctx );
memset( key, 0x00, sizeof( key ) );
memset( msg, 0x00, sizeof( msg ) );
memset( result, '+', sizeof( result ) );
memset( expected_result, 0x00, sizeof( expected_result ) );
key_len = unhexify( key, key_hex );
msg_len = unhexify( msg, msg_hex );
result_len = unhexify( expected_result, result_hex );
output_len = sizeof( result );
TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_len * 8, 0 )
== 0 );
TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, cipher_id,
key->x, key->len * 8, 0 ) == 0 );
/* Test with input == output */
TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx, mode, msg, msg_len,
result, &output_len, sizeof( result ) ) == expected_ret );
TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx, mode, msg->x, msg->len,
result, &result_len, sizeof( result ) ) == expected_ret );
if( expected_ret == 0 )
{
TEST_ASSERT( output_len == result_len );
TEST_ASSERT( memcmp( expected_result, result, result_len ) == 0 );
TEST_ASSERT( result_len == expected_result->len );
TEST_ASSERT( memcmp( expected_result->x, result, result_len ) == 0 );
}
else
{
TEST_ASSERT( output_len == 0 );
TEST_ASSERT( result_len == 0 );
}
/* Check that the function didn't write beyond the end of the buffer. */
for( i = msg_len - 8; i < sizeof( result ); i++ )
for( i = msg->len - 8; i < sizeof( result ); i++ )
{
TEST_ASSERT( result[i] == '+' );
}