📄 crypto_wrapper.cpp
字号:
AES::~AES() { ysDelete(pimpl_); }int AES::get_keySize() const{ return pimpl_->keySz_;}void AES::set_encryptKey(const byte* k, const byte* iv){ pimpl_->encryption.SetKey(k, pimpl_->keySz_, iv);}void AES::set_decryptKey(const byte* k, const byte* iv){ pimpl_->decryption.SetKey(k, pimpl_->keySz_, iv);}// AES encrypt plain of length sz into ciphervoid AES::encrypt(byte* cipher, const byte* plain, unsigned int sz){ pimpl_->encryption.Process(cipher, plain, sz);}// AES decrypt cipher of length sz into plainvoid AES::decrypt(byte* plain, const byte* cipher, unsigned int sz){ pimpl_->decryption.Process(plain, cipher, sz);}struct RandomPool::RandomImpl { TaoCrypt::RandomNumberGenerator RNG_;};RandomPool::RandomPool() : pimpl_(new (ys) RandomImpl) {}RandomPool::~RandomPool() { ysDelete(pimpl_); }int RandomPool::GetError() const{ return pimpl_->RNG_.GetError(); }void RandomPool::Fill(opaque* dst, uint sz) const{ pimpl_->RNG_.GenerateBlock(dst, sz);}// Implementation of DSS Authenticationstruct DSS::DSSImpl { void SetPublic (const byte*, unsigned int); void SetPrivate(const byte*, unsigned int); TaoCrypt::DSA_PublicKey publicKey_; TaoCrypt::DSA_PrivateKey privateKey_;};// Decode and store the public keyvoid DSS::DSSImpl::SetPublic(const byte* key, unsigned int sz){ TaoCrypt::Source source(key, sz); publicKey_.Initialize(source);}// Decode and store the public keyvoid DSS::DSSImpl::SetPrivate(const byte* key, unsigned int sz){ TaoCrypt::Source source(key, sz); privateKey_.Initialize(source); publicKey_ = TaoCrypt::DSA_PublicKey(privateKey_);}// Set public or private keyDSS::DSS(const byte* key, unsigned int sz, bool publicKey) : pimpl_(new (ys) DSSImpl){ if (publicKey) pimpl_->SetPublic(key, sz); else pimpl_->SetPrivate(key, sz);}DSS::~DSS(){ ysDelete(pimpl_);}uint DSS::get_signatureLength() const{ return pimpl_->publicKey_.SignatureLength();}// DSS Sign message of length sz into sigvoid DSS::sign(byte* sig, const byte* sha_digest, unsigned int /* shaSz */, const RandomPool& random){ using namespace TaoCrypt; DSA_Signer signer(pimpl_->privateKey_); signer.Sign(sha_digest, sig, random.pimpl_->RNG_);}// DSS Verify message of length sz against sig, is it correct?bool DSS::verify(const byte* sha_digest, unsigned int /* shaSz */, const byte* sig, unsigned int /* sigSz */){ using namespace TaoCrypt; DSA_Verifier ver(pimpl_->publicKey_); return ver.Verify(sha_digest, sig);}// Implementation of RSA key interfacestruct RSA::RSAImpl { void SetPublic (const byte*, unsigned int); void SetPrivate(const byte*, unsigned int); TaoCrypt::RSA_PublicKey publicKey_; TaoCrypt::RSA_PrivateKey privateKey_;};// Decode and store the public keyvoid RSA::RSAImpl::SetPublic(const byte* key, unsigned int sz){ TaoCrypt::Source source(key, sz); publicKey_.Initialize(source);}// Decode and store the private keyvoid RSA::RSAImpl::SetPrivate(const byte* key, unsigned int sz){ TaoCrypt::Source source(key, sz); privateKey_.Initialize(source); publicKey_ = TaoCrypt::RSA_PublicKey(privateKey_);}// Set public or private keyRSA::RSA(const byte* key, unsigned int sz, bool publicKey) : pimpl_(new (ys) RSAImpl){ if (publicKey) pimpl_->SetPublic(key, sz); else pimpl_->SetPrivate(key, sz);}RSA::~RSA(){ ysDelete(pimpl_);}// get cipher text length, varies on key sizeunsigned int RSA::get_cipherLength() const{ return pimpl_->publicKey_.FixedCiphertextLength();}// get signautre length, varies on key sizeunsigned int RSA::get_signatureLength() const{ return get_cipherLength();}// RSA Sign message of length sz into sigvoid RSA::sign(byte* sig, const byte* message, unsigned int sz, const RandomPool& random){ TaoCrypt::RSAES_Decryptor dec(pimpl_->privateKey_); dec.SSL_Sign(message, sz, sig, random.pimpl_->RNG_);}// RSA Verify message of length sz against sigbool RSA::verify(const byte* message, unsigned int sz, const byte* sig, unsigned int){ TaoCrypt::RSAES_Encryptor enc(pimpl_->publicKey_); return enc.SSL_Verify(message, sz, sig);}// RSA public encrypt plain of length sz into ciphervoid RSA::encrypt(byte* cipher, const byte* plain, unsigned int sz, const RandomPool& random){ TaoCrypt::RSAES_Encryptor enc(pimpl_->publicKey_); enc.Encrypt(plain, sz, cipher, random.pimpl_->RNG_);}// RSA private decrypt cipher of length sz into plainvoid RSA::decrypt(byte* plain, const byte* cipher, unsigned int sz, const RandomPool& random){ TaoCrypt::RSAES_Decryptor dec(pimpl_->privateKey_); dec.Decrypt(cipher, sz, plain, random.pimpl_->RNG_);}struct Integer::IntegerImpl { TaoCrypt::Integer int_; IntegerImpl() {} explicit IntegerImpl(const TaoCrypt::Integer& i) : int_(i) {}};Integer::Integer() : pimpl_(new (ys) IntegerImpl) {}Integer::~Integer() { ysDelete(pimpl_); }Integer::Integer(const Integer& other) : pimpl_(new (ys) IntegerImpl(other.pimpl_->int_)){}Integer& Integer::operator=(const Integer& that){ pimpl_->int_ = that.pimpl_->int_; return *this;}void Integer::assign(const byte* num, unsigned int sz){ pimpl_->int_ = TaoCrypt::Integer(num, sz);}struct DiffieHellman::DHImpl { TaoCrypt::DH dh_; TaoCrypt::RandomNumberGenerator& ranPool_; byte* publicKey_; byte* privateKey_; byte* agreedKey_; DHImpl(TaoCrypt::RandomNumberGenerator& r) : ranPool_(r), publicKey_(0), privateKey_(0), agreedKey_(0) {} ~DHImpl() { ysArrayDelete(agreedKey_); ysArrayDelete(privateKey_); ysArrayDelete(publicKey_); } DHImpl(const DHImpl& that) : dh_(that.dh_), ranPool_(that.ranPool_), publicKey_(0), privateKey_(0), agreedKey_(0) { uint length = dh_.GetByteLength(); AllocKeys(length, length, length); } void AllocKeys(unsigned int pubSz, unsigned int privSz, unsigned int agrSz) { publicKey_ = new (ys) byte[pubSz]; privateKey_ = new (ys) byte[privSz]; agreedKey_ = new (ys) byte[agrSz]; }};/*// server Side DH, server's viewDiffieHellman::DiffieHellman(const char* file, const RandomPool& random) : pimpl_(new (ys) DHImpl(random.pimpl_->RNG_)){ using namespace TaoCrypt; Source source; FileSource(file, source); if (source.size() == 0) return; // TODO add error state, and force check HexDecoder hd(source); pimpl_->dh_.Initialize(source); uint length = pimpl_->dh_.GetByteLength(); pimpl_->AllocKeys(length, length, length); pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_, pimpl_->publicKey_);}*/// server Side DH, client's viewDiffieHellman::DiffieHellman(const byte* p, unsigned int pSz, const byte* g, unsigned int gSz, const byte* pub, unsigned int pubSz, const RandomPool& random) : pimpl_(new (ys) DHImpl(random.pimpl_->RNG_)){ using TaoCrypt::Integer; pimpl_->dh_.Initialize(Integer(p, pSz).Ref(), Integer(g, gSz).Ref()); pimpl_->publicKey_ = new (ys) opaque[pubSz]; memcpy(pimpl_->publicKey_, pub, pubSz);}// Server Side DH, server's viewDiffieHellman::DiffieHellman(const Integer& p, const Integer& g, const RandomPool& random): pimpl_(new (ys) DHImpl(random.pimpl_->RNG_)){ using TaoCrypt::Integer; pimpl_->dh_.Initialize(p.pimpl_->int_, g.pimpl_->int_); uint length = pimpl_->dh_.GetByteLength(); pimpl_->AllocKeys(length, length, length); pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_, pimpl_->publicKey_);}DiffieHellman::~DiffieHellman() { ysDelete(pimpl_); }// Client side and view, use server that for p and gDiffieHellman::DiffieHellman(const DiffieHellman& that) : pimpl_(new (ys) DHImpl(*that.pimpl_)){ pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_, pimpl_->publicKey_);}DiffieHellman& DiffieHellman::operator=(const DiffieHellman& that){ pimpl_->dh_ = that.pimpl_->dh_; pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_, pimpl_->publicKey_); return *this;}void DiffieHellman::makeAgreement(const byte* other){ pimpl_->dh_.Agree(pimpl_->agreedKey_, pimpl_->privateKey_, other); }uint DiffieHellman::get_agreedKeyLength() const{ return pimpl_->dh_.GetByteLength();}const byte* DiffieHellman::get_agreedKey() const{ return pimpl_->agreedKey_;}const byte* DiffieHellman::get_publicKey() const{ return pimpl_->publicKey_;}void DiffieHellman::set_sizes(int& pSz, int& gSz, int& pubSz) const{ using TaoCrypt::Integer; Integer p = pimpl_->dh_.GetP(); Integer g = pimpl_->dh_.GetG(); pSz = p.ByteCount(); gSz = g.ByteCount(); pubSz = pimpl_->dh_.GetByteLength();}void DiffieHellman::get_parms(byte* bp, byte* bg, byte* bpub) const{ using TaoCrypt::Integer; Integer p = pimpl_->dh_.GetP(); Integer g = pimpl_->dh_.GetG(); p.Encode(bp, p.ByteCount()); g.Encode(bg, g.ByteCount()); memcpy(bpub, pimpl_->publicKey_, pimpl_->dh_.GetByteLength());}// convert PEM file to DER x509 typex509* PemToDer(const char* fname, CertType type){ using namespace TaoCrypt; char header[80]; char footer[80]; if (type == Cert) { strncpy(header, "-----BEGIN CERTIFICATE-----", sizeof(header)); strncpy(footer, "-----END CERTIFICATE-----", sizeof(footer)); } else { strncpy(header, "-----BEGIN RSA PRIVATE KEY-----", sizeof(header)); strncpy(footer, "-----END RSA PRIVATE KEY-----", sizeof(header)); } FILE* file = fopen(fname, "rb"); if (!file) return 0; long begin = -1; long end = 0; bool foundEnd = false; char line[80]; while(fgets(line, sizeof(line), file)) if (strncmp(header, line, strlen(header)) == 0) { begin = ftell(file); break; } while(fgets(line, sizeof(line), file)) if (strncmp(footer, line, strlen(footer)) == 0) { foundEnd = true; break; } else end = ftell(file); if (begin == -1 || !foundEnd) { fclose(file); return 0; } input_buffer tmp(end - begin); fseek(file, begin, SEEK_SET); size_t bytes = fread(tmp.get_buffer(), end - begin, 1, file); if (bytes != 1) { fclose(file); return 0; } Source der(tmp.get_buffer(), end - begin); Base64Decoder b64Dec(der); uint sz = der.size(); mySTL::auto_ptr<x509> x(new (ys) x509(sz), ysDelete); memcpy(x->use_buffer(), der.get_buffer(), sz); fclose(file); return x.release();}} // namespace#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATIONnamespace yaSSL {template void ysDelete<DiffieHellman::DHImpl>(DiffieHellman::DHImpl*);template void ysDelete<Integer::IntegerImpl>(Integer::IntegerImpl*);template void ysDelete<RSA::RSAImpl>(RSA::RSAImpl*);template void ysDelete<DSS::DSSImpl>(DSS::DSSImpl*);template void ysDelete<RandomPool::RandomImpl>(RandomPool::RandomImpl*);template void ysDelete<AES::AESImpl>(AES::AESImpl*);template void ysDelete<RC4::RC4Impl>(RC4::RC4Impl*);template void ysDelete<DES_EDE::DES_EDEImpl>(DES_EDE::DES_EDEImpl*);template void ysDelete<DES::DESImpl>(DES::DESImpl*);template void ysDelete<HMAC_RMD::HMAC_RMDImpl>(HMAC_RMD::HMAC_RMDImpl*);template void ysDelete<HMAC_SHA::HMAC_SHAImpl>(HMAC_SHA::HMAC_SHAImpl*);template void ysDelete<HMAC_MD5::HMAC_MD5Impl>(HMAC_MD5::HMAC_MD5Impl*);template void ysDelete<RMD::RMDImpl>(RMD::RMDImpl*);template void ysDelete<SHA::SHAImpl>(SHA::SHAImpl*);template void ysDelete<MD5::MD5Impl>(MD5::MD5Impl*);}#endif // HAVE_EXPLICIT_TEMPLATE_INSTANTIATION#endif // !USE_CRYPTOPP_LIB
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -