Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
cx_blake2.h
Go to the documentation of this file.
1/*
2 BLAKE2 reference source code package - reference C implementations
3
4 Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
5 terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6 your option. The terms of these licenses can be found at:
7
8 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9 - OpenSSL license : https://www.openssl.org/source/license.html
10 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
11
12 More information about the BLAKE2 hash function can be found at
13 https://blake2.net.
14*/
15
16#ifndef CX_BLAKE2_H
17#define CX_BLAKE2_H
18
19#ifdef HAVE_BLAKE2
20
21#include "lcx_blake2.h"
22#include <stddef.h>
23#include <stdint.h>
24
25#if defined(_MSC_VER)
26#define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop))
27#else
28#define BLAKE2_PACKED(x) x __attribute__((packed))
29#endif
30
31#if defined(__cplusplus)
32extern "C" {
33#endif
34enum blake2s_constant {
35 BLAKE2S_BLOCKBYTES = 64,
36 BLAKE2S_OUTBYTES = 32,
37 BLAKE2S_KEYBYTES = 32,
38 BLAKE2S_SALTBYTES = 8,
39 BLAKE2S_PERSONALBYTES = 8
40};
41
42BLAKE2_PACKED(struct blake2s_param__ {
43 uint8_t digest_length; /* 1 */
44 uint8_t key_length; /* 2 */
45 uint8_t fanout; /* 3 */
46 uint8_t depth; /* 4 */
47 uint32_t leaf_length; /* 8 */
48 uint32_t node_offset; /* 12 */
49 uint16_t xof_length; /* 14 */
50 uint8_t node_depth; /* 15 */
51 uint8_t inner_length; /* 16 */
52 /* uint8_t reserved[0]; */
53 uint8_t salt[BLAKE2S_SALTBYTES]; /* 24 */
54 uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */
55});
56
57typedef struct blake2s_param__ blake2s_param;
58
59BLAKE2_PACKED(struct blake2b_param__ {
60 uint8_t digest_length; /* 1 */
61 uint8_t key_length; /* 2 */
62 uint8_t fanout; /* 3 */
63 uint8_t depth; /* 4 */
64 uint32_t leaf_length; /* 8 */
65 uint32_t node_offset; /* 12 */
66 uint32_t xof_length; /* 16 */
67 uint8_t node_depth; /* 17 */
68 uint8_t inner_length; /* 18 */
69 uint8_t reserved[14]; /* 32 */
70 uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
71 uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
72});
73
74typedef struct blake2b_param__ blake2b_param;
75
76/* Padded structs result in a compile-time error */
77enum {
78 BLAKE2_DUMMY_1 = 1 / (sizeof(blake2s_param) == BLAKE2S_OUTBYTES),
79 BLAKE2_DUMMY_2 = 1 / (sizeof(blake2b_param) == BLAKE2B_OUTBYTES)
80};
81
82/* Streaming API */
83
84int blake2b_init(blake2b_state *S,
85 size_t outlen,
86 unsigned char *salt,
87 size_t salt_len,
88 unsigned char *personal,
89 size_t personal_len);
90int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key, size_t keylen);
91int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
92void blake2b_update(blake2b_state *S, const void *in, size_t inlen);
93int blake2b_final(blake2b_state *S, void *out, size_t outlen);
94
95/* Simple API */
96int blake2b(void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen);
97
98/* This is simply an alias for blake2b */
99int blake2(void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen);
100
101#if defined(__cplusplus)
102}
103#endif
104
105#endif // HAVE_BLAKE2
106
107#endif // CX_BLAKE2_H
BLAKE2 crypographic hash function.