source: box/trunk/lib/crypto/CipherAES.cpp @ 217

Revision 217, 4.1 KB checked in by martin, 6 years ago (diff)

Set svn:eol-style as appropriate for all files

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