/*
 * 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.
 */
 #include <Library/UefiLib.h>
 #include <Library/BaseLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Uefi.h>

#ifndef PORTABLE_BLAKE2_IMPL_H
#define PORTABLE_BLAKE2_IMPL_H

#ifdef _WIN32
#define BLAKE2_INLINE __inline
#elif defined(__GNUC__) || defined(__clang__)
#define BLAKE2_INLINE __inline__
#else
#define BLAKE2_INLINE
#endif

/* Argon2 Team - Begin Code */
/*
   Not an exhaustive list, but should cover the majority of modern platforms
   Additionally, the code will always be correct---this is only a performance
   tweak.
*/
#if (defined(__BYTE_ORDER__) &&                                                \
     (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) ||                           \
    defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
    defined(__AARCH64EL__) || defined(__amd64__) || defined(__i386__) ||       \
    defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) ||                \
    defined(_M_ARM)
#define NATIVE_LITTLE_ENDIAN
#endif
/* Argon2 Team - End Code */

static BLAKE2_INLINE UINT32 load32(const VOID *src) {
#if defined(NATIVE_LITTLE_ENDIAN)
    UINT32 w;
    CopyMem(&w, src, sizeof w);
    return w;
#else
    const UINT8 *p = (const UINT8 *)src;
    UINT32 w = *p++;
    w |= (UINT32)(*p++) << 8;
    w |= (UINT32)(*p++) << 16;
    w |= (UINT32)(*p++) << 24;
    return w;
#endif
}

static BLAKE2_INLINE UINT64 load64(const VOID *src) {
#if defined(NATIVE_LITTLE_ENDIAN)
    UINT64 w;
    CopyMem(&w, src, sizeof w);
    return w;
#else
    const UINT8 *p = (const UINT8 *)src;
    UINT64 w = *p++;
    w |= (UINT64)(*p++) << 8;
    w |= (UINT64)(*p++) << 16;
    w |= (UINT64)(*p++) << 24;
    w |= (UINT64)(*p++) << 32;
    w |= (UINT64)(*p++) << 40;
    w |= (UINT64)(*p++) << 48;
    w |= (UINT64)(*p++) << 56;
    return w;
#endif
}

static BLAKE2_INLINE VOID store32(VOID *dst, UINT32 w) {
#if defined(NATIVE_LITTLE_ENDIAN)
    CopyMem(dst, &w, sizeof w);
#else
    UINT8 *p = (UINT8 *)dst;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
#endif
}

static BLAKE2_INLINE VOID store64(VOID *dst, UINT64 w) {
#if defined(NATIVE_LITTLE_ENDIAN)
    CopyMem(dst, &w, sizeof w);
#else
    UINT8 *p = (UINT8 *)dst;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
#endif
}

static BLAKE2_INLINE UINT64 load48(const VOID *src) {
    const UINT8 *p = (const UINT8 *)src;
    UINT64 w = *p++;
    w |= (UINT64)(*p++) << 8;
    w |= (UINT64)(*p++) << 16;
    w |= (UINT64)(*p++) << 24;
    w |= (UINT64)(*p++) << 32;
    w |= (UINT64)(*p++) << 40;
    return w;
}

static BLAKE2_INLINE VOID store48(VOID *dst, UINT64 w) {
    UINT8 *p = (UINT8 *)dst;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
    w >>= 8;
    *p++ = (UINT8)w;
}

static BLAKE2_INLINE UINT32 rotr32(const UINT32 w, const UINTN c) {
    return (w >> c) | (w << (32 - c));
}

static BLAKE2_INLINE UINT64 rotr64(const UINT64 w, const UINTN c) {
    return (w >> c) | (w << (64 - c));
}

VOID clear_internal_memory(VOID *v, UINTN n);

#endif
