| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/aead.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 |
| 19 | #include <openssl/cipher.h> |
| 20 | #include <openssl/crypto.h> |
| 21 | #include <openssl/err.h> |
| 22 |
| 23 | #include "../fipsmodule/aes/internal.h" |
| 24 | #include "../fipsmodule/cipher/internal.h" |
| 25 | #include "../internal.h" |
| 26 |
| 27 |
| 28 | #define EVP_AEAD_AES_GCM_SIV_NONCE_LEN 12 |
| 29 | #define EVP_AEAD_AES_GCM_SIV_TAG_LEN 16 |
| 30 |
| 31 | namespace { |
| 32 | // TODO(davidben): AES-GCM-SIV assembly is not correct for Windows. It must save |
| 33 | // and restore xmm6 through xmm15. |
| 34 | #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \ |
| 35 | !defined(OPENSSL_WINDOWS) |
| 36 | #define AES_GCM_SIV_ASM |
| 37 |
| 38 | // Optimised AES-GCM-SIV |
| 39 |
| 40 | struct aead_aes_gcm_siv_asm_ctx { |
| 41 | alignas(16) uint8_t key[16 * 15]; |
| 42 | int is_128_bit; |
| 43 | }; |
| 44 |
| 45 | // The assembly code assumes 8-byte alignment of the EVP_AEAD_CTX's state, and |
| 46 | // aligns to 16 bytes itself. |
| 47 | static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) + 8 >= |
| 48 | sizeof(struct aead_aes_gcm_siv_asm_ctx), |
| 49 | "AEAD state is too small"); |
| 50 | static_assert(alignof(union evp_aead_ctx_st_state) >= 8, |
| 51 | "AEAD state has insufficient alignment"); |
| 52 |
| 53 | extern "C" { |
| 54 | // aes128gcmsiv_aes_ks writes an AES-128 key schedule for |key| to |
| 55 | // |out_expanded_key|. |
| 56 | extern void aes128gcmsiv_aes_ks(const uint8_t key[16], |
| 57 | uint8_t out_expanded_key[16 * 15]); |
| 58 |
| 59 | // aes256gcmsiv_aes_ks writes an AES-256 key schedule for |key| to |
| 60 | // |out_expanded_key|. |
| 61 | extern void aes256gcmsiv_aes_ks(const uint8_t key[32], |
| 62 | uint8_t out_expanded_key[16 * 15]); |
| 63 |
| 64 | // aesgcmsiv_polyval_horner updates the POLYVAL value in |in_out_poly| to |
| 65 | // include a number (|in_blocks|) of 16-byte blocks of data from |in|, given |
| 66 | // the POLYVAL key in |key|. |
| 67 | extern void aesgcmsiv_polyval_horner(const uint8_t in_out_poly[16], |
| 68 | const uint8_t key[16], const uint8_t *in, |
| 69 | size_t in_blocks); |
| 70 |
| 71 | // aesgcmsiv_htable_init writes powers 1..8 of |auth_key| to |out_htable|. |
| 72 | extern void aesgcmsiv_htable_init(uint8_t out_htable[16 * 8], |
| 73 | const uint8_t auth_key[16]); |
| 74 |
| 75 | // aesgcmsiv_htable6_init writes powers 1..6 of |auth_key| to |out_htable|. |
| 76 | extern void aesgcmsiv_htable6_init(uint8_t out_htable[16 * 6], |
| 77 | const uint8_t auth_key[16]); |
| 78 |
| 79 | // aesgcmsiv_htable_polyval updates the POLYVAL value in |in_out_poly| to |
| 80 | // include |in_len| bytes of data from |in|. (Where |in_len| must be a multiple |
| 81 | // of 16.) It uses the precomputed powers of the key given in |htable|. |
| 82 | extern void aesgcmsiv_htable_polyval(const uint8_t htable[16 * 8], |
| 83 | const uint8_t *in, size_t in_len, |
| 84 | uint8_t in_out_poly[16]); |
| 85 |
| 86 | // aes128gcmsiv_dec decrypts |in_len| & ~15 bytes from |out| and writes them to |
| 87 | // |in|. |in| and |out| may be equal, but must not otherwise alias. |
| 88 | // |
| 89 | // |in_out_calculated_tag_and_scratch|, on entry, must contain: |
| 90 | // 1. The current value of the calculated tag, which will be updated during |
| 91 | // decryption and written back to the beginning of this buffer on exit. |
| 92 | // 2. The claimed tag, which is needed to derive counter values. |
| 93 | // |
| 94 | // While decrypting, the whole of |in_out_calculated_tag_and_scratch| may be |
| 95 | // used for other purposes. In order to decrypt and update the POLYVAL value, it |
| 96 | // uses the expanded key from |key| and the table of powers in |htable|. |
| 97 | extern void aes128gcmsiv_dec(const uint8_t *in, uint8_t *out, |
| 98 | uint8_t in_out_calculated_tag_and_scratch[16 * 8], |
| 99 | const uint8_t htable[16 * 6], |
| 100 | const struct aead_aes_gcm_siv_asm_ctx *key, |
| 101 | size_t in_len); |
| 102 |
| 103 | // aes256gcmsiv_dec acts like |aes128gcmsiv_dec|, but for AES-256. |
| 104 | extern void aes256gcmsiv_dec(const uint8_t *in, uint8_t *out, |
| 105 | uint8_t in_out_calculated_tag_and_scratch[16 * 8], |
| 106 | const uint8_t htable[16 * 6], |
| 107 | const struct aead_aes_gcm_siv_asm_ctx *key, |
| 108 | size_t in_len); |
| 109 |
| 110 | // aes128gcmsiv_kdf performs the AES-GCM-SIV KDF given the expanded key from |
| 111 | // |key_schedule| and the nonce in |nonce|. Note that, while only 12 bytes of |
| 112 | // the nonce are used, 16 bytes are read and so the value must be |
| 113 | // right-padded. |
| 114 | extern void aes128gcmsiv_kdf(const uint8_t nonce[16], |
| 115 | uint64_t out_key_material[8], |
| 116 | const uint8_t *key_schedule); |
| 117 |
| 118 | // aes256gcmsiv_kdf acts like |aes128gcmsiv_kdf|, but for AES-256. |
| 119 | extern void aes256gcmsiv_kdf(const uint8_t nonce[16], |
| 120 | uint64_t out_key_material[12], |
| 121 | const uint8_t *key_schedule); |
| 122 |
| 123 | // aes128gcmsiv_aes_ks_enc_x1 performs a key expansion of the AES-128 key in |
| 124 | // |key|, writes the expanded key to |out_expanded_key| and encrypts a single |
| 125 | // block from |in| to |out|. |
| 126 | extern void aes128gcmsiv_aes_ks_enc_x1(const uint8_t in[16], uint8_t out[16], |
| 127 | uint8_t out_expanded_key[16 * 15], |
| 128 | const uint64_t key[2]); |
| 129 |
| 130 | // aes256gcmsiv_aes_ks_enc_x1 acts like |aes128gcmsiv_aes_ks_enc_x1|, but for |
| 131 | // AES-256. |
| 132 | extern void aes256gcmsiv_aes_ks_enc_x1(const uint8_t in[16], uint8_t out[16], |
| 133 | uint8_t out_expanded_key[16 * 15], |
| 134 | const uint64_t key[4]); |
| 135 |
| 136 | // aes128gcmsiv_ecb_enc_block encrypts a single block from |in| to |out| using |
| 137 | // the expanded key in |expanded_key|. |
| 138 | extern void aes128gcmsiv_ecb_enc_block( |
| 139 | const uint8_t in[16], uint8_t out[16], |
| 140 | const struct aead_aes_gcm_siv_asm_ctx *expanded_key); |
| 141 |
| 142 | // aes256gcmsiv_ecb_enc_block acts like |aes128gcmsiv_ecb_enc_block|, but for |
| 143 | // AES-256. |
| 144 | extern void aes256gcmsiv_ecb_enc_block( |
| 145 | const uint8_t in[16], uint8_t out[16], |
| 146 | const struct aead_aes_gcm_siv_asm_ctx *expanded_key); |
| 147 |
| 148 | // aes128gcmsiv_enc_msg_x4 encrypts |in_len| bytes from |in| to |out| using the |
| 149 | // expanded key from |key|. (The value of |in_len| must be a multiple of 16.) |
| 150 | // The |in| and |out| buffers may be equal but must not otherwise overlap. The |
| 151 | // initial counter is constructed from the given |tag| as required by |
| 152 | // AES-GCM-SIV. |
| 153 | extern void aes128gcmsiv_enc_msg_x4(const uint8_t *in, uint8_t *out, |
| 154 | const uint8_t *tag, |
| 155 | const struct aead_aes_gcm_siv_asm_ctx *key, |
| 156 | size_t in_len); |
| 157 |
| 158 | // aes256gcmsiv_enc_msg_x4 acts like |aes128gcmsiv_enc_msg_x4|, but for |
| 159 | // AES-256. |
| 160 | extern void aes256gcmsiv_enc_msg_x4(const uint8_t *in, uint8_t *out, |
| 161 | const uint8_t *tag, |
| 162 | const struct aead_aes_gcm_siv_asm_ctx *key, |
| 163 | size_t in_len); |
| 164 |
| 165 | // aes128gcmsiv_enc_msg_x8 acts like |aes128gcmsiv_enc_msg_x4|, but is |
| 166 | // optimised for longer messages. |
| 167 | extern void aes128gcmsiv_enc_msg_x8(const uint8_t *in, uint8_t *out, |
| 168 | const uint8_t *tag, |
| 169 | const struct aead_aes_gcm_siv_asm_ctx *key, |
| 170 | size_t in_len); |
| 171 |
| 172 | // aes256gcmsiv_enc_msg_x8 acts like |aes256gcmsiv_enc_msg_x4|, but is |
| 173 | // optimised for longer messages. |
| 174 | extern void aes256gcmsiv_enc_msg_x8(const uint8_t *in, uint8_t *out, |
| 175 | const uint8_t *tag, |
| 176 | const struct aead_aes_gcm_siv_asm_ctx *key, |
| 177 | size_t in_len); |
| 178 | } |
| 179 |
| 180 | // asm_ctx_from_ctx returns a 16-byte aligned context pointer from |ctx|. |
| 181 | struct aead_aes_gcm_siv_asm_ctx *asm_ctx_from_ctx(const EVP_AEAD_CTX *ctx) { |
| 182 | // ctx->state must already be 8-byte aligned. Thus, at most, we may need to |
| 183 | // add eight to align it to 16 bytes. |
| 184 | const uintptr_t offset = ((uintptr_t)&ctx->state) & 8; |
| 185 | return (struct aead_aes_gcm_siv_asm_ctx *)(&ctx->state.opaque[offset]); |
| 186 | } |
| 187 |
| 188 | int aead_aes_gcm_siv_asm_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
| 189 | size_t key_len, size_t tag_len) { |
| 190 | const size_t key_bits = key_len * 8; |
| 191 |
| 192 | if (key_bits != 128 && key_bits != 256) { |
| 193 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
| 194 | return 0; // EVP_AEAD_CTX_init should catch this. |
| 195 | } |
| 196 |
| 197 | if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { |
| 198 | tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN; |
| 199 | } |
| 200 |
| 201 | if (tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN) { |
| 202 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE); |
| 203 | return 0; |
| 204 | } |
| 205 |
| 206 | struct aead_aes_gcm_siv_asm_ctx *gcm_siv_ctx = asm_ctx_from_ctx(ctx); |
| 207 | assert((((uintptr_t)gcm_siv_ctx) & 15) == 0); |
| 208 |
| 209 | if (key_bits == 128) { |
| 210 | aes128gcmsiv_aes_ks(key, &gcm_siv_ctx->key[0]); |
| 211 | gcm_siv_ctx->is_128_bit = 1; |
| 212 | } else { |
| 213 | aes256gcmsiv_aes_ks(key, &gcm_siv_ctx->key[0]); |
| 214 | gcm_siv_ctx->is_128_bit = 0; |
| 215 | } |
| 216 |
| 217 | ctx->tag_len = tag_len; |
| 218 |
| 219 | return 1; |
| 220 | } |
| 221 |
| 222 | void aead_aes_gcm_siv_asm_cleanup(EVP_AEAD_CTX *ctx) {} |
| 223 |
| 224 | // gcm_siv_asm_polyval evaluates POLYVAL at |auth_key| on the given plaintext |
| 225 | // and AD. The result is written to |out_tag|. |
| 226 | void gcm_siv_asm_polyval(uint8_t out_tag[16], const uint8_t *in, size_t in_len, |
| 227 | const uint8_t *ad, size_t ad_len, |
| 228 | const uint8_t auth_key[16], const uint8_t nonce[12]) { |
| 229 | OPENSSL_memset(out_tag, 0, 16); |
| 230 | const size_t ad_blocks = ad_len / 16; |
| 231 | const size_t in_blocks = in_len / 16; |
| 232 | int htable_init = 0; |
| 233 | alignas(16) uint8_t htable[16 * 8]; |
| 234 |
| 235 | if (ad_blocks > 8 || in_blocks > 8) { |
| 236 | htable_init = 1; |
| 237 | aesgcmsiv_htable_init(htable, auth_key); |
| 238 | } |
| 239 |
| 240 | if (htable_init) { |
| 241 | aesgcmsiv_htable_polyval(htable, ad, ad_len & ~15, out_tag); |
| 242 | } else { |
| 243 | aesgcmsiv_polyval_horner(out_tag, auth_key, ad, ad_blocks); |
| 244 | } |
| 245 |
| 246 | uint8_t scratch[16]; |
| 247 | if (ad_len & 15) { |
| 248 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
| 249 | OPENSSL_memcpy(scratch, &ad[ad_len & ~15], ad_len & 15); |
| 250 | aesgcmsiv_polyval_horner(out_tag, auth_key, scratch, 1); |
| 251 | } |
| 252 |
| 253 | if (htable_init) { |
| 254 | aesgcmsiv_htable_polyval(htable, in, in_len & ~15, out_tag); |
| 255 | } else { |
| 256 | aesgcmsiv_polyval_horner(out_tag, auth_key, in, in_blocks); |
| 257 | } |
| 258 |
| 259 | if (in_len & 15) { |
| 260 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
| 261 | OPENSSL_memcpy(scratch, &in[in_len & ~15], in_len & 15); |
| 262 | aesgcmsiv_polyval_horner(out_tag, auth_key, scratch, 1); |
| 263 | } |
| 264 |
| 265 | uint8_t length_block[16]; |
| 266 | CRYPTO_store_u64_le(length_block, ad_len * 8); |
| 267 | CRYPTO_store_u64_le(length_block + 8, in_len * 8); |
| 268 | aesgcmsiv_polyval_horner(out_tag, auth_key, length_block, 1); |
| 269 |
| 270 | for (size_t i = 0; i < 12; i++) { |
| 271 | out_tag[i] ^= nonce[i]; |
| 272 | } |
| 273 |
| 274 | out_tag[15] &= 0x7f; |
| 275 | } |
| 276 |
| 277 | // aead_aes_gcm_siv_asm_crypt_last_block handles the encryption/decryption |
| 278 | // (same thing in CTR mode) of the final block of a plaintext/ciphertext. It |
| 279 | // writes |in_len| & 15 bytes to |out| + |in_len|, based on an initial counter |
| 280 | // derived from |tag|. |
| 281 | void aead_aes_gcm_siv_asm_crypt_last_block( |
| 282 | int is_128_bit, uint8_t *out, const uint8_t *in, size_t in_len, |
| 283 | const uint8_t tag[16], |
| 284 | const struct aead_aes_gcm_siv_asm_ctx *enc_key_expanded) { |
| 285 | alignas(16) uint8_t counter[16]; |
| 286 | OPENSSL_memcpy(&counter, tag, sizeof(counter)); |
| 287 | counter[15] |= 0x80; |
| 288 | CRYPTO_store_u32_le(counter, CRYPTO_load_u32_le(counter) + in_len / 16); |
| 289 |
| 290 | if (is_128_bit) { |
| 291 | aes128gcmsiv_ecb_enc_block(counter, counter, enc_key_expanded); |
| 292 | } else { |
| 293 | aes256gcmsiv_ecb_enc_block(counter, counter, enc_key_expanded); |
| 294 | } |
| 295 |
| 296 | const size_t last_bytes_offset = in_len & ~15; |
| 297 | const size_t last_bytes_len = in_len & 15; |
| 298 | uint8_t *last_bytes_out = &out[last_bytes_offset]; |
| 299 | const uint8_t *last_bytes_in = &in[last_bytes_offset]; |
| 300 | for (size_t i = 0; i < last_bytes_len; i++) { |
| 301 | last_bytes_out[i] = last_bytes_in[i] ^ counter[i]; |
| 302 | } |
| 303 | } |
| 304 |
| 305 | // aead_aes_gcm_siv_kdf calculates the record encryption and authentication |
| 306 | // keys given the |nonce|. |
| 307 | void aead_aes_gcm_siv_kdf(int is_128_bit, |
| 308 | const struct aead_aes_gcm_siv_asm_ctx *gcm_siv_ctx, |
| 309 | uint64_t out_record_auth_key[2], |
| 310 | uint64_t out_record_enc_key[4], |
| 311 | const uint8_t nonce[12]) { |
| 312 | alignas(16) uint8_t padded_nonce[16]; |
| 313 | OPENSSL_memcpy(padded_nonce, nonce, 12); |
| 314 |
| 315 | alignas(16) uint64_t key_material[12]; |
| 316 | if (is_128_bit) { |
| 317 | aes128gcmsiv_kdf(padded_nonce, key_material, &gcm_siv_ctx->key[0]); |
| 318 | out_record_enc_key[0] = key_material[4]; |
| 319 | out_record_enc_key[1] = key_material[6]; |
| 320 | } else { |
| 321 | aes256gcmsiv_kdf(padded_nonce, key_material, &gcm_siv_ctx->key[0]); |
| 322 | out_record_enc_key[0] = key_material[4]; |
| 323 | out_record_enc_key[1] = key_material[6]; |
| 324 | out_record_enc_key[2] = key_material[8]; |
| 325 | out_record_enc_key[3] = key_material[10]; |
| 326 | } |
| 327 |
| 328 | out_record_auth_key[0] = key_material[0]; |
| 329 | out_record_auth_key[1] = key_material[2]; |
| 330 | } |
| 331 |
| 332 | int aead_aes_gcm_siv_asm_seal_scatter( |
| 333 | const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag, |
| 334 | size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce, |
| 335 | size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in, |
| 336 | size_t extra_in_len, const uint8_t *ad, size_t ad_len) { |
| 337 | const struct aead_aes_gcm_siv_asm_ctx *gcm_siv_ctx = asm_ctx_from_ctx(ctx); |
| 338 | const uint64_t in_len_64 = in_len; |
| 339 | const uint64_t ad_len_64 = ad_len; |
| 340 |
| 341 | if (in_len_64 > (UINT64_C(1) << 36) || ad_len_64 >= (UINT64_C(1) << 61)) { |
| 342 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 343 | return 0; |
| 344 | } |
| 345 |
| 346 | if (max_out_tag_len < EVP_AEAD_AES_GCM_SIV_TAG_LEN) { |
| 347 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 348 | return 0; |
| 349 | } |
| 350 |
| 351 | if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) { |
| 352 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 353 | return 0; |
| 354 | } |
| 355 |
| 356 | alignas(16) uint64_t record_auth_key[2]; |
| 357 | alignas(16) uint64_t record_enc_key[4]; |
| 358 | aead_aes_gcm_siv_kdf(gcm_siv_ctx->is_128_bit, gcm_siv_ctx, record_auth_key, |
| 359 | record_enc_key, nonce); |
| 360 |
| 361 | alignas(16) uint8_t tag[16] = {0}; |
| 362 | gcm_siv_asm_polyval(tag, in, in_len, ad, ad_len, |
| 363 | (const uint8_t *)record_auth_key, nonce); |
| 364 |
| 365 | struct aead_aes_gcm_siv_asm_ctx enc_key_expanded; |
| 366 |
| 367 | if (gcm_siv_ctx->is_128_bit) { |
| 368 | aes128gcmsiv_aes_ks_enc_x1(tag, tag, &enc_key_expanded.key[0], |
| 369 | record_enc_key); |
| 370 |
| 371 | if (in_len < 128) { |
| 372 | aes128gcmsiv_enc_msg_x4(in, out, tag, &enc_key_expanded, in_len & ~15); |
| 373 | } else { |
| 374 | aes128gcmsiv_enc_msg_x8(in, out, tag, &enc_key_expanded, in_len & ~15); |
| 375 | } |
| 376 | } else { |
| 377 | aes256gcmsiv_aes_ks_enc_x1(tag, tag, &enc_key_expanded.key[0], |
| 378 | record_enc_key); |
| 379 |
| 380 | if (in_len < 128) { |
| 381 | aes256gcmsiv_enc_msg_x4(in, out, tag, &enc_key_expanded, in_len & ~15); |
| 382 | } else { |
| 383 | aes256gcmsiv_enc_msg_x8(in, out, tag, &enc_key_expanded, in_len & ~15); |
| 384 | } |
| 385 | } |
| 386 |
| 387 | if (in_len & 15) { |
| 388 | aead_aes_gcm_siv_asm_crypt_last_block(gcm_siv_ctx->is_128_bit, out, in, |
| 389 | in_len, tag, &enc_key_expanded); |
| 390 | } |
| 391 |
| 392 | OPENSSL_memcpy(out_tag, tag, sizeof(tag)); |
| 393 | *out_tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN; |
| 394 |
| 395 | return 1; |
| 396 | } |
| 397 |
| 398 | int aead_aes_gcm_siv_asm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 399 | const uint8_t *nonce, size_t nonce_len, |
| 400 | const uint8_t *in, size_t in_len, |
| 401 | const uint8_t *in_tag, size_t in_tag_len, |
| 402 | const uint8_t *ad, size_t ad_len) { |
| 403 | const uint64_t ad_len_64 = ad_len; |
| 404 | if (ad_len_64 >= (UINT64_C(1) << 61)) { |
| 405 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 406 | return 0; |
| 407 | } |
| 408 |
| 409 | const uint64_t in_len_64 = in_len; |
| 410 | if (in_len_64 > UINT64_C(1) << 36 || |
| 411 | in_tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN) { |
| 412 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 413 | return 0; |
| 414 | } |
| 415 |
| 416 | if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) { |
| 417 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 418 | return 0; |
| 419 | } |
| 420 |
| 421 | const struct aead_aes_gcm_siv_asm_ctx *gcm_siv_ctx = asm_ctx_from_ctx(ctx); |
| 422 |
| 423 | alignas(16) uint64_t record_auth_key[2]; |
| 424 | alignas(16) uint64_t record_enc_key[4]; |
| 425 | aead_aes_gcm_siv_kdf(gcm_siv_ctx->is_128_bit, gcm_siv_ctx, record_auth_key, |
| 426 | record_enc_key, nonce); |
| 427 |
| 428 | struct aead_aes_gcm_siv_asm_ctx expanded_key; |
| 429 | if (gcm_siv_ctx->is_128_bit) { |
| 430 | aes128gcmsiv_aes_ks((const uint8_t *)record_enc_key, &expanded_key.key[0]); |
| 431 | } else { |
| 432 | aes256gcmsiv_aes_ks((const uint8_t *)record_enc_key, &expanded_key.key[0]); |
| 433 | } |
| 434 | // calculated_tag is 16*8 bytes, rather than 16 bytes, because |
| 435 | // aes[128|256]gcmsiv_dec uses the extra as scratch space. |
| 436 | alignas(16) uint8_t calculated_tag[16 * 8] = {0}; |
| 437 |
| 438 | OPENSSL_memset(calculated_tag, 0, EVP_AEAD_AES_GCM_SIV_TAG_LEN); |
| 439 | const size_t ad_blocks = ad_len / 16; |
| 440 | aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key, ad, |
| 441 | ad_blocks); |
| 442 |
| 443 | uint8_t scratch[16]; |
| 444 | if (ad_len & 15) { |
| 445 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
| 446 | OPENSSL_memcpy(scratch, &ad[ad_len & ~15], ad_len & 15); |
| 447 | aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key, |
| 448 | scratch, 1); |
| 449 | } |
| 450 |
| 451 | alignas(16) uint8_t htable[16 * 6]; |
| 452 | aesgcmsiv_htable6_init(htable, (const uint8_t *)record_auth_key); |
| 453 |
| 454 | // aes[128|256]gcmsiv_dec needs access to the claimed tag. So it's put into |
| 455 | // its scratch space. |
| 456 | memcpy(calculated_tag + 16, in_tag, EVP_AEAD_AES_GCM_SIV_TAG_LEN); |
| 457 | if (gcm_siv_ctx->is_128_bit) { |
| 458 | aes128gcmsiv_dec(in, out, calculated_tag, htable, &expanded_key, in_len); |
| 459 | } else { |
| 460 | aes256gcmsiv_dec(in, out, calculated_tag, htable, &expanded_key, in_len); |
| 461 | } |
| 462 |
| 463 | if (in_len & 15) { |
| 464 | aead_aes_gcm_siv_asm_crypt_last_block(gcm_siv_ctx->is_128_bit, out, in, |
| 465 | in_len, in_tag, &expanded_key); |
| 466 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
| 467 | OPENSSL_memcpy(scratch, out + (in_len & ~15), in_len & 15); |
| 468 | aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key, |
| 469 | scratch, 1); |
| 470 | } |
| 471 |
| 472 | uint8_t length_block[16]; |
| 473 | CRYPTO_store_u64_le(length_block, ad_len * 8); |
| 474 | CRYPTO_store_u64_le(length_block + 8, in_len * 8); |
| 475 | aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key, |
| 476 | length_block, 1); |
| 477 |
| 478 | for (size_t i = 0; i < 12; i++) { |
| 479 | calculated_tag[i] ^= nonce[i]; |
| 480 | } |
| 481 |
| 482 | calculated_tag[15] &= 0x7f; |
| 483 |
| 484 | if (gcm_siv_ctx->is_128_bit) { |
| 485 | aes128gcmsiv_ecb_enc_block(calculated_tag, calculated_tag, &expanded_key); |
| 486 | } else { |
| 487 | aes256gcmsiv_ecb_enc_block(calculated_tag, calculated_tag, &expanded_key); |
| 488 | } |
| 489 |
| 490 | if (CRYPTO_memcmp(calculated_tag, in_tag, EVP_AEAD_AES_GCM_SIV_TAG_LEN) != |
| 491 | 0) { |
| 492 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 493 | return 0; |
| 494 | } |
| 495 |
| 496 | return 1; |
| 497 | } |
| 498 |
| 499 | const EVP_AEAD aead_aes_128_gcm_siv_asm = { |
| 500 | 16, // key length |
| 501 | EVP_AEAD_AES_GCM_SIV_NONCE_LEN, // nonce length |
| 502 | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // overhead |
| 503 | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // max tag length |
| 504 | 0, // seal_scatter_supports_extra_in |
| 505 |
| 506 | aead_aes_gcm_siv_asm_init, |
| 507 | NULL /* init_with_direction */, |
| 508 | aead_aes_gcm_siv_asm_cleanup, |
| 509 | NULL /* open */, |
| 510 | aead_aes_gcm_siv_asm_seal_scatter, |
| 511 | aead_aes_gcm_siv_asm_open_gather, |
| 512 | NULL /* get_iv */, |
| 513 | NULL /* tag_len */, |
| 514 | }; |
| 515 |
| 516 | const EVP_AEAD aead_aes_256_gcm_siv_asm = { |
| 517 | 32, // key length |
| 518 | EVP_AEAD_AES_GCM_SIV_NONCE_LEN, // nonce length |
| 519 | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // overhead |
| 520 | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // max tag length |
| 521 | 0, // seal_scatter_supports_extra_in |
| 522 |
| 523 | aead_aes_gcm_siv_asm_init, |
| 524 | NULL /* init_with_direction */, |
| 525 | aead_aes_gcm_siv_asm_cleanup, |
| 526 | NULL /* open */, |
| 527 | aead_aes_gcm_siv_asm_seal_scatter, |
| 528 | aead_aes_gcm_siv_asm_open_gather, |
| 529 | NULL /* get_iv */, |
| 530 | NULL /* tag_len */, |
| 531 | }; |
| 532 |
| 533 | #endif // X86_64 && !NO_ASM && !WINDOWS |
| 534 |
| 535 |
| 536 | struct aead_aes_gcm_siv_ctx { |
| 537 | union { |
| 538 | double align; |
| 539 | AES_KEY ks; |
| 540 | } ks; |
| 541 | block128_f kgk_block; |
| 542 | unsigned is_256 : 1; |
| 543 | }; |
| 544 |
| 545 | static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >= |
| 546 | sizeof(struct aead_aes_gcm_siv_ctx), |
| 547 | "AEAD state is too small"); |
| 548 | static_assert(alignof(union evp_aead_ctx_st_state) >= |
| 549 | alignof(struct aead_aes_gcm_siv_ctx), |
| 550 | "AEAD state has insufficient alignment"); |
| 551 |
| 552 | int aead_aes_gcm_siv_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, |
| 553 | size_t tag_len) { |
| 554 | const size_t key_bits = key_len * 8; |
| 555 |
| 556 | if (key_bits != 128 && key_bits != 256) { |
| 557 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
| 558 | return 0; // EVP_AEAD_CTX_init should catch this. |
| 559 | } |
| 560 |
| 561 | if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { |
| 562 | tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN; |
| 563 | } |
| 564 | if (tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN) { |
| 565 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE); |
| 566 | return 0; |
| 567 | } |
| 568 |
| 569 | struct aead_aes_gcm_siv_ctx *gcm_siv_ctx = |
| 570 | (struct aead_aes_gcm_siv_ctx *)&ctx->state; |
| 571 | OPENSSL_memset(gcm_siv_ctx, 0, sizeof(struct aead_aes_gcm_siv_ctx)); |
| 572 |
| 573 | aes_ctr_set_key(&gcm_siv_ctx->ks.ks, NULL, &gcm_siv_ctx->kgk_block, key, |
| 574 | key_len); |
| 575 | gcm_siv_ctx->is_256 = (key_len == 32); |
| 576 | ctx->tag_len = tag_len; |
| 577 |
| 578 | return 1; |
| 579 | } |
| 580 |
| 581 | void aead_aes_gcm_siv_cleanup(EVP_AEAD_CTX *ctx) {} |
| 582 |
| 583 | // gcm_siv_crypt encrypts (or decrypts—it's the same thing) |in_len| bytes from |
| 584 | // |in| to |out|, using the block function |enc_block| with |key| in counter |
| 585 | // mode, starting at |initial_counter|. This differs from the traditional |
| 586 | // counter mode code in that the counter is handled little-endian, only the |
| 587 | // first four bytes are used and the GCM-SIV tweak to the final byte is |
| 588 | // applied. The |in| and |out| pointers may be equal but otherwise must not |
| 589 | // alias. |
| 590 | void gcm_siv_crypt(uint8_t *out, const uint8_t *in, size_t in_len, |
| 591 | const uint8_t initial_counter[AES_BLOCK_SIZE], |
| 592 | block128_f enc_block, const AES_KEY *key) { |
| 593 | uint8_t counter[16]; |
| 594 |
| 595 | OPENSSL_memcpy(counter, initial_counter, AES_BLOCK_SIZE); |
| 596 | counter[15] |= 0x80; |
| 597 |
| 598 | for (size_t done = 0; done < in_len;) { |
| 599 | uint8_t keystream[AES_BLOCK_SIZE]; |
| 600 | enc_block(counter, keystream, key); |
| 601 | CRYPTO_store_u32_le(counter, CRYPTO_load_u32_le(counter) + 1); |
| 602 |
| 603 | size_t todo = AES_BLOCK_SIZE; |
| 604 | if (in_len - done < todo) { |
| 605 | todo = in_len - done; |
| 606 | } |
| 607 |
| 608 | for (size_t i = 0; i < todo; i++) { |
| 609 | out[done + i] = keystream[i] ^ in[done + i]; |
| 610 | } |
| 611 |
| 612 | done += todo; |
| 613 | } |
| 614 | } |
| 615 |
| 616 |
| 617 | // POLYVAL. |
| 618 | // |
| 619 | // POLYVAL is a polynomial authenticator that operates over a field very |
| 620 | // similar to the one that GHASH uses. See |
| 621 | // https://www.rfc-editor.org/rfc/rfc8452.html#section-3. |
| 622 |
| 623 | // POLYVAL(H, X_1, ..., X_n) = |
| 624 | // ByteReverse(GHASH(mulX_GHASH(ByteReverse(H)), ByteReverse(X_1), ..., |
| 625 | // ByteReverse(X_n))). |
| 626 | // |
| 627 | // See https://www.rfc-editor.org/rfc/rfc8452.html#appendix-A. |
| 628 |
| 629 | struct polyval_ctx { |
| 630 | uint8_t S[16]; |
| 631 | u128 Htable[16]; |
| 632 | gmult_func gmult; |
| 633 | ghash_func ghash; |
| 634 | }; |
| 635 |
| 636 | // byte_reverse reverses the order of the bytes in |b->c|. |
| 637 | void byte_reverse(uint8_t b[16]) { |
| 638 | uint64_t hi = CRYPTO_load_u64_le(b); |
| 639 | uint64_t lo = CRYPTO_load_u64_le(b + 8); |
| 640 | CRYPTO_store_u64_le(b, CRYPTO_bswap8(lo)); |
| 641 | CRYPTO_store_u64_le(b + 8, CRYPTO_bswap8(hi)); |
| 642 | } |
| 643 |
| 644 | // reverse_and_mulX_ghash interprets |b| as a reversed element of the GHASH |
| 645 | // field, multiplies that by 'x' and serialises the result back into |b|, but |
| 646 | // with GHASH's backwards bit ordering. |
| 647 | void reverse_and_mulX_ghash(uint8_t b[16]) { |
| 648 | uint64_t hi = CRYPTO_load_u64_le(b); |
| 649 | uint64_t lo = CRYPTO_load_u64_le(b + 8); |
| 650 | const crypto_word_t carry = constant_time_eq_w(hi & 1, 1); |
| 651 | hi >>= 1; |
| 652 | hi |= lo << 63; |
| 653 | lo >>= 1; |
| 654 | lo ^= ((uint64_t)constant_time_select_w(carry, 0xe1, 0)) << 56; |
| 655 |
| 656 | CRYPTO_store_u64_le(b, CRYPTO_bswap8(lo)); |
| 657 | CRYPTO_store_u64_le(b + 8, CRYPTO_bswap8(hi)); |
| 658 | } |
| 659 |
| 660 | void crypto_polyval_init(struct polyval_ctx *ctx, const uint8_t key[16]) { |
| 661 | alignas(8) uint8_t H[16]; |
| 662 | OPENSSL_memcpy(H, key, 16); |
| 663 | reverse_and_mulX_ghash(H); |
| 664 |
| 665 | CRYPTO_ghash_init(&ctx->gmult, &ctx->ghash, ctx->Htable, H); |
| 666 | OPENSSL_memset(&ctx->S, 0, sizeof(ctx->S)); |
| 667 | } |
| 668 |
| 669 | void crypto_polyval_update_blocks(struct polyval_ctx *ctx, const uint8_t *in, |
| 670 | size_t in_len) { |
| 671 | assert((in_len & 15) == 0); |
| 672 | alignas(8) uint8_t buf[32 * 16]; |
| 673 |
| 674 | while (in_len > 0) { |
| 675 | size_t todo = in_len; |
| 676 | if (todo > sizeof(buf)) { |
| 677 | todo = sizeof(buf); |
| 678 | } |
| 679 | OPENSSL_memcpy(buf, in, todo); |
| 680 | in += todo; |
| 681 | in_len -= todo; |
| 682 |
| 683 | size_t blocks = todo / 16; |
| 684 | for (size_t i = 0; i < blocks; i++) { |
| 685 | byte_reverse(buf + 16 * i); |
| 686 | } |
| 687 |
| 688 | ctx->ghash(ctx->S, ctx->Htable, buf, todo); |
| 689 | } |
| 690 | } |
| 691 |
| 692 | void crypto_polyval_finish(const struct polyval_ctx *ctx, uint8_t out[16]) { |
| 693 | OPENSSL_memcpy(out, &ctx->S, 16); |
| 694 | byte_reverse(out); |
| 695 | } |
| 696 |
| 697 | // gcm_siv_polyval evaluates POLYVAL at |auth_key| on the given plaintext and |
| 698 | // AD. The result is written to |out_tag|. |
| 699 | void gcm_siv_polyval(uint8_t out_tag[16], const uint8_t *in, size_t in_len, |
| 700 | const uint8_t *ad, size_t ad_len, |
| 701 | const uint8_t auth_key[16], |
| 702 | const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN]) { |
| 703 | struct polyval_ctx polyval_ctx; |
| 704 | crypto_polyval_init(&polyval_ctx, auth_key); |
| 705 |
| 706 | crypto_polyval_update_blocks(&polyval_ctx, ad, ad_len & ~15); |
| 707 |
| 708 | uint8_t scratch[16]; |
| 709 | if (ad_len & 15) { |
| 710 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
| 711 | OPENSSL_memcpy(scratch, &ad[ad_len & ~15], ad_len & 15); |
| 712 | crypto_polyval_update_blocks(&polyval_ctx, scratch, sizeof(scratch)); |
| 713 | } |
| 714 |
| 715 | crypto_polyval_update_blocks(&polyval_ctx, in, in_len & ~15); |
| 716 | if (in_len & 15) { |
| 717 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
| 718 | OPENSSL_memcpy(scratch, &in[in_len & ~15], in_len & 15); |
| 719 | crypto_polyval_update_blocks(&polyval_ctx, scratch, sizeof(scratch)); |
| 720 | } |
| 721 |
| 722 | uint8_t length_block[16]; |
| 723 | CRYPTO_store_u64_le(length_block, ((uint64_t)ad_len) * 8); |
| 724 | CRYPTO_store_u64_le(length_block + 8, ((uint64_t)in_len) * 8); |
| 725 | crypto_polyval_update_blocks(&polyval_ctx, length_block, |
| 726 | sizeof(length_block)); |
| 727 |
| 728 | crypto_polyval_finish(&polyval_ctx, out_tag); |
| 729 | for (size_t i = 0; i < EVP_AEAD_AES_GCM_SIV_NONCE_LEN; i++) { |
| 730 | out_tag[i] ^= nonce[i]; |
| 731 | } |
| 732 | out_tag[15] &= 0x7f; |
| 733 | } |
| 734 |
| 735 | // gcm_siv_record_keys contains the keys used for a specific GCM-SIV record. |
| 736 | struct gcm_siv_record_keys { |
| 737 | uint8_t auth_key[16]; |
| 738 | union { |
| 739 | double align; |
| 740 | AES_KEY ks; |
| 741 | } enc_key; |
| 742 | block128_f enc_block; |
| 743 | }; |
| 744 |
| 745 | // gcm_siv_keys calculates the keys for a specific GCM-SIV record with the |
| 746 | // given nonce and writes them to |*out_keys|. |
| 747 | void gcm_siv_keys(const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx, |
| 748 | struct gcm_siv_record_keys *out_keys, |
| 749 | const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN]) { |
| 750 | const AES_KEY *const key = &gcm_siv_ctx->ks.ks; |
| 751 | uint8_t key_material[(128 /* POLYVAL key */ + 256 /* max AES key */) / 8]; |
| 752 | const size_t blocks_needed = gcm_siv_ctx->is_256 ? 6 : 4; |
| 753 |
| 754 | uint8_t counter[AES_BLOCK_SIZE]; |
| 755 | OPENSSL_memset(counter, 0, AES_BLOCK_SIZE - EVP_AEAD_AES_GCM_SIV_NONCE_LEN); |
| 756 | OPENSSL_memcpy(counter + AES_BLOCK_SIZE - EVP_AEAD_AES_GCM_SIV_NONCE_LEN, |
| 757 | nonce, EVP_AEAD_AES_GCM_SIV_NONCE_LEN); |
| 758 | for (size_t i = 0; i < blocks_needed; i++) { |
| 759 | counter[0] = i; |
| 760 |
| 761 | uint8_t ciphertext[AES_BLOCK_SIZE]; |
| 762 | gcm_siv_ctx->kgk_block(counter, ciphertext, key); |
| 763 | OPENSSL_memcpy(&key_material[i * 8], ciphertext, 8); |
| 764 | } |
| 765 |
| 766 | OPENSSL_memcpy(out_keys->auth_key, key_material, 16); |
| 767 | // Note the |ctr128_f| function uses a big-endian couner, while AES-GCM-SIV |
| 768 | // uses a little-endian counter. We ignore the return value and only use |
| 769 | // |block128_f|. This has a significant performance cost for the fallback |
| 770 | // bitsliced AES implementations (bsaes and aes_nohw). |
| 771 | // |
| 772 | // We currently do not consider AES-GCM-SIV to be performance-sensitive on |
| 773 | // client hardware. If this changes, we can write little-endian |ctr128_f| |
| 774 | // functions. |
| 775 | aes_ctr_set_key(&out_keys->enc_key.ks, NULL, &out_keys->enc_block, |
| 776 | key_material + 16, gcm_siv_ctx->is_256 ? 32 : 16); |
| 777 | } |
| 778 |
| 779 | int aead_aes_gcm_siv_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 780 | uint8_t *out_tag, size_t *out_tag_len, |
| 781 | size_t max_out_tag_len, const uint8_t *nonce, |
| 782 | size_t nonce_len, const uint8_t *in, |
| 783 | size_t in_len, const uint8_t *extra_in, |
| 784 | size_t extra_in_len, const uint8_t *ad, |
| 785 | size_t ad_len) { |
| 786 | const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx = |
| 787 | (struct aead_aes_gcm_siv_ctx *)&ctx->state; |
| 788 | const uint64_t in_len_64 = in_len; |
| 789 | const uint64_t ad_len_64 = ad_len; |
| 790 |
| 791 | if (in_len + EVP_AEAD_AES_GCM_SIV_TAG_LEN < in_len || |
| 792 | in_len_64 > (UINT64_C(1) << 36) || ad_len_64 >= (UINT64_C(1) << 61)) { |
| 793 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 794 | return 0; |
| 795 | } |
| 796 |
| 797 | if (max_out_tag_len < EVP_AEAD_AES_GCM_SIV_TAG_LEN) { |
| 798 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 799 | return 0; |
| 800 | } |
| 801 |
| 802 | if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) { |
| 803 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 804 | return 0; |
| 805 | } |
| 806 |
| 807 | struct gcm_siv_record_keys keys; |
| 808 | gcm_siv_keys(gcm_siv_ctx, &keys, nonce); |
| 809 |
| 810 | uint8_t tag[16]; |
| 811 | gcm_siv_polyval(tag, in, in_len, ad, ad_len, keys.auth_key, nonce); |
| 812 | keys.enc_block(tag, tag, &keys.enc_key.ks); |
| 813 |
| 814 | gcm_siv_crypt(out, in, in_len, tag, keys.enc_block, &keys.enc_key.ks); |
| 815 |
| 816 | OPENSSL_memcpy(out_tag, tag, EVP_AEAD_AES_GCM_SIV_TAG_LEN); |
| 817 | *out_tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN; |
| 818 |
| 819 | return 1; |
| 820 | } |
| 821 |
| 822 | int aead_aes_gcm_siv_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 823 | const uint8_t *nonce, size_t nonce_len, |
| 824 | const uint8_t *in, size_t in_len, |
| 825 | const uint8_t *in_tag, size_t in_tag_len, |
| 826 | const uint8_t *ad, size_t ad_len) { |
| 827 | const uint64_t ad_len_64 = ad_len; |
| 828 | if (ad_len_64 >= (UINT64_C(1) << 61)) { |
| 829 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 830 | return 0; |
| 831 | } |
| 832 |
| 833 | const uint64_t in_len_64 = in_len; |
| 834 | if (in_tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN || |
| 835 | in_len_64 > (UINT64_C(1) << 36) + AES_BLOCK_SIZE) { |
| 836 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 837 | return 0; |
| 838 | } |
| 839 |
| 840 | if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) { |
| 841 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 842 | return 0; |
| 843 | } |
| 844 |
| 845 | const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx = |
| 846 | (struct aead_aes_gcm_siv_ctx *)&ctx->state; |
| 847 |
| 848 | struct gcm_siv_record_keys keys; |
| 849 | gcm_siv_keys(gcm_siv_ctx, &keys, nonce); |
| 850 |
| 851 | gcm_siv_crypt(out, in, in_len, in_tag, keys.enc_block, &keys.enc_key.ks); |
| 852 |
| 853 | uint8_t expected_tag[EVP_AEAD_AES_GCM_SIV_TAG_LEN]; |
| 854 | gcm_siv_polyval(expected_tag, out, in_len, ad, ad_len, keys.auth_key, nonce); |
| 855 | keys.enc_block(expected_tag, expected_tag, &keys.enc_key.ks); |
| 856 |
| 857 | if (CRYPTO_memcmp(expected_tag, in_tag, sizeof(expected_tag)) != 0) { |
| 858 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 859 | return 0; |
| 860 | } |
| 861 |
| 862 | return 1; |
| 863 | } |
| 864 |
| 865 | const EVP_AEAD aead_aes_128_gcm_siv = { |
| 866 | 16, // key length |
| 867 | EVP_AEAD_AES_GCM_SIV_NONCE_LEN, // nonce length |
| 868 | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // overhead |
| 869 | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // max tag length |
| 870 | 0, // seal_scatter_supports_extra_in |
| 871 |
| 872 | aead_aes_gcm_siv_init, |
| 873 | NULL /* init_with_direction */, |
| 874 | aead_aes_gcm_siv_cleanup, |
| 875 | NULL /* open */, |
| 876 | aead_aes_gcm_siv_seal_scatter, |
| 877 | aead_aes_gcm_siv_open_gather, |
| 878 | NULL /* get_iv */, |
| 879 | NULL /* tag_len */, |
| 880 | }; |
| 881 |
| 882 | const EVP_AEAD aead_aes_256_gcm_siv = { |
| 883 | 32, // key length |
| 884 | EVP_AEAD_AES_GCM_SIV_NONCE_LEN, // nonce length |
| 885 | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // overhead |
| 886 | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // max tag length |
| 887 | 0, // seal_scatter_supports_extra_in |
| 888 |
| 889 | aead_aes_gcm_siv_init, |
| 890 | NULL /* init_with_direction */, |
| 891 | aead_aes_gcm_siv_cleanup, |
| 892 | NULL /* open */, |
| 893 | aead_aes_gcm_siv_seal_scatter, |
| 894 | aead_aes_gcm_siv_open_gather, |
| 895 | NULL /* get_iv */, |
| 896 | NULL /* tag_len */, |
| 897 | }; |
| 898 | } // namespace |
| 899 |
| 900 | #if defined(AES_GCM_SIV_ASM) |
| 901 |
| 902 | const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void) { |
| 903 | if (CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable()) { |
| 904 | return &aead_aes_128_gcm_siv_asm; |
| 905 | } |
| 906 | return &aead_aes_128_gcm_siv; |
| 907 | } |
| 908 |
| 909 | const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void) { |
| 910 | if (CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable()) { |
| 911 | return &aead_aes_256_gcm_siv_asm; |
| 912 | } |
| 913 | return &aead_aes_256_gcm_siv; |
| 914 | } |
| 915 |
| 916 | #else |
| 917 |
| 918 | const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void) { return &aead_aes_128_gcm_siv; } |
| 919 |
| 920 | const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void) { return &aead_aes_256_gcm_siv; } |
| 921 |
| 922 | #endif // AES_GCM_SIV_ASM |
| 923 |