• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2    BLAKE2 reference source code package - optimized C implementations
3 
4    Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
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 #pragma once
14 #ifndef __BLAKE2_H__
15 #define __BLAKE2_H__
16 
17 #include <stddef.h>
18 #include <stdint.h>
19 
20 #if defined(_WIN32) || defined(__CYGWIN__)
21     #define BLAKE2_DLL_IMPORT __declspec(dllimport)
22     #define BLAKE2_DLL_EXPORT __declspec(dllexport)
23     #define BLAKE2_DLL_PRIVATE
24 #elif __GNUC__ >= 4
25   #define BLAKE2_DLL_IMPORT   __attribute__ ((visibility ("default")))
26   #define BLAKE2_DLL_EXPORT   __attribute__ ((visibility ("default")))
27   #define BLAKE2_DLL_PRIVATE  __attribute__ ((visibility ("hidden")))
28 #else
29   #define BLAKE2_DLL_IMPORT
30   #define BLAKE2_DLL_EXPORT
31   #define BLAKE2_DLL_PRIVATE
32 #endif
33 
34 #if defined(BLAKE2_DLL)
35   #if defined(BLAKE2_DLL_EXPORTS) // defined if we are building the DLL
36     #define BLAKE2_API BLAKE2_DLL_EXPORT
37   #else
38     #define BLAKE2_API BLAKE2_DLL_IMPORT
39   #endif
40   #define BLAKE2_PRIVATE BLAKE2_DLL_PRIVATE // must only be used by hidden logic
41 #else
42   #define BLAKE2_API
43   #define BLAKE2_PRIVATE
44 #endif
45 
46 #if defined(__cplusplus)
47 extern "C" {
48 #elif defined(_MSC_VER) && !defined(inline)
49 #define inline __inline
50 #endif
51 
52   enum blake2s_constant
53   {
54     BLAKE2S_BLOCKBYTES = 64,
55     BLAKE2S_OUTBYTES   = 32,
56     BLAKE2S_KEYBYTES   = 32,
57     BLAKE2S_SALTBYTES  = 8,
58     BLAKE2S_PERSONALBYTES = 8
59   };
60 
61   enum blake2b_constant
62   {
63     BLAKE2B_BLOCKBYTES = 128,
64     BLAKE2B_OUTBYTES   = 64,
65     BLAKE2B_KEYBYTES   = 64,
66     BLAKE2B_SALTBYTES  = 16,
67     BLAKE2B_PERSONALBYTES = 16
68   };
69 
70 #pragma pack(push, 1)
71   typedef struct __blake2s_param
72   {
73     uint8_t  digest_length; // 1
74     uint8_t  key_length;    // 2
75     uint8_t  fanout;        // 3
76     uint8_t  depth;         // 4
77     uint32_t leaf_length;   // 8
78     uint8_t  node_offset[6];// 14
79     uint8_t  node_depth;    // 15
80     uint8_t  inner_length;  // 16
81     // uint8_t  reserved[0];
82     uint8_t  salt[BLAKE2S_SALTBYTES]; // 24
83     uint8_t  personal[BLAKE2S_PERSONALBYTES];  // 32
84   } blake2s_param;
85 
86   typedef struct __blake2s_state
87   {
88     uint32_t h[8];
89     uint32_t t[2];
90     uint32_t f[2];
91     uint8_t  buf[2 * BLAKE2S_BLOCKBYTES];
92     uint32_t buflen;
93     uint8_t  outlen;
94     uint8_t  last_node;
95   } blake2s_state;
96 
97   typedef struct __blake2b_param
98   {
99     uint8_t  digest_length; // 1
100     uint8_t  key_length;    // 2
101     uint8_t  fanout;        // 3
102     uint8_t  depth;         // 4
103     uint32_t leaf_length;   // 8
104     uint64_t node_offset;   // 16
105     uint8_t  node_depth;    // 17
106     uint8_t  inner_length;  // 18
107     uint8_t  reserved[14];  // 32
108     uint8_t  salt[BLAKE2B_SALTBYTES]; // 48
109     uint8_t  personal[BLAKE2B_PERSONALBYTES];  // 64
110   } blake2b_param;
111 
112   typedef struct __blake2b_state
113   {
114     uint64_t h[8];
115     uint64_t t[2];
116     uint64_t f[2];
117     uint8_t  buf[2 * BLAKE2B_BLOCKBYTES];
118     uint32_t buflen;
119     uint8_t  outlen;
120     uint8_t  last_node;
121   } blake2b_state;
122 
123   typedef struct __blake2sp_state
124   {
125     blake2s_state S[8][1];
126     blake2s_state R[1];
127     uint8_t  buf[8 * BLAKE2S_BLOCKBYTES];
128     uint32_t buflen;
129     uint8_t  outlen;
130   } blake2sp_state;
131 
132   typedef struct __blake2bp_state
133   {
134     blake2b_state S[4][1];
135     blake2b_state R[1];
136     uint8_t  buf[4 * BLAKE2B_BLOCKBYTES];
137     uint32_t buflen;
138     uint8_t  outlen;
139   } blake2bp_state;
140 #pragma pack(pop)
141 
142   // Streaming API
143   BLAKE2_API int blake2s_init( blake2s_state *S, size_t outlen );
144   BLAKE2_API int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
145   BLAKE2_API int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
146   BLAKE2_API int blake2s_update( blake2s_state *S, const uint8_t *in, size_t inlen );
147   BLAKE2_API int blake2s_final( blake2s_state *S, uint8_t *out, size_t outlen );
148 
149   BLAKE2_API int blake2b_init( blake2b_state *S, size_t outlen );
150   BLAKE2_API int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
151   BLAKE2_API int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
152   BLAKE2_API int blake2b_update( blake2b_state *S, const uint8_t *in, size_t inlen );
153   BLAKE2_API int blake2b_final( blake2b_state *S, uint8_t *out, size_t outlen );
154 
155   BLAKE2_API int blake2sp_init( blake2sp_state *S, size_t outlen );
156   BLAKE2_API int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen );
157   BLAKE2_API int blake2sp_update( blake2sp_state *S, const uint8_t *in, size_t inlen );
158   BLAKE2_API int blake2sp_final( blake2sp_state *S, uint8_t *out, size_t outlen );
159 
160   BLAKE2_API int blake2bp_init( blake2bp_state *S, size_t outlen );
161   BLAKE2_API int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen );
162   BLAKE2_API int blake2bp_update( blake2bp_state *S, const uint8_t *in, size_t inlen );
163   BLAKE2_API int blake2bp_final( blake2bp_state *S, uint8_t *out, size_t outlen );
164 
165   // Simple API
166   BLAKE2_API int blake2s( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
167   BLAKE2_API int blake2b( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
168 
169   BLAKE2_API int blake2sp( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
170   BLAKE2_API int blake2bp( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
171 
172 #if defined(__cplusplus)
173 }
174 #endif
175 
176 #endif
177 
178