File: crypt.h

package info (click to toggle)
yapet 0.6-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 6,012 kB
  • ctags: 2,913
  • sloc: ansic: 13,661; cpp: 11,384; sh: 4,814; makefile: 847; yacc: 291; sed: 16
file content (305 lines) | stat: -rw-r--r-- 10,776 bytes parent folder | download
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// -*- c++ -*-
//
// $Id: crypt.h 2850 2009-09-01 10:53:25Z rafi $
//
// Copyright (C) 2008, 2009  Rafael Ostertag
//
// This file is part of YAPET.
//
// YAPET is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// YAPET.  If not, see <http://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with the OpenSSL project's OpenSSL library (or a modified version of that
// library), containing parts covered by the terms of the OpenSSL or SSLeay
// licenses, Rafael Ostertag grants you additional permission to convey the
// resulting work.  Corresponding Source for a non-source form of such a
// combination shall include the source code for the parts of OpenSSL used as
// well as that of the covered work.
//

#ifndef _CRYPT_H
#define _CRYPT_H

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif

#include "../intl.h"


#include <openssl/evp.h>

#include "yapetexception.h"
#include "key.h"
#include "bdbuffer.h"
#include "record.h"

namespace YAPET {
    /**
     * @brief Encrypts and decrypts data
     *
     * This class encrypts and decrypts data using the key provided by
     * \c Key.
     *
     * Encrypted data is returned in \c BDBuffer. Decrypted data is
     * returned as a struct as defined in \c structs.h using the
     * template \c Record.
     *
     * Currently, the blowfish algorithm in CBC mode from the openssl
     * library is used for encryption and decryption. The class itself
     * does not have any encryption/decryption logic.
     *
     * @sa Key, BDBuffer, Record
     */
    class Crypt {
        private:
            /**
             * @brief Pointer to the cipher used for
             * encryption/decryption
             *
             * Pointer the cipher implemented in the openssl
             * library. Currently, blowfish is used for
             * encryption/decryption.
             */
            const EVP_CIPHER* cipher;
            /**
             * @brief Length of the initialization vector
             *
             * Length of the initialization vector as expected by the
             * cipher implementation in bytes.
             */
            uint32_t iv_length;
            /**
             * @brief Length of the encryption/decryption key
             *
             * Length of the encryption/decryption key as expected by
             * the cipher implementation in bytes.
             */
            uint32_t key_length;
            /**
             * @brief the key used for encryption/decryption
             *
             * The key used for encryption/decryption.
             */
            Key key;

        public:
            //! Constructor
            Crypt (const Key& k) throw (YAPETException);
            Crypt (const Crypt& c);
            inline ~Crypt() {}

            /**
             * @brief Returns the length of the initialization vector
             *
             * Returns the length in bytes of the initialization
             * vector the cipher algorithm expects
             *
             * @return the length of the initialization vector in
             * bytes.
             */
            inline uint32_t getIVLength() const {
                return iv_length;
            }
            /**
             * @brief Returns the length of the key
             *
             * Returns the length in bytes of the key the cipher
             * algorithm expects.
             *
             * @return the length of the key in bytes.
             */
            inline uint32_t getKeyLength() const {
                return key_length;
            }

            /**
             * @brief Encrypts data
             *
             * Encrypts the data provided in \c data which has to be
             * an instance of the \c Record template.
             *
             * The encrypted data is returned as \c BDBuffer with the
             * size set exactly to the size of the encrypted data. The
             * memory occupied by this object has to be freed by the
             * caller.
             *
             * @param data the data to be encrypted.
             *
             * @return pointer to a \c BDBuffer holding the encrypted
             * data. The caller is responsible for freeing the memory
             * occupied by the object returned.
             *
             * @throw YAPETException
             *
             * @throw YAPETEncryptionException
             *
             * @sa Record, BDBuffer
             */
            template<class T>
            BDBuffer* encrypt (const Record<T>& data)
            throw (YAPETException, YAPETEncryptionException) {
                if (key.ivec_size() != iv_length)
                    throw YAPETException (_ ("IVec length missmatch") );

                EVP_CIPHER_CTX ctx;
                EVP_CIPHER_CTX_init (&ctx);
                int retval = EVP_EncryptInit_ex (&ctx,
                                                 cipher,
                                                 NULL,
                                                 key,
                                                 key.getIVec() );

                if (retval == 0) {
                    EVP_CIPHER_CTX_cleanup (&ctx);
                    throw YAPETEncryptionException (_ ("Error initializing encryption engine") );
                }

                retval = EVP_CIPHER_CTX_set_key_length (&ctx, key.size() );

                if (retval == 0) {
                    EVP_CIPHER_CTX_cleanup (&ctx);
                    throw YAPETException (_ ("Error setting the key length") );
                }

                BDBuffer* encdata =
                    new BDBuffer (data.size() + EVP_MAX_BLOCK_LENGTH);
                int outlen;
                retval = EVP_EncryptUpdate (&ctx,
                                            *encdata,
                                            &outlen,
                                            data,
                                            data.size() );

                if (retval == 0) {
                    EVP_CIPHER_CTX_cleanup (&ctx);
                    delete encdata;
                    throw YAPETEncryptionException (_ ("Error encrypting data") );
                }

                int tmplen;
                retval = EVP_EncryptFinal_ex (&ctx,
                                              encdata->at (outlen),
                                              &tmplen);

                if (retval == 0) {
                    EVP_CIPHER_CTX_cleanup (&ctx);
                    delete encdata;
                    throw YAPETEncryptionException (_ ("Error finalizing encryption") );
                }

                encdata->resize (outlen + tmplen);
                EVP_CIPHER_CTX_cleanup (&ctx);
                return encdata;
            }

            /**
             * @brief Decrypts data
             *
             * Decrypts the data supplied in the \c BDBuffer. It
             * returns the decrypted data as a \c Record of the
             * specified type.
             *
             * The \c Record has to be freed by the caller.
             *
             * @param data the \c BDBuffer to decrypt.
             *
             * @return pointer to a \c Record of the specified type,
             * holding the decrypted data. The caller is responsible
             * for freeing the memory occupied by the object returned.
             *
             * @throw YAPETException
             *
             * @throw YAPETEncryptionException
             *
             * @sa Record, BDBuffer
             */
            template<class T>
            Record<T>* decrypt (const BDBuffer& data)
            throw (YAPETException, YAPETEncryptionException) {
                if ( ( (unsigned int) key.ivec_size() ) != iv_length)
                    throw YAPETException (_ ("IVec length missmatch") );

                EVP_CIPHER_CTX ctx;
                EVP_CIPHER_CTX_init (&ctx);
                int retval = EVP_DecryptInit_ex (&ctx,
                                                 cipher,
                                                 NULL,
                                                 key,
                                                 key.getIVec() );

                if (retval == 0) {
                    EVP_CIPHER_CTX_cleanup (&ctx);
                    throw YAPETEncryptionException (_ ("Error initializing encryption engine") );
                }

                retval = EVP_CIPHER_CTX_set_key_length (&ctx, key.size() );

                if (retval == 0) {
                    EVP_CIPHER_CTX_cleanup (&ctx);
                    throw YAPETException (_ ("Error setting the key length") );
                }

                BDBuffer* decdata = new BDBuffer (data.size() );
                int outlen;
                retval = EVP_DecryptUpdate (&ctx,
                                            *decdata,
                                            &outlen,
                                            data,
                                            data.size() );

                if (retval == 0) {
                    EVP_CIPHER_CTX_cleanup (&ctx);
                    delete decdata;
                    throw YAPETEncryptionException (_ ("Error decrypting data") );
                }

                int tmplen;
                retval = EVP_DecryptFinal_ex (&ctx,
                                              decdata->at (outlen),
                                              &tmplen);

                if (retval == 0) {
                    EVP_CIPHER_CTX_cleanup (&ctx);
                    delete decdata;
                    throw YAPETEncryptionException (_ ("Error finalizing decryption") );
                }

                decdata->resize (outlen + tmplen);
                EVP_CIPHER_CTX_cleanup (&ctx);
		Record<T>* r = NULL;
		try {
		    r = new Record<T>;
		    *r = *decdata;
		} catch (...) {
		    if ( r != NULL)
			delete r;
		    delete decdata;
		    throw;
		}
                delete decdata;
                return r;
            }

            const Crypt& operator= (const Crypt& c);
    };
}

#endif // _CRYPT_H