Merged GCM refactoring into development

GCM is now independent of AES and can be used as a mode for any
cipher-layer supported 128-bit based block cipher
This commit is contained in:
Paul Bakker 2013-09-10 14:41:05 +02:00
commit 36b7e1efe7
9 changed files with 850 additions and 28 deletions

View file

@ -70,6 +70,9 @@ typedef enum {
typedef enum {
POLARSSL_CIPHER_NONE = 0,
POLARSSL_CIPHER_NULL,
POLARSSL_CIPHER_AES_128_ECB,
POLARSSL_CIPHER_AES_192_ECB,
POLARSSL_CIPHER_AES_256_ECB,
POLARSSL_CIPHER_AES_128_CBC,
POLARSSL_CIPHER_AES_192_CBC,
POLARSSL_CIPHER_AES_256_CBC,
@ -82,6 +85,9 @@ typedef enum {
POLARSSL_CIPHER_AES_128_GCM,
POLARSSL_CIPHER_AES_192_GCM,
POLARSSL_CIPHER_AES_256_GCM,
POLARSSL_CIPHER_CAMELLIA_128_ECB,
POLARSSL_CIPHER_CAMELLIA_192_ECB,
POLARSSL_CIPHER_CAMELLIA_256_ECB,
POLARSSL_CIPHER_CAMELLIA_128_CBC,
POLARSSL_CIPHER_CAMELLIA_192_CBC,
POLARSSL_CIPHER_CAMELLIA_256_CBC,
@ -91,9 +97,13 @@ typedef enum {
POLARSSL_CIPHER_CAMELLIA_128_CTR,
POLARSSL_CIPHER_CAMELLIA_192_CTR,
POLARSSL_CIPHER_CAMELLIA_256_CTR,
POLARSSL_CIPHER_DES_ECB,
POLARSSL_CIPHER_DES_CBC,
POLARSSL_CIPHER_DES_EDE_ECB,
POLARSSL_CIPHER_DES_EDE_CBC,
POLARSSL_CIPHER_DES_EDE3_ECB,
POLARSSL_CIPHER_DES_EDE3_CBC,
POLARSSL_CIPHER_BLOWFISH_ECB,
POLARSSL_CIPHER_BLOWFISH_CBC,
POLARSSL_CIPHER_BLOWFISH_CFB64,
POLARSSL_CIPHER_BLOWFISH_CTR,
@ -102,6 +112,7 @@ typedef enum {
typedef enum {
POLARSSL_MODE_NONE = 0,
POLARSSL_MODE_ECB,
POLARSSL_MODE_CBC,
POLARSSL_MODE_CFB,
POLARSSL_MODE_OFB,
@ -145,6 +156,10 @@ typedef struct {
/** Base Cipher type (e.g. POLARSSL_CIPHER_ID_AES) */
cipher_id_t cipher;
/** Encrypt using ECB */
int (*ecb_func)( void *ctx, operation_t mode,
const unsigned char *input, unsigned char *output );
/** Encrypt using CBC */
int (*cbc_func)( void *ctx, operation_t mode, size_t length, unsigned char *iv,
const unsigned char *input, unsigned char *output );
@ -270,6 +285,22 @@ const cipher_info_t *cipher_info_from_string( const char *cipher_name );
*/
const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type );
/**
* \brief Returns the cipher information structure associated
* with the given cipher id, key size and mode.
*
* \param cipher_id Id of the cipher to search for
* (e.g. POLARSSL_CIPHER_ID_AES)
* \param key_length Length of the key in bits
* \param mode Cipher mode (e.g. POLARSSL_MODE_CBC)
*
* \return the cipher information structure associated with the
* given cipher_type, or NULL if not found.
*/
const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
int key_length,
const cipher_mode_t mode );
/**
* \brief Initialises and fills the cipher context structure with
* the appropriate values.
@ -497,6 +528,8 @@ int cipher_update_ad( cipher_context_t *ctx,
* that cannot be written immediately will either be added
* to the next block, or flushed when cipher_final is
* called.
* Exception: for POLARSSL_MODE_ECB, expects single block
* in size (e.g. 16 bytes for AES)
*
* \param ctx generic cipher context
* \param input buffer holding the input data

View file

@ -38,6 +38,10 @@ extern "C" {
#if defined(POLARSSL_AES_C)
extern const cipher_info_t aes_128_ecb_info;
extern const cipher_info_t aes_192_ecb_info;
extern const cipher_info_t aes_256_ecb_info;
extern const cipher_info_t aes_128_cbc_info;
extern const cipher_info_t aes_192_cbc_info;
extern const cipher_info_t aes_256_cbc_info;
@ -64,6 +68,10 @@ extern const cipher_info_t aes_256_gcm_info;
#if defined(POLARSSL_CAMELLIA_C)
extern const cipher_info_t camellia_128_ecb_info;
extern const cipher_info_t camellia_192_ecb_info;
extern const cipher_info_t camellia_256_ecb_info;
extern const cipher_info_t camellia_128_cbc_info;
extern const cipher_info_t camellia_192_cbc_info;
extern const cipher_info_t camellia_256_cbc_info;
@ -84,6 +92,10 @@ extern const cipher_info_t camellia_256_ctr_info;
#if defined(POLARSSL_DES_C)
extern const cipher_info_t des_ecb_info;
extern const cipher_info_t des_ede_ecb_info;
extern const cipher_info_t des_ede3_ecb_info;
extern const cipher_info_t des_cbc_info;
extern const cipher_info_t des_ede_cbc_info;
extern const cipher_info_t des_ede3_cbc_info;
@ -91,6 +103,7 @@ extern const cipher_info_t des_ede3_cbc_info;
#endif /* defined(POLARSSL_DES_C) */
#if defined(POLARSSL_BLOWFISH_C)
extern const cipher_info_t blowfish_ecb_info;
extern const cipher_info_t blowfish_cbc_info;
#if defined(POLARSSL_CIPHER_MODE_CFB)

View file

@ -1,7 +1,7 @@
/**
* \file gcm.h
*
* \brief Galois/Counter mode for AES
* \brief Galois/Counter mode for 128-bit block ciphers
*
* Copyright (C) 2006-2013, Brainspark B.V.
*
@ -27,7 +27,7 @@
#ifndef POLARSSL_GCM_H
#define POLARSSL_GCM_H
#include "aes.h"
#include "cipher.h"
#ifdef _MSC_VER
#include <basetsd.h>
@ -50,7 +50,7 @@ extern "C" {
* \brief GCM context structure
*/
typedef struct {
aes_context aes_ctx; /*!< AES context used */
cipher_context_t cipher_ctx;/*!< cipher context used */
uint64_t HL[16]; /*!< Precalculated HTable */
uint64_t HH[16]; /*!< Precalculated HTable */
uint64_t len; /*!< Total data length */
@ -66,15 +66,17 @@ gcm_context;
* \brief GCM initialization (encryption)
*
* \param ctx GCM context to be initialized
* \param cipher cipher to use (a 128-bit block cipher)
* \param key encryption key
* \param keysize must be 128, 192 or 256
*
* \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
* \return 0 if successful, or a cipher specific error code
*/
int gcm_init( gcm_context *ctx, const unsigned char *key, unsigned int keysize );
int gcm_init( gcm_context *ctx, cipher_id_t cipher, const unsigned char *key,
unsigned int keysize );
/**
* \brief GCM buffer encryption/decryption using AES
* \brief GCM buffer encryption/decryption using a block cipher
*
* \note On encryption, the output buffer can be the same as the input buffer.
* On decryption, the output buffer cannot be the same as input buffer.
@ -108,7 +110,7 @@ int gcm_crypt_and_tag( gcm_context *ctx,
unsigned char *tag );
/**
* \brief GCM buffer authenticated decryption using AES
* \brief GCM buffer authenticated decryption using a block cipher
*
* \note On decryption, the output buffer cannot be the same as input buffer.
* If buffers overlap, the output buffer must trail at least 8 bytes