From f7e5bb59044bf5b323580c9342a02d11a85ee478 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Fri, 11 Nov 2011 10:53:37 +0000 Subject: [PATCH] - Added cipher_get_cipher_mode() and cipher_get_cipher_operation() introspection functions (Closes ticket #40) --- include/polarssl/cipher.h | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/include/polarssl/cipher.h b/include/polarssl/cipher.h index f1196da3a..6a0173220 100644 --- a/include/polarssl/cipher.h +++ b/include/polarssl/cipher.h @@ -88,6 +88,7 @@ typedef enum { } cipher_mode_t; typedef enum { + POLARSSL_OPERATION_NONE = -1, POLARSSL_DECRYPT = 0, POLARSSL_ENCRYPT, } operation_t; @@ -146,7 +147,7 @@ typedef struct { /** Full cipher identifier (e.g. POLARSSL_CIPHER_AES_256_CBC) */ cipher_type_t type; - /** Cipher mode (e.g. POLARSSL_CIPHER_MODE_CBC) */ + /** Cipher mode (e.g. POLARSSL_MODE_CBC) */ cipher_mode_t mode; /** Cipher key length, in bits (default length for variable sized ciphers) */ @@ -267,6 +268,23 @@ static inline unsigned int cipher_get_block_size( const cipher_context_t *ctx ) return ctx->cipher_info->block_size; } +/** + * \brief Returns the mode of operation for the cipher. + * (e.g. POLARSSL_MODE_CBC) + * + * \param ctx cipher's context. Must have been initialised. + * + * \return mode of operation, or POLARSSL_MODE_NONE if ctx + * has not been initialised. + */ +static inline cipher_mode_t cipher_get_cipher_mode( const cipher_context_t *ctx ) +{ + if( NULL == ctx || NULL == ctx->cipher_info ) + return POLARSSL_MODE_NONE; + + return ctx->cipher_info->mode; +} + /** * \brief Returns the size of the cipher's IV. * @@ -331,6 +349,23 @@ static inline int cipher_get_key_size ( const cipher_context_t *ctx ) return ctx->key_length; } +/** + * \brief Returns the operation of the given cipher. + * + * \param ctx cipher's context. Must have been initialised. + * + * \return operation (POLARSSL_ENCRYPT or POLARSSL_DECRYPT), + * or POLARSSL_OPERATION_NONE if ctx has not been + * initialised. + */ +static inline operation_t cipher_get_operation( const cipher_context_t *ctx ) +{ + if( NULL == ctx || NULL == ctx->cipher_info ) + return POLARSSL_OPERATION_NONE; + + return ctx->operation; +} + /** * \brief Set the key to use with the given context. *