barretenberg
Loading...
Searching...
No Matches
blake2-impl.hpp
1/*
2 BLAKE2 reference source code package - reference C implementations
3
4 Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. 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
21namespace blake2 {
22
23#if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
24#if defined(_MSC_VER)
25#define BLAKE2_INLINE __inline
26#elif defined(__GNUC__)
27#define BLAKE2_INLINE __inline__
28#else
29#define BLAKE2_INLINE
30#endif
31#else
32#define BLAKE2_INLINE inline
33#endif
34
35static BLAKE2_INLINE uint32_t load32(const void* src)
36{
37#if defined(NATIVE_LITTLE_ENDIAN)
38 uint32_t w;
39 memcpy(&w, src, sizeof w);
40 return w;
41#else
42 const uint8_t* p = (const uint8_t*)src;
43 return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);
44#endif
45}
46
47static BLAKE2_INLINE uint64_t load64(const void* src)
48{
49#if defined(NATIVE_LITTLE_ENDIAN)
50 uint64_t w;
51 memcpy(&w, src, sizeof w);
52 return w;
53#else
54 const uint8_t* p = (const uint8_t*)src;
55 return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) | ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |
56 ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) | ((uint64_t)(p[6]) << 48) | ((uint64_t)(p[7]) << 56);
57#endif
58}
59
60static BLAKE2_INLINE uint16_t load16(const void* src)
61{
62#if defined(NATIVE_LITTLE_ENDIAN)
63 uint16_t w;
64 memcpy(&w, src, sizeof w);
65 return w;
66#else
67 const uint8_t* p = (const uint8_t*)src;
68 return (uint16_t)(((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8));
69#endif
70}
71
72static BLAKE2_INLINE void store16(void* dst, uint16_t w)
73{
74#if defined(NATIVE_LITTLE_ENDIAN)
75 memcpy(dst, &w, sizeof w);
76#else
77 uint8_t* p = (uint8_t*)dst;
78 *p++ = (uint8_t)w;
79 w = (uint16_t)(w >> 8);
80 *p++ = (uint8_t)w;
81#endif
82}
83
84static BLAKE2_INLINE void store32(void* dst, uint32_t w)
85{
86#if defined(NATIVE_LITTLE_ENDIAN)
87 memcpy(dst, &w, sizeof w);
88#else
89 uint8_t* p = (uint8_t*)dst;
90 p[0] = (uint8_t)(w >> 0);
91 p[1] = (uint8_t)(w >> 8);
92 p[2] = (uint8_t)(w >> 16);
93 p[3] = (uint8_t)(w >> 24);
94#endif
95}
96
97static BLAKE2_INLINE void store64(void* dst, uint64_t w)
98{
99#if defined(NATIVE_LITTLE_ENDIAN)
100 memcpy(dst, &w, sizeof w);
101#else
102 uint8_t* p = (uint8_t*)dst;
103 p[0] = (uint8_t)(w >> 0);
104 p[1] = (uint8_t)(w >> 8);
105 p[2] = (uint8_t)(w >> 16);
106 p[3] = (uint8_t)(w >> 24);
107 p[4] = (uint8_t)(w >> 32);
108 p[5] = (uint8_t)(w >> 40);
109 p[6] = (uint8_t)(w >> 48);
110 p[7] = (uint8_t)(w >> 56);
111#endif
112}
113
114static BLAKE2_INLINE uint64_t load48(const void* src)
115{
116 const uint8_t* p = (const uint8_t*)src;
117 return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) | ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |
118 ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40);
119}
120
121static BLAKE2_INLINE void store48(void* dst, uint64_t w)
122{
123 uint8_t* p = (uint8_t*)dst;
124 p[0] = (uint8_t)(w >> 0);
125 p[1] = (uint8_t)(w >> 8);
126 p[2] = (uint8_t)(w >> 16);
127 p[3] = (uint8_t)(w >> 24);
128 p[4] = (uint8_t)(w >> 32);
129 p[5] = (uint8_t)(w >> 40);
130}
131
132static BLAKE2_INLINE uint32_t rotr32(const uint32_t w, const unsigned c)
133{
134 return (w >> c) | (w << (32 - c));
135}
136
137static BLAKE2_INLINE uint64_t rotr64(const uint64_t w, const unsigned c)
138{
139 return (w >> c) | (w << (64 - c));
140}
141
142/* prevents compiler optimizing out memset() */
143static BLAKE2_INLINE void secure_zero_memory(void* v, size_t n)
144{
145 static void* (*const volatile memset_v)(void*, int, size_t) = &memset;
146 memset_v(v, 0, n);
147}
148
149} // namespace blake2
150
151#endif