Path: blob/master/Utilities/cmliblzma/liblzma/check/sha256.c
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file sha256.c5/// \brief SHA-2566//7// The C code is based on the public domain SHA-256 code found from8// Crypto++ Library 5.5.1 released in 2007: https://www.cryptopp.com/9// A few minor tweaks have been made in liblzma.10//11// Authors: Wei Dai12// Lasse Collin13//14///////////////////////////////////////////////////////////////////////////////1516#include "check.h"1718// Rotate a uint32_t. GCC can optimize this to a rotate instruction19// at least on x86.20static inline uint32_t21rotr_32(uint32_t num, unsigned amount)22{23return (num >> amount) | (num << (32 - amount));24}2526#define blk0(i) (W[i] = conv32be(data[i]))27#define blk2(i) (W[i & 15] += s1(W[(i - 2) & 15]) + W[(i - 7) & 15] \28+ s0(W[(i - 15) & 15]))2930#define Ch(x, y, z) (z ^ (x & (y ^ z)))31#define Maj(x, y, z) ((x & (y ^ z)) + (y & z))3233#define a(i) T[(0 - i) & 7]34#define b(i) T[(1 - i) & 7]35#define c(i) T[(2 - i) & 7]36#define d(i) T[(3 - i) & 7]37#define e(i) T[(4 - i) & 7]38#define f(i) T[(5 - i) & 7]39#define g(i) T[(6 - i) & 7]40#define h(i) T[(7 - i) & 7]4142#define R(i, j, blk) \43h(i) += S1(e(i)) + Ch(e(i), f(i), g(i)) + SHA256_K[i + j] + blk; \44d(i) += h(i); \45h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))46#define R0(i) R(i, 0, blk0(i))47#define R2(i) R(i, j, blk2(i))4849#define S0(x) rotr_32(x ^ rotr_32(x ^ rotr_32(x, 9), 11), 2)50#define S1(x) rotr_32(x ^ rotr_32(x ^ rotr_32(x, 14), 5), 6)51#define s0(x) (rotr_32(x ^ rotr_32(x, 11), 7) ^ (x >> 3))52#define s1(x) (rotr_32(x ^ rotr_32(x, 2), 17) ^ (x >> 10))535455static const uint32_t SHA256_K[64] = {560x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,570x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,580xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,590x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,600xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,610x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,620x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,630xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,640x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,650x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,660xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,670xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,680x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,690x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,700x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,710x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,72};737475static void76transform(uint32_t state[8], const uint32_t data[16])77{78uint32_t W[16];79uint32_t T[8];8081// Copy state[] to working vars.82memcpy(T, state, sizeof(T));8384// The first 16 operations unrolled85R0( 0); R0( 1); R0( 2); R0( 3);86R0( 4); R0( 5); R0( 6); R0( 7);87R0( 8); R0( 9); R0(10); R0(11);88R0(12); R0(13); R0(14); R0(15);8990// The remaining 48 operations partially unrolled91for (unsigned int j = 16; j < 64; j += 16) {92R2( 0); R2( 1); R2( 2); R2( 3);93R2( 4); R2( 5); R2( 6); R2( 7);94R2( 8); R2( 9); R2(10); R2(11);95R2(12); R2(13); R2(14); R2(15);96}9798// Add the working vars back into state[].99state[0] += a(0);100state[1] += b(0);101state[2] += c(0);102state[3] += d(0);103state[4] += e(0);104state[5] += f(0);105state[6] += g(0);106state[7] += h(0);107}108109110static void111process(lzma_check_state *check)112{113transform(check->state.sha256.state, check->buffer.u32);114return;115}116117118extern void119lzma_sha256_init(lzma_check_state *check)120{121static const uint32_t s[8] = {1220x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,1230x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,124};125126memcpy(check->state.sha256.state, s, sizeof(s));127check->state.sha256.size = 0;128129return;130}131132133extern void134lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)135{136// Copy the input data into a properly aligned temporary buffer.137// This way we can be called with arbitrarily sized buffers138// (no need to be multiple of 64 bytes), and the code works also139// on architectures that don't allow unaligned memory access.140while (size > 0) {141const size_t copy_start = check->state.sha256.size & 0x3F;142size_t copy_size = 64 - copy_start;143if (copy_size > size)144copy_size = size;145146memcpy(check->buffer.u8 + copy_start, buf, copy_size);147148buf += copy_size;149size -= copy_size;150check->state.sha256.size += copy_size;151152if ((check->state.sha256.size & 0x3F) == 0)153process(check);154}155156return;157}158159160extern void161lzma_sha256_finish(lzma_check_state *check)162{163// Add padding as described in RFC 3174 (it describes SHA-1 but164// the same padding style is used for SHA-256 too).165size_t pos = check->state.sha256.size & 0x3F;166check->buffer.u8[pos++] = 0x80;167168while (pos != 64 - 8) {169if (pos == 64) {170process(check);171pos = 0;172}173174check->buffer.u8[pos++] = 0x00;175}176177// Convert the message size from bytes to bits.178check->state.sha256.size *= 8;179180check->buffer.u64[(64 - 8) / 8] = conv64be(check->state.sha256.size);181182process(check);183184for (size_t i = 0; i < 8; ++i)185check->buffer.u32[i] = conv32be(check->state.sha256.state[i]);186187return;188}189190191