merged removal of AES code from RC_0_16

This commit is contained in:
Arvid Norberg 2013-08-31 20:23:58 +00:00
parent b6083fbc28
commit 850b81e625
2 changed files with 0 additions and 111 deletions

View File

@ -41,8 +41,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <gcrypt.h>
#elif defined TORRENT_USE_OPENSSL
#include <openssl/rc4.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#else
// RC4 state from libtomcrypt
struct rc4 {
@ -200,104 +198,6 @@ namespace libtorrent
bool m_decrypt;
};
#ifdef TORRENT_USE_OPENSSL
struct aes256_handler : encryption_handler
{
aes256_handler() : m_enc_pos(0), m_dec_pos(0)
{
EVP_CIPHER_CTX_init(&m_enc);
EVP_CIPHER_CTX_init(&m_dec);
}
~aes256_handler()
{
EVP_CIPHER_CTX_cleanup(&m_enc);
EVP_CIPHER_CTX_cleanup(&m_dec);
}
void set_incoming_key(unsigned char const* in_key, int len)
{
const int nrounds = 5;
boost::uint8_t salt[8] = { 0xf1, 0x03, 0x46, 0xe2, 0xb1, 0xa8, 0x29, 0x63 };
boost::uint8_t key[32];
boost::uint8_t iv[32];
EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt, in_key, len, nrounds, key, iv);
TORRENT_ASSERT(len == 32);
EVP_EncryptInit_ex(&m_enc, EVP_aes_256_cbc(), NULL, key, iv);
// since we're using the AES as a stream cipher, both the encrypt and
// decrypt context will in fact only _encrypt_ stuff, so initializing
// this as encrypt is not a typo
EVP_EncryptInit_ex(&m_dec, EVP_aes_256_cbc(), NULL, key, iv);
m_enc_pos = 0;
m_dec_pos = 0;
std::memcpy(m_enc_state, iv, sizeof(m_enc_state));
std::memcpy(m_dec_state, iv, sizeof(m_enc_state));
}
void set_outgoing_key(unsigned char const* key, int len) { /* no-op */ }
void encrypt(char* pos, int len)
{
while (len > 0)
{
while (m_enc_pos < AES_BLOCK_SIZE && len > 0)
{
*pos ^= m_enc_state[m_enc_pos];
++m_enc_pos;
++pos;
--len;
}
if (m_enc_pos == AES_BLOCK_SIZE)
{
next_block(&m_enc, m_enc_state);
m_enc_pos = 0;
}
}
}
void decrypt(char* pos, int len)
{
while (len > 0)
{
while (m_dec_pos < AES_BLOCK_SIZE && len > 0)
{
*pos ^= m_dec_state[m_dec_pos];
++m_dec_pos;
++pos;
--len;
}
if (m_dec_pos == AES_BLOCK_SIZE)
{
next_block(&m_dec, m_dec_state);
m_dec_pos = 0;
}
}
}
private:
// we're turning the AES block-cipher into a stream cipher. This
// function will produce the next block in the sequence of
// block-sized buffers. We're using "Output feedback" (OFB) mode.
void next_block(EVP_CIPHER_CTX* ctx, boost::uint8_t* pad)
{
int outlen = AES_BLOCK_SIZE;
EVP_EncryptUpdate(ctx, pad, &outlen, pad, AES_BLOCK_SIZE);
TORRENT_ASSERT(outlen == AES_BLOCK_SIZE);
}
EVP_CIPHER_CTX m_enc;
EVP_CIPHER_CTX m_dec;
boost::uint8_t m_enc_state[AES_BLOCK_SIZE];
boost::uint8_t m_dec_state[AES_BLOCK_SIZE];
int m_enc_pos;
int m_dec_pos;
};
#endif // TORRENT_USE_OPENSSL
} // namespace libtorrent
#endif // TORRENT_PE_CRYPTO_HPP_INCLUDED

View File

@ -195,17 +195,6 @@ int test_main()
rc42.set_outgoing_key(&test2_key[0], 20);
test_enc_handler(&rc41, &rc42);
#ifdef TORRENT_USE_OPENSSL
fprintf(stderr, "testing AES-256 handler\n");
char key1[32];
std::generate(key1, key1 + 32, &std::rand);
aes256_handler aes1;
aes1.set_incoming_key((const unsigned char*)key1, 32);
aes256_handler aes2;
aes2.set_incoming_key((const unsigned char*)key1, 32);
test_enc_handler(&aes1, &aes2);
#endif
test_transfer(pe_settings::disabled);
test_transfer(pe_settings::forced, pe_settings::plaintext);