⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crypto_wrapper.cpp

📁 一个不错的关于手机模块程序This page contains everything that has changed in the history of DC++. Read this to fin
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* crypto_wrapper.cpp  
 *
 * Copyright (C) 2003 Sawtooth Consulting Ltd.
 *
 * This file is part of yaSSL.
 *
 * yaSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * yaSSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

/*  The crypto wrapper source implements the policies for the cipher
 *  components used by SSL.
 *
 *  The implementation relies on a specfic library, taoCrypt.
 */

#if !defined(USE_CRYPTOPP_LIB)

#include "runtime.hpp"
#include "crypto_wrapper.hpp"
#include "cert_wrapper.hpp"

#include "md5.hpp"
#include "sha.hpp"
#include "ripemd.hpp"
#include "hmac.hpp"
#include "modes.hpp"
#include "des.hpp"
#include "arc4.hpp"
#include "aes.hpp"
#include "rsa.hpp"
#include "dsa.hpp"
#include "dh.hpp"
#include "random.hpp"
#include "file.hpp"
#include "coding.hpp"


namespace yaSSL {


// MD5 Implementation
struct MD5::MD5Impl {
    TaoCrypt::MD5 md5_;
    MD5Impl() {}
    explicit MD5Impl(const TaoCrypt::MD5& md5) : md5_(md5) {}
};


MD5::MD5() : pimpl_(NEW_YS MD5Impl) {}


MD5::~MD5() { ysDelete(pimpl_); }


MD5::MD5(const MD5& that) : Digest(), pimpl_(NEW_YS 
                                             MD5Impl(that.pimpl_->md5_)) {}


MD5& MD5::operator=(const MD5& that)
{
    pimpl_->md5_ = that.pimpl_->md5_;
    return *this;
}


uint MD5::get_digestSize() const
{
    return MD5_LEN;
}


uint MD5::get_padSize() const
{
    return PAD_MD5;
}


// Fill out with MD5 digest from in that is sz bytes, out must be >= digest sz
void MD5::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->md5_.Update(in, sz);
    pimpl_->md5_.Final(out);
}

// Fill out with MD5 digest from previous updates
void MD5::get_digest(byte* out)
{
    pimpl_->md5_.Final(out);
}


// Update the current digest
void MD5::update(const byte* in, unsigned int sz)
{
    pimpl_->md5_.Update(in, sz);
}


// SHA Implementation
struct SHA::SHAImpl {
    TaoCrypt::SHA sha_;
    SHAImpl() {}
    explicit SHAImpl(const TaoCrypt::SHA& sha) : sha_(sha) {}
};


SHA::SHA() : pimpl_(NEW_YS SHAImpl) {}


SHA::~SHA() { ysDelete(pimpl_); }


SHA::SHA(const SHA& that) : Digest(), pimpl_(NEW_YS SHAImpl(that.pimpl_->sha_)) {}

SHA& SHA::operator=(const SHA& that)
{
    pimpl_->sha_ = that.pimpl_->sha_;
    return *this;
}


uint SHA::get_digestSize() const
{
    return SHA_LEN;
}


uint SHA::get_padSize() const
{
    return PAD_SHA;
}


// Fill out with SHA digest from in that is sz bytes, out must be >= digest sz
void SHA::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->sha_.Update(in, sz);
    pimpl_->sha_.Final(out);
}


// Fill out with SHA digest from previous updates
void SHA::get_digest(byte* out)
{
    pimpl_->sha_.Final(out);
}


// Update the current digest
void SHA::update(const byte* in, unsigned int sz)
{
    pimpl_->sha_.Update(in, sz);
}


// RMD-160 Implementation
struct RMD::RMDImpl {
    TaoCrypt::RIPEMD160 rmd_;
    RMDImpl() {}
    explicit RMDImpl(const TaoCrypt::RIPEMD160& rmd) : rmd_(rmd) {}
};


