• 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 
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_aesni_gcm_crypt is true if this context should use the assembly
140   // functions |aesni_gcm_encrypt| and |aesni_gcm_decrypt| to process data.
141   unsigned use_aesni_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 AESNI_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 #define GHASH_ASM_ARM
283 #define GCM_FUNCREF
284 
gcm_pmull_capable(void)285 OPENSSL_INLINE int gcm_pmull_capable(void) {
286   return CRYPTO_is_ARMv8_PMULL_capable();
287 }
288 
289 void gcm_init_v8(u128 Htable[16], const uint64_t Xi[2]);
290 void gcm_gmult_v8(uint64_t Xi[2], const u128 Htable[16]);
291 void gcm_ghash_v8(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
292                   size_t len);
293 
gcm_neon_capable(void)294 OPENSSL_INLINE int gcm_neon_capable(void) { return CRYPTO_is_NEON_capable(); }
295 
296 void gcm_init_neon(u128 Htable[16], const uint64_t Xi[2]);
297 void gcm_gmult_neon(uint64_t Xi[2], const u128 Htable[16]);
298 void gcm_ghash_neon(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
299                     size_t len);
300 
301 #elif defined(OPENSSL_PPC64LE)
302 #define GHASH_ASM_PPC64LE
303 #define GCM_FUNCREF
304 void gcm_init_p8(u128 Htable[16], const uint64_t Xi[2]);
305 void gcm_gmult_p8(uint64_t Xi[2], const u128 Htable[16]);
306 void gcm_ghash_p8(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
307                   size_t len);
308 #endif
309 #endif  // OPENSSL_NO_ASM
310 
311 
312 // CBC.
313 
314 // cbc128_f is the type of a function that performs CBC-mode encryption.
315 typedef void (*cbc128_f)(const uint8_t *in, uint8_t *out, size_t len,
316                          const AES_KEY *key, uint8_t ivec[16], int enc);
317 
318 // CRYPTO_cbc128_encrypt encrypts |len| bytes from |in| to |out| using the
319 // given IV and block cipher in CBC mode. The input need not be a multiple of
320 // 128 bits long, but the output will round up to the nearest 128 bit multiple,
321 // zero padding the input if needed. The IV will be updated on return.
322 void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
323                            const AES_KEY *key, uint8_t ivec[16],
324                            block128_f block);
325 
326 // CRYPTO_cbc128_decrypt decrypts |len| bytes from |in| to |out| using the
327 // given IV and block cipher in CBC mode. If |len| is not a multiple of 128
328 // bits then only that many bytes will be written, but a multiple of 128 bits
329 // is always read from |in|. The IV will be updated on return.
330 void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
331                            const AES_KEY *key, uint8_t ivec[16],
332                            block128_f block);
333 
334 
335 // OFB.
336 
337 // CRYPTO_ofb128_encrypt encrypts (or decrypts, it's the same with OFB mode)
338 // |len| bytes from |in| to |out| using |block| in OFB mode. There's no
339 // requirement that |len| be a multiple of any value and any partial blocks are
340 // stored in |ivec| and |*num|, the latter must be zero before the initial
341 // call.
342 void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
343                            const AES_KEY *key, uint8_t ivec[16], unsigned *num,
344                            block128_f block);
345 
346 
347 // CFB.
348 
349 // CRYPTO_cfb128_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
350 // from |in| to |out| using |block| in CFB mode. There's no requirement that
351 // |len| be a multiple of any value and any partial blocks are stored in |ivec|
352 // and |*num|, the latter must be zero before the initial call.
353 void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
354                            const AES_KEY *key, uint8_t ivec[16], unsigned *num,
355                            int enc, block128_f block);
356 
357 // CRYPTO_cfb128_8_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
358 // from |in| to |out| using |block| in CFB-8 mode. Prior to the first call
359 // |num| should be set to zero.
360 void CRYPTO_cfb128_8_encrypt(const uint8_t *in, uint8_t *out, size_t len,
361                              const AES_KEY *key, uint8_t ivec[16],
362                              unsigned *num, int enc, block128_f block);
363 
364 // CRYPTO_cfb128_1_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
365 // from |in| to |out| using |block| in CFB-1 mode. Prior to the first call
366 // |num| should be set to zero.
367 void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits,
368                              const AES_KEY *key, uint8_t ivec[16],
369                              unsigned *num, int enc, block128_f block);
370 
371 size_t CRYPTO_cts128_encrypt_block(const uint8_t *in, uint8_t *out, size_t len,
372                                    const AES_KEY *key, uint8_t ivec[16],
373                                    block128_f block);
374 
375 
376 // POLYVAL.
377 //
378 // POLYVAL is a polynomial authenticator that operates over a field very
379 // similar to the one that GHASH uses. See
380 // https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02#section-3.
381 
382 typedef union {
383   uint64_t u[2];
384   uint8_t c[16];
385 } polyval_block;
386 
387 struct polyval_ctx {
388   // Note that the order of |S|, |H| and |Htable| is fixed by the MOVBE-based,
389   // x86-64, GHASH assembly. Additionally, some assembly routines require
390   // |Htable| to be 16-byte aligned.
391   polyval_block S;
392   u128 H;
393   alignas(16) u128 Htable[16];
394   gmult_func gmult;
395   ghash_func ghash;
396 };
397 
398 // CRYPTO_POLYVAL_init initialises |ctx| using |key|.
399 void CRYPTO_POLYVAL_init(struct polyval_ctx *ctx, const uint8_t key[16]);
400 
401 // CRYPTO_POLYVAL_update_blocks updates the accumulator in |ctx| given the
402 // blocks from |in|. Only a whole number of blocks can be processed so |in_len|
403 // must be a multiple of 16.
404 void CRYPTO_POLYVAL_update_blocks(struct polyval_ctx *ctx, const uint8_t *in,
405                                   size_t in_len);
406 
407 // CRYPTO_POLYVAL_finish writes the accumulator from |ctx| to |out|.
408 void CRYPTO_POLYVAL_finish(const struct polyval_ctx *ctx, uint8_t out[16]);
409 
410 
411 #if defined(__cplusplus)
412 }  // extern C
413 #endif
414 
415 #endif  // OPENSSL_HEADER_MODES_INTERNAL_H
416