Add 'no padding' mode

This commit is contained in:
Manuel Pégourié-Gonnard 2013-07-26 16:50:44 +02:00 committed by Paul Bakker
parent 0e7d2c0f95
commit ebdc413f44
7 changed files with 321 additions and 1 deletions

View File

@ -109,6 +109,7 @@ typedef enum {
POLARSSL_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding */
POLARSSL_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding */
POLARSSL_PADDING_ZEROS, /**< zero padding (not reversible!) */
POLARSSL_PADDING_NONE, /**< never pad (full blocks only) */
} cipher_padding_t;
typedef enum {

View File

@ -657,6 +657,23 @@ static int get_zeros_padding( unsigned char *input, size_t input_len,
return 0;
}
/*
* No padding: don't pad :)
*
* There is no add_padding function (check for NULL in cipher_finish)
* but a trivial get_padding function
*/
static int get_no_padding( unsigned char *input, size_t input_len,
size_t *data_len )
{
if( NULL == input || NULL == data_len )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
*data_len = input_len;
return 0;
}
int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
{
int ret = 0;
@ -677,12 +694,27 @@ int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
{
if( POLARSSL_ENCRYPT == ctx->operation )
{
/* check for 'no padding' mode */
if( NULL == ctx->add_padding )
{
if( 0 != ctx->unprocessed_len )
return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
return 0;
}
ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
ctx->unprocessed_len );
}
else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
{
/* For decrypt operations, expect a full block */
/*
* For decrypt operations, expect a full block,
* or an empty block if no padding
*/
if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
return 0;
return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}
@ -743,6 +775,13 @@ int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
return 0;
}
if( POLARSSL_PADDING_NONE == mode )
{
ctx->add_padding = NULL;
ctx->get_padding = get_no_padding;
return 0;
}
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
}

View File

@ -245,6 +245,66 @@ AES Encrypt and decrypt 49 bytes with zeros padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:49:POLARSSL_PADDING_ZEROS
AES Encrypt and decrypt 0 bytes with no padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:0:POLARSSL_PADDING_NONE
AES Encrypt and decrypt 16 bytes with no padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:16:POLARSSL_PADDING_NONE
AES Encrypt and decrypt 32 bytes with no padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:32:POLARSSL_PADDING_NONE
AES Encrypt and decrypt 48 bytes with no padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:48:POLARSSL_PADDING_NONE
AES Try encrypting 1 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:1:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Try encrypting 2 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:2:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Try encrypting 7 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:7:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Try encrypting 8 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:8:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Try encrypting 9 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:9:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Try encrypting 15 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:15:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Try encrypting 17 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:17:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Try encrypting 31 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:31:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Try encrypting 33 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:33:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Try encrypting 47 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:47:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Try encrypting 49 bytes with no padding
depends_on:POLARSSL_AES_C
enc_fail:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_NONE:128:49:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
AES Encrypt and decrypt 0 bytes in multiple parts
depends_on:POLARSSL_AES_C
enc_dec_buf_multipart:POLARSSL_CIPHER_AES_128_CBC:128:0:0:

View File

@ -245,6 +245,66 @@ BLOWFISH Encrypt and decrypt 49 bytes with zeros padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:49:POLARSSL_PADDING_ZEROS
BLOWFISH Encrypt and decrypt 0 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:0:POLARSSL_PADDING_NONE
BLOWFISH Encrypt and decrypt 8 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:8:POLARSSL_PADDING_NONE
BLOWFISH Encrypt and decrypt 16 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:16:POLARSSL_PADDING_NONE
BLOWFISH Encrypt and decrypt 32 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:32:POLARSSL_PADDING_NONE
BLOWFISH Encrypt and decrypt 48 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:48:POLARSSL_PADDING_NONE
BLOWFISH Try encrypting 1 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_fail:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_NONE:128:1:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
BLOWFISH Try encrypting 2 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_fail:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_NONE:128:2:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
BLOWFISH Try encrypting 7 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_fail:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_NONE:128:7:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
BLOWFISH Try encrypting 9 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_fail:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_NONE:128:9:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
BLOWFISH Try encrypting 15 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_fail:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_NONE:128:15:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
BLOWFISH Try encrypting 17 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_fail:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_NONE:128:17:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
BLOWFISH Try encrypting 31 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_fail:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_NONE:128:31:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
BLOWFISH Try encrypting 33 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_fail:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_NONE:128:33:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
BLOWFISH Try encrypting 47 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_fail:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_NONE:128:47:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
BLOWFISH Try encrypting 49 bytes with no padding
depends_on:POLARSSL_BLOWFISH_C
enc_fail:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_NONE:128:49:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
BLOWFISH Encrypt and decrypt 0 bytes in multiple parts
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf_multipart:POLARSSL_CIPHER_BLOWFISH_CBC:128:0:0:

View File