RMD::RMD() : pimpl_(NEW_YS RMDImpl) {}


RMD::~RMD() { ysDelete(pimpl_); }


RMD::RMD(const RMD& that) : Digest(), pimpl_(NEW_YS RMDImpl(that.pimpl_->rmd_)) {}

RMD& RMD::operator=(const RMD& that)
{
    pimpl_->rmd_ = that.pimpl_->rmd_;
    return *this;
}


uint RMD::get_digestSize() const
{
    return RMD_LEN;
}


uint RMD::get_padSize() const
{
    return PAD_RMD;
}


// Fill out with RMD digest from in that is sz bytes, out must be >= digest sz
void RMD::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->rmd_.Update(in, sz);
    pimpl_->rmd_.Final(out);
}


// Fill out with RMD digest from previous updates
void RMD::get_digest(byte* out)
{
    pimpl_->rmd_.Final(out);
}


// Update the current digest
void RMD::update(const byte* in, unsigned int sz)
{
    pimpl_->rmd_.Update(in, sz);
}


// HMAC_MD5 Implementation
struct HMAC_MD5::HMAC_MD5Impl {
    TaoCrypt::HMAC<TaoCrypt::MD5> mac_;
    HMAC_MD5Impl() {}
};


HMAC_MD5::HMAC_MD5(const byte* secret, unsigned int len) 
    : pimpl_(NEW_YS HMAC_MD5Impl) 
{
    pimpl_->mac_.SetKey(secret, len);
}


HMAC_MD5::~HMAC_MD5() { ysDelete(pimpl_); }


uint HMAC_MD5::get_digestSize() const
{
    return MD5_LEN;
}


uint HMAC_MD5::get_padSize() const
{
    return PAD_MD5;
}


// Fill out with MD5 digest from in that is sz bytes, out must be >= digest sz
void HMAC_MD5::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
    pimpl_->mac_.Final(out);
}

// Fill out with MD5 digest from previous updates
void HMAC_MD5::get_digest(byte* out)
{
    pimpl_->mac_.Final(out);
}


// Update the current digest
void HMAC_MD5::update(const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
}


// HMAC_SHA Implementation
struct HMAC_SHA::HMAC_SHAImpl {
    TaoCrypt::HMAC<TaoCrypt::SHA> mac_;
    HMAC_SHAImpl() {}
};


HMAC_SHA::HMAC_SHA(const byte* secret, unsigned int len) 
    : pimpl_(NEW_YS HMAC_SHAImpl) 
{
    pimpl_->mac_.SetKey(secret, len);
}


HMAC_SHA::~HMAC_SHA() { ysDelete(pimpl_); }


uint HMAC_SHA::get_digestSize() const
{
    return SHA_LEN;
}


uint HMAC_SHA::get_padSize() const
{
    return PAD_SHA;
}


// Fill out with SHA digest from in that is sz bytes, out must be >= digest sz
void HMAC_SHA::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
    pimpl_->mac_.Final(out);
}

// Fill out with SHA digest from previous updates
void HMAC_SHA::get_digest(byte* out)
{
    pimpl_->mac_.Final(out);
}


// Update the current digest
void HMAC_SHA::update(const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
}



// HMAC_RMD Implementation
struct HMAC_RMD::HMAC_RMDImpl {
    TaoCrypt::HMAC<TaoCrypt::RIPEMD160> mac_;
    HMAC_RMDImpl() {}
};


HMAC_RMD::HMAC_RMD(const byte* secret, unsigned int len) 
    : pimpl_(NEW_YS HMAC_RMDImpl) 
{
    pimpl_->mac_.SetKey(secret, len);
}


HMAC_RMD::~HMAC_RMD() { ysDelete(pimpl_); }


uint HMAC_RMD::get_digestSize() const
{
    return RMD_LEN;
}


uint HMAC_RMD::get_padSize() const
{
    return PAD_RMD;
}


// Fill out with RMD digest from in that is sz bytes, out must be >= digest sz
void HMAC_RMD::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
    pimpl_->mac_.Final(out);
}

// Fill out with RMD digest from previous updates
void HMAC_RMD::get_digest(byte* out)
{
    pimpl_->mac_.Final(out);
}


// Update the current digest
void HMAC_RMD::update(const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
}


struct DES::DESImpl {
    TaoCrypt::DES_CBC_Encryption encryption;
    TaoCrypt::DES_CBC_Decryption decryption;
};


DES::DES() : pimpl_(NEW_YS DESImpl) {}

DES::~DES() { ysDelete(pimpl_); }


void DES::set_encryptKey(const byte* k, const byte* iv)
{
    pimpl_->encryption.SetKey(k, DES_KEY_SZ, iv);
}


void DES::set_decryptKey(const byte* k, const byte* iv)
{
    pimpl_->decryption.SetKey(k, DES_KEY_SZ, iv);
}

// DES encrypt plain of length sz into cipher
void DES::encrypt(byte* cipher, const byte* plain, unsigned int sz)
{
    pimpl_->encryption.Process(cipher, plain, sz);
}


// DES decrypt cipher of length sz into plain
void DES::decrypt(byte* plain, const byte* cipher, unsigned int sz)
{
    pimpl_->decryption.Process(plain, cipher, sz);
}


struct DES_EDE::DES_EDEImpl {
    TaoCrypt::DES_EDE3_CBC_Encryption encryption;
    TaoCrypt::DES_EDE3_CBC_Decryption decryption;
};


DES_EDE::DES_EDE() : pimpl_(NEW_YS DES_EDEImpl) {}

DES_EDE::~DES_EDE() { ysDelete(pimpl_); }


void DES_EDE::set_encryptKey(const byte* k, const byte* iv)
{
    pimpl_->encryption.SetKey(k, DES_EDE_KEY_SZ, iv);
}


void DES_EDE::set_decryptKey(const byte* k, const byte* iv)
{
    pimpl_->decryption.SetKey(k, DES_EDE_KEY_SZ, iv);
}


// 3DES encrypt plain of length sz into cipher
void DES_EDE::encrypt(byte* cipher, const byte* plain, unsigned int sz)
{
    pimpl_->encryption.Process(cipher, plain, sz);
}


// 3DES decrypt cipher of length sz into plain
void DES_EDE::decrypt(byte* plain, const byte* cipher, unsigned int sz)
{
    pimpl_->decryption.Process(plain, cipher, sz);
}


// Implementation of alledged RC4
struct RC4::RC4Impl {
    TaoCrypt::ARC4::Encryption encryption;
    TaoCrypt::ARC4::Decryption decryption;
};


RC4::RC4() : pimpl_(NEW_YS RC4Impl) {}

RC4::~RC4() { ysDelete(pimpl_); }


void RC4::set_encryptKey(const byte* k, const byte*)
{
    pimpl_->encryption.SetKey(k, RC4_KEY_SZ);
}


void RC4::set_decryptKey(const byte* k, const byte*)
{
    pimpl_->decryption.SetKey(k, RC4_KEY_SZ);
}


// RC4 encrypt plain of length sz into cipher
void RC4::encrypt(byte* cipher, const byte* plain, unsigned int sz)
{
    pimpl_->encryption.Process(cipher, plain, sz);
}


// RC4 decrypt cipher of length sz into plain
void RC4::decrypt(byte* plain, const byte* cipher, unsigned int sz)
{
    pimpl_->decryption.Process(plain, cipher, sz);
}



// Implementation of AES
struct AES::AESImpl {
    TaoCrypt::AES_CBC_Encryption encryption;
    TaoCrypt::AES_CBC_Decryption decryption;
    unsigned int keySz_;

    AESImpl(unsigned int ks) : keySz_(ks) {}
};


AES::AES(unsigned int ks) : pimpl_(NEW_YS AESImpl(ks)) {}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -