|
| 1 | +// Copyright (c) 2020 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +// Based on https://github.com/mjosaarinen/tiny_sha3/blob/master/sha3.c |
| 6 | +// by Markku-Juhani O. Saarinen <[email protected]> |
| 7 | + |
| 8 | +#include <crypto/sha3.h> |
| 9 | +#include <crypto/common.h> |
| 10 | +#include <span.h> |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <array> // For std::begin and std::end. |
| 14 | + |
| 15 | +#include <stdint.h> |
| 16 | + |
| 17 | +// Internal implementation code. |
| 18 | +namespace |
| 19 | +{ |
| 20 | +uint64_t Rotl(uint64_t x, int n) { return (x << n) | (x >> (64 - n)); } |
| 21 | +} // namespace |
| 22 | + |
| 23 | +void KeccakF(uint64_t (&st)[25]) |
| 24 | +{ |
| 25 | + static constexpr uint64_t RNDC[24] = { |
| 26 | + 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000, |
| 27 | + 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009, |
| 28 | + 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a, |
| 29 | + 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003, |
| 30 | + 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a, |
| 31 | + 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008 |
| 32 | + }; |
| 33 | + static constexpr int ROTC[24] = { |
| 34 | + 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, |
| 35 | + 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44 |
| 36 | + }; |
| 37 | + static constexpr int PILN[24] = { |
| 38 | + 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, |
| 39 | + 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1 |
| 40 | + }; |
| 41 | + static constexpr int ROUNDS = 24; |
| 42 | + |
| 43 | + for (int round = 0; round < ROUNDS; ++round) { |
| 44 | + uint64_t bc[5], t; |
| 45 | + |
| 46 | + // Theta |
| 47 | + for (int i = 0; i < 5; i++) { |
| 48 | + bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20]; |
| 49 | + } |
| 50 | + |
| 51 | + for (int i = 0; i < 5; i++) { |
| 52 | + t = bc[(i + 4) % 5] ^ Rotl(bc[(i + 1) % 5], 1); |
| 53 | + for (int j = 0; j < 25; j += 5) st[j + i] ^= t; |
| 54 | + } |
| 55 | + |
| 56 | + // Rho Pi |
| 57 | + t = st[1]; |
| 58 | + for (int i = 0; i < 24; i++) { |
| 59 | + int j = PILN[i]; |
| 60 | + bc[0] = st[j]; |
| 61 | + st[j] = Rotl(t, ROTC[i]); |
| 62 | + t = bc[0]; |
| 63 | + } |
| 64 | + |
| 65 | + // Chi |
| 66 | + for (int j = 0; j < 25; j += 5) { |
| 67 | + for (int i = 0; i < 5; i++) bc[i] = st[j + i]; |
| 68 | + for (int i = 0; i < 5; i++) { |
| 69 | + st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5]; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Iota |
| 74 | + st[0] ^= RNDC[round]; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +SHA3_256& SHA3_256::Write(Span<const unsigned char> data) |
| 79 | +{ |
| 80 | + if (m_bufsize && m_bufsize + data.size() >= sizeof(m_buffer)) { |
| 81 | + // Fill the buffer and process it. |
| 82 | + std::copy(data.begin(), data.begin() + sizeof(m_buffer) - m_bufsize, m_buffer + m_bufsize); |
| 83 | + data = data.subspan(sizeof(m_buffer) - m_bufsize); |
| 84 | + m_state[m_pos++] ^= ReadLE64(m_buffer); |
| 85 | + m_bufsize = 0; |
| 86 | + if (m_pos == RATE_BUFFERS) { |
| 87 | + KeccakF(m_state); |
| 88 | + m_pos = 0; |
| 89 | + } |
| 90 | + } |
| 91 | + while (data.size() >= sizeof(m_buffer)) { |
| 92 | + // Process chunks directly from the buffer. |
| 93 | + m_state[m_pos++] ^= ReadLE64(data.data()); |
| 94 | + data = data.subspan(8); |
| 95 | + if (m_pos == RATE_BUFFERS) { |
| 96 | + KeccakF(m_state); |
| 97 | + m_pos = 0; |
| 98 | + } |
| 99 | + } |
| 100 | + if (data.size()) { |
| 101 | + // Keep the remainder in the buffer. |
| 102 | + std::copy(data.begin(), data.end(), m_buffer + m_bufsize); |
| 103 | + m_bufsize += data.size(); |
| 104 | + } |
| 105 | + return *this; |
| 106 | +} |
| 107 | + |
| 108 | +SHA3_256& SHA3_256::Finalize(Span<unsigned char> output) |
| 109 | +{ |
| 110 | + assert(output.size() == OUTPUT_SIZE); |
| 111 | + std::fill(m_buffer + m_bufsize, m_buffer + sizeof(m_buffer), 0); |
| 112 | + m_buffer[m_bufsize] ^= 0x06; |
| 113 | + m_state[m_pos] ^= ReadLE64(m_buffer); |
| 114 | + m_state[RATE_BUFFERS - 1] ^= 0x8000000000000000; |
| 115 | + KeccakF(m_state); |
| 116 | + for (unsigned i = 0; i < 4; ++i) { |
| 117 | + WriteLE64(output.data() + 8 * i, m_state[i]); |
| 118 | + } |
| 119 | + return *this; |
| 120 | +} |
| 121 | + |
| 122 | +SHA3_256& SHA3_256::Reset() |
| 123 | +{ |
| 124 | + m_bufsize = 0; |
| 125 | + m_pos = 0; |
| 126 | + std::fill(std::begin(m_state), std::end(m_state), 0); |
| 127 | + return *this; |
| 128 | +} |
0 commit comments