]> Git Repo - cpuminer-multi.git/blob - sha3/blake2s.h
mingw: fix neoscrypt bug (related to cflags)
[cpuminer-multi.git] / sha3 / blake2s.h
1 /**
2  * BLAKE2 reference source code package - reference C implementations
3  *
4  * Written in 2012 by Samuel Neves <[email protected]>
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 #include <inttypes.h>
22 #define inline __inline
23 #define ALIGN(x) __declspec(align(x))
24 #else
25 #define ALIGN(x) __attribute__((aligned(x)))
26 #endif
27
28 /* blake2-impl.h */
29
30 static inline uint32_t load32(const void *src)
31 {
32 #if defined(NATIVE_LITTLE_ENDIAN)
33         return *(uint32_t *)(src);
34 #else
35         const uint8_t *p = (uint8_t *)src;
36         uint32_t w = *p++;
37         w |= (uint32_t)(*p++) << 8;
38         w |= (uint32_t)(*p++) << 16;
39         w |= (uint32_t)(*p++) << 24;
40         return w;
41 #endif
42 }
43
44 static inline void store32(void *dst, uint32_t w)
45 {
46 #if defined(NATIVE_LITTLE_ENDIAN)
47         *(uint32_t *)(dst) = w;
48 #else
49         uint8_t *p = (uint8_t *)dst;
50         *p++ = (uint8_t)w; w >>= 8;
51         *p++ = (uint8_t)w; w >>= 8;
52         *p++ = (uint8_t)w; w >>= 8;
53         *p++ = (uint8_t)w;
54 #endif
55 }
56
57 static inline uint64_t load48(const void *src)
58 {
59         const uint8_t *p = (const uint8_t *)src;
60         uint64_t w = *p++;
61         w |= (uint64_t)(*p++) << 8;
62         w |= (uint64_t)(*p++) << 16;
63         w |= (uint64_t)(*p++) << 24;
64         w |= (uint64_t)(*p++) << 32;
65         w |= (uint64_t)(*p++) << 40;
66         return w;
67 }
68
69 static inline void store48(void *dst, uint64_t w)
70 {
71         uint8_t *p = (uint8_t *)dst;
72         *p++ = (uint8_t)w; w >>= 8;
73         *p++ = (uint8_t)w; w >>= 8;
74         *p++ = (uint8_t)w; w >>= 8;
75         *p++ = (uint8_t)w; w >>= 8;
76         *p++ = (uint8_t)w; w >>= 8;
77         *p++ = (uint8_t)w;
78 }
79
80 /* prevents compiler optimizing out memset() */
81 static inline void secure_zero_memory(void *v, size_t n)
82 {
83         volatile uint8_t *p = ( volatile uint8_t * )v;
84
85         while( n-- ) *p++ = 0;
86 }
87
88 /* blake2.h */
89
90 #if defined(__cplusplus)
91 extern "C" {
92 #endif
93
94         enum blake2s_constant
95         {
96                 BLAKE2S_BLOCKBYTES = 64,
97                 BLAKE2S_OUTBYTES   = 32,
98                 BLAKE2S_KEYBYTES   = 32,
99                 BLAKE2S_SALTBYTES  = 8,
100                 BLAKE2S_PERSONALBYTES = 8
101         };
102
103 #pragma pack(push, 1)
104         typedef struct __blake2s_param
105         {
106                 uint8_t  digest_length; // 1
107                 uint8_t  key_length;    // 2
108                 uint8_t  fanout;        // 3
109                 uint8_t  depth;         // 4
110                 uint32_t leaf_length;   // 8
111                 uint8_t  node_offset[6];// 14
112                 uint8_t  node_depth;    // 15
113                 uint8_t  inner_length;  // 16
114                 // uint8_t  reserved[0];
115                 uint8_t  salt[BLAKE2S_SALTBYTES]; // 24
116                 uint8_t  personal[BLAKE2S_PERSONALBYTES];  // 32
117         } blake2s_param;
118
119         ALIGN( 64 ) typedef struct __blake2s_state
120         {
121                 uint32_t h[8];
122                 uint32_t t[2];
123                 uint32_t f[2];
124                 uint8_t  buf[2 * BLAKE2S_BLOCKBYTES];
125                 size_t   buflen;
126                 uint8_t  last_node;
127         } blake2s_state ;
128 #pragma pack(pop)
129
130         int blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCKBYTES] );
131
132         // Streaming API
133         int blake2s_init( blake2s_state *S, const uint8_t outlen );
134         int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
135         int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
136         int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen );
137         int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen );
138
139         // Simple API
140         int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
141
142         // Direct Hash Mining Helpers
143         #define blake2s_salt32(out, in, inlen, key32) blake2s(out, in, key32, 32, inlen, 32) /* neoscrypt */
144         #define blake2s_simple(out, in, inlen) blake2s(out, in, NULL, 32, inlen, 0)
145
146 #if defined(__cplusplus)
147 }
148 #endif
149
150 #endif
This page took 0.030558 seconds and 4 git commands to generate.