aboutsummaryrefslogtreecommitdiffstats
path: root/src/wrap/python/core.cpp
blob: 67e17c4d535e177735e7411d1447f24ca3637e59 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Boost.Python module definition
* (C) 1999-2007 Jack Lloyd
*/

#include <botan/init.h>
#include <botan/pipe.h>
#include <botan/lookup.h>
#include <botan/cryptobox.h>
#include <botan/pbkdf2.h>
#include <botan/hmac.h>
using namespace Botan;

#include "python_botan.h"

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;
   };

std::string cryptobox_encrypt(const std::string& in,
                              const std::string& passphrase,
                              Python_RandomNumberGenerator& rng)
   {
   const byte* in_bytes = reinterpret_cast<const byte*>(in.data());

   return CryptoBox::encrypt(in_bytes, in.size(),
                             passphrase, rng.get_underlying_rng());
   }

std::string cryptobox_decrypt(const std::string& in,
                              const std::string& passphrase)
   {
   const byte* in_bytes = reinterpret_cast<const byte*>(in.data());

   return CryptoBox::decrypt(in_bytes, in.size(),
                             passphrase);
   }

std::string python_pbkdf2(const std::string& passphrase,
                          const std::string& salt,
                          u32bit iterations,
                          u32bit output_size,
                          const std::string& hash_fn)
   {
   PKCS5_PBKDF2 pbkdf2(new HMAC(get_hash(hash_fn)));

   return make_string(
      pbkdf2.derive_key(output_size,
                        passphrase,
                        reinterpret_cast<const byte*>(salt.data()),
                        salt.size(),
                        iterations).bits_of());
   }

std::string python_kdf2(const std::string& param,
                        const std::string& masterkey,
                        u32bit outputlength)
   {
   std::unique_ptr<KDF> kdf(get_kdf("KDF2(SHA-1)"));

   return make_string(
      kdf->derive_key(outputlength,
                      reinterpret_cast<const byte*>(masterkey.data()),
                      masterkey.length(),
                      param));
   }

BOOST_PYTHON_MODULE(_botan)
   {
   python::class_<LibraryInitializer>("LibraryInitializer")
      .def(python::init< python::optional<std::string> >());

   python::class_<Python_RandomNumberGenerator>("RandomNumberGenerator")
      .def(python::init<>())
      .def("__str__", &Python_RandomNumberGenerator::name)
      .def("name", &Python_RandomNumberGenerator::name)
      .def("reseed", &Python_RandomNumberGenerator::reseed)
      .def("add_entropy", &Python_RandomNumberGenerator::add_entropy)
      .def("gen_random_byte", &Python_RandomNumberGenerator::gen_random_byte)
      .def("gen_random", &Python_RandomNumberGenerator::gen_random);

   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);

   python::def("cryptobox_encrypt", cryptobox_encrypt);
   python::def("cryptobox_decrypt", cryptobox_decrypt);
   python::def("pbkdf2", python_pbkdf2);
   python::def("derive_key", python_kdf2);

   export_filters();
   export_rsa();
   export_x509();
   }