Commit 2b222c83 authored by Paul Bakker's avatar Paul Bakker
Browse files

- Changed interface for AES and Camellia setkey functions to indicate invalid key lengths.

parent 33768bf1
......@@ -13,6 +13,8 @@ Changes
this is mind when checking for errors.
* RSA_RAW renamed to SIG_RSA_RAW for consistency.
* Fixed typo in name of POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE.
* Changed interface for AES and Camellia setkey functions
to indicate invalid key lengths.
Bug fixes
* Fixed include location of endian.h on FreeBSD (found by
......
......@@ -25,6 +25,8 @@
#define AES_ENCRYPT 1
#define AES_DECRYPT 0
#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH 0x0800
/**
* \brief AES context structure
*/
......@@ -46,8 +48,10 @@ extern "C" {
* \param ctx AES context to be initialized
* \param key encryption key
* \param keysize must be 128, 192 or 256
*
* \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
*/
void aes_setkey_enc( aes_context *ctx, unsigned char *key, int keysize );
int aes_setkey_enc( aes_context *ctx, unsigned char *key, int keysize );
/**
* \brief AES key schedule (decryption)
......@@ -55,8 +59,10 @@ void aes_setkey_enc( aes_context *ctx, unsigned char *key, int keysize );
* \param ctx AES context to be initialized
* \param key decryption key
* \param keysize must be 128, 192 or 256
*
* \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
*/
void aes_setkey_dec( aes_context *ctx, unsigned char *key, int keysize );
int aes_setkey_dec( aes_context *ctx, unsigned char *key, int keysize );
/**
* \brief AES-ECB block encryption/decryption
......
......@@ -30,6 +30,8 @@ typedef UINT32 uint32_t;
#define CAMELLIA_ENCRYPT 1
#define CAMELLIA_DECRYPT 0
#define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH 0x0a00
/**
* \brief CAMELLIA context structure
*/
......@@ -50,8 +52,10 @@ extern "C" {
* \param ctx CAMELLIA context to be initialized
* \param key encryption key
* \param keysize must be 128, 192 or 256
*
* \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
*/
void camellia_setkey_enc( camellia_context *ctx, unsigned char *key, int keysize );
int camellia_setkey_enc( camellia_context *ctx, unsigned char *key, int keysize );
/**
* \brief CAMELLIA key schedule (decryption)
......@@ -59,8 +63,10 @@ void camellia_setkey_enc( camellia_context *ctx, unsigned char *key, int keysize
* \param ctx CAMELLIA context to be initialized
* \param key decryption key
* \param keysize must be 128, 192 or 256
*
* \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
*/
void camellia_setkey_dec( camellia_context *ctx, unsigned char *key, int keysize );
int camellia_setkey_dec( camellia_context *ctx, unsigned char *key, int keysize );
/**
* \brief CAMELLIA-ECB block encryption/decryption
......
......@@ -438,7 +438,7 @@ static void aes_gen_tables( void )
/*
* AES key schedule (encryption)
*/
void aes_setkey_enc( aes_context *ctx, unsigned char *key, int keysize )
int aes_setkey_enc( aes_context *ctx, unsigned char *key, int keysize )
{
int i;
unsigned long *RK;
......@@ -456,7 +456,7 @@ void aes_setkey_enc( aes_context *ctx, unsigned char *key, int keysize )
case 128: ctx->nr = 10; break;
case 192: ctx->nr = 12; break;
case 256: ctx->nr = 14; break;
default : return;
default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
}
#if defined(PADLOCK_ALIGN16)
......@@ -536,24 +536,27 @@ void aes_setkey_enc( aes_context *ctx, unsigned char *key, int keysize )
break;
}
return( 0 );
}
/*
* AES key schedule (decryption)
*/
void aes_setkey_dec( aes_context *ctx, unsigned char *key, int keysize )
int aes_setkey_dec( aes_context *ctx, unsigned char *key, int keysize )
{
int i, j;
aes_context cty;
unsigned long *RK;
unsigned long *SK;
int ret;
switch( keysize )
{
case 128: ctx->nr = 10; break;
case 192: ctx->nr = 12; break;
case 256: ctx->nr = 14; break;
default : return;
default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
}
#if defined(PADLOCK_ALIGN16)
......@@ -562,7 +565,10 @@ void aes_setkey_dec( aes_context *ctx, unsigned char *key, int keysize )
ctx->rk = RK = ctx->buf;
#endif
aes_setkey_enc( &cty, key, keysize );
ret = aes_setkey_enc( &cty, key, keysize );
if( ret != 0 )
return( ret );
SK = cty.rk + cty.nr * 4;
*RK++ = *SK++;
......@@ -587,6 +593,8 @@ void aes_setkey_dec( aes_context *ctx, unsigned char *key, int keysize )
*RK++ = *SK++;
memset( &cty, 0, sizeof( aes_context ) );
return( 0 );
}
#define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
......
......@@ -304,7 +304,7 @@ static void camellia_feistel(uint32_t x[2], uint32_t k[2], uint32_t z[2])
/*
* Camellia key schedule (encryption)
*/
void camellia_setkey_enc( camellia_context *ctx, unsigned char *key, int keysize )
int camellia_setkey_enc( camellia_context *ctx, unsigned char *key, int keysize )
{
int i, idx;
uint32_t *RK;
......@@ -323,7 +323,7 @@ void camellia_setkey_enc( camellia_context *ctx, unsigned char *key, int keysize
case 128: ctx->nr = 3; idx = 0; break;
case 192:
case 256: ctx->nr = 4; idx = 1; break;
default : return;
default : return( POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH );
}
for( i = 0; i < keysize / 8; ++i)
......@@ -400,29 +400,34 @@ void camellia_setkey_enc( camellia_context *ctx, unsigned char *key, int keysize
RK[32 + 12 * idx + i] = RK[transposes[idx][i]];
}
}
return( 0 );
}
/*
* Camellia key schedule (decryption)
*/
void camellia_setkey_dec( camellia_context *ctx, unsigned char *key, int keysize )
int camellia_setkey_dec( camellia_context *ctx, unsigned char *key, int keysize )
{
int i, idx;
camellia_context cty;
uint32_t *RK;
uint32_t *SK;
int ret;
switch( keysize )
{
case 128: ctx->nr = 3; idx = 0; break;
case 192:
case 256: ctx->nr = 4; idx = 1; break;
default : return;
default : return( POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH );
}
RK = ctx->rk;
camellia_setkey_enc(&cty, key, keysize);
ret = camellia_setkey_enc(&cty, key, keysize);
if( ret != 0 )
return( ret );
SK = cty.rk + 24 * 2 + 8 * idx * 2;
......@@ -445,6 +450,8 @@ void camellia_setkey_dec( camellia_context *ctx, unsigned char *key, int keysize
*RK++ = *SK++;
memset( &cty, 0, sizeof( camellia_context ) );
return( 0 );
}
/*
......
This diff is collapsed.
......@@ -3,7 +3,7 @@ BEGIN_HEADER
END_HEADER
BEGIN_CASE
aes_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
aes_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string:setkey_result
{
unsigned char key_str[100];
unsigned char src_str[100];
......@@ -20,16 +20,19 @@ aes_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
key_len = unhexify( key_str, {hex_key_string} );
unhexify( src_str, {hex_src_string} );
aes_setkey_enc( &ctx, key_str, key_len * 8 );
aes_crypt_ecb( &ctx, AES_ENCRYPT, src_str, output );
hexify( dst_str, output, 16 );
TEST_ASSERT( aes_setkey_enc( &ctx, key_str, key_len * 8 ) == {setkey_result} );
if( {setkey_result} == 0 )
{
aes_crypt_ecb( &ctx, AES_ENCRYPT, src_str, output );
hexify( dst_str, output, 16 );
TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
}
}
END_CASE
BEGIN_CASE
aes_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
aes_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string:setkey_result
{
unsigned char key_str[100];
unsigned char src_str[100];
......@@ -46,11 +49,14 @@ aes_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
key_len = unhexify( key_str, {hex_key_string} );
unhexify( src_str, {hex_src_string} );
aes_setkey_dec( &ctx, key_str, key_len * 8 );
aes_crypt_ecb( &ctx, AES_DECRYPT, src_str, output );
hexify( dst_str, output, 16 );
TEST_ASSERT( aes_setkey_dec( &ctx, key_str, key_len * 8 ) == {setkey_result} );
if( {setkey_result} == 0 )
{
aes_crypt_ecb( &ctx, AES_DECRYPT, src_str, output );
hexify( dst_str, output, 16 );
TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
}
}
END_CASE
......
Camellia-128-ECB Encrypt RFC3713 #1
camellia_encrypt_ecb:"0123456789abcdeffedcba9876543210":"0123456789abcdeffedcba9876543210":"67673138549669730857065648eabe43"
camellia_encrypt_ecb:"0123456789abcdeffedcba9876543210":"0123456789abcdeffedcba9876543210":"67673138549669730857065648eabe43":0
Camellia-192-ECB Encrypt RFC3713 #1
camellia_encrypt_ecb:"0123456789abcdeffedcba98765432100011223344556677":"0123456789abcdeffedcba9876543210":"b4993401b3e996f84ee5cee7d79b09b9"
camellia_encrypt_ecb:"0123456789abcdeffedcba98765432100011223344556677":"0123456789abcdeffedcba9876543210":"b4993401b3e996f84ee5cee7d79b09b9":0
Camellia-256-ECB Encrypt RFC3713 #1
camellia_encrypt_ecb:"0123456789abcdeffedcba987654321000112233445566778899aabbccddeeff":"0123456789abcdeffedcba9876543210":"9acc237dff16d76c20ef7c919e3a7509"
camellia_encrypt_ecb:"0123456789abcdeffedcba987654321000112233445566778899aabbccddeeff":"0123456789abcdeffedcba9876543210":"9acc237dff16d76c20ef7c919e3a7509":0
Camellia-128-ECB Encrypt Perl EVP #1
camellia_encrypt_ecb:"000102030405060708090A0B0C0D0E0F":"00112233445566778899AABBCCDDEEFF":"77CF412067AF8270613529149919546F"
camellia_encrypt_ecb:"000102030405060708090A0B0C0D0E0F":"00112233445566778899AABBCCDDEEFF":"77CF412067AF8270613529149919546F":0
Camellia-192-ECB Encrypt Perl EVP #1
camellia_encrypt_ecb:"000102030405060708090A0B0C0D0E0F1011121314151617":"00112233445566778899AABBCCDDEEFF":"B22F3C36B72D31329EEE8ADDC2906C68"
camellia_encrypt_ecb:"000102030405060708090A0B0C0D0E0F1011121314151617":"00112233445566778899AABBCCDDEEFF":"B22F3C36B72D31329EEE8ADDC2906C68":0
Camellia-256-ECB Encrypt Perl EVP #1
camellia_encrypt_ecb:"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F":"00112233445566778899AABBCCDDEEFF":"2EDF1F3418D53B88841FC8985FB1ECF2"
camellia_encrypt_ecb:"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F":"00112233445566778899AABBCCDDEEFF":"2EDF1F3418D53B88841FC8985FB1ECF2":0
Camellia-128-ECB Encrypt Perl EVP #1
camellia_encrypt_ecb:"2B7E151628AED2A6ABF7158809CF4F3C":"6BC1BEE22E409F96E93D7E117393172A":"432FC5DCD628115B7C388D770B270C96"
camellia_encrypt_ecb:"2B7E151628AED2A6ABF7158809CF4F3C":"6BC1BEE22E409F96E93D7E117393172A":"432FC5DCD628115B7C388D770B270C96":0
Camellia-128-ECB Encrypt Perl EVP #2
camellia_encrypt_ecb:"2B7E151628AED2A6ABF7158809CF4F3C":"AE2D8A571E03AC9C9EB76FAC45AF8E51":"0BE1F14023782A22E8384C5ABB7FAB2B"
camellia_encrypt_ecb:"2B7E151628AED2A6ABF7158809CF4F3C":"AE2D8A571E03AC9C9EB76FAC45AF8E51":"0BE1F14023782A22E8384C5ABB7FAB2B":0
Camellia-128-ECB Encrypt Perl EVP #3
camellia_encrypt_ecb:"2B7E151628AED2A6ABF7158809CF4F3C":"30C81C46A35CE411E5FBC1191A0A52EF":"A0A1ABCD1893AB6FE0FE5B65DF5F8636"
camellia_encrypt_ecb:"2B7E151628AED2A6ABF7158809CF4F3C":"30C81C46A35CE411E5FBC1191A0A52EF":"A0A1ABCD1893AB6FE0FE5B65DF5F8636":0
Camellia-128-ECB Encrypt Perl EVP #4
camellia_encrypt_ecb:"2B7E151628AED2A6ABF7158809CF4F3C":"F69F2445DF4F9B17AD2B417BE66C3710":"E61925E0D5DFAA9BB29F815B3076E51A"
camellia_encrypt_ecb:"2B7E151628AED2A6ABF7158809CF4F3C":"F69F2445DF4F9B17AD2B417BE66C3710":"E61925E0D5DFAA9BB29F815B3076E51A":0
Camellia-192-ECB Encrypt Perl EVP #1
camellia_encrypt_ecb:"8E73B0F7DA0E6452C810F32B809079E562F8EAD2522C6B7B":"6BC1BEE22E409F96E93D7E117393172A":"CCCC6C4E138B45848514D48D0D3439D3"
camellia_encrypt_ecb:"8E73B0F7DA0E6452C810F32B809079E562F8EAD2522C6B7B":"6BC1BEE22E409F96E93D7E117393172A":"CCCC6C4E138B45848514D48D0D3439D3":0
Camellia-192-ECB Encrypt Perl EVP #2
camellia_encrypt_ecb:"8E73B0F7DA0E6452C810F32B809079E562F8EAD2522C6B7B":"AE2D8A571E03AC9C9EB76FAC45AF8E51":"5713C62C14B2EC0F8393B6AFD6F5785A"
camellia_encrypt_ecb:"8E73B0F7DA0E6452C810F32B809079E562F8EAD2522C6B7B":"AE2D8A571E03AC9C9EB76FAC45AF8E51":"5713C62C14B2EC0F8393B6AFD6F5785A":0
Camellia-192-ECB Encrypt Perl EVP #3
camellia_encrypt_ecb:"8E73B0F7DA0E6452C810F32B809079E562F8EAD2522C6B7B":"30C81C46A35CE411E5FBC1191A0A52EF":"B40ED2B60EB54D09D030CF511FEEF366"
camellia_encrypt_ecb:"8E73B0F7DA0E6452C810F32B809079E562F8EAD2522C6B7B":"30C81C46A35CE411E5FBC1191A0A52EF":"B40ED2B60EB54D09D030CF511FEEF366":0
Camellia-192-ECB Encrypt Perl EVP #4
camellia_encrypt_ecb:"8E73B0F7DA0E6452C810F32B809079E562F8EAD2522C6B7B":"F69F2445DF4F9B17AD2B417BE66C3710":"909DBD95799096748CB27357E73E1D26"
camellia_encrypt_ecb:"8E73B0F7DA0E6452C810F32B809079E562F8EAD2522C6B7B":"F69F2445DF4F9B17AD2B417BE66C3710":"909DBD95799096748CB27357E73E1D26":0
Camellia-256-ECB Encrypt Perl EVP #1
camellia_encrypt_ecb:"603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4":"6BC1BEE22E409F96E93D7E117393172A":"BEFD219B112FA00098919CD101C9CCFA"
camellia_encrypt_ecb:"603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4":"6BC1BEE22E409F96E93D7E117393172A":"BEFD219B112FA00098919CD101C9CCFA":0
Camellia-256-ECB Encrypt Perl EVP #2
camellia_encrypt_ecb:"603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4":"AE2D8A571E03AC9C9EB76FAC45AF8E51":"C91D3A8F1AEA08A9386CF4B66C0169EA"
camellia_encrypt_ecb:"603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4":"AE2D8A571E03AC9C9EB76FAC45AF8E51":"C91D3A8F1AEA08A9386CF4B66C0169EA":0
Camellia-256-ECB Encrypt Perl EVP #3
camellia_encrypt_ecb:"603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4":"30C81C46A35CE411E5FBC1191A0A52EF":"A623D711DC5F25A51BB8A80D56397D28"
camellia_encrypt_ecb:"603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4":"30C81C46A35CE411E5FBC1191A0A52EF":"A623D711DC5F25A51BB8A80D56397D28":0
Camellia-256-ECB Encrypt Perl EVP #4
camellia_encrypt_ecb:"603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4":"F69F2445DF4F9B17AD2B417BE66C3710":"7960109FB6DC42947FCFE59EA3C5EB6B"
camellia_encrypt_ecb:"603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4":"F69F2445DF4F9B17AD2B417BE66C3710":"7960109FB6DC42947FCFE59EA3C5EB6B":0
Camellia-128-CBC Encrypt Perl EVP #1
camellia_encrypt_cbc:"2B7E151628AED2A6ABF7158809CF4F3C":"000102030405060708090A0B0C0D0E0F":"6BC1BEE22E409F96E93D7E117393172A":"1607CF494B36BBF00DAEB0B503C831AB"
......@@ -160,5 +160,11 @@ camellia_decrypt_cfb128:"603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A
Camellia-256-CFB128 Decrypt Perl EVP #4
camellia_decrypt_cfb128:"603DEB1015CA71BE2B73AEF0857D77811F352C073B6108D72D9810A30914DFF4":"555FC3F34BDD2D54C62D9E3BF338C1C4":"F69F2445DF4F9B17AD2B417BE66C3710":"5953ADCE14DB8C7F39F1BD39F359BFFA"
Camellia-ECB Encrypt (Invalid key length)
camellia_encrypt_ecb:"0123456789abcdeffedcba98765432":"0123456789abcdeffedcba9876543210":"67673138549669730857065648eabe43":POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Camellia-ECB Decrypt (Invalid key length)
camellia_decrypt_ecb:"0123456789abcdeffedcba98765432":"0123456789abcdeffedcba9876543210":"67673138549669730857065648eabe43":POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH
Camellia Selftest
camellia_selftest:
......@@ -3,7 +3,7 @@ BEGIN_HEADER
END_HEADER
BEGIN_CASE
camellia_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
camellia_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string:setkey_result
{
unsigned char key_str[100];
unsigned char src_str[100];
......@@ -20,16 +20,19 @@ camellia_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
key_len = unhexify( key_str, {hex_key_string} );
unhexify( src_str, {hex_src_string} );
camellia_setkey_enc( &ctx, key_str, key_len * 8 );
camellia_crypt_ecb( &ctx, CAMELLIA_ENCRYPT, src_str, output );
hexify( dst_str, output, 16 );
TEST_ASSERT( camellia_setkey_enc( &ctx, key_str, key_len * 8 ) == {setkey_result} );
if( {setkey_result} == 0 )
{
camellia_crypt_ecb( &ctx, CAMELLIA_ENCRYPT, src_str, output );
hexify( dst_str, output, 16 );
TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
}
}
END_CASE
BEGIN_CASE
camellia_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
camellia_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string:setkey_result
{
unsigned char key_str[100];
unsigned char src_str[100];
......@@ -46,11 +49,14 @@ camellia_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
key_len = unhexify( key_str, {hex_key_string} );
unhexify( src_str, {hex_src_string} );
camellia_setkey_dec( &ctx, key_str, key_len * 8 );
camellia_crypt_ecb( &ctx, CAMELLIA_DECRYPT, src_str, output );
hexify( dst_str, output, 16 );
TEST_ASSERT( camellia_setkey_dec( &ctx, key_str, key_len * 8 ) == {setkey_result} );
if( {setkey_result} == 0 )
{
camellia_crypt_ecb( &ctx, CAMELLIA_DECRYPT, src_str, output );
hexify( dst_str, output, 16 );
TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
}
}
END_CASE
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment