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