Coverage Report

Created: 2021-05-04 09:02

/src/botan/src/lib/base/symkey.cpp
Line
Count
Source (jump to first uncovered line)
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
#include <botan/rng.h>
10
#include <botan/hex.h>
11
#include <algorithm>
12
13
namespace Botan {
14
15
/*
16
* Create an OctetString from RNG output
17
*/
18
OctetString::OctetString(RandomNumberGenerator& rng,
19
                         size_t len)
20
0
   {
21
0
   rng.random_vec(m_data, len);
22
0
   }
23
24
/*
25
* Create an OctetString from a hex string
26
*/
27
OctetString::OctetString(const std::string& hex_string)
28
68.9k
   {
29
68.9k
   if(!hex_string.empty())
30
47.3k
      {
31
47.3k
      m_data.resize(1 + hex_string.length() / 2);
32
47.3k
      m_data.resize(hex_decode(m_data.data(), hex_string));
33
47.3k
      }
34
68.9k
   }
35
36
/*
37
* Create an OctetString from a byte string
38
*/
39
OctetString::OctetString(const uint8_t in[], size_t n)
40
0
   {
41
0
   m_data.assign(in, in + n);
42
0
   }
43
44
namespace {
45
46
uint8_t odd_parity_of(uint8_t x)
47
0
   {
48
0
   uint8_t f = x | 0x01;
49
0
   f ^= (f >> 4);
50
0
   f ^= (f >> 2);
51
0
   f ^= (f >> 1);
52
53
0
   return (x & 0xFE) ^ (f & 0x01);
54
0
   }
55
56
}
57
58
/*
59
* Set the parity of each key byte to odd
60
*/
61
void OctetString::set_odd_parity()
62
0
   {
63
0
   for(size_t j = 0; j != m_data.size(); ++j)
64
0
      m_data[j] = odd_parity_of(m_data[j]);
65
0
   }
66
67
/*
68
* Hex encode an OctetString
69
*/
70
std::string OctetString::to_string() const
71
0
   {
72
0
   return hex_encode(m_data.data(), m_data.size());
73
0
   }
74
75
/*
76
* XOR Operation for OctetStrings
77
*/
78
OctetString& OctetString::operator^=(const OctetString& k)
79
0
   {
80
0
   if(&k == this) { zeroise(m_data); return (*this); }
81
0
   xor_buf(m_data.data(), k.begin(), std::min(length(), k.length()));
82
0
   return (*this);
83
0
   }
84
85
/*
86
* Equality Operation for OctetStrings
87
*/
88
bool operator==(const OctetString& s1, const OctetString& s2)
89
0
   {
90
0
   return (s1.bits_of() == s2.bits_of());
91
0
   }
92
93
/*
94
* Unequality Operation for OctetStrings
95
*/
96
bool operator!=(const OctetString& s1, const OctetString& s2)
97
0
   {
98
0
   return !(s1 == s2);
99
0
   }
100
101
/*
102
* Append Operation for OctetStrings
103
*/
104
OctetString operator+(const OctetString& k1, const OctetString& k2)
105
0
   {
106
0
   secure_vector<uint8_t> out;
107
0
   out += k1.bits_of();
108
0
   out += k2.bits_of();
109
0
   return OctetString(out);
110
0
   }
111
112
/*
113
* XOR Operation for OctetStrings
114
*/
115
OctetString operator^(const OctetString& k1, const OctetString& k2)
116
0
   {
117
0
   secure_vector<uint8_t> out(std::max(k1.length(), k2.length()));
118
119
0
   copy_mem(out.data(), k1.begin(), k1.length());
120
0
   xor_buf(out.data(), k2.begin(), k2.length());
121
0
   return OctetString(out);
122
0
   }
123
124
}