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