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