/*
 * Argon2 reference source code package - reference C implementations
 *
 * Copyright 2015
 * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
 *
 * You may use this work under the terms of a Creative Commons CC0 1.0
 * License/Waiver or the Apache Public License 2.0, at your option. The terms of
 * these licenses can be found at:
 *
 * - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0
 * - Apache 2.0        : https://www.apache.org/licenses/LICENSE-2.0
 *
 * You should have received a copy of both of these licenses along with this
 * software. If not, they may be obtained at the above URLs.
 */

#ifndef PORTABLE_BLAKE2_H
#define PORTABLE_BLAKE2_H
#include <Library/UefiLib.h>
#include <Library/BaseLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseMemoryLib.h>
#include <Uefi.h>
#include "argon2.h"

#if defined(__cplusplus)
extern "C" {
#endif

#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif

enum blake2b_constant {
    BLAKE2B_BLOCKBYTES = 128,
    BLAKE2B_OUTBYTES = 64,
    BLAKE2B_KEYBYTES = 64,
    BLAKE2B_SALTBYTES = 16,
    BLAKE2B_PERSONALBYTES = 16
};

#pragma pack(push, 1)
typedef struct __blake2b_param {
    UINT8 digest_length;                   /* 1 */
    UINT8 key_length;                      /* 2 */
    UINT8 fanout;                          /* 3 */
    UINT8 depth;                           /* 4 */
    UINT32 leaf_length;                    /* 8 */
    UINT64 node_offset;                    /* 16 */
    UINT8 node_depth;                      /* 17 */
    UINT8 inner_length;                    /* 18 */
    UINT8 reserved[14];                    /* 32 */
    UINT8 salt[BLAKE2B_SALTBYTES];         /* 48 */
    UINT8 personal[BLAKE2B_PERSONALBYTES]; /* 64 */
} blake2b_param;
#pragma pack(pop)

typedef struct __blake2b_state {
    UINT64 h[8];
    UINT64 t[2];
    UINT64 f[2];
    UINT8 buf[BLAKE2B_BLOCKBYTES];
    UINTN buflen;
    UINTN outlen;
    UINT8 last_node;
} blake2b_state;

/* Ensure param structs have not been wrongly padded */
/* Poor man's static_assert */
enum {
    blake2_size_check_0 = 1 / !!(CHAR_BIT == 8),
    blake2_size_check_2 =
        1 / !!(sizeof(blake2b_param) == sizeof(UINT64) * CHAR_BIT)
};

/* Streaming API */
ARGON2_LOCAL INTN blake2b_init(blake2b_state *S, UINTN outlen);
ARGON2_LOCAL INTN blake2b_init_key(blake2b_state *S, UINTN outlen, const VOID *key,
                     UINTN keylen);
ARGON2_LOCAL INTN blake2b_init_param(blake2b_state *S, const blake2b_param *P);
ARGON2_LOCAL INTN blake2b_update(blake2b_state *S, const VOID *in, UINTN inlen);
ARGON2_LOCAL INTN blake2b_final(blake2b_state *S, VOID *out, UINTN outlen);

/* Simple API */
ARGON2_LOCAL INTN blake2b(VOID *out, UINTN outlen, const VOID *in, UINTN inlen,
                         const VOID *key, UINTN keylen);

/* Argon2 Team - Begin Code */
ARGON2_LOCAL INTN blake2b_long(VOID *out, UINTN outlen, const VOID *in, UINTN inlen);
/* Argon2 Team - End Code */

#if defined(__cplusplus)
}
#endif

#endif
