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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

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

92.16
/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

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

22
#endif
23

24
namespace Botan_Tests {
25

26
namespace {
27

28
#if defined(BOTAN_HAS_MCELIECE)
29

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

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

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

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

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

61
         result.test_eq("public key fingerprint", hash_bytes(mce_priv.public_key_bits()), fprint_pub);
18✔
62
         result.test_eq("private key fingerprint", hash_bytes(mce_priv.private_key_bits()), fprint_priv);
18✔
63

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

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

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

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

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

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

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

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

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

103
   #if defined(BOTAN_HAS_SHA2_32)
104

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

113
         hash->update(key.private_key_bits());
170✔
114
         return Botan::hex_encode(hash->final());
340✔
115
      }
170✔
116

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

123
         hash->update(key.public_key_bits());
170✔
124
         return Botan::hex_encode(hash->final());
340✔
125
      }
170✔
126

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

132
         const keygen_params param_sets[] = {
1✔
133
            {256, 5, 15}, {512, 5, 33}, {1024, 15, 35}, {2048, 33, 50}, {6624, 110, 115}};
134

135
         std::vector<Test::Result> results;
1✔
136

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

142
            for(size_t t = params.t_min; t <= params.t_max; ++t) {
90✔
143
               Test::Result result("McEliece keygen");
85✔
144
               result.start_timer();
85✔
145

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

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

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

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

160
               result.end_timer();
85✔
161

162
               results.push_back(result);
85✔
163

164
      #if defined(BOTAN_HAS_KDF2)
165
               results.push_back(test_kem(sk, pk, this->rng()));
170✔
166
      #endif
167
            }
255✔
168
         }
169

170
         return results;
1✔
171
      }
×
172

173
   private:
174
      static Test::Result test_kem(const Botan::McEliece_PrivateKey& sk,
85✔
175
                                   const Botan::McEliece_PublicKey& pk,
176
                                   Botan::RandomNumberGenerator& rng) {
177
         Test::Result result("McEliece KEM");
85✔
178
         result.start_timer();
85✔
179

180
         Botan::PK_KEM_Encryptor enc_op(pk, "KDF2(SHA-256)");
85✔
181
         Botan::PK_KEM_Decryptor dec_op(sk, rng, "KDF2(SHA-256)");
85✔
182

183
         const size_t trials = (Test::run_long_tests() ? 30 : 10);
85✔
184
         for(size_t i = 0; i < trials; i++) {
2,635✔
185
            Botan::secure_vector<uint8_t> salt = rng.random_vec(i);
2,550✔
186

187
            const auto kem_result = enc_op.encrypt(rng, 64, salt);
2,550✔
188

189
            Botan::secure_vector<uint8_t> shared_key2 = dec_op.decrypt(kem_result.encapsulated_shared_key(), 64, salt);
2,550✔
190

191
            result.test_eq("same key", kem_result.shared_key(), shared_key2);
5,100✔
192
         }
5,015✔
193
         result.end_timer();
85✔
194
         return result;
85✔
195
      }
85✔
196
};
197

198
BOTAN_REGISTER_TEST("pubkey", "mceliece", McEliece_Tests);
199

200
   #endif
201

202
#endif
203

204
}  // namespace
205

206
}  // 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