8 #include <botan/skein_512.h>
9 #include <botan/parsing.h>
10 #include <botan/exceptn.h>
16 const std::string& arg_personalization) :
17 m_personalization(arg_personalization),
18 m_output_bits(arg_output_bits),
20 m_T(2), m_buffer(64), m_buf_pos(0)
22 if(m_output_bits == 0 || m_output_bits % 8 != 0 || m_output_bits > 512)
30 if(m_personalization !=
"")
32 m_personalization +
")";
38 return new Skein_512(m_output_bits, m_personalization);
49 void Skein_512::reset_tweak(type_code
type,
bool is_final)
53 m_T[1] = (
static_cast<uint64_t
>(
type) << 56) |
54 (
static_cast<uint64_t
>(1) << 62) |
55 (
static_cast<uint64_t
>(is_final) << 63);
58 void Skein_512::initial_block()
60 const uint8_t zeros[64] = { 0 };
62 m_threefish->set_key(zeros,
sizeof(zeros));
65 uint8_t config_str[32] = { 0x53, 0x48, 0x41, 0x33, 0x01, 0x00, 0 };
66 store_le(uint32_t(m_output_bits), config_str + 8);
68 reset_tweak(SKEIN_CONFIG,
true);
69 ubi_512(config_str,
sizeof(config_str));
71 if(m_personalization !=
"")
78 if(m_personalization.length() > 64)
79 throw Invalid_Argument(
"Skein personalization must be less than 64 bytes");
81 const uint8_t* bits =
reinterpret_cast<const uint8_t*
>(m_personalization.data());
82 reset_tweak(SKEIN_PERSONALIZATION,
true);
83 ubi_512(bits, m_personalization.length());
86 reset_tweak(SKEIN_MSG,
false);
89 void Skein_512::ubi_512(
const uint8_t msg[],
size_t msg_len)
91 secure_vector<uint64_t> M(8);
95 const size_t to_proc = std::min<size_t>(msg_len, 64);
98 load_le(M.data(), msg, to_proc / 8);
102 for(
size_t j = 0; j != to_proc % 8; ++j)
103 M[to_proc/8] |= static_cast<uint64_t>(msg[8*(to_proc/8)+j]) << (8*j);
106 m_threefish->skein_feedfwd(M, m_T);
109 m_T[1] &= ~(
static_cast<uint64_t
>(1) << 62);
116 void Skein_512::add_data(
const uint8_t input[],
size_t length)
124 if(m_buf_pos + length > 64)
126 ubi_512(m_buffer.data(), m_buffer.size());
128 input += (64 - m_buf_pos);
129 length -= (64 - m_buf_pos);
134 const size_t full_blocks = (length - 1) / 64;
137 ubi_512(input, 64*full_blocks);
139 length -= full_blocks * 64;
141 buffer_insert(m_buffer, m_buf_pos, input + full_blocks * 64, length);
145 void Skein_512::final_result(uint8_t out[])
147 m_T[1] |= (
static_cast<uint64_t
>(1) << 63);
149 for(
size_t i = m_buf_pos; i != m_buffer.size(); ++i)
152 ubi_512(m_buffer.data(), m_buf_pos);
154 const uint8_t counter[8] = { 0 };
156 reset_tweak(SKEIN_OUTPUT,
true);
157 ubi_512(counter,
sizeof(counter));
std::string to_string(const BER_Object &obj)
T load_le(const uint8_t in[], size_t off)
Skein_512(size_t output_bits=512, const std::string &personalization="")
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
HashFunction * clone() const override
void store_le(uint16_t in, uint8_t out[2])
void zeroise(std::vector< T, Alloc > &vec)
void copy_out_vec_le(uint8_t out[], size_t out_bytes, const std::vector< T, Alloc > &in)
std::string name() const override