Embedded SDK
Embedded SDK
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)
32 extern "C" {
33 #endif
34 enum blake2s_constant {
35  BLAKE2S_BLOCKBYTES = 64,
36  BLAKE2S_OUTBYTES = 32,
37  BLAKE2S_KEYBYTES = 32,
38  BLAKE2S_SALTBYTES = 8,
39  BLAKE2S_PERSONALBYTES = 8
40 };
41 
42 /* move to cx.h */
43 /*enum blake2b_constant
44  {
45  BLAKE2B_BLOCKBYTES = 128,
46  BLAKE2B_OUTBYTES = 64,
47  BLAKE2B_KEYBYTES = 64,
48  BLAKE2B_SALTBYTES = 16,
49  BLAKE2B_PERSONALBYTES = 16
50  };
51 */
52 typedef struct blake2s_state__ {
53  uint32_t h[8];
54  uint32_t t[2];
55  uint32_t f[2];
56  uint8_t buf[BLAKE2S_BLOCKBYTES];
57  size_t buflen;
58  size_t outlen;
59  uint8_t last_node;
60 } blake2s_state;
61 
62 /* move to cx.h */
63 /*
64 typedef struct blake2b_state__
65 {
66  uint64_t h[8];
67  uint64_t t[2];
68  uint64_t f[2];
69  uint8_t buf[BLAKE2B_BLOCKBYTES];
70  size_t buflen;
71  size_t outlen;
72  uint8_t last_node;
73 } blake2b_state;
74 */
75 typedef struct blake2sp_state__ {
76  blake2s_state S[8][1];
77  blake2s_state R[1];
78  uint8_t buf[8 * BLAKE2S_BLOCKBYTES];
79  size_t buflen;
80  size_t outlen;
81 } blake2sp_state;
82 
83 typedef struct blake2bp_state__ {
84  blake2b_state S[4][1];
85  blake2b_state R[1];
86  uint8_t buf[4 * BLAKE2B_BLOCKBYTES];
87  size_t buflen;
88  size_t outlen;
89 } blake2bp_state;
90 
91 BLAKE2_PACKED(struct blake2s_param__ {
92  uint8_t digest_length; /* 1 */
93  uint8_t key_length; /* 2 */
94  uint8_t fanout; /* 3 */
95  uint8_t depth; /* 4 */
96  uint32_t leaf_length; /* 8 */
97  uint32_t node_offset; /* 12 */
98  uint16_t xof_length; /* 14 */
99  uint8_t node_depth; /* 15 */
100  uint8_t inner_length; /* 16 */
101  /* uint8_t reserved[0]; */
102  uint8_t salt[BLAKE2S_SALTBYTES]; /* 24 */
103  uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */
104 });
105 
106 typedef struct blake2s_param__ blake2s_param;
107 
108 BLAKE2_PACKED(struct blake2b_param__ {
109  uint8_t digest_length; /* 1 */
110  uint8_t key_length; /* 2 */
111  uint8_t fanout; /* 3 */
112  uint8_t depth; /* 4 */
113  uint32_t leaf_length; /* 8 */
114  uint32_t node_offset; /* 12 */
115  uint32_t xof_length; /* 16 */
116  uint8_t node_depth; /* 17 */
117  uint8_t inner_length; /* 18 */
118  uint8_t reserved[14]; /* 32 */
119  uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
120  uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
121 });
122 
123 typedef struct blake2b_param__ blake2b_param;
124 
125 typedef struct blake2xs_state__ {
126  blake2s_state S[1];
127  blake2s_param P[1];
128 } blake2xs_state;
129 
130 typedef struct blake2xb_state__ {
131  blake2b_state S[1];
132  blake2b_param P[1];
133 } blake2xb_state;
134 
135 /* Padded structs result in a compile-time error */
136 enum {
137  BLAKE2_DUMMY_1 = 1 / (sizeof(blake2s_param) == BLAKE2S_OUTBYTES),
138  BLAKE2_DUMMY_2 = 1 / (sizeof(blake2b_param) == BLAKE2B_OUTBYTES)
139 };
140 
141 /* Streaming API */
142 int blake2s_init(blake2s_state *S, size_t outlen);
143 int blake2s_init_key(blake2s_state *S, size_t outlen, const void *key, size_t keylen);
144 int blake2s_init_param(blake2s_state *S, const blake2s_param *P);
145 int blake2s_update(blake2s_state *S, const void *in, size_t inlen);
146 int blake2s_final(blake2s_state *S, void *out, size_t outlen);
147 
148 int blake2b_init(blake2b_state *S,
149  size_t outlen,
150  unsigned char *salt,
151  size_t salt_len,
152  unsigned char *personal,
153  size_t personal_len);
154 int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key, size_t keylen);
155 int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
156 void blake2b_update(blake2b_state *S, const void *in, size_t inlen);
157 int blake2b_final(blake2b_state *S, void *out, size_t outlen);
158 
159 int blake2sp_init(blake2sp_state *S, size_t outlen);
160 int blake2sp_init_key(blake2sp_state *S, size_t outlen, const void *key, size_t keylen);
161 int blake2sp_update(blake2sp_state *S, const void *in, size_t inlen);
162 int blake2sp_final(blake2sp_state *S, void *out, size_t outlen);
163 
164 int blake2bp_init(blake2bp_state *S, size_t outlen);
165 int blake2bp_init_key(blake2bp_state *S, size_t outlen, const void *key, size_t keylen);
166 int blake2bp_update(blake2bp_state *S, const void *in, size_t inlen);
167 int blake2bp_final(blake2bp_state *S, void *out, size_t outlen);
168 
169 /* Variable output length API */
170 int blake2xs_init(blake2xs_state *S, const size_t outlen);
171 int blake2xs_init_key(blake2xs_state *S, const size_t outlen, const void *key, size_t keylen);
172 int blake2xs_update(blake2xs_state *S, const void *in, size_t inlen);
173 int blake2xs_final(blake2xs_state *S, void *out, size_t outlen);
174 
175 int blake2xb_init(blake2xb_state *S, const size_t outlen);
176 int blake2xb_init_key(blake2xb_state *S, const size_t outlen, const void *key, size_t keylen);
177 int blake2xb_update(blake2xb_state *S, const void *in, size_t inlen);
178 int blake2xb_final(blake2xb_state *S, void *out, size_t outlen);
179 
180 /* Simple API */
181 int blake2s(void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen);
182 int blake2b(void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen);
183 
184 int blake2sp(void *out,
185  size_t outlen,
186  const void *in,
187  size_t inlen,
188  const void *key,
189  size_t keylen);
190 int blake2bp(void *out,
191  size_t outlen,
192  const void *in,
193  size_t inlen,
194  const void *key,
195  size_t keylen);
196 
197 int blake2xs(void *out,
198  size_t outlen,
199  const void *in,
200  size_t inlen,
201  const void *key,
202  size_t keylen);
203 int blake2xb(void *out,
204  size_t outlen,
205  const void *in,
206  size_t inlen,
207  const void *key,
208  size_t keylen);
209 
210 /* This is simply an alias for blake2b */
211 int blake2(void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen);
212 
213 #if defined(__cplusplus)
214 }
215 #endif
216 
217 #endif // HAVE_BLAKE2
218 
219 #endif // CX_BLAKE2_H
BLAKE2 crypographic hash function.
unsigned short uint16_t
Definition: usbd_conf.h:54
unsigned char uint8_t
Definition: usbd_conf.h:53