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