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