@ -245,6 +245,66 @@ CAMELLIA Encrypt and decrypt 49 bytes with zeros padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:49:POLARSSL_PADDING_ZEROS
CAMELLIA Encrypt and decrypt 0 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:0:POLARSSL_PADDING_NONE
CAMELLIA Encrypt and decrypt 16 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:16:POLARSSL_PADDING_NONE
CAMELLIA Encrypt and decrypt 32 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:32:POLARSSL_PADDING_NONE
CAMELLIA Encrypt and decrypt 48 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:48:POLARSSL_PADDING_NONE
CAMELLIA Try encrypting 1 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:1:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Try encrypting 2 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:2:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Try encrypting 7 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:7:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Try encrypting 8 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:8:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Try encrypting 9 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:9:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Try encrypting 15 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:15:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Try encrypting 17 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:17:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Try encrypting 31 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:31:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Try encrypting 33 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:33:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Try encrypting 47 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:47:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Try encrypting 49 bytes with no padding
depends_on:POLARSSL_CAMELLIA_C
enc_fail:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_NONE:128:49:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
CAMELLIA Encrypt and decrypt 0 bytes in multiple parts
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf_multipart:POLARSSL_CIPHER_CAMELLIA_128_CBC:128:0:0:

View File

@ -245,6 +245,66 @@ DES Encrypt and decrypt 49 bytes with zeros padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:49:POLARSSL_PADDING_ZEROS
DES Encrypt and decrypt 0 bytes with no padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:0:POLARSSL_PADDING_NONE
DES Encrypt and decrypt 8 bytes with no padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:8:POLARSSL_PADDING_NONE
DES Encrypt and decrypt 16 bytes with no padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:16:POLARSSL_PADDING_NONE
DES Encrypt and decrypt 32 bytes with no padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:32:POLARSSL_PADDING_NONE
DES Encrypt and decrypt 48 bytes with no padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:48:POLARSSL_PADDING_NONE
DES Try encrypting 1 bytes with no padding
depends_on:POLARSSL_DES_C
enc_fail:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_NONE:56:1:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
DES Try encrypting 2 bytes with no padding
depends_on:POLARSSL_DES_C
enc_fail:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_NONE:56:2:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
DES Try encrypting 7 bytes with no padding
depends_on:POLARSSL_DES_C
enc_fail:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_NONE:56:7:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
DES Try encrypting 9 bytes with no padding
depends_on:POLARSSL_DES_C
enc_fail:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_NONE:56:9:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
DES Try encrypting 15 bytes with no padding
depends_on:POLARSSL_DES_C
enc_fail:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_NONE:56:15:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
DES Try encrypting 17 bytes with no padding
depends_on:POLARSSL_DES_C
enc_fail:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_NONE:56:17:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
DES Try encrypting 31 bytes with no padding
depends_on:POLARSSL_DES_C
enc_fail:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_NONE:56:31:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
DES Try encrypting 33 bytes with no padding
depends_on:POLARSSL_DES_C
enc_fail:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_NONE:56:33:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
DES Try encrypting 47 bytes with no padding
depends_on:POLARSSL_DES_C
enc_fail:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_NONE:56:47:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
DES Try encrypting 49 bytes with no padding
depends_on:POLARSSL_DES_C
enc_fail:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_NONE:56:49:POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
DES Encrypt and decrypt 0 bytes in multiple parts
depends_on:POLARSSL_DES_C
enc_dec_buf_multipart:POLARSSL_CIPHER_DES_CBC:56:0:0:

View File

@ -91,6 +91,46 @@ enc_dec_buf:cipher_id:cipher_string:key_len:length:pad_mode:
TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
END_CASE
BEGIN_CASE
enc_fail:cipher_id:pad_mode:key_len:length:ret:
size_t length = {length};
unsigned char key[32];
unsigned char iv[16];
const cipher_info_t *cipher_info;
cipher_context_t ctx;
unsigned char inbuf[64];
unsigned char encbuf[64];
size_t outlen = 0;
memset( key, 0, 32 );
memset( iv , 0, 16 );
memset( &ctx, 0, sizeof( ctx ) );
memset( inbuf, 5, 64 );
memset( encbuf, 0, 64 );
/* Check and get info structures */
cipher_info = cipher_info_from_type( {cipher_id} );
TEST_ASSERT( NULL != cipher_info );
/* Initialise context */
TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
TEST_ASSERT( 0 == cipher_setkey( &ctx, key, {key_len}, POLARSSL_ENCRYPT ) );
TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, {pad_mode} ) );
TEST_ASSERT( 0 == cipher_reset( &ctx, iv ) );
/* encode length number of bytes from inbuf */
TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
TEST_ASSERT( {ret} == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
/* done */
TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
END_CASE
BEGIN_CASE
dec_empty_buf:
unsigned char key[32];