2 * Copyright (c) 2020 Doc.ai and/or its affiliates.
3 * Copyright (c) 2012 Samuel Neves <sneves@dei.uc.pt>.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 More information about the BLAKE2 hash function can be found at
21 #ifndef __included_crypto_blake2_impl_h__
22 #define __included_crypto_blake2_impl_h__
26 #include <vppinfra/byte_order.h>
28 #if defined(CLIB_ARCH_IS_LITTLE_ENDIAN)
29 #define NATIVE_LITTLE_ENDIAN
32 #if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
34 #define BLAKE2_INLINE __inline
35 #elif defined(__GNUC__)
36 #define BLAKE2_INLINE __inline__
41 #define BLAKE2_INLINE inline
44 static BLAKE2_INLINE uint32_t
45 load32 (const void *src)
47 #if defined(NATIVE_LITTLE_ENDIAN)
49 memcpy (&w, src, sizeof w);
52 const uint8_t *p = (const uint8_t *) src;
53 return ((uint32_t) (p[0]) << 0) |
54 ((uint32_t) (p[1]) << 8) |
55 ((uint32_t) (p[2]) << 16) | ((uint32_t) (p[3]) << 24);
59 static BLAKE2_INLINE uint64_t
60 load64 (const void *src)
62 #if defined(NATIVE_LITTLE_ENDIAN)
64 memcpy (&w, src, sizeof w);
67 const uint8_t *p = (const uint8_t *) src;
68 return ((uint64_t) (p[0]) << 0) |
69 ((uint64_t) (p[1]) << 8) |
70 ((uint64_t) (p[2]) << 16) |
71 ((uint64_t) (p[3]) << 24) |
72 ((uint64_t) (p[4]) << 32) |
73 ((uint64_t) (p[5]) << 40) |
74 ((uint64_t) (p[6]) << 48) | ((uint64_t) (p[7]) << 56);
78 static BLAKE2_INLINE uint16_t
79 load16 (const void *src)
81 #if defined(NATIVE_LITTLE_ENDIAN)
83 memcpy (&w, src, sizeof w);
86 const uint8_t *p = (const uint8_t *) src;
87 return (uint16_t) (((uint32_t) (p[0]) << 0) | ((uint32_t) (p[1]) << 8));
91 static BLAKE2_INLINE void
92 store16 (void *dst, uint16_t w)
94 #if defined(NATIVE_LITTLE_ENDIAN)
95 memcpy (dst, &w, sizeof w);
97 uint8_t *p = (uint8_t *) dst;
104 static BLAKE2_INLINE void
105 store32 (void *dst, uint32_t w)
107 #if defined(NATIVE_LITTLE_ENDIAN)
108 memcpy (dst, &w, sizeof w);
110 uint8_t *p = (uint8_t *) dst;
111 p[0] = (uint8_t) (w >> 0);
112 p[1] = (uint8_t) (w >> 8);
113 p[2] = (uint8_t) (w >> 16);
114 p[3] = (uint8_t) (w >> 24);
118 static BLAKE2_INLINE void
119 store64 (void *dst, uint64_t w)
121 #if defined(NATIVE_LITTLE_ENDIAN)
122 memcpy (dst, &w, sizeof w);
124 uint8_t *p = (uint8_t *) dst;
125 p[0] = (uint8_t) (w >> 0);
126 p[1] = (uint8_t) (w >> 8);
127 p[2] = (uint8_t) (w >> 16);
128 p[3] = (uint8_t) (w >> 24);
129 p[4] = (uint8_t) (w >> 32);
130 p[5] = (uint8_t) (w >> 40);
131 p[6] = (uint8_t) (w >> 48);
132 p[7] = (uint8_t) (w >> 56);
136 static BLAKE2_INLINE uint64_t
137 load48 (const void *src)
139 const uint8_t *p = (const uint8_t *) src;
140 return ((uint64_t) (p[0]) << 0) |
141 ((uint64_t) (p[1]) << 8) |
142 ((uint64_t) (p[2]) << 16) |
143 ((uint64_t) (p[3]) << 24) |
144 ((uint64_t) (p[4]) << 32) | ((uint64_t) (p[5]) << 40);
147 static BLAKE2_INLINE void
148 store48 (void *dst, uint64_t w)
150 uint8_t *p = (uint8_t *) dst;
151 p[0] = (uint8_t) (w >> 0);
152 p[1] = (uint8_t) (w >> 8);
153 p[2] = (uint8_t) (w >> 16);
154 p[3] = (uint8_t) (w >> 24);
155 p[4] = (uint8_t) (w >> 32);
156 p[5] = (uint8_t) (w >> 40);
159 static BLAKE2_INLINE uint32_t
160 rotr32 (const uint32_t w, const unsigned c)
162 return (w >> c) | (w << (32 - c));
165 static BLAKE2_INLINE uint64_t
166 rotr64 (const uint64_t w, const unsigned c)
168 return (w >> c) | (w << (64 - c));
171 /* prevents compiler optimizing out memset() */
172 static BLAKE2_INLINE void
173 secure_zero_memory (void *v, size_t n)
175 static void *(*const volatile memset_v) (void *, int, size_t) = &memset;
179 #endif //__included_crypto_blake2_impl_h__
182 * fd.io coding-style-patch-verification: ON
185 * eval: (c-set-style "gnu")