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

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

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

95.56
/src/lib/base/symkey.cpp
1
/*
2
* OctetString
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/symkey.h>
9

10
#include <botan/hex.h>
11
#include <botan/rng.h>
12
#include <algorithm>
13

14
namespace Botan {
15

16
/*
17
* Create an OctetString from RNG output
18
*/
19
OctetString::OctetString(RandomNumberGenerator& rng, size_t len) {
11✔
20
   rng.random_vec(m_data, len);
11✔
21
}
11✔
22

23
/*
24
* Create an OctetString from a hex string
25
*/
26
OctetString::OctetString(std::string_view hex_string) {
19,341✔
27
   if(!hex_string.empty()) {
19,341✔
28
      m_data.resize(1 + hex_string.length() / 2);
15,074✔
29
      m_data.resize(hex_decode(m_data.data(), hex_string));
15,074✔
30
   }
31
}
19,341✔
32

33
/*
34
* Create an OctetString from a byte string
35
*/
36
OctetString::OctetString(const uint8_t in[], size_t n) {
2,197✔
37
   m_data.assign(in, in + n);
2,197✔
38
}
2,197✔
39

40
namespace {
41

42
uint8_t odd_parity_of(uint8_t x) {
16✔
43
   uint8_t f = x | 0x01;
16✔
44
   f ^= (f >> 4);
16✔
45
   f ^= (f >> 2);
16✔
46
   f ^= (f >> 1);
16✔
47

48
   return (x & 0xFE) ^ (f & 0x01);
16✔
49
}
50

51
}  // namespace
52

53
/*
54
* Set the parity of each key byte to odd
55
*/
56
void OctetString::set_odd_parity() {
2✔
57
   for(size_t j = 0; j != m_data.size(); ++j) {
18✔
58
      m_data[j] = odd_parity_of(m_data[j]);
16✔
59
   }
60
}
2✔
61

62
/*
63
* Hex encode an OctetString
64
*/
65
std::string OctetString::to_string() const {
108✔
66
   return hex_encode(m_data.data(), m_data.size());
108✔
67
}
68

69
/*
70
* XOR Operation for OctetStrings
71
*/
72
OctetString& OctetString::operator^=(const OctetString& k) {
1✔
73
   if(&k == this) {
1✔
74
      zeroise(m_data);
×
75
      return (*this);
×
76
   }
77
   xor_buf(m_data.data(), k.begin(), std::min(length(), k.length()));
1✔
78
   return (*this);
1✔
79
}
80

81
/*
82
* Equality Operation for OctetStrings
83
*/
84
bool operator==(const OctetString& s1, const OctetString& s2) {
129✔
85
   return (s1.bits_of() == s2.bits_of());
387✔
86
}
87

88
/*
89
* Unequality Operation for OctetStrings
90
*/
91
bool operator!=(const OctetString& s1, const OctetString& s2) {
19✔
92
   return !(s1 == s2);
19✔
93
}
94

95
/*
96
* Append Operation for OctetStrings
97
*/
98
OctetString operator+(const OctetString& k1, const OctetString& k2) {
1✔
99
   secure_vector<uint8_t> out;
1✔
100
   out += k1.bits_of();
1✔
101
   out += k2.bits_of();
1✔
102
   return OctetString(out);
1✔
103
}
1✔
104

105
/*
106
* XOR Operation for OctetStrings
107
*/
108
OctetString operator^(const OctetString& k1, const OctetString& k2) {
3✔
109
   secure_vector<uint8_t> out(std::max(k1.length(), k2.length()));
3✔
110

111
   copy_mem(out.data(), k1.begin(), k1.length());
3✔
112
   xor_buf(out.data(), k2.begin(), k2.length());
3✔
113
   return OctetString(out);
3✔
114
}
3✔
115

116
}  // namespace Botan
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

© 2025 Coveralls, Inc