1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
// --------------------------------------------------------------------------
//
// File
// Name: CipherAES.cpp
// Purpose: AES cipher description
// Created: 27/4/04
//
// --------------------------------------------------------------------------
#include "Box.h"
// Only available in new versions of openssl
#ifndef HAVE_OLD_SSL
#include <openssl/evp.h>
#define BOX_LIB_CRYPTO_OPENSSL_HEADERS_INCLUDED_TRUE
#include "CipherAES.h"
#include "CipherException.h"
#include "MemLeakFindOn.h"
// --------------------------------------------------------------------------
//
// Function
// Name: CipherAES::CipherAES(CipherDescription::CipherMode, const void *, unsigned int, const void *)
// Purpose: Constructor -- note key material and IV are not copied. KeyLength in bytes.
// Created: 27/4/04
//
// --------------------------------------------------------------------------
CipherAES::CipherAES(CipherDescription::CipherMode Mode, const void *pKey, unsigned int KeyLength, const void *pInitialisationVector)
: CipherDescription(),
mMode(Mode),
mpKey(pKey),
mKeyLength(KeyLength),
mpInitialisationVector(pInitialisationVector)
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: CipherAES::CipherAES(const CipherAES &)
// Purpose: Copy constructor
// Created: 27/4/04
//
// --------------------------------------------------------------------------
CipherAES::CipherAES(const CipherAES &rToCopy)
: CipherDescription(rToCopy),
mMode(rToCopy.mMode),
mpKey(rToCopy.mpKey),
mKeyLength(rToCopy.mKeyLength),
mpInitialisationVector(rToCopy.mpInitialisationVector)
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: ~CipherAES::CipherAES()
// Purpose: Destructor
// Created: 27/4/04
//
// --------------------------------------------------------------------------
CipherAES::~CipherAES()
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: CipherAES::operator=(const CipherAES &)
// Purpose: Assignment operator
// Created: 27/4/04
//
// --------------------------------------------------------------------------
CipherAES &CipherAES::operator=(const CipherAES &rToCopy)
{
CipherDescription::operator=(rToCopy);
mMode = rToCopy.mMode;
mpKey = rToCopy.mpKey;
mKeyLength = rToCopy.mKeyLength;
mpInitialisationVector = rToCopy.mpInitialisationVector;
return *this;
}
// --------------------------------------------------------------------------
//
// Function
// Name: CipherAES::GetCipher()
// Purpose: Returns cipher object
// Created: 27/4/04
//
// --------------------------------------------------------------------------
const EVP_CIPHER *CipherAES::GetCipher() const
{
switch(mMode)
{
case CipherDescription::Mode_ECB:
switch(mKeyLength)
{
case (128/8): return EVP_aes_128_ecb(); break;
case (192/8): return EVP_aes_192_ecb(); break;
case (256/8): return EVP_aes_256_ecb(); break;
default:
THROW_EXCEPTION(CipherException, EVPBadKeyLength)
break;
}
break;
case CipherDescription::Mode_CBC:
switch(mKeyLength)
{
case (128/8): return EVP_aes_128_cbc(); break;
case (192/8): return EVP_aes_192_cbc(); break;
case (256/8): return EVP_aes_256_cbc(); break;
default:
THROW_EXCEPTION(CipherException, EVPBadKeyLength)
break;
}
break;
default:
break;
}
// Unknown!
THROW_EXCEPTION(CipherException, UnknownCipherMode)
}
// --------------------------------------------------------------------------
//
// Function
// Name: CipherAES::SetupParameters(EVP_CIPHER_CTX *)
// Purpose: Set up various parameters for cipher
// Created: 27/4/04
//
// --------------------------------------------------------------------------
void CipherAES::SetupParameters(EVP_CIPHER_CTX *pCipherContext) const
{
ASSERT(pCipherContext != 0);
// Set key (key length is implied)
if(EVP_CipherInit_ex(pCipherContext, NULL, NULL, (unsigned char*)mpKey, (unsigned char*)mpInitialisationVector, -1) != 1)
{
THROW_EXCEPTION(CipherException, EVPInitFailure)
}
}
#endif // n HAVE_OLD_SSL
|