e77ad92fe7501afb50bf610bd3dabc8f7d153c98
[platform/upstream/cryptsetup.git] / lib / crypto_backend / argon2 / blake2 / blake2-impl.h
1 /*
2  * Argon2 reference source code package - reference C implementations
3  *
4  * Copyright 2015
5  * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
6  *
7  * You may use this work under the terms of a Creative Commons CC0 1.0
8  * License/Waiver or the Apache Public License 2.0, at your option. The terms of
9  * these licenses can be found at:
10  *
11  * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
12  * - Apache 2.0        : http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * You should have received a copy of both of these licenses along with this
15  * software. If not, they may be obtained at the above URLs.
16  */
17
18 #ifndef PORTABLE_BLAKE2_IMPL_H
19 #define PORTABLE_BLAKE2_IMPL_H
20
21 #include <stdint.h>
22 #include <string.h>
23
24 #if defined(_MSC_VER)
25 #define BLAKE2_INLINE __inline
26 #elif defined(__GNUC__) || defined(__clang__)
27 #define BLAKE2_INLINE __inline__
28 #else
29 #define BLAKE2_INLINE
30 #endif
31
32 /* Argon2 Team - Begin Code */
33 /*
34    Not an exhaustive list, but should cover the majority of modern platforms
35    Additionally, the code will always be correct---this is only a performance
36    tweak.
37 */
38 #if (defined(__BYTE_ORDER__) &&                                                \
39      (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) ||                           \
40     defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
41     defined(__AARCH64EL__) || defined(__amd64__) || defined(__i386__) ||       \
42     defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) ||                \
43     defined(_M_ARM)
44 #define NATIVE_LITTLE_ENDIAN
45 #endif
46 /* Argon2 Team - End Code */
47
48 static BLAKE2_INLINE uint32_t load32(const void *src) {
49 #if defined(NATIVE_LITTLE_ENDIAN)
50     uint32_t w;
51     memcpy(&w, src, sizeof w);
52     return w;
53 #else
54     const uint8_t *p = (const uint8_t *)src;
55     uint32_t w = *p++;
56     w |= (uint32_t)(*p++) << 8;
57     w |= (uint32_t)(*p++) << 16;
58     w |= (uint32_t)(*p++) << 24;
59     return w;
60 #endif
61 }
62
63 static BLAKE2_INLINE uint64_t load64(const void *src) {
64 #if defined(NATIVE_LITTLE_ENDIAN)
65     uint64_t w;
66     memcpy(&w, src, sizeof w);
67     return w;
68 #else
69     const uint8_t *p = (const uint8_t *)src;
70     uint64_t w = *p++;
71     w |= (uint64_t)(*p++) << 8;
72     w |= (uint64_t)(*p++) << 16;
73     w |= (uint64_t)(*p++) << 24;
74     w |= (uint64_t)(*p++) << 32;
75     w |= (uint64_t)(*p++) << 40;
76     w |= (uint64_t)(*p++) << 48;
77     w |= (uint64_t)(*p++) << 56;
78     return 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++ = (uint8_t)w;
88     w >>= 8;
89     *p++ = (uint8_t)w;
90     w >>= 8;
91     *p++ = (uint8_t)w;
92     w >>= 8;
93     *p++ = (uint8_t)w;
94 #endif
95 }
96
97 static BLAKE2_INLINE void store64(void *dst, uint64_t w) {
98 #if defined(NATIVE_LITTLE_ENDIAN)
99     memcpy(dst, &w, sizeof w);
100 #else
101     uint8_t *p = (uint8_t *)dst;
102     *p++ = (uint8_t)w;
103     w >>= 8;
104     *p++ = (uint8_t)w;
105     w >>= 8;
106     *p++ = (uint8_t)w;
107     w >>= 8;
108     *p++ = (uint8_t)w;
109     w >>= 8;
110     *p++ = (uint8_t)w;
111     w >>= 8;
112     *p++ = (uint8_t)w;
113     w >>= 8;
114     *p++ = (uint8_t)w;
115     w >>= 8;
116     *p++ = (uint8_t)w;
117 #endif
118 }
119
120 static BLAKE2_INLINE uint64_t load48(const void *src) {
121     const uint8_t *p = (const uint8_t *)src;
122     uint64_t w = *p++;
123     w |= (uint64_t)(*p++) << 8;
124     w |= (uint64_t)(*p++) << 16;
125     w |= (uint64_t)(*p++) << 24;
126     w |= (uint64_t)(*p++) << 32;
127     w |= (uint64_t)(*p++) << 40;
128     return w;
129 }
130
131 static BLAKE2_INLINE void store48(void *dst, uint64_t w) {
132     uint8_t *p = (uint8_t *)dst;
133     *p++ = (uint8_t)w;
134     w >>= 8;
135     *p++ = (uint8_t)w;
136     w >>= 8;
137     *p++ = (uint8_t)w;
138     w >>= 8;
139     *p++ = (uint8_t)w;
140     w >>= 8;
141     *p++ = (uint8_t)w;
142     w >>= 8;
143     *p++ = (uint8_t)w;
144 }
145
146 static BLAKE2_INLINE uint32_t rotr32(const uint32_t w, const unsigned c) {
147     return (w >> c) | (w << (32 - c));
148 }
149
150 static BLAKE2_INLINE uint64_t rotr64(const uint64_t w, const unsigned c) {
151     return (w >> c) | (w << (64 - c));
152 }
153
154 #endif