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 #pragma once
16 #ifndef __BLAKE2_H__
17 #define __BLAKE2_H__
18 
19 #include <stddef.h>
20 
21 #include "blake2-config.h"
22 
23 #ifdef BLAKE2_NO_INLINE
24 #define BLAKE2_LOCAL_INLINE(type) static type
25 #endif
26 
27 #ifndef BLAKE2_LOCAL_INLINE
28 #define BLAKE2_LOCAL_INLINE(type) static inline type
29 #endif
30 
31 #if defined(__cplusplus)
32 extern "C" {
33 #endif
34 
35   enum blake2s_constant
36   {
37     BLAKE2S_BLOCKBYTES = 64,
38     BLAKE2S_OUTBYTES   = 32,
39     BLAKE2S_KEYBYTES   = 32,
40     BLAKE2S_SALTBYTES  = 8,
41     BLAKE2S_PERSONALBYTES = 8
42   };
43 
44   enum blake2b_constant
45   {
46     BLAKE2B_BLOCKBYTES = 128,
47     BLAKE2B_OUTBYTES   = 64,
48     BLAKE2B_KEYBYTES   = 64,
49     BLAKE2B_SALTBYTES  = 16,
50     BLAKE2B_PERSONALBYTES = 16
51   };
52 
53   typedef struct __blake2s_state
54   {
55     uint32_t h[8];
56     uint32_t t[2];
57     uint32_t f[2];
58     uint8_t  buf[2 * BLAKE2S_BLOCKBYTES];
59     size_t   buflen;
60     uint8_t  last_node;
61   } blake2s_state;
62 
63   typedef struct __blake2b_state
64   {
65     uint64_t h[8];
66     uint64_t t[2];
67     uint64_t f[2];
68     uint8_t  buf[2 * BLAKE2B_BLOCKBYTES];
69     size_t   buflen;
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   } blake2sp_state;
80 
81   typedef struct __blake2bp_state
82   {
83     blake2b_state S[4][1];
84     blake2b_state R[1];
85     uint8_t buf[4 * BLAKE2B_BLOCKBYTES];
86     size_t  buflen;
87   } blake2bp_state;
88 
89 
90 #pragma pack(push, 1)
91   typedef struct __blake2s_param
92   {
93     uint8_t  digest_length; /* 1 */
94     uint8_t  key_length;    /* 2 */
95     uint8_t  fanout;        /* 3 */
96     uint8_t  depth;         /* 4 */
97     uint32_t leaf_length;   /* 8 */
98     uint8_t  node_offset[6];// 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   } blake2s_param;
105 
106   typedef struct __blake2b_param
107   {
108     uint8_t  digest_length; /* 1 */
109     uint8_t  key_length;    /* 2 */
110     uint8_t  fanout;        /* 3 */
111     uint8_t  depth;         /* 4 */
112     uint32_t leaf_length;   /* 8 */
113     uint64_t node_offset;   /* 16 */
114     uint8_t  node_depth;    /* 17 */
115     uint8_t  inner_length;  /* 18 */
116     uint8_t  reserved[14];  /* 32 */
117     uint8_t  salt[BLAKE2B_SALTBYTES]; /* 48 */
118     uint8_t  personal[BLAKE2B_PERSONALBYTES];  /* 64 */
119   } blake2b_param;
120 #pragma pack(pop)
121 
122   /* Streaming API */
123   int blake2s_init( blake2s_state *S, const uint8_t outlen );
124   int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
125   int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
126   int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen );
127   int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen );
128 
129   int blake2b_init( blake2b_state *S, const uint8_t outlen );
130   int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
131   int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
132   int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen );
133   int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen );
134 
135   int blake2sp_init( blake2sp_state *S, const uint8_t outlen );
136   int blake2sp_init_key( blake2sp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
137   int blake2sp_update( blake2sp_state *S, const uint8_t *in, uint64_t inlen );
138   int blake2sp_final( blake2sp_state *S, uint8_t *out, uint8_t outlen );
139 
140   int blake2bp_init( blake2bp_state *S, const uint8_t outlen );
141   int blake2bp_init_key( blake2bp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
142   int blake2bp_update( blake2bp_state *S, const uint8_t *in, uint64_t inlen );
143   int blake2bp_final( blake2bp_state *S, uint8_t *out, uint8_t outlen );
144 
145   /* Simple API */
146   int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
147   int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
148 
149   int blake2sp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
150   int blake2bp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
151 
blake2(uint8_t * out,const void * in,const void * key,const uint8_t outlen,const uint64_t inlen,uint8_t keylen)152   static inline int blake2( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen )
153   {
154     return blake2b( out, in, key, outlen, inlen, keylen );
155   }
156 
157 #if defined(__cplusplus)
158 }
159 #endif
160 
161 #endif
162 
163