Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/modes/internal.h

raw
1 // Copyright (c) 2008 The OpenSSL Project. All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifndef OPENSSL_HEADER_MODES_INTERNAL_H
5 #define OPENSSL_HEADER_MODES_INTERNAL_H
6
7 #include <openssl/base.h>
8
9 #include <openssl/aes.h>
10
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "../../internal.h"
16 #include "../cpucap/internal.h"
17
18 #if defined(__cplusplus)
19 extern "C" {
20 #endif
21
22 // The maximum permitted number of cipher blocks per data unit in XTS mode.
23 // Reference IEEE Std 1619-2018.
24 #define XTS_MAX_BLOCKS_PER_DATA_UNIT (1<<20)
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 |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 OPENSSL_INLINE void CRYPTO_xor16(uint8_t out[16], const uint8_t a[16],
39 const uint8_t b[16]) {
40 // TODO(davidben): Ideally we'd leave this to the compiler, which could use
41 // vector registers, etc. But the compiler doesn't know that |in| and |out|
42 // cannot partially alias. |restrict| is slightly two strict (we allow exact
43 // aliasing), but perhaps in-place could be a separate function?
44 OPENSSL_STATIC_ASSERT(16 % sizeof(crypto_word_t) == 0,
45 block_cannot_be_evenly_divided_into_crypto_word_t)
46 for (size_t i = 0; i < 16; i += sizeof(crypto_word_t)) {
47 CRYPTO_store_word_le(
48 out + i, CRYPTO_load_word_le(a + i) ^ CRYPTO_load_word_le(b + i));
49 }
50 }
51
52
53 // CTR.
54
55 // ctr128_f is the type of a function that performs CTR-mode encryption.
56 typedef void (*ctr128_f)(const uint8_t *in, uint8_t *out, size_t blocks,
57 const AES_KEY *key, const uint8_t ivec[16]);
58
59 // CRYPTO_ctr128_encrypt encrypts (or decrypts, it's the same in CTR mode)
60 // |len| bytes from |in| to |out| using |block| in counter mode. There's no
61 // requirement that |len| be a multiple of any value and any partial blocks are
62 // stored in |ecount_buf| and |*num|, which must be zeroed before the initial
63 // call. The counter is a 128-bit, big-endian value in |ivec| and is
64 // incremented by this function.
65 void CRYPTO_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
66 const AES_KEY *key, uint8_t ivec[16],
67 uint8_t ecount_buf[16], unsigned *num,
68 block128_f block);
69
70 // CRYPTO_ctr128_encrypt_ctr32 acts like |CRYPTO_ctr128_encrypt| but takes
71 // |ctr|, a function that performs CTR mode but only deals with the lower 32
72 // bits of the counter. This is useful when |ctr| can be an optimised
73 // function.
74 void CRYPTO_ctr128_encrypt_ctr32(const uint8_t *in, uint8_t *out, size_t len,
75 const AES_KEY *key, uint8_t ivec[16],
76 uint8_t ecount_buf[16], unsigned *num,
77 ctr128_f ctr);
78
79
80 // GCM.
81 //
82 // This API differs from the upstream API slightly. The |GCM128_CONTEXT| does
83 // not have a |key| pointer that points to the key as upstream's version does.
84 // Instead, every function takes a |key| parameter. This way |GCM128_CONTEXT|
85 // can be safely copied. Additionally, |gcm_key| is split into a separate
86 // struct.
87
88 typedef struct { uint64_t hi,lo; } u128;
89
90 // gmult_func multiplies |Xi| by the GCM key and writes the result back to
91 // |Xi|.
92 typedef void (*gmult_func)(uint8_t Xi[16], const u128 Htable[16]);
93
94 // ghash_func repeatedly multiplies |Xi| by the GCM key and adds in blocks from
95 // |inp|. The result is written back to |Xi| and the |len| argument must be a
96 // multiple of 16.
97 typedef void (*ghash_func)(uint8_t Xi[16], const u128 Htable[16],
98 const uint8_t *inp, size_t len);
99
100 typedef struct gcm128_key_st {
101 // |gcm_*_ssse3| require a 16-byte-aligned |Htable| when hashing data, but not
102 // initialization. |GCM128_KEY| is not itself aligned to simplify embedding in
103 // |EVP_AEAD_CTX|, but |Htable|'s offset must be a multiple of 16.
104 // TODO(crbug.com/boringssl/604): Revisit this.
105 u128 Htable[16];
106 gmult_func gmult;
107 ghash_func ghash;
108
109 block128_f block;
110
111 // use_hw_gcm_crypt is true if this context should use platform-specific
112 // assembly to process GCM data.
113 unsigned use_hw_gcm_crypt:1;
114 } GCM128_KEY;
115
116 // GCM128_CONTEXT contains state for a single GCM operation. The structure
117 // should be zero-initialized before use.
118 typedef struct {
119 // The following 5 names follow names in GCM specification
120 uint8_t Yi[16];
121 uint8_t EKi[16];
122 uint8_t EK0[16];
123 struct {
124 uint64_t aad;
125 uint64_t msg;
126 } len;
127 uint8_t Xi[16];
128
129 // |gcm_*_ssse3| require |Htable| to be 16-byte-aligned.
130 // TODO(crbug.com/boringssl/604): Revisit this.
131 alignas(16) GCM128_KEY gcm_key;
132
133 unsigned mres, ares;
134 } GCM128_CONTEXT;
135
136 typedef struct xts128_context {
137 AES_KEY *key1, *key2;
138 block128_f block1, block2;
139 } XTS128_CONTEXT;
140
141 typedef struct {
142 union {
143 double align;
144 AES_KEY ks;
145 } ks1, ks2; // AES key schedules to use
146 XTS128_CONTEXT xts;
147 } EVP_AES_XTS_CTX;
148
149 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
150 // crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is
151 // used.
152 int crypto_gcm_clmul_enabled(void);
153 // crypto_gcm_avx512_enabled returns one if the AVX512 VAES + VPCLMULQDQ
154 // implementation of GCM is used.
155 int crypto_gcm_avx512_enabled(void);
156 #endif
157
158 // CRYPTO_ghash_init writes a precomputed table of powers of |gcm_key| to
159 // |out_table| and sets |*out_mult| and |*out_hash| to (potentially hardware
160 // accelerated) functions for performing operations in the GHASH field. If the
161 // AVX implementation was used |*out_is_avx| will be true.
162 void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash,
163 u128 out_table[16], int *out_is_avx,
164 const uint8_t gcm_key[16]);
165
166 // CRYPTO_gcm128_init_key initialises |gcm_key| to use |block| (typically AES)
167 // with the given key. |block_is_hwaes| is one if |block| is |aes_hw_encrypt|.
168 OPENSSL_EXPORT void CRYPTO_gcm128_init_key(GCM128_KEY *gcm_key,
169 const AES_KEY *key, block128_f block,
170 int block_is_hwaes);
171
172 // CRYPTO_gcm128_setiv sets the IV (nonce) for |ctx|. The |key| must be the
173 // same key that was passed to |CRYPTO_gcm128_init|.
174 OPENSSL_EXPORT void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const AES_KEY *key,
175 const uint8_t *iv, size_t iv_len);
176
177 // CRYPTO_gcm128_aad sets the authenticated data for an instance of GCM.
178 // This must be called before and data is encrypted. It returns one on success
179 // and zero otherwise.
180 OPENSSL_EXPORT int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad,
181 size_t len);
182
183 // CRYPTO_gcm128_encrypt encrypts |len| bytes from |in| to |out|. The |key|
184 // must be the same key that was passed to |CRYPTO_gcm128_init|. It returns one
185 // on success and zero otherwise.
186 OPENSSL_EXPORT int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
187 const AES_KEY *key, const uint8_t *in,
188 uint8_t *out, size_t len);
189
190 // CRYPTO_gcm128_decrypt decrypts |len| bytes from |in| to |out|. The |key|
191 // must be the same key that was passed to |CRYPTO_gcm128_init|. It returns one
192 // on success and zero otherwise.
193 OPENSSL_EXPORT int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
194 const AES_KEY *key, const uint8_t *in,
195 uint8_t *out, size_t len);
196
197 // CRYPTO_gcm128_encrypt_ctr32 encrypts |len| bytes from |in| to |out| using
198 // a CTR function that only handles the bottom 32 bits of the nonce, like
199 // |CRYPTO_ctr128_encrypt_ctr32|. The |key| must be the same key that was
200 // passed to |CRYPTO_gcm128_init|. It returns one on success and zero
201 // otherwise.
202 OPENSSL_EXPORT int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
203 const AES_KEY *key,
204 const uint8_t *in, uint8_t *out,
205 size_t len, ctr128_f stream);
206
207 // CRYPTO_gcm128_decrypt_ctr32 decrypts |len| bytes from |in| to |out| using
208 // a CTR function that only handles the bottom 32 bits of the nonce, like
209 // |CRYPTO_ctr128_encrypt_ctr32|. The |key| must be the same key that was
210 // passed to |CRYPTO_gcm128_init|. It returns one on success and zero
211 // otherwise.
212 OPENSSL_EXPORT int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
213 const AES_KEY *key,
214 const uint8_t *in, uint8_t *out,
215 size_t len, ctr128_f stream);
216
217 // CRYPTO_gcm128_finish calculates the authenticator and compares it against
218 // |len| bytes of |tag|. It returns one on success and zero otherwise.
219 OPENSSL_EXPORT int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag,
220 size_t len);
221
222 // CRYPTO_gcm128_tag calculates the authenticator and copies it into |tag|.
223 // The minimum of |len| and 16 bytes are copied into |tag|.
224 OPENSSL_EXPORT void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, uint8_t *tag,
225 size_t len);
226
227
228 // GCM assembly.
229
230 void gcm_init_nohw(u128 Htable[16], const uint64_t H[2]);
231 void gcm_gmult_nohw(uint8_t Xi[16], const u128 Htable[16]);
232 void gcm_ghash_nohw(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp,
233 size_t len);
234
235 #if !defined(OPENSSL_NO_ASM)
236
237 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
238 #define GCM_FUNCREF
239 void gcm_init_clmul(u128 Htable[16], const uint64_t Xi[2]);
240 void gcm_gmult_clmul(uint8_t Xi[16], const u128 Htable[16]);
241 void gcm_ghash_clmul(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp,
242 size_t len);
243
244 // |gcm_gmult_ssse3| and |gcm_ghash_ssse3| require |Htable| to be
245 // 16-byte-aligned, but |gcm_init_ssse3| does not.
246 void gcm_init_ssse3(u128 Htable[16], const uint64_t Xi[2]);
247 void gcm_gmult_ssse3(uint8_t Xi[16], const u128 Htable[16]);
248 void gcm_ghash_ssse3(uint8_t Xi[16], const u128 Htable[16], const uint8_t *in,
249 size_t len);
250
251 #if defined(OPENSSL_X86_64) && !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_AVX)
252 #define GHASH_ASM_X86_64
253 void gcm_init_avx(u128 Htable[16], const uint64_t Xi[2]);
254 void gcm_gmult_avx(uint8_t Xi[16], const u128 Htable[16]);
255 void gcm_ghash_avx(uint8_t Xi[16], const u128 Htable[16], const uint8_t *in,
256 size_t len);
257 #if !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX)
258 void gcm_init_avx512(u128 Htable[16], const uint64_t Xi[2]);
259 void gcm_gmult_avx512(uint8_t Xi[2], const u128 Htable[16]);
260 void gcm_ghash_avx512(uint8_t Xi[2], const u128 Htable[16], const uint8_t *in,
261 size_t len);
262 #endif
263 #define HW_GCM
264 size_t aesni_gcm_encrypt(const uint8_t *in, uint8_t *out, size_t len,
265 const AES_KEY *key, uint8_t ivec[16],
266 const u128 Htable[16], uint8_t Xi[16]);
267 size_t aesni_gcm_decrypt(const uint8_t *in, uint8_t *out, size_t len,
268 const AES_KEY *key, uint8_t ivec[16],
269 const u128 Htable[16], uint8_t Xi[16]);
270 void gcm_setiv_avx512(const AES_KEY *key, const GCM128_CONTEXT *ctx,
271 const uint8_t *iv, size_t ivlen);
272 void aes_gcm_encrypt_avx512(const AES_KEY *key, const GCM128_CONTEXT *ctx,
273 unsigned *pblocklen, const uint8_t *in, size_t len,
274 uint8_t *out);
275 void aes_gcm_decrypt_avx512(const AES_KEY *key, const GCM128_CONTEXT *ctx,
276 unsigned *pblocklen, const uint8_t *in, size_t len,
277 uint8_t *out);
278 #endif // OPENSSL_X86_64 && !MY_ASSEMBLER_IS_TOO_OLD_FOR_AVX
279
280 #if defined(OPENSSL_X86)
281 #define GHASH_ASM_X86
282 #endif // OPENSSL_X86
283
284 #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
285
286 #define GHASH_ASM_ARM
287 #define GCM_FUNCREF
288
289 OPENSSL_INLINE int gcm_pmull_capable(void) {
290 return CRYPTO_is_ARMv8_PMULL_capable();
291 }
292
293 void gcm_init_v8(u128 Htable[16], const uint64_t H[2]);
294 void gcm_gmult_v8(uint8_t Xi[16], const u128 Htable[16]);
295 void gcm_ghash_v8(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp,
296 size_t len);
297
298 OPENSSL_INLINE int gcm_neon_capable(void) { return CRYPTO_is_NEON_capable(); }
299
300 void gcm_init_neon(u128 Htable[16], const uint64_t H[2]);
301 void gcm_gmult_neon(uint8_t Xi[16], const u128 Htable[16]);
302 void gcm_ghash_neon(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp,
303 size_t len);
304
305 #if defined(OPENSSL_AARCH64)
306 #define HW_GCM
307 // Note that in the argument list of the following functions,
308 // - the length is provided in bits (not bytes)
309 // - the order of arguments is different from that of |aesni_gcm_encrypt|.
310
311 // These functions are defined in aesv8-gcm-armv8.pl.
312 void aes_gcm_enc_kernel(const uint8_t *in, uint64_t in_bits, void *out,
313 void *Xi, uint8_t *ivec, const AES_KEY *key,
314 const u128 Htable[16]);
315 void aes_gcm_dec_kernel(const uint8_t *in, uint64_t in_bits, void *out,
316 void *Xi, uint8_t *ivec, const AES_KEY *key,
317 const u128 Htable[16]);
318
319 // These functions are defined in aesv8-gcm-armv8-unroll8.pl.
320 // They take input length in BITS and return number of BYTES processed.
321 size_t aesv8_gcm_8x_enc_128(const uint8_t *in, size_t bit_len, uint8_t *out,
322 uint8_t *Xi, uint8_t ivec[16], const AES_KEY *key,
323 const u128 Htable[16]);
324 size_t aesv8_gcm_8x_dec_128(const uint8_t *in, size_t bit_len, uint8_t *out,
325 uint8_t *Xi, uint8_t ivec[16], const AES_KEY *key,
326 const u128 Htable[16]);
327 size_t aesv8_gcm_8x_enc_192(const uint8_t *in, size_t bit_len, uint8_t *out,
328 uint8_t *Xi, uint8_t ivec[16], const AES_KEY *key,
329 const u128 Htable[16]);
330 size_t aesv8_gcm_8x_dec_192(const uint8_t *in, size_t bit_len, uint8_t *out,
331 uint8_t *Xi, uint8_t ivec[16], const AES_KEY *key,
332 const u128 Htable[16]);
333 size_t aesv8_gcm_8x_enc_256(const uint8_t *in, size_t bit_len, uint8_t *out,
334 uint8_t *Xi, uint8_t ivec[16], const AES_KEY *key,
335 const u128 Htable[16]);
336 size_t aesv8_gcm_8x_dec_256(const uint8_t *in, size_t bit_len, uint8_t *out,
337 uint8_t *Xi, uint8_t ivec[16], const AES_KEY *key,
338 const u128 Htable[16]);
339 #endif
340
341 #elif defined(OPENSSL_PPC64LE)
342 #define GHASH_ASM_PPC64LE
343 #define GCM_FUNCREF
344 void gcm_init_p8(u128 Htable[16], const uint64_t Xi[2]);
345 void gcm_gmult_p8(uint8_t Xi[16], const u128 Htable[16]);
346 void gcm_ghash_p8(uint8_t Xi[16], const u128 Htable[16],
347 const uint8_t *inp, size_t len);
348 #endif
349 #endif // OPENSSL_NO_ASM
350
351
352 // CBC.
353
354 // cbc128_f is the type of a function that performs CBC-mode encryption.
355 typedef void (*cbc128_f)(const uint8_t *in, uint8_t *out, size_t len,
356 const AES_KEY *key, uint8_t ivec[16], int enc);
357
358 // CRYPTO_cbc128_encrypt encrypts |len| bytes from |in| to |out| using the
359 // given IV and block cipher in CBC mode. The input need not be a multiple of
360 // 128 bits long, but the output will round up to the nearest 128 bit multiple,
361 // zero padding the input if needed. The IV will be updated on return.
362 void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
363 const AES_KEY *key, uint8_t ivec[16],
364 block128_f block);
365
366 // CRYPTO_cbc128_decrypt decrypts |len| bytes from |in| to |out| using the
367 // given IV and block cipher in CBC mode. If |len| is not a multiple of 128
368 // bits then only that many bytes will be written, but a multiple of 128 bits
369 // is always read from |in|. The IV will be updated on return.
370 void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
371 const AES_KEY *key, uint8_t ivec[16],
372 block128_f block);
373
374
375 // OFB.
376
377 // CRYPTO_ofb128_encrypt encrypts (or decrypts, it's the same with OFB mode)
378 // |len| bytes from |in| to |out| using |block| in OFB mode. There's no
379 // requirement that |len| be a multiple of any value and any partial blocks are
380 // stored in |ivec| and |*num|, the latter must be zero before the initial
381 // call.
382 void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
383 const AES_KEY *key, uint8_t ivec[16], unsigned *num,
384 block128_f block);
385
386
387 // CFB.
388
389 // CRYPTO_cfb128_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
390 // from |in| to |out| using |block| in CFB mode. There's no requirement that
391 // |len| be a multiple of any value and any partial blocks are stored in |ivec|
392 // and |*num|, the latter must be zero before the initial call.
393 void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
394 const AES_KEY *key, uint8_t ivec[16], unsigned *num,
395 int enc, block128_f block);
396
397 // CRYPTO_cfb128_8_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
398 // from |in| to |out| using |block| in CFB-8 mode. Prior to the first call
399 // |num| should be set to zero.
400 void CRYPTO_cfb128_8_encrypt(const uint8_t *in, uint8_t *out, size_t len,
401 const AES_KEY *key, uint8_t ivec[16],
402 unsigned *num, int enc, block128_f block);
403
404 // CRYPTO_cfb128_1_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
405 // from |in| to |out| using |block| in CFB-1 mode. Prior to the first call
406 // |num| should be set to zero.
407 void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits,
408 const AES_KEY *key, uint8_t ivec[16],
409 unsigned *num, int enc, block128_f block);
410
411 size_t CRYPTO_cts128_encrypt_block(const uint8_t *in, uint8_t *out, size_t len,
412 const AES_KEY *key, uint8_t ivec[16],
413 block128_f block);
414
415 // XTS.
416
417 // CRYPTO_xts128_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
418 // from |in| to |out| using the given IV in XTS mode. There's no requirement
419 // that |len| be a multiple of any value.
420 size_t CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx,
421 const uint8_t iv[16], const uint8_t *inp,
422 uint8_t *out, size_t len, int enc);
423
424 // POLYVAL.
425 //
426 // POLYVAL is a polynomial authenticator that operates over a field very
427 // similar to the one that GHASH uses. See
428 // https://www.rfc-editor.org/rfc/rfc8452.html#section-3.
429
430 struct polyval_ctx {
431 uint8_t S[16];
432 // |gcm_*_ssse3| require |Htable| to be 16-byte-aligned.
433 // TODO(crbug.com/boringssl/604): Revisit this.
434 alignas(16) u128 Htable[16];
435 gmult_func gmult;
436 ghash_func ghash;
437 };
438
439 // CRYPTO_POLYVAL_init initialises |ctx| using |key|.
440 void CRYPTO_POLYVAL_init(struct polyval_ctx *ctx, const uint8_t key[16]);
441
442 // CRYPTO_POLYVAL_update_blocks updates the accumulator in |ctx| given the
443 // blocks from |in|. Only a whole number of blocks can be processed so |in_len|
444 // must be a multiple of 16.
445 void CRYPTO_POLYVAL_update_blocks(struct polyval_ctx *ctx, const uint8_t *in,
446 size_t in_len);
447
448 // CRYPTO_POLYVAL_finish writes the accumulator from |ctx| to |out|.
449 void CRYPTO_POLYVAL_finish(const struct polyval_ctx *ctx, uint8_t out[16]);
450
451
452 #if defined(__cplusplus)
453 } // extern C
454 #endif
455
456 #endif // OPENSSL_HEADER_MODES_INTERNAL_H