aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2009-10-09 16:47:26 +0000
committerlloyd <[email protected]>2009-10-09 16:47:26 +0000
commit2ce232944ab89b021d59d8c793815e54dd61c322 (patch)
treeee98cdf7ded1b3cff3ed08f81949e6ee566ed716
parentb37dbf1f1c80168f79bcb59809be5002dc7515b4 (diff)
Simplify Boost.Python wrapper code. Now mostly functional! Yay
-rw-r--r--wrappers/boost-python/src/block.cpp152
-rw-r--r--wrappers/boost-python/src/core.cpp178
-rw-r--r--wrappers/boost-python/src/hash.cpp108
-rw-r--r--wrappers/boost-python/src/macs.cpp59
-rw-r--r--wrappers/boost-python/src/python_botan.h9
-rw-r--r--wrappers/boost-python/src/stream.cpp54
6 files changed, 157 insertions, 403 deletions
diff --git a/wrappers/boost-python/src/block.cpp b/wrappers/boost-python/src/block.cpp
deleted file mode 100644
index 185b88811..000000000
--- a/wrappers/boost-python/src/block.cpp
+++ /dev/null
@@ -1,152 +0,0 @@
-/*************************************************
-* Boost.Python module definition *
-* (C) 1999-2007 Jack Lloyd *
-*************************************************/
-
-#include <botan/botan.h>
-using namespace Botan;
-
-#include "python_botan.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 clear() throw() {}
-
- 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 clear() throw() { cipher->clear(); }
-
- 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);
- }
-
- void clear() throw()
- {
- this->get_override("clear")();
- }
-
- 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));
- }
diff --git a/wrappers/boost-python/src/core.cpp b/wrappers/boost-python/src/core.cpp
index 7ef2a9970..1992e3d61 100644
--- a/wrappers/boost-python/src/core.cpp
+++ b/wrappers/boost-python/src/core.cpp
@@ -1,14 +1,13 @@
-/*************************************************
-* Boost.Python module definition *
-* (C) 1999-2007 Jack Lloyd *
-*************************************************/
-
-#include <botan/botan.h>
+/*
+* Boost.Python module definition
+* (C) 1999-2007 Jack Lloyd
+*/
+
+#include <botan/init.h>
+#include <botan/pipe.h>
+#include <botan/lookup.h>
using namespace Botan;
-#include <boost/python.hpp>
-namespace python = boost::python;
-
#include "python_botan.h"
class Python_RandomNumberGenerator
@@ -20,7 +19,7 @@ class Python_RandomNumberGenerator
std::string name() const { return rng->name(); }
- void reseed() { rng->reseed(); }
+ void reseed() { rng->reseed(192); }
int gen_random_byte() { return rng->next_byte(); }
@@ -38,6 +37,132 @@ class Python_RandomNumberGenerator
RandomNumberGenerator* rng;
};
+class Py_Cipher
+ {
+ public:
+ Py_Cipher(std::string algo_name, std::string direction,
+ std::string key);
+
+ std::string cipher_noiv(const std::string& text);
+
+ std::string cipher(const std::string& text,
+ const std::string& iv);
+
+ std::string name() const { return algo_name; }
+ private:
+ std::string algo_name;
+ Keyed_Filter* filter;
+ Pipe pipe;
+ };
+
+std::string Py_Cipher::cipher(const std::string& input,
+ const std::string& iv_str)
+ {
+ if(iv_str.size())
+ {
+ const byte* iv_bytes = reinterpret_cast<const byte*>(iv_str.data());
+ u32bit iv_len = iv_str.size();
+ filter->set_iv(InitializationVector(iv_bytes, iv_len));
+ }
+
+ pipe.process_msg(input);
+ return pipe.read_all_as_string(Pipe::LAST_MESSAGE);
+ }
+
+// For IV-less algorithms
+std::string Py_Cipher::cipher_noiv(const std::string& input)
+ {
+ pipe.process_msg(input);
+ return pipe.read_all_as_string(Pipe::LAST_MESSAGE);
+ }
+
+Py_Cipher::Py_Cipher(std::string algo_name,
+ std::string direction,
+ std::string key_str)
+ {
+ const byte* key_bytes = reinterpret_cast<const byte*>(key_str.data());
+ u32bit key_len = key_str.size();
+
+ Cipher_Dir dir;
+
+ if(direction == "encrypt")
+ dir = ENCRYPTION;
+ else if(direction == "decrypt")
+ dir = DECRYPTION;
+ else
+ throw std::invalid_argument("Bad cipher direction " + direction);
+
+ filter = get_cipher(algo_name, dir);
+ filter->set_key(SymmetricKey(key_bytes, key_len));
+ pipe.append(filter);
+ }
+
+class Py_HashFunction
+ {
+ public:
+ Py_HashFunction(const std::string& algo_name)
+ {
+ hash = get_hash(algo_name);
+ }
+
+ ~Py_HashFunction() { delete hash; }
+
+ void update(const std::string& input)
+ {
+ hash->update(input);
+ }
+
+ std::string final()
+ {
+ std::string out(output_length(), 0);
+ hash->final(reinterpret_cast<byte*>(&out[0]));
+ return out;
+ }
+
+ std::string name() const
+ {
+ return hash->name();
+ }
+
+ u32bit output_length() const
+ {
+ return hash->OUTPUT_LENGTH;
+ }
+
+ private:
+ HashFunction* hash;
+ };
+
+class Py_MAC
+ {
+ public:
+
+ Py_MAC(const std::string& name, const std::string& key_str)
+ {
+ mac = get_mac(name);
+
+ mac->set_key(reinterpret_cast<const byte*>(key_str.data()),
+ key_str.size());
+ }
+
+ ~Py_MAC() { delete mac; }
+
+ u32bit output_length() const { return mac->OUTPUT_LENGTH; }
+
+ std::string name() const { return mac->name(); }
+
+ void update(const std::string& in) { mac->update(in); }
+
+ std::string final()
+ {
+ std::string out(output_length(), 0);
+ mac->final(reinterpret_cast<byte*>(&out[0]));
+ return out;
+ }
+ private:
+ MessageAuthenticationCode* mac;
+ };
+
BOOST_PYTHON_MODULE(_botan)
{
python::class_<LibraryInitializer>("LibraryInitializer")
@@ -52,20 +177,25 @@ BOOST_PYTHON_MODULE(_botan)
.def("gen_random_byte", &Python_RandomNumberGenerator::gen_random_byte)
.def("gen_random", &Python_RandomNumberGenerator::gen_random);
- python::class_<OctetString>("OctetString")
- .def(python::init< python::optional<std::string> >())
- //.def(python::init< u32bit >())
- .def("__str__", &OctetString::as_string)
- .def("__len__", &OctetString::length);
-
- python::enum_<Cipher_Dir>("cipher_dir")
- .value("encryption", ENCRYPTION)
- .value("decryption", DECRYPTION);
-
- export_block_ciphers();
- export_stream_ciphers();
- export_hash_functions();
- export_macs();
+ python::class_<Py_Cipher, boost::noncopyable>
+ ("Cipher", python::init<std::string, std::string, std::string>())
+ .def("name", &Py_Cipher::name)
+ .def("cipher", &Py_Cipher::cipher)
+ .def("cipher", &Py_Cipher::cipher_noiv);
+
+ python::class_<Py_HashFunction, boost::noncopyable>
+ ("HashFunction", python::init<std::string>())
+ .def("update", &Py_HashFunction::update)
+ .def("final", &Py_HashFunction::final)
+ .def("name", &Py_HashFunction::name)
+ .def("output_length", &Py_HashFunction::output_length);
+
+ python::class_<Py_MAC, boost::noncopyable>
+ ("MAC", python::init<std::string, std::string>())
+ .def("update", &Py_MAC::update)
+ .def("final", &Py_MAC::final)
+ .def("name", &Py_MAC::name)
+ .def("output_length", &Py_MAC::output_length);
export_filters();
export_pk();
diff --git a/wrappers/boost-python/src/hash.cpp b/wrappers/boost-python/src/hash.cpp
deleted file mode 100644
index 6ef198087..000000000
--- a/wrappers/boost-python/src/hash.cpp
+++ /dev/null
@@ -1,108 +0,0 @@
-/*************************************************
-* Boost.Python module definition *
-* (C) 1999-2007 Jack Lloyd *
-*************************************************/
-
-#include <botan/botan.h>
-using namespace Botan;
-
-#include "python_botan.h"
-
-class Py_HashFunction : public HashFunction
- {
- public:
- virtual void hash_str(const std::string&) = 0;
- virtual std::string final_str() = 0;
-
- void clear() throw() {}
-
- void add_data(const byte input[], u32bit length)
- {
- hash_str(make_string(input, length));
- }
-
- void final_result(byte output[])
- {
- string2binary(final_str(), output, OUTPUT_LENGTH);
- }
-
- Py_HashFunction(u32bit digest_size, u32bit block_size) :
- HashFunction(digest_size, block_size) {}
- };
-
-class Wrapped_HashFunction : public HashFunction
- {
- public:
- void add_data(const byte in[], u32bit len) { hash->update(in, len); }
- void final_result(byte out[]) { hash->final(out); }
-
- void clear() throw() {}
-
- std::string name() const { return hash->name(); }
- HashFunction* clone() const { return hash->clone(); }
-
- Wrapped_HashFunction(python::object py_obj, HashFunction* h) :
- HashFunction(h->OUTPUT_LENGTH, h->HASH_BLOCK_SIZE),
- obj(py_obj), hash(h) {}
- private:
- python::object obj;
- HashFunction* hash;
- };
-
-class Py_HashFunction_Wrapper : public Py_HashFunction,
- public python::wrapper<Py_HashFunction>
- {
- public:
- HashFunction* clone() const
- {
- python::object self = get_owner(this);
- python::object py_clone = self.attr("__class__")();
- HashFunction* hf = python::extract<HashFunction*>(py_clone);
- return new Wrapped_HashFunction(py_clone, hf);
- }
-
- std::string name() const
- {
- return this->get_override("name")();
- }
-
- void hash_str(const std::string& in)
- {
- this->get_override("update")(in);
- }
-
- std::string final_str()
- {
- return this->get_override("final")();
- }
-
- Py_HashFunction_Wrapper(u32bit digest_size, u32bit block_size) :
- Py_HashFunction(digest_size, block_size) {}
- };
-
-std::string final_str(HashFunction* hash)
- {
- SecureVector<byte> digest = hash->final();
- return std::string((const char*)digest.begin(), digest.size());
- }
-
-void export_hash_functions()
- {
- void (HashFunction::*update_str)(const std::string&) =
- &HashFunction::update;
-
- python::class_<HashFunction, boost::noncopyable>
- ("HashFunction", python::no_init)
- .def("__init__", python::make_constructor(get_hash))
- .def_readonly("digest_size", &HashFunction::OUTPUT_LENGTH)
- .def("name", &HashFunction::name)
- .def("update", update_str)
- .def("final", final_str);
-
- python::class_<Py_HashFunction_Wrapper, python::bases<HashFunction>,
- boost::noncopyable>
- ("HashFunctionImpl", python::init<u32bit, u32bit>())
- .def("name", python::pure_virtual(&Py_HashFunction::name))
- .def("update", python::pure_virtual(&Py_HashFunction::hash_str))
- .def("final", python::pure_virtual(&Py_HashFunction::final_str));
- }
diff --git a/wrappers/boost-python/src/macs.cpp b/wrappers/boost-python/src/macs.cpp
deleted file mode 100644
index 54a55afe2..000000000
--- a/wrappers/boost-python/src/macs.cpp
+++ /dev/null
@@ -1,59 +0,0 @@
-/*************************************************
-* Boost.Python module definition *
-* (C) 1999-2007 Jack Lloyd *
-*************************************************/
-
-#include <botan/botan.h>
-using namespace Botan;
-
-#include <boost/python.hpp>
-namespace python = boost::python;
-
-class Py_MAC
- {
- public:
- u32bit output_length() const { return mac->OUTPUT_LENGTH; }
- u32bit keylength_min() const { return mac->MINIMUM_KEYLENGTH; }
- u32bit keylength_max() const { return mac->MAXIMUM_KEYLENGTH; }
- u32bit keylength_mod() const { return mac->KEYLENGTH_MULTIPLE; }
- std::string name() const { return mac->name(); }
- void clear() throw() { mac->clear(); }
-
- void set_key(const OctetString& key) { mac->set_key(key); }
-
- bool valid_keylength(u32bit kl) const
- {
- return mac->valid_keylength(kl);
- }
-
- void update(const std::string& in) { mac->update(in); }
-
- std::string final()
- {
- SecureVector<byte> result = mac->final();
- return std::string((const char*)result.begin(), result.size());
- }
-
- Py_MAC(const std::string& name)
- {
- mac = get_mac(name);
- }
- ~Py_MAC() { delete mac; }
- private:
- MessageAuthenticationCode* mac;
- };
-
-void export_macs()
- {
- python::class_<Py_MAC>("MAC", python::init<std::string>())
- .add_property("output_length", &Py_MAC::output_length)
- .add_property("keylength_min", &Py_MAC::keylength_min)
- .add_property("keylength_max", &Py_MAC::keylength_max)
- .add_property("keylength_mod", &Py_MAC::keylength_mod)
- .add_property("name", &Py_MAC::name)
- .def("valid_keylength", &Py_MAC::valid_keylength)
- .def("set_key", &Py_MAC::set_key)
- .def("clear", &Py_MAC::clear)
- .def("update", &Py_MAC::update)
- .def("final", &Py_MAC::final);
- }
diff --git a/wrappers/boost-python/src/python_botan.h b/wrappers/boost-python/src/python_botan.h
index d0fd10207..18c51dbb1 100644
--- a/wrappers/boost-python/src/python_botan.h
+++ b/wrappers/boost-python/src/python_botan.h
@@ -2,17 +2,14 @@
#ifndef BOTAN_BOOST_PYTHON_COMMON_H__
#define BOTAN_BOOST_PYTHON_COMMON_H__
-#include <botan/base.h>
+#include <botan/exceptn.h>
+#include <botan/parsing.h>
+#include <botan/secmem.h>
using namespace Botan;
#include <boost/python.hpp>
namespace python = boost::python;
-
-extern void export_block_ciphers();
-extern void export_stream_ciphers();
-extern void export_hash_functions();
-extern void export_macs();
extern void export_filters();
extern void export_pk();
extern void export_x509();
diff --git a/wrappers/boost-python/src/stream.cpp b/wrappers/boost-python/src/stream.cpp
deleted file mode 100644
index f94bd7fd5..000000000
--- a/wrappers/boost-python/src/stream.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
-/*************************************************
-* Boost.Python module definition *
-* (C) 1999-2007 Jack Lloyd *
-*************************************************/
-
-#include <botan/botan.h>
-using namespace Botan;
-
-#include "python_botan.h"
-
-class Py_StreamCipher
- {
- public:
- u32bit keylength_min() const { return cipher->MINIMUM_KEYLENGTH; }
- u32bit keylength_max() const { return cipher->MAXIMUM_KEYLENGTH; }
- u32bit keylength_mod() const { return cipher->KEYLENGTH_MULTIPLE; }
-
- void set_key(const OctetString& key) { cipher->set_key(key); }
- bool valid_keylength(u32bit kl) const
- {
- return cipher->valid_keylength(kl);
- }
-
- std::string name() const { return cipher->name(); }
- void clear() throw() { cipher->clear(); }
-
- std::string crypt(const std::string& in) const
- {
- SecureVector<byte> out(in.size());
- cipher->encrypt((const byte*)in.data(), out.begin(), in.size());
- return std::string((const char*)out.begin(), out.size());
- }
-
- Py_StreamCipher(const std::string& name)
- {
- cipher = get_stream_cipher(name);
- }
- ~Py_StreamCipher() { delete cipher; }
- private:
- StreamCipher* cipher;
- };
-
-void export_stream_ciphers()
- {
- python::class_<Py_StreamCipher>("StreamCipher", python::init<std::string>())
- .add_property("keylength_min", &Py_StreamCipher::keylength_min)
- .add_property("keylength_max", &Py_StreamCipher::keylength_max)
- .add_property("keylength_mod", &Py_StreamCipher::keylength_mod)
- .add_property("name", &Py_StreamCipher::name)
- .def("clear", &Py_StreamCipher::clear)
- .def("valid_keylength", &Py_StreamCipher::valid_keylength)
- .def("set_key", &Py_StreamCipher::set_key)
- .def("crypt", &Py_StreamCipher::crypt);
- }