1 // Copyright 2017 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_AES_INTERNAL_H
16 #define OPENSSL_HEADER_CRYPTO_FIPSMODULE_AES_INTERNAL_H
17
18 #include <stdlib.h>
19
20 #include "../bcm_interface.h"
21 #include "../../internal.h"
22
23 extern "C" {
24
25
26 // block128_f is the type of an AES block cipher implementation.
27 //
28 // Unlike upstream OpenSSL, it and the other functions in this file hard-code
29 // |AES_KEY|. It is undefined in C to call a function pointer with anything
30 // other than the original type. Thus we either must match |block128_f| to the
31 // type signature of |BCM_aes_encrypt| and friends or pass in |void*| wrapper
32 // functions.
33 //
34 // These functions are called exclusively with AES, so we use the former.
35 typedef void (*block128_f)(const uint8_t in[16], uint8_t out[16],
36 const AES_KEY *key);
37
38 // ctr128_f is the type of a function that performs CTR-mode encryption.
39 typedef void (*ctr128_f)(const uint8_t *in, uint8_t *out, size_t blocks,
40 const AES_KEY *key, const uint8_t ivec[16]);
41
42 // aes_ctr_set_key initialises |*aes_key| using |key_bytes| bytes from |key|,
43 // where |key_bytes| must either be 16, 24 or 32. If not NULL, |*out_block| is
44 // set to a function that encrypts single blocks. If not NULL, |*out_is_hwaes|
45 // is set to whether the hardware AES implementation was used. It returns a
46 // function for optimised CTR-mode.
47 ctr128_f aes_ctr_set_key(AES_KEY *aes_key, int *out_is_hwaes,
48 block128_f *out_block, const uint8_t *key,
49 size_t key_bytes);
50
51
52 // AES implementations.
53
54 #if !defined(OPENSSL_NO_ASM)
55
56 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
57 #define HWAES
58 #define HWAES_ECB
59
hwaes_capable(void)60 inline int hwaes_capable(void) { return CRYPTO_is_AESNI_capable(); }
61
62 #define VPAES
63 #define VPAES_CBC
vpaes_capable(void)64 inline int vpaes_capable(void) { return CRYPTO_is_SSSE3_capable(); }
65
66 #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
67 #define HWAES
68
hwaes_capable(void)69 inline int hwaes_capable(void) { return CRYPTO_is_ARMv8_AES_capable(); }
70
71 #if defined(OPENSSL_ARM)
72 #define BSAES
73 #define VPAES
bsaes_capable(void)74 inline int bsaes_capable(void) { return CRYPTO_is_NEON_capable(); }
vpaes_capable(void)75 inline int vpaes_capable(void) { return CRYPTO_is_NEON_capable(); }
76 #endif
77
78 #if defined(OPENSSL_AARCH64)
79 #define VPAES
80 #define VPAES_CBC
vpaes_capable(void)81 inline int vpaes_capable(void) { return CRYPTO_is_NEON_capable(); }
82 #endif
83
84 #endif
85
86 #endif // !NO_ASM
87
88
89 #if defined(HWAES)
90
91 int aes_hw_set_encrypt_key(const uint8_t *user_key, int bits, AES_KEY *key);
92 int aes_hw_set_decrypt_key(const uint8_t *user_key, int bits, AES_KEY *key);
93 void aes_hw_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
94 void aes_hw_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
95 void aes_hw_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
96 const AES_KEY *key, uint8_t *ivec, int enc);
97 void aes_hw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
98 const AES_KEY *key, const uint8_t ivec[16]);
99
100 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
101 // On x86 and x86_64, |aes_hw_set_decrypt_key| is implemented in terms of
102 // |aes_hw_set_encrypt_key| and a conversion function.
103 void aes_hw_encrypt_key_to_decrypt_key(AES_KEY *key);
104
105 // There are two variants of this function, one which uses aeskeygenassist
106 // ("base") and one which uses aesenclast + pshufb ("alt"). aesenclast is
107 // overall faster but is slower on some older processors. It doesn't use AVX,
108 // but AVX is used as a proxy to detecting this. See
109 // https://groups.google.com/g/mailing.openssl.dev/c/OuFXwW4NfO8/m/7d2ZXVjkxVkJ
110 //
111 // TODO(davidben): It is unclear if the aeskeygenassist version is still
112 // worthwhile. However, the aesenclast version requires SSSE3. SSSE3 long
113 // predates AES-NI, but it's not clear if AES-NI implies SSSE3. In OpenSSL, the
114 // CCM AES-NI assembly seems to assume it does.
aes_hw_set_encrypt_key_alt_capable(void)115 inline int aes_hw_set_encrypt_key_alt_capable(void) {
116 return hwaes_capable() && CRYPTO_is_SSSE3_capable();
117 }
aes_hw_set_encrypt_key_alt_preferred(void)118 inline int aes_hw_set_encrypt_key_alt_preferred(void) {
119 return hwaes_capable() && CRYPTO_is_AVX_capable();
120 }
121 int aes_hw_set_encrypt_key_base(const uint8_t *user_key, int bits,
122 AES_KEY *key);
123 int aes_hw_set_encrypt_key_alt(const uint8_t *user_key, int bits, AES_KEY *key);
124 #endif // OPENSSL_X86 || OPENSSL_X86_64
125
126 #else
127
128 // If HWAES isn't defined then we provide dummy functions for each of the hwaes
129 // functions.
hwaes_capable(void)130 inline int hwaes_capable(void) { return 0; }
131
aes_hw_set_encrypt_key(const uint8_t * user_key,int bits,AES_KEY * key)132 inline int aes_hw_set_encrypt_key(const uint8_t *user_key, int bits,
133 AES_KEY *key) {
134 abort();
135 }
136
aes_hw_set_decrypt_key(const uint8_t * user_key,int bits,AES_KEY * key)137 inline int aes_hw_set_decrypt_key(const uint8_t *user_key, int bits,
138 AES_KEY *key) {
139 abort();
140 }
141
aes_hw_encrypt(const uint8_t * in,uint8_t * out,const AES_KEY * key)142 inline void aes_hw_encrypt(const uint8_t *in, uint8_t *out,
143 const AES_KEY *key) {
144 abort();
145 }
146
aes_hw_decrypt(const uint8_t * in,uint8_t * out,const AES_KEY * key)147 inline void aes_hw_decrypt(const uint8_t *in, uint8_t *out,
148 const AES_KEY *key) {
149 abort();
150 }
151
aes_hw_cbc_encrypt(const uint8_t * in,uint8_t * out,size_t length,const AES_KEY * key,uint8_t * ivec,int enc)152 inline void aes_hw_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
153 const AES_KEY *key, uint8_t *ivec, int enc) {
154 abort();
155 }
156
aes_hw_ctr32_encrypt_blocks(const uint8_t * in,uint8_t * out,size_t len,const AES_KEY * key,const uint8_t ivec[16])157 inline void aes_hw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
158 size_t len, const AES_KEY *key,
159 const uint8_t ivec[16]) {
160 abort();
161 }
162
163 #endif // !HWAES
164
165
166 #if defined(HWAES_ECB)
167 void aes_hw_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length,
168 const AES_KEY *key, int enc);
169 #endif // HWAES_ECB
170
171
172 #if defined(BSAES)
173 // Note |bsaes_cbc_encrypt| requires |enc| to be zero.
174 void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
175 const AES_KEY *key, uint8_t ivec[16], int enc);
176 void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
177 const AES_KEY *key, const uint8_t ivec[16]);
178 // VPAES to BSAES conversions are available on all BSAES platforms.
179 void vpaes_encrypt_key_to_bsaes(AES_KEY *out_bsaes, const AES_KEY *vpaes);
180 void vpaes_decrypt_key_to_bsaes(AES_KEY *out_bsaes, const AES_KEY *vpaes);
181 void vpaes_ctr32_encrypt_blocks_with_bsaes(const uint8_t *in, uint8_t *out,
182 size_t blocks, const AES_KEY *key,
183 const uint8_t ivec[16]);
184 #else
bsaes_capable(void)185 inline int bsaes_capable(void) { return 0; }
186
187 // On other platforms, bsaes_capable() will always return false and so the
188 // 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)189 inline void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
190 const AES_KEY *key, uint8_t ivec[16], int enc) {
191 abort();
192 }
193
bsaes_ctr32_encrypt_blocks(const uint8_t * in,uint8_t * out,size_t len,const AES_KEY * key,const uint8_t ivec[16])194 inline void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
195 size_t len, const AES_KEY *key,
196 const uint8_t ivec[16]) {
197 abort();
198 }
199
vpaes_encrypt_key_to_bsaes(AES_KEY * out_bsaes,const AES_KEY * vpaes)200 inline void vpaes_encrypt_key_to_bsaes(AES_KEY *out_bsaes,
201 const AES_KEY *vpaes) {
202 abort();
203 }
204
vpaes_decrypt_key_to_bsaes(AES_KEY * out_bsaes,const AES_KEY * vpaes)205 inline void vpaes_decrypt_key_to_bsaes(AES_KEY *out_bsaes,
206 const AES_KEY *vpaes) {
207 abort();
208 }
209 #endif // !BSAES
210
211
212 #if defined(VPAES)
213 // On platforms where VPAES gets defined (just above), then these functions are
214 // provided by asm.
215 int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
216 int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
217
218 void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
219 void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
220
221 #if defined(VPAES_CBC)
222 void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
223 const AES_KEY *key, uint8_t *ivec, int enc);
224 #endif
225 void vpaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
226 const AES_KEY *key, const uint8_t ivec[16]);
227 #else
vpaes_capable(void)228 inline int vpaes_capable(void) { return 0; }
229
230 // On other platforms, vpaes_capable() will always return false and so the
231 // following will never be called.
vpaes_set_encrypt_key(const uint8_t * userKey,int bits,AES_KEY * key)232 inline int vpaes_set_encrypt_key(const uint8_t *userKey, int bits,
233 AES_KEY *key) {
234 abort();
235 }
vpaes_set_decrypt_key(const uint8_t * userKey,int bits,AES_KEY * key)236 inline int vpaes_set_decrypt_key(const uint8_t *userKey, int bits,
237 AES_KEY *key) {
238 abort();
239 }
vpaes_encrypt(const uint8_t * in,uint8_t * out,const AES_KEY * key)240 inline void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
241 abort();
242 }
vpaes_decrypt(const uint8_t * in,uint8_t * out,const AES_KEY * key)243 inline void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
244 abort();
245 }
vpaes_cbc_encrypt(const uint8_t * in,uint8_t * out,size_t length,const AES_KEY * key,uint8_t * ivec,int enc)246 inline void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
247 const AES_KEY *key, uint8_t *ivec, int enc) {
248 abort();
249 }
vpaes_ctr32_encrypt_blocks(const uint8_t * in,uint8_t * out,size_t len,const AES_KEY * key,const uint8_t ivec[16])250 inline void vpaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
251 size_t len, const AES_KEY *key,
252 const uint8_t ivec[16]) {
253 abort();
254 }
255 #endif // !VPAES
256
257
258 int aes_nohw_set_encrypt_key(const uint8_t *key, unsigned bits,
259 AES_KEY *aeskey);
260 int aes_nohw_set_decrypt_key(const uint8_t *key, unsigned bits,
261 AES_KEY *aeskey);
262 void aes_nohw_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
263 void aes_nohw_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
264 void aes_nohw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
265 size_t blocks, const AES_KEY *key,
266 const uint8_t ivec[16]);
267 void aes_nohw_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len,
268 const AES_KEY *key, uint8_t *ivec, int enc);
269
270 // Modes
271
CRYPTO_xor16(uint8_t out[16],const uint8_t a[16],const uint8_t b[16])272 inline void CRYPTO_xor16(uint8_t out[16], const uint8_t a[16],
273 const uint8_t b[16]) {
274 // TODO(davidben): Ideally we'd leave this to the compiler, which could use
275 // vector registers, etc. But the compiler doesn't know that |in| and |out|
276 // cannot partially alias. |restrict| is slightly two strict (we allow exact
277 // aliasing), but perhaps in-place could be a separate function?
278 static_assert(16 % sizeof(crypto_word_t) == 0,
279 "block cannot be evenly divided into words");
280 for (size_t i = 0; i < 16; i += sizeof(crypto_word_t)) {
281 CRYPTO_store_word_le(
282 out + i, CRYPTO_load_word_le(a + i) ^ CRYPTO_load_word_le(b + i));
283 }
284 }
285
286
287 // CTR.
288
289 // CRYPTO_ctr128_encrypt_ctr32 encrypts (or decrypts, it's the same in CTR mode)
290 // |len| bytes from |in| to |out| using |block| in counter mode. There's no
291 // requirement that |len| be a multiple of any value and any partial blocks are
292 // stored in |ecount_buf| and |*num|, which must be zeroed before the initial
293 // call. The counter is a 128-bit, big-endian value in |ivec| and is
294 // incremented by this function. If the counter overflows, it wraps around.
295 // |ctr| must be a function that performs CTR mode but only deals with the lower
296 // 32 bits of the counter.
297 void CRYPTO_ctr128_encrypt_ctr32(const uint8_t *in, uint8_t *out, size_t len,
298 const AES_KEY *key, uint8_t ivec[16],
299 uint8_t ecount_buf[16], unsigned *num,
300 ctr128_f ctr);
301
302
303 // GCM.
304 //
305 // This API differs from the upstream API slightly. The |GCM128_CONTEXT| does
306 // not have a |key| pointer that points to the key as upstream's version does.
307 // Instead, every function takes a |key| parameter. This way |GCM128_CONTEXT|
308 // can be safely copied. Additionally, |gcm_key| is split into a separate
309 // struct.
310
311 // gcm_impl_t specifies an assembly implementation of AES-GCM.
312 enum gcm_impl_t {
313 gcm_separate = 0, // No combined AES-GCM, but may have AES-CTR and GHASH.
314 gcm_x86_aesni,
315 gcm_x86_vaes_avx2,
316 gcm_x86_vaes_avx512,
317 gcm_arm64_aes,
318 };
319
320 typedef struct { uint64_t hi,lo; } u128;
321
322 // gmult_func multiplies |Xi| by the GCM key and writes the result back to
323 // |Xi|.
324 typedef void (*gmult_func)(uint8_t Xi[16], const u128 Htable[16]);
325
326 // ghash_func repeatedly multiplies |Xi| by the GCM key and adds in blocks from
327 // |inp|. The result is written back to |Xi| and the |len| argument must be a
328 // multiple of 16.
329 typedef void (*ghash_func)(uint8_t Xi[16], const u128 Htable[16],
330 const uint8_t *inp, size_t len);
331
332 typedef struct gcm128_key_st {
333 u128 Htable[16];
334 gmult_func gmult;
335 ghash_func ghash;
336 AES_KEY aes;
337
338 ctr128_f ctr;
339 block128_f block;
340 enum gcm_impl_t impl;
341 } GCM128_KEY;
342
343 // GCM128_CONTEXT contains state for a single GCM operation. The structure
344 // should be zero-initialized before use.
345 typedef struct {
346 // The following 5 names follow names in GCM specification
347 uint8_t Yi[16];
348 uint8_t EKi[16];
349 uint8_t EK0[16];
350 struct {
351 uint64_t aad;
352 uint64_t msg;
353 } len;
354 uint8_t Xi[16];
355 unsigned mres, ares;
356 } GCM128_CONTEXT;
357
358 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
359 // crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is
360 // used.
361 int crypto_gcm_clmul_enabled(void);
362 #endif
363
364 // CRYPTO_ghash_init writes a precomputed table of powers of |gcm_key| to
365 // |out_table| and sets |*out_mult| and |*out_hash| to (potentially hardware
366 // accelerated) functions for performing operations in the GHASH field.
367 void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash,
368 u128 out_table[16], const uint8_t gcm_key[16]);
369
370 // CRYPTO_gcm128_init_aes_key initialises |gcm_key| to with AES key |key|.
371 void CRYPTO_gcm128_init_aes_key(GCM128_KEY *gcm_key, const uint8_t *key,
372 size_t key_bytes);
373
374 // CRYPTO_gcm128_init_ctx initializes |ctx| to encrypt with |key| and |iv|.
375 void CRYPTO_gcm128_init_ctx(const GCM128_KEY *key, GCM128_CONTEXT *ctx,
376 const uint8_t *iv, size_t iv_len);
377
378 // CRYPTO_gcm128_aad adds to the authenticated data for an instance of GCM.
379 // This must be called before and data is encrypted. |key| must be the same
380 // value that was passed to |CRYPTO_gcm128_init_ctx|. It returns one on success
381 // and zero otherwise.
382 int CRYPTO_gcm128_aad(const GCM128_KEY *key, GCM128_CONTEXT *ctx,
383 const uint8_t *aad, size_t aad_len);
384
385 // CRYPTO_gcm128_encrypt encrypts |len| bytes from |in| to |out|. |key| must be
386 // the same value that was passed to |CRYPTO_gcm128_init_ctx|. It returns one on
387 // success and zero otherwise.
388 int CRYPTO_gcm128_encrypt(const GCM128_KEY *key, GCM128_CONTEXT *ctx,
389 const uint8_t *in, uint8_t *out, size_t len);
390
391 // CRYPTO_gcm128_decrypt decrypts |len| bytes from |in| to |out|. |key| must be
392 // the same value that was passed to |CRYPTO_gcm128_init_ctx|. It returns one on
393 // success and zero otherwise.
394 int CRYPTO_gcm128_decrypt(const GCM128_KEY *key, GCM128_CONTEXT *ctx,
395 const uint8_t *in, uint8_t *out, size_t len);
396
397 // CRYPTO_gcm128_finish calculates the authenticator and compares it against
398 // |len| bytes of |tag|. |key| must be the same value that was passed to
399 // |CRYPTO_gcm128_init_ctx|. It returns one on success and zero otherwise.
400 int CRYPTO_gcm128_finish(const GCM128_KEY *key, GCM128_CONTEXT *ctx,
401 const uint8_t *tag, size_t len);
402
403 // CRYPTO_gcm128_tag calculates the authenticator and copies it into |tag|.
404 // The minimum of |len| and 16 bytes are copied into |tag|. |key| must be the
405 // same value that was passed to |CRYPTO_gcm128_init_ctx|.
406 void CRYPTO_gcm128_tag(const GCM128_KEY *key, GCM128_CONTEXT *ctx, uint8_t *tag,
407 size_t len);
408
409
410 // GCM assembly.
411
412 void gcm_init_nohw(u128 Htable[16], const uint64_t H[2]);
413 void gcm_gmult_nohw(uint8_t Xi[16], const u128 Htable[16]);
414 void gcm_ghash_nohw(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp,
415 size_t len);
416
417 #if !defined(OPENSSL_NO_ASM)
418
419 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
420 #define GCM_FUNCREF
421 void gcm_init_clmul(u128 Htable[16], const uint64_t Xi[2]);
422 void gcm_gmult_clmul(uint8_t Xi[16], const u128 Htable[16]);
423 void gcm_ghash_clmul(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp,
424 size_t len);
425
426 void gcm_init_ssse3(u128 Htable[16], const uint64_t Xi[2]);
427 void gcm_gmult_ssse3(uint8_t Xi[16], const u128 Htable[16]);
428 void gcm_ghash_ssse3(uint8_t Xi[16], const u128 Htable[16], const uint8_t *in,
429 size_t len);
430
431 #if defined(OPENSSL_X86_64)
432 #define GHASH_ASM_X86_64
433 void gcm_init_avx(u128 Htable[16], const uint64_t Xi[2]);
434 void gcm_gmult_avx(uint8_t Xi[16], const u128 Htable[16]);
435 void gcm_ghash_avx(uint8_t Xi[16], const u128 Htable[16], const uint8_t *in,
436 size_t len);
437
438 #define HW_GCM
439 size_t aesni_gcm_encrypt(const uint8_t *in, uint8_t *out, size_t len,
440 const AES_KEY *key, uint8_t ivec[16],
441 const u128 Htable[16], uint8_t Xi[16]);
442 size_t aesni_gcm_decrypt(const uint8_t *in, uint8_t *out, size_t len,
443 const AES_KEY *key, uint8_t ivec[16],
444 const u128 Htable[16], uint8_t Xi[16]);
445
446 void gcm_init_vpclmulqdq_avx2(u128 Htable[16], const uint64_t H[2]);
447 void gcm_gmult_vpclmulqdq_avx2(uint8_t Xi[16], const u128 Htable[16]);
448 void gcm_ghash_vpclmulqdq_avx2(uint8_t Xi[16], const u128 Htable[16],
449 const uint8_t *in, size_t len);
450 void aes_gcm_enc_update_vaes_avx2(const uint8_t *in, uint8_t *out, size_t len,
451 const AES_KEY *key, const uint8_t ivec[16],
452 const u128 Htable[16], uint8_t Xi[16]);
453 void aes_gcm_dec_update_vaes_avx2(const uint8_t *in, uint8_t *out, size_t len,
454 const AES_KEY *key, const uint8_t ivec[16],
455 const u128 Htable[16], uint8_t Xi[16]);
456
457 void gcm_init_vpclmulqdq_avx512(u128 Htable[16], const uint64_t H[2]);
458 void gcm_gmult_vpclmulqdq_avx512(uint8_t Xi[16], const u128 Htable[16]);
459 void gcm_ghash_vpclmulqdq_avx512(uint8_t Xi[16], const u128 Htable[16],
460 const uint8_t *in, size_t len);
461 void aes_gcm_enc_update_vaes_avx512(const uint8_t *in, uint8_t *out, size_t len,
462 const AES_KEY *key, const uint8_t ivec[16],
463 const u128 Htable[16], uint8_t Xi[16]);
464 void aes_gcm_dec_update_vaes_avx512(const uint8_t *in, uint8_t *out, size_t len,
465 const AES_KEY *key, const uint8_t ivec[16],
466 const u128 Htable[16], uint8_t Xi[16]);
467
468 #endif // OPENSSL_X86_64
469
470 #if defined(OPENSSL_X86)
471 #define GHASH_ASM_X86
472 #endif // OPENSSL_X86
473
474 #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
475
476 #define GHASH_ASM_ARM
477 #define GCM_FUNCREF
478
gcm_pmull_capable(void)479 inline int gcm_pmull_capable(void) { return CRYPTO_is_ARMv8_PMULL_capable(); }
480
481 void gcm_init_v8(u128 Htable[16], const uint64_t H[2]);
482 void gcm_gmult_v8(uint8_t Xi[16], const u128 Htable[16]);
483 void gcm_ghash_v8(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp,
484 size_t len);
485
gcm_neon_capable(void)486 inline int gcm_neon_capable(void) { return CRYPTO_is_NEON_capable(); }
487
488 void gcm_init_neon(u128 Htable[16], const uint64_t H[2]);
489 void gcm_gmult_neon(uint8_t Xi[16], const u128 Htable[16]);
490 void gcm_ghash_neon(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp,
491 size_t len);
492
493 #if defined(OPENSSL_AARCH64)
494 #define HW_GCM
495 // These functions are defined in aesv8-gcm-armv8.pl.
496 void aes_gcm_enc_kernel(const uint8_t *in, uint64_t in_bits, void *out,
497 void *Xi, uint8_t *ivec, const AES_KEY *key,
498 const u128 Htable[16]);
499 void aes_gcm_dec_kernel(const uint8_t *in, uint64_t in_bits, void *out,
500 void *Xi, uint8_t *ivec, const AES_KEY *key,
501 const u128 Htable[16]);
502 #endif
503
504 #endif
505 #endif // OPENSSL_NO_ASM
506
507
508 // CBC.
509
510 // cbc128_f is the type of a function that performs CBC-mode encryption.
511 typedef void (*cbc128_f)(const uint8_t *in, uint8_t *out, size_t len,
512 const AES_KEY *key, uint8_t ivec[16], int enc);
513
514 // CRYPTO_cbc128_encrypt encrypts |len| bytes from |in| to |out| using the
515 // given IV and block cipher in CBC mode. The input need not be a multiple of
516 // 128 bits long, but the output will round up to the nearest 128 bit multiple,
517 // zero padding the input if needed. The IV will be updated on return.
518 void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
519 const AES_KEY *key, uint8_t ivec[16],
520 block128_f block);
521
522 // CRYPTO_cbc128_decrypt decrypts |len| bytes from |in| to |out| using the
523 // given IV and block cipher in CBC mode. If |len| is not a multiple of 128
524 // bits then only that many bytes will be written, but a multiple of 128 bits
525 // is always read from |in|. The IV will be updated on return.
526 void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
527 const AES_KEY *key, uint8_t ivec[16],
528 block128_f block);
529
530
531 // OFB.
532
533 // CRYPTO_ofb128_encrypt encrypts (or decrypts, it's the same with OFB mode)
534 // |len| bytes from |in| to |out| using |block| in OFB mode. There's no
535 // requirement that |len| be a multiple of any value and any partial blocks are
536 // stored in |ivec| and |*num|, the latter must be zero before the initial
537 // call.
538 void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
539 const AES_KEY *key, uint8_t ivec[16], unsigned *num,
540 block128_f block);
541
542
543 // CFB.
544
545 // CRYPTO_cfb128_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
546 // from |in| to |out| using |block| in CFB mode. There's no requirement that
547 // |len| be a multiple of any value and any partial blocks are stored in |ivec|
548 // and |*num|, the latter must be zero before the initial call.
549 void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
550 const AES_KEY *key, uint8_t ivec[16], unsigned *num,
551 int enc, block128_f block);
552
553 // CRYPTO_cfb128_8_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
554 // from |in| to |out| using |block| in CFB-8 mode. Prior to the first call
555 // |num| should be set to zero.
556 void CRYPTO_cfb128_8_encrypt(const uint8_t *in, uint8_t *out, size_t len,
557 const AES_KEY *key, uint8_t ivec[16],
558 unsigned *num, int enc, block128_f block);
559
560 // CRYPTO_cfb128_1_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
561 // from |in| to |out| using |block| in CFB-1 mode. Prior to the first call
562 // |num| should be set to zero.
563 void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits,
564 const AES_KEY *key, uint8_t ivec[16],
565 unsigned *num, int enc, block128_f block);
566
567 size_t CRYPTO_cts128_encrypt_block(const uint8_t *in, uint8_t *out, size_t len,
568 const AES_KEY *key, uint8_t ivec[16],
569 block128_f block);
570
571
572 } // extern C
573
574 #endif // OPENSSL_HEADER_CRYPTO_FIPSMODULE_AES_INTERNAL_H
575