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