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    All code is triple-licensed under the
11    [CC0](http://creativecommons.org/publicdomain/zero/1.0), the
12    [OpenSSL Licence](https://www.openssl.org/source/license.html), or
13    the [Apache Public License 2.0](http://www.apache.org/licenses/LICENSE-2.0),
14    at your choosing.
15  */
16 
17 #ifndef blake2_H
18 #define blake2_H
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include "crypto_generichash_blake2b.h"
24 #include "export.h"
25 
26 #define blake2b_init_param crypto_generichash_blake2b__init_param
27 #define blake2b_init crypto_generichash_blake2b__init
28 #define blake2b_init_salt_personal \
29     crypto_generichash_blake2b__init_salt_personal
30 #define blake2b_init_key crypto_generichash_blake2b__init_key
31 #define blake2b_init_key_salt_personal \
32     crypto_generichash_blake2b__init_key_salt_personal
33 #define blake2b_update crypto_generichash_blake2b__update
34 #define blake2b_final crypto_generichash_blake2b__final
35 #define blake2b crypto_generichash_blake2b__blake2b
36 #define blake2b_salt_personal crypto_generichash_blake2b__blake2b_salt_personal
37 #define blake2b_pick_best_implementation \
38     crypto_generichash_blake2b__pick_best_implementation
39 
40 enum blake2b_constant {
41     BLAKE2B_BLOCKBYTES    = 128,
42     BLAKE2B_OUTBYTES      = 64,
43     BLAKE2B_KEYBYTES      = 64,
44     BLAKE2B_SALTBYTES     = 16,
45     BLAKE2B_PERSONALBYTES = 16
46 };
47 
48 #if defined(__IBMC__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC)
49 #pragma pack(1)
50 #else
51 #pragma pack(push, 1)
52 #endif
53 
54 typedef struct blake2b_param_ {
55     uint8_t digest_length;                   /*  1 */
56     uint8_t key_length;                      /*  2 */
57     uint8_t fanout;                          /*  3 */
58     uint8_t depth;                           /*  4 */
59     uint8_t leaf_length[4];                  /*  8 */
60     uint8_t node_offset[8];                  /* 16 */
61     uint8_t node_depth;                      /* 17 */
62     uint8_t inner_length;                    /* 18 */
63     uint8_t reserved[14];                    /* 32 */
64     uint8_t salt[BLAKE2B_SALTBYTES];         /* 48 */
65     uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
66 } blake2b_param;
67 
68 typedef crypto_generichash_blake2b_state blake2b_state;
69 
70 #if defined(__IBMC__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC)
71 #pragma pack()
72 #else
73 #pragma pack(pop)
74 #endif
75 
76 /* Streaming API */
77 int blake2b_init(blake2b_state *S, const uint8_t outlen);
78 int blake2b_init_salt_personal(blake2b_state *S, const uint8_t outlen,
79                                const void *salt, const void *personal);
80 int blake2b_init_key(blake2b_state *S, const uint8_t outlen, const void *key,
81                      const uint8_t keylen);
82 int blake2b_init_key_salt_personal(blake2b_state *S, const uint8_t outlen,
83                                    const void *key, const uint8_t keylen,
84                                    const void *salt, const void *personal);
85 int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
86 int blake2b_update(blake2b_state *S, const uint8_t *in, uint64_t inlen);
87 int blake2b_final(blake2b_state *S, uint8_t *out, uint8_t outlen);
88 
89 /* Simple API */
90 int blake2b(uint8_t *out, const void *in, const void *key, const uint8_t outlen,
91             const uint64_t inlen, uint8_t keylen);
92 int blake2b_salt_personal(uint8_t *out, const void *in, const void *key,
93                           const uint8_t outlen, const uint64_t inlen,
94                           uint8_t keylen, const void *salt,
95                           const void *personal);
96 
97 typedef int (*blake2b_compress_fn)(blake2b_state *S,
98                                    const uint8_t  block[BLAKE2B_BLOCKBYTES]);
99 int blake2b_pick_best_implementation(void);
100 int blake2b_compress_ref(blake2b_state *S,
101                          const uint8_t  block[BLAKE2B_BLOCKBYTES]);
102 int blake2b_compress_ssse3(blake2b_state *S,
103                            const uint8_t  block[BLAKE2B_BLOCKBYTES]);
104 int blake2b_compress_sse41(blake2b_state *S,
105                            const uint8_t  block[BLAKE2B_BLOCKBYTES]);
106 int blake2b_compress_avx2(blake2b_state *S,
107                           const uint8_t  block[BLAKE2B_BLOCKBYTES]);
108 
109 #endif
110