Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 BOTAN_ASSERT_NOMSG(r.size() > 0);
27 for(size_t i = 0; i != mk.size(); ++i)
28 r[i % r.size()] ^= mk[i];
29 return r;
30 }
31
32 }
33
34 secure_vector<uint8_t>
35 mceies_encrypt(const McEliece_PublicKey& pubkey,
36 const uint8_t pt[], size_t pt_len,
37 const uint8_t ad[], size_t ad_len,
38 RandomNumberGenerator& rng,
39 const std::string& algo)
40 {
41 PK_KEM_Encryptor kem_op(pubkey, rng, "KDF1(SHA-512)");
42
43 secure_vector<uint8_t> mce_ciphertext, mce_key;
44 kem_op.encrypt(mce_ciphertext, mce_key, 64, rng);
45
46 const size_t mce_code_bytes = (pubkey.get_code_length() + 7) / 8;
47
48 BOTAN_ASSERT(mce_ciphertext.size() == mce_code_bytes, "Unexpected size");
49
50 std::unique_ptr<AEAD_Mode> aead = AEAD_Mode::create_or_throw(algo, ENCRYPTION);
51
52 const size_t nonce_len = aead->default_nonce_length();
53
54 aead->set_key(aead_key(mce_key, *aead));
55 aead->set_associated_data(ad, ad_len);
56
57 const secure_vector<uint8_t> nonce = rng.random_vec(nonce_len);
58
59 secure_vector<uint8_t> msg(mce_ciphertext.size() + nonce.size() + pt_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);
63
64 aead->start(nonce);
65 aead->finish(msg, mce_ciphertext.size() + nonce.size());
66 return msg;
67 }
68
69 secure_vector<uint8_t>
70 mceies_decrypt(const McEliece_PrivateKey& privkey,
71 const uint8_t ct[], size_t ct_len,
72 const uint8_t ad[], size_t ad_len,
73 const std::string& algo)
74 {
75 try
76 {
77 Null_RNG null_rng;
78 PK_KEM_Decryptor kem_op(privkey, null_rng, "KDF1(SHA-512)");
79
80 const size_t mce_code_bytes = (privkey.get_code_length() + 7) / 8;
81
82 std::unique_ptr<AEAD_Mode> aead = AEAD_Mode::create_or_throw(algo, DECRYPTION);
83
84 const size_t nonce_len = aead->default_nonce_length();
85
86 if(ct_len < mce_code_bytes + nonce_len + aead->tag_size())
87 throw Decoding_Error("Input message too small to be valid");
88
89 const secure_vector<uint8_t> mce_key = kem_op.decrypt(ct, mce_code_bytes, 64);
90
91 aead->set_key(aead_key(mce_key, *aead));
92 aead->set_associated_data(ad, ad_len);
93
94 secure_vector<uint8_t> pt(ct + mce_code_bytes + nonce_len, ct + ct_len);
95
96 aead->start(&ct[mce_code_bytes], nonce_len);
97 aead->finish(pt, 0);
98 return pt;
99 }
100 catch(Invalid_Authentication_Tag&)
101 {
102 throw;
103 }
104 catch(std::exception& e)
105 {
106 throw Decoding_Error("mce_decrypt failed: " + std::string(e.what()));
107 }
108 }
109
110 }
Botan::McEliece_PrivateKey
Definition: mceliece.h:70
Botan::mceies_decrypt
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:70
Botan::RandomNumberGenerator::random_vec
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:141
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::McEliece_PublicKey::get_code_length
size_t get_code_length() const
Definition: mceliece.h:50
BOTAN_ASSERT_NOMSG
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
Botan::Decoding_Error
Definition: exceptn.h:199
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Botan::PK_KEM_Decryptor::decrypt
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:177
Botan::DECRYPTION
Definition: cipher_mode.h:23
Botan::mceies_encrypt
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:35
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:132
Botan
Definition: alg_id.cpp:13
Botan::Null_RNG
Definition: rng.h:204
Botan::ENCRYPTION
Definition: cipher_mode.h:23
Botan::McEliece_PublicKey
Definition: mceliece.h:21
Botan::PK_KEM_Decryptor
Definition: pubkey.h:718
Botan::AEAD_Mode::create_or_throw
static std::unique_ptr< AEAD_Mode > create_or_throw(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
Definition: aead.cpp:42
Botan::Invalid_Authentication_Tag
Definition: exceptn.h:290
Botan::PK_KEM_Encryptor
Definition: pubkey.h:624
Botan::PK_KEM_Encryptor::encrypt
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:150