Add blake2 support.
[oweals/openssl.git] / crypto / blake2 / blake2_impl.h
1 /*
2  * BLAKE2 reference source code package - reference C implementations
3  *
4  * Copyright 2012, Samuel Neves <sneves@dei.uc.pt>.
5  * You may use this under the terms of the CC0, the OpenSSL Licence, or the
6  * Apache Public License 2.0, at your option.  The terms of these licenses can
7  * be found at:
8  *
9  * - OpenSSL license   : https://www.openssl.org/source/license.html
10  * - Apache 2.0        : http://www.apache.org/licenses/LICENSE-2.0
11  * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
12  *
13  * More information about the BLAKE2 hash function can be found at
14  * https://blake2.net.
15  */
16
17 /* crypto/blake2/blake2_impl.h */
18
19 #include <stdint.h>
20 #include <string.h>
21
22 static inline uint32_t load32(const void *src)
23 {
24 #if defined(L_ENDIAN)
25     uint32_t w;
26     memcpy(&w, src, sizeof(w));
27     return w;
28 #else
29     const uint8_t *p = (const uint8_t *)src;
30     uint32_t w = *p++;
31     w |= (uint32_t)(*p++) <<  8;
32     w |= (uint32_t)(*p++) << 16;
33     w |= (uint32_t)(*p++) << 24;
34     return w;
35 #endif
36 }
37
38 static inline uint64_t load64(const void *src)
39 {
40 #if defined(L_ENDIAN)
41     uint64_t w;
42     memcpy(&w, src, sizeof(w));
43     return w;
44 #else
45     const uint8_t *p = (const uint8_t *)src;
46     uint64_t w = *p++;
47     w |= (uint64_t)(*p++) <<  8;
48     w |= (uint64_t)(*p++) << 16;
49     w |= (uint64_t)(*p++) << 24;
50     w |= (uint64_t)(*p++) << 32;
51     w |= (uint64_t)(*p++) << 40;
52     w |= (uint64_t)(*p++) << 48;
53     w |= (uint64_t)(*p++) << 56;
54     return w;
55 #endif
56 }
57
58 static inline void store32(void *dst, uint32_t w)
59 {
60 #if defined(L_ENDIAN)
61     memcpy(dst, &w, sizeof(w));
62 #else
63     uint8_t *p = (uint8_t *)dst;
64     *p++ = (uint8_t)w;
65     w >>= 8;
66     *p++ = (uint8_t)w;
67     w >>= 8;
68     *p++ = (uint8_t)w;
69     w >>= 8;
70     *p++ = (uint8_t)w;
71 #endif
72 }
73
74 static inline void store64(void *dst, uint64_t w)
75 {
76 #if defined(L_ENDIAN)
77     memcpy(dst, &w, sizeof(w));
78 #else
79     uint8_t *p = (uint8_t *)dst;
80     *p++ = (uint8_t)w;
81     w >>= 8;
82     *p++ = (uint8_t)w;
83     w >>= 8;
84     *p++ = (uint8_t)w;
85     w >>= 8;
86     *p++ = (uint8_t)w;
87     w >>= 8;
88     *p++ = (uint8_t)w;
89     w >>= 8;
90     *p++ = (uint8_t)w;
91     w >>= 8;
92     *p++ = (uint8_t)w;
93     w >>= 8;
94     *p++ = (uint8_t)w;
95 #endif
96 }
97
98 static inline uint64_t load48(const void *src)
99 {
100     const uint8_t *p = (const uint8_t *)src;
101     uint64_t w = *p++;
102     w |= (uint64_t)(*p++) <<  8;
103     w |= (uint64_t)(*p++) << 16;
104     w |= (uint64_t)(*p++) << 24;
105     w |= (uint64_t)(*p++) << 32;
106     w |= (uint64_t)(*p++) << 40;
107     return w;
108 }
109
110 static inline void store48(void *dst, uint64_t w)
111 {
112     uint8_t *p = (uint8_t *)dst;
113     *p++ = (uint8_t)w;
114     w >>= 8;
115     *p++ = (uint8_t)w;
116     w >>= 8;
117     *p++ = (uint8_t)w;
118     w >>= 8;
119     *p++ = (uint8_t)w;
120     w >>= 8;
121     *p++ = (uint8_t)w;
122     w >>= 8;
123     *p++ = (uint8_t)w;
124 }
125
126 static inline uint32_t rotl32(const uint32_t w, const unsigned c)
127 {
128     return (w << c) | (w >> (32 - c));
129 }
130
131 static inline uint64_t rotl64(const uint64_t w, const unsigned c)
132 {
133     return (w << c) | (w >> (64 - c));
134 }
135
136 static inline uint32_t rotr32(const uint32_t w, const unsigned c)
137 {
138     return (w >> c) | (w << (32 - c));
139 }
140
141 static inline uint64_t rotr64(const uint64_t w, const unsigned c)
142 {
143     return (w >> c) | (w << (64 - c));
144 }