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