e44aac6685dca9428b7703dff082688f380282f0
[trex.git] /
1 /*
2    BLAKE2 reference source code package - reference C implementations
3
4    Written in 2012 by Samuel Neves <[email protected]>
5
6    To the extent possible under law, the author(s) have dedicated all copyright
7    and related and neighboring rights to this software to the public domain
8    worldwide. This software is distributed without any warranty.
9
10    You should have received a copy of the CC0 Public Domain Dedication along with
11    this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
12 */
13
14 #ifndef blake2_H
15 #define blake2_H
16
17 #include <stddef.h>
18 #include <stdint.h>
19
20 #include "crypto_generichash_blake2b.h"
21
22 #define blake2b_init_param             crypto_generichash_blake2b__init_param
23 #define blake2b_init                   crypto_generichash_blake2b__init
24 #define blake2b_init_salt_personal     crypto_generichash_blake2b__init_salt_personal
25 #define blake2b_init_key               crypto_generichash_blake2b__init_key
26 #define blake2b_init_key_salt_personal crypto_generichash_blake2b__init_key_salt_personal
27 #define blake2b_update                 crypto_generichash_blake2b__update
28 #define blake2b_final                  crypto_generichash_blake2b__final
29 #define blake2b                        crypto_generichash_blake2b__blake2b
30 #define blake2b_salt_personal          crypto_generichash_blake2b__blake2b_salt_personal
31
32 #if defined(_MSC_VER)
33 #define ALIGN(x) __declspec(align(x))
34 #else
35 #define ALIGN(x) __attribute__((aligned(x)))
36 #endif
37
38 #if defined(__cplusplus)
39 extern "C" {
40 #endif
41
42   enum blake2s_constant
43   {
44     BLAKE2S_BLOCKBYTES = 64,
45     BLAKE2S_OUTBYTES   = 32,
46     BLAKE2S_KEYBYTES   = 32,
47     BLAKE2S_SALTBYTES  = 8,
48     BLAKE2S_PERSONALBYTES = 8
49   };
50
51   enum blake2b_constant
52   {
53     BLAKE2B_BLOCKBYTES = 128,
54     BLAKE2B_OUTBYTES   = 64,
55     BLAKE2B_KEYBYTES   = 64,
56     BLAKE2B_SALTBYTES  = 16,
57     BLAKE2B_PERSONALBYTES = 16
58   };
59
60 #pragma pack(push, 1)
61   typedef struct blake2s_param_
62   {
63     uint8_t  digest_length; // 1
64     uint8_t  key_length;    // 2
65     uint8_t  fanout;        // 3
66     uint8_t  depth;         // 4
67     uint32_t leaf_length;   // 8
68     uint8_t  node_offset[6];// 14
69     uint8_t  node_depth;    // 15
70     uint8_t  inner_length;  // 16
71     // uint8_t  reserved[0];
72     uint8_t  salt[BLAKE2S_SALTBYTES]; // 24
73     uint8_t  personal[BLAKE2S_PERSONALBYTES];  // 32
74   } blake2s_param;
75
76   ALIGN( 64 ) typedef struct blake2s_state_
77   {
78     uint32_t h[8];
79     uint32_t t[2];
80     uint32_t f[2];
81     uint8_t  buf[2 * BLAKE2S_BLOCKBYTES];
82     size_t   buflen;
83     uint8_t  last_node;
84   } blake2s_state ;
85
86   typedef struct blake2b_param_
87   {
88     uint8_t  digest_length; // 1
89     uint8_t  key_length;    // 2
90     uint8_t  fanout;        // 3
91     uint8_t  depth;         // 4
92     uint32_t leaf_length;   // 8
93     uint64_t node_offset;   // 16
94     uint8_t  node_depth;    // 17
95     uint8_t  inner_length;  // 18
96     uint8_t  reserved[14];  // 32
97     uint8_t  salt[BLAKE2B_SALTBYTES]; // 48
98     uint8_t  personal[BLAKE2B_PERSONALBYTES];  // 64
99   } blake2b_param;
100
101 #ifndef DEFINE_BLAKE2B_STATE
102 typedef crypto_generichash_blake2b_state blake2b_state;
103 #else
104   ALIGN( 64 ) typedef struct blake2b_state_
105   {
106     uint64_t h[8];
107     uint64_t t[2];
108     uint64_t f[2];
109     uint8_t  buf[2 * BLAKE2B_BLOCKBYTES];
110     size_t   buflen;
111     uint8_t  last_node;
112   } blake2b_state;
113 #endif
114
115   typedef struct blake2sp_state_
116   {
117     blake2s_state S[8][1];
118     blake2s_state R[1];
119     uint8_t buf[8 * BLAKE2S_BLOCKBYTES];
120     size_t  buflen;
121   } blake2sp_state;
122
123   typedef struct blake2bp_state_
124   {
125     blake2b_state S[4][1];
126     blake2b_state R[1];
127     uint8_t buf[4 * BLAKE2B_BLOCKBYTES];
128     size_t  buflen;
129   } blake2bp_state;
130 #pragma pack(pop)
131
132   // Streaming API
133   int blake2s_init( blake2s_state *S, const uint8_t outlen );
134   int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
135   int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
136   int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen );
137   int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen );
138
139   int blake2b_init( blake2b_state *S, const uint8_t outlen );
140   int blake2b_init_salt_personal( blake2b_state *S, const uint8_t outlen,
141                                   const void *personal, const void *salt );
142   int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
143   int blake2b_init_key_salt_personal( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen,
144                                       const void *salt, const void *personal );
145   int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
146   int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen );
147   int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen );
148
149   int blake2sp_init( blake2sp_state *S, const uint8_t outlen );
150   int blake2sp_init_key( blake2sp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
151   int blake2sp_update( blake2sp_state *S, const uint8_t *in, uint64_t inlen );
152   int blake2sp_final( blake2sp_state *S, uint8_t *out, uint8_t outlen );
153
154   int blake2bp_init( blake2bp_state *S, const uint8_t outlen );
155   int blake2bp_init_key( blake2bp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
156   int blake2bp_update( blake2bp_state *S, const uint8_t *in, uint64_t inlen );
157   int blake2bp_final( blake2bp_state *S, uint8_t *out, uint8_t outlen );
158
159   // Simple API
160   int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
161   int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
162   int blake2b_salt_personal( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen, const void *salt, const void *personal );
163
164   int blake2sp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
165   int blake2bp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
166
167   static inline int blake2( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen )
168   {
169     return blake2b( out, in, key, outlen, inlen, keylen );
170   }
171
172 #if defined(__cplusplus)
173 }
174 #endif
175
176 #endif
177