1 /* ==================================================================== 2 * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 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 14 * distribution. 15 * 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/)" 20 * 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. 25 * 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. 29 * 30 * 6. Redistributions of any form whatsoever must retain the following 31 * acknowledgment: 32 * "This product includes software developed by the OpenSSL Project 33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 34 * 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 * ==================================================================== */ 48 49 #ifndef OPENSSL_HEADER_MODES_INTERNAL_H 50 #define OPENSSL_HEADER_MODES_INTERNAL_H 51 52 #include <openssl/base.h> 53 54 #if defined(__cplusplus) 55 extern "C" { 56 #endif 57 58 59 #define asm __asm__ 60 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 65 #endif 66 67 #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) 68 #if defined(__GNUC__) && __GNUC__ >= 2 69 #if defined(OPENSSL_X86_64) 70 #define BSWAP8(x) \ 71 ({ \ 72 uint64_t ret = (x); \ 73 asm("bswapq %0" : "+r"(ret)); \ 74 ret; \ 75 }) 76 #define BSWAP4(x) \ 77 ({ \ 78 uint32_t ret = (x); \ 79 asm("bswapl %0" : "+r"(ret)); \ 80 ret; \ 81 }) 82 #elif defined(OPENSSL_X86) 83 #define BSWAP8(x) \ 84 ({ \ 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; \ 88 }) 89 #define BSWAP4(x) \ 90 ({ \ 91 uint32_t ret = (x); \ 92 asm("bswapl %0" : "+r"(ret)); \ 93 ret; \ 94 }) 95 #elif defined(OPENSSL_AARCH64) 96 #define BSWAP8(x) \ 97 ({ \ 98 uint64_t ret; \ 99 asm("rev %0,%1" : "=r"(ret) : "r"(x)); \ 100 ret; \ 101 }) 102 #define BSWAP4(x) \ 103 ({ \ 104 uint32_t ret; \ 105 asm("rev %w0,%w1" : "=r"(ret) : "r"(x)); \ 106 ret; \ 107 }) 108 #elif defined(OPENSSL_ARM) && !defined(STRICT_ALIGNMENT) 109 #define BSWAP8(x) \ 110 ({ \ 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; \ 114 }) 115 #define BSWAP4(x) \ 116 ({ \ 117 uint32_t ret; \ 118 asm("rev %0,%1" : "=r"(ret) : "r"((uint32_t)(x))); \ 119 ret; \ 120 }) 121 #endif 122 #elif defined(_MSC_VER) 123 #if _MSC_VER >= 1300 124 #pragma warning(push, 3) 125 #include <intrin.h> 126 #pragma warning(pop) 127 #pragma intrinsic(_byteswap_uint64, _byteswap_ulong) 128 #define BSWAP8(x) _byteswap_uint64((uint64_t)(x)) 129 #define BSWAP4(x) _byteswap_ulong((uint32_t)(x)) 130 #elif defined(OPENSSL_X86) 131 __inline uint32_t _bswap4(uint32_t val) { 132 _asm mov eax, val 133 _asm bswap eax 134 } 135 #define BSWAP4(x) _bswap4(x) 136 #endif 137 #endif 138 #endif 139 140 #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT) 141 #define GETU32(p) BSWAP4(*(const uint32_t *)(p)) 142 #define PUTU32(p, v) *(uint32_t *)(p) = BSWAP4(v) 143 #else 144 #define GETU32(p) \ 145 ((uint32_t)(p)[0] << 24 | (uint32_t)(p)[1] << 16 | (uint32_t)(p)[2] << 8 | (uint32_t)(p)[3]) 146 #define PUTU32(p, v) \ 147 ((p)[0] = (uint8_t)((v) >> 24), (p)[1] = (uint8_t)((v) >> 16), \ 148 (p)[2] = (uint8_t)((v) >> 8), (p)[3] = (uint8_t)(v)) 149 #endif 150 151 152 /* GCM definitions */ 153 typedef struct { uint64_t hi,lo; } u128; 154 155 struct gcm128_context { 156 /* Following 6 names follow names in GCM specification */ 157 union { 158 uint64_t u[2]; 159 uint32_t d[4]; 160 uint8_t c[16]; 161 size_t t[16 / sizeof(size_t)]; 162 } Yi, EKi, EK0, len, Xi, H; 163 164 /* Relative position of Xi, H and pre-computed Htable is used in some 165 * assembler modules, i.e. don't change the order! */ 166 u128 Htable[16]; 167 void (*gmult)(uint64_t Xi[2], const u128 Htable[16]); 168 void (*ghash)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp, 169 size_t len); 170 171 unsigned int mres, ares; 172 block128_f block; 173 void *key; 174 }; 175 176 struct xts128_context { 177 void *key1, *key2; 178 block128_f block1, block2; 179 }; 180 181 struct ccm128_context { 182 union { 183 uint64_t u[2]; 184 uint8_t c[16]; 185 } nonce, cmac; 186 uint64_t blocks; 187 block128_f block; 188 void *key; 189 }; 190 191 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) 192 /* crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is 193 * used. */ 194 int crypto_gcm_clmul_enabled(void); 195 #endif 196 197 198 #if defined(__cplusplus) 199 } /* extern C */ 200 #endif 201 202 #endif /* OPENSSL_HEADER_MODES_INTERNAL_H */ 203