Tor 0.4.9.0-alpha-dev
blake2.h
1/* Copyright (c) 2020 tevador <tevador@gmail.com> */
2/* See LICENSE for licensing information */
3
4/* Original code from Argon2 reference source code package used under CC0 Licence
5 * https://github.com/P-H-C/phc-winner-argon2
6 * Copyright 2015
7 * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
8*/
9
10#ifndef PORTABLE_BLAKE2_H
11#define PORTABLE_BLAKE2_H
12
13#include <stdint.h>
14#include <limits.h>
15#include <stddef.h>
16#include <hashx.h>
17
18#if defined(__cplusplus)
19extern "C" {
20#endif
21
22enum blake2b_constant {
23 BLAKE2B_BLOCKBYTES = 128,
24 BLAKE2B_OUTBYTES = 64,
25 BLAKE2B_KEYBYTES = 64,
26 BLAKE2B_SALTBYTES = 16,
27 BLAKE2B_PERSONALBYTES = 16
28};
29
30#pragma pack(push, 1)
31typedef struct blake2b_param {
32 uint8_t digest_length; /* 1 */
33 uint8_t key_length; /* 2 */
34 uint8_t fanout; /* 3 */
35 uint8_t depth; /* 4 */
36 uint32_t leaf_length; /* 8 */
37 uint64_t node_offset; /* 16 */
38 uint8_t node_depth; /* 17 */
39 uint8_t inner_length; /* 18 */
40 uint8_t reserved[14]; /* 32 */
41 uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
42 uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
44#pragma pack(pop)
45
46typedef struct blake2b_state {
47 uint64_t h[8];
48 uint64_t t[2];
49 uint64_t f[2];
50 uint8_t buf[BLAKE2B_BLOCKBYTES];
51 unsigned buflen;
52 unsigned outlen;
53 uint8_t last_node;
55
56/* Ensure param structs have not been wrongly padded */
57/* Poor man's static_assert */
58enum {
59 blake2_size_check_0 = 1 / !!(CHAR_BIT == 8),
60 blake2_size_check_2 =
61 1 / !!(sizeof(blake2b_param) == sizeof(uint64_t) * CHAR_BIT)
62};
63
64HASHX_PRIVATE int hashx_blake2b_init_param(blake2b_state* S, const blake2b_param* P);
65HASHX_PRIVATE int hashx_blake2b_update(blake2b_state* S, const void* in, size_t inlen);
66HASHX_PRIVATE int hashx_blake2b_final(blake2b_state* S, void* out, size_t outlen);
67HASHX_PRIVATE void hashx_blake2b_4r(const blake2b_param* P, const void* in, size_t inlen, void* out);
68
69#if defined(__cplusplus)
70}
71#endif
72
73#endif