Add cipher_set_padding() (no effect yet)

Fix pattern in tests/.gitignore along the way.
This commit is contained in:
Manuel Pégourié-Gonnard 2013-07-24 18:05:00 +02:00 committed by Paul Bakker
parent 0f2f0bfc87
commit d5fdcaf9e5
7 changed files with 111 additions and 1 deletions

View file

@ -104,6 +104,10 @@ typedef enum {
POLARSSL_MODE_STREAM,
} cipher_mode_t;
typedef enum {
POLARSSL_PADDING_PKCS7 = 0, /**< PKCS7 padding (default) */
} cipher_padding_t;
typedef enum {
POLARSSL_OPERATION_NONE = -1,
POLARSSL_DECRYPT = 0,
@ -398,6 +402,18 @@ static inline operation_t cipher_get_operation( const cipher_context_t *ctx )
int cipher_setkey( cipher_context_t *ctx, const unsigned char *key, int key_length,
const operation_t operation );
/**
* \brief Set padding mode, for cipher modes that use padding.
* (Default: PKCS7 padding.)
*
* \param ctx generic cipher context
* \param mode padding mode
*
* \returns 0 on success, POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
* if parameters verification fails.
*/
int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode );
/**
* \brief Reset the given context, setting the IV to iv
*

View file

@ -368,6 +368,18 @@ int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
}
int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
{
if( NULL == ctx ||
POLARSSL_MODE_CBC != ctx->cipher_info->mode ||
POLARSSL_PADDING_PKCS7 != mode )
{
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
}
return 0;
}
int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
{
if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )

2
tests/.gitignore vendored
View file

@ -1,2 +1,2 @@
test_suite*
/test_suite*
data_files/mpi_write

View file

@ -44,6 +44,7 @@ add_test_suite(cipher cipher.blowfish)
add_test_suite(cipher cipher.camellia)
add_test_suite(cipher cipher.des)
add_test_suite(cipher cipher.null)
add_test_suite(cipher cipher.padding)
add_test_suite(ctr_drbg)
add_test_suite(debug)
add_test_suite(des)

View file

@ -30,6 +30,7 @@ APPS = test_suite_aes.ecb test_suite_aes.cbc \
test_suite_cipher.blowfish \
test_suite_cipher.camellia \
test_suite_cipher.des test_suite_cipher.null \
test_suite_cipher.padding \
test_suite_ctr_drbg test_suite_debug \
test_suite_des test_suite_dhm \
test_suite_ecdh test_suite_ecdsa \
@ -89,6 +90,10 @@ test_suite_cipher.null.c : suites/test_suite_cipher.function suites/test_suite_c
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.null
test_suite_cipher.padding.c : suites/test_suite_cipher.function suites/test_suite_cipher.padding.data scripts/generate_code.pl suites/helpers.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.padding
test_suite_gcm.decrypt_128.c : suites/test_suite_gcm.function suites/test_suite_gcm.decrypt_128.data scripts/generate_code.pl suites/helpers.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_gcm test_suite_gcm.decrypt_128
@ -169,6 +174,10 @@ test_suite_cipher.null: test_suite_cipher.null.c ../library/libpolarssl.a
echo " CC $@.c"
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
test_suite_cipher.padding: test_suite_cipher.padding.c ../library/libpolarssl.a
echo " CC $@.c"
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
test_suite_ctr_drbg: test_suite_ctr_drbg.c ../library/libpolarssl.a
echo " CC $@.c"
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@

View file

@ -252,6 +252,19 @@ enc_dec_buf_multipart:cipher_id:key_len:first_length:second_length:
TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
END_CASE
BEGIN_CASE
set_padding:cipher_id:pad_mode:ret:
const cipher_info_t *cipher_info;
cipher_context_t ctx;
cipher_info = cipher_info_from_type( {cipher_id} );
TEST_ASSERT( NULL != cipher_info );
TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
TEST_ASSERT( {ret} == cipher_set_padding_mode( &ctx, {pad_mode} ) );
TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
END_CASE
BEGIN_CASE
cipher_selftest:

View file

@ -0,0 +1,59 @@
Set padding with AES-CBC
depends_on:POLARSSL_AES_C
set_padding:POLARSSL_CIPHER_AES_128_CBC:POLARSSL_PADDING_PKCS7:0
Set padding with AES-CFB
depends_on:POLARSSL_AES_C:POLARSSL_CIPHER_MODE_CFB
set_padding:POLARSSL_CIPHER_AES_128_CFB128:POLARSSL_PADDING_PKCS7:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Set padding with AES-CTR
depends_on:POLARSSL_AES_C:POLARSSL_CIPHER_MODE_CTR
set_padding:POLARSSL_CIPHER_AES_128_CTR:POLARSSL_PADDING_PKCS7:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Set padding with CAMELLIA-CBC
depends_on:POLARSSL_CAMELLIA_C
set_padding:POLARSSL_CIPHER_CAMELLIA_128_CBC:POLARSSL_PADDING_PKCS7:0
Set padding with CAMELLIA-CFB
depends_on:POLARSSL_CAMELLIA_C:POLARSSL_CIPHER_MODE_CFB
set_padding:POLARSSL_CIPHER_CAMELLIA_128_CFB128:POLARSSL_PADDING_PKCS7:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Set padding with CAMELLIA-CTR
depends_on:POLARSSL_CAMELLIA_C:POLARSSL_CIPHER_MODE_CTR
set_padding:POLARSSL_CIPHER_CAMELLIA_128_CTR:POLARSSL_PADDING_PKCS7:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Set padding with DES-CBC
depends_on:POLARSSL_DES_C
set_padding:POLARSSL_CIPHER_DES_CBC:POLARSSL_PADDING_PKCS7:0
Set padding with BLOWFISH-CBC
depends_on:POLARSSL_BLOWFISH_C
set_padding:POLARSSL_CIPHER_BLOWFISH_CBC:POLARSSL_PADDING_PKCS7:0
Set padding with BLOWFISH-CFB
depends_on:POLARSSL_BLOWFISH_C:POLARSSL_CIPHER_MODE_CFB
set_padding:POLARSSL_CIPHER_BLOWFISH_CFB64:POLARSSL_PADDING_PKCS7:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Set padding with BLOWFISH-CTR
depends_on:POLARSSL_BLOWFISH_C:POLARSSL_CIPHER_MODE_CTR
set_padding:POLARSSL_CIPHER_BLOWFISH_CTR:POLARSSL_PADDING_PKCS7:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Set padding with NULL
depends_on:POLARSSL_CIPHER_NULL_CIPHER
set_padding:POLARSSL_CIPHER_NULL:POLARSSL_PADDING_PKCS7:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Set non-existent padding with AES-CBC
depends_on:POLARSSL_AES_C
set_padding:POLARSSL_CIPHER_AES_128_CBC:-1:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Set non-existent padding with CAMELLIA-CBC
depends_on:POLARSSL_CAMELLIA_C
set_padding:POLARSSL_CIPHER_CAMELLIA_128_CBC:-1:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Set non-existent padding with DES-CBC
depends_on:POLARSSL_DES_C
set_padding:POLARSSL_CIPHER_DES_CBC:-1:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA
Set non-existent padding with BLOWFISH-CBC
depends_on:POLARSSL_BLOWFISH_C
set_padding:POLARSSL_CIPHER_BLOWFISH_CBC:-1:POLARSSL_ERR_CIPHER_BAD_INPUT_DATA