aboutsummaryrefslogtreecommitdiffstats
path: root/misc/python/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2006-08-31 18:39:13 +0000
committerlloyd <[email protected]>2006-08-31 18:39:13 +0000
commitc69e0026cfc84427514827313113cfa7c32cb69f (patch)
tree9ecf81a134f40b0ab137b23199a124fda0e95610 /misc/python/src
parent928d989fd9823ccfac6ef83dc1abe668f38fa42d (diff)
Merge the block cipher, stream cipher, hash, and MAC exports into algos.cpp
Add various useful conversion functions
Diffstat (limited to 'misc/python/src')
-rw-r--r--misc/python/src/algos.cpp205
-rw-r--r--misc/python/src/block.cpp78
-rw-r--r--misc/python/src/core.cpp10
-rw-r--r--misc/python/src/hash.cpp44
-rw-r--r--misc/python/src/mac.cpp59
-rw-r--r--misc/python/src/stream.cpp55
6 files changed, 207 insertions, 244 deletions
diff --git a/misc/python/src/algos.cpp b/misc/python/src/algos.cpp
new file mode 100644
index 000000000..09acf1d74
--- /dev/null
+++ b/misc/python/src/algos.cpp
@@ -0,0 +1,205 @@
+/*************************************************
+* Boost.Python module definition *
+* (C) 1999-2006 The Botan Project *
+*************************************************/
+
+#include <botan/botan.h>
+using namespace Botan;
+
+#include <boost/python.hpp>
+namespace python = boost::python;
+
+/* Encrypt or decrypt */
+std::string process(const std::string& in, BlockCipher* cipher,
+ void (BlockCipher::* proc)(const byte[], byte[]) const)
+ {
+ if(in.size() != cipher->BLOCK_SIZE)
+ throw Invalid_Argument("Input for cipher " + cipher->name() +
+ " must be " + to_string(cipher->BLOCK_SIZE) +
+ " bytes");
+
+ const byte* in_data = (const byte*)in.data();
+
+ SecureVector<byte> out(cipher->BLOCK_SIZE);
+ (cipher->*proc)(in_data, out);
+
+ return std::string((const char*)out.begin(), out.size());
+ }
+
+class Py_BlockCipher
+ {
+ public:
+ BlockCipher* object() { return cipher; }
+
+ u32bit block_size() const { return cipher->BLOCK_SIZE; }
+ u32bit keylength_min() const { return cipher->MINIMUM_KEYLENGTH; }
+ u32bit keylength_max() const { return cipher->MAXIMUM_KEYLENGTH; }
+ u32bit keylength_mod() const { return cipher->KEYLENGTH_MULTIPLE; }
+
+ 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 encrypt(const std::string& in) const
+ {
+ return process(in, cipher, &BlockCipher::encrypt);
+ }
+ std::string decrypt(const std::string& in) const
+ {
+ return process(in, cipher, &BlockCipher::decrypt);
+ }
+
+ void set_key(const OctetString& key) { cipher->set_key(key); }
+
+ Py_BlockCipher(const std::string& name)
+ {
+ cipher = get_block_cipher(name);
+ }
+ ~Py_BlockCipher() { delete cipher; }
+ private:
+ BlockCipher* cipher;
+ };
+
+class Py_StreamCipher
+ {
+ public:
+ StreamCipher* object() { return cipher; }
+
+ 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;
+ };
+
+class Py_HashFunction
+ {
+ public:
+ HashFunction* object() { return hash; }
+
+ u32bit output_length() const { return hash->OUTPUT_LENGTH; }
+ std::string name() const { return hash->name(); }
+ void clear() throw() { hash->clear(); }
+
+ void update(const std::string& in) { hash->update(in); }
+
+ std::string final()
+ {
+ SecureVector<byte> result = hash->final();
+ return std::string((const char*)result.begin(), result.size());
+ }
+
+ Py_HashFunction(const std::string& name)
+ {
+ hash = get_hash(name);
+ }
+ ~Py_HashFunction() { delete hash; }
+ private:
+ HashFunction* hash;
+ };
+
+class Py_MAC
+ {
+ public:
+ MessageAuthenticationCode* object() { return mac; }
+
+ 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_basic_algos()
+ {
+ python::class_<Py_BlockCipher>("BlockCipher", python::init<std::string>())
+ .add_property("block_size", &Py_BlockCipher::block_size)
+ .add_property("keylength_min", &Py_BlockCipher::keylength_min)
+ .add_property("keylength_max", &Py_BlockCipher::keylength_max)
+ .add_property("keylength_mod", &Py_BlockCipher::keylength_mod)
+ .add_property("name", &Py_BlockCipher::name)
+ .def("clear", &Py_BlockCipher::clear)
+ .def("valid_keylength", &Py_BlockCipher::valid_keylength)
+ .def("set_key", &Py_BlockCipher::set_key)
+ .def("encrypt", &Py_BlockCipher::encrypt)
+ .def("decrypt", &Py_BlockCipher::decrypt);
+
+ 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);
+
+ python::class_<Py_HashFunction>("HashFunction", python::init<std::string>())
+ .add_property("output_length", &Py_HashFunction::output_length)
+ .add_property("name", &Py_HashFunction::name)
+ .def("clear", &Py_HashFunction::clear)
+ .def("update", &Py_HashFunction::update)
+ .def("final", &Py_HashFunction::final);
+
+ 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/misc/python/src/block.cpp b/misc/python/src/block.cpp
deleted file mode 100644
index 57b132e3e..000000000
--- a/misc/python/src/block.cpp
+++ /dev/null
@@ -1,78 +0,0 @@
-/*************************************************
-* Boost.Python module definition *
-* (C) 1999-2006 The Botan Project *
-*************************************************/
-
-#include <botan/botan.h>
-using namespace Botan;
-
-#include <boost/python.hpp>
-namespace python = boost::python;
-
-/* Encrypt or decrypt */
-std::string process(const std::string& in, BlockCipher* cipher,
- void (BlockCipher::* proc)(const byte[], byte[]) const)
- {
- if(in.size() != cipher->BLOCK_SIZE)
- throw Invalid_Argument("Input for cipher " + cipher->name() +
- " must be " + to_string(cipher->BLOCK_SIZE) +
- " bytes");
-
- const byte* in_data = (const byte*)in.data();
-
- SecureVector<byte> out(cipher->BLOCK_SIZE);
- (cipher->*proc)(in_data, out);
-
- return std::string((const char*)out.begin(), out.size());
- }
-
-class Py_BlockCipher
- {
- public:
- u32bit block_size() const { return cipher->BLOCK_SIZE; }
- u32bit keylength_min() const { return cipher->MINIMUM_KEYLENGTH; }
- u32bit keylength_max() const { return cipher->MAXIMUM_KEYLENGTH; }
- u32bit keylength_mod() const { return cipher->KEYLENGTH_MULTIPLE; }
-
- 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 encrypt(const std::string& in) const
- {
- return process(in, cipher, &BlockCipher::encrypt);
- }
- std::string decrypt(const std::string& in) const
- {
- return process(in, cipher, &BlockCipher::decrypt);
- }
-
- void set_key(const OctetString& key) { cipher->set_key(key); }
-
- Py_BlockCipher(const std::string& name)
- {
- cipher = get_block_cipher(name);
- }
- ~Py_BlockCipher() { delete cipher; }
- private:
- BlockCipher* cipher;
- };
-
-void export_block_ciphers()
- {
- python::class_<Py_BlockCipher>("BlockCipher", python::init<std::string>())
- .add_property("block_size", &Py_BlockCipher::block_size)
- .add_property("keylength_min", &Py_BlockCipher::keylength_min)
- .add_property("keylength_max", &Py_BlockCipher::keylength_max)
- .add_property("keylength_mod", &Py_BlockCipher::keylength_mod)
- .add_property("name", &Py_BlockCipher::name)
- .def("clear", &Py_BlockCipher::clear)
- .def("valid_keylength", &Py_BlockCipher::valid_keylength)
- .def("set_key", &Py_BlockCipher::set_key)
- .def("encrypt", &Py_BlockCipher::encrypt)
- .def("decrypt", &Py_BlockCipher::decrypt);
- }
diff --git a/misc/python/src/core.cpp b/misc/python/src/core.cpp
index 0f30f470e..649b48a88 100644
--- a/misc/python/src/core.cpp
+++ b/misc/python/src/core.cpp
@@ -9,10 +9,7 @@ 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_basic_algos();
extern void export_filters();
extern void export_pk();
extern void export_x509();
@@ -32,10 +29,7 @@ BOOST_PYTHON_MODULE(_botan)
.value("encryption", ENCRYPTION)
.value("decryption", DECRYPTION);
- export_block_ciphers();
- export_stream_ciphers();
- export_hash_functions();
- export_macs();
+ export_basic_algos();
export_filters();
export_pk();
export_x509();
diff --git a/misc/python/src/hash.cpp b/misc/python/src/hash.cpp
deleted file mode 100644
index 3e1afd79c..000000000
--- a/misc/python/src/hash.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-/*************************************************
-* Boost.Python module definition *
-* (C) 1999-2006 The Botan Project *
-*************************************************/
-
-#include <botan/botan.h>
-using namespace Botan;
-
-#include <boost/python.hpp>
-namespace python = boost::python;
-
-class Py_HashFunction
- {
- public:
- u32bit output_length() const { return hash->OUTPUT_LENGTH; }
- std::string name() const { return hash->name(); }
- void clear() throw() { hash->clear(); }
-
- void update(const std::string& in) { hash->update(in); }
-
- std::string final()
- {
- SecureVector<byte> result = hash->final();
- return std::string((const char*)result.begin(), result.size());
- }
-
- Py_HashFunction(const std::string& name)
- {
- hash = get_hash(name);
- }
- ~Py_HashFunction() { delete hash; }
- private:
- HashFunction* hash;
- };
-
-void export_hash_functions()
- {
- python::class_<Py_HashFunction>("HashFunction", python::init<std::string>())
- .add_property("output_length", &Py_HashFunction::output_length)
- .add_property("name", &Py_HashFunction::name)
- .def("clear", &Py_HashFunction::clear)
- .def("update", &Py_HashFunction::update)
- .def("final", &Py_HashFunction::final);
- }
diff --git a/misc/python/src/mac.cpp b/misc/python/src/mac.cpp
deleted file mode 100644
index 0a8f1d418..000000000
--- a/misc/python/src/mac.cpp
+++ /dev/null
@@ -1,59 +0,0 @@
-/*************************************************
-* Boost.Python module definition *
-* (C) 1999-2006 The Botan Project *
-*************************************************/
-
-#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/misc/python/src/stream.cpp b/misc/python/src/stream.cpp
deleted file mode 100644
index 35e9e7c13..000000000
--- a/misc/python/src/stream.cpp
+++ /dev/null
@@ -1,55 +0,0 @@
-/*************************************************
-* Boost.Python module definition *
-* (C) 1999-2006 The Botan Project *
-*************************************************/
-
-#include <botan/botan.h>
-using namespace Botan;
-
-#include <boost/python.hpp>
-namespace python = boost::python;
-
-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);
- }