• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

crypt/key.cc

Go to the documentation of this file.
00001 // $Id: key.cc 3342 2010-09-17 18:32:00Z java $
00002 //
00003 // Copyright (C) 2008-2010  Rafael Ostertag
00004 //
00005 // This file is part of YAPET.
00006 //
00007 // YAPET is free software: you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the Free Software
00009 // Foundation, either version 3 of the License, or (at your option) any later
00010 // version.
00011 //
00012 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015 // details.
00016 //
00017 // You should have received a copy of the GNU General Public License along with
00018 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
00019 //
00020 // Additional permission under GNU GPL version 3 section 7
00021 //
00022 // If you modify this program, or any covered work, by linking or combining it
00023 // with the OpenSSL project's OpenSSL library (or a modified version of that
00024 // library), containing parts covered by the terms of the OpenSSL or SSLeay
00025 // licenses, Rafael Ostertag grants you additional permission to convey the
00026 // resulting work.  Corresponding Source for a non-source form of such a
00027 // combination shall include the source code for the parts of OpenSSL used as
00028 // well as that of the covered work.
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     // Sentinel variable to check the size of the key
00059     uint8_t eff_keylength;
00060     //
00061     // First run (sha1)
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     // Second run (md5)
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     // Third run (ripemd160)
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     // The initialization vector
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 }

Generated on Sun Sep 19 2010 15:37:13 for YAPET by  doxygen 1.7.1