/* * Copyright (C) 2006-2009 Vincent Hanquez <vincent@snarc.org> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#include <string.h>#include "crypton_sha256.h"#include "crypton_bitfn.h"#include "crypton_align.h"#ifdef WITH_X86_SHA_NI#include "crypton_cpu.h"#endifvoid crypton_sha224_init(struct sha224_ctx *ctx){ memset(ctx, 0, sizeof(*ctx)); ctx->h[0] = 0xc1059ed8; ctx->h[1] = 0x367cd507; ctx->h[2] = 0x3070dd17; ctx->h[3] = 0xf70e5939; ctx->h[4] = 0xffc00b31; ctx->h[5] = 0x68581511; ctx->h[6] = 0x64f98fa7; ctx->h[7] = 0xbefa4fa4;}void crypton_sha256_init(struct sha256_ctx *ctx){ memset(ctx, 0, sizeof(*ctx)); ctx->h[0] = 0x6a09e667; ctx->h[1] = 0xbb67ae85; ctx->h[2] = 0x3c6ef372; ctx->h[3] = 0xa54ff53a; ctx->h[4] = 0x510e527f; ctx->h[5] = 0x9b05688c; ctx->h[6] = 0x1f83d9ab; ctx->h[7] = 0x5be0cd19;}/* 232 times the cube root of the first 64 primes 2..311 */static const uint32_t k[] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};#define e0(x) (ror32(x, 2) ^ ror32(x,13) ^ ror32(x,22))#define e1(x) (ror32(x, 6) ^ ror32(x,11) ^ ror32(x,25))#define s0(x) (ror32(x, 7) ^ ror32(x,18) ^ (x >> 3))#define s1(x) (ror32(x,17) ^ ror32(x,19) ^ (x >> 10))/* The sixteen words are read out of the block rather than the block being * pointed at as though it were an array of them; see crypton_md5.c. */static void sha256_do_chunk_generic(struct sha256_ctx *ctx, const uint8_t *buf){ uint32_t a, b, c, d, e, f, g, h, t1, t2; int i; uint32_t w[64]; for (i = 0; i < 16; i++) w[i] = load_be32(buf + 4 * i); for (i = 16; i < 64; i++) w[i] = s1(w[i - 2]) + w[i - 7] + s0(w[i - 15]) + w[i - 16]; a = ctx->h[0]; b = ctx->h[1]; c = ctx->h[2]; d = ctx->h[3]; e = ctx->h[4]; f = ctx->h[5]; g = ctx->h[6]; h = ctx->h[7];#define R(a, b, c, d, e, f, g, h, k, w) \ t1 = h + e1(e) + (g ^ (e & (f ^ g))) + k + w; \ t2 = e0(a) + ((a & b) | (c & (a | b))); \ d += t1; \ h = t1 + t2; for (i = 0; i < 64; i += 8) { R(a, b, c, d, e, f, g, h, k[i + 0], w[i + 0]); R(h, a, b, c, d, e, f, g, k[i + 1], w[i + 1]); R(g, h, a, b, c, d, e, f, k[i + 2], w[i + 2]); R(f, g, h, a, b, c, d, e, k[i + 3], w[i + 3]); R(e, f, g, h, a, b, c, d, k[i + 4], w[i + 4]); R(d, e, f, g, h, a, b, c, k[i + 5], w[i + 5]); R(c, d, e, f, g, h, a, b, k[i + 6], w[i + 6]); R(b, c, d, e, f, g, h, a, k[i + 7], w[i + 7]); }#undef R ctx->h[0] += a; ctx->h[1] += b; ctx->h[2] += c; ctx->h[3] += d; ctx->h[4] += e; ctx->h[5] += f; ctx->h[6] += g; ctx->h[7] += h;}#ifdef WITH_ARMV8_SHA2/* * AArch64 can do four rounds at a time with the SHA-2 instructions; see * sha256_armv8.c. They are optional in ARMv8.0, so ask before using them. * Two threads racing to answer here both write the same value. */extern void crypton_sha256_armv8_do_chunk(uint32_t state[8], const uint8_t buf[64]);extern int crypton_sha256_armv8_available(void);/* Resolved before there is a second thread; see the constructor in * cbits/crypton_aes.c for why. The test below then only ever reads. */static int sha256_use_armv8 = -1;__attribute__((constructor))static void sha256_armv8_ctor(void){ sha256_use_armv8 = crypton_sha256_armv8_available();}#endif#if (defined(WITH_ARMV8_SHA256_ASM) && defined(WITH_ARMV8_SHA2)) \ || defined(WITH_X86_SHA256_ASM)/* * SHA-256 from CRYPTOGAMS, in cbits/asm/sha256-armv8-*.S and * cbits/asm/sha256-x86_64-*.S, which take any number of blocks at once and * schedule the instructions across them -- which is where they are ahead * of the intrinsics above, the instructions being the same ones. Each * picks its own path from the word the processor was asked about, so the * answer to the runtime check goes there rather than into a branch here. */#define SHA256_ASM 1#include "crypton_cpu.h"extern void crypton_sha256_asm_block_data_order(uint32_t state[8], const void *data, size_t blocks);#ifdef WITH_ARMV8_SHA256_ASM/* The assembly picks its path from crypton_armcap_P, so the answer to the * runtime check has to reach that word rather than the flag above. It is * set here, before there is a second thread, for the reason the constructor * in cbits/crypton_aes.c gives. * * This ran from sha256_asm_ready below until 2.1.3, guarded by the flag * still being unresolved -- which stopped happening when the constructor * above was added, so the bit was never set and the assembly took its * generic path. SHA-256 was 5.7 times slower on an Apple M4 for it. */__attribute__((constructor))static void sha256_armcap_ctor(void){ if (crypton_sha256_armv8_available()) crypton_armcap_P |= CRYPTON_ARMCAP_SHA256;}#endifstatic void sha256_asm_ready(void){#ifndef WITH_ARMV8_SHA256_ASM crypton_x86_ia32cap_resolve();#endif}#endifstatic void sha256_do_chunk(struct sha256_ctx *ctx, const uint8_t *buf){#ifdef SHA256_ASM sha256_asm_ready(); crypton_sha256_asm_block_data_order(ctx->h, buf, 1); return;#endif#if defined(WITH_ARMV8_SHA2) && !defined(SHA256_ASM) if (sha256_use_armv8 < 0) sha256_use_armv8 = crypton_sha256_armv8_available(); if (sha256_use_armv8) { crypton_sha256_armv8_do_chunk(ctx->h, buf); return; }#endif sha256_do_chunk_generic(ctx, buf);}void crypton_sha224_update(struct sha224_ctx *ctx, const uint8_t *data, uint32_t len){ return crypton_sha256_update(ctx, data, len);}void crypton_sha256_update(struct sha256_ctx *ctx, const uint8_t *data, uint32_t len){ uint32_t index, to_fill; /* check for partial buffer */ index = (uint32_t) (ctx->sz & 0x3f); to_fill = 64 - index; ctx->sz += len; /* process partial buffer if there's enough data to make a block */ if (index && len >= to_fill) { memcpy(ctx->buf + index, data, to_fill); sha256_do_chunk(ctx, ctx->buf); len -= to_fill; data += to_fill; index = 0; }#ifdef SHA256_ASM /* the assembly reads the message a byte at a time as far as the * machine is concerned, so it wants no alignment and no copy, and * it takes the whole run of blocks in one call */ if (len >= 64) { size_t blocks = len / 64; sha256_asm_ready(); crypton_sha256_asm_block_data_order(ctx->h, data, blocks); data += blocks * 64; len -= (uint32_t) blocks * 64; }#else /* No trampoline: load_be32 does not ask for a boundary. */ for (; len >= 64; len -= 64, data += 64) sha256_do_chunk(ctx, data);#endif /* append data into buf */ if (len) memcpy(ctx->buf + index, data, len);}void crypton_sha224_finalize(struct sha224_ctx *ctx, uint8_t *out){ uint8_t intermediate[SHA256_DIGEST_SIZE]; crypton_sha256_finalize(ctx, intermediate); memcpy(out, intermediate, SHA224_DIGEST_SIZE);}void crypton_sha224_finalize_prefix(struct sha224_ctx *ctx, const uint8_t *data, uint32_t len, uint32_t n, uint8_t *out){ uint8_t intermediate[SHA256_DIGEST_SIZE]; crypton_sha256_finalize_prefix(ctx, data, len, n, intermediate); memcpy(out, intermediate, SHA224_DIGEST_SIZE);}void crypton_sha256_finalize(struct sha256_ctx *ctx, uint8_t *out){ static uint8_t padding[64] = { 0x80, }; uint64_t bits; uint32_t i, index, padlen; /* cpu -> big endian */ bits = cpu_to_be64(ctx->sz << 3); /* pad out to 56 */ index = (uint32_t) (ctx->sz & 0x3f); padlen = (index < 56) ? (56 - index) : ((64 + 56) - index); crypton_sha256_update(ctx, padding, padlen); /* append length */ crypton_sha256_update(ctx, (uint8_t *) &bits, sizeof(bits)); /* store to digest */ for (i = 0; i < 8; i++) store_be32(out+4*i, ctx->h[i]);}#define HASHED(m) SHA256_##m#define HASHED_LOWER(m) sha256_##m#define CRYPTON_HASHED(m) crypton_sha256_##m#define SHA256_BLOCK_SIZE 64#define SHA256_BITS_ELEMS 1static inline uint32_t crypton_sha256_get_index(const struct sha256_ctx *ctx){ return (uint32_t) (ctx->sz & 0x3f);}static inline void crypton_sha256_incr_sz(struct sha256_ctx *ctx, uint64_t *bits, uint32_t n){ ctx->sz += n; *bits = cpu_to_be64(ctx->sz << 3);}static inline void crypton_sha256_select_digest(const struct sha256_ctx *ctx, uint8_t *out, uint32_t out_mask){ uint32_t i; for (i = 0; i < 8; i++) xor_be32(out+4*i, ctx->h[i] & out_mask);}#include <crypton_hash_prefix.c>