22 #if !defined(USE_CRYPTOPP_LIB)
24 #include "runtime.hpp"
25 #include "crypto_wrapper.hpp"
26 #include "cert_wrapper.hpp"
55 MD5::MD5() : pimpl_(NEW_YS
MD5Impl) {}
58 MD5::~MD5() { ysDelete(pimpl_); }
61 MD5::MD5(
const MD5& that) : Digest(), pimpl_(NEW_YS
62 MD5Impl(that.pimpl_->md5_)) {}
65 MD5& MD5::operator=(
const MD5& that)
67 pimpl_->md5_ = that.pimpl_->md5_;
72 uint MD5::get_digestSize()
const
78 uint MD5::get_padSize()
const
85 void MD5::get_digest(byte* out,
const byte* in,
unsigned int sz)
87 pimpl_->md5_.Update(in, sz);
88 pimpl_->md5_.Final(out);
92 void MD5::get_digest(byte* out)
94 pimpl_->md5_.Final(out);
99 void MD5::update(
const byte* in,
unsigned int sz)
101 pimpl_->md5_.Update(in, sz);
113 SHA::SHA() : pimpl_(NEW_YS
SHAImpl) {}
116 SHA::~SHA() { ysDelete(pimpl_); }
119 SHA::SHA(
const SHA& that) : Digest(), pimpl_(NEW_YS SHAImpl(that.pimpl_->sha_)) {}
121 SHA& SHA::operator=(
const SHA& that)
123 pimpl_->sha_ = that.pimpl_->sha_;
128 uint SHA::get_digestSize()
const
134 uint SHA::get_padSize()
const
141 void SHA::get_digest(byte* out,
const byte* in,
unsigned int sz)
143 pimpl_->sha_.Update(in, sz);
144 pimpl_->sha_.Final(out);
149 void SHA::get_digest(byte* out)
151 pimpl_->sha_.Final(out);
156 void SHA::update(
const byte* in,
unsigned int sz)
158 pimpl_->sha_.Update(in, sz);
170 RMD::RMD() : pimpl_(NEW_YS
RMDImpl) {}
173 RMD::~RMD() { ysDelete(pimpl_); }
176 RMD::RMD(
const RMD& that) : Digest(), pimpl_(NEW_YS RMDImpl(that.pimpl_->rmd_)) {}
178 RMD& RMD::operator=(
const RMD& that)
180 pimpl_->rmd_ = that.pimpl_->rmd_;
185 uint RMD::get_digestSize()
const
191 uint RMD::get_padSize()
const
198 void RMD::get_digest(byte* out,
const byte* in,
unsigned int sz)
200 pimpl_->rmd_.Update(in, sz);
201 pimpl_->rmd_.Final(out);
206 void RMD::get_digest(byte* out)
208 pimpl_->rmd_.Final(out);
213 void RMD::update(
const byte* in,
unsigned int sz)
215 pimpl_->rmd_.Update(in, sz);
226 HMAC_MD5::HMAC_MD5(
const byte* secret,
unsigned int len)
229 pimpl_->mac_.SetKey(secret, len);
233 HMAC_MD5::~HMAC_MD5() { ysDelete(pimpl_); }
236 uint HMAC_MD5::get_digestSize()
const
242 uint HMAC_MD5::get_padSize()
const
249 void HMAC_MD5::get_digest(byte* out,
const byte* in,
unsigned int sz)
251 pimpl_->mac_.Update(in, sz);
252 pimpl_->mac_.Final(out);
256 void HMAC_MD5::get_digest(byte* out)
258 pimpl_->mac_.Final(out);
263 void HMAC_MD5::update(
const byte* in,
unsigned int sz)
265 pimpl_->mac_.Update(in, sz);
276 HMAC_SHA::HMAC_SHA(
const byte* secret,
unsigned int len)
279 pimpl_->mac_.SetKey(secret, len);
283 HMAC_SHA::~HMAC_SHA() { ysDelete(pimpl_); }
286 uint HMAC_SHA::get_digestSize()
const
292 uint HMAC_SHA::get_padSize()
const
299 void HMAC_SHA::get_digest(byte* out,
const byte* in,
unsigned int sz)
301 pimpl_->mac_.Update(in, sz);
302 pimpl_->mac_.Final(out);
306 void HMAC_SHA::get_digest(byte* out)
308 pimpl_->mac_.Final(out);
313 void HMAC_SHA::update(
const byte* in,
unsigned int sz)
315 pimpl_->mac_.Update(in, sz);
327 HMAC_RMD::HMAC_RMD(
const byte* secret,
unsigned int len)
330 pimpl_->mac_.SetKey(secret, len);
334 HMAC_RMD::~HMAC_RMD() { ysDelete(pimpl_); }
337 uint HMAC_RMD::get_digestSize()
const
343 uint HMAC_RMD::get_padSize()
const
350 void HMAC_RMD::get_digest(byte* out,
const byte* in,
unsigned int sz)
352 pimpl_->mac_.Update(in, sz);
353 pimpl_->mac_.Final(out);
357 void HMAC_RMD::get_digest(byte* out)
359 pimpl_->mac_.Final(out);
364 void HMAC_RMD::update(
const byte* in,
unsigned int sz)
366 pimpl_->mac_.Update(in, sz);
376 DES::DES() : pimpl_(NEW_YS
DESImpl) {}
378 DES::~DES() { ysDelete(pimpl_); }
381 void DES::set_encryptKey(
const byte* k,
const byte* iv)
383 pimpl_->encryption.SetKey(k, DES_KEY_SZ, iv);
387 void DES::set_decryptKey(
const byte* k,
const byte* iv)
389 pimpl_->decryption.SetKey(k, DES_KEY_SZ, iv);
393 void DES::encrypt(byte* cipher,
const byte* plain,
unsigned int sz)
395 pimpl_->encryption.Process(cipher, plain, sz);
400 void DES::decrypt(byte* plain,
const byte* cipher,
unsigned int sz)
402 pimpl_->decryption.Process(plain, cipher, sz);
414 DES_EDE::~DES_EDE() { ysDelete(pimpl_); }
417 void DES_EDE::set_encryptKey(
const byte* k,
const byte* iv)
419 pimpl_->encryption.SetKey(k, DES_EDE_KEY_SZ, iv);
423 void DES_EDE::set_decryptKey(
const byte* k,
const byte* iv)
425 pimpl_->decryption.SetKey(k, DES_EDE_KEY_SZ, iv);
430 void DES_EDE::encrypt(byte* cipher,
const byte* plain,
unsigned int sz)
432 pimpl_->encryption.Process(cipher, plain, sz);
437 void DES_EDE::decrypt(byte* plain,
const byte* cipher,
unsigned int sz)
439 pimpl_->decryption.Process(plain, cipher, sz);
450 RC4::RC4() : pimpl_(NEW_YS
RC4Impl) {}
452 RC4::~RC4() { ysDelete(pimpl_); }
455 void RC4::set_encryptKey(
const byte* k,
const byte*)
457 pimpl_->encryption.SetKey(k, RC4_KEY_SZ);
461 void RC4::set_decryptKey(
const byte* k,
const byte*)
463 pimpl_->decryption.SetKey(k, RC4_KEY_SZ);
468 void RC4::encrypt(byte* cipher,
const byte* plain,
unsigned int sz)
470 pimpl_->encryption.Process(cipher, plain, sz);
475 void RC4::decrypt(byte* plain,
const byte* cipher,
unsigned int sz)
477 pimpl_->decryption.Process(plain, cipher, sz);
488 AESImpl(
unsigned int ks) : keySz_(ks) {}
492 AES::AES(
unsigned int ks) : pimpl_(NEW_YS
AESImpl(ks)) {}
494 AES::~AES() { ysDelete(pimpl_); }
497 int AES::get_keySize()
const
499 return pimpl_->keySz_;
503 void AES::set_encryptKey(
const byte* k,
const byte* iv)
505 pimpl_->encryption.SetKey(k, pimpl_->keySz_, iv);
509 void AES::set_decryptKey(
const byte* k,
const byte* iv)
511 pimpl_->decryption.SetKey(k, pimpl_->keySz_, iv);
516 void AES::encrypt(byte* cipher,
const byte* plain,
unsigned int sz)
518 pimpl_->encryption.Process(cipher, plain, sz);
523 void AES::decrypt(byte* plain,
const byte* cipher,
unsigned int sz)
525 pimpl_->decryption.Process(plain, cipher, sz);
533 RandomPool::RandomPool() : pimpl_(NEW_YS
RandomImpl) {}
535 RandomPool::~RandomPool() { ysDelete(pimpl_); }
537 int RandomPool::GetError()
const
539 return pimpl_->RNG_.GetError();
542 void RandomPool::Fill(opaque* dst, uint sz)
const
544 pimpl_->RNG_.GenerateBlock(dst, sz);
550 void SetPublic (
const byte*,
unsigned int);
551 void SetPrivate(
const byte*,
unsigned int);
558 void DSS::DSSImpl::SetPublic(
const byte* key,
unsigned int sz)
561 publicKey_.Initialize(source);
566 void DSS::DSSImpl::SetPrivate(
const byte* key,
unsigned int sz)
569 privateKey_.Initialize(source);
576 DSS::DSS(
const byte* key,
unsigned int sz,
bool publicKey)
577 : pimpl_(NEW_YS DSSImpl)
580 pimpl_->SetPublic(key, sz);
582 pimpl_->SetPrivate(key, sz);
592 uint DSS::get_signatureLength()
const
594 return pimpl_->publicKey_.SignatureLength();
599 void DSS::sign(byte* sig,
const byte* sha_digest,
unsigned int ,
600 const RandomPool& random)
602 using namespace TaoCrypt;
605 signer.Sign(sha_digest, sig, random.pimpl_->RNG_);
610 bool DSS::verify(
const byte* sha_digest,
unsigned int ,
611 const byte* sig,
unsigned int )
613 using namespace TaoCrypt;
616 return ver.Verify(sha_digest, sig);
621 struct RSA::RSAImpl {
622 void SetPublic (
const byte*,
unsigned int);
623 void SetPrivate(
const byte*,
unsigned int);
630 void RSA::RSAImpl::SetPublic(
const byte* key,
unsigned int sz)
633 publicKey_.Initialize(source);
638 void RSA::RSAImpl::SetPrivate(
const byte* key,
unsigned int sz)
641 privateKey_.Initialize(source);
647 RSA::RSA(
const byte* key,
unsigned int sz,
bool publicKey)
648 : pimpl_(NEW_YS RSAImpl)
651 pimpl_->SetPublic(key, sz);
653 pimpl_->SetPrivate(key, sz);
663 unsigned int RSA::get_cipherLength()
const
665 return pimpl_->publicKey_.FixedCiphertextLength();
670 unsigned int RSA::get_signatureLength()
const
672 return get_cipherLength();
677 void RSA::sign(byte* sig,
const byte*
message,
unsigned int sz,
678 const RandomPool& random)
681 dec.SSL_Sign(message, sz, sig, random.pimpl_->RNG_);
686 bool RSA::verify(
const byte* message,
unsigned int sz,
const byte* sig,
690 return enc.SSL_Verify(message, sz, sig);
695 void RSA::encrypt(byte* cipher,
const byte* plain,
unsigned int sz,
696 const RandomPool& random)
700 enc.Encrypt(plain, sz, cipher, random.pimpl_->RNG_);
705 void RSA::decrypt(byte* plain,
const byte* cipher,
unsigned int sz,
706 const RandomPool& random)
709 dec.Decrypt(cipher, sz, plain, random.pimpl_->RNG_);
722 Integer::~Integer() { ysDelete(pimpl_); }
726 Integer::Integer(
const Integer& other) : pimpl_(NEW_YS
727 IntegerImpl(other.pimpl_->int_))
733 pimpl_->int_ = that.pimpl_->int_;
739 void Integer::assign(
const byte* num,
unsigned int sz)
753 privateKey_(0), agreedKey_(0) {}
756 ysArrayDelete(agreedKey_);
757 ysArrayDelete(privateKey_);
758 ysArrayDelete(publicKey_);
761 DHImpl(
const DHImpl& that) : dh_(that.dh_), ranPool_(that.ranPool_),
762 publicKey_(0), privateKey_(0), agreedKey_(0)
764 uint length = dh_.GetByteLength();
765 AllocKeys(length, length, length);
768 void AllocKeys(
unsigned int pubSz,
unsigned int privSz,
unsigned int agrSz)
770 publicKey_ = NEW_YS byte[pubSz];
771 privateKey_ = NEW_YS byte[privSz];
772 agreedKey_ = NEW_YS byte[agrSz];
802 DiffieHellman::DiffieHellman(
const byte* p,
unsigned int pSz,
const byte* g,
803 unsigned int gSz,
const byte* pub,
805 : pimpl_(NEW_YS
DHImpl(random.pimpl_->RNG_))
809 pimpl_->dh_.Initialize(
Integer(p, pSz).Ref(),
Integer(g, gSz).Ref());
810 pimpl_->publicKey_ = NEW_YS opaque[pubSz];
811 memcpy(pimpl_->publicKey_, pub, pubSz);
818 : pimpl_(NEW_YS DHImpl(random.pimpl_->RNG_))
822 pimpl_->dh_.Initialize(p.pimpl_->int_, g.pimpl_->int_);
824 uint length = pimpl_->dh_.GetByteLength();
826 pimpl_->AllocKeys(length, length, length);
827 pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_,
831 DiffieHellman::~DiffieHellman() { ysDelete(pimpl_); }
835 DiffieHellman::DiffieHellman(
const DiffieHellman& that)
836 : pimpl_(NEW_YS DHImpl(*that.pimpl_))
838 pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_,
843 DiffieHellman& DiffieHellman::operator=(
const DiffieHellman& that)
845 pimpl_->dh_ = that.pimpl_->dh_;
846 pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_,
852 void DiffieHellman::makeAgreement(
const byte* other,
unsigned int otherSz)
854 pimpl_->dh_.Agree(pimpl_->agreedKey_, pimpl_->privateKey_, other, otherSz);
858 uint DiffieHellman::get_agreedKeyLength()
const
860 return pimpl_->dh_.GetByteLength();
864 const byte* DiffieHellman::get_agreedKey()
const
866 return pimpl_->agreedKey_;
870 const byte* DiffieHellman::get_publicKey()
const
872 return pimpl_->publicKey_;
876 void DiffieHellman::set_sizes(
int& pSz,
int& gSz,
int& pubSz)
const
879 Integer p = pimpl_->dh_.GetP();
880 Integer g = pimpl_->dh_.GetG();
884 pubSz = pimpl_->dh_.GetByteLength();
888 void DiffieHellman::get_parms(byte* bp, byte* bg, byte* bpub)
const
891 Integer p = pimpl_->dh_.GetP();
892 Integer g = pimpl_->dh_.GetG();
894 p.Encode(bp, p.ByteCount());
895 g.Encode(bg, g.ByteCount());
896 memcpy(bpub, pimpl_->publicKey_, pimpl_->dh_.GetByteLength());
901 x509* PemToDer(FILE*
file, CertType
type, EncryptedInfo* info)
903 using namespace TaoCrypt;
909 strncpy(header,
"-----BEGIN CERTIFICATE-----",
sizeof(header));
910 strncpy(footer,
"-----END CERTIFICATE-----",
sizeof(footer));
912 strncpy(header,
"-----BEGIN RSA PRIVATE KEY-----",
sizeof(header));
913 strncpy(footer,
"-----END RSA PRIVATE KEY-----",
sizeof(header));
918 bool foundEnd =
false;
922 while(fgets(line,
sizeof(line), file))
923 if (strncmp(header, line, strlen(header)) == 0) {
929 if (fgets(line,
sizeof(line), file)) {
930 char encHeader[] =
"Proc-Type";
931 if (strncmp(encHeader, line, strlen(encHeader)) == 0 &&
932 fgets(line,
sizeof(line), file)) {
934 char* start = strstr(line,
"DES");
935 char* finish = strstr(line,
",");
937 start = strstr(line,
"AES");
941 if ( start && finish && (start < finish)) {
942 memcpy(info->name, start, finish - start);
943 info->name[finish - start] = 0;
944 memcpy(info->iv, finish + 1,
sizeof(info->iv));
946 char* newline = strstr(line,
"\r");
947 if (!newline) newline = strstr(line,
"\n");
948 if (newline && (newline > finish)) {
949 info->ivSz = newline - (finish + 1);
954 if (fgets(line,
sizeof(line), file))
960 while(fgets(line,
sizeof(line), file))
961 if (strncmp(footer, line, strlen(footer)) == 0) {
968 if (begin == -1 || !foundEnd)
971 input_buffer tmp(end - begin);
972 fseek(file, begin, SEEK_SET);
973 size_t bytes = fread(tmp.get_buffer(), end - begin, 1,
file);
977 Source der(tmp.get_buffer(), end - begin);
980 uint sz = der.size();
982 memcpy(x->use_buffer(), der.get_buffer(), sz);
991 #ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
993 template void ysDelete<DiffieHellman::DHImpl>(DiffieHellman::DHImpl*);
994 template void ysDelete<Integer::IntegerImpl>(Integer::IntegerImpl*);
995 template void ysDelete<RSA::RSAImpl>(RSA::RSAImpl*);
996 template void ysDelete<DSS::DSSImpl>(DSS::DSSImpl*);
997 template void ysDelete<RandomPool::RandomImpl>(RandomPool::RandomImpl*);
998 template void ysDelete<AES::AESImpl>(AES::AESImpl*);
999 template void ysDelete<RC4::RC4Impl>(RC4::RC4Impl*);
1000 template void ysDelete<DES_EDE::DES_EDEImpl>(DES_EDE::DES_EDEImpl*);
1001 template void ysDelete<DES::DESImpl>(DES::DESImpl*);
1002 template void ysDelete<HMAC_RMD::HMAC_RMDImpl>(HMAC_RMD::HMAC_RMDImpl*);
1003 template void ysDelete<HMAC_SHA::HMAC_SHAImpl>(HMAC_SHA::HMAC_SHAImpl*);
1004 template void ysDelete<HMAC_MD5::HMAC_MD5Impl>(HMAC_MD5::HMAC_MD5Impl*);
1005 template void ysDelete<RMD::RMDImpl>(RMD::RMDImpl*);
1006 template void ysDelete<SHA::SHAImpl>(SHA::SHAImpl*);
1007 template void ysDelete<MD5::MD5Impl>(MD5::MD5Impl*);
1009 #endif // HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
1011 #endif // !USE_CRYPTOPP_LIB