/*
 * 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 @.^.^.LICENSE.
 * See the Licence for the specific language governing permissions
 * and limitations under the Licence.
 *
 * CDDL HEADER END
 *
 * Copyright (c) RISC OS Developments 2026. All rights reserved.
 * SPDX-License-Identifier: CDDL-1.0
 */

#ifndef __SHA256_H
#define __SHA256_H

#include <stdlib.h>

#define SHA256_RESULTLEN (256/8)
#define SHA256_BLOCKLEN  (64)

typedef unsigned int sha256_uint32;

struct sha256_context
{
    sha256_uint32 state[8];
    sha256_uint32 lo, hi;           /* message length in bits, two halves */
    unsigned char buffer[SHA256_BLOCKLEN];
    unsigned int  used;             /* bytes currently in buffer */
};

void sha256_init(struct sha256_context *ctx);
void sha256_update(struct sha256_context *ctx, const void *data, size_t size);
void sha256_final(struct sha256_context *ctx,
                  unsigned char result[SHA256_RESULTLEN]);

void sha256_get_digest(const void *data, size_t size,
                       unsigned char result[SHA256_RESULTLEN]);

void hmac_sha256(const unsigned char *key, size_t key_len,
                 const void *data, size_t size,
                 unsigned char result[SHA256_RESULTLEN]);

/*
    The same, over two pieces without joining them first.  A signature is
    taken over a header and a body that are already adjacent in the packet
    buffer, but the key schedule is worth not repeating.
*/
void hmac_sha256_2(const unsigned char *key, size_t key_len,
                   const void *d1, size_t n1,
                   const void *d2, size_t n2,
                   unsigned char result[SHA256_RESULTLEN]);

#endif
