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