/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "Licence").
 * You may not use this file except in compliance with the Licence.
 *
 * You can obtain a copy of the licence at RISC OS path @.^.LICENCE
 * or  http://www.riscosdev.com/lanman98/LICENCE.CDDL
 * See the Licence for the specific language governing permissions
 * and limitations under the Licence.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the Licence file. If applicable, add the
 * following below this CDDL HEADER, with the fields enclosed by
 * brackets "[]" replaced with your own identifying information:
 * Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 *   Copyright 2026 RISC OS Developments.  All rights reserved.
 *   Use is subject to license terms.
 */

/*
    SHA-512.

    SMB 3.1.1 needs it for the pre-authentication integrity hash: a running
    digest over the negotiate and session setup messages, which then feeds
    the key derivation, so that a listener cannot alter what was agreed
    without the keys coming out different at both ends.

    The same shape as the SHA-256 beside it, in 64 bit words: a 128 byte
    block, eighty rounds, and the message length counted in bits into the
    last sixteen bytes.  A 32 bit compiler does the 64 bit arithmetic in
    pairs of registers, which is slower than the 32 bit hash and is done
    perhaps a dozen times in the life of a connection.
*/

#include <string.h>
#include "sha512.h"

/* First 64 bits of the fractional part of the cube root of each of the
   first eighty primes */
static const sha512_uint64 K[80] =
{
    0x428A2F98D728AE22ULL, 0x7137449123EF65CDULL,
    0xB5C0FBCFEC4D3B2FULL, 0xE9B5DBA58189DBBCULL,
    0x3956C25BF348B538ULL, 0x59F111F1B605D019ULL,
    0x923F82A4AF194F9BULL, 0xAB1C5ED5DA6D8118ULL,
    0xD807AA98A3030242ULL, 0x12835B0145706FBEULL,
    0x243185BE4EE4B28CULL, 0x550C7DC3D5FFB4E2ULL,
    0x72BE5D74F27B896FULL, 0x80DEB1FE3B1696B1ULL,
    0x9BDC06A725C71235ULL, 0xC19BF174CF692694ULL,
    0xE49B69C19EF14AD2ULL, 0xEFBE4786384F25E3ULL,
    0x0FC19DC68B8CD5B5ULL, 0x240CA1CC77AC9C65ULL,
    0x2DE92C6F592B0275ULL, 0x4A7484AA6EA6E483ULL,
    0x5CB0A9DCBD41FBD4ULL, 0x76F988DA831153B5ULL,
    0x983E5152EE66DFABULL, 0xA831C66D2DB43210ULL,
    0xB00327C898FB213FULL, 0xBF597FC7BEEF0EE4ULL,
    0xC6E00BF33DA88FC2ULL, 0xD5A79147930AA725ULL,
    0x06CA6351E003826FULL, 0x142929670A0E6E70ULL,
    0x27B70A8546D22FFCULL, 0x2E1B21385C26C926ULL,
    0x4D2C6DFC5AC42AEDULL, 0x53380D139D95B3DFULL,
    0x650A73548BAF63DEULL, 0x766A0ABB3C77B2A8ULL,
    0x81C2C92E47EDAEE6ULL, 0x92722C851482353BULL,
    0xA2BFE8A14CF10364ULL, 0xA81A664BBC423001ULL,
    0xC24B8B70D0F89791ULL, 0xC76C51A30654BE30ULL,
    0xD192E819D6EF5218ULL, 0xD69906245565A910ULL,
    0xF40E35855771202AULL, 0x106AA07032BBD1B8ULL,
    0x19A4C116B8D2D0C8ULL, 0x1E376C085141AB53ULL,
    0x2748774CDF8EEB99ULL, 0x34B0BCB5E19B48A8ULL,
    0x391C0CB3C5C95A63ULL, 0x4ED8AA4AE3418ACBULL,
    0x5B9CCA4F7763E373ULL, 0x682E6FF3D6B2B8A3ULL,
    0x748F82EE5DEFB2FCULL, 0x78A5636F43172F60ULL,
    0x84C87814A1F0AB72ULL, 0x8CC702081A6439ECULL,
    0x90BEFFFA23631E28ULL, 0xA4506CEBDE82BDE9ULL,
    0xBEF9A3F7B2C67915ULL, 0xC67178F2E372532BULL,
    0xCA273ECEEA26619CULL, 0xD186B8C721C0C207ULL,
    0xEADA7DD6CDE0EB1EULL, 0xF57D4F7FEE6ED178ULL,
    0x06F067AA72176FBAULL, 0x0A637DC5A2C898A6ULL,
    0x113F9804BEF90DAEULL, 0x1B710B35131C471BULL,
    0x28DB77F523047D84ULL, 0x32CAAB7B40C72493ULL,
    0x3C9EBE0A15C9BEBCULL, 0x431D67C49C100D4CULL,
    0x4CC5D4BECB3E42B6ULL, 0x597F299CFC657E2AULL,
    0x5FCB6FAB3AD6FAECULL, 0x6C44198C4A475817ULL,
};

