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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

94.74
/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) { rng.random_vec(m_data, len); }
11✔
20

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

31
/*
32
* Create an OctetString from a byte string
33
*/
34
OctetString::OctetString(const uint8_t in[], size_t n) { m_data.assign(in, in + n); }
4,394✔
35

36
namespace {
37

38
uint8_t odd_parity_of(uint8_t x) {
16✔
39
   uint8_t f = x | 0x01;
16✔
40
   f ^= (f >> 4);
16✔
41
   f ^= (f >> 2);
16✔
42
   f ^= (f >> 1);
16✔
43

44
   return (x & 0xFE) ^ (f & 0x01);
16✔
45
}
46

47
}  // namespace
48

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

58
/*
59
* Hex encode an OctetString
60
*/
61
std::string OctetString::to_string() const { return hex_encode(m_data.data(), m_data.size()); }
108✔
62

63
/*
64
* XOR Operation for OctetStrings
65
*/
66
OctetString& OctetString::operator^=(const OctetString& k) {
1✔
67
   if(&k == this) {
1✔
68
      zeroise(m_data);
×
69
      return (*this);
×
70
   }
71
   xor_buf(m_data.data(), k.begin(), std::min(length(), k.length()));
1✔
72
   return (*this);
1✔
73
}
74

75
/*
76
* Equality Operation for OctetStrings
77
*/
78
bool operator==(const OctetString& s1, const OctetString& s2) { return (s1.bits_of() == s2.bits_of()); }
384✔
79

80
/*
81
* Unequality Operation for OctetStrings
82
*/
83
bool operator!=(const OctetString& s1, const OctetString& s2) { return !(s1 == s2); }
18✔
84

85
/*
86
* Append Operation for OctetStrings
87
*/
88
OctetString operator+(const OctetString& k1, const OctetString& k2) {
1✔
89
   secure_vector<uint8_t> out;
1✔
90
   out += k1.bits_of();
1✔
91
   out += k2.bits_of();
1✔
92
   return OctetString(out);
1✔
93
}
1✔
94

95
/*
96
* XOR Operation for OctetStrings
97
*/
98
OctetString operator^(const OctetString& k1, const OctetString& k2) {
3✔
99
   secure_vector<uint8_t> out(std::max(k1.length(), k2.length()));
3✔
100

101
   copy_mem(out.data(), k1.begin(), k1.length());
3✔
102
   xor_buf(out.data(), k2.begin(), k2.length());
3✔
103
   return OctetString(out);
3✔
104
}
3✔
105

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