Botan  2.4.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(get_aead(algo, ENCRYPTION));
50  if(!aead)
51  throw Exception("mce_encrypt unable to create AEAD instance '" + algo + "'");
52 
53  const size_t nonce_len = aead->default_nonce_length();
54 
55  aead->set_key(aead_key(mce_key, *aead));
56  aead->set_associated_data(ad, ad_len);
57 
58  const secure_vector<uint8_t> nonce = rng.random_vec(nonce_len);
59 
60  secure_vector<uint8_t> msg(mce_ciphertext.size() + nonce.size() + pt_len);
61  copy_mem(msg.data(), mce_ciphertext.data(), mce_ciphertext.size());
62  copy_mem(msg.data() + mce_ciphertext.size(), nonce.data(), nonce.size());
63  copy_mem(msg.data() + mce_ciphertext.size() + nonce.size(), pt, pt_len);
64 
65  aead->start(nonce);
66  aead->finish(msg, mce_ciphertext.size() + nonce.size());
67  return msg;
68  }
69 
72  const uint8_t ct[], size_t ct_len,
73  const uint8_t ad[], size_t ad_len,
74  const std::string& algo)
75  {
76  try
77  {
78  Null_RNG null_rng;
79  PK_KEM_Decryptor kem_op(privkey, null_rng, "KDF1(SHA-512)");
80 
81  const size_t mce_code_bytes = (privkey.get_code_length() + 7) / 8;
82 
83  std::unique_ptr<AEAD_Mode> aead(get_aead(algo, DECRYPTION));
84  if(!aead)
85  throw Exception("Unable to create AEAD instance '" + algo + "'");
86 
87  const size_t nonce_len = aead->default_nonce_length();
88 
89  if(ct_len < mce_code_bytes + nonce_len + aead->tag_size())
90  throw Exception("Input message too small to be valid");
91 
92  const secure_vector<uint8_t> mce_key = kem_op.decrypt(ct, mce_code_bytes, 64);
93 
94  aead->set_key(aead_key(mce_key, *aead));
95  aead->set_associated_data(ad, ad_len);
96 
97  secure_vector<uint8_t> pt(ct + mce_code_bytes + nonce_len, ct + ct_len);
98 
99  aead->start(&ct[mce_code_bytes], nonce_len);
100  aead->finish(pt, 0);
101  return pt;
102  }
103  catch(Integrity_Failure&)
104  {
105  throw;
106  }
107  catch(std::exception& e)
108  {
109  throw Exception("mce_decrypt failed: " + std::string(e.what()));
110  }
111  }
112 
113 }
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:71
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:132
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:29
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:97
Definition: alg_id.cpp:13
uint32_t get_code_length() const
Definition: mceliece.h:50
AEAD_Mode * get_aead(const std::string &algo, Cipher_Dir dir)
Definition: aead.cpp:42
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
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