• 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

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,339✔
25
   if(!hex_string.empty()) {
19,339✔
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,339✔
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
}
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
}
2✔
56

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

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

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

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

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

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

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

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