Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

skein_512.cpp
Go to the documentation of this file.
1 /*
2 * The Skein-512 hash function
3 * (C) 2009,2010,2014 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/skein_512.h>
9 #include <botan/loadstor.h>
10 #include <botan/exceptn.h>
11 #include <algorithm>
12
13 namespace Botan {
14
15 Skein_512::Skein_512(size_t arg_output_bits,
16 const std::string& arg_personalization) :
17 m_personalization(arg_personalization),
18 m_output_bits(arg_output_bits),
19 m_threefish(new Threefish_512),
20 m_T(2), m_buffer(64), m_buf_pos(0)
21 {
22 if(m_output_bits == 0 || m_output_bits % 8 != 0 || m_output_bits > 512)
23 throw Invalid_Argument("Bad output bits size for Skein-512");
24
25 initial_block();
26 }
27
28 std::string Skein_512::name() const
29 {
30 if(m_personalization != "")
31 return "Skein-512(" + std::to_string(m_output_bits) + "," +
32 m_personalization + ")";
33 return "Skein-512(" + std::to_string(m_output_bits) + ")";
34 }
35
36 HashFunction* Skein_512::clone() const
37 {
38 return new Skein_512(m_output_bits, m_personalization);
39 }
40
41 std::unique_ptr<HashFunction> Skein_512::copy_state() const
42 {
43 std::unique_ptr<Skein_512> copy(new Skein_512(m_output_bits, m_personalization));
44
45 copy->m_threefish->m_K = this->m_threefish->m_K;
46 copy->m_T = this->m_T;
47 copy->m_buffer = this->m_buffer;
48 copy->m_buf_pos = this->m_buf_pos;
49
50 // work around GCC 4.8 bug
51 return std::unique_ptr<HashFunction>(copy.release());
52 }
53
54 void Skein_512::clear()
55 {
56 zeroise(m_buffer);
57 m_buf_pos = 0;
58
59 initial_block();
60 }
61
62 void Skein_512::reset_tweak(type_code type, bool is_final)
63 {
64 m_T[0] = 0;
65
66 m_T[1] = (static_cast<uint64_t>(type) << 56) |
67 (static_cast<uint64_t>(1) << 62) |
68 (static_cast<uint64_t>(is_final) << 63);
69 }
70
71 void Skein_512::initial_block()
72 {
73 const uint8_t zeros[64] = { 0 };
74
75 m_threefish->set_key(zeros, sizeof(zeros));
76
77 // ASCII("SHA3") followed by version (0x0001) code
78 uint8_t config_str[32] = { 0x53, 0x48, 0x41, 0x33, 0x01, 0x00, 0 };
79 store_le(uint32_t(m_output_bits), config_str + 8);
80
81 reset_tweak(SKEIN_CONFIG, true);
82 ubi_512(config_str, sizeof(config_str));
83
84 if(m_personalization != "")
85 {
86 /*
87 This is a limitation of this implementation, and not of the
88 algorithm specification. Could be fixed relatively easily, but
89 doesn't seem worth the trouble.
90 */
91 if(m_personalization.length() > 64)
92 throw Invalid_Argument("Skein personalization must be less than 64 bytes");
93
94 const uint8_t* bits = cast_char_ptr_to_uint8(m_personalization.data());
95 reset_tweak(SKEIN_PERSONALIZATION, true);
96 ubi_512(bits, m_personalization.length());
97 }
98
99 reset_tweak(SKEIN_MSG, false);
100 }
101
102 void Skein_512::ubi_512(const uint8_t msg[], size_t msg_len)
103 {
104 secure_vector<uint64_t> M(8);
105
106 do
107 {
108 const size_t to_proc = std::min<size_t>(msg_len, 64);
109 m_T[0] += to_proc;
110
111 load_le(M.data(), msg, to_proc / 8);
112
113 if(to_proc % 8)
114 {
115 for(size_t j = 0; j != to_proc % 8; ++j)
116 M[to_proc/8] |= static_cast<uint64_t>(msg[8*(to_proc/8)+j]) << (8*j);
117 }
118
119 m_threefish->skein_feedfwd(M, m_T);
120
121 // clear first flag if set
122 m_T[1] &= ~(static_cast<uint64_t>(1) << 62);
123
124 msg_len -= to_proc;
125 msg += to_proc;
126 } while(msg_len);
127 }
128
129 void Skein_512::add_data(const uint8_t input[], size_t length)
130 {
131 if(length == 0)
132 return;
133
134 if(m_buf_pos)
135 {
136 buffer_insert(m_buffer, m_buf_pos, input, length);
137 if(m_buf_pos + length > 64)
138 {
139 ubi_512(m_buffer.data(), m_buffer.size());
140
141 input += (64 - m_buf_pos);
142 length -= (64 - m_buf_pos);
143 m_buf_pos = 0;
144 }
145 }
146
147 const size_t full_blocks = (length - 1) / 64;
148
149 if(full_blocks)
150 ubi_512(input, 64*full_blocks);
151
152 length -= full_blocks * 64;
153
154 buffer_insert(m_buffer, m_buf_pos, input + full_blocks * 64, length);
155 m_buf_pos += length;
156 }
157
158 void Skein_512::final_result(uint8_t out[])
159 {
160 m_T[1] |= (static_cast<uint64_t>(1) << 63); // final block flag
161
162 for(size_t i = m_buf_pos; i != m_buffer.size(); ++i)
163 m_buffer[i] = 0;
164
165 ubi_512(m_buffer.data(), m_buf_pos);
166
167 const uint8_t counter[8] = { 0 };
168
169 reset_tweak(SKEIN_OUTPUT, true);
170 ubi_512(counter, sizeof(counter));
171
172 copy_out_vec_le(out, m_output_bits / 8, m_threefish->m_K);
173
174 m_buf_pos = 0;
175 initial_block();
176 }
177
178 }
Botan::Invalid_Argument
Definition: exceptn.h:136
Botan::Skein_512::copy_state
std::unique_ptr< HashFunction > copy_state() const override
Definition: skein_512.cpp:41
Botan::Skein_512::clear
void clear() override
Definition: skein_512.cpp:54
Botan::cast_char_ptr_to_uint8
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:189
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
type
MechanismType type
Definition: p11_mechanism.cpp:44
Botan::Threefish_512
Definition: threefish_512.h:20
Botan::load_le
T load_le(const uint8_t in[], size_t off)
Definition: loadstor.h:123
Botan
Definition: alg_id.cpp:13
Botan::Skein_512::Skein_512
Skein_512(size_t output_bits=512, const std::string &personalization="")
Definition: skein_512.cpp:15
Botan::HashFunction
Definition: hash.h:20
Botan::buffer_insert
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:80
Botan::Skein_512::clone
HashFunction * clone() const override
Definition: skein_512.cpp:36
Botan::store_le
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:454
Botan::zeroise
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:160
Botan::copy_out_vec_le
void copy_out_vec_le(uint8_t out[], size_t out_bytes, const std::vector< T, Alloc > &in)
Definition: loadstor.h:694
Botan::Skein_512::name
std::string name() const override
Definition: skein_512.cpp:28