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

Revision 217, 5.3 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:    CipherBlowfish.cpp
5//              Purpose: Blowfish cipher description
6//              Created: 1/12/03
7//
8// --------------------------------------------------------------------------
9
10#include "Box.h"
11
12#include <openssl/evp.h>
13
14#ifdef HAVE_OLD_SSL
15        #include <string.h>
16        #include <strings.h>
17#endif
18
19#define BOX_LIB_CRYPTO_OPENSSL_HEADERS_INCLUDED_TRUE
20
21#include "CipherBlowfish.h"
22#include "CipherException.h"
23
24#include "MemLeakFindOn.h"
25
26// --------------------------------------------------------------------------
27//
28// Function
29//              Name:    CipherBlowfish::CipherBlowfish(CipherDescription::CipherMode, const void *, unsigned int, const void *)
30//              Purpose: Constructor -- note key material and IV are not copied. KeyLength in bytes.
31//              Created: 1/12/03
32//
33// --------------------------------------------------------------------------
34CipherBlowfish::CipherBlowfish(CipherDescription::CipherMode Mode, const void *pKey, unsigned int KeyLength, const void *pInitialisationVector)
35        : CipherDescription(),
36          mMode(Mode)
37#ifndef HAVE_OLD_SSL
38        , mpKey(pKey),
39          mKeyLength(KeyLength),
40          mpInitialisationVector(pInitialisationVector)
41{
42}
43#else
44{
45        mKey.assign((const char *)pKey, KeyLength);
46        if(pInitialisationVector == 0)
47        {
48                bzero(mInitialisationVector, sizeof(mInitialisationVector));
49        }
50        else
51        {
52                ::memcpy(mInitialisationVector, pInitialisationVector, sizeof(mInitialisationVector));
53        }
54}
55#endif
56
57
58// --------------------------------------------------------------------------
59//
60// Function
61//              Name:    CipherBlowfish::CipherBlowfish(const CipherBlowfish &)
62//              Purpose: Copy constructor
63//              Created: 1/12/03
64//
65// --------------------------------------------------------------------------
66CipherBlowfish::CipherBlowfish(const CipherBlowfish &rToCopy)
67        : CipherDescription(rToCopy),
68          mMode(rToCopy.mMode),
69#ifndef HAVE_OLD_SSL
70          mpKey(rToCopy.mpKey),
71          mKeyLength(rToCopy.mKeyLength),
72          mpInitialisationVector(rToCopy.mpInitialisationVector)
73{
74}
75#else
76          mKey(rToCopy.mKey)
77{
78        ::memcpy(mInitialisationVector, rToCopy.mInitialisationVector, sizeof(mInitialisationVector));
79}
80#endif
81
82
83#ifdef HAVE_OLD_SSL
84// Hack functions to support old OpenSSL API
85CipherDescription *CipherBlowfish::Clone() const
86{
87        return new CipherBlowfish(*this);
88}
89void CipherBlowfish::SetIV(const void *pIV)
90{
91        if(pIV == 0)
92        {
93                bzero(mInitialisationVector, sizeof(mInitialisationVector));
94        }
95        else
96        {
97                ::memcpy(mInitialisationVector, pIV, sizeof(mInitialisationVector));
98        }
99}
100#endif
101
102
103// --------------------------------------------------------------------------
104//
105// Function
106//              Name:    ~CipherBlowfish::CipherBlowfish()
107//              Purpose: Destructor
108//              Created: 1/12/03
109//
110// --------------------------------------------------------------------------
111CipherBlowfish::~CipherBlowfish()
112{
113#ifdef HAVE_OLD_SSL
114        // Zero copy of key
115        for(unsigned int l = 0; l < mKey.size(); ++l)
116        {
117                mKey[l] = '\0';
118        }
119#endif
120}
121
122
123
124// --------------------------------------------------------------------------
125//
126// Function
127//              Name:    CipherBlowfish::operator=(const CipherBlowfish &)
128//              Purpose: Assignment operator
129//              Created: 1/12/03
130//
131// --------------------------------------------------------------------------
132CipherBlowfish &CipherBlowfish::operator=(const CipherBlowfish &rToCopy)
133{
134        CipherDescription::operator=(rToCopy);
135
136        mMode = rToCopy.mMode;
137#ifndef HAVE_OLD_SSL
138        mpKey = rToCopy.mpKey;
139        mKeyLength = rToCopy.mKeyLength;
140        mpInitialisationVector = rToCopy.mpInitialisationVector;
141#else
142        mKey = rToCopy.mKey;
143        ::memcpy(mInitialisationVector, rToCopy.mInitialisationVector, sizeof(mInitialisationVector));
144#endif
145
146        return *this;
147}
148
149
150// --------------------------------------------------------------------------
151//
152// Function
153//              Name:    CipherBlowfish::GetCipher()
154//              Purpose: Returns cipher object
155//              Created: 1/12/03
156//
157// --------------------------------------------------------------------------
158const EVP_CIPHER *CipherBlowfish::GetCipher() const
159{
160        switch(mMode)
161        {
162        case CipherDescription::Mode_ECB:
163                return EVP_bf_ecb();
164                break;
165       
166        case CipherDescription::Mode_CBC:
167                return EVP_bf_cbc();
168                break;
169       
170        case CipherDescription::Mode_CFB:
171                return EVP_bf_cfb();
172                break;
173       
174        case CipherDescription::Mode_OFB:
175                return EVP_bf_ofb();
176                break;
177       
178        default:
179                break;
180        }
181
182        // Unknown!
183        THROW_EXCEPTION(CipherException, UnknownCipherMode)
184}
185
186// --------------------------------------------------------------------------
187//
188// Function
189//              Name:    CipherBlowfish::SetupParameters(EVP_CIPHER_CTX *)
190//              Purpose: Set up various parameters for cipher
191//              Created: 1/12/03
192//
193// --------------------------------------------------------------------------
194void CipherBlowfish::SetupParameters(EVP_CIPHER_CTX *pCipherContext) const
195{
196        ASSERT(pCipherContext != 0);
197       
198        // Set key length
199#ifndef HAVE_OLD_SSL
200        if(EVP_CIPHER_CTX_set_key_length(pCipherContext, mKeyLength) != 1)
201#else
202        if(EVP_CIPHER_CTX_set_key_length(pCipherContext, mKey.size()) != 1)
203#endif
204        {
205                THROW_EXCEPTION(CipherException, EVPBadKeyLength)
206        }
207        // Set key
208#ifndef HAVE_OLD_SSL
209        if(EVP_CipherInit_ex(pCipherContext, NULL, NULL, (unsigned char*)mpKey, (unsigned char*)mpInitialisationVector, -1) != 1)
210#else
211        if(EVP_CipherInit(pCipherContext, NULL, (unsigned char*)mKey.c_str(), (unsigned char*)mInitialisationVector, -1) != 1)
212#endif
213        {
214                THROW_EXCEPTION(CipherException, EVPInitFailure)
215        }
216       
217}
218
219
220
Note: See TracBrowser for help on using the repository browser.