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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/*************************************************
* Boost.Python module definition *
* (C) 1999-2006 The Botan Project *
*************************************************/
#include <botan/botan.h>
using namespace Botan;
#include "common.h"
class Py_BlockCipher : public BlockCipher
{
public:
virtual std::string enc_str(const std::string&) const = 0;
virtual std::string dec_str(const std::string&) const = 0;
virtual void set_key_obj(const OctetString&) = 0;
void enc(const byte in[], byte out[]) const
{
string2binary(
enc_str(make_string(in, BLOCK_SIZE)),
out, BLOCK_SIZE);
}
void dec(const byte in[], byte out[]) const
{
string2binary(
dec_str(make_string(in, BLOCK_SIZE)),
out, BLOCK_SIZE);
}
void key(const byte key[], u32bit len)
{
set_key_obj(OctetString(key, len));
}
Py_BlockCipher(u32bit bs, u32bit kmin, u32bit kmax, u32bit kmod) :
BlockCipher(bs, kmin, kmax, kmod)
{
}
};
std::string encrypt_string(BlockCipher* cipher, const std::string& in)
{
if(in.size() != cipher->BLOCK_SIZE)
throw Bad_Size(in.size(), cipher->BLOCK_SIZE);
SecureVector<byte> out(cipher->BLOCK_SIZE);
cipher->encrypt((const byte*)in.data(), out);
return make_string(out);
}
std::string decrypt_string(BlockCipher* cipher, const std::string& in)
{
if(in.size() != cipher->BLOCK_SIZE)
throw Bad_Size(in.size(), cipher->BLOCK_SIZE);
SecureVector<byte> out(cipher->BLOCK_SIZE);
cipher->decrypt((const byte*)in.data(), out);
return make_string(out);
}
class Wrapped_Block_Cipher : public BlockCipher
{
public:
void enc(const byte in[], byte out[]) const { cipher->encrypt(in, out); }
void dec(const byte in[], byte out[]) const { cipher->decrypt(in, out); }
void key(const byte key[], u32bit len) { cipher->set_key(key, len); }
std::string name() const { return cipher->name(); }
BlockCipher* clone() const { return cipher->clone(); }
Wrapped_Block_Cipher(python::object py_obj, BlockCipher* c) :
BlockCipher(c->BLOCK_SIZE, c->MINIMUM_KEYLENGTH,
c->MAXIMUM_KEYLENGTH, c->KEYLENGTH_MULTIPLE),
obj(py_obj), cipher(c) {}
private:
python::object obj;
BlockCipher* cipher;
};
class Py_BlockCipher_Wrapper : public Py_BlockCipher,
public python::wrapper<Py_BlockCipher>
{
public:
BlockCipher* clone() const
{
python::object self = get_owner(this);
python::object py_clone = self.attr("__class__")();
BlockCipher* bc = python::extract<BlockCipher*>(py_clone);
return new Wrapped_Block_Cipher(py_clone, bc);
}
std::string name() const
{
return this->get_override("name")();
}
std::string enc_str(const std::string& in) const
{
return this->get_override("encrypt")(in);
}
std::string dec_str(const std::string& in) const
{
return this->get_override("decrypt")(in);
}
void set_key_obj(const OctetString& key)
{
this->get_override("set_key")(key);
}
Py_BlockCipher_Wrapper(u32bit bs, u32bit kmin,
u32bit kmax, u32bit kmod) :
Py_BlockCipher(bs, kmin, kmax, kmod) {}
};
void export_block_ciphers()
{
void (BlockCipher::*set_key_ptr)(const OctetString&) =
&BlockCipher::set_key;
python::class_<BlockCipher, boost::noncopyable>
("BlockCipher", python::no_init)
.def("__init__", python::make_constructor(get_block_cipher))
.def_readonly("block_size", &BlockCipher::BLOCK_SIZE)
.def_readonly("keylength_min", &BlockCipher::MINIMUM_KEYLENGTH)
.def_readonly("keylength_max", &BlockCipher::MAXIMUM_KEYLENGTH)
.def_readonly("keylength_mod", &BlockCipher::KEYLENGTH_MULTIPLE)
.def("valid_keylength", &BlockCipher::valid_keylength)
.def("name", &BlockCipher::name)
.def("set_key", set_key_ptr)
.def("encrypt", encrypt_string)
.def("decrypt", decrypt_string);
python::class_<Py_BlockCipher_Wrapper, python::bases<BlockCipher>,
boost::noncopyable>
("BlockCipherImpl", python::init<u32bit, u32bit, u32bit, u32bit>())
.def("name", python::pure_virtual(&Py_BlockCipher::name))
.def("encrypt", python::pure_virtual(&Py_BlockCipher::enc_str))
.def("decrypt", python::pure_virtual(&Py_BlockCipher::dec_str))
.def("set_key", python::pure_virtual(&Py_BlockCipher::set_key_obj));
}
|