1 /* Copyright (c) 2017, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #ifndef OPENSSL_HEADER_AES_INTERNAL_H
16 #define OPENSSL_HEADER_AES_INTERNAL_H
17
18 #include <stdlib.h>
19
20 #include "../../internal.h"
21
22 #if defined(__cplusplus)
23 extern "C" {
24 #endif
25
26
27 #if !defined(OPENSSL_NO_ASM)
28
29 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
30 #define HWAES
31 #define HWAES_ECB
32
hwaes_capable(void)33 OPENSSL_INLINE int hwaes_capable(void) { return CRYPTO_is_AESNI_capable(); }
34
35 #define VPAES
36 #if defined(OPENSSL_X86_64)
37 #define VPAES_CTR32
38 #endif
39 #define VPAES_CBC
vpaes_capable(void)40 OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_SSSE3_capable(); }
41
42 #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
43 #define HWAES
44
45 OPENSSL_INLINE int hwaes_capable(void) { return CRYPTO_is_ARMv8_AES_capable(); }
46
47 #if defined(OPENSSL_ARM)
48 #define BSAES
49 #define VPAES
50 #define VPAES_CTR32
51 OPENSSL_INLINE int bsaes_capable(void) { return CRYPTO_is_NEON_capable(); }
52 OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_NEON_capable(); }
53 #endif
54
55 #if defined(OPENSSL_AARCH64)
56 #define VPAES
57 #define VPAES_CBC
58 #define VPAES_CTR32
59 OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_NEON_capable(); }
60 #endif
61
62 #endif
63
64 #endif // !NO_ASM
65
66
67 #if defined(HWAES)
68
69 int aes_hw_set_encrypt_key(const uint8_t *user_key, const int bits,
70 AES_KEY *key);
71 int aes_hw_set_decrypt_key(const uint8_t *user_key, const int bits,
72 AES_KEY *key);
73 void aes_hw_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
74 void aes_hw_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
75 void aes_hw_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
76 const AES_KEY *key, uint8_t *ivec, const int enc);
77 void aes_hw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
78 const AES_KEY *key, const uint8_t ivec[16]);
79
80 #else
81
82 // If HWAES isn't defined then we provide dummy functions for each of the hwaes
83 // functions.
hwaes_capable(void)84 OPENSSL_INLINE int hwaes_capable(void) { return 0; }
85
aes_hw_set_encrypt_key(const uint8_t * user_key,int bits,AES_KEY * key)86 OPENSSL_INLINE int aes_hw_set_encrypt_key(const uint8_t *user_key, int bits,
87 AES_KEY *key) {
88 abort();
89 }
90
aes_hw_set_decrypt_key(const uint8_t * user_key,int bits,AES_KEY * key)91 OPENSSL_INLINE int aes_hw_set_decrypt_key(const uint8_t *user_key, int bits,
92 AES_KEY *key) {
93 abort();
94 }
95
aes_hw_encrypt(const uint8_t * in,uint8_t * out,const AES_KEY * key)96 OPENSSL_INLINE void aes_hw_encrypt(const uint8_t *in, uint8_t *out,
97 const AES_KEY *key) {
98 abort();
99 }
100
aes_hw_decrypt(const uint8_t * in,uint8_t * out,const AES_KEY * key)101 OPENSSL_INLINE void aes_hw_decrypt(const uint8_t *in, uint8_t *out,
102 const AES_KEY *key) {
103 abort();
104 }
105
aes_hw_cbc_encrypt(const uint8_t * in,uint8_t * out,size_t length,const AES_KEY * key,uint8_t * ivec,int enc)106 OPENSSL_INLINE void aes_hw_cbc_encrypt(const uint8_t *in, uint8_t *out,
107 size_t length, const AES_KEY *key,
108 uint8_t *ivec, int enc) {
109 abort();
110 }
111
aes_hw_ctr32_encrypt_blocks(const uint8_t * in,uint8_t * out,size_t len,const AES_KEY * key,const uint8_t ivec[16])112 OPENSSL_INLINE void aes_hw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
113 size_t len, const AES_KEY *key,
114 const uint8_t ivec[16]) {
115 abort();
116 }
117
118 #endif // !HWAES
119
120
121 #if defined(HWAES_ECB)
122 void aes_hw_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length,
123 const AES_KEY *key, const int enc);
124 #endif // HWAES_ECB
125
126
127 #if defined(BSAES)
128 // Note |bsaes_cbc_encrypt| requires |enc| to be zero.
129 void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
130 const AES_KEY *key, uint8_t ivec[16], int enc);
131 void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
132 const AES_KEY *key, const uint8_t ivec[16]);
133 // VPAES to BSAES conversions are available on all BSAES platforms.
134 void vpaes_encrypt_key_to_bsaes(AES_KEY *out_bsaes, const AES_KEY *vpaes);
135 void vpaes_decrypt_key_to_bsaes(AES_KEY *out_bsaes, const AES_KEY *vpaes);
136 #else
bsaes_capable(void)137 OPENSSL_INLINE char bsaes_capable(void) { return 0; }
138
139 // On other platforms, bsaes_capable() will always return false and so the
140 // following will never be called.
bsaes_cbc_encrypt(const uint8_t * in,uint8_t * out,size_t length,const AES_KEY * key,uint8_t ivec[16],int enc)141 OPENSSL_INLINE void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out,
142 size_t length, const AES_KEY *key,
143 uint8_t ivec[16], int enc) {
144 abort();
145 }
146
bsaes_ctr32_encrypt_blocks(const uint8_t * in,uint8_t * out,size_t len,const AES_KEY * key,const uint8_t ivec[16])147 OPENSSL_INLINE void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
148 size_t len, const AES_KEY *key,
149 const uint8_t ivec[16]) {
150 abort();
151 }
152
vpaes_encrypt_key_to_bsaes(AES_KEY * out_bsaes,const AES_KEY * vpaes)153 OPENSSL_INLINE void vpaes_encrypt_key_to_bsaes(AES_KEY *out_bsaes,
154 const AES_KEY *vpaes) {
155 abort();
156 }
157
vpaes_decrypt_key_to_bsaes(AES_KEY * out_bsaes,const AES_KEY * vpaes)158 OPENSSL_INLINE void vpaes_decrypt_key_to_bsaes(AES_KEY *out_bsaes,
159 const AES_KEY *vpaes) {
160 abort();
161 }
162 #endif // !BSAES
163
164
165 #if defined(VPAES)
166 // On platforms where VPAES gets defined (just above), then these functions are
167 // provided by asm.
168 int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
169 int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
170
171 void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
172 void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
173
174 #if defined(VPAES_CBC)
175 void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
176 const AES_KEY *key, uint8_t *ivec, int enc);
177 #endif
178 #if defined(VPAES_CTR32)
179 void vpaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
180 const AES_KEY *key, const uint8_t ivec[16]);
181 #endif
182 #else
vpaes_capable(void)183 OPENSSL_INLINE char vpaes_capable(void) { return 0; }
184
185 // On other platforms, vpaes_capable() will always return false and so the
186 // following will never be called.
vpaes_set_encrypt_key(const uint8_t * userKey,int bits,AES_KEY * key)187 OPENSSL_INLINE int vpaes_set_encrypt_key(const uint8_t *userKey, int bits,
188 AES_KEY *key) {
189 abort();
190 }
vpaes_set_decrypt_key(const uint8_t * userKey,int bits,AES_KEY * key)191 OPENSSL_INLINE int vpaes_set_decrypt_key(const uint8_t *userKey, int bits,
192 AES_KEY *key) {
193 abort();
194 }
vpaes_encrypt(const uint8_t * in,uint8_t * out,const AES_KEY * key)195 OPENSSL_INLINE void vpaes_encrypt(const uint8_t *in, uint8_t *out,
196 const AES_KEY *key) {
197 abort();
198 }
vpaes_decrypt(const uint8_t * in,uint8_t * out,const AES_KEY * key)199 OPENSSL_INLINE void vpaes_decrypt(const uint8_t *in, uint8_t *out,
200 const AES_KEY *key) {
201 abort();
202 }
vpaes_cbc_encrypt(const uint8_t * in,uint8_t * out,size_t length,const AES_KEY * key,uint8_t * ivec,int enc)203 OPENSSL_INLINE void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out,
204 size_t length, const AES_KEY *key,
205 uint8_t *ivec, int enc) {
206 abort();
207 }
208 #endif // !VPAES
209
210
211 int aes_nohw_set_encrypt_key(const uint8_t *key, unsigned bits,
212 AES_KEY *aeskey);
213 int aes_nohw_set_decrypt_key(const uint8_t *key, unsigned bits,
214 AES_KEY *aeskey);
215 void aes_nohw_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
216 void aes_nohw_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
217 void aes_nohw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
218 size_t blocks, const AES_KEY *key,
219 const uint8_t ivec[16]);
220 void aes_nohw_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
221 const AES_KEY *key, uint8_t *ivec, const int enc);
222
223
224 #if defined(__cplusplus)
225 } // extern C
226 #endif
227
228 #endif // OPENSSL_HEADER_AES_INTERNAL_H
229