Botan  2.6.0
Crypto and TLS for C++11
mceies.cpp
Go to the documentation of this file.
1 /*
2 * McEliece Integrated Encryption System
3 * (C) 2014,2015 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/mceies.h>
9 #include <botan/aead.h>
10 #include <botan/rng.h>
11 #include <botan/mceliece.h>
12 #include <botan/pubkey.h>
13 
14 namespace Botan {
15 
16 namespace {
17 
18 secure_vector<uint8_t> aead_key(const secure_vector<uint8_t>& mk,
19  const AEAD_Mode& aead)
20  {
21  // Fold the key as required for the AEAD mode in use
22  if(aead.valid_keylength(mk.size()))
23  return mk;
24 
25  secure_vector<uint8_t> r(aead.key_spec().maximum_keylength());
26  for(size_t i = 0; i != mk.size(); ++i)
27  r[i % r.size()] ^= mk[i];
28  return r;
29  }
30 
31 }
32 
33 secure_vector<uint8_t>
35  const uint8_t pt[], size_t pt_len,
36  const uint8_t ad[], size_t ad_len,
38  const std::string& algo)
39  {
40  PK_KEM_Encryptor kem_op(pubkey, rng, "KDF1(SHA-512)");
41 
42  secure_vector<uint8_t> mce_ciphertext, mce_key;
43  kem_op.encrypt(mce_ciphertext, mce_key, 64, rng);
44 
45  const size_t mce_code_bytes = (pubkey.get_code_length() + 7) / 8;
46 
47  BOTAN_ASSERT(mce_ciphertext.size() == mce_code_bytes, "Unexpected size");
48 
49  std::unique_ptr<AEAD_Mode> aead = AEAD_Mode::create_or_throw(algo, ENCRYPTION);
50 
51  const size_t nonce_len = aead->default_nonce_length();
52 
53  aead->set_key(aead_key(mce_key, *aead));
54  aead->set_associated_data(ad, ad_len);
55 
56  const secure_vector<uint8_t> nonce = rng.random_vec(nonce_len);
57 
58  secure_vector<uint8_t> msg(mce_ciphertext.size() + nonce.size() + pt_len);
59  copy_mem(msg.data(), mce_ciphertext.data(), mce_ciphertext.size());
60  copy_mem(msg.data() + mce_ciphertext.size(), nonce.data(), nonce.size());
61  copy_mem(msg.data() + mce_ciphertext.size() + nonce.size(), pt, pt_len);
62 
63  aead->start(nonce);
64  aead->finish(msg, mce_ciphertext.size() + nonce.size());
65  return msg;
66  }
67 
68 secure_vector<uint8_t>
70  const uint8_t ct[], size_t ct_len,
71  const uint8_t ad[], size_t ad_len,
72  const std::string& algo)
73  {
74  try
75  {
76  Null_RNG null_rng;
77  PK_KEM_Decryptor kem_op(privkey, null_rng, "KDF1(SHA-512)");
78 
79  const size_t mce_code_bytes = (privkey.get_code_length() + 7) / 8;
80 
81  std::unique_ptr<AEAD_Mode> aead = AEAD_Mode::create_or_throw(algo, DECRYPTION);
82 
83  const size_t nonce_len = aead->default_nonce_length();
84 
85  if(ct_len < mce_code_bytes + nonce_len + aead->tag_size())
86  throw Exception("Input message too small to be valid");
87 
88  const secure_vector<uint8_t> mce_key = kem_op.decrypt(ct, mce_code_bytes, 64);
89 
90  aead->set_key(aead_key(mce_key, *aead));
91  aead->set_associated_data(ad, ad_len);
92 
93  secure_vector<uint8_t> pt(ct + mce_code_bytes + nonce_len, ct + ct_len);
94 
95  aead->start(&ct[mce_code_bytes], nonce_len);
96  aead->finish(pt, 0);
97  return pt;
98  }
99  catch(Integrity_Failure&)
100  {
101  throw;
102  }
103  catch(std::exception& e)
104  {
105  throw Exception("mce_decrypt failed: " + std::string(e.what()));
106  }
107  }
108 
109 }
secure_vector< uint8_t > mceies_decrypt(const McEliece_PrivateKey &privkey, const uint8_t ct[], size_t ct_len, const uint8_t ad[], size_t ad_len, const std::string &algo)
Definition: mceies.cpp:69
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:132
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:30
secure_vector< uint8_t > decrypt(const uint8_t encap_key[], size_t encap_key_len, size_t desired_shared_key_len, const uint8_t salt[], size_t salt_len)
Definition: pubkey.cpp:167
secure_vector< uint8_t > mceies_encrypt(const McEliece_PublicKey &pubkey, const uint8_t pt[], size_t pt_len, const uint8_t ad[], size_t ad_len, RandomNumberGenerator &rng, const std::string &algo)
Definition: mceies.cpp:34
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108
Definition: alg_id.cpp:13
uint32_t get_code_length() const
Definition: mceliece.h:50
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
static std::unique_ptr< AEAD_Mode > create_or_throw(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
Definition: aead.cpp:42
void encrypt(secure_vector< uint8_t > &out_encapsulated_key, secure_vector< uint8_t > &out_shared_key, size_t desired_shared_key_len, Botan::RandomNumberGenerator &rng, const uint8_t salt[], size_t salt_len)
Definition: pubkey.cpp:140