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
|
// -*- c++ -*-
//
// $Id: key.h 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.
//
#ifndef _KEY_H
#define _KEY_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <openssl/evp.h>
#include "yapetexception.h"
/**
* @brief Namespace for cryptographic stuff
*
* Namespace for cryptographic stuff. Has no front-end and relies on
* openssl.
*/
namespace YAPET {
/**
* @brief Converts the password into the key
*
* Converts the password into the key which is used by the other
* cryptographic related classes.
*
* The key uses the maximum length of 448bits (56bytes) allowed
* for blowfish.
*
* The key is computed using three passes. The first pass hashes
* the password using the sha1 algorithm. This hash is then
* re-hashed using md5 which is then appended to the key generated
* by the previous pass (sha1 + md5). The last pass hashes the
* result of the former two passes using RIPEMD-160 and appended
* the result to the key (sha1 + md5 + ripemd160).
*
* The initialization vector is computed by hashing the key using
* the md5 algorithm and taking only the first eight bytes.
*/
class Key {
private:
enum {
/**
* The max length of the blowfish key in bytes (448 bits)
*/
KEYLENGTH = 56,
/**
* The length of the output of md5 (128 bits)
*/
MD5_LEN = 16,
/**
* The length of the output of sha1 (160 bits)
*/
SHA1_LEN = 20,
/**
* The lenght of the output of ripemd-160 (160 bits)
*/
RIPEMD160_LEN = 20,
/**
* The length of the initialization vector
*/
IVECLENGTH = 8
};
/**
* @brief Holds the key
*
* This is the key used to encrypt and decrypt data.
*/
uint8_t key[KEYLENGTH];
/**
* @brief Holds the initialization vector
*
* The initialization vector used for encryption and
* decryption.
*/
uint8_t IVec[IVECLENGTH];
//! Cleanup routine
void cleanup();
public:
//! Initializes the key
Key (const char* password) throw (YAPETException);
Key (const Key& k);
~Key();
/**
* @brief Gets the pointer to the key
*
* Returns the key and its length. The key is not
* terminated by \c \\0. So make sure you read only \c
* key_len bytes from the pointer returned.
*
* @param key_len reference to an integer receiving the
* key length in bytes
*
* @return pointer to the array of unsigned 8bit integers
* holding the key.
*/
inline const uint8_t* getKey (int& key_len) const {
key_len = KEYLENGTH;
return key;
}
/**
* @brief Gets the pointer to the key
*
* Returns the key only. Please note that the key is not
* terminated by \c \\0, so make sure you read only as many
* bytes as returned by \c size().
*
* @return pointer to the array of usigned 8bit integers
* holding the key.
*/
inline const uint8_t* getKey() const {
return key;
}
/**
* @brief Gets the pointer to the initialization vector
*
* Gets the initialization vector and its length. Please
* remember that the initialization vector is not \c \\0
* terminated. So make sure you read only \c ivec_len
* bytes from the pointer returned.
*
* @param ivec_len a reference to an integer receiving the
* length of the initialization vector.
*
* @return pointer to the array of unsigned 8bit integers
* holding the initialization vector.
*/
inline const uint8_t* getIVec (int& ivec_len) const {
ivec_len = IVECLENGTH;
return IVec;
}
/**
* @brief Gets the initialization vector
*
* Gets the initialization vector. Please remember that
* the initialization vector is not \c \\0 terminated. So
* make sure you read only as many bytes as returned by \c
* ivec_size().
*
* @return pointer to the array of unsigned 8bit integers
* holding the initialization vector.
*/
inline const uint8_t* getIVec() const {
return IVec;
}
/**
* @brief Returns the key length in bytes
*
* Returns the key length in bytes
*
* @return key length in bytes.
*/
inline uint32_t size() const {
return KEYLENGTH;
}
/**
* @brief Returns the length of the initialization vector
*
* Returns the length of the initialization vector in
* bytes.
*
* @return the size of the initialization vector in bytes.
*/
inline uint32_t ivec_size() const {
return IVECLENGTH;
}
/**
* @brief Gets the pointer to the key
*
* Returns the key and its length. The key is not
* terminated by \c \\0. So make sure you read only \c
* key_len bytes from the pointer returned.
*
* @param key_len reference to an integer receiving the
* key length in bytes
*
* @return pointer to the array of unsigned 8bit integers
* holding the key.
*/
inline const uint8_t* operator() (int& key_len) const {
return getKey (key_len);
}
/**
* @brief Gets the pointer to the key
*
* Returns the key only. Please note that the key is not
* terminated by \c \\0, so make sure you read only as many
* bytes as returned by \c size().
*
* @return pointer to the array of usigned 8bit integers
* holding the key.
*/
inline const uint8_t* operator() () const {
return key;
}
/**
* @brief Cast operator
*
* Returns the pointer to the key.
*/
inline operator uint8_t*() {
return key;
}
/**
* @brief Cast operator
*
* Returns the pointer to the key.
*/
inline operator const uint8_t*() const {
return key;
}
const Key& operator= (const Key& k);
//! Compares two keys for equality
bool operator== (const Key& k) const;
//! Compares two keys for inequality
bool operator!= (const Key& k) const {
return !operator== (k);
}
};
}
#endif // _KEY_H
|