aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/pubkey/mce/mce_kem.cpp
blob: dede67731090e7ae759fdf39c7d843524eb7d444 (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
/**
 * (C) 2014 cryptosource GmbH
 * (C) 2014 Falko Strenzke fstrenzke@cryptosource.de
 *
 * Botan is released under the Simplified BSD License (see license.txt)
 *
 */

#include <botan/mce_kem.h>
#include <botan/internal/mce_internal.h>
#include <botan/sha2_64.h>

namespace Botan {

McEliece_KEM_Encryptor::McEliece_KEM_Encryptor(const McEliece_PublicKey& public_key) :
   m_key(public_key)
   {
   }

std::pair<secure_vector<byte>, secure_vector<byte>>
McEliece_KEM_Encryptor::encrypt(RandomNumberGenerator& rng)
   {
   const secure_vector<byte> plaintext = m_key.random_plaintext_element(rng);

   secure_vector<byte> ciphertext, error_mask;
   mceliece_encrypt(ciphertext, error_mask, plaintext, m_key, rng);

   SHA_512 hash;
   hash.update(plaintext);
   hash.update(error_mask);
   secure_vector<byte> sym_key = hash.final();

   return std::make_pair(ciphertext, sym_key);
   }

McEliece_KEM_Decryptor::McEliece_KEM_Decryptor(const McEliece_PrivateKey& key) : m_key(key) { }

secure_vector<Botan::byte> McEliece_KEM_Decryptor::decrypt(const byte msg[], size_t msg_len)
   {
   secure_vector<byte> plaintext, error_mask;
   mceliece_decrypt(plaintext, error_mask, msg, msg_len, m_key);

   SHA_512 hash;
   hash.update(plaintext);
   hash.update(error_mask);

   secure_vector<byte> sym_key = hash.final();
   return sym_key;
   }

}