Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/blake2/blake2-impl.h
2065 views
1
/*
2
BLAKE2 reference source code package - reference C implementations
3
4
Copyright 2012, Samuel Neves <[email protected]>. You may use this under the
5
terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6
your option. The terms of these licenses can be found at:
7
8
- CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9
- OpenSSL license : https://www.openssl.org/source/license.html
10
- Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
11
12
More information about the BLAKE2 hash function can be found at
13
https://blake2.net.
14
*/
15
#ifndef BLAKE2_IMPL_H
16
#define BLAKE2_IMPL_H
17
18
#include <stdint.h>
19
#include <string.h>
20
21
static inline uint32_t load32( const void *src )
22
{
23
#if defined(NATIVE_LITTLE_ENDIAN)
24
uint32_t w;
25
memcpy(&w, src, sizeof w);
26
return w;
27
#else
28
const uint8_t *p = ( const uint8_t * )src;
29
return (( uint32_t )( p[0] ) << 0) |
30
(( uint32_t )( p[1] ) << 8) |
31
(( uint32_t )( p[2] ) << 16) |
32
(( uint32_t )( p[3] ) << 24) ;
33
#endif
34
}
35
36
static inline uint64_t load64( const void *src )
37
{
38
#if defined(NATIVE_LITTLE_ENDIAN)
39
uint64_t w;
40
memcpy(&w, src, sizeof w);
41
return w;
42
#else
43
const uint8_t *p = ( const uint8_t * )src;
44
return (( uint64_t )( p[0] ) << 0) |
45
(( uint64_t )( p[1] ) << 8) |
46
(( uint64_t )( p[2] ) << 16) |
47
(( uint64_t )( p[3] ) << 24) |
48
(( uint64_t )( p[4] ) << 32) |
49
(( uint64_t )( p[5] ) << 40) |
50
(( uint64_t )( p[6] ) << 48) |
51
(( uint64_t )( p[7] ) << 56) ;
52
#endif
53
}
54
55
static inline uint16_t load16( const void *src )
56
{
57
#if defined(NATIVE_LITTLE_ENDIAN)
58
uint16_t w;
59
memcpy(&w, src, sizeof w);
60
return w;
61
#else
62
const uint8_t *p = ( const uint8_t * )src;
63
return (( uint16_t )( p[0] ) << 0) |
64
(( uint16_t )( p[1] ) << 8) ;
65
#endif
66
}
67
68
static inline void store16( void *dst, uint16_t w )
69
{
70
#if defined(NATIVE_LITTLE_ENDIAN)
71
memcpy(dst, &w, sizeof w);
72
#else
73
uint8_t *p = ( uint8_t * )dst;
74
*p++ = ( uint8_t )w; w >>= 8;
75
*p++ = ( uint8_t )w;
76
#endif
77
}
78
79
static inline void store32( void *dst, uint32_t w )
80
{
81
#if defined(NATIVE_LITTLE_ENDIAN)
82
memcpy(dst, &w, sizeof w);
83
#else
84
uint8_t *p = ( uint8_t * )dst;
85
p[0] = (uint8_t)(w >> 0);
86
p[1] = (uint8_t)(w >> 8);
87
p[2] = (uint8_t)(w >> 16);
88
p[3] = (uint8_t)(w >> 24);
89
#endif
90
}
91
92
static inline void store64( void *dst, uint64_t w )
93
{
94
#if defined(NATIVE_LITTLE_ENDIAN)
95
memcpy(dst, &w, sizeof w);
96
#else
97
uint8_t *p = ( uint8_t * )dst;
98
p[0] = (uint8_t)(w >> 0);
99
p[1] = (uint8_t)(w >> 8);
100
p[2] = (uint8_t)(w >> 16);
101
p[3] = (uint8_t)(w >> 24);
102
p[4] = (uint8_t)(w >> 32);
103
p[5] = (uint8_t)(w >> 40);
104
p[6] = (uint8_t)(w >> 48);
105
p[7] = (uint8_t)(w >> 56);
106
#endif
107
}
108
109
static inline uint64_t load48( const void *src )
110
{
111
const uint8_t *p = ( const uint8_t * )src;
112
return (( uint64_t )( p[0] ) << 0) |
113
(( uint64_t )( p[1] ) << 8) |
114
(( uint64_t )( p[2] ) << 16) |
115
(( uint64_t )( p[3] ) << 24) |
116
(( uint64_t )( p[4] ) << 32) |
117
(( uint64_t )( p[5] ) << 40) ;
118
}
119
120
static inline void store48( void *dst, uint64_t w )
121
{
122
uint8_t *p = ( uint8_t * )dst;
123
p[0] = (uint8_t)(w >> 0);
124
p[1] = (uint8_t)(w >> 8);
125
p[2] = (uint8_t)(w >> 16);
126
p[3] = (uint8_t)(w >> 24);
127
p[4] = (uint8_t)(w >> 32);
128
p[5] = (uint8_t)(w >> 40);
129
}
130
131
static inline uint32_t rotr32( const uint32_t w, const unsigned c )
132
{
133
return ( w >> c ) | ( w << ( 32 - c ) );
134
}
135
136
static inline uint64_t rotr64( const uint64_t w, const unsigned c )
137
{
138
return ( w >> c ) | ( w << ( 64 - c ) );
139
}
140
141
/* prevents compiler optimizing out memset() */
142
static inline void secure_zero_memory(void *v, size_t n)
143
{
144
static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
145
memset_v(v, 0, n);
146
}
147
148
#endif
149
150
151