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 : https://creativecommons.org/publicdomain/zero/1.0
9    - OpenSSL license   : https://www.openssl.org/source/license.html
10    - Apache 2.0        : https://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 #ifndef BLAKE2_H
16 #define BLAKE2_H
17 
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 /* Pack a structure if possible.  This might save space, and is not
22    needed for correctness.  */
23 #ifdef _MSC_VER
24 # define BLAKE2_PACKED(x) __pragma (pack (push, 1)) x __pragma (pack (pop))
25 #else
26 # define BLAKE2_PACKED(x) x _GL_ATTRIBUTE_PACKED
27 #endif
28 
29 #if defined(__cplusplus)
30 extern "C" {
31 #endif
32 
33   enum blake2s_constant
34   {
35     BLAKE2S_BLOCKBYTES = 64,
36     BLAKE2S_OUTBYTES   = 32,
37     BLAKE2S_KEYBYTES   = 32,
38     BLAKE2S_SALTBYTES  = 8,
39     BLAKE2S_PERSONALBYTES = 8
40   };
41 
42   enum blake2b_constant
43   {
44     BLAKE2B_BLOCKBYTES = 128,
45     BLAKE2B_OUTBYTES   = 64,
46     BLAKE2B_KEYBYTES   = 64,
47     BLAKE2B_SALTBYTES  = 16,
48     BLAKE2B_PERSONALBYTES = 16
49   };
50 
51   typedef struct blake2s_state__
52   {
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   typedef struct blake2b_state__
63   {
64     uint64_t h[8];
65     uint64_t t[2];
66     uint64_t f[2];
67     uint8_t  buf[BLAKE2B_BLOCKBYTES];
68     size_t   buflen;
69     size_t   outlen;
70     uint8_t  last_node;
71   } blake2b_state;
72 
73   typedef struct blake2sp_state__
74   {
75     blake2s_state S[8][1];
76     blake2s_state R[1];
77     uint8_t       buf[8 * BLAKE2S_BLOCKBYTES];
78     size_t        buflen;
79     size_t        outlen;
80   } blake2sp_state;
81 
82   typedef struct blake2bp_state__
83   {
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 
92   BLAKE2_PACKED(struct blake2s_param__
93   {
94     uint8_t  digest_length; /* 1 */
95     uint8_t  key_length;    /* 2 */
96     uint8_t  fanout;        /* 3 */
97     uint8_t  depth;         /* 4 */
98     uint32_t leaf_length;   /* 8 */
99     uint32_t node_offset;  /* 12 */
100     uint16_t xof_length;    /* 14 */
101     uint8_t  node_depth;    /* 15 */
102     uint8_t  inner_length;  /* 16 */
103     /* uint8_t  reserved[0]; */
104     uint8_t  salt[BLAKE2S_SALTBYTES]; /* 24 */
105     uint8_t  personal[BLAKE2S_PERSONALBYTES];  /* 32 */
106   });
107 
108   typedef struct blake2s_param__ blake2s_param;
109 
110   BLAKE2_PACKED(struct blake2b_param__
111   {
112     uint8_t  digest_length; /* 1 */
113     uint8_t  key_length;    /* 2 */
114     uint8_t  fanout;        /* 3 */
115     uint8_t  depth;         /* 4 */
116     uint32_t leaf_length;   /* 8 */
117     uint32_t node_offset;   /* 12 */
118     uint32_t xof_length;    /* 16 */
119     uint8_t  node_depth;    /* 17 */
120     uint8_t  inner_length;  /* 18 */
121     uint8_t  reserved[14];  /* 32 */
122     uint8_t  salt[BLAKE2B_SALTBYTES]; /* 48 */
123     uint8_t  personal[BLAKE2B_PERSONALBYTES];  /* 64 */
124   });
125 
126   typedef struct blake2b_param__ blake2b_param;
127 
128   typedef struct blake2xs_state__
129   {
130     blake2s_state S[1];
131     blake2s_param P[1];
132   } blake2xs_state;
133 
134   typedef struct blake2xb_state__
135   {
136     blake2b_state S[1];
137     blake2b_param P[1];
138   } blake2xb_state;
139 
140   /* Padded structs result in a compile-time error */
141   enum {
142     BLAKE2_DUMMY_1 = 1 / (sizeof (blake2s_param) == BLAKE2S_OUTBYTES),
143     BLAKE2_DUMMY_2 = 1 / (sizeof (blake2b_param) == BLAKE2B_OUTBYTES)
144   };
145 
146   /* Streaming API */
147   int blake2s_init( blake2s_state *S, size_t outlen );
148   int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
149   int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
150   int blake2s_update( blake2s_state *S, const void *in, size_t inlen );
151   int blake2s_final( blake2s_state *S, void *out, size_t outlen );
152 
153   int blake2b_init( blake2b_state *S, size_t outlen );
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     _GL_ATTRIBUTE_NONNULL ();
157   int blake2b_update( blake2b_state *S, const void *in, size_t inlen );
158   int blake2b_final( blake2b_state *S, void *out, size_t outlen );
159 
160   int blake2sp_init( blake2sp_state *S, size_t outlen );
161   int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen );
162   int blake2sp_update( blake2sp_state *S, const void *in, size_t inlen );
163   int blake2sp_final( blake2sp_state *S, void *out, size_t outlen );
164 
165   int blake2bp_init( blake2bp_state *S, size_t outlen );
166   int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen );
167   int blake2bp_update( blake2bp_state *S, const void *in, size_t inlen );
168   int blake2bp_final( blake2bp_state *S, void *out, size_t outlen );
169 
170   /* Variable output length API */
171   int blake2xs_init( blake2xs_state *S, const size_t outlen );
172   int blake2xs_init_key( blake2xs_state *S, const size_t outlen, const void *key, size_t keylen );
173   int blake2xs_update( blake2xs_state *S, const void *in, size_t inlen );
174   int blake2xs_final(blake2xs_state *S, void *out, size_t outlen);
175 
176   int blake2xb_init( blake2xb_state *S, const size_t outlen );
177   int blake2xb_init_key( blake2xb_state *S, const size_t outlen, const void *key, size_t keylen );
178   int blake2xb_update( blake2xb_state *S, const void *in, size_t inlen );
179   int blake2xb_final(blake2xb_state *S, void *out, size_t outlen);
180 
181   /* Simple API */
182   int blake2s( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
183   int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
184 
185   int blake2sp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
186   int blake2bp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
187 
188   int blake2xs( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
189   int blake2xb( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
190 
191   /* This is simply an alias for blake2b */
192   int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
193 
194 #if defined(__cplusplus)
195 }
196 #endif
197 
198 #endif
199