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
00064 const EVP_MD* md = EVP_sha1();
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
00071 int retval = EVP_DigestInit_ex(&mdctx, md, NULL);
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 if (retval == 0) {
00079 EVP_MD_CTX_cleanup(&mdctx);
00080 throw YAPETException(_("Run 1: Unable to update the digest"));
00081 }
00082
00083 unsigned int tmplen;
00084 retval = EVP_DigestFinal_ex(&mdctx, key, &tmplen);
00085 if (retval == 0) {
00086 EVP_MD_CTX_cleanup(&mdctx);
00087 cleanup();
00088 throw YAPETException(_("Run 1: Unable to finalize the digest"));
00089 }
00090
00091 if (tmplen != SHA1_LEN) {
00092 EVP_MD_CTX_cleanup(&mdctx);
00093 cleanup();
00094 throw YAPETException(_("Run 1: Digest does not have expected length"));
00095 }
00096
00097 eff_keylength = tmplen;
00098 EVP_MD_CTX_cleanup(&mdctx);
00099
00100
00101
00102
00103 md = EVP_md5();
00104 if (md == NULL) {
00105 cleanup();
00106 throw YAPETException(_("Run 2: Unable to initialize the EVP_MD structure"));
00107 }
00108
00109 EVP_MD_CTX_init(&mdctx);
00110 retval = EVP_DigestInit_ex(&mdctx, md, NULL);
00111 if (retval == 0) {
00112 EVP_MD_CTX_cleanup(&mdctx);
00113 cleanup();
00114 throw YAPETException(_("Run 2: Unable to initialize the digest"));
00115 }
00116
00117 retval = EVP_DigestUpdate(&mdctx, key, SHA1_LEN);
00118 if (retval == 0) {
00119 EVP_MD_CTX_cleanup(&mdctx);
00120 cleanup();
00121 throw YAPETException(_("Run 2: Unable to update the digest"));
00122 }
00123
00124 retval = EVP_DigestFinal_ex(&mdctx, key + SHA1_LEN, &tmplen);
00125 if (retval == 0) {
00126 EVP_MD_CTX_cleanup(&mdctx);
00127 cleanup();
00128 throw YAPETException(_("Run 2: Unable to finalize the digest"));
00129 }
00130
00131 if (tmplen != MD5_LEN) {
00132 EVP_MD_CTX_cleanup(&mdctx);
00133 cleanup();
00134 throw YAPETException(_("Run 2: Digest does not have expected length"));
00135 }
00136
00137 eff_keylength += tmplen;
00138 EVP_MD_CTX_cleanup(&mdctx);
00139
00140
00141
00142
00143 md = EVP_ripemd160();
00144 if (md == NULL) {
00145 cleanup();
00146 throw YAPETException(_("Run 3: Unable to initialize the EVP_MD structure"));
00147 }
00148
00149 EVP_MD_CTX_init(&mdctx);
00150 retval = EVP_DigestInit_ex(&mdctx, md, NULL);
00151 if (retval == 0) {
00152 EVP_MD_CTX_cleanup(&mdctx);
00153 cleanup();
00154 throw YAPETException(_("Run 3: Unable to initialize the digest"));
00155 }
00156
00157 retval = EVP_DigestUpdate(&mdctx, key, SHA1_LEN + MD5_LEN);
00158 if (retval == 0) {
00159 EVP_MD_CTX_cleanup(&mdctx);
00160 cleanup();
00161 throw YAPETException(_("Run 3: Unable to update the digest"));
00162 }
00163
00164 retval = EVP_DigestFinal_ex(&mdctx, key + SHA1_LEN + MD5_LEN, &tmplen);
00165 if (retval == 0) {
00166 EVP_MD_CTX_cleanup(&mdctx);
00167 cleanup();
00168 throw YAPETException(_("Run 3: Unable to finalize the digest"));
00169 }
00170
00171 if (tmplen != RIPEMD160_LEN) {
00172 EVP_MD_CTX_cleanup(&mdctx);
00173 cleanup();
00174 throw YAPETException(_("Run 3: Digest does not have expected length"));
00175 }
00176
00177 eff_keylength += tmplen;
00178 EVP_MD_CTX_cleanup(&mdctx);
00179
00180 if (eff_keylength != KEYLENGTH) {
00181 cleanup();
00182 char tmp[100];
00183 snprintf(tmp,
00184 100,
00185 _("Effective key length of %d does not match expected key length %d"),
00186 eff_keylength,
00187 KEYLENGTH);
00188 throw YAPETException(tmp);
00189 }
00190
00191
00192
00193
00194 uint8_t ivec_hash_buf[MD5_LEN];
00195 md = EVP_md5();
00196 if (md == NULL) {
00197 cleanup();
00198 throw YAPETException(_("IVec: Unable to initialize the EVP_MD structure"));
00199 }
00200
00201 EVP_MD_CTX_init(&mdctx);
00202 retval = EVP_DigestInit_ex(&mdctx, md, NULL);
00203 if (retval == 0) {
00204 EVP_MD_CTX_cleanup(&mdctx);
00205 cleanup();
00206 throw YAPETException(_("IVec: Unable to initialize the digest"));
00207 }
00208
00209 retval = EVP_DigestUpdate(&mdctx, key, SHA1_LEN + MD5_LEN + RIPEMD160_LEN);
00210 if (retval == 0) {
00211 EVP_MD_CTX_cleanup(&mdctx);
00212 cleanup();
00213 throw YAPETException(_("IVec: Unable to update the digest"));
00214 }
00215
00216 retval = EVP_DigestFinal_ex(&mdctx, ivec_hash_buf, &tmplen);
00217 if (retval == 0) {
00218 EVP_MD_CTX_cleanup(&mdctx);
00219 cleanup();
00220 throw YAPETException(_("IVec: Unable to finalize the digest"));
00221 }
00222
00223 if (tmplen != MD5_LEN) {
00224 EVP_MD_CTX_cleanup(&mdctx);
00225 cleanup();
00226 throw YAPETException(_("IVec: Digest does not have expected length"));
00227 }
00228
00229 EVP_MD_CTX_cleanup(&mdctx);
00230
00231 memcpy(IVec, ivec_hash_buf, IVECLENGTH);
00232 memset(ivec_hash_buf, 0, MD5_LEN);
00233 }
00234
00235 Key::Key(const Key& k) {
00236 memcpy(key, k.key, KEYLENGTH);
00237 memcpy(IVec, k.IVec, IVECLENGTH);
00238 }
00239
00240 Key::~Key() {
00241 cleanup();
00242 }
00243
00244 const Key&
00245 Key::operator=(const Key& k) {
00246 if (this == &k) return *this;
00247
00248 cleanup();
00249
00250 memcpy(key, k.key, KEYLENGTH);
00251 memcpy(IVec, k.IVec, IVECLENGTH);
00252
00253 return *this;
00254 }
00255
00266 bool
00267 Key::operator==(const Key& k) const {
00268 if (k.size() != size()) return false;
00269 if (k.ivec_size() != ivec_size()) return false;
00270
00271 int retval = memcmp(k.key, key, size());
00272 if (retval != 0)
00273 return false;
00274
00275 retval = memcmp(k.IVec, IVec, ivec_size());
00276 if (retval != 0)
00277 return false;
00278
00279 return true;
00280 }