OSDN Git Service

Added the BLAKE2 hash algorithm.
[mutilities/MUtilities.git] / src / 3rd_party / blake2 / include / 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 #ifndef __cplusplus
23 #define inline __inline
24 #endif
25 #else
26 #define ALIGN(x) __attribute__((aligned(x)))
27 #endif
28
29 namespace MUtils
30 {
31         namespace Hash
32         {
33                 namespace Internal
34                 {
35                         namespace Blake2Impl
36                         {
37                                 enum blake2b_constant
38                                 {
39                                         BLAKE2B_BLOCKBYTES = 128,
40                                         BLAKE2B_OUTBYTES   = 64,
41                                         BLAKE2B_KEYBYTES   = 64,
42                                         BLAKE2B_SALTBYTES  = 16,
43                                         BLAKE2B_PERSONALBYTES = 16
44                                 };
45
46                                 #pragma pack(push, 1)
47                                 typedef struct __blake2b_param
48                                 {
49                                         uint8_t  digest_length; // 1
50                                         uint8_t  key_length;    // 2
51                                         uint8_t  fanout;        // 3
52                                         uint8_t  depth;         // 4
53                                         uint32_t leaf_length;   // 8
54                                         uint64_t node_offset;   // 16
55                                         uint8_t  node_depth;    // 17
56                                         uint8_t  inner_length;  // 18
57                                         uint8_t  reserved[14];  // 32
58                                         uint8_t  salt[BLAKE2B_SALTBYTES]; // 48
59                                         uint8_t  personal[BLAKE2B_PERSONALBYTES];  // 64
60                                 } blake2b_param;
61
62                                 ALIGN( 64 ) typedef struct __blake2b_state
63                                 {
64                                         uint64_t h[8];
65                                         uint64_t t[2];
66                                         uint64_t f[2];
67                                         uint8_t  buf[2 * BLAKE2B_BLOCKBYTES];
68                                         size_t   buflen;
69                                         uint8_t  last_node;
70                                 } blake2b_state;
71                                 #pragma pack(pop)
72
73                                 // Streaming API
74                                 int blake2b_init( blake2b_state *S, const uint8_t outlen );
75                                 int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
76                                 int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
77                                 int blake2b_update( blake2b_state *S, const uint8_t *in, uint64_t inlen );
78                                 int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen );
79
80                                 // Simple API
81                                 int blake2b( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
82
83                                 static inline int blake2( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen )
84                                 {
85                                         return blake2b( out, in, key, outlen, inlen, keylen );
86                                 }
87                         }
88                 }
89         }
90 }
91
92 #endif
93