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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

96.61
/src/lib/pubkey/mce/mceliece.cpp
1
/*
2
 * (C) Copyright Projet SECRET, INRIA, Rocquencourt
3
 * (C) Bhaskar Biswas and  Nicolas Sendrier
4
 *
5
 * (C) 2014 cryptosource GmbH
6
 * (C) 2014 Falko Strenzke fstrenzke@cryptosource.de
7
 *
8
 * Botan is released under the Simplified BSD License (see license.txt)
9
 *
10
 */
11

12
#include <botan/internal/mce_internal.h>
13

14
#include <botan/mceliece.h>
15
#include <botan/internal/bit_ops.h>
16
#include <botan/internal/code_based_util.h>
17

18
namespace Botan {
19

20
namespace {
21

22
secure_vector<uint8_t> concat_vectors(const secure_vector<uint8_t>& a,
2,646✔
23
                                      const secure_vector<uint8_t>& b,
24
                                      size_t dimension,
25
                                      size_t codimension) {
26
   secure_vector<uint8_t> x(bit_size_to_byte_size(dimension) + bit_size_to_byte_size(codimension));
2,646✔
27

28
   const size_t final_bits = dimension % 8;
2,646✔
29

30
   if(final_bits == 0) {
2,646✔
31
      const size_t dim_bytes = bit_size_to_byte_size(dimension);
713✔
32
      copy_mem(&x[0], a.data(), dim_bytes);
713✔
33
      copy_mem(&x[dim_bytes], b.data(), bit_size_to_byte_size(codimension));
713✔
34
   } else {
35
      copy_mem(&x[0], a.data(), (dimension / 8));
1,933✔
36
      size_t l = dimension / 8;
1,933✔
37
      x[l] = static_cast<uint8_t>(a[l] & ((1 << final_bits) - 1));
1,933✔
38

39
      for(size_t k = 0; k < codimension / 8; ++k) {
90,737✔
40
         x[l] ^= static_cast<uint8_t>(b[k] << final_bits);
88,804✔
41
         ++l;
88,804✔
42
         x[l] = static_cast<uint8_t>(b[k] >> (8 - final_bits));
88,804✔
43
      }
44
      x[l] ^= static_cast<uint8_t>(b[codimension / 8] << final_bits);
1,933✔
45
   }
46

47
   return x;
2,646✔
48
}
×
49

50
secure_vector<uint8_t> mult_by_pubkey(const secure_vector<uint8_t>& cleartext,
2,646✔
51
                                      const std::vector<uint8_t>& public_matrix,
52
                                      size_t code_length,
53
                                      size_t t) {
54
   const size_t ext_deg = ceil_log2(code_length);
2,646✔
55
   const size_t codimension = ext_deg * t;
2,646✔
56
   const size_t dimension = code_length - codimension;
2,646✔
57
   secure_vector<uint8_t> cR(bit_size_to_32bit_size(codimension) * sizeof(uint32_t));
2,646✔
58

59
   const uint8_t* pt = public_matrix.data();
2,646✔
60

61
   for(size_t i = 0; i < dimension / 8; ++i) {
345,526✔
62
      for(size_t j = 0; j < 8; ++j) {
3,085,920✔
63
         if(cleartext[i] & (1 << j)) {
2,743,040✔
64
            xor_buf(cR.data(), pt, cR.size());
1,371,874✔
65
         }
66
         pt += cR.size();
2,743,040✔
67
      }
68
   }
69

70
   for(size_t i = 0; i < dimension % 8; ++i) {
10,096✔
71
      if(cleartext[dimension / 8] & (1 << i)) {
7,450✔
72
         xor_buf(cR.data(), pt, cR.size());
3,654✔
73
      }
74
      pt += cR.size();
7,450✔
75
   }
76

77
   secure_vector<uint8_t> ciphertext = concat_vectors(cleartext, cR, dimension, codimension);
2,646✔
78
   ciphertext.resize((code_length + 7) / 8);
2,646✔
79
   return ciphertext;
2,646✔
80
}
2,646✔
81

82
secure_vector<uint8_t> create_random_error_vector(size_t code_length, size_t error_weight, RandomNumberGenerator& rng) {
2,646✔
83
   secure_vector<uint8_t> result((code_length + 7) / 8);
2,646✔
84

85
   size_t bits_set = 0;
2,646✔
86

87
   while(bits_set < error_weight) {
85,370✔
88
      gf2m x = random_code_element(static_cast<uint16_t>(code_length), rng);
82,724✔
89

90
      const size_t byte_pos = x / 8;
82,724✔
91
      const size_t bit_pos = x % 8;
82,724✔
92

93
      const uint8_t mask = (1 << bit_pos);
82,724✔
94

95
      if(result[byte_pos] & mask)
82,724✔
96
         continue;  // already set this bit
1,085✔
97

98
      result[byte_pos] |= mask;
81,639✔
99
      bits_set++;
81,639✔
100
   }
101

102
   return result;
2,646✔
103
}
×
104

105
}
106

107
void mceliece_encrypt(secure_vector<uint8_t>& ciphertext_out,
2,646✔
108
                      secure_vector<uint8_t>& error_mask_out,
109
                      const secure_vector<uint8_t>& plaintext,
110
                      const McEliece_PublicKey& key,
111
                      RandomNumberGenerator& rng) {
112
   const uint16_t code_length = static_cast<uint16_t>(key.get_code_length());
2,646✔
113

114
   secure_vector<uint8_t> error_mask = create_random_error_vector(code_length, key.get_t(), rng);
2,646✔
115

116
   secure_vector<uint8_t> ciphertext =
2,646✔
117
      mult_by_pubkey(plaintext, key.get_public_matrix(), key.get_code_length(), key.get_t());
2,646✔
118

119
   ciphertext ^= error_mask;
2,646✔
120

121
   ciphertext_out.swap(ciphertext);
2,646✔
122
   error_mask_out.swap(error_mask);
2,646✔
123
}
2,646✔
124

125
}
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