Botan 2.19.5 Crypto and TLS for C&

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/exceptn.h>
10#include <botan/sha3.h>
11#include <botan/loadstor.h>
12
13 namespace Botan {
14
15 SHAKE_128_Cipher::SHAKE_128_Cipher() :
16 m_buf_pos(0)
17 {}
18
19 void SHAKE_128_Cipher::cipher(const uint8_t in[], uint8_t out[], size_t length)
20 {
21 const size_t SHAKE_128_BYTERATE = (1600-256)/8;
22
23 verify_key_set(m_state.empty() == false);
24
25 while(length >= SHAKE_128_BYTERATE - m_buf_pos)
26 {
27 xor_buf(out, in, &m_buffer[m_buf_pos], SHAKE_128_BYTERATE - m_buf_pos);
28 length -= (SHAKE_128_BYTERATE - m_buf_pos);
29 in += (SHAKE_128_BYTERATE - m_buf_pos);
30 out += (SHAKE_128_BYTERATE - m_buf_pos);
31
32 SHA_3::permute(m_state.data());
33 copy_out_le(m_buffer.data(), SHAKE_128_BYTERATE, m_state.data());
34
35 m_buf_pos = 0;
36 }
37 xor_buf(out, in, &m_buffer[m_buf_pos], length);
38 m_buf_pos += length;
39 }
40
41 void SHAKE_128_Cipher::key_schedule(const uint8_t key[], size_t length)
42 {
43 const size_t SHAKE_128_BITRATE = (1600-256);
44 m_state.resize(25);
45 m_buffer.resize(SHAKE_128_BITRATE/8);
46 zeroise(m_state);
47 m_buf_pos = 0;
48
49 const size_t S_pos = SHA_3::absorb(SHAKE_128_BITRATE, m_state, 0, key, length);
50 SHA_3::finish(SHAKE_128_BITRATE, m_state, S_pos, 0x1F, 0x80);
51 copy_out_le(m_buffer.data(), m_buffer.size(), m_state.data());
52 }
53
54 void SHAKE_128_Cipher::clear()
55 {
56 zap(m_state);
57 zap(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
76 Key_Length_Specification SHAKE_128_Cipher::key_spec() const
77 {
78 return Key_Length_Specification(1, 160);
79 }
80
81std::string SHAKE_128_Cipher::name() const
82 {
83 return "SHAKE-128";
84 }
85
86 StreamCipher* SHAKE_128_Cipher::clone() const
87 {
88 return new SHAKE_128_Cipher;
89 }
90
91}
Botan::Invalid_IV_Length
Definition: exceptn.h:162
Botan::Key_Length_Specification
Definition: sym_algo.h:20
Botan::Not_Implemented
Definition: exceptn.h:356
Botan::SHAKE_128_Cipher::clear
void clear() override
Definition: shake_cipher.cpp:54
Botan::SHAKE_128_Cipher::set_iv
void set_iv(const uint8_t iv[], size_t iv_len) override
Definition: shake_cipher.cpp:61
Botan::SHAKE_128_Cipher::clone
StreamCipher * clone() const override
Definition: shake_cipher.cpp:86
Botan::SHAKE_128_Cipher::cipher
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
Definition: shake_cipher.cpp:19
Botan::SHAKE_128_Cipher::seek
void seek(uint64_t offset) override
Definition: shake_cipher.cpp:71
Botan::SHAKE_128_Cipher::SHAKE_128_Cipher
SHAKE_128_Cipher()
Definition: shake_cipher.cpp:15
Botan::SHAKE_128_Cipher::key_spec
Key_Length_Specification key_spec() const override
Definition: shake_cipher.cpp:76
Botan::SHAKE_128_Cipher::name
std::string name() const override
Definition: shake_cipher.cpp:81
Botan::SHA_3::permute
static void permute(uint64_t A[25])
Definition: sha3.cpp:17
Botan::SHA_3::finish
static void finish(size_t bitrate, secure_vector< uint64_t > &S, size_t S_pos, uint8_t init_pad, uint8_t fini_pad)
Definition: sha3.cpp:94
Botan::SHA_3::absorb
static size_t absorb(size_t bitrate, secure_vector< uint64_t > &S, size_t S_pos, const uint8_t input[], size_t length)
Definition: sha3.cpp:47
Botan::StreamCipher
Definition: stream_cipher.h:22
Botan::SymmetricAlgorithm::verify_key_set
void verify_key_set(bool cond) const
Definition: sym_algo.h:171
Botan
Definition: alg_id.cpp:13
Botan::zeroise
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:114
Botan::copy_out_le
void copy_out_le(uint8_t out[], size_t out_bytes, const T in[])
Definition: loadstor.h:679
Botan::zap
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:124
Botan::xor_buf
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:262