Add arc4 support in the cipher layer

This commit is contained in:
Manuel Pégourié-Gonnard 2013-08-28 13:50:42 +02:00
parent f451bac000
commit 37e230c022
6 changed files with 183 additions and 5 deletions

View File

@ -151,6 +151,10 @@ typedef struct {
int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, unsigned char *nonce_counter,
unsigned char *stream_block, const unsigned char *input, unsigned char *output );
/** Encrypt using STREAM */
int (*stream_func)( void *ctx, size_t length,
const unsigned char *input, unsigned char *output );
/** Set key for encryption purposes */
int (*setkey_enc_func)( void *ctx, const unsigned char *key, unsigned int key_length);

View File

@ -36,6 +36,10 @@
#include <stdlib.h>
#if defined(POLARSSL_ARC4_C)
#define POLARSSL_CIPHER_MODE_STREAM
#endif
#if defined _MSC_VER && !defined strcasecmp
#define strcasecmp _stricmp
#endif
@ -61,6 +65,10 @@ static const int supported_ciphers[] = {
#endif /* defined(POLARSSL_AES_C) */
#if defined(POLARSSL_ARC4_C)
POLARSSL_CIPHER_ARC4_128,
#endif
#if defined(POLARSSL_CAMELLIA_C)
POLARSSL_CIPHER_CAMELLIA_128_CBC,
POLARSSL_CIPHER_CAMELLIA_192_CBC,
@ -279,6 +287,11 @@ const cipher_info_t *cipher_info_from_string( const char *cipher_name )
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
#endif
#if defined(POLARSSL_ARC4_C)
if( !strcasecmp( "ARC4-128", cipher_name ) )
return( cipher_info_from_type( POLARSSL_CIPHER_ARC4_128 ) );
#endif
#if defined(POLARSSL_DES_C)
if( !strcasecmp( "DES-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
@ -527,6 +540,21 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
}
#endif
#if defined(POLARSSL_CIPHER_MODE_STREAM)
if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
{
if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
ilen, input, output ) ) )
{
return ret;
}
*olen = ilen;
return 0;
}
#endif
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
}
@ -697,6 +725,7 @@ int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
POLARSSL_MODE_STREAM == ctx->cipher_info->mode ||
POLARSSL_MODE_NULL == ctx->cipher_info->mode )
{
return 0;

View File

@ -37,6 +37,10 @@
#include "polarssl/aes.h"
#endif
#if defined(POLARSSL_ARC4_C)
#include "polarssl/arc4.h"
#endif
#if defined(POLARSSL_CAMELLIA_C)
#include "polarssl/camellia.h"
#endif
@ -129,6 +133,7 @@ const cipher_base_t aes_info = {
aes_crypt_cbc_wrap,
aes_crypt_cfb128_wrap,
aes_crypt_ctr_wrap,
NULL,
aes_setkey_enc_wrap,
aes_setkey_dec_wrap,
aes_ctx_alloc,
@ -324,6 +329,7 @@ const cipher_base_t camellia_info = {
camellia_crypt_cbc_wrap,
camellia_crypt_cfb128_wrap,
camellia_crypt_ctr_wrap,
NULL,
camellia_setkey_enc_wrap,
camellia_setkey_dec_wrap,
camellia_ctx_alloc,
@ -531,6 +537,7 @@ const cipher_base_t des_info = {
des_crypt_cbc_wrap,
des_crypt_cfb128_wrap,
des_crypt_ctr_wrap,
NULL,
des_setkey_enc_wrap,
des_setkey_dec_wrap,
des_ctx_alloc,
@ -552,6 +559,7 @@ const cipher_base_t des_ede_info = {
des3_crypt_cbc_wrap,
des_crypt_cfb128_wrap,
des_crypt_ctr_wrap,
NULL,
des3_set2key_enc_wrap,
des3_set2key_dec_wrap,
des3_ctx_alloc,
@ -573,6 +581,7 @@ const cipher_base_t des_ede3_info = {
des3_crypt_cbc_wrap,
des_crypt_cfb128_wrap,
des_crypt_ctr_wrap,
NULL,
des3_set3key_enc_wrap,
des3_set3key_dec_wrap,
des3_ctx_alloc,
@ -661,6 +670,7 @@ const cipher_base_t blowfish_info = {
blowfish_crypt_cbc_wrap,
blowfish_crypt_cfb64_wrap,
blowfish_crypt_ctr_wrap,
NULL,
blowfish_setkey_enc_wrap,
blowfish_setkey_dec_wrap,
blowfish_ctx_alloc,
@ -703,15 +713,28 @@ const cipher_info_t blowfish_ctr_info = {
#endif /* POLARSSL_BLOWFISH_C */
#if defined(POLARSSL_ARC4_C)
static void * arc4_ctx_alloc( void )
static int arc4_crypt_stream_wrap( void *ctx, size_t length,
const unsigned char *input,
unsigned char *output )
{
return (void *) 1;
return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
}
static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
unsigned int key_length )
{
arc4_setup( (arc4_context *) ctx, key, key_length );
return( 0 );
}
static void * arc4_ctx_alloc( void )
{
return polarssl_malloc( sizeof( arc4_context ) );
}
static void arc4_ctx_free( void *ctx )
{
((void) ctx);
polarssl_free( ctx );
}
const cipher_base_t arc4_base_info = {
@ -719,8 +742,9 @@ const cipher_base_t arc4_base_info = {
NULL,
NULL,
NULL,
NULL,
NULL,
arc4_crypt_stream_wrap,
arc4_setkey_wrap,
arc4_setkey_wrap,
arc4_ctx_alloc,
arc4_ctx_free
};
@ -755,6 +779,7 @@ const cipher_base_t null_base_info = {
NULL,
NULL,
NULL,
NULL,
null_ctx_alloc,
null_ctx_free
};

View File

@ -40,6 +40,7 @@ add_test_suite(base64)
add_test_suite(blowfish)
add_test_suite(camellia)
add_test_suite(cipher cipher.aes)
add_test_suite(cipher cipher.arc4)
add_test_suite(cipher cipher.blowfish)
add_test_suite(cipher cipher.camellia)
add_test_suite(cipher cipher.des)

View File

@ -27,6 +27,7 @@ APPS = test_suite_aes.ecb test_suite_aes.cbc \
test_suite_arc4 \
test_suite_base64 test_suite_blowfish \
test_suite_camellia test_suite_cipher.aes \
test_suite_cipher.arc4 \
test_suite_cipher.blowfish \
test_suite_cipher.camellia \
test_suite_cipher.des test_suite_cipher.null \
@ -74,6 +75,10 @@ test_suite_cipher.aes.c : suites/test_suite_cipher.function suites/test_suite_ci
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.aes
test_suite_cipher.arc4.c : suites/test_suite_cipher.function suites/test_suite_cipher.arc4.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.arc4
test_suite_cipher.blowfish.c : suites/test_suite_cipher.function suites/test_suite_cipher.blowfish.data scripts/generate_code.pl suites/helpers.function suites/main_test.function
echo " Generate $@"
scripts/generate_code.pl suites test_suite_cipher test_suite_cipher.blowfish
@ -158,6 +163,10 @@ test_suite_cipher.aes: test_suite_cipher.aes.c ../library/libpolarssl.a
echo " CC $@.c"
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
test_suite_cipher.arc4: test_suite_cipher.arc4.c ../library/libpolarssl.a
echo " CC $@.c"
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
test_suite_cipher.blowfish: test_suite_cipher.blowfish.c ../library/libpolarssl.a
echo " CC $@.c"
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@

View File

@ -0,0 +1,110 @@
Cipher Selftest
depends_on:POLARSSL_SELF_TEST
cipher_selftest:
Decrypt empty buffer
dec_empty_buf:
ARC4 Encrypt and decrypt 0 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:0:-1
ARC4 Encrypt and decrypt 1 byte
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:1:-1
ARC4 Encrypt and decrypt 2 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:2:-1
ARC4 Encrypt and decrypt 7 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:7:-1
ARC4 Encrypt and decrypt 8 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:8:-1
ARC4 Encrypt and decrypt 9 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:9:-1
ARC4 Encrypt and decrypt 15 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:15:-1
ARC4 Encrypt and decrypt 16 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:16:-1
ARC4 Encrypt and decrypt 17 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:17:-1
ARC4 Encrypt and decrypt 31 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:31:-1
ARC4 Encrypt and decrypt 32 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:32:-1
ARC4 Encrypt and decrypt 32 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:33:-1
ARC4 Encrypt and decrypt 47 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:47:-1
ARC4 Encrypt and decrypt 48 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:48:-1
ARC4 Encrypt and decrypt 49 bytes
depends_on:POLARSSL_ARC4_C
enc_dec_buf:POLARSSL_CIPHER_ARC4_128:"ARC4-128":128:49:-1
ARC4 Encrypt and decrypt 0 bytes in multiple parts
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:0:0:
ARC4 Encrypt and decrypt 1 bytes in multiple parts 1
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:1:0:
ARC4 Encrypt and decrypt 1 bytes in multiple parts 2
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:0:1:
ARC4 Encrypt and decrypt 16 bytes in multiple parts 1
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:16:0:
ARC4 Encrypt and decrypt 16 bytes in multiple parts 2
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:0:16:
ARC4 Encrypt and decrypt 16 bytes in multiple parts 3
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:1:15:
ARC4 Encrypt and decrypt 16 bytes in multiple parts 4
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:15:1:
ARC4 Encrypt and decrypt 22 bytes in multiple parts 1
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:15:7:
ARC4 Encrypt and decrypt 22 bytes in multiple parts 1
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:16:6:
ARC4 Encrypt and decrypt 22 bytes in multiple parts 1
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:17:6:
ARC4 Encrypt and decrypt 32 bytes in multiple parts 1
depends_on:POLARSSL_ARC4_C
enc_dec_buf_multipart:POLARSSL_CIPHER_ARC4_128:128:16:16: