PKIFAlgorithm.cpp

Go to the documentation of this file.
00001 
00009 #include "PKIFAlgorithm.h"
00010 #include "ToolkitUtils.h"
00011 #include "OID.h"
00012 
00013 static CPKIFAlgorithm md5("MD5",PKIFCRYPTO::MD5,16);
00014 static CPKIFAlgorithm sha1("SHA-1",PKIFCRYPTO::SHA1,20);
00015 static CPKIFAlgorithm sha256("SHA-256",PKIFCRYPTO::SHA256,32);
00016 static CPKIFAlgorithm sha384("SHA-384",PKIFCRYPTO::SHA384,48);
00017 static CPKIFAlgorithm sha512("SHA-512",PKIFCRYPTO::SHA512,64);
00018 static CPKIFAlgorithm sha224("SHA-224",PKIFCRYPTO::SHA224,28);
00019 
00020 static CPKIFAlgorithm desECB("DES ECB",PKIFCRYPTO::DES,PKIFCRYPTO::ECB,8,8);
00021 static CPKIFAlgorithm tdesECB("Triple DES ECB",PKIFCRYPTO::TDES,PKIFCRYPTO::ECB,24,8);
00022 static CPKIFAlgorithm aes128ECB("AES-128 ECB",PKIFCRYPTO::AES128,PKIFCRYPTO::ECB,16,16);
00023 static CPKIFAlgorithm aes192ECB("AES-192 ECB",PKIFCRYPTO::AES192,PKIFCRYPTO::ECB,24,16);
00024 static CPKIFAlgorithm aes256ECB("AES-256 ECB",PKIFCRYPTO::AES256,PKIFCRYPTO::ECB,32,16);
00025 
00026 static CPKIFAlgorithm aes128Wrap("AES-128 Wrap",PKIFCRYPTO::AES128Wrap,PKIFCRYPTO::ECB,16,8);
00027 static CPKIFAlgorithm aes192Wrap("AES-192 Wrap",PKIFCRYPTO::AES192Wrap,PKIFCRYPTO::ECB,24,8);
00028 static CPKIFAlgorithm aes256Wrap("AES-256 Wrap",PKIFCRYPTO::AES256Wrap,PKIFCRYPTO::ECB,32,8);
00029 
00030 static CPKIFAlgorithm desCBC("DES CBC",PKIFCRYPTO::DES,PKIFCRYPTO::CBC,8,8);
00031 static CPKIFAlgorithm tdesCBC("Triple DES CBC",PKIFCRYPTO::TDES,PKIFCRYPTO::CBC,24,8);
00032 static CPKIFAlgorithm aes128CBC("AES-128 CBC",PKIFCRYPTO::AES128,PKIFCRYPTO::CBC,16,16);
00033 static CPKIFAlgorithm aes192CBC("AES-192 CBC",PKIFCRYPTO::AES192,PKIFCRYPTO::CBC,24,16);
00034 static CPKIFAlgorithm aes256CBC("AES-256 CBC",PKIFCRYPTO::AES256,PKIFCRYPTO::CBC,32,16);
00035 
00036 static CPKIFAlgorithm rsa("RSA",PKIFCRYPTO::RSA);
00037 static CPKIFAlgorithm dss("DSS",PKIFCRYPTO::DSS);
00038 static CPKIFAlgorithm ecc("ECC",PKIFCRYPTO::ECC);
00039 
00040 static CPKIFAlgorithm rsasha512("RSA with SHA512",PKIFCRYPTO::RSA,PKIFCRYPTO::SHA512);
00041 static CPKIFAlgorithm rsasha384("RSA with SHA384",PKIFCRYPTO::RSA,PKIFCRYPTO::SHA384);
00042 static CPKIFAlgorithm rsasha256("RSA with SHA256",PKIFCRYPTO::RSA,PKIFCRYPTO::SHA256);
00043 static CPKIFAlgorithm rsasha224("RSA with SHA224",PKIFCRYPTO::RSA,PKIFCRYPTO::SHA224);
00044 static CPKIFAlgorithm rsasha1("RSA with SHA1",PKIFCRYPTO::RSA,PKIFCRYPTO::SHA1);
00045 static CPKIFAlgorithm rsamd5("RSA with MD5",PKIFCRYPTO::RSA,PKIFCRYPTO::MD5);
00046 static CPKIFAlgorithm dsasha1("DSA with SHA1",PKIFCRYPTO::DSS,PKIFCRYPTO::SHA1);
00047 static CPKIFAlgorithm ecdsasha1("ECDSA with SHA1",PKIFCRYPTO::ECC,PKIFCRYPTO::SHA1);
00048 static CPKIFAlgorithm ecdsasha224("ECDSA with SHA224",PKIFCRYPTO::ECC,PKIFCRYPTO::SHA224);
00049 static CPKIFAlgorithm ecdsasha256("ECDSA with SHA256",PKIFCRYPTO::ECC,PKIFCRYPTO::SHA256);
00050 static CPKIFAlgorithm ecdsasha384("ECDSA with SHA384",PKIFCRYPTO::ECC,PKIFCRYPTO::SHA384);
00051 static CPKIFAlgorithm ecdsasha512("ECDSA with SHA512",PKIFCRYPTO::ECC,PKIFCRYPTO::SHA512);
00052 
00053 //static CPKIFAlgorithm ecdhStandardDHSHA1KDF("Standard ECDH with SHA1 KDF",PKIFCRYPTO::ECC,PKIFCRYPTO::SHA1);
00054 //static CPKIFAlgorithm ecdhCofactorDHSHA1KDF("Cofactor ECDH with SHA1 KDF",PKIFCRYPTO::ECC,PKIFCRYPTO::SHA1);
00055 //static CPKIFAlgorithm ecmqvSinglePassSHA1KDF("Single Pass ECMQV",PKIFCRYPTO::ECC,PKIFCRYPTO::SHA1);
00056 static CPKIFAlgorithm ecdhStandardDHSHA256KDF("Standard ECDH with SHA256 KDF",PKIFCRYPTO::ECC,PKIFCRYPTO::SHA256);
00057 static CPKIFAlgorithm ecdhStandardDHSHA384KDF("Standard ECDH with SHA384 KDF",PKIFCRYPTO::ECC,PKIFCRYPTO::SHA384);
00058 
00059 static CPKIFAlgorithm invalid;
00060 
00061 static bool s_initialized = false;
00063 struct PKIFAlgorithmImpl
00064 {
00065     // with the introduction of signature algs,
00066     // this can no longer be a union since both
00067     // the asymAlg member and the hashAlg member
00068     // need to be populated.
00069     PKIFCRYPTO::HASH_ALG hashAlg;
00070     PKIFCRYPTO::SYMKEY_ALG symAlg;
00071     PKIFCRYPTO::ASYMKEY_ALG asymAlg;
00072     int blocklen;
00073     int keylen;
00074     int digestlen;
00075     PKIFCRYPTO::SYMKEY_MODE mode;
00076     CPKIFOIDPtr oid;
00077     std::string description;
00078 
00079     PKIFAlgorithmImpl();
00080 };
00082 
00089 PKIFAlgorithmImpl::PKIFAlgorithmImpl()
00090 {
00091     blocklen = -1;
00092     keylen = -1;
00093     digestlen = -1;
00094     description = "Uninitialized";
00095 }
00096 
00104 CPKIFAlgorithm::CPKIFAlgorithm(
00106     const std::string & description,
00108     PKIFCRYPTO::HASH_ALG alg, 
00110     int digestsize)
00111 :m_impl(new PKIFAlgorithmImpl)
00112 {
00113     m_impl->hashAlg = alg;
00114     m_impl->digestlen = digestsize;
00115     m_impl->description = description;
00116 }
00124 CPKIFAlgorithm::CPKIFAlgorithm(
00126     const std::string & description,
00128     PKIFCRYPTO::SYMKEY_ALG alg, 
00130     PKIFCRYPTO::SYMKEY_MODE mode, 
00132     int keylen, 
00134     int blocksize)
00135 :m_impl(new PKIFAlgorithmImpl)
00136 {
00137     m_impl->symAlg = alg;
00138     m_impl->keylen = keylen;
00139     m_impl->blocklen = blocksize;
00140     m_impl->description = description;
00141     m_impl->mode = mode;
00142 }
00150 CPKIFAlgorithm::CPKIFAlgorithm(
00152     const std::string & description, 
00154     PKIFCRYPTO::ASYMKEY_ALG alg)
00155 :m_impl(new PKIFAlgorithmImpl)
00156 {
00157     m_impl->asymAlg = alg;
00158     m_impl->description = description;
00159 }
00160 
00168 CPKIFAlgorithm::CPKIFAlgorithm(
00170     const std::string & description,
00172     PKIFCRYPTO::ASYMKEY_ALG ka,
00174     PKIFCRYPTO::HASH_ALG ha)
00175 :m_impl(new PKIFAlgorithmImpl)
00176 {
00177     m_impl->asymAlg = ka;
00178     m_impl->hashAlg = ha;
00179     m_impl->description = description;
00180 }
00188 CPKIFAlgorithm::CPKIFAlgorithm(void)
00189 :m_impl(new PKIFAlgorithmImpl)
00190 {
00191 }
00199 CPKIFAlgorithm::~CPKIFAlgorithm(void)
00200 {
00201     PKIFDelete(m_impl);
00202     m_impl = 0;
00203 }
00211 CPKIFAlgorithm * CPKIFAlgorithm::GetAlg(
00213     PKIFCRYPTO::HASH_ALG alg)
00214 {
00215     if(!s_initialized) {
00216         init();
00217     }
00218     switch(alg)
00219     {
00220     case PKIFCRYPTO::MD5:
00221         return &md5;
00222         break;
00223     case PKIFCRYPTO::SHA1:
00224         return &sha1;
00225         break;
00226     case PKIFCRYPTO::SHA256:
00227         return &sha256;
00228         break;
00229     case PKIFCRYPTO::SHA384:
00230         return &sha384;
00231         break;
00232     case PKIFCRYPTO::SHA512:
00233         return &sha512;
00234         break;
00235     default:
00236         break;
00237     }
00238     return &invalid;
00239 }
00247 CPKIFAlgorithm * CPKIFAlgorithm::GetAlg(
00249     PKIFCRYPTO::SYMKEY_ALG alg, 
00251     PKIFCRYPTO::SYMKEY_MODE mode)
00252 {
00253     if(!s_initialized) {
00254         init();
00255     }
00256     switch(alg)
00257     {
00258     case PKIFCRYPTO::DES:
00259         return (mode == PKIFCRYPTO::ECB) ? &desECB : &desCBC;
00260         break;
00261     case PKIFCRYPTO::TDES:
00262         return (mode == PKIFCRYPTO::ECB) ? &tdesECB : &tdesCBC;
00263         break;
00264     case PKIFCRYPTO::AES:
00265     case PKIFCRYPTO::AES128:
00266         return (mode == PKIFCRYPTO::ECB) ? &aes128ECB : &aes128CBC;
00267         break;
00268     case PKIFCRYPTO::AES192:
00269         return (mode == PKIFCRYPTO::ECB) ? &aes192ECB : &aes192CBC;
00270         break;
00271     case PKIFCRYPTO::AES256:
00272         return (mode == PKIFCRYPTO::ECB) ? &aes256ECB : &aes256CBC;
00273         break;
00274     case PKIFCRYPTO::AES128Wrap:
00275         return &aes128Wrap;
00276         break;
00277     case PKIFCRYPTO::AES192Wrap:
00278         return &aes192Wrap;
00279         break;
00280     case PKIFCRYPTO::AES256Wrap:
00281         return &aes256Wrap;
00282         break;
00283     default:
00284         break;
00285     }
00286     return &invalid;
00287 }
00295 CPKIFAlgorithm * CPKIFAlgorithm::GetAlg(
00297     PKIFCRYPTO::ASYMKEY_ALG alg)
00298 {
00299     if(!s_initialized) {
00300         init();
00301     }
00302     switch(alg)
00303     {
00304     case PKIFCRYPTO::RSA:
00305         return &rsa;
00306         break;
00307     case PKIFCRYPTO::DSS:
00308         return &dss;
00309         break;
00310     case PKIFCRYPTO::ECC:
00311         return &ecc;
00312         break;
00313     default:
00314         break;
00315     }
00316     return &invalid;
00317 }
00325 CPKIFAlgorithm * CPKIFAlgorithm::GetAlg(
00327     CPKIFOIDPtr oid)
00328 {
00329     if(!s_initialized) init();
00330     if(*oid == *g_md5) return &md5;
00331     if(*oid == *g_sha1) return &sha1;
00332     if(*oid == *g_sha256) return &sha256;
00333     if(*oid == *g_sha384) return &sha384;
00334     if(*oid == *g_sha512) return &sha512;
00335     if(*oid == *g_sha224) return &sha224;
00336 
00337     if(*oid == *g_desECB) return &desECB;
00338     if(*oid == *g_tdesECB) return &tdesECB;
00339     if(*oid == *g_aes128ECB) return &aes128ECB;
00340     if(*oid == *g_aes192ECB) return &aes192ECB;
00341     if(*oid == *g_aes256ECB) return &aes256ECB;
00342 
00343     if(*oid == *g_desCBC) return &desCBC;
00344     if(*oid == *g_tdesCBC) return &tdesCBC;
00345     if(*oid == *g_aes128CBC) return &aes128CBC;
00346     if(*oid == *g_aes192CBC) return &aes192CBC;
00347     if(*oid == *g_aes256CBC) return &aes256CBC;
00348 
00349     if(*oid == *g_aes128Wrap) return &aes128Wrap;
00350     if(*oid == *g_aes192Wrap) return &aes192Wrap;
00351     if(*oid == *g_aes256Wrap) return &aes256Wrap;
00352     
00353     if(*oid == *g_rsa) return &rsa;
00354     if(*oid == *g_dsa) return &dss;
00355     if(*oid == *g_ecc) return &ecc;
00356 
00357     if(*oid == g_md5WithRSAEncryption) return &rsamd5;
00358     if(*oid == g_sha1WithRSAEncryption) return &rsasha1;
00359     if(*oid == g_sha224WithRSAEncryption) return &rsasha224;
00360     if(*oid == g_sha256WithRSAEncryption) return &rsasha256;
00361     if(*oid == g_sha384WithRSAEncryption) return &rsasha384;
00362     if(*oid == g_sha512WithRSAEncryption) return &rsasha512;
00363     if(*oid == g_dsaWithSHA1) return &dsasha1;
00364     if(*oid == g_dsaWithSHA1Alternative) return &dsasha1;
00365 
00366     if(*oid == g_ecdsa_sha1) return &ecdsasha1;
00367     if(*oid == g_ecdsa_sha224) return &ecdsasha224;
00368     if(*oid == g_ecdsa_sha256) return &ecdsasha256;
00369     if(*oid == g_ecdsa_sha384) return &ecdsasha384;
00370     if(*oid == g_ecdsa_sha512) return &ecdsasha512;
00371 
00372     // We were misusing the old OIDs before, pairing them with
00373     // the draft suite b standard KDF. Now that it's standardized
00374     // and has its own OIDs, use those instead and disable support
00375     // for the old ones, which might not interoperate well anyway.
00376     // if support for these OIDs returns, implement the X9.63 KDF
00377     // to accompany them
00378     // if(*oid == g_ecdh_std_sha1kdf) return &ecdhStandardDH;
00379     //if(*oid == g_ecdh_cofactor_sha1kdf) return &ecdhCofactorDH;
00380     //if(*oid == g_ecmqv_sha1kdf) return &ecmqvSinglePass;
00381     if(*oid == g_ecdh_std_sha256kdf) return &ecdhStandardDHSHA256KDF;
00382     if(*oid == g_ecdh_std_sha384kdf) return &ecdhStandardDHSHA384KDF;
00383 
00384     return 0;
00385 
00386 }
00387 
00388 // the OIDs need to be initialized here rather than in c'tors because
00389 // at the time the c'tors are called the global oids may be invalid
00397 void CPKIFAlgorithm::init(void)
00398 {
00399     if(s_initialized) return;
00400     md5.m_impl->oid = g_md5;
00401     sha1.m_impl->oid = g_sha1;
00402     sha256.m_impl->oid = g_sha256;
00403     sha384.m_impl->oid = g_sha384;
00404     sha512.m_impl->oid = g_sha512;
00405     sha224.m_impl->oid = g_sha224;
00406 
00407     desECB.m_impl->oid = g_desECB;
00408     tdesECB.m_impl->oid = g_tdesECB;
00409     aes128ECB.m_impl->oid = g_aes128ECB;
00410     aes192ECB.m_impl->oid = g_aes192ECB;
00411     aes256ECB.m_impl->oid = g_aes256ECB;
00412 
00413     desCBC.m_impl->oid = g_desCBC;
00414     tdesCBC.m_impl->oid = g_tdesCBC;
00415     aes128CBC.m_impl->oid = g_aes128CBC;
00416     aes192CBC.m_impl->oid = g_aes192CBC;
00417     aes256CBC.m_impl->oid = g_aes256CBC;
00418 
00419     aes128Wrap.m_impl->oid = g_aes128Wrap;
00420     aes192Wrap.m_impl->oid = g_aes192Wrap;
00421     aes256Wrap.m_impl->oid = g_aes256Wrap;
00422     
00423     rsa.m_impl->oid = g_rsa;
00424     dss.m_impl->oid = g_dsa;
00425     ecc.m_impl->oid = g_ecc;
00426 
00427     rsasha1.m_impl->oid = g_sha1WithRSAEncryption;
00428     rsasha224.m_impl->oid = g_sha224WithRSAEncryption;
00429     rsasha256.m_impl->oid = g_sha256WithRSAEncryption;
00430     rsasha384.m_impl->oid = g_sha384WithRSAEncryption;
00431     rsasha512.m_impl->oid = g_sha512WithRSAEncryption;
00432     rsamd5.m_impl->oid = g_md5WithRSAEncryption;
00433     dsasha1.m_impl->oid = g_dsaWithSHA1;
00434 
00435     ecdsasha1.m_impl->oid = g_ecdsa_sha1;
00436     ecdsasha224.m_impl->oid = g_ecdsa_sha224;
00437     ecdsasha256.m_impl->oid = g_ecdsa_sha256;
00438     ecdsasha384.m_impl->oid = g_ecdsa_sha384;
00439     ecdsasha512.m_impl->oid = g_ecdsa_sha512;
00440     
00441     ecdhStandardDHSHA256KDF.m_impl->oid = g_ecdh_std_sha256kdf;
00442     ecdhStandardDHSHA384KDF.m_impl->oid = g_ecdh_std_sha384kdf;
00443     
00444     s_initialized = true;
00445 
00446 }
00454 const std::string & CPKIFAlgorithm::Description(void) const
00455 {
00456     return m_impl->description;
00457 }
00465 int CPKIFAlgorithm::BlockSize(void) const
00466 {
00467     return m_impl->blocklen;
00468 }
00476 int CPKIFAlgorithm::DigestSize(void) const
00477 {
00478     return m_impl->digestlen;
00479 }
00487 int CPKIFAlgorithm::KeySize(void) const
00488 {
00489     return m_impl->keylen;
00490 }
00498 CPKIFOIDPtr CPKIFAlgorithm::OID() const
00499 {
00500     return m_impl->oid;
00501 }
00509 bool CPKIFAlgorithm::NeedsIV() const
00510 {
00511     switch(m_impl->mode)
00512     {
00513     case PKIFCRYPTO::CBC:
00514     case PKIFCRYPTO::CFB8:
00515     case PKIFCRYPTO::CFB64:
00516     case PKIFCRYPTO::OFB8:
00517     case PKIFCRYPTO::OFB64:
00518         return true;
00519 
00520     case PKIFCRYPTO::ECB:
00521     default:
00522         return false;
00523         break;
00524     }
00525     return false;
00526 }
00534 PKIFCRYPTO::HASH_ALG CPKIFAlgorithm::HashAlg(void) const
00535 {
00536     return m_impl->hashAlg;
00537 }
00545 PKIFCRYPTO::SYMKEY_ALG CPKIFAlgorithm::SymkeyAlg(void) const
00546 {
00547     return m_impl->symAlg;
00548 }
00556 PKIFCRYPTO::SYMKEY_MODE CPKIFAlgorithm::SymkeyMode(void) const
00557 {
00558     return m_impl->mode;
00559 }
00567 PKIFCRYPTO::ASYMKEY_ALG CPKIFAlgorithm::AsymkeyAlg(void) const
00568 {
00569     return m_impl->asymAlg;
00570 }

Generated on Mon Nov 15 11:15:53 2010 for PublicKeyInfrastructureFramework(PKIF) by  doxygen 1.5.6