Botan 2.19.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#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(const std::string& 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
44/*
45* Set the parity of each key byte to odd
46*/
48 {
49 const uint8_t ODD_PARITY[256] = {
50 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07, 0x08, 0x08, 0x0B, 0x0B,
51 0x0D, 0x0D, 0x0E, 0x0E, 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
52 0x19, 0x19, 0x1A, 0x1A, 0x1C, 0x1C, 0x1F, 0x1F, 0x20, 0x20, 0x23, 0x23,
53 0x25, 0x25, 0x26, 0x26, 0x29, 0x29, 0x2A, 0x2A, 0x2C, 0x2C, 0x2F, 0x2F,
54 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37, 0x38, 0x38, 0x3B, 0x3B,
55 0x3D, 0x3D, 0x3E, 0x3E, 0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
56 0x49, 0x49, 0x4A, 0x4A, 0x4C, 0x4C, 0x4F, 0x4F, 0x51, 0x51, 0x52, 0x52,
57 0x54, 0x54, 0x57, 0x57, 0x58, 0x58, 0x5B, 0x5B, 0x5D, 0x5D, 0x5E, 0x5E,
58 0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67, 0x68, 0x68, 0x6B, 0x6B,
59 0x6D, 0x6D, 0x6E, 0x6E, 0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
60 0x79, 0x79, 0x7A, 0x7A, 0x7C, 0x7C, 0x7F, 0x7F, 0x80, 0x80, 0x83, 0x83,
61 0x85, 0x85, 0x86, 0x86, 0x89, 0x89, 0x8A, 0x8A, 0x8C, 0x8C, 0x8F, 0x8F,
62 0x91, 0x91, 0x92, 0x92, 0x94, 0x94, 0x97, 0x97, 0x98, 0x98, 0x9B, 0x9B,
63 0x9D, 0x9D, 0x9E, 0x9E, 0xA1, 0xA1, 0xA2, 0xA2, 0xA4, 0xA4, 0xA7, 0xA7,
64 0xA8, 0xA8, 0xAB, 0xAB, 0xAD, 0xAD, 0xAE, 0xAE, 0xB0, 0xB0, 0xB3, 0xB3,
65 0xB5, 0xB5, 0xB6, 0xB6, 0xB9, 0xB9, 0xBA, 0xBA, 0xBC, 0xBC, 0xBF, 0xBF,
66 0xC1, 0xC1, 0xC2, 0xC2, 0xC4, 0xC4, 0xC7, 0xC7, 0xC8, 0xC8, 0xCB, 0xCB,
67 0xCD, 0xCD, 0xCE, 0xCE, 0xD0, 0xD0, 0xD3, 0xD3, 0xD5, 0xD5, 0xD6, 0xD6,
68 0xD9, 0xD9, 0xDA, 0xDA, 0xDC, 0xDC, 0xDF, 0xDF, 0xE0, 0xE0, 0xE3, 0xE3,
69 0xE5, 0xE5, 0xE6, 0xE6, 0xE9, 0xE9, 0xEA, 0xEA, 0xEC, 0xEC, 0xEF, 0xEF,
70 0xF1, 0xF1, 0xF2, 0xF2, 0xF4, 0xF4, 0xF7, 0xF7, 0xF8, 0xF8, 0xFB, 0xFB,
71 0xFD, 0xFD, 0xFE, 0xFE };
72
73 for(size_t j = 0; j != m_data.size(); ++j)
74 m_data[j] = ODD_PARITY[m_data[j]];
75 }
76
77/*
78* Hex encode an OctetString
79*/
80std::string OctetString::to_string() const
81 {
82 return hex_encode(m_data.data(), m_data.size());
83 }
84
85/*
86* XOR Operation for OctetStrings
87*/
89 {
90 if(&k == this) { zeroise(m_data); return (*this); }
91 xor_buf(m_data.data(), k.begin(), std::min(length(), k.length()));
92 return (*this);
93 }
94
95/*
96* Equality Operation for OctetStrings
97*/
98bool operator==(const OctetString& s1, const OctetString& s2)
99 {
100 return (s1.bits_of() == s2.bits_of());
101 }
102
103/*
104* Unequality Operation for OctetStrings
105*/
106bool operator!=(const OctetString& s1, const OctetString& s2)
107 {
108 return !(s1 == s2);
109 }
110
111/*
112* Append Operation for OctetStrings
113*/
115 {
117 out += k1.bits_of();
118 out += k2.bits_of();
119 return OctetString(out);
120 }
121
122/*
123* XOR Operation for OctetStrings
124*/
126 {
127 secure_vector<uint8_t> out(std::max(k1.length(), k2.length()));
128
129 copy_mem(out.data(), k1.begin(), k1.length());
130 xor_buf(out.data(), k2.begin(), k2.length());
131 return OctetString(out);
132 }
133
134}
secure_vector< uint8_t > bits_of() const
Definition: symkey.h:31
void set_odd_parity()
Definition: symkey.cpp:47
OctetString(const std::string &str="")
Definition: symkey.cpp:27
OctetString & operator^=(const OctetString &other)
Definition: symkey.cpp:88
const uint8_t * begin() const
Definition: symkey.h:36
std::string to_string() const
Definition: symkey.cpp:80
size_t length() const
Definition: symkey.h:25
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:143
Definition: alg_id.cpp:13
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:117
OctetString operator^(const OctetString &k1, const OctetString &k2)
Definition: symkey.cpp:125
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:82
OID operator+(const OID &oid, uint32_t new_comp)
Definition: asn1_oid.cpp:122
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:133
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:65
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:262
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:31
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:89
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65