Add zeros-and-length (ANSI X.923) padding

This commit is contained in:
Manuel Pégourié-Gonnard 2013-07-26 14:55:18 +02:00 committed by Paul Bakker
parent 679f9e90ad
commit 8d4291b52a
7 changed files with 302 additions and 0 deletions

View File

@ -107,6 +107,7 @@ typedef enum {
typedef enum {
POLARSSL_PADDING_PKCS7 = 0, /**< PKCS7 padding (default) */
POLARSSL_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding */
POLARSSL_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding */
} cipher_padding_t;
typedef enum {

View File

@ -594,6 +594,42 @@ static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
return 0;
}
/*
* Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
*/
static void add_zeros_and_len_padding( unsigned char *output,
size_t output_len, size_t data_len )
{
size_t padding_len = output_len - data_len;
unsigned char i = 0;
for( i = 1; i < padding_len; i++ )
output[data_len + i - 1] = 0x00;
output[output_len - 1] = (unsigned char) padding_len;
}
static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
size_t *data_len )
{
unsigned int i, padding_len = 0;
if( NULL == input || NULL == data_len )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
padding_len = input[input_len - 1];
if( padding_len > input_len || padding_len == 0 )
return POLARSSL_ERR_CIPHER_INVALID_PADDING;
for( i = input_len - padding_len; i < input_len - 1; i++ )
if( input[i] != 0x00 )
return POLARSSL_ERR_CIPHER_INVALID_PADDING;
*data_len = input_len - padding_len;
return 0;
}
int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
{
int ret = 0;
@ -666,6 +702,13 @@ int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
return 0;
}
if( POLARSSL_PADDING_ZEROS_AND_LEN == mode )
{
ctx->add_padding = add_zeros_and_len_padding;
ctx->get_padding = get_zeros_and_len_padding;
return 0;
}
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
}

View File

@ -125,6 +125,66 @@ AES Encrypt and decrypt 49 bytes with one and zeros padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:49:POLARSSL_PADDING_ONE_AND_ZEROS
AES Encrypt and decrypt 0 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:0:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 1 byte with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:1:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 2 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:2:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 7 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:7:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 8 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:8:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 9 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:9:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 15 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:15:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 16 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:16:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 17 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:17:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 31 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:31:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 32 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:32:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 33 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:33:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 47 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:47:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 48 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:48:POLARSSL_PADDING_ZEROS_AND_LEN
AES Encrypt and decrypt 49 bytes with zeros and len padding
depends_on:POLARSSL_AES_C
enc_dec_buf:POLARSSL_CIPHER_AES_128_CBC:AES-128-CBC:128:49:POLARSSL_PADDING_ZEROS_AND_LEN
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

@ -125,6 +125,66 @@ BLOWFISH Encrypt and decrypt 49 bytes with one and zeros padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:49:POLARSSL_PADDING_ONE_AND_ZEROS
BLOWFISH Encrypt and decrypt 0 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:0:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 1 byte with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:1:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 2 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:2:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 7 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:7:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 8 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:8:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 9 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:9:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 15 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:15:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 16 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:16:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 17 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:17:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 31 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:31:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 32 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:32:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 32 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:33:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 47 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:47:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 48 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:48:POLARSSL_PADDING_ZEROS_AND_LEN
BLOWFISH Encrypt and decrypt 49 bytes with zeros and len padding
depends_on:POLARSSL_BLOWFISH_C
enc_dec_buf:POLARSSL_CIPHER_BLOWFISH_CBC:BLOWFISH-CBC:128:49:POLARSSL_PADDING_ZEROS_AND_LEN
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

@ -125,6 +125,66 @@ CAMELLIA Encrypt and decrypt 49 bytes with one and zeros padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:49:POLARSSL_PADDING_ONE_AND_ZEROS
CAMELLIA Encrypt and decrypt 0 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:0:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 1 byte with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:1:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 2 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:2:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 7 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:7:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 8 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:8:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 9 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:9:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 15 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:15:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 16 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:16:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 17 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:17:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 31 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:31:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 32 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:32:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 32 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:33:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 47 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:47:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 48 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:48:POLARSSL_PADDING_ZEROS_AND_LEN
CAMELLIA Encrypt and decrypt 49 bytes with zeros and len padding
depends_on:POLARSSL_CAMELLIA_C
enc_dec_buf:POLARSSL_CIPHER_CAMELLIA_128_CBC:CAMELLIA-128-CBC:128:49:POLARSSL_PADDING_ZEROS_AND_LEN
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

@ -125,6 +125,66 @@ DES Encrypt and decrypt 49 bytes with one and zeros padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:49:POLARSSL_PADDING_ONE_AND_ZEROS
DES Encrypt and decrypt 0 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:0:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 1 byte with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:1:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 2 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:2:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 7 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:7:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 8 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:8:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 9 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:9:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 15 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:15:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 16 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:16:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 17 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:17:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 31 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:31:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 32 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:32:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 32 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:33:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 47 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:47:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 48 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:48:POLARSSL_PADDING_ZEROS_AND_LEN
DES Encrypt and decrypt 49 bytes with zeros and len padding
depends_on:POLARSSL_DES_C
enc_dec_buf:POLARSSL_CIPHER_DES_CBC:DES-CBC:56:49:POLARSSL_PADDING_ZEROS_AND_LEN
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

@ -102,3 +102,21 @@ check_padding:POLARSSL_PADDING_ONE_AND_ZEROS:"DABBAD0000":POLARSSL_ERR_CIPHER_IN
Check one and zeros padding #7 (overlong)
check_padding:POLARSSL_PADDING_ONE_AND_ZEROS:"0000000000":POLARSSL_ERR_CIPHER_INVALID_PADDING:4
Check zeros and len padding #1 (correct)
check_padding:POLARSSL_PADDING_ZEROS_AND_LEN:"DABBAD0001":0:4
Check zeros and len padding #2 (correct)
check_padding:POLARSSL_PADDING_ZEROS_AND_LEN:"DABBAD000002":0:4
Check zeros and len padding #3 (correct)
check_padding:POLARSSL_PADDING_ZEROS_AND_LEN:"DABBAD000003":0:3
Check zeros and len padding #4 (correct)
check_padding:POLARSSL_PADDING_ZEROS_AND_LEN:"000003":0:0
Check zeros and len padding #5 (overlong)
check_padding:POLARSSL_PADDING_ZEROS_AND_LEN:"000004":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check zeros and len padding #6 (not enough zeros)
check_padding:POLARSSL_PADDING_ZEROS_AND_LEN:"DABBAD000004":POLARSSL_ERR_CIPHER_INVALID_PADDING:0