libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_generichash / blake2 / ref / blake2.h
blob81f63c04e7a067cf228fdc882e020b8651605cd6
1 /*
2 BLAKE2 reference source code package - reference C implementations
4 Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
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.
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/>.
14 #ifndef __BLAKE2_H__
15 #define __BLAKE2_H__
17 #include <stddef.h>
18 #include <stdint.h>
20 #include "crypto_generichash_blake2b.h"
22 #define blake2b_init_param crypto_generichash_blake2b__init_param
23 #define blake2b_init crypto_generichash_blake2b__init
24 #define blake2b_init_key crypto_generichash_blake2b__init_key
25 #define blake2b_update crypto_generichash_blake2b__update
26 #define blake2b_final crypto_generichash_blake2b__final
27 #define blake2b crypto_generichash_blake2b__blake2b
29 #if defined(_MSC_VER)
30 #define ALIGN(x) __declspec(align(x))
31 #else
32 #define ALIGN(x) __attribute__((aligned(x)))
33 #endif
35 #if defined(__cplusplus)
36 extern "C" {
37 #endif
39 enum blake2s_constant
41 BLAKE2S_BLOCKBYTES = 64,
42 BLAKE2S_OUTBYTES = 32,
43 BLAKE2S_KEYBYTES = 32,
44 BLAKE2S_SALTBYTES = 8,
45 BLAKE2S_PERSONALBYTES = 8
48 enum blake2b_constant
50 BLAKE2B_BLOCKBYTES = 128,
51 BLAKE2B_OUTBYTES = 64,
52 BLAKE2B_KEYBYTES = 64,
53 BLAKE2B_SALTBYTES = 16,
54 BLAKE2B_PERSONALBYTES = 16
57 #pragma pack(push, 1)
58 typedef struct __blake2s_param
60 uint8_t digest_length; // 1
61 uint8_t key_length; // 2
62 uint8_t fanout; // 3
63 uint8_t depth; // 4
64 uint32_t leaf_length; // 8
65 uint8_t node_offset[6];// 14
66 uint8_t node_depth; // 15
67 uint8_t inner_length; // 16
68 // uint8_t reserved[0];
69 uint8_t salt[BLAKE2S_SALTBYTES]; // 24
70 uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32
71 } blake2s_param;
73 ALIGN( 64 ) typedef struct __blake2s_state
75 uint32_t h[8];
76 uint32_t t[2];
77 uint32_t f[2];
78 uint8_t buf[2 * BLAKE2S_BLOCKBYTES];
79 size_t buflen;
80 uint8_t last_node;
81 } blake2s_state ;
83 typedef struct __blake2b_param
85 uint8_t digest_length; // 1
86 uint8_t key_length; // 2
87 uint8_t fanout; // 3
88 uint8_t depth; // 4
89 uint32_t leaf_length; // 8
90 uint64_t node_offset; // 16
91 uint8_t node_depth; // 17
92 uint8_t inner_length; // 18
93 uint8_t reserved[14]; // 32
94 uint8_t salt[BLAKE2B_SALTBYTES]; // 48
95 uint8_t personal[BLAKE2B_PERSONALBYTES]; // 64
96 } blake2b_param;
98 #ifndef DEFINE_BLAKE2B_STATE
99 typedef crypto_generichash_blake2b_state blake2b_state;
100 #else
101 ALIGN( 64 ) typedef struct __blake2b_state
103 uint64_t h[8];
104 uint64_t t[2];
105 uint64_t f[2];
106 uint8_t buf[2 * BLAKE2B_BLOCKBYTES];
107 size_t buflen;
108 uint8_t last_node;
109 } blake2b_state;
110 #endif
112 typedef struct __blake2sp_state
114 blake2s_state S[8][1];
115 blake2s_state R[1];
116 uint8_t buf[8 * BLAKE2S_BLOCKBYTES];
117 size_t buflen;
118 } blake2sp_state;
120 typedef struct __blake2bp_state
122 blake2b_state S[4][1];
123 blake2b_state R[1];
124 uint8_t buf[4 * BLAKE2B_BLOCKBYTES];
125 size_t buflen;
126 } blake2bp_state;
127 #pragma pack(pop)
129 // Streaming API
130 int blake2s_init( blake2s_state *S, const uint8_t outlen );
131 int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
132 int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
133 int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen );
134 int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen );
136 int blake2b_init( blake2b_state *S, const uint8_t outlen );
137 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
138 int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
139 int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen );
140 int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen );
142 int blake2sp_init( blake2sp_state *S, const uint8_t outlen );
143 int blake2sp_init_key( blake2sp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
144 int blake2sp_update( blake2sp_state *S, const uint8_t *in, uint64_t inlen );
145 int blake2sp_final( blake2sp_state *S, uint8_t *out, uint8_t outlen );
147 int blake2bp_init( blake2bp_state *S, const uint8_t outlen );
148 int blake2bp_init_key( blake2bp_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
149 int blake2bp_update( blake2bp_state *S, const uint8_t *in, uint64_t inlen );
150 int blake2bp_final( blake2bp_state *S, uint8_t *out, uint8_t outlen );
152 // Simple API
153 int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
154 int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
156 int blake2sp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
157 int blake2bp( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
159 static inline int blake2( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen )
161 return blake2b( out, in, key, outlen, inlen, keylen );
164 #if defined(__cplusplus)
166 #endif
168 #endif