• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 27806188297

18 Jun 2026 04:12PM UTC coverage: 89.37% (-0.03%) from 89.397%
27806188297

push

github

web-flow
Merge pull request #5677 from randombit/jack/oid-names

Add OID::registered_name

111637 of 124915 relevant lines covered (89.37%)

10895907.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.23
/src/tests/test_mceliece.cpp
1
/*
2
* (C) 2014 cryptosource GmbH
3
* (C) 2014 Falko Strenzke fstrenzke@cryptosource.de
4
* (C) 2014,2015 Jack Lloyd
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include "tests.h"
10

11
#if defined(BOTAN_HAS_MCELIECE)
12

13
   #include <botan/hash.h>
14
   #include <botan/hex.h>
15
   #include <botan/mceliece.h>
16
   #include <botan/pubkey.h>
17
   #include <botan/rng.h>
18

19
   #if defined(BOTAN_HAS_HMAC_DRBG)
20
      #include <botan/hmac_drbg.h>
21
   #endif
22

23
#endif
24

25
namespace Botan_Tests {
26

27
namespace {
28

29
#if defined(BOTAN_HAS_MCELIECE)
30

31
   #if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_32) && defined(BOTAN_HAS_SHA2_64)
32
class McEliece_Keygen_Encrypt_Test final : public Text_Based_Test {
×
33
   public:
34
      McEliece_Keygen_Encrypt_Test() :
1✔
35
            Text_Based_Test("pubkey/mce.vec",
36
                            "McElieceSeed,KeyN,KeyT,PublicKeyFingerprint,PrivateKeyFingerprint,"
37
                            "EncryptPRNGSeed,SharedKey,Ciphertext",
38
                            "") {}
2✔
39

40
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
6✔
41
         const std::vector<uint8_t> keygen_seed = vars.get_req_bin("McElieceSeed");
6✔
42
         const std::vector<uint8_t> fprint_pub = vars.get_req_bin("PublicKeyFingerprint");
6✔
43
         const std::vector<uint8_t> fprint_priv = vars.get_req_bin("PrivateKeyFingerprint");
6✔
44
         const std::vector<uint8_t> encrypt_seed = vars.get_req_bin("EncryptPRNGSeed");
6✔
45
         const std::vector<uint8_t> ciphertext = vars.get_req_bin("Ciphertext");
6✔
46
         const std::vector<uint8_t> shared_key = vars.get_req_bin("SharedKey");
6✔
47
         const size_t keygen_n = vars.get_req_sz("KeyN");
6✔
48
         const size_t keygen_t = vars.get_req_sz("KeyT");
6✔
49

50
         Test::Result result("McEliece keygen");
6✔
51
         result.start_timer();
6✔
52

53
         if(Test::run_long_tests() == false && keygen_n > 3072) {
6✔
54
            result.test_note("Skipping because long");
×
55
            return result;
×
56
         }
57

58
         Botan::HMAC_DRBG rng("SHA-384");
6✔
59
         rng.initialize_with(keygen_seed.data(), keygen_seed.size());
6✔
60
         const Botan::McEliece_PrivateKey mce_priv(rng, keygen_n, keygen_t);
6✔
61

62
         result.test_bin_eq("public key fingerprint", hash_bytes(mce_priv.public_key_bits()), fprint_pub);
12✔
63
         result.test_bin_eq("private key fingerprint", hash_bytes(mce_priv.private_key_bits()), fprint_priv);
12✔
64

65
         rng.clear();
6✔
66
         rng.initialize_with(encrypt_seed.data(), encrypt_seed.size());
6✔
67

68
         try {
6✔
69
            Botan::PK_KEM_Encryptor kem_enc(mce_priv, "KDF1(SHA-512)");
6✔
70
            Botan::PK_KEM_Decryptor kem_dec(mce_priv, this->rng(), "KDF1(SHA-512)");
6✔
71

72
            const auto kem_result = kem_enc.encrypt(rng, 64);
6✔
73

74
            Botan::secure_vector<uint8_t> dec_shared_key =
6✔
75
               kem_dec.decrypt(kem_result.encapsulated_shared_key(), 64, {});
6✔
76

77
            result.test_bin_eq("ciphertext", kem_result.encapsulated_shared_key(), ciphertext);
6✔
78
            result.test_bin_eq("encrypt shared", kem_result.shared_key(), shared_key);
6✔
79
            result.test_bin_eq("decrypt shared", dec_shared_key, shared_key);
6✔
80
         } catch(Botan::Lookup_Error&) {}
6✔
81

82
         result.end_timer();
6✔
83
         return result;
6✔
84
      }
42✔
85

86
   private:
87
      static std::vector<uint8_t> hash_bytes(const uint8_t b[], size_t len, const std::string& hash_fn = "SHA-256") {
12✔
88
         auto hash = Botan::HashFunction::create(hash_fn);
12✔
89
         hash->update(b, len);
12✔
90
         std::vector<uint8_t> r(hash->output_length());
12✔
91
         hash->final(r.data());
12✔
92
         return r;
12✔
93
      }
12✔
94

95
      template <typename A>
96
      std::vector<uint8_t> hash_bytes(const std::vector<uint8_t, A>& v) {
12✔
97
         return hash_bytes(v.data(), v.size());
24✔
98
      }
99
};
100

101
BOTAN_REGISTER_TEST("pubkey", "mce_keygen", McEliece_Keygen_Encrypt_Test);
102
   #endif
103

104
   #if defined(BOTAN_HAS_SHA2_32)
105

106
class McEliece_Tests final : public Test {
1✔
107
   public:
108
      static std::string fingerprint(const Botan::Private_Key& key, const std::string& hash_algo = "SHA-256") {
12✔
109
         auto hash = Botan::HashFunction::create(hash_algo);
12✔
110
         if(!hash) {
12✔
111
            throw Test_Error("Hash " + hash_algo + " not available");
×
112
         }
113

114
         hash->update(key.private_key_bits());
12✔
115
         return Botan::hex_encode(hash->final());
24✔
116
      }
12✔
117

118
      static std::string fingerprint(const Botan::Public_Key& key, const std::string& hash_algo = "SHA-256") {
12✔
119
         auto hash = Botan::HashFunction::create(hash_algo);
12✔
120
         if(!hash) {
12✔
121
            throw Test_Error("Hash " + hash_algo + " not available");
×
122
         }
123

124
         hash->update(key.public_key_bits());
12✔
125
         return Botan::hex_encode(hash->final());
24✔
126
      }
12✔
127

128
      std::vector<Test::Result> run() override {
1✔
129
         struct keygen_params {
1✔
130
               size_t code_length, t;
131
         };
132

133
         const keygen_params param_sets[] = {{1632, 33}, {2480, 45}, {2960, 57}, {3408, 67}, {4624, 95}, {6624, 115}};
1✔
134

135
         std::vector<Test::Result> results;
1✔
136
         results.push_back(test_invalid_params(this->rng()));
2✔
137

138
         for(const auto& params : param_sets) {
7✔
139
            if(Test::run_long_tests() == false && params.code_length >= 2048) {
6✔
140
               continue;
×
141
            }
142

143
            Test::Result result("McEliece keygen");
6✔
144
            result.start_timer();
6✔
145

146
            const Botan::McEliece_PrivateKey sk1(this->rng(), params.code_length, params.t);
6✔
147
            const Botan::McEliece_PublicKey& pk1 = sk1;
6✔
148

149
            const std::vector<uint8_t> pk_enc = pk1.public_key_bits();
6✔
150
            const Botan::secure_vector<uint8_t> sk_enc = sk1.private_key_bits();
6✔
151

152
            const Botan::McEliece_PublicKey pk(pk_enc);
6✔
153
            const Botan::McEliece_PrivateKey sk(sk_enc);
6✔
154

155
            result.test_str_eq("decoded public key equals original", fingerprint(pk1), fingerprint(pk));
6✔
156
            result.test_str_eq("decoded private key equals original", fingerprint(sk1), fingerprint(sk));
6✔
157
            result.test_is_true("key validation passes", sk.check_key(this->rng(), false));
6✔
158
            result.end_timer();
6✔
159

160
            results.push_back(result);
6✔
161

162
      #if defined(BOTAN_HAS_KDF2)
163
            results.push_back(test_kem(sk, pk, this->rng()));
12✔
164
      #endif
165
         }
18✔
166

167
         return results;
1✔
168
      }
×
169

170
   private:
171
      static Test::Result test_invalid_params(Botan::RandomNumberGenerator& rng) {
1✔
172
         Test::Result result("McEliece invalid parameters");
1✔
173

174
         result.test_throws("unsupported keygen parameters", [&] { Botan::McEliece_PrivateKey(rng, 2048, 50); });
2✔
175
         result.test_throws("unsupported public key parameters", [&] {
1✔
176
            const std::vector<uint8_t> pub_matrix;
1✔
177
            Botan::McEliece_PublicKey(pub_matrix, 50, 2048);
1✔
178
         });
×
179
         result.test_throws("wrong public matrix length", [&] {
1✔
180
            const std::vector<uint8_t> pub_matrix;
1✔
181
            Botan::McEliece_PublicKey(pub_matrix, 33, 1632);
1✔
182
         });
×
183

184
         return result;
1✔
185
      }
×
186

187
      static Test::Result test_kem(const Botan::McEliece_PrivateKey& sk,
6✔
188
                                   const Botan::McEliece_PublicKey& pk,
189
                                   Botan::RandomNumberGenerator& rng) {
190
         Test::Result result("McEliece KEM");
6✔
191
         result.start_timer();
6✔
192

193
         Botan::PK_KEM_Encryptor enc_op(pk, "KDF2(SHA-256)");
6✔
194
         Botan::PK_KEM_Decryptor dec_op(sk, rng, "KDF2(SHA-256)");
6✔
195

196
         const size_t trials = (Test::run_long_tests() ? 30 : 10);
6✔
197
         for(size_t i = 0; i < trials; i++) {
186✔
198
            Botan::secure_vector<uint8_t> salt = rng.random_vec(i);
180✔
199

200
            const auto kem_result = enc_op.encrypt(rng, 64, salt);
180✔
201

202
            Botan::secure_vector<uint8_t> shared_key2 = dec_op.decrypt(kem_result.encapsulated_shared_key(), 64, salt);
180✔
203

204
            result.test_bin_eq("same key", kem_result.shared_key(), shared_key2);
180✔
205
         }
354✔
206
         result.end_timer();
6✔
207
         return result;
6✔
208
      }
6✔
209
};
210

211
BOTAN_REGISTER_TEST("pubkey", "mceliece", McEliece_Tests);
212

213
   #endif
214

215
#endif
216

217
}  // namespace
218

219
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc