Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/crypto/blake2s.h
1201 views
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
#if defined(_MSC_VER) || defined(__x86_64__) || defined(__x86__)
29
#define NATIVE_LITTLE_ENDIAN
30
#endif
31
32
/* blake2-impl.h */
33
34
static inline uint32_t load32(const void *src)
35
{
36
#if defined(NATIVE_LITTLE_ENDIAN)
37
return *(uint32_t *)(src);
38
#else
39
const uint8_t *p = (uint8_t *)src;
40
uint32_t w = *p++;
41
w |= (uint32_t)(*p++) << 8;
42
w |= (uint32_t)(*p++) << 16;
43
w |= (uint32_t)(*p++) << 24;
44
return w;
45
#endif
46
}
47
48
static inline void store32(void *dst, uint32_t w)
49
{
50
#if defined(NATIVE_LITTLE_ENDIAN)
51
*(uint32_t *)(dst) = w;
52
#else
53
uint8_t *p = (uint8_t *)dst;
54
*p++ = (uint8_t)w; w >>= 8;
55
*p++ = (uint8_t)w; w >>= 8;
56
*p++ = (uint8_t)w; w >>= 8;
57
*p++ = (uint8_t)w;
58
#endif
59
}
60
61
static inline uint64_t load48(const void *src)
62
{
63
const uint8_t *p = (const uint8_t *)src;
64
uint64_t w = *p++;
65
w |= (uint64_t)(*p++) << 8;
66
w |= (uint64_t)(*p++) << 16;
67
w |= (uint64_t)(*p++) << 24;
68
w |= (uint64_t)(*p++) << 32;
69
w |= (uint64_t)(*p++) << 40;
70
return w;
71
}
72
73
static inline void store48(void *dst, uint64_t w)
74
{
75
uint8_t *p = (uint8_t *)dst;
76
*p++ = (uint8_t)w; w >>= 8;
77
*p++ = (uint8_t)w; w >>= 8;
78
*p++ = (uint8_t)w; w >>= 8;
79
*p++ = (uint8_t)w; w >>= 8;
80
*p++ = (uint8_t)w; w >>= 8;
81
*p++ = (uint8_t)w;
82
}
83
84
/* prevents compiler optimizing out memset() */
85
static inline void secure_zero_memory(void *v, size_t n)
86
{
87
volatile uint8_t *p = ( volatile uint8_t * )v;
88
89
while( n-- ) *p++ = 0;
90
}
91
92
/* blake2.h */
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
#if defined(__cplusplus)
131
extern "C" {
132
#endif
133
134
int blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCKBYTES] );
135
136
// Streaming API
137
int blake2s_init( blake2s_state *S, const uint8_t outlen );
138
int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
139
int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
140
int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen );
141
int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen );
142
143
// Simple API
144
int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
145
146
// Direct Hash Mining Helpers
147
#define blake2s_salt32(out, in, inlen, key32) blake2s(out, in, key32, 32, inlen, 32) /* neoscrypt */
148
#define blake2s_simple(out, in, inlen) blake2s(out, in, NULL, 32, inlen, 0)
149
150
#if defined(__cplusplus)
151
}
152
#endif
153
154
#endif
155
156