]> pd.if.org Git - zpackage/blob - lib/blake2.h
109bf1a3c4d50ffc14fb39711760175cbb6a1e51
[zpackage] / lib / blake2.h
1 #ifndef BLAKE2_H
2 #define BLAKE2_H
3
4 #include <stddef.h>
5 #include <stdint.h>
6
7 #if defined(_MSC_VER)
8 #define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop))
9 #else
10 #define BLAKE2_PACKED(x) x __attribute__((packed))
11 #endif
12
13   enum blake2s_constant
14   {
15     BLAKE2S_BLOCKBYTES = 64,
16     BLAKE2S_OUTBYTES   = 32,
17     BLAKE2S_KEYBYTES   = 32,
18     BLAKE2S_SALTBYTES  = 8,
19     BLAKE2S_PERSONALBYTES = 8
20   };
21
22   enum blake2b_constant
23   {
24     BLAKE2B_BLOCKBYTES = 128,
25     BLAKE2B_OUTBYTES   = 64,
26     BLAKE2B_KEYBYTES   = 64,
27     BLAKE2B_SALTBYTES  = 16,
28     BLAKE2B_PERSONALBYTES = 16
29   };
30
31   typedef struct blake2s_state__
32   {
33     uint32_t h[8];
34     uint32_t t[2];
35     uint32_t f[2];
36     uint8_t  buf[BLAKE2S_BLOCKBYTES];
37     size_t   buflen;
38     size_t   outlen;
39     uint8_t  last_node;
40   } blake2s_state;
41
42   typedef struct blake2b_state__
43   {
44     uint64_t h[8];
45     uint64_t t[2];
46     uint64_t f[2];
47     uint8_t  buf[BLAKE2B_BLOCKBYTES];
48     size_t   buflen;
49     size_t   outlen;
50     uint8_t  last_node;
51   } blake2b_state;
52
53   typedef struct blake2sp_state__
54   {
55     blake2s_state S[8][1];
56     blake2s_state R[1];
57     uint8_t       buf[8 * BLAKE2S_BLOCKBYTES];
58     size_t        buflen;
59     size_t        outlen;
60   } blake2sp_state;
61
62   typedef struct blake2bp_state__
63   {
64     blake2b_state S[4][1];
65     blake2b_state R[1];
66     uint8_t       buf[4 * BLAKE2B_BLOCKBYTES];
67     size_t        buflen;
68     size_t        outlen;
69   } blake2bp_state;
70
71
72   BLAKE2_PACKED(struct blake2s_param__
73   {
74     uint8_t  digest_length; /* 1 */
75     uint8_t  key_length;    /* 2 */
76     uint8_t  fanout;        /* 3 */
77     uint8_t  depth;         /* 4 */
78     uint32_t leaf_length;   /* 8 */
79     uint32_t node_offset;  /* 12 */
80     uint16_t xof_length;    /* 14 */
81     uint8_t  node_depth;    /* 15 */
82     uint8_t  inner_length;  /* 16 */
83     /* uint8_t  reserved[0]; */
84     uint8_t  salt[BLAKE2S_SALTBYTES]; /* 24 */
85     uint8_t  personal[BLAKE2S_PERSONALBYTES];  /* 32 */
86   });
87
88   typedef struct blake2s_param__ blake2s_param;
89
90   BLAKE2_PACKED(struct blake2b_param__
91   {
92     uint8_t  digest_length; /* 1 */
93     uint8_t  key_length;    /* 2 */
94     uint8_t  fanout;        /* 3 */
95     uint8_t  depth;         /* 4 */
96     uint32_t leaf_length;   /* 8 */
97     uint32_t node_offset;   /* 12 */
98     uint32_t xof_length;    /* 16 */
99     uint8_t  node_depth;    /* 17 */
100     uint8_t  inner_length;  /* 18 */
101     uint8_t  reserved[14];  /* 32 */
102     uint8_t  salt[BLAKE2B_SALTBYTES]; /* 48 */
103     uint8_t  personal[BLAKE2B_PERSONALBYTES];  /* 64 */
104   });
105
106   typedef struct blake2b_param__ blake2b_param;
107
108   typedef struct blake2xs_state__
109   {
110     blake2s_state S[1];
111     blake2s_param P[1];
112   } blake2xs_state;
113
114   typedef struct blake2xb_state__
115   {
116     blake2b_state S[1];
117     blake2b_param P[1];
118   } blake2xb_state;
119
120   /* Padded structs result in a compile-time error */
121   enum {
122     BLAKE2_DUMMY_1 = 1/(sizeof(blake2s_param) == BLAKE2S_OUTBYTES),
123     BLAKE2_DUMMY_2 = 1/(sizeof(blake2b_param) == BLAKE2B_OUTBYTES)
124   };
125
126   /* Streaming API */
127   int blake2s_init( blake2s_state *S, size_t outlen );
128   int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
129   int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
130   int blake2s_update( blake2s_state *S, const void *in, size_t inlen );
131   int blake2s_final( blake2s_state *S, void *out, size_t outlen );
132
133   int blake2b_init( blake2b_state *S, size_t outlen );
134   int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
135   int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
136   int blake2b_update( blake2b_state *S, const void *in, size_t inlen );
137   int blake2b_final( blake2b_state *S, void *out, size_t outlen );
138
139   int blake2sp_init( blake2sp_state *S, size_t outlen );
140   int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen );
141   int blake2sp_update( blake2sp_state *S, const void *in, size_t inlen );
142   int blake2sp_final( blake2sp_state *S, void *out, size_t outlen );
143
144   int blake2bp_init( blake2bp_state *S, size_t outlen );
145   int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen );
146   int blake2bp_update( blake2bp_state *S, const void *in, size_t inlen );
147   int blake2bp_final( blake2bp_state *S, void *out, size_t outlen );
148
149   /* Variable output length API */
150   int blake2xs_init( blake2xs_state *S, const size_t outlen );
151   int blake2xs_init_key( blake2xs_state *S, const size_t outlen, const void *key, size_t keylen );
152   int blake2xs_update( blake2xs_state *S, const void *in, size_t inlen );
153   int blake2xs_final(blake2xs_state *S, void *out, size_t outlen);
154
155   int blake2xb_init( blake2xb_state *S, const size_t outlen );
156   int blake2xb_init_key( blake2xb_state *S, const size_t outlen, const void *key, size_t keylen );
157   int blake2xb_update( blake2xb_state *S, const void *in, size_t inlen );
158   int blake2xb_final(blake2xb_state *S, void *out, size_t outlen);
159
160   /* Simple API */
161   int blake2s( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
162   int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
163
164   int blake2sp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
165   int blake2bp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
166
167   int blake2xs( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
168   int blake2xb( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
169
170   /* This is simply an alias for blake2b */
171   int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
172
173 #endif