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