aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_mceliece.cpp
blob: 36ecd660e6af47b7d8264155d20431f245f6cc33 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* (C) 2014 cryptosource GmbH
* (C) 2014 Falko Strenzke fstrenzke@cryptosource.de
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_MCELIECE)

#include <botan/mceliece.h>
#include <botan/pubkey.h>
#include <botan/oids.h>
#include <botan/hmac_drbg.h>
#include <botan/loadstor.h>
#include <botan/hash.h>
#include <botan/hex.h>

#if defined(BOTAN_HAS_MCEIES)
#include <botan/mceies.h>
#endif

#endif

namespace Botan_Tests {

namespace {

#if defined(BOTAN_HAS_MCELIECE)

std::vector<byte> hash_bytes(const byte b[], size_t len, const std::string& hash_fn = "SHA-256")
   {
   std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(hash_fn));
   hash->update(b, len);
   std::vector<byte> r(hash->output_length());
   hash->final(r.data());
   return r;
   }

template<typename A>
std::vector<byte> hash_bytes(const std::vector<byte, A>& v)
   {
   return hash_bytes(v.data(), v.size());
   }

class McEliece_Keygen_Encrypt_Test : public Text_Based_Test
   {
   public:
      McEliece_Keygen_Encrypt_Test() :
         Text_Based_Test("McEliece",
                         "pubkey/mce.vec",
                         {"McElieceSeed", "KeyN","KeyT","PublicKeyFingerprint",
                          "PrivateKeyFingerprint", "EncryptPRNGSeed",
                            "SharedKey", "Ciphertext" })
         {}

      Test::Result run_one_test(const std::string&, const VarMap& vars) override
         {
         const std::vector<byte> keygen_seed  = get_req_bin(vars, "McElieceSeed");
         const std::vector<byte> fprint_pub   = get_req_bin(vars, "PublicKeyFingerprint");
         const std::vector<byte> fprint_priv  = get_req_bin(vars, "PrivateKeyFingerprint");
         const std::vector<byte> encrypt_seed = get_req_bin(vars, "EncryptPRNGSeed");
         const std::vector<byte> ciphertext   = get_req_bin(vars, "Ciphertext");
         const std::vector<byte> shared_key   = get_req_bin(vars, "SharedKey");
         const size_t keygen_n = get_req_sz(vars, "KeyN");
         const size_t keygen_t = get_req_sz(vars, "KeyT");

         Botan::HMAC_DRBG rng("HMAC(SHA-384)");

         rng.add_entropy(keygen_seed.data(), keygen_seed.size());
         Botan::McEliece_PrivateKey mce_priv(rng, keygen_n, keygen_t);

         Test::Result result("McEliece keygen");

         result.test_eq("public key fingerprint", hash_bytes(mce_priv.x509_subject_public_key()), fprint_pub);
         result.test_eq("private key fingerprint", hash_bytes(mce_priv.pkcs8_private_key()), fprint_priv);

         rng.clear();
         rng.add_entropy(encrypt_seed.data(), encrypt_seed.size());

         Botan::PK_KEM_Encryptor kem_enc(mce_priv, "KDF1(SHA-512)");
         Botan::PK_KEM_Decryptor kem_dec(mce_priv, "KDF1(SHA-512)");

         Botan::secure_vector<byte> encap_key, prod_shared_key;
         kem_enc.encrypt(encap_key, prod_shared_key, 64, rng);

         Botan::secure_vector<byte> dec_shared_key = kem_dec.decrypt(encap_key.data(), encap_key.size(), 64);

         result.test_eq("ciphertext", encap_key, ciphertext);
         result.test_eq("encrypt shared", prod_shared_key, shared_key);
         result.test_eq("decrypt shared", dec_shared_key, shared_key);
         return result;
         }

   };

BOTAN_REGISTER_TEST("mce_keygen", McEliece_Keygen_Encrypt_Test);

class McEliece_Tests : public Test
   {
   public:

      std::string fingerprint(const Botan::Private_Key& key, const std::string& hash_algo = "SHA-256")
         {
         std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(hash_algo));
         if(!hash)
            throw Test_Error("Hash " + hash_algo + " not available");

         hash->update(key.pkcs8_private_key());
         return Botan::hex_encode(hash->final());
         }

      std::string fingerprint(const Botan::Public_Key& key, const std::string& hash_algo = "SHA-256")
         {
         std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(hash_algo));
         if(!hash)
            throw Test_Error("Hash " + hash_algo + " not available");

         hash->update(key.x509_subject_public_key());
         return Botan::hex_encode(hash->final());
         }

      std::vector<Test::Result> run() override
         {
         size_t params__n__t_min_max[] = {
            256, 5, 15,
            512, 5, 33,
            1024, 15, 35,
            2048, 33, 50,
            2960, 50, 56,
            6624, 110, 115
         };

         std::vector<Test::Result> results;

         for(size_t i = 0; i < sizeof(params__n__t_min_max)/sizeof(params__n__t_min_max[0]); i+=3)
            {
            const size_t code_length = params__n__t_min_max[i];
            const size_t min_t = params__n__t_min_max[i+1];
            const size_t max_t = params__n__t_min_max[i+2];

            for(size_t t = min_t; t <= max_t; ++t)
               {
               Botan::McEliece_PrivateKey sk1(Test::rng(), code_length, t);
               const Botan::McEliece_PublicKey& pk1 = sk1;

               const std::vector<byte> pk_enc = pk1.x509_subject_public_key();
               const Botan::secure_vector<byte> sk_enc = sk1.pkcs8_private_key();

               Botan::McEliece_PublicKey pk(pk_enc);
               Botan::McEliece_PrivateKey sk(sk_enc);

               Test::Result result("McEliece keygen");

               result.test_eq("decoded public key equals original", fingerprint(pk1), fingerprint(pk));

               result.test_eq("decoded private key equals original", fingerprint(sk1), fingerprint(sk));

               result.test_eq("key validation passes", sk.check_key(Test::rng(), false), true);

               results.push_back(result);

               results.push_back(test_kem(sk, pk));

#if defined(BOTAN_HAS_MCEIES)
               results.push_back(test_mceies(sk, pk));
#endif
               }
            }

         return results;
         }

   private:
      Test::Result test_kem(const Botan::McEliece_PrivateKey& sk,
                            const Botan::McEliece_PublicKey& pk)
         {
         Test::Result result("McEliece KEM");

         Botan::PK_KEM_Encryptor enc_op(pk, "KDF2(SHA-256)");
         Botan::PK_KEM_Decryptor dec_op(sk, "KDF2(SHA-256)");

         for(size_t i = 0; i <= Test::soak_level(); i++)
            {
            Botan::secure_vector<byte> salt = Test::rng().random_vec(i);

            Botan::secure_vector<byte> encap_key, shared_key;
            enc_op.encrypt(encap_key, shared_key, 64, Test::rng(), salt);

            Botan::secure_vector<byte> shared_key2 = dec_op.decrypt(encap_key, 64, salt);

            result.test_eq("same key", shared_key, shared_key2);
            }
         return result;
         }

#if defined(BOTAN_HAS_MCEIES)
      Test::Result test_mceies(const Botan::McEliece_PrivateKey& sk,
                               const Botan::McEliece_PublicKey& pk)
         {
         Test::Result result("McEliece IES");

         for(size_t i = 0; i <= Test::soak_level(); ++i)
            {
            uint8_t ad[8];
            Botan::store_be(static_cast<Botan::u64bit>(i), ad);
            const size_t ad_len = sizeof(ad);

            const Botan::secure_vector<byte> pt = Test::rng().random_vec(Test::rng().next_byte());

            const Botan::secure_vector<byte> ct = mceies_encrypt(pk, pt.data(), pt.size(), ad, ad_len, Test::rng());
            const Botan::secure_vector<byte> dec = mceies_decrypt(sk, ct.data(), ct.size(), ad, ad_len);

            result.test_eq("decrypted ok", dec, pt);

            Botan::secure_vector<byte> bad_ct = ct;
            for(size_t j = 0; j != 3; ++j)
               {
               bad_ct = mutate_vec(ct, true);

               try
                  {
                  mceies_decrypt(sk, bad_ct.data(), bad_ct.size(), ad, ad_len);
                  result.test_failure("AEAD decrypted manipulated ciphertext");
                  result.test_note("Manipulated text was " + Botan::hex_encode(bad_ct));
                  }
               catch(Botan::Integrity_Failure&)
                  {
                  result.test_note("AEAD rejected manipulated ciphertext");
                  }
               catch(std::exception& e)
                  {
                  result.test_failure("AEAD rejected manipulated ciphertext with unexpected error", e.what());
                  }
               }
            }

         return result;
         }
#endif

   };

BOTAN_REGISTER_TEST("mceliece", McEliece_Tests);

#endif

}

}