Botan 3.0.0
Crypto and TLS for C&
sha3.h
Go to the documentation of this file.
1/*
2* SHA-3
3* (C) 2010,2016 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_SHA3_H_
9#define BOTAN_SHA3_H_
10
11#include <botan/hash.h>
12#include <botan/secmem.h>
13#include <string>
14
15namespace Botan {
16
17/**
18* SHA-3
19*/
20class SHA_3 : public HashFunction
21 {
22 public:
23
24 /**
25 * @param output_bits the size of the hash output; must be one of
26 * 224, 256, 384, or 512
27 */
28 explicit SHA_3(size_t output_bits);
29
30 size_t hash_block_size() const override { return m_bitrate / 8; }
31 size_t output_length() const override { return m_output_bits / 8; }
32
33 std::unique_ptr<HashFunction> new_object() const override;
34 std::unique_ptr<HashFunction> copy_state() const override;
35 std::string name() const override;
36 void clear() override;
37 std::string provider() const override;
38
39 // Static functions for internal usage
40
41 /**
42 * Absorb data into the provided state
43 * @param bitrate the bitrate to absorb into the sponge
44 * @param S the sponge state
45 * @param S_pos where to begin absorbing into S
46 * @param input the input data
47 * @param length size of input in bytes
48 */
49 static size_t absorb(size_t bitrate,
50 secure_vector<uint64_t>& S, size_t S_pos,
51 const uint8_t input[], size_t length);
52
53 /**
54 * Add final padding and permute. The padding is assumed to be
55 * init_pad || 00... || fini_pad
56 *
57 * @param bitrate the bitrate to absorb into the sponge
58 * @param S the sponge state
59 * @param S_pos where to begin absorbing into S
60 * @param init_pad the leading pad bits
61 * @param fini_pad the final pad bits
62 */
63 static void finish(size_t bitrate,
64 secure_vector<uint64_t>& S, size_t S_pos,
65 uint8_t init_pad, uint8_t fini_pad);
66
67 /**
68 * Expand from provided state
69 * @param bitrate sponge parameter
70 * @param S the state
71 * @param output the output buffer
72 * @param output_length the size of output in bytes
73 */
74 static void expand(size_t bitrate,
76 uint8_t output[], size_t output_length);
77
78 /**
79 * The bare Keccak-1600 permutation
80 */
81 static void permute(uint64_t A[25]);
82
83 private:
84 void add_data(const uint8_t input[], size_t length) override;
85 void final_result(uint8_t out[]) override;
86
87#if defined(BOTAN_HAS_SHA3_BMI2)
88 static void permute_bmi2(uint64_t A[25]);
89#endif
90
91 size_t m_output_bits, m_bitrate;
93 size_t m_S_pos;
94 };
95
96/**
97* SHA-3-224
98*/
99class SHA_3_224 final : public SHA_3
100 {
101 public:
102 SHA_3_224() : SHA_3(224) {}
103 };
104
105/**
106* SHA-3-256
107*/
108class SHA_3_256 final : public SHA_3
109 {
110 public:
111 SHA_3_256() : SHA_3(256) {}
112 };
113
114/**
115* SHA-3-384
116*/
117class SHA_3_384 final : public SHA_3
118 {
119 public:
120 SHA_3_384() : SHA_3(384) {}
121 };
122
123/**
124* SHA-3-512
125*/
126class SHA_3_512 final : public SHA_3
127 {
128 public:
129 SHA_3_512() : SHA_3(512) {}
130 };
131
132}
133
134#endif
static void permute(uint64_t A[25])
Definition: sha3.cpp:18
void clear() override
Definition: sha3.cpp:173
size_t hash_block_size() const override
Definition: sha3.h:30
std::unique_ptr< HashFunction > new_object() const override
Definition: sha3.cpp:168
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:95
std::string provider() const override
Definition: sha3.cpp:151
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:48
static void expand(size_t bitrate, secure_vector< uint64_t > &S, uint8_t output[], size_t output_length)
Definition: sha3.cpp:107
std::string name() const override
Definition: sha3.cpp:146
size_t output_length() const override
Definition: sha3.h:31
std::unique_ptr< HashFunction > copy_state() const override
Definition: sha3.cpp:163
int(* final)(unsigned char *, CTX *)
Definition: alg_id.cpp:12
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64