8 #include <botan/mceies.h>
9 #include <botan/aead.h>
10 #include <botan/mceliece.h>
11 #include <botan/pubkey.h>
17 secure_vector<uint8_t> aead_key(
const secure_vector<uint8_t>& mk,
18 const AEAD_Mode& aead)
21 if(aead.valid_keylength(mk.size()))
24 secure_vector<uint8_t> r(aead.key_spec().maximum_keylength());
25 for(
size_t i = 0; i != mk.size(); ++i)
26 r[i % r.size()] ^= mk[i];
32 secure_vector<uint8_t>
34 const uint8_t pt[],
size_t pt_len,
35 const uint8_t ad[],
size_t ad_len,
37 const std::string& algo)
42 kem_op.
encrypt(mce_ciphertext, mce_key, 64, rng);
46 BOTAN_ASSERT(mce_ciphertext.size() == mce_code_bytes,
"Unexpected size");
50 throw Exception(
"mce_encrypt unable to create AEAD instance '" + algo +
"'");
52 const size_t nonce_len = aead->default_nonce_length();
54 aead->set_key(aead_key(mce_key, *aead));
55 aead->set_associated_data(ad, ad_len);
60 copy_mem(msg.data(), mce_ciphertext.data(), mce_ciphertext.size());
61 copy_mem(msg.data() + mce_ciphertext.size(), nonce.data(), nonce.size());
62 copy_mem(msg.data() + mce_ciphertext.size() + nonce.size(), pt, pt_len);
65 aead->finish(msg, mce_ciphertext.size() + nonce.size());
69 secure_vector<uint8_t>
71 const uint8_t ct[],
size_t ct_len,
72 const uint8_t ad[],
size_t ad_len,
73 const std::string& algo)
84 throw Exception(
"Unable to create AEAD instance '" + algo +
"'");
86 const size_t nonce_len = aead->default_nonce_length();
88 if(ct_len < mce_code_bytes + nonce_len + aead->tag_size())
89 throw Exception(
"Input message too small to be valid");
93 aead->set_key(aead_key(mce_key, *aead));
94 aead->set_associated_data(ad, ad_len);
98 aead->start(&ct[mce_code_bytes], nonce_len);
106 catch(std::exception& e)
108 throw Exception(
"mce_decrypt failed: " + std::string(e.what()));
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)
secure_vector< uint8_t > random_vec(size_t bytes)
uint32_t get_code_length() const
#define BOTAN_ASSERT(expr, assertion_made)
std::vector< T, secure_allocator< T >> secure_vector
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)
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)
AEAD_Mode * get_aead(const std::string &algo, Cipher_Dir dir)
void copy_mem(T *out, const T *in, size_t n)
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)