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