Changeset View
Changeset View
Standalone View
Standalone View
src/crypto/sha256.cpp
// Copyright (c) 2014 The Bitcoin Core developers | // Copyright (c) 2014 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "crypto/sha256.h" | #include "crypto/sha256.h" | ||||
#include "crypto/common.h" | #include "crypto/common.h" | ||||
#include <atomic> | #include <atomic> | ||||
#include <cassert> | #include <cassert> | ||||
#include <cstring> | #include <cstring> | ||||
#if defined(__x86_64__) || defined(__amd64__) | #if defined(__x86_64__) || defined(__amd64__) | ||||
#if defined(USE_ASM) | #if defined(USE_ASM) | ||||
#include <cpuid.h> | #include <cpuid.h> | ||||
namespace sha256_sse4 { | namespace sha256_sse4 { | ||||
void Transform(uint32_t *s, const unsigned char *chunk, size_t blocks); | void Transform(uint32_t *s, const uint8_t *chunk, size_t blocks); | ||||
} | } | ||||
#endif | #endif | ||||
#endif | #endif | ||||
// Internal implementation code. | // Internal implementation code. | ||||
namespace { | namespace { | ||||
/// Internal SHA-256 implementation. | /// Internal SHA-256 implementation. | ||||
namespace sha256 { | namespace sha256 { | ||||
Show All 35 Lines | inline void Initialize(uint32_t *s) { | ||||
s[4] = 0x510e527ful; | s[4] = 0x510e527ful; | ||||
s[5] = 0x9b05688cul; | s[5] = 0x9b05688cul; | ||||
s[6] = 0x1f83d9abul; | s[6] = 0x1f83d9abul; | ||||
s[7] = 0x5be0cd19ul; | s[7] = 0x5be0cd19ul; | ||||
} | } | ||||
/** Perform a number of SHA-256 transformations, processing 64-byte chunks. | /** Perform a number of SHA-256 transformations, processing 64-byte chunks. | ||||
*/ | */ | ||||
void Transform(uint32_t *s, const unsigned char *chunk, size_t blocks) { | void Transform(uint32_t *s, const uint8_t *chunk, size_t blocks) { | ||||
while (blocks--) { | while (blocks--) { | ||||
uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], | uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], | ||||
g = s[6], h = s[7]; | g = s[6], h = s[7]; | ||||
uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, | uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, | ||||
w14, w15; | w14, w15; | ||||
Round(a, b, c, d, e, f, g, h, 0x428a2f98, w0 = ReadBE32(chunk + 0)); | Round(a, b, c, d, e, f, g, h, 0x428a2f98, w0 = ReadBE32(chunk + 0)); | ||||
Round(h, a, b, c, d, e, f, g, 0x71374491, w1 = ReadBE32(chunk + 4)); | Round(h, a, b, c, d, e, f, g, 0x71374491, w1 = ReadBE32(chunk + 4)); | ||||
▲ Show 20 Lines • Show All 133 Lines • ▼ Show 20 Lines | void Transform(uint32_t *s, const uint8_t *chunk, size_t blocks) { | ||||
s[6] += g; | s[6] += g; | ||||
s[7] += h; | s[7] += h; | ||||
chunk += 64; | chunk += 64; | ||||
} | } | ||||
} | } | ||||
} // namespace sha256 | } // namespace sha256 | ||||
typedef void (*TransformType)(uint32_t *, const unsigned char *, size_t); | typedef void (*TransformType)(uint32_t *, const uint8_t *, size_t); | ||||
bool SelfTest(TransformType tr) { | bool SelfTest(TransformType tr) { | ||||
static const unsigned char in1[65] = {0, 0x80}; | static const uint8_t in1[65] = {0, 0x80}; | ||||
static const unsigned char in2[129] = { | static const uint8_t in2[129] = { | ||||
0, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, | 0, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, | ||||
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, | 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, | ||||
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, | 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, | ||||
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0x80, 0, 0, | 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0x80, 0, 0, | ||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||||
0, 0, 0, 0, 0, 0, 0, 0, 2, 0}; | 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}; | ||||
▲ Show 20 Lines • Show All 94 Lines • Show Last 20 Lines |