#define ROTR(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
#define SHR(x, n)  ((x) >> (n))
#define CH(x, y, z)  (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define BSIG0(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
#define BSIG1(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
#define SSIG0(x) (ROTR(x,  1) ^ ROTR(x,  8) ^ SHR(x, 7))
#define SSIG1(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHR(x, 6))

static void sha512_block(struct sha512_context *ctx, const unsigned char *p)
{
    sha512_uint64 w[80];
    sha512_uint64 a, b, c, d, e, f, g, h, t1, t2;
    int i;

    for(i = 0; i < 16; i++)
    {
        int j = i * 8;
        w[i] = ((sha512_uint64) p[j] << 56) | ((sha512_uint64) p[j + 1] << 48)
             | ((sha512_uint64) p[j + 2] << 40) | ((sha512_uint64) p[j + 3] << 32)
             | ((sha512_uint64) p[j + 4] << 24) | ((sha512_uint64) p[j + 5] << 16)
             | ((sha512_uint64) p[j + 6] << 8)  | ((sha512_uint64) p[j + 7]);
    }
    for(i = 16; i < 80; i++)
        w[i] = SSIG1(w[i - 2]) + w[i - 7] + SSIG0(w[i - 15]) + w[i - 16];

    a = ctx->state[0]; b = ctx->state[1]; c = ctx->state[2]; d = ctx->state[3];
    e = ctx->state[4]; f = ctx->state[5]; g = ctx->state[6]; h = ctx->state[7];

    for(i = 0; i < 80; i++)
    {
        t1 = h + BSIG1(e) + CH(e, f, g) + K[i] + w[i];
        t2 = BSIG0(a) + MAJ(a, b, c);
        h = g; g = f; f = e; e = d + t1;
        d = c; c = b; b = a; a = t1 + t2;
    }

    ctx->state[0] += a; ctx->state[1] += b; ctx->state[2] += c;
    ctx->state[3] += d; ctx->state[4] += e; ctx->state[5] += f;
    ctx->state[6] += g; ctx->state[7] += h;

    memset(w, 0, sizeof(w));
}

void sha512_init(struct sha512_context *ctx)
{
    ctx->state[0] = 0x6A09E667F3BCC908ULL;
    ctx->state[1] = 0xBB67AE8584CAA73BULL;
    ctx->state[2] = 0x3C6EF372FE94F82BULL;
    ctx->state[3] = 0xA54FF53A5F1D36F1ULL;
    ctx->state[4] = 0x510E527FADE682D1ULL;
    ctx->state[5] = 0x9B05688C2B3E6C1FULL;
    ctx->state[6] = 0x1F83D9ABFB41BD6BULL;
    ctx->state[7] = 0x5BE0CD19137E2179ULL;
    ctx->count = 0;
    ctx->used = 0;
}

void sha512_update(struct sha512_context *ctx, const void *data, size_t size)
{
    const unsigned char *p = (const unsigned char *) data;
    size_t take;

    ctx->count += (sha512_uint64) size;
    while(size > 0)
    {
        take = SHA512_BLOCKLEN - ctx->used;
        if(take > size)
            take = size;
        memcpy(ctx->buffer + ctx->used, p, take);
        ctx->used += (unsigned int) take;
        p += take;
        size -= take;
        if(ctx->used == SHA512_BLOCKLEN)
        {
            sha512_block(ctx, ctx->buffer);
            ctx->used = 0;
        }
    }
}

void sha512_final(struct sha512_context *ctx,
                  unsigned char result[SHA512_RESULTLEN])
{
    sha512_uint64 bits;
    int i;

    bits = ctx->count << 3;

    /*
        A single set bit, then zeros, then the length in the last sixteen
        bytes.  Only the low eight of those are ever used here: the high
        half would need a message of two thousand million gigabytes.
    */
    ctx->buffer[ctx->used++] = 0x80;
    if(ctx->used > SHA512_BLOCKLEN - 16)
    {
        memset(ctx->buffer + ctx->used, 0, SHA512_BLOCKLEN - ctx->used);
        sha512_block(ctx, ctx->buffer);
        ctx->used = 0;
    }
    memset(ctx->buffer + ctx->used, 0, SHA512_BLOCKLEN - 16 - ctx->used);
    memset(ctx->buffer + SHA512_BLOCKLEN - 16, 0, 8);
    for(i = 0; i < 8; i++)
        ctx->buffer[SHA512_BLOCKLEN - 1 - i] =
            (unsigned char) ((bits >> (8 * i)) & 0xFF);
    sha512_block(ctx, ctx->buffer);

    for(i = 0; i < 8; i++)
    {
        sha512_uint64 v = ctx->state[i];
        int j;

        for(j = 0; j < 8; j++)
            result[i * 8 + j] = (unsigned char) ((v >> (56 - 8 * j)) & 0xFF);
    }
    memset(ctx, 0, sizeof(*ctx));
}

void sha512_get_digest(const void *data, size_t size,
                       unsigned char result[SHA512_RESULTLEN])
{
    struct sha512_context ctx;

    sha512_init(&ctx);
    sha512_update(&ctx, data, size);
    sha512_final(&ctx, result);
}
