/* */ #include "LibsslARC4Context.h" #include #include "DlAbortEx.h" #include "StringFormat.h" namespace aria2 { namespace { void handleError() { throw DL_ABORT_EX (StringFormat("Exception in libssl routine(ARC4Context class): %s", ERR_error_string(ERR_get_error(), 0)).str()); } } // namespace LibsslARC4Context::LibsslARC4Context():cipherCtx_(0) {} LibsslARC4Context::~LibsslARC4Context() { if(cipherCtx_) { EVP_CIPHER_CTX_cleanup(cipherCtx_); } delete cipherCtx_; } EVP_CIPHER_CTX* LibsslARC4Context::getCipherContext() const { return cipherCtx_; } // enc == 1: encryption // enc == 0: decryption void LibsslARC4Context::init (const unsigned char* key, size_t keyLength, int enc) { if(cipherCtx_) { EVP_CIPHER_CTX_cleanup(cipherCtx_); } delete cipherCtx_; cipherCtx_ = new EVP_CIPHER_CTX; EVP_CIPHER_CTX_init(cipherCtx_); if(!EVP_CipherInit_ex(cipherCtx_, EVP_rc4(), 0, 0, 0, enc)) { handleError(); } if(!EVP_CIPHER_CTX_set_key_length(cipherCtx_, keyLength)) { handleError(); } if(!EVP_CipherInit_ex(cipherCtx_, 0, 0, key, 0, -1)) { handleError(); } } } // namespace aria2