Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / modes / internal.h
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 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) {
129   _asm mov eax, val
130   _asm bswap eax
131 }
132 #define BSWAP4(x) _bswap4(x)
133 #endif
134 #endif
135 #endif
136
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)
140 #else
141 #define GETU32(p) \
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))
146 #endif
147
148
149 /* GCM definitions */
150 typedef struct { uint64_t hi,lo; } u128;
151
152 struct gcm128_context {
153   /* Following 6 names follow names in GCM specification */
154   union {
155     uint64_t u[2];
156     uint32_t d[4];
157     uint8_t c[16];
158     size_t t[16 / sizeof(size_t)];
159   } Yi, EKi, EK0, len, Xi, H;
160
161   /* Relative position of Xi, H and pre-computed Htable is used in some
162    * assembler modules, i.e. don't change the order! */
163   u128 Htable[16];
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,
166                 size_t len);
167
168   unsigned int mres, ares;
169   block128_f block;
170   void *key;
171 };
172
173 struct xts128_context {
174   void *key1, *key2;
175   block128_f block1, block2;
176 };
177
178 struct ccm128_context {
179   union {
180     uint64_t u[2];
181     uint8_t c[16];
182   } nonce, cmac;
183   uint64_t blocks;
184   block128_f block;
185   void *key;
186 };
187
188 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
189 /* crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is
190  * used. */
191 int crypto_gcm_clmul_enabled(void);
192 #endif
193
194
195 #if defined(__cplusplus)
196 } /* extern C */
197 #endif
198
199 #endif /* OPENSSL_HEADER_MODES_INTERNAL_H */