00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "../intl.h"
00032 #include "key.h"
00033
00034 #ifdef HAVE_STRING_H
00035 # include <string.h>
00036 #endif
00037
00038 using namespace YAPET;
00039
00044 void
00045 Key::cleanup() {
00046 memset (key, 0, KEYLENGTH);
00047 memset (IVec, 0, IVECLENGTH);
00048 }
00049
00057 Key::Key (const char* password) throw (YAPETException) {
00058
00059 uint8_t eff_keylength;
00060
00061
00062
00063 const EVP_MD* md = EVP_sha1();
00064
00065 if (md == NULL)
00066 throw YAPETException (_ ("Run 1: Unable to initialize the EVP_MD structure") );
00067
00068 EVP_MD_CTX mdctx;
00069 EVP_MD_CTX_init (&mdctx);
00070 int retval = EVP_DigestInit_ex (&mdctx, md, NULL);
00071
00072 if (retval == 0) {
00073 EVP_MD_CTX_cleanup (&mdctx);
00074 throw YAPETException (_ ("Run 1: Unable to initialize the digest") );
00075 }
00076
00077 retval = EVP_DigestUpdate (&mdctx, password, strlen (password) );
00078
00079 if (retval == 0) {
00080 EVP_MD_CTX_cleanup (&mdctx);
00081 throw YAPETException (_ ("Run 1: Unable to update the digest") );
00082 }
00083
00084 unsigned int tmplen;
00085 retval = EVP_DigestFinal_ex (&mdctx, key, &tmplen);
00086
00087 if (retval == 0) {
00088 EVP_MD_CTX_cleanup (&mdctx);
00089 cleanup();
00090 throw YAPETException (_ ("Run 1: Unable to finalize the digest") );
00091 }
00092
00093 if (tmplen != SHA1_LEN) {
00094 EVP_MD_CTX_cleanup (&mdctx);
00095 cleanup();
00096 throw YAPETException (_ ("Run 1: Digest does not have expected length") );
00097 }
00098
00099 eff_keylength = tmplen;
00100 EVP_MD_CTX_cleanup (&mdctx);
00101
00102
00103
00104 md = EVP_md5();
00105
00106 if (md == NULL) {
00107 cleanup();
00108 throw YAPETException (_ ("Run 2: Unable to initialize the EVP_MD structure") );
00109 }
00110
00111 EVP_MD_CTX_init (&mdctx);
00112 retval = EVP_DigestInit_ex (&mdctx, md, NULL);
00113
00114 if (retval == 0) {
00115 EVP_MD_CTX_cleanup (&mdctx);
00116 cleanup();
00117 throw YAPETException (_ ("Run 2: Unable to initialize the digest") );
00118 }
00119
00120 retval = EVP_DigestUpdate (&mdctx, key, SHA1_LEN);
00121
00122 if (retval == 0) {
00123 EVP_MD_CTX_cleanup (&mdctx);
00124 cleanup();
00125 throw YAPETException (_ ("Run 2: Unable to update the digest") );
00126 }
00127
00128 retval = EVP_DigestFinal_ex (&mdctx, key + SHA1_LEN, &tmplen);
00129
00130 if (retval == 0) {
00131 EVP_MD_CTX_cleanup (&mdctx);
00132 cleanup();
00133 throw YAPETException (_ ("Run 2: Unable to finalize the digest") );
00134 }
00135
00136 if (tmplen != MD5_LEN) {
00137 EVP_MD_CTX_cleanup (&mdctx);
00138 cleanup();
00139 throw YAPETException (_ ("Run 2: Digest does not have expected length") );
00140 }
00141
00142 eff_keylength += tmplen;
00143 EVP_MD_CTX_cleanup (&mdctx);
00144
00145
00146
00147 md = EVP_ripemd160();
00148
00149 if (md == NULL) {
00150 cleanup();
00151 throw YAPETException (_ ("Run 3: Unable to initialize the EVP_MD structure") );
00152 }
00153
00154 EVP_MD_CTX_init (&mdctx);
00155 retval = EVP_DigestInit_ex (&mdctx, md, NULL);
00156
00157 if (retval == 0) {
00158 EVP_MD_CTX_cleanup (&mdctx);
00159 cleanup();
00160 throw YAPETException (_ ("Run 3: Unable to initialize the digest") );
00161 }
00162
00163 retval = EVP_DigestUpdate (&mdctx, key, SHA1_LEN + MD5_LEN);
00164
00165 if (retval == 0) {
00166 EVP_MD_CTX_cleanup (&mdctx);
00167 cleanup();
00168 throw YAPETException (_ ("Run 3: Unable to update the digest") );
00169 }
00170
00171 retval = EVP_DigestFinal_ex (&mdctx, key + SHA1_LEN + MD5_LEN, &tmplen);
00172
00173 if (retval == 0) {
00174 EVP_MD_CTX_cleanup (&mdctx);
00175 cleanup();
00176 throw YAPETException (_ ("Run 3: Unable to finalize the digest") );
00177 }
00178
00179 if (tmplen != RIPEMD160_LEN) {
00180 EVP_MD_CTX_cleanup (&mdctx);
00181 cleanup();
00182 throw YAPETException (_ ("Run 3: Digest does not have expected length") );
00183 }
00184
00185 eff_keylength += tmplen;
00186 EVP_MD_CTX_cleanup (&mdctx);
00187
00188 if (eff_keylength != KEYLENGTH) {
00189 cleanup();
00190 char tmp[100];
00191 snprintf (tmp,
00192 100,
00193 _ ("Effective key length of %d does not match expected key length %d"),
00194 eff_keylength,
00195 KEYLENGTH);
00196 throw YAPETException (tmp);
00197 }
00198
00199
00200
00201
00202 uint8_t ivec_hash_buf[MD5_LEN];
00203 md = EVP_md5();
00204
00205 if (md == NULL) {
00206 cleanup();
00207 throw YAPETException (_ ("IVec: Unable to initialize the EVP_MD structure") );
00208 }
00209
00210 EVP_MD_CTX_init (&mdctx);
00211 retval = EVP_DigestInit_ex (&mdctx, md, NULL);
00212
00213 if (retval == 0) {
00214 EVP_MD_CTX_cleanup (&mdctx);
00215 cleanup();
00216 throw YAPETException (_ ("IVec: Unable to initialize the digest") );
00217 }
00218
00219 retval = EVP_DigestUpdate (&mdctx, key, SHA1_LEN + MD5_LEN + RIPEMD160_LEN);
00220
00221 if (retval == 0) {
00222 EVP_MD_CTX_cleanup (&mdctx);
00223 cleanup();
00224 throw YAPETException (_ ("IVec: Unable to update the digest") );
00225 }
00226
00227 retval = EVP_DigestFinal_ex (&mdctx, ivec_hash_buf, &tmplen);
00228
00229 if (retval == 0) {
00230 EVP_MD_CTX_cleanup (&mdctx);
00231 cleanup();
00232 throw YAPETException (_ ("IVec: Unable to finalize the digest") );
00233 }
00234
00235 if (tmplen != MD5_LEN) {
00236 EVP_MD_CTX_cleanup (&mdctx);
00237 cleanup();
00238 throw YAPETException (_ ("IVec: Digest does not have expected length") );
00239 }
00240
00241 EVP_MD_CTX_cleanup (&mdctx);
00242 memcpy (IVec, ivec_hash_buf, IVECLENGTH);
00243 memset (ivec_hash_buf, 0, MD5_LEN);
00244 }
00245
00246 Key::Key (const Key& k) {
00247 memcpy (key, k.key, KEYLENGTH);
00248 memcpy (IVec, k.IVec, IVECLENGTH);
00249 }
00250
00251 Key::~Key() {
00252 cleanup();
00253 }
00254
00255 const Key&
00256 Key::operator= (const Key & k) {
00257 if (this == &k) return *this;
00258
00259 cleanup();
00260 memcpy (key, k.key, KEYLENGTH);
00261 memcpy (IVec, k.IVec, IVECLENGTH);
00262 return *this;
00263 }
00264
00275 bool
00276 Key::operator== (const Key& k) const {
00277 if (k.size() != size() ) return false;
00278
00279 if (k.ivec_size() != ivec_size() ) return false;
00280
00281 int retval = memcmp (k.key, key, size() );
00282
00283 if (retval != 0)
00284 return false;
00285
00286 retval = memcmp (k.IVec, IVec, ivec_size() );
00287
00288 if (retval != 0)
00289 return false;
00290
00291 return true;
00292 }