Botan 3.6.1
Crypto and TLS for C&
symkey.cpp
Go to the documentation of this file.
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/mem_ops.h>
12#include <botan/rng.h>
13#include <algorithm>
14
15namespace Botan {
16
17/*
18* Create an OctetString from RNG output
19*/
21 rng.random_vec(m_data, len);
22}
23
24/*
25* Create an OctetString from a hex string
26*/
27OctetString::OctetString(std::string_view hex_string) {
28 if(!hex_string.empty()) {
29 m_data.resize(1 + hex_string.length() / 2);
30 m_data.resize(hex_decode(m_data.data(), hex_string));
31 }
32}
33
34/*
35* Create an OctetString from a byte string
36*/
37OctetString::OctetString(const uint8_t in[], size_t n) {
38 m_data.assign(in, in + n);
39}
40
41namespace {
42
43uint8_t odd_parity_of(uint8_t x) {
44 uint8_t f = x | 0x01;
45 f ^= (f >> 4);
46 f ^= (f >> 2);
47 f ^= (f >> 1);
48
49 return (x & 0xFE) ^ (f & 0x01);
50}
51
52} // namespace
53
54/*
55* Set the parity of each key byte to odd
56*/
58 for(size_t j = 0; j != m_data.size(); ++j) {
59 m_data[j] = odd_parity_of(m_data[j]);
60 }
61}
62
63/*
64* Hex encode an OctetString
65*/
66std::string OctetString::to_string() const {
67 return hex_encode(m_data.data(), m_data.size());
68}
69
70/*
71* XOR Operation for OctetStrings
72*/
74 if(&k == this) {
75 zeroise(m_data);
76 return (*this);
77 }
78 xor_buf(m_data.data(), k.begin(), std::min(length(), k.length()));
79 return (*this);
80}
81
82/*
83* Equality Operation for OctetStrings
84*/
85bool operator==(const OctetString& s1, const OctetString& s2) {
86 return (s1.bits_of() == s2.bits_of());
87}
88
89/*
90* Unequality Operation for OctetStrings
91*/
92bool operator!=(const OctetString& s1, const OctetString& s2) {
93 return !(s1 == s2);
94}
95
96/*
97* Append Operation for OctetStrings
98*/
101 out += k1.bits_of();
102 out += k2.bits_of();
103 return OctetString(out);
104}
105
106/*
107* XOR Operation for OctetStrings
108*/
110 secure_vector<uint8_t> out(std::max(k1.length(), k2.length()));
111
112 copy_mem(out.data(), k1.begin(), k1.length());
113 xor_buf(out.data(), k2.begin(), k2.length());
114 return OctetString(out);
115}
116
117} // namespace Botan
secure_vector< uint8_t > bits_of() const
Definition symkey.h:36
void set_odd_parity()
Definition symkey.cpp:57
OctetString & operator^=(const OctetString &other)
Definition symkey.cpp:73
OctetString(std::string_view str="")
Definition symkey.cpp:27
const uint8_t * begin() const
Definition symkey.h:41
std::string to_string() const
Definition symkey.cpp:66
size_t length() const
Definition symkey.h:27
void random_vec(std::span< uint8_t > v)
Definition rng.h:180
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:108
OctetString operator^(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:109
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:69
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:54
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:35
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:341
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition hex.cpp:73
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146