From 67e6fb70f9a874ccf467529371cd819779b35214 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Sat, 23 Dec 2006 17:54:27 +0000 Subject: [PATCH] Added counter mode support. --- lib/silccrypt/silccipher.c | 52 ++++++++++++++++++++++++----------- lib/silccrypt/silccipher.h | 55 ++++++++++++++++++++++++++++++-------- 2 files changed, 80 insertions(+), 27 deletions(-) diff --git a/lib/silccrypt/silccipher.c b/lib/silccrypt/silccipher.c index e967e831..0aea2dc0 100644 --- a/lib/silccrypt/silccipher.c +++ b/lib/silccrypt/silccipher.c @@ -34,30 +34,42 @@ SilcDList silc_cipher_list = NULL; #endif /* SILC_EPOC */ /* Macro to define cipher to cipher list */ -#define SILC_CIPHER_API_DEF(name, cipher, keylen, blocklen, ivlen) \ +#define SILC_CIPHER_API_DEF(name, cipher, keylen, blocklen, ivlen, mode) \ { name, silc_##cipher##_set_key, silc_##cipher##_encrypt, \ silc_##cipher##_decrypt, silc_##cipher##_context_len, \ - keylen, blocklen, ivlen } + keylen, blocklen, ivlen, mode } /* Static list of ciphers for silc_cipher_register_default(). */ const SilcCipherObject silc_default_ciphers[] = { - SILC_CIPHER_API_DEF("aes-256-ctr", aes_ctr, 256, 16, 16), - SILC_CIPHER_API_DEF("aes-192-ctr", aes_ctr, 192, 16, 16), - SILC_CIPHER_API_DEF("aes-128-ctr", aes_ctr, 128, 16, 16), - SILC_CIPHER_API_DEF("aes-256-cbc", aes_cbc, 256, 16, 16), - SILC_CIPHER_API_DEF("aes-192-cbc", aes_cbc, 192, 16, 16), - SILC_CIPHER_API_DEF("aes-128-cbc", aes_cbc, 128, 16, 16), - SILC_CIPHER_API_DEF("twofish-256-cbc", twofish_cbc, 256, 16, 16), - SILC_CIPHER_API_DEF("twofish-192-cbc", twofish_cbc, 192, 16, 16), - SILC_CIPHER_API_DEF("twofish-128-cbc", twofish_cbc, 128, 16, 16), - SILC_CIPHER_API_DEF("cast-256-cbc", cast_cbc, 256, 16, 16), - SILC_CIPHER_API_DEF("cast-192-cbc", cast_cbc, 192, 16, 16), - SILC_CIPHER_API_DEF("cast-128-cbc", cast_cbc, 128, 16, 16), + SILC_CIPHER_API_DEF("aes-256-ctr", aes_ctr, 256, 16, 16, + SILC_CIPHER_MODE_CTR), + SILC_CIPHER_API_DEF("aes-192-ctr", aes_ctr, 192, 16, 16, + SILC_CIPHER_MODE_CTR), + SILC_CIPHER_API_DEF("aes-128-ctr", aes_ctr, 128, 16, 16, + SILC_CIPHER_MODE_CTR), + SILC_CIPHER_API_DEF("aes-256-cbc", aes_cbc, 256, 16, 16, + SILC_CIPHER_MODE_CBC), + SILC_CIPHER_API_DEF("aes-192-cbc", aes_cbc, 192, 16, 16, + SILC_CIPHER_MODE_CBC), + SILC_CIPHER_API_DEF("aes-128-cbc", aes_cbc, 128, 16, 16, + SILC_CIPHER_MODE_CBC), + SILC_CIPHER_API_DEF("twofish-256-cbc", twofish_cbc, 256, 16, 16, + SILC_CIPHER_MODE_CBC), + SILC_CIPHER_API_DEF("twofish-192-cbc", twofish_cbc, 192, 16, 16, + SILC_CIPHER_MODE_CBC), + SILC_CIPHER_API_DEF("twofish-128-cbc", twofish_cbc, 128, 16, 16, + SILC_CIPHER_MODE_CBC), + SILC_CIPHER_API_DEF("cast-256-cbc", cast_cbc, 256, 16, 16, + SILC_CIPHER_MODE_CBC), + SILC_CIPHER_API_DEF("cast-192-cbc", cast_cbc, 192, 16, 16, + SILC_CIPHER_MODE_CBC), + SILC_CIPHER_API_DEF("cast-128-cbc", cast_cbc, 128, 16, 16, + SILC_CIPHER_MODE_CBC), #ifdef SILC_DEBUG - SILC_CIPHER_API_DEF("none", none, 0, 0, 0), + SILC_CIPHER_API_DEF("none", none, 0, 0, 0, 0), #endif /* SILC_DEBUG */ - { NULL, NULL, 0, 0, 0 } + { NULL, NULL, 0, 0, 0, 0 } }; /* Register a new cipher into SILC. This is used at the initialization of @@ -91,6 +103,7 @@ SilcBool silc_cipher_register(const SilcCipherObject *cipher) new->encrypt = cipher->encrypt; new->decrypt = cipher->decrypt; new->context_len = cipher->context_len; + new->mode = cipher->mode; /* Add to list */ if (silc_cipher_list == NULL) @@ -355,3 +368,10 @@ const char *silc_cipher_get_name(SilcCipher cipher) { return (const char *)cipher->cipher->name; } + +/* Returns cipher mode */ + +SilcCipherMode silc_cipher_get_mode(SilcCipher cipher) +{ + return cipher->cipher->mode; +} diff --git a/lib/silccrypt/silccipher.h b/lib/silccrypt/silccipher.h index 3c557f23..25083699 100644 --- a/lib/silccrypt/silccipher.h +++ b/lib/silccrypt/silccipher.h @@ -55,9 +55,10 @@ typedef struct { SilcBool (*decrypt)(void *, const unsigned char *, unsigned char *, SilcUInt32, unsigned char *); SilcUInt32 (*context_len)(); - unsigned int key_len : 12; - unsigned int block_len : 10; - unsigned int iv_len : 10; + unsigned int key_len : 10; + unsigned int block_len : 8; + unsigned int iv_len : 8; + unsigned int mode : 6; } SilcCipherObject; #define SILC_CIPHER_MAX_IV_SIZE 16 @@ -82,31 +83,49 @@ extern DLLAPI const SilcCipherObject silc_default_ciphers[]; #define SILC_CIPHER_SIM_ENCRYPT "encrypt" #define SILC_CIPHER_SIM_DECRYPT "decrypt" #define SILC_CIPHER_SIM_CONTEXT_LEN "context_len" -#define SILC_CIPHER_SIM_SET_IV "set_iv" /* These macros can be used to implement the SILC Crypto API and to avoid errors in the API these macros should be used always. */ #define SILC_CIPHER_API_SET_KEY(cipher) \ -SilcBool silc_##cipher##_set_key(void *context, \ +SilcBool silc_##cipher##_set_key(void *context, \ const unsigned char *key, \ SilcUInt32 keylen, \ SilcBool encryption) -#define SILC_CIPHER_API_ENCRYPT(cipher) \ -SilcBool silc_##cipher##_encrypt(void *context, \ +#define SILC_CIPHER_API_ENCRYPT(cipher) \ +SilcBool silc_##cipher##_encrypt(void *context, \ const unsigned char *src, \ unsigned char *dst, \ SilcUInt32 len, \ unsigned char *iv) #define SILC_CIPHER_API_DECRYPT(cipher) \ -SilcBool silc_##cipher##_decrypt(void *context, \ +SilcBool silc_##cipher##_decrypt(void *context, \ const unsigned char *src, \ unsigned char *dst, \ SilcUInt32 len, \ unsigned char *iv) #define SILC_CIPHER_API_CONTEXT_LEN(cipher) \ SilcUInt32 silc_##cipher##_context_len() -#define SILC_CIPHER_API_SET_IV(cipher) \ -SilcBool silc_##cipher##_set_iv(void *context, const unsigned char *iv) + +/****d* silccrypt/SilcCipherAPI/SilcCipherMode + * + * NAME + * + * typedef enum { ... } SilcCipherMode; + * + * DESCRIPTION + * + * Cipher modes. + * + * SOURCE + */ +typedef enum { + SILC_CIPHER_MODE_ECB = 1, /* ECB mode */ + SILC_CIPHER_MODE_CBC = 2, /* CBC mode */ + SILC_CIPHER_MODE_CTR = 3, /* CTR mode */ + SILC_CIPHER_MODE_CFB = 4, /* CFB mode */ + SILC_CIPHER_MODE_OFB = 5, /* OFB mode */ +} SilcCipherMode; +/***/ /* Prototypes */ @@ -303,7 +322,8 @@ void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv); * DESCRIPTION * * Returns the IV (initial vector) of the cipher. The returned - * pointer must not be freed by the caller. + * pointer must not be freed by the caller. If the caller modifies + * the returned pointer the IV inside cipher is also modified. * ***/ unsigned char *silc_cipher_get_iv(SilcCipher cipher); @@ -360,4 +380,17 @@ SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher); ***/ const char *silc_cipher_get_name(SilcCipher cipher); +/****f* silccrypt/SilcCipherAPI/silc_cipher_get_mode + * + * SYNOPSIS + * + * SilcCipherMode silc_cipher_get_mode(SilcCipher cipher); + * + * DESCRIPTION + * + * Returns the cipher mode. + * + ***/ +SilcCipherMode silc_cipher_get_mode(SilcCipher cipher); + #endif /* SILCCIPHER_H */ -- 2.24.0