1 /* ====================================================================
2 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ==================================================================== */
49 #ifndef OPENSSL_HEADER_MODES_INTERNAL_H
50 #define OPENSSL_HEADER_MODES_INTERNAL_H
52 #include <openssl/base.h>
54 #if defined(__cplusplus)
61 #define STRICT_ALIGNMENT 1
62 #if defined(OPENSSL_X86_64) || defined(OPENSSL_X86) || defined(OPENSSL_AARCH64)
63 #undef STRICT_ALIGNMENT
64 #define STRICT_ALIGNMENT 0
67 #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM)
68 #if defined(__GNUC__) && __GNUC__ >= 2
69 #if defined(OPENSSL_X86_64)
73 asm("bswapq %0" : "+r"(ret)); \
79 asm("bswapl %0" : "+r"(ret)); \
82 #elif defined(OPENSSL_X86)
85 uint32_t lo = (uint64_t)(x) >> 32, hi = (x); \
86 asm("bswapl %0; bswapl %1" : "+r"(hi), "+r"(lo)); \
87 (uint64_t) hi << 32 | lo; \
92 asm("bswapl %0" : "+r"(ret)); \
95 #elif defined(OPENSSL_AARCH64)
99 asm("rev %0,%1" : "=r"(ret) : "r"(x)); \
105 asm("rev %w0,%w1" : "=r"(ret) : "r"(x)); \
108 #elif defined(OPENSSL_ARM) && !defined(STRICT_ALIGNMENT)
111 uint32_t lo = (uint64_t)(x) >> 32, hi = (x); \
112 asm("rev %0,%0; rev %1,%1" : "+r"(hi), "+r"(lo)); \
113 (uint64_t) hi << 32 | lo; \
118 asm("rev %0,%1" : "=r"(ret) : "r"((uint32_t)(x))); \
122 #elif defined(_MSC_VER)
124 #pragma intrinsic(_byteswap_uint64, _byteswap_ulong)
125 #define BSWAP8(x) _byteswap_uint64((uint64_t)(x))
126 #define BSWAP4(x) _byteswap_ulong((uint32_t)(x))
127 #elif defined(OPENSSL_X86)
128 __inline uint32_t _bswap4(uint32_t val) {
132 #define BSWAP4(x) _bswap4(x)
137 #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
138 #define GETU32(p) BSWAP4(*(const uint32_t *)(p))
139 #define PUTU32(p, v) *(uint32_t *)(p) = BSWAP4(v)
142 ((uint32_t)(p)[0] << 24 | (uint32_t)(p)[1] << 16 | (uint32_t)(p)[2] << 8 | (uint32_t)(p)[3])
143 #define PUTU32(p, v) \
144 ((p)[0] = (uint8_t)((v) >> 24), (p)[1] = (uint8_t)((v) >> 16), \
145 (p)[2] = (uint8_t)((v) >> 8), (p)[3] = (uint8_t)(v))
149 /* GCM definitions */
150 typedef struct { uint64_t hi,lo; } u128;
152 struct gcm128_context {
153 /* Following 6 names follow names in GCM specification */
158 size_t t[16 / sizeof(size_t)];
159 } Yi, EKi, EK0, len, Xi, H;
161 /* Relative position of Xi, H and pre-computed Htable is used in some
162 * assembler modules, i.e. don't change the order! */
164 void (*gmult)(uint64_t Xi[2], const u128 Htable[16]);
165 void (*ghash)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
168 unsigned int mres, ares;
173 struct xts128_context {
175 block128_f block1, block2;
178 struct ccm128_context {
188 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
189 /* crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is
191 int crypto_gcm_clmul_enabled(void);
195 #if defined(__cplusplus)
199 #endif /* OPENSSL_HEADER_MODES_INTERNAL_H */