-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptopp_rng.cpp
91 lines (73 loc) · 2.72 KB
/
cryptopp_rng.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// Created by marandil on 15.03.17.
//
#include "cryptopp_rng.hpp"
#include <mdlutils/exceptions.hpp>
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <arc4.h>
#include <salsa.h>
#include <chacha.h>
#include <sosemanuk.h>
#include <aes.h>
#include <modes.h>
#include "cipher_common.hpp"
#include <iostream>
template<typename Enc, size_t KeyLength, size_t BlockSize, bool use_iv = true, typename... Args>
struct cryptopp_rng_impl : public blocked_rng
{
Enc instance;
cryptopp_rng_impl(Args... args) : blocked_rng(BlockSize), instance()
{
}
virtual ~cryptopp_rng_impl()
{
}
template<class V = void>
typename std::enable_if<use_iv, V>::type
_set_seed_internal(integer seed)
{
auto sb = decode(seed, KeyLength);
instance.SetKeyWithIV(sb.second, sb.first, zero_buffer);
}
template<class V = void>
typename std::enable_if<!use_iv, V>::type
_set_seed_internal(integer seed)
{
auto sb = decode(seed, KeyLength);
instance.SetKey(sb.second, sb.first);
}
virtual void set_seed(integer seed)
{
this->_set_seed_internal(seed);
}
virtual void next()
{
instance.ProcessData(this->block, zero_buffer, this->block_size);
}
};
bit_function_p cryptopp_rng::from_string(std::string name)
{
if (name == "RC4")
return std::make_shared<cryptopp_rng_impl<CryptoPP::Weak::ARC4::Encryption, 32, 32, false>>();
if (name == "CHACHA-20")
return std::make_shared<cryptopp_rng_impl<CryptoPP::ChaCha20::Encryption, 32, 32>>();
if (name == "SALSA-20")
return std::make_shared<cryptopp_rng_impl<CryptoPP::Salsa20::Encryption, 32, 32>>();
if (name == "Sosemanuk")
return std::make_shared<cryptopp_rng_impl<CryptoPP::Sosemanuk::Encryption, 32, 32>>();
// block ciphers
if (name == "AES128CTR")
return std::make_shared<cryptopp_rng_impl<CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption, 16, 16>>();
if (name == "AES192CTR")
return std::make_shared<cryptopp_rng_impl<CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption, 24, 16>>();
if (name == "AES256CTR")
return std::make_shared<cryptopp_rng_impl<CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption, 32, 16>>();
if (name == "AES128CBC")
return std::make_shared<cryptopp_rng_impl<CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption, 16, 16>>();
if (name == "AES192CBC")
return std::make_shared<cryptopp_rng_impl<CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption, 24, 16>>();
if (name == "AES256CBC")
return std::make_shared<cryptopp_rng_impl<CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption, 32, 16>>();
throw std::exception();
//mdl_throw(mdl::not_implemented_exception, "");
}