Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 54 | #include <string.h> |
| 55 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 56 | #include "../../internal.h" |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 57 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 58 | #if defined(__cplusplus) |
| 59 | extern "C" { |
| 60 | #endif |
| 61 | |
| 62 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 63 | #define STRICT_ALIGNMENT 1 |
| 64 | #if defined(OPENSSL_X86_64) || defined(OPENSSL_X86) || defined(OPENSSL_AARCH64) |
| 65 | #undef STRICT_ALIGNMENT |
| 66 | #define STRICT_ALIGNMENT 0 |
| 67 | #endif |
| 68 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 69 | static inline uint32_t GETU32(const void *in) { |
| 70 | uint32_t v; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 71 | OPENSSL_memcpy(&v, in, sizeof(v)); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 72 | return CRYPTO_bswap4(v); |
| 73 | } |
| 74 | |
| 75 | static inline void PUTU32(void *out, uint32_t v) { |
| 76 | v = CRYPTO_bswap4(v); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 77 | OPENSSL_memcpy(out, &v, sizeof(v)); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 80 | static inline size_t load_word_le(const void *in) { |
| 81 | size_t v; |
| 82 | OPENSSL_memcpy(&v, in, sizeof(v)); |
| 83 | return v; |
| 84 | } |
| 85 | |
| 86 | static inline void store_word_le(void *out, size_t v) { |
| 87 | OPENSSL_memcpy(out, &v, sizeof(v)); |
| 88 | } |
| 89 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 90 | // block128_f is the type of a 128-bit, block cipher. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 91 | typedef void (*block128_f)(const uint8_t in[16], uint8_t out[16], |
| 92 | const void *key); |
| 93 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 94 | // GCM definitions |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 95 | typedef struct { uint64_t hi,lo; } u128; |
| 96 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 97 | // gmult_func multiplies |Xi| by the GCM key and writes the result back to |
| 98 | // |Xi|. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 99 | typedef void (*gmult_func)(uint64_t Xi[2], const u128 Htable[16]); |
| 100 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 101 | // ghash_func repeatedly multiplies |Xi| by the GCM key and adds in blocks from |
| 102 | // |inp|. The result is written back to |Xi| and the |len| argument must be a |
| 103 | // multiple of 16. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 104 | typedef void (*ghash_func)(uint64_t Xi[2], const u128 Htable[16], |
| 105 | const uint8_t *inp, size_t len); |
| 106 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 107 | // This differs from upstream's |gcm128_context| in that it does not have the |
| 108 | // |key| pointer, in order to make it |memcpy|-friendly. Rather the key is |
| 109 | // passed into each call that needs it. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 110 | struct gcm128_context { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 111 | // Following 6 names follow names in GCM specification |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 112 | union { |
| 113 | uint64_t u[2]; |
| 114 | uint32_t d[4]; |
| 115 | uint8_t c[16]; |
| 116 | size_t t[16 / sizeof(size_t)]; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 117 | } Yi, EKi, EK0, len, Xi; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 118 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 119 | // Note that the order of |Xi|, |H| and |Htable| is fixed by the MOVBE-based, |
| 120 | // x86-64, GHASH assembly. |
Steven Valdez | b0b45c6 | 2017-01-17 16:23:54 -0500 | [diff] [blame] | 121 | u128 H; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 122 | u128 Htable[16]; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 123 | gmult_func gmult; |
| 124 | ghash_func ghash; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 125 | |
| 126 | unsigned int mres, ares; |
| 127 | block128_f block; |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 128 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 129 | // use_aesni_gcm_crypt is true if this context should use the assembly |
| 130 | // functions |aesni_gcm_encrypt| and |aesni_gcm_decrypt| to process data. |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 131 | unsigned use_aesni_gcm_crypt:1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 134 | #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 135 | // crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is |
| 136 | // used. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 137 | int crypto_gcm_clmul_enabled(void); |
| 138 | #endif |
| 139 | |
| 140 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 141 | // CTR. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 142 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 143 | // ctr128_f is the type of a function that performs CTR-mode encryption. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 144 | typedef void (*ctr128_f)(const uint8_t *in, uint8_t *out, size_t blocks, |
| 145 | const void *key, const uint8_t ivec[16]); |
| 146 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 147 | // CRYPTO_ctr128_encrypt encrypts (or decrypts, it's the same in CTR mode) |
| 148 | // |len| bytes from |in| to |out| using |block| in counter mode. There's no |
| 149 | // requirement that |len| be a multiple of any value and any partial blocks are |
| 150 | // stored in |ecount_buf| and |*num|, which must be zeroed before the initial |
| 151 | // call. The counter is a 128-bit, big-endian value in |ivec| and is |
| 152 | // incremented by this function. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 153 | void CRYPTO_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 154 | const void *key, uint8_t ivec[16], |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 155 | uint8_t ecount_buf[16], unsigned *num, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 156 | block128_f block); |
| 157 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 158 | // CRYPTO_ctr128_encrypt_ctr32 acts like |CRYPTO_ctr128_encrypt| but takes |
| 159 | // |ctr|, a function that performs CTR mode but only deals with the lower 32 |
| 160 | // bits of the counter. This is useful when |ctr| can be an optimised |
| 161 | // function. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 162 | void CRYPTO_ctr128_encrypt_ctr32(const uint8_t *in, uint8_t *out, size_t len, |
| 163 | const void *key, uint8_t ivec[16], |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 164 | uint8_t ecount_buf[16], unsigned *num, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 165 | ctr128_f ctr); |
| 166 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 167 | #if !defined(OPENSSL_NO_ASM) && \ |
| 168 | (defined(OPENSSL_X86) || defined(OPENSSL_X86_64)) |
| 169 | void aesni_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t blocks, |
| 170 | const void *key, const uint8_t *ivec); |
| 171 | #endif |
| 172 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 173 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 174 | // GCM. |
| 175 | // |
| 176 | // This API differs from the upstream API slightly. The |GCM128_CONTEXT| does |
| 177 | // not have a |key| pointer that points to the key as upstream's version does. |
| 178 | // Instead, every function takes a |key| parameter. This way |GCM128_CONTEXT| |
| 179 | // can be safely copied. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 180 | |
| 181 | typedef struct gcm128_context GCM128_CONTEXT; |
| 182 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 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. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 187 | void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash, |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 188 | u128 *out_key, u128 out_table[16], int *out_is_avx, |
Steven Valdez | b0b45c6 | 2017-01-17 16:23:54 -0500 | [diff] [blame] | 189 | const uint8_t *gcm_key); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 190 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 191 | // CRYPTO_gcm128_init initialises |ctx| to use |block| (typically AES) with |
| 192 | // the given key. |is_aesni_encrypt| is one if |block| is |aesni_encrypt|. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 193 | OPENSSL_EXPORT void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, const void *key, |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 194 | block128_f block, int is_aesni_encrypt); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 195 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 196 | // CRYPTO_gcm128_setiv sets the IV (nonce) for |ctx|. The |key| must be the |
| 197 | // same key that was passed to |CRYPTO_gcm128_init|. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 198 | OPENSSL_EXPORT void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const void *key, |
| 199 | const uint8_t *iv, size_t iv_len); |
| 200 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 201 | // CRYPTO_gcm128_aad sets the authenticated data for an instance of GCM. |
| 202 | // This must be called before and data is encrypted. It returns one on success |
| 203 | // and zero otherwise. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 204 | OPENSSL_EXPORT int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad, |
| 205 | size_t len); |
| 206 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 207 | // CRYPTO_gcm128_encrypt encrypts |len| bytes from |in| to |out|. The |key| |
| 208 | // must be the same key that was passed to |CRYPTO_gcm128_init|. It returns one |
| 209 | // on success and zero otherwise. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 210 | OPENSSL_EXPORT int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, const void *key, |
| 211 | const uint8_t *in, uint8_t *out, |
| 212 | size_t len); |
| 213 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 214 | // CRYPTO_gcm128_decrypt decrypts |len| bytes from |in| to |out|. The |key| |
| 215 | // must be the same key that was passed to |CRYPTO_gcm128_init|. It returns one |
| 216 | // on success and zero otherwise. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 217 | OPENSSL_EXPORT int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const void *key, |
| 218 | const uint8_t *in, uint8_t *out, |
| 219 | size_t len); |
| 220 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 221 | // CRYPTO_gcm128_encrypt_ctr32 encrypts |len| bytes from |in| to |out| using |
| 222 | // a CTR function that only handles the bottom 32 bits of the nonce, like |
| 223 | // |CRYPTO_ctr128_encrypt_ctr32|. The |key| must be the same key that was |
| 224 | // passed to |CRYPTO_gcm128_init|. It returns one on success and zero |
| 225 | // otherwise. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 226 | OPENSSL_EXPORT int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx, |
| 227 | const void *key, |
| 228 | const uint8_t *in, uint8_t *out, |
| 229 | size_t len, ctr128_f stream); |
| 230 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 231 | // CRYPTO_gcm128_decrypt_ctr32 decrypts |len| bytes from |in| to |out| using |
| 232 | // a CTR function that only handles the bottom 32 bits of the nonce, like |
| 233 | // |CRYPTO_ctr128_encrypt_ctr32|. The |key| must be the same key that was |
| 234 | // passed to |CRYPTO_gcm128_init|. It returns one on success and zero |
| 235 | // otherwise. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 236 | OPENSSL_EXPORT int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx, |
| 237 | const void *key, |
| 238 | const uint8_t *in, uint8_t *out, |
| 239 | size_t len, ctr128_f stream); |
| 240 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 241 | // CRYPTO_gcm128_finish calculates the authenticator and compares it against |
| 242 | // |len| bytes of |tag|. It returns one on success and zero otherwise. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 243 | OPENSSL_EXPORT int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag, |
| 244 | size_t len); |
| 245 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 246 | // CRYPTO_gcm128_tag calculates the authenticator and copies it into |tag|. |
| 247 | // The minimum of |len| and 16 bytes are copied into |tag|. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 248 | OPENSSL_EXPORT void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, uint8_t *tag, |
| 249 | size_t len); |
| 250 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 251 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 252 | // CBC. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 253 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 254 | // cbc128_f is the type of a function that performs CBC-mode encryption. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 255 | typedef void (*cbc128_f)(const uint8_t *in, uint8_t *out, size_t len, |
| 256 | const void *key, uint8_t ivec[16], int enc); |
| 257 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 258 | // CRYPTO_cbc128_encrypt encrypts |len| bytes from |in| to |out| using the |
| 259 | // given IV and block cipher in CBC mode. The input need not be a multiple of |
| 260 | // 128 bits long, but the output will round up to the nearest 128 bit multiple, |
| 261 | // zero padding the input if needed. The IV will be updated on return. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 262 | void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 263 | const void *key, uint8_t ivec[16], block128_f block); |
| 264 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 265 | // CRYPTO_cbc128_decrypt decrypts |len| bytes from |in| to |out| using the |
| 266 | // given IV and block cipher in CBC mode. If |len| is not a multiple of 128 |
| 267 | // bits then only that many bytes will be written, but a multiple of 128 bits |
| 268 | // is always read from |in|. The IV will be updated on return. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 269 | void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 270 | const void *key, uint8_t ivec[16], block128_f block); |
| 271 | |
| 272 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 273 | // OFB. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 274 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 275 | // CRYPTO_ofb128_encrypt encrypts (or decrypts, it's the same with OFB mode) |
| 276 | // |len| bytes from |in| to |out| using |block| in OFB mode. There's no |
| 277 | // requirement that |len| be a multiple of any value and any partial blocks are |
| 278 | // stored in |ivec| and |*num|, the latter must be zero before the initial |
| 279 | // call. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 280 | void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out, |
| 281 | size_t len, const void *key, uint8_t ivec[16], |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 282 | unsigned *num, block128_f block); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 283 | |
| 284 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 285 | // CFB. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 286 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 287 | // CRYPTO_cfb128_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes |
| 288 | // from |in| to |out| using |block| in CFB mode. There's no requirement that |
| 289 | // |len| be a multiple of any value and any partial blocks are stored in |ivec| |
| 290 | // and |*num|, the latter must be zero before the initial call. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 291 | void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 292 | const void *key, uint8_t ivec[16], unsigned *num, |
| 293 | int enc, block128_f block); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 294 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 295 | // CRYPTO_cfb128_8_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes |
| 296 | // from |in| to |out| using |block| in CFB-8 mode. Prior to the first call |
| 297 | // |num| should be set to zero. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 298 | void CRYPTO_cfb128_8_encrypt(const uint8_t *in, uint8_t *out, size_t len, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 299 | const void *key, uint8_t ivec[16], unsigned *num, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 300 | int enc, block128_f block); |
| 301 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 302 | // CRYPTO_cfb128_1_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes |
| 303 | // from |in| to |out| using |block| in CFB-1 mode. Prior to the first call |
| 304 | // |num| should be set to zero. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 305 | void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 306 | const void *key, uint8_t ivec[16], unsigned *num, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 307 | int enc, block128_f block); |
| 308 | |
| 309 | size_t CRYPTO_cts128_encrypt_block(const uint8_t *in, uint8_t *out, size_t len, |
| 310 | const void *key, uint8_t ivec[16], |
| 311 | block128_f block); |
| 312 | |
| 313 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 314 | // POLYVAL. |
| 315 | // |
| 316 | // POLYVAL is a polynomial authenticator that operates over a field very |
| 317 | // similar to the one that GHASH uses. See |
| 318 | // https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02#section-3. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 319 | |
| 320 | typedef union { |
| 321 | uint64_t u[2]; |
| 322 | uint8_t c[16]; |
| 323 | } polyval_block; |
| 324 | |
| 325 | struct polyval_ctx { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 326 | // Note that the order of |S|, |H| and |Htable| is fixed by the MOVBE-based, |
| 327 | // x86-64, GHASH assembly. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 328 | polyval_block S; |
Steven Valdez | b0b45c6 | 2017-01-17 16:23:54 -0500 | [diff] [blame] | 329 | u128 H; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 330 | u128 Htable[16]; |
| 331 | gmult_func gmult; |
| 332 | ghash_func ghash; |
| 333 | }; |
| 334 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 335 | // CRYPTO_POLYVAL_init initialises |ctx| using |key|. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 336 | void CRYPTO_POLYVAL_init(struct polyval_ctx *ctx, const uint8_t key[16]); |
| 337 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 338 | // CRYPTO_POLYVAL_update_blocks updates the accumulator in |ctx| given the |
| 339 | // blocks from |in|. Only a whole number of blocks can be processed so |in_len| |
| 340 | // must be a multiple of 16. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 341 | void CRYPTO_POLYVAL_update_blocks(struct polyval_ctx *ctx, const uint8_t *in, |
| 342 | size_t in_len); |
| 343 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 344 | // CRYPTO_POLYVAL_finish writes the accumulator from |ctx| to |out|. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 345 | void CRYPTO_POLYVAL_finish(const struct polyval_ctx *ctx, uint8_t out[16]); |
| 346 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 347 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 348 | #if defined(__cplusplus) |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 349 | } // extern C |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 350 | #endif |
| 351 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 352 | #endif // OPENSSL_HEADER_MODES_INTERNAL_H |