File: key.cc

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 (292 lines) | stat: -rw-r--r-- 7,975 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
// $Id: key.cc 2719 2009-08-20 01:30: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.
//

#include "../intl.h"
#include "key.h"

#ifdef HAVE_STRING_H
# include <string.h>
#endif

using namespace YAPET;

/**
 * It clears the memory occupied by the key and the initialization
 * vector by setting it to zero.
 */
void
Key::cleanup() {
    memset (key, 0, KEYLENGTH);
    memset (IVec, 0, IVECLENGTH);
}

/**
 * Initializes the key and the initialization vector. Make sure you
 * securely destroy the password provided to this method.
 *
 * @param password a pointer to the location the password is
 * stored. The password has to be zero-terminated.
 */
Key::Key (const char* password) throw (YAPETException) {
    // Sentinel variable to check the size of the key
    uint8_t eff_keylength;
    //
    // First run (sha1)
    //
    const EVP_MD* md = EVP_sha1();

    if (md == NULL)
        throw YAPETException (_ ("Run 1: Unable to initialize the EVP_MD structure") );

    EVP_MD_CTX mdctx;
    EVP_MD_CTX_init (&mdctx);
    int retval = EVP_DigestInit_ex (&mdctx, md, NULL);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        throw YAPETException (_ ("Run 1: Unable to initialize the digest") );
    }

    retval = EVP_DigestUpdate (&mdctx, password, strlen (password) );

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        throw YAPETException (_ ("Run 1: Unable to update the digest") );
    }

    unsigned int tmplen;
    retval = EVP_DigestFinal_ex (&mdctx, key, &tmplen);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("Run 1: Unable to finalize the digest") );
    }

    if (tmplen != SHA1_LEN) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("Run 1: Digest does not have expected length") );
    }

    eff_keylength = tmplen;
    EVP_MD_CTX_cleanup (&mdctx);
    //
    // Second run (md5)
    //
    md = EVP_md5();

    if (md == NULL) {
        cleanup();
        throw YAPETException (_ ("Run 2: Unable to initialize the EVP_MD structure") );
    }

    EVP_MD_CTX_init (&mdctx);
    retval = EVP_DigestInit_ex (&mdctx, md, NULL);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("Run 2: Unable to initialize the digest") );
    }

    retval = EVP_DigestUpdate (&mdctx, key, SHA1_LEN);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("Run 2: Unable to update the digest") );
    }

    retval = EVP_DigestFinal_ex (&mdctx, key + SHA1_LEN, &tmplen);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("Run 2: Unable to finalize the digest") );
    }

    if (tmplen != MD5_LEN) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("Run 2: Digest does not have expected length") );
    }

    eff_keylength += tmplen;
    EVP_MD_CTX_cleanup (&mdctx);
    //
    // Third run (ripemd160)
    //
    md = EVP_ripemd160();

    if (md == NULL) {
        cleanup();
        throw YAPETException (_ ("Run 3: Unable to initialize the EVP_MD structure") );
    }

    EVP_MD_CTX_init (&mdctx);
    retval = EVP_DigestInit_ex (&mdctx, md, NULL);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("Run 3: Unable to initialize the digest") );
    }

    retval = EVP_DigestUpdate (&mdctx, key, SHA1_LEN + MD5_LEN);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("Run 3: Unable to update the digest") );
    }

    retval = EVP_DigestFinal_ex (&mdctx, key + SHA1_LEN + MD5_LEN, &tmplen);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("Run 3: Unable to finalize the digest") );
    }

    if (tmplen != RIPEMD160_LEN) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("Run 3: Digest does not have expected length") );
    }

    eff_keylength += tmplen;
    EVP_MD_CTX_cleanup (&mdctx);

    if (eff_keylength != KEYLENGTH) {
        cleanup();
        char tmp[100];
        snprintf (tmp,
                  100,
                  _ ("Effective key length of %d does not match expected key length %d"),
                  eff_keylength,
                  KEYLENGTH);
        throw YAPETException (tmp);
    }

    //
    // The initialization vector
    //
    uint8_t ivec_hash_buf[MD5_LEN];
    md = EVP_md5();

    if (md == NULL) {
        cleanup();
        throw YAPETException (_ ("IVec: Unable to initialize the EVP_MD structure") );
    }

    EVP_MD_CTX_init (&mdctx);
    retval = EVP_DigestInit_ex (&mdctx, md, NULL);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("IVec: Unable to initialize the digest") );
    }

    retval = EVP_DigestUpdate (&mdctx, key, SHA1_LEN + MD5_LEN + RIPEMD160_LEN);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("IVec: Unable to update the digest") );
    }

    retval = EVP_DigestFinal_ex (&mdctx, ivec_hash_buf, &tmplen);

    if (retval == 0) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("IVec: Unable to finalize the digest") );
    }

    if (tmplen != MD5_LEN) {
        EVP_MD_CTX_cleanup (&mdctx);
        cleanup();
        throw YAPETException (_ ("IVec: Digest does not have expected length") );
    }

    EVP_MD_CTX_cleanup (&mdctx);
    memcpy (IVec, ivec_hash_buf, IVECLENGTH);
    memset (ivec_hash_buf, 0, MD5_LEN);
}

Key::Key (const Key& k) {
    memcpy (key, k.key, KEYLENGTH);
    memcpy (IVec, k.IVec, IVECLENGTH);
}

Key::~Key() {
    cleanup();
}

const Key&
Key::operator= (const Key & k) {
    if (this == &k) return *this;

    cleanup();
    memcpy (key, k.key, KEYLENGTH);
    memcpy (IVec, k.IVec, IVECLENGTH);
    return *this;
}

/**
 * Compares the key \c k provided for equality with this key.
 *
 * Both, the key itself and the initialization vector are compared.
 *
 * @param k reference to the key compared with this.
 *
 * @return \c true if both keys and initialization vectors are equal,
 * \c false otherwise.
 */
bool
Key::operator== (const Key& k) const {
    if (k.size() != size() ) return false;

    if (k.ivec_size() != ivec_size() ) return false;

    int retval = memcmp (k.key, key, size() );

    if (retval != 0)
        return false;

    retval = memcmp (k.IVec, IVec, ivec_size() );

    if (retval != 0)
        return false;

    return true;
}