Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

shake_cipher.cpp
Go to the documentation of this file.
1 /*
2 * SHAKE-128
3 * (C) 2016 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/shake_cipher.h>
9 #include <botan/sha3.h>
10 #include <botan/loadstor.h>
11
12 namespace Botan {
13
14 SHAKE_128_Cipher::SHAKE_128_Cipher() :
15 m_state(25),
16 m_buffer((1600 - 256) / 8),
17 m_buf_pos(0)
18 {}
19
20 void SHAKE_128_Cipher::cipher(const uint8_t in[], uint8_t out[], size_t length)
21 {
22 while(length >= m_buffer.size() - m_buf_pos)
23 {
24 xor_buf(out, in, &m_buffer[m_buf_pos], m_buffer.size() - m_buf_pos);
25 length -= (m_buffer.size() - m_buf_pos);
26 in += (m_buffer.size() - m_buf_pos);
27 out += (m_buffer.size() - m_buf_pos);
28
29 SHA_3::permute(m_state.data());
30 copy_out_le(m_buffer.data(), m_buffer.size(), m_state.data());
31
32 m_buf_pos = 0;
33 }
34 xor_buf(out, in, &m_buffer[m_buf_pos], length);
35 m_buf_pos += length;
36 }
37
38 void SHAKE_128_Cipher::key_schedule(const uint8_t key[], size_t length)
39 {
40 zeroise(m_state);
41
42 for(size_t i = 0; i < length/8; ++i)
43 {
44 m_state[i] ^= load_le<uint64_t>(key, i);
45 }
46
47 m_state[length/8] ^= 0x000000000000001F;
48 m_state[20] ^= 0x8000000000000000;
49
50 SHA_3::permute(m_state.data());
51 copy_out_le(m_buffer.data(), m_buffer.size(), m_state.data());
52 }
53
54 void SHAKE_128_Cipher::clear()
55 {
56 zeroise(m_state);
57 zeroise(m_buffer);
58 m_buf_pos = 0;
59 }
60
61 void SHAKE_128_Cipher::set_iv(const uint8_t[], size_t length)
62 {
63 /*
64 * This could be supported in some way (say, by treating iv as
65 * a prefix or suffix of the key).
66 */
67 if(length != 0)
68 throw Invalid_IV_Length(name(), length);
69 }
70
71 void SHAKE_128_Cipher::seek(uint64_t)
72 {
73 throw Not_Implemented("SHAKE_128_Cipher::seek");
74 }
75 }
Botan::xor_buf
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:115
Botan::SHAKE_128_Cipher::cipher
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
Definition: shake_cipher.cpp:20
Botan::copy_out_le
void copy_out_le(uint8_t out[], size_t out_bytes, const T in[])
Definition: loadstor.h:682
Botan::Invalid_IV_Length
Definition: exceptn.h:109
Botan::SHAKE_128_Cipher::seek
void seek(uint64_t offset) override
Definition: shake_cipher.cpp:71
Botan::SHAKE_128_Cipher::set_iv
void set_iv(const uint8_t iv[], size_t iv_len) override
Definition: shake_cipher.cpp:61
Botan::load_le< uint64_t >
uint64_t load_le< uint64_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:243
Botan
Definition: alg_id.cpp:13
Botan::SHAKE_128_Cipher::clear
void clear() override
Definition: shake_cipher.cpp:54
Botan::SHA_3::permute
static void permute(uint64_t A[25])
Definition: sha3.cpp:15
Botan::SHAKE_128_Cipher::SHAKE_128_Cipher
SHAKE_128_Cipher()
Definition: shake_cipher.cpp:14
Botan::SHAKE_128_Cipher::name
std::string name() const override
Definition: shake_cipher.h:51
Botan::Not_Implemented
Definition: exceptn.h:244
Botan::zeroise
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:211