Coverage Report

Created: 2024-11-21 07:03

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