1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy /* 22eda14cbcSMatt Macy * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 23eda14cbcSMatt Macy */ 24eda14cbcSMatt Macy 25eda14cbcSMatt Macy #include <sys/zfs_context.h> 26eda14cbcSMatt Macy #include <modes/modes.h> 27eda14cbcSMatt Macy #include <sys/crypto/common.h> 28eda14cbcSMatt Macy #include <sys/crypto/icp.h> 29eda14cbcSMatt Macy #include <sys/crypto/impl.h> 30eda14cbcSMatt Macy #include <sys/byteorder.h> 31eda14cbcSMatt Macy #include <sys/simd.h> 32eda14cbcSMatt Macy #include <modes/gcm_impl.h> 33eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 34eda14cbcSMatt Macy #include <aes/aes_impl.h> 35eda14cbcSMatt Macy #endif 36eda14cbcSMatt Macy 37eda14cbcSMatt Macy #define GHASH(c, d, t, o) \ 38eda14cbcSMatt Macy xor_block((uint8_t *)(d), (uint8_t *)(c)->gcm_ghash); \ 39eda14cbcSMatt Macy (o)->mul((uint64_t *)(void *)(c)->gcm_ghash, (c)->gcm_H, \ 40eda14cbcSMatt Macy (uint64_t *)(void *)(t)); 41eda14cbcSMatt Macy 42eda14cbcSMatt Macy /* Select GCM implementation */ 43eda14cbcSMatt Macy #define IMPL_FASTEST (UINT32_MAX) 44eda14cbcSMatt Macy #define IMPL_CYCLE (UINT32_MAX-1) 45eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 46eda14cbcSMatt Macy #define IMPL_AVX (UINT32_MAX-2) 47eda14cbcSMatt Macy #endif 48eda14cbcSMatt Macy #define GCM_IMPL_READ(i) (*(volatile uint32_t *) &(i)) 49eda14cbcSMatt Macy static uint32_t icp_gcm_impl = IMPL_FASTEST; 50eda14cbcSMatt Macy static uint32_t user_sel_impl = IMPL_FASTEST; 51eda14cbcSMatt Macy 52eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 53eda14cbcSMatt Macy /* Does the architecture we run on support the MOVBE instruction? */ 54eda14cbcSMatt Macy boolean_t gcm_avx_can_use_movbe = B_FALSE; 55eda14cbcSMatt Macy /* 56eda14cbcSMatt Macy * Whether to use the optimized openssl gcm and ghash implementations. 57eda14cbcSMatt Macy * Set to true if module parameter icp_gcm_impl == "avx". 58eda14cbcSMatt Macy */ 59eda14cbcSMatt Macy static boolean_t gcm_use_avx = B_FALSE; 60eda14cbcSMatt Macy #define GCM_IMPL_USE_AVX (*(volatile boolean_t *)&gcm_use_avx) 61eda14cbcSMatt Macy 627877fdebSMatt Macy extern boolean_t atomic_toggle_boolean_nv(volatile boolean_t *); 637877fdebSMatt Macy 64eda14cbcSMatt Macy static inline boolean_t gcm_avx_will_work(void); 65eda14cbcSMatt Macy static inline void gcm_set_avx(boolean_t); 66eda14cbcSMatt Macy static inline boolean_t gcm_toggle_avx(void); 677877fdebSMatt Macy static inline size_t gcm_simd_get_htab_size(boolean_t); 68eda14cbcSMatt Macy 69eda14cbcSMatt Macy static int gcm_mode_encrypt_contiguous_blocks_avx(gcm_ctx_t *, char *, size_t, 70eda14cbcSMatt Macy crypto_data_t *, size_t); 71eda14cbcSMatt Macy 72eda14cbcSMatt Macy static int gcm_encrypt_final_avx(gcm_ctx_t *, crypto_data_t *, size_t); 73eda14cbcSMatt Macy static int gcm_decrypt_final_avx(gcm_ctx_t *, crypto_data_t *, size_t); 74eda14cbcSMatt Macy static int gcm_init_avx(gcm_ctx_t *, unsigned char *, size_t, unsigned char *, 75eda14cbcSMatt Macy size_t, size_t); 76eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */ 77eda14cbcSMatt Macy 78eda14cbcSMatt Macy /* 79eda14cbcSMatt Macy * Encrypt multiple blocks of data in GCM mode. Decrypt for GCM mode 80eda14cbcSMatt Macy * is done in another function. 81eda14cbcSMatt Macy */ 82eda14cbcSMatt Macy int 83eda14cbcSMatt Macy gcm_mode_encrypt_contiguous_blocks(gcm_ctx_t *ctx, char *data, size_t length, 84eda14cbcSMatt Macy crypto_data_t *out, size_t block_size, 85eda14cbcSMatt Macy int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 86eda14cbcSMatt Macy void (*copy_block)(uint8_t *, uint8_t *), 87eda14cbcSMatt Macy void (*xor_block)(uint8_t *, uint8_t *)) 88eda14cbcSMatt Macy { 89eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 90eda14cbcSMatt Macy if (ctx->gcm_use_avx == B_TRUE) 91eda14cbcSMatt Macy return (gcm_mode_encrypt_contiguous_blocks_avx( 92eda14cbcSMatt Macy ctx, data, length, out, block_size)); 93eda14cbcSMatt Macy #endif 94eda14cbcSMatt Macy 95eda14cbcSMatt Macy const gcm_impl_ops_t *gops; 96eda14cbcSMatt Macy size_t remainder = length; 97eda14cbcSMatt Macy size_t need = 0; 98eda14cbcSMatt Macy uint8_t *datap = (uint8_t *)data; 99eda14cbcSMatt Macy uint8_t *blockp; 100eda14cbcSMatt Macy uint8_t *lastp; 101eda14cbcSMatt Macy void *iov_or_mp; 102eda14cbcSMatt Macy offset_t offset; 103eda14cbcSMatt Macy uint8_t *out_data_1; 104eda14cbcSMatt Macy uint8_t *out_data_2; 105eda14cbcSMatt Macy size_t out_data_1_len; 106eda14cbcSMatt Macy uint64_t counter; 107eda14cbcSMatt Macy uint64_t counter_mask = ntohll(0x00000000ffffffffULL); 108eda14cbcSMatt Macy 109eda14cbcSMatt Macy if (length + ctx->gcm_remainder_len < block_size) { 110eda14cbcSMatt Macy /* accumulate bytes here and return */ 111da5137abSMartin Matuska memcpy((uint8_t *)ctx->gcm_remainder + ctx->gcm_remainder_len, 112da5137abSMartin Matuska datap, 113eda14cbcSMatt Macy length); 114eda14cbcSMatt Macy ctx->gcm_remainder_len += length; 115eda14cbcSMatt Macy if (ctx->gcm_copy_to == NULL) { 116eda14cbcSMatt Macy ctx->gcm_copy_to = datap; 117eda14cbcSMatt Macy } 118eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 119eda14cbcSMatt Macy } 120eda14cbcSMatt Macy 121eda14cbcSMatt Macy lastp = (uint8_t *)ctx->gcm_cb; 122eda14cbcSMatt Macy crypto_init_ptrs(out, &iov_or_mp, &offset); 123eda14cbcSMatt Macy 124eda14cbcSMatt Macy gops = gcm_impl_get_ops(); 125eda14cbcSMatt Macy do { 126eda14cbcSMatt Macy /* Unprocessed data from last call. */ 127eda14cbcSMatt Macy if (ctx->gcm_remainder_len > 0) { 128eda14cbcSMatt Macy need = block_size - ctx->gcm_remainder_len; 129eda14cbcSMatt Macy 130eda14cbcSMatt Macy if (need > remainder) 131eda14cbcSMatt Macy return (CRYPTO_DATA_LEN_RANGE); 132eda14cbcSMatt Macy 133da5137abSMartin Matuska memcpy(&((uint8_t *)ctx->gcm_remainder) 134da5137abSMartin Matuska [ctx->gcm_remainder_len], datap, need); 135eda14cbcSMatt Macy 136eda14cbcSMatt Macy blockp = (uint8_t *)ctx->gcm_remainder; 137eda14cbcSMatt Macy } else { 138eda14cbcSMatt Macy blockp = datap; 139eda14cbcSMatt Macy } 140eda14cbcSMatt Macy 141eda14cbcSMatt Macy /* 142eda14cbcSMatt Macy * Increment counter. Counter bits are confined 143eda14cbcSMatt Macy * to the bottom 32 bits of the counter block. 144eda14cbcSMatt Macy */ 145eda14cbcSMatt Macy counter = ntohll(ctx->gcm_cb[1] & counter_mask); 146eda14cbcSMatt Macy counter = htonll(counter + 1); 147eda14cbcSMatt Macy counter &= counter_mask; 148eda14cbcSMatt Macy ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter; 149eda14cbcSMatt Macy 150eda14cbcSMatt Macy encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb, 151eda14cbcSMatt Macy (uint8_t *)ctx->gcm_tmp); 152eda14cbcSMatt Macy xor_block(blockp, (uint8_t *)ctx->gcm_tmp); 153eda14cbcSMatt Macy 154eda14cbcSMatt Macy lastp = (uint8_t *)ctx->gcm_tmp; 155eda14cbcSMatt Macy 156eda14cbcSMatt Macy ctx->gcm_processed_data_len += block_size; 157eda14cbcSMatt Macy 158eda14cbcSMatt Macy crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1, 159eda14cbcSMatt Macy &out_data_1_len, &out_data_2, block_size); 160eda14cbcSMatt Macy 161eda14cbcSMatt Macy /* copy block to where it belongs */ 162eda14cbcSMatt Macy if (out_data_1_len == block_size) { 163eda14cbcSMatt Macy copy_block(lastp, out_data_1); 164eda14cbcSMatt Macy } else { 165da5137abSMartin Matuska memcpy(out_data_1, lastp, out_data_1_len); 166eda14cbcSMatt Macy if (out_data_2 != NULL) { 167da5137abSMartin Matuska memcpy(out_data_2, 168da5137abSMartin Matuska lastp + out_data_1_len, 169eda14cbcSMatt Macy block_size - out_data_1_len); 170eda14cbcSMatt Macy } 171eda14cbcSMatt Macy } 172eda14cbcSMatt Macy /* update offset */ 173eda14cbcSMatt Macy out->cd_offset += block_size; 174eda14cbcSMatt Macy 175eda14cbcSMatt Macy /* add ciphertext to the hash */ 176eda14cbcSMatt Macy GHASH(ctx, ctx->gcm_tmp, ctx->gcm_ghash, gops); 177eda14cbcSMatt Macy 178eda14cbcSMatt Macy /* Update pointer to next block of data to be processed. */ 179eda14cbcSMatt Macy if (ctx->gcm_remainder_len != 0) { 180eda14cbcSMatt Macy datap += need; 181eda14cbcSMatt Macy ctx->gcm_remainder_len = 0; 182eda14cbcSMatt Macy } else { 183eda14cbcSMatt Macy datap += block_size; 184eda14cbcSMatt Macy } 185eda14cbcSMatt Macy 186eda14cbcSMatt Macy remainder = (size_t)&data[length] - (size_t)datap; 187eda14cbcSMatt Macy 188eda14cbcSMatt Macy /* Incomplete last block. */ 189eda14cbcSMatt Macy if (remainder > 0 && remainder < block_size) { 190da5137abSMartin Matuska memcpy(ctx->gcm_remainder, datap, remainder); 191eda14cbcSMatt Macy ctx->gcm_remainder_len = remainder; 192eda14cbcSMatt Macy ctx->gcm_copy_to = datap; 193eda14cbcSMatt Macy goto out; 194eda14cbcSMatt Macy } 195eda14cbcSMatt Macy ctx->gcm_copy_to = NULL; 196eda14cbcSMatt Macy 197eda14cbcSMatt Macy } while (remainder > 0); 198eda14cbcSMatt Macy out: 199eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 200eda14cbcSMatt Macy } 201eda14cbcSMatt Macy 202eda14cbcSMatt Macy int 203eda14cbcSMatt Macy gcm_encrypt_final(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size, 204eda14cbcSMatt Macy int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 205eda14cbcSMatt Macy void (*copy_block)(uint8_t *, uint8_t *), 206eda14cbcSMatt Macy void (*xor_block)(uint8_t *, uint8_t *)) 207eda14cbcSMatt Macy { 208e92ffd9bSMartin Matuska (void) copy_block; 209eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 210eda14cbcSMatt Macy if (ctx->gcm_use_avx == B_TRUE) 211eda14cbcSMatt Macy return (gcm_encrypt_final_avx(ctx, out, block_size)); 212eda14cbcSMatt Macy #endif 213eda14cbcSMatt Macy 214eda14cbcSMatt Macy const gcm_impl_ops_t *gops; 215eda14cbcSMatt Macy uint64_t counter_mask = ntohll(0x00000000ffffffffULL); 216eda14cbcSMatt Macy uint8_t *ghash, *macp = NULL; 217eda14cbcSMatt Macy int i, rv; 218eda14cbcSMatt Macy 219eda14cbcSMatt Macy if (out->cd_length < 220eda14cbcSMatt Macy (ctx->gcm_remainder_len + ctx->gcm_tag_len)) { 221eda14cbcSMatt Macy return (CRYPTO_DATA_LEN_RANGE); 222eda14cbcSMatt Macy } 223eda14cbcSMatt Macy 224eda14cbcSMatt Macy gops = gcm_impl_get_ops(); 225eda14cbcSMatt Macy ghash = (uint8_t *)ctx->gcm_ghash; 226eda14cbcSMatt Macy 227eda14cbcSMatt Macy if (ctx->gcm_remainder_len > 0) { 228eda14cbcSMatt Macy uint64_t counter; 229eda14cbcSMatt Macy uint8_t *tmpp = (uint8_t *)ctx->gcm_tmp; 230eda14cbcSMatt Macy 231eda14cbcSMatt Macy /* 232eda14cbcSMatt Macy * Here is where we deal with data that is not a 233eda14cbcSMatt Macy * multiple of the block size. 234eda14cbcSMatt Macy */ 235eda14cbcSMatt Macy 236eda14cbcSMatt Macy /* 237eda14cbcSMatt Macy * Increment counter. 238eda14cbcSMatt Macy */ 239eda14cbcSMatt Macy counter = ntohll(ctx->gcm_cb[1] & counter_mask); 240eda14cbcSMatt Macy counter = htonll(counter + 1); 241eda14cbcSMatt Macy counter &= counter_mask; 242eda14cbcSMatt Macy ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter; 243eda14cbcSMatt Macy 244eda14cbcSMatt Macy encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb, 245eda14cbcSMatt Macy (uint8_t *)ctx->gcm_tmp); 246eda14cbcSMatt Macy 247eda14cbcSMatt Macy macp = (uint8_t *)ctx->gcm_remainder; 248da5137abSMartin Matuska memset(macp + ctx->gcm_remainder_len, 0, 249eda14cbcSMatt Macy block_size - ctx->gcm_remainder_len); 250eda14cbcSMatt Macy 251eda14cbcSMatt Macy /* XOR with counter block */ 252eda14cbcSMatt Macy for (i = 0; i < ctx->gcm_remainder_len; i++) { 253eda14cbcSMatt Macy macp[i] ^= tmpp[i]; 254eda14cbcSMatt Macy } 255eda14cbcSMatt Macy 256eda14cbcSMatt Macy /* add ciphertext to the hash */ 257eda14cbcSMatt Macy GHASH(ctx, macp, ghash, gops); 258eda14cbcSMatt Macy 259eda14cbcSMatt Macy ctx->gcm_processed_data_len += ctx->gcm_remainder_len; 260eda14cbcSMatt Macy } 261eda14cbcSMatt Macy 262eda14cbcSMatt Macy ctx->gcm_len_a_len_c[1] = 263eda14cbcSMatt Macy htonll(CRYPTO_BYTES2BITS(ctx->gcm_processed_data_len)); 264eda14cbcSMatt Macy GHASH(ctx, ctx->gcm_len_a_len_c, ghash, gops); 265eda14cbcSMatt Macy encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_J0, 266eda14cbcSMatt Macy (uint8_t *)ctx->gcm_J0); 267eda14cbcSMatt Macy xor_block((uint8_t *)ctx->gcm_J0, ghash); 268eda14cbcSMatt Macy 269eda14cbcSMatt Macy if (ctx->gcm_remainder_len > 0) { 270eda14cbcSMatt Macy rv = crypto_put_output_data(macp, out, ctx->gcm_remainder_len); 271eda14cbcSMatt Macy if (rv != CRYPTO_SUCCESS) 272eda14cbcSMatt Macy return (rv); 273eda14cbcSMatt Macy } 274eda14cbcSMatt Macy out->cd_offset += ctx->gcm_remainder_len; 275eda14cbcSMatt Macy ctx->gcm_remainder_len = 0; 276eda14cbcSMatt Macy rv = crypto_put_output_data(ghash, out, ctx->gcm_tag_len); 277eda14cbcSMatt Macy if (rv != CRYPTO_SUCCESS) 278eda14cbcSMatt Macy return (rv); 279eda14cbcSMatt Macy out->cd_offset += ctx->gcm_tag_len; 280eda14cbcSMatt Macy 281eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 282eda14cbcSMatt Macy } 283eda14cbcSMatt Macy 284eda14cbcSMatt Macy /* 285eda14cbcSMatt Macy * This will only deal with decrypting the last block of the input that 286eda14cbcSMatt Macy * might not be a multiple of block length. 287eda14cbcSMatt Macy */ 288eda14cbcSMatt Macy static void 289eda14cbcSMatt Macy gcm_decrypt_incomplete_block(gcm_ctx_t *ctx, size_t block_size, size_t index, 290eda14cbcSMatt Macy int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 291eda14cbcSMatt Macy void (*xor_block)(uint8_t *, uint8_t *)) 292eda14cbcSMatt Macy { 293eda14cbcSMatt Macy uint8_t *datap, *outp, *counterp; 294eda14cbcSMatt Macy uint64_t counter; 295eda14cbcSMatt Macy uint64_t counter_mask = ntohll(0x00000000ffffffffULL); 296eda14cbcSMatt Macy int i; 297eda14cbcSMatt Macy 298eda14cbcSMatt Macy /* 299eda14cbcSMatt Macy * Increment counter. 300eda14cbcSMatt Macy * Counter bits are confined to the bottom 32 bits 301eda14cbcSMatt Macy */ 302eda14cbcSMatt Macy counter = ntohll(ctx->gcm_cb[1] & counter_mask); 303eda14cbcSMatt Macy counter = htonll(counter + 1); 304eda14cbcSMatt Macy counter &= counter_mask; 305eda14cbcSMatt Macy ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter; 306eda14cbcSMatt Macy 307eda14cbcSMatt Macy datap = (uint8_t *)ctx->gcm_remainder; 308eda14cbcSMatt Macy outp = &((ctx->gcm_pt_buf)[index]); 309eda14cbcSMatt Macy counterp = (uint8_t *)ctx->gcm_tmp; 310eda14cbcSMatt Macy 311eda14cbcSMatt Macy /* authentication tag */ 312da5137abSMartin Matuska memset((uint8_t *)ctx->gcm_tmp, 0, block_size); 313da5137abSMartin Matuska memcpy((uint8_t *)ctx->gcm_tmp, datap, ctx->gcm_remainder_len); 314eda14cbcSMatt Macy 315eda14cbcSMatt Macy /* add ciphertext to the hash */ 316eda14cbcSMatt Macy GHASH(ctx, ctx->gcm_tmp, ctx->gcm_ghash, gcm_impl_get_ops()); 317eda14cbcSMatt Macy 318eda14cbcSMatt Macy /* decrypt remaining ciphertext */ 319eda14cbcSMatt Macy encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb, counterp); 320eda14cbcSMatt Macy 321eda14cbcSMatt Macy /* XOR with counter block */ 322eda14cbcSMatt Macy for (i = 0; i < ctx->gcm_remainder_len; i++) { 323eda14cbcSMatt Macy outp[i] = datap[i] ^ counterp[i]; 324eda14cbcSMatt Macy } 325eda14cbcSMatt Macy } 326eda14cbcSMatt Macy 327eda14cbcSMatt Macy int 328eda14cbcSMatt Macy gcm_mode_decrypt_contiguous_blocks(gcm_ctx_t *ctx, char *data, size_t length, 329eda14cbcSMatt Macy crypto_data_t *out, size_t block_size, 330eda14cbcSMatt Macy int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 331eda14cbcSMatt Macy void (*copy_block)(uint8_t *, uint8_t *), 332eda14cbcSMatt Macy void (*xor_block)(uint8_t *, uint8_t *)) 333eda14cbcSMatt Macy { 334e92ffd9bSMartin Matuska (void) out, (void) block_size, (void) encrypt_block, (void) copy_block, 335e92ffd9bSMartin Matuska (void) xor_block; 336eda14cbcSMatt Macy size_t new_len; 337eda14cbcSMatt Macy uint8_t *new; 338eda14cbcSMatt Macy 339eda14cbcSMatt Macy /* 340eda14cbcSMatt Macy * Copy contiguous ciphertext input blocks to plaintext buffer. 341eda14cbcSMatt Macy * Ciphertext will be decrypted in the final. 342eda14cbcSMatt Macy */ 343eda14cbcSMatt Macy if (length > 0) { 344eda14cbcSMatt Macy new_len = ctx->gcm_pt_buf_len + length; 345c03c5b1cSMartin Matuska new = vmem_alloc(new_len, KM_SLEEP); 346eda14cbcSMatt Macy if (new == NULL) { 347eda14cbcSMatt Macy vmem_free(ctx->gcm_pt_buf, ctx->gcm_pt_buf_len); 348eda14cbcSMatt Macy ctx->gcm_pt_buf = NULL; 349eda14cbcSMatt Macy return (CRYPTO_HOST_MEMORY); 350eda14cbcSMatt Macy } 351c03c5b1cSMartin Matuska 352c03c5b1cSMartin Matuska if (ctx->gcm_pt_buf != NULL) { 353da5137abSMartin Matuska memcpy(new, ctx->gcm_pt_buf, ctx->gcm_pt_buf_len); 354eda14cbcSMatt Macy vmem_free(ctx->gcm_pt_buf, ctx->gcm_pt_buf_len); 355c03c5b1cSMartin Matuska } else { 356c03c5b1cSMartin Matuska ASSERT0(ctx->gcm_pt_buf_len); 357c03c5b1cSMartin Matuska } 358c03c5b1cSMartin Matuska 359eda14cbcSMatt Macy ctx->gcm_pt_buf = new; 360eda14cbcSMatt Macy ctx->gcm_pt_buf_len = new_len; 361da5137abSMartin Matuska memcpy(&ctx->gcm_pt_buf[ctx->gcm_processed_data_len], data, 362eda14cbcSMatt Macy length); 363eda14cbcSMatt Macy ctx->gcm_processed_data_len += length; 364eda14cbcSMatt Macy } 365eda14cbcSMatt Macy 366eda14cbcSMatt Macy ctx->gcm_remainder_len = 0; 367eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 368eda14cbcSMatt Macy } 369eda14cbcSMatt Macy 370eda14cbcSMatt Macy int 371eda14cbcSMatt Macy gcm_decrypt_final(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size, 372eda14cbcSMatt Macy int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 373eda14cbcSMatt Macy void (*xor_block)(uint8_t *, uint8_t *)) 374eda14cbcSMatt Macy { 375eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 376eda14cbcSMatt Macy if (ctx->gcm_use_avx == B_TRUE) 377eda14cbcSMatt Macy return (gcm_decrypt_final_avx(ctx, out, block_size)); 378eda14cbcSMatt Macy #endif 379eda14cbcSMatt Macy 380eda14cbcSMatt Macy const gcm_impl_ops_t *gops; 381eda14cbcSMatt Macy size_t pt_len; 382eda14cbcSMatt Macy size_t remainder; 383eda14cbcSMatt Macy uint8_t *ghash; 384eda14cbcSMatt Macy uint8_t *blockp; 385eda14cbcSMatt Macy uint8_t *cbp; 386eda14cbcSMatt Macy uint64_t counter; 387eda14cbcSMatt Macy uint64_t counter_mask = ntohll(0x00000000ffffffffULL); 388eda14cbcSMatt Macy int processed = 0, rv; 389eda14cbcSMatt Macy 390eda14cbcSMatt Macy ASSERT(ctx->gcm_processed_data_len == ctx->gcm_pt_buf_len); 391eda14cbcSMatt Macy 392eda14cbcSMatt Macy gops = gcm_impl_get_ops(); 393eda14cbcSMatt Macy pt_len = ctx->gcm_processed_data_len - ctx->gcm_tag_len; 394eda14cbcSMatt Macy ghash = (uint8_t *)ctx->gcm_ghash; 395eda14cbcSMatt Macy blockp = ctx->gcm_pt_buf; 396eda14cbcSMatt Macy remainder = pt_len; 397eda14cbcSMatt Macy while (remainder > 0) { 398eda14cbcSMatt Macy /* Incomplete last block */ 399eda14cbcSMatt Macy if (remainder < block_size) { 400da5137abSMartin Matuska memcpy(ctx->gcm_remainder, blockp, remainder); 401eda14cbcSMatt Macy ctx->gcm_remainder_len = remainder; 402eda14cbcSMatt Macy /* 403eda14cbcSMatt Macy * not expecting anymore ciphertext, just 404eda14cbcSMatt Macy * compute plaintext for the remaining input 405eda14cbcSMatt Macy */ 406eda14cbcSMatt Macy gcm_decrypt_incomplete_block(ctx, block_size, 407eda14cbcSMatt Macy processed, encrypt_block, xor_block); 408eda14cbcSMatt Macy ctx->gcm_remainder_len = 0; 409eda14cbcSMatt Macy goto out; 410eda14cbcSMatt Macy } 411eda14cbcSMatt Macy /* add ciphertext to the hash */ 412eda14cbcSMatt Macy GHASH(ctx, blockp, ghash, gops); 413eda14cbcSMatt Macy 414eda14cbcSMatt Macy /* 415eda14cbcSMatt Macy * Increment counter. 416eda14cbcSMatt Macy * Counter bits are confined to the bottom 32 bits 417eda14cbcSMatt Macy */ 418eda14cbcSMatt Macy counter = ntohll(ctx->gcm_cb[1] & counter_mask); 419eda14cbcSMatt Macy counter = htonll(counter + 1); 420eda14cbcSMatt Macy counter &= counter_mask; 421eda14cbcSMatt Macy ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter; 422eda14cbcSMatt Macy 423eda14cbcSMatt Macy cbp = (uint8_t *)ctx->gcm_tmp; 424eda14cbcSMatt Macy encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb, cbp); 425eda14cbcSMatt Macy 426eda14cbcSMatt Macy /* XOR with ciphertext */ 427eda14cbcSMatt Macy xor_block(cbp, blockp); 428eda14cbcSMatt Macy 429eda14cbcSMatt Macy processed += block_size; 430eda14cbcSMatt Macy blockp += block_size; 431eda14cbcSMatt Macy remainder -= block_size; 432eda14cbcSMatt Macy } 433eda14cbcSMatt Macy out: 434eda14cbcSMatt Macy ctx->gcm_len_a_len_c[1] = htonll(CRYPTO_BYTES2BITS(pt_len)); 435eda14cbcSMatt Macy GHASH(ctx, ctx->gcm_len_a_len_c, ghash, gops); 436eda14cbcSMatt Macy encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_J0, 437eda14cbcSMatt Macy (uint8_t *)ctx->gcm_J0); 438eda14cbcSMatt Macy xor_block((uint8_t *)ctx->gcm_J0, ghash); 439eda14cbcSMatt Macy 440eda14cbcSMatt Macy /* compare the input authentication tag with what we calculated */ 441da5137abSMartin Matuska if (memcmp(&ctx->gcm_pt_buf[pt_len], ghash, ctx->gcm_tag_len)) { 442eda14cbcSMatt Macy /* They don't match */ 443eda14cbcSMatt Macy return (CRYPTO_INVALID_MAC); 444eda14cbcSMatt Macy } else { 445eda14cbcSMatt Macy rv = crypto_put_output_data(ctx->gcm_pt_buf, out, pt_len); 446eda14cbcSMatt Macy if (rv != CRYPTO_SUCCESS) 447eda14cbcSMatt Macy return (rv); 448eda14cbcSMatt Macy out->cd_offset += pt_len; 449eda14cbcSMatt Macy } 450eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 451eda14cbcSMatt Macy } 452eda14cbcSMatt Macy 453eda14cbcSMatt Macy static int 454eda14cbcSMatt Macy gcm_validate_args(CK_AES_GCM_PARAMS *gcm_param) 455eda14cbcSMatt Macy { 456eda14cbcSMatt Macy size_t tag_len; 457eda14cbcSMatt Macy 458eda14cbcSMatt Macy /* 459eda14cbcSMatt Macy * Check the length of the authentication tag (in bits). 460eda14cbcSMatt Macy */ 461eda14cbcSMatt Macy tag_len = gcm_param->ulTagBits; 462eda14cbcSMatt Macy switch (tag_len) { 463eda14cbcSMatt Macy case 32: 464eda14cbcSMatt Macy case 64: 465eda14cbcSMatt Macy case 96: 466eda14cbcSMatt Macy case 104: 467eda14cbcSMatt Macy case 112: 468eda14cbcSMatt Macy case 120: 469eda14cbcSMatt Macy case 128: 470eda14cbcSMatt Macy break; 471eda14cbcSMatt Macy default: 472eda14cbcSMatt Macy return (CRYPTO_MECHANISM_PARAM_INVALID); 473eda14cbcSMatt Macy } 474eda14cbcSMatt Macy 475eda14cbcSMatt Macy if (gcm_param->ulIvLen == 0) 476eda14cbcSMatt Macy return (CRYPTO_MECHANISM_PARAM_INVALID); 477eda14cbcSMatt Macy 478eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 479eda14cbcSMatt Macy } 480eda14cbcSMatt Macy 481eda14cbcSMatt Macy static void 482eda14cbcSMatt Macy gcm_format_initial_blocks(uchar_t *iv, ulong_t iv_len, 483eda14cbcSMatt Macy gcm_ctx_t *ctx, size_t block_size, 484eda14cbcSMatt Macy void (*copy_block)(uint8_t *, uint8_t *), 485eda14cbcSMatt Macy void (*xor_block)(uint8_t *, uint8_t *)) 486eda14cbcSMatt Macy { 487eda14cbcSMatt Macy const gcm_impl_ops_t *gops; 488eda14cbcSMatt Macy uint8_t *cb; 489eda14cbcSMatt Macy ulong_t remainder = iv_len; 490eda14cbcSMatt Macy ulong_t processed = 0; 491eda14cbcSMatt Macy uint8_t *datap, *ghash; 492eda14cbcSMatt Macy uint64_t len_a_len_c[2]; 493eda14cbcSMatt Macy 494eda14cbcSMatt Macy gops = gcm_impl_get_ops(); 495eda14cbcSMatt Macy ghash = (uint8_t *)ctx->gcm_ghash; 496eda14cbcSMatt Macy cb = (uint8_t *)ctx->gcm_cb; 497eda14cbcSMatt Macy if (iv_len == 12) { 498da5137abSMartin Matuska memcpy(cb, iv, 12); 499eda14cbcSMatt Macy cb[12] = 0; 500eda14cbcSMatt Macy cb[13] = 0; 501eda14cbcSMatt Macy cb[14] = 0; 502eda14cbcSMatt Macy cb[15] = 1; 503eda14cbcSMatt Macy /* J0 will be used again in the final */ 504eda14cbcSMatt Macy copy_block(cb, (uint8_t *)ctx->gcm_J0); 505eda14cbcSMatt Macy } else { 506eda14cbcSMatt Macy /* GHASH the IV */ 507eda14cbcSMatt Macy do { 508eda14cbcSMatt Macy if (remainder < block_size) { 509da5137abSMartin Matuska memset(cb, 0, block_size); 510da5137abSMartin Matuska memcpy(cb, &(iv[processed]), remainder); 511eda14cbcSMatt Macy datap = (uint8_t *)cb; 512eda14cbcSMatt Macy remainder = 0; 513eda14cbcSMatt Macy } else { 514eda14cbcSMatt Macy datap = (uint8_t *)(&(iv[processed])); 515eda14cbcSMatt Macy processed += block_size; 516eda14cbcSMatt Macy remainder -= block_size; 517eda14cbcSMatt Macy } 518eda14cbcSMatt Macy GHASH(ctx, datap, ghash, gops); 519eda14cbcSMatt Macy } while (remainder > 0); 520eda14cbcSMatt Macy 521eda14cbcSMatt Macy len_a_len_c[0] = 0; 522eda14cbcSMatt Macy len_a_len_c[1] = htonll(CRYPTO_BYTES2BITS(iv_len)); 523eda14cbcSMatt Macy GHASH(ctx, len_a_len_c, ctx->gcm_J0, gops); 524eda14cbcSMatt Macy 525eda14cbcSMatt Macy /* J0 will be used again in the final */ 526eda14cbcSMatt Macy copy_block((uint8_t *)ctx->gcm_J0, (uint8_t *)cb); 527eda14cbcSMatt Macy } 528eda14cbcSMatt Macy } 529eda14cbcSMatt Macy 530eda14cbcSMatt Macy static int 531eda14cbcSMatt Macy gcm_init(gcm_ctx_t *ctx, unsigned char *iv, size_t iv_len, 532eda14cbcSMatt Macy unsigned char *auth_data, size_t auth_data_len, size_t block_size, 533eda14cbcSMatt Macy int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 534eda14cbcSMatt Macy void (*copy_block)(uint8_t *, uint8_t *), 535eda14cbcSMatt Macy void (*xor_block)(uint8_t *, uint8_t *)) 536eda14cbcSMatt Macy { 537eda14cbcSMatt Macy const gcm_impl_ops_t *gops; 538eda14cbcSMatt Macy uint8_t *ghash, *datap, *authp; 539eda14cbcSMatt Macy size_t remainder, processed; 540eda14cbcSMatt Macy 541eda14cbcSMatt Macy /* encrypt zero block to get subkey H */ 542da5137abSMartin Matuska memset(ctx->gcm_H, 0, sizeof (ctx->gcm_H)); 543eda14cbcSMatt Macy encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_H, 544eda14cbcSMatt Macy (uint8_t *)ctx->gcm_H); 545eda14cbcSMatt Macy 546eda14cbcSMatt Macy gcm_format_initial_blocks(iv, iv_len, ctx, block_size, 547eda14cbcSMatt Macy copy_block, xor_block); 548eda14cbcSMatt Macy 549eda14cbcSMatt Macy gops = gcm_impl_get_ops(); 550eda14cbcSMatt Macy authp = (uint8_t *)ctx->gcm_tmp; 551eda14cbcSMatt Macy ghash = (uint8_t *)ctx->gcm_ghash; 552da5137abSMartin Matuska memset(authp, 0, block_size); 553da5137abSMartin Matuska memset(ghash, 0, block_size); 554eda14cbcSMatt Macy 555eda14cbcSMatt Macy processed = 0; 556eda14cbcSMatt Macy remainder = auth_data_len; 557eda14cbcSMatt Macy do { 558eda14cbcSMatt Macy if (remainder < block_size) { 559eda14cbcSMatt Macy /* 560eda14cbcSMatt Macy * There's not a block full of data, pad rest of 561eda14cbcSMatt Macy * buffer with zero 562eda14cbcSMatt Macy */ 563c03c5b1cSMartin Matuska 564c03c5b1cSMartin Matuska if (auth_data != NULL) { 565da5137abSMartin Matuska memset(authp, 0, block_size); 566da5137abSMartin Matuska memcpy(authp, &(auth_data[processed]), 567da5137abSMartin Matuska remainder); 568c03c5b1cSMartin Matuska } else { 569c03c5b1cSMartin Matuska ASSERT0(remainder); 570c03c5b1cSMartin Matuska } 571c03c5b1cSMartin Matuska 572eda14cbcSMatt Macy datap = (uint8_t *)authp; 573eda14cbcSMatt Macy remainder = 0; 574eda14cbcSMatt Macy } else { 575eda14cbcSMatt Macy datap = (uint8_t *)(&(auth_data[processed])); 576eda14cbcSMatt Macy processed += block_size; 577eda14cbcSMatt Macy remainder -= block_size; 578eda14cbcSMatt Macy } 579eda14cbcSMatt Macy 580eda14cbcSMatt Macy /* add auth data to the hash */ 581eda14cbcSMatt Macy GHASH(ctx, datap, ghash, gops); 582eda14cbcSMatt Macy 583eda14cbcSMatt Macy } while (remainder > 0); 584eda14cbcSMatt Macy 585eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 586eda14cbcSMatt Macy } 587eda14cbcSMatt Macy 588eda14cbcSMatt Macy /* 589eda14cbcSMatt Macy * The following function is called at encrypt or decrypt init time 590eda14cbcSMatt Macy * for AES GCM mode. 591eda14cbcSMatt Macy * 592eda14cbcSMatt Macy * Init the GCM context struct. Handle the cycle and avx implementations here. 593eda14cbcSMatt Macy */ 594eda14cbcSMatt Macy int 595eda14cbcSMatt Macy gcm_init_ctx(gcm_ctx_t *gcm_ctx, char *param, size_t block_size, 596eda14cbcSMatt Macy int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 597eda14cbcSMatt Macy void (*copy_block)(uint8_t *, uint8_t *), 598eda14cbcSMatt Macy void (*xor_block)(uint8_t *, uint8_t *)) 599eda14cbcSMatt Macy { 600eda14cbcSMatt Macy int rv; 601eda14cbcSMatt Macy CK_AES_GCM_PARAMS *gcm_param; 602eda14cbcSMatt Macy 603eda14cbcSMatt Macy if (param != NULL) { 604eda14cbcSMatt Macy gcm_param = (CK_AES_GCM_PARAMS *)(void *)param; 605eda14cbcSMatt Macy 606eda14cbcSMatt Macy if ((rv = gcm_validate_args(gcm_param)) != 0) { 607eda14cbcSMatt Macy return (rv); 608eda14cbcSMatt Macy } 609eda14cbcSMatt Macy 610eda14cbcSMatt Macy gcm_ctx->gcm_tag_len = gcm_param->ulTagBits; 611eda14cbcSMatt Macy gcm_ctx->gcm_tag_len >>= 3; 612eda14cbcSMatt Macy gcm_ctx->gcm_processed_data_len = 0; 613eda14cbcSMatt Macy 614eda14cbcSMatt Macy /* these values are in bits */ 615eda14cbcSMatt Macy gcm_ctx->gcm_len_a_len_c[0] 616eda14cbcSMatt Macy = htonll(CRYPTO_BYTES2BITS(gcm_param->ulAADLen)); 617eda14cbcSMatt Macy 618eda14cbcSMatt Macy rv = CRYPTO_SUCCESS; 619eda14cbcSMatt Macy gcm_ctx->gcm_flags |= GCM_MODE; 620eda14cbcSMatt Macy } else { 621eda14cbcSMatt Macy return (CRYPTO_MECHANISM_PARAM_INVALID); 622eda14cbcSMatt Macy } 623eda14cbcSMatt Macy 624eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 625eda14cbcSMatt Macy if (GCM_IMPL_READ(icp_gcm_impl) != IMPL_CYCLE) { 626eda14cbcSMatt Macy gcm_ctx->gcm_use_avx = GCM_IMPL_USE_AVX; 627eda14cbcSMatt Macy } else { 628eda14cbcSMatt Macy /* 629eda14cbcSMatt Macy * Handle the "cycle" implementation by creating avx and 630eda14cbcSMatt Macy * non-avx contexts alternately. 631eda14cbcSMatt Macy */ 632eda14cbcSMatt Macy gcm_ctx->gcm_use_avx = gcm_toggle_avx(); 633eda14cbcSMatt Macy /* 634eda14cbcSMatt Macy * We don't handle byte swapped key schedules in the avx 635eda14cbcSMatt Macy * code path. 636eda14cbcSMatt Macy */ 637eda14cbcSMatt Macy aes_key_t *ks = (aes_key_t *)gcm_ctx->gcm_keysched; 638eda14cbcSMatt Macy if (ks->ops->needs_byteswap == B_TRUE) { 639eda14cbcSMatt Macy gcm_ctx->gcm_use_avx = B_FALSE; 640eda14cbcSMatt Macy } 641eda14cbcSMatt Macy /* Use the MOVBE and the BSWAP variants alternately. */ 642eda14cbcSMatt Macy if (gcm_ctx->gcm_use_avx == B_TRUE && 643eda14cbcSMatt Macy zfs_movbe_available() == B_TRUE) { 644eda14cbcSMatt Macy (void) atomic_toggle_boolean_nv( 645eda14cbcSMatt Macy (volatile boolean_t *)&gcm_avx_can_use_movbe); 646eda14cbcSMatt Macy } 647eda14cbcSMatt Macy } 6487877fdebSMatt Macy /* Allocate Htab memory as needed. */ 6497877fdebSMatt Macy if (gcm_ctx->gcm_use_avx == B_TRUE) { 6507877fdebSMatt Macy size_t htab_len = gcm_simd_get_htab_size(gcm_ctx->gcm_use_avx); 6517877fdebSMatt Macy 6527877fdebSMatt Macy if (htab_len == 0) { 6537877fdebSMatt Macy return (CRYPTO_MECHANISM_PARAM_INVALID); 6547877fdebSMatt Macy } 6557877fdebSMatt Macy gcm_ctx->gcm_htab_len = htab_len; 6567877fdebSMatt Macy gcm_ctx->gcm_Htable = 657c03c5b1cSMartin Matuska (uint64_t *)kmem_alloc(htab_len, KM_SLEEP); 6587877fdebSMatt Macy 6597877fdebSMatt Macy if (gcm_ctx->gcm_Htable == NULL) { 6607877fdebSMatt Macy return (CRYPTO_HOST_MEMORY); 6617877fdebSMatt Macy } 6627877fdebSMatt Macy } 663eda14cbcSMatt Macy /* Avx and non avx context initialization differs from here on. */ 664eda14cbcSMatt Macy if (gcm_ctx->gcm_use_avx == B_FALSE) { 665eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */ 666eda14cbcSMatt Macy if (gcm_init(gcm_ctx, gcm_param->pIv, gcm_param->ulIvLen, 667eda14cbcSMatt Macy gcm_param->pAAD, gcm_param->ulAADLen, block_size, 668eda14cbcSMatt Macy encrypt_block, copy_block, xor_block) != 0) { 669eda14cbcSMatt Macy rv = CRYPTO_MECHANISM_PARAM_INVALID; 670eda14cbcSMatt Macy } 671eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 672eda14cbcSMatt Macy } else { 673eda14cbcSMatt Macy if (gcm_init_avx(gcm_ctx, gcm_param->pIv, gcm_param->ulIvLen, 674eda14cbcSMatt Macy gcm_param->pAAD, gcm_param->ulAADLen, block_size) != 0) { 675eda14cbcSMatt Macy rv = CRYPTO_MECHANISM_PARAM_INVALID; 676eda14cbcSMatt Macy } 677eda14cbcSMatt Macy } 678eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */ 679eda14cbcSMatt Macy 680eda14cbcSMatt Macy return (rv); 681eda14cbcSMatt Macy } 682eda14cbcSMatt Macy 683eda14cbcSMatt Macy int 684eda14cbcSMatt Macy gmac_init_ctx(gcm_ctx_t *gcm_ctx, char *param, size_t block_size, 685eda14cbcSMatt Macy int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 686eda14cbcSMatt Macy void (*copy_block)(uint8_t *, uint8_t *), 687eda14cbcSMatt Macy void (*xor_block)(uint8_t *, uint8_t *)) 688eda14cbcSMatt Macy { 689eda14cbcSMatt Macy int rv; 690eda14cbcSMatt Macy CK_AES_GMAC_PARAMS *gmac_param; 691eda14cbcSMatt Macy 692eda14cbcSMatt Macy if (param != NULL) { 693eda14cbcSMatt Macy gmac_param = (CK_AES_GMAC_PARAMS *)(void *)param; 694eda14cbcSMatt Macy 695eda14cbcSMatt Macy gcm_ctx->gcm_tag_len = CRYPTO_BITS2BYTES(AES_GMAC_TAG_BITS); 696eda14cbcSMatt Macy gcm_ctx->gcm_processed_data_len = 0; 697eda14cbcSMatt Macy 698eda14cbcSMatt Macy /* these values are in bits */ 699eda14cbcSMatt Macy gcm_ctx->gcm_len_a_len_c[0] 700eda14cbcSMatt Macy = htonll(CRYPTO_BYTES2BITS(gmac_param->ulAADLen)); 701eda14cbcSMatt Macy 702eda14cbcSMatt Macy rv = CRYPTO_SUCCESS; 703eda14cbcSMatt Macy gcm_ctx->gcm_flags |= GMAC_MODE; 704eda14cbcSMatt Macy } else { 705eda14cbcSMatt Macy return (CRYPTO_MECHANISM_PARAM_INVALID); 706eda14cbcSMatt Macy } 707eda14cbcSMatt Macy 708eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 709eda14cbcSMatt Macy /* 710eda14cbcSMatt Macy * Handle the "cycle" implementation by creating avx and non avx 711eda14cbcSMatt Macy * contexts alternately. 712eda14cbcSMatt Macy */ 713eda14cbcSMatt Macy if (GCM_IMPL_READ(icp_gcm_impl) != IMPL_CYCLE) { 714eda14cbcSMatt Macy gcm_ctx->gcm_use_avx = GCM_IMPL_USE_AVX; 715eda14cbcSMatt Macy } else { 716eda14cbcSMatt Macy gcm_ctx->gcm_use_avx = gcm_toggle_avx(); 717eda14cbcSMatt Macy } 718eda14cbcSMatt Macy /* We don't handle byte swapped key schedules in the avx code path. */ 719eda14cbcSMatt Macy aes_key_t *ks = (aes_key_t *)gcm_ctx->gcm_keysched; 720eda14cbcSMatt Macy if (ks->ops->needs_byteswap == B_TRUE) { 721eda14cbcSMatt Macy gcm_ctx->gcm_use_avx = B_FALSE; 722eda14cbcSMatt Macy } 7237877fdebSMatt Macy /* Allocate Htab memory as needed. */ 7247877fdebSMatt Macy if (gcm_ctx->gcm_use_avx == B_TRUE) { 7257877fdebSMatt Macy size_t htab_len = gcm_simd_get_htab_size(gcm_ctx->gcm_use_avx); 7267877fdebSMatt Macy 7277877fdebSMatt Macy if (htab_len == 0) { 7287877fdebSMatt Macy return (CRYPTO_MECHANISM_PARAM_INVALID); 7297877fdebSMatt Macy } 7307877fdebSMatt Macy gcm_ctx->gcm_htab_len = htab_len; 7317877fdebSMatt Macy gcm_ctx->gcm_Htable = 732c03c5b1cSMartin Matuska (uint64_t *)kmem_alloc(htab_len, KM_SLEEP); 7337877fdebSMatt Macy 7347877fdebSMatt Macy if (gcm_ctx->gcm_Htable == NULL) { 7357877fdebSMatt Macy return (CRYPTO_HOST_MEMORY); 7367877fdebSMatt Macy } 7377877fdebSMatt Macy } 7387877fdebSMatt Macy 739eda14cbcSMatt Macy /* Avx and non avx context initialization differs from here on. */ 740eda14cbcSMatt Macy if (gcm_ctx->gcm_use_avx == B_FALSE) { 741eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */ 742eda14cbcSMatt Macy if (gcm_init(gcm_ctx, gmac_param->pIv, AES_GMAC_IV_LEN, 743eda14cbcSMatt Macy gmac_param->pAAD, gmac_param->ulAADLen, block_size, 744eda14cbcSMatt Macy encrypt_block, copy_block, xor_block) != 0) { 745eda14cbcSMatt Macy rv = CRYPTO_MECHANISM_PARAM_INVALID; 746eda14cbcSMatt Macy } 747eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 748eda14cbcSMatt Macy } else { 749eda14cbcSMatt Macy if (gcm_init_avx(gcm_ctx, gmac_param->pIv, AES_GMAC_IV_LEN, 750eda14cbcSMatt Macy gmac_param->pAAD, gmac_param->ulAADLen, block_size) != 0) { 751eda14cbcSMatt Macy rv = CRYPTO_MECHANISM_PARAM_INVALID; 752eda14cbcSMatt Macy } 753eda14cbcSMatt Macy } 754eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */ 755eda14cbcSMatt Macy 756eda14cbcSMatt Macy return (rv); 757eda14cbcSMatt Macy } 758eda14cbcSMatt Macy 759eda14cbcSMatt Macy void * 760eda14cbcSMatt Macy gcm_alloc_ctx(int kmflag) 761eda14cbcSMatt Macy { 762eda14cbcSMatt Macy gcm_ctx_t *gcm_ctx; 763eda14cbcSMatt Macy 764eda14cbcSMatt Macy if ((gcm_ctx = kmem_zalloc(sizeof (gcm_ctx_t), kmflag)) == NULL) 765eda14cbcSMatt Macy return (NULL); 766eda14cbcSMatt Macy 767eda14cbcSMatt Macy gcm_ctx->gcm_flags = GCM_MODE; 768eda14cbcSMatt Macy return (gcm_ctx); 769eda14cbcSMatt Macy } 770eda14cbcSMatt Macy 771eda14cbcSMatt Macy void * 772eda14cbcSMatt Macy gmac_alloc_ctx(int kmflag) 773eda14cbcSMatt Macy { 774eda14cbcSMatt Macy gcm_ctx_t *gcm_ctx; 775eda14cbcSMatt Macy 776eda14cbcSMatt Macy if ((gcm_ctx = kmem_zalloc(sizeof (gcm_ctx_t), kmflag)) == NULL) 777eda14cbcSMatt Macy return (NULL); 778eda14cbcSMatt Macy 779eda14cbcSMatt Macy gcm_ctx->gcm_flags = GMAC_MODE; 780eda14cbcSMatt Macy return (gcm_ctx); 781eda14cbcSMatt Macy } 782eda14cbcSMatt Macy 783eda14cbcSMatt Macy /* GCM implementation that contains the fastest methods */ 784eda14cbcSMatt Macy static gcm_impl_ops_t gcm_fastest_impl = { 785eda14cbcSMatt Macy .name = "fastest" 786eda14cbcSMatt Macy }; 787eda14cbcSMatt Macy 788eda14cbcSMatt Macy /* All compiled in implementations */ 789e92ffd9bSMartin Matuska static const gcm_impl_ops_t *gcm_all_impl[] = { 790eda14cbcSMatt Macy &gcm_generic_impl, 791eda14cbcSMatt Macy #if defined(__x86_64) && defined(HAVE_PCLMULQDQ) 792eda14cbcSMatt Macy &gcm_pclmulqdq_impl, 793eda14cbcSMatt Macy #endif 794eda14cbcSMatt Macy }; 795eda14cbcSMatt Macy 796eda14cbcSMatt Macy /* Indicate that benchmark has been completed */ 797eda14cbcSMatt Macy static boolean_t gcm_impl_initialized = B_FALSE; 798eda14cbcSMatt Macy 799eda14cbcSMatt Macy /* Hold all supported implementations */ 800eda14cbcSMatt Macy static size_t gcm_supp_impl_cnt = 0; 801eda14cbcSMatt Macy static gcm_impl_ops_t *gcm_supp_impl[ARRAY_SIZE(gcm_all_impl)]; 802eda14cbcSMatt Macy 803eda14cbcSMatt Macy /* 804eda14cbcSMatt Macy * Returns the GCM operations for encrypt/decrypt/key setup. When a 805eda14cbcSMatt Macy * SIMD implementation is not allowed in the current context, then 806eda14cbcSMatt Macy * fallback to the fastest generic implementation. 807eda14cbcSMatt Macy */ 808eda14cbcSMatt Macy const gcm_impl_ops_t * 809716fd348SMartin Matuska gcm_impl_get_ops(void) 810eda14cbcSMatt Macy { 811eda14cbcSMatt Macy if (!kfpu_allowed()) 812eda14cbcSMatt Macy return (&gcm_generic_impl); 813eda14cbcSMatt Macy 814eda14cbcSMatt Macy const gcm_impl_ops_t *ops = NULL; 815eda14cbcSMatt Macy const uint32_t impl = GCM_IMPL_READ(icp_gcm_impl); 816eda14cbcSMatt Macy 817eda14cbcSMatt Macy switch (impl) { 818eda14cbcSMatt Macy case IMPL_FASTEST: 819eda14cbcSMatt Macy ASSERT(gcm_impl_initialized); 820eda14cbcSMatt Macy ops = &gcm_fastest_impl; 821eda14cbcSMatt Macy break; 822eda14cbcSMatt Macy case IMPL_CYCLE: 823eda14cbcSMatt Macy /* Cycle through supported implementations */ 824eda14cbcSMatt Macy ASSERT(gcm_impl_initialized); 825eda14cbcSMatt Macy ASSERT3U(gcm_supp_impl_cnt, >, 0); 826eda14cbcSMatt Macy static size_t cycle_impl_idx = 0; 827eda14cbcSMatt Macy size_t idx = (++cycle_impl_idx) % gcm_supp_impl_cnt; 828eda14cbcSMatt Macy ops = gcm_supp_impl[idx]; 829eda14cbcSMatt Macy break; 830eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 831eda14cbcSMatt Macy case IMPL_AVX: 832eda14cbcSMatt Macy /* 833eda14cbcSMatt Macy * Make sure that we return a valid implementation while 834eda14cbcSMatt Macy * switching to the avx implementation since there still 835eda14cbcSMatt Macy * may be unfinished non-avx contexts around. 836eda14cbcSMatt Macy */ 837eda14cbcSMatt Macy ops = &gcm_generic_impl; 838eda14cbcSMatt Macy break; 839eda14cbcSMatt Macy #endif 840eda14cbcSMatt Macy default: 841eda14cbcSMatt Macy ASSERT3U(impl, <, gcm_supp_impl_cnt); 842eda14cbcSMatt Macy ASSERT3U(gcm_supp_impl_cnt, >, 0); 843eda14cbcSMatt Macy if (impl < ARRAY_SIZE(gcm_all_impl)) 844eda14cbcSMatt Macy ops = gcm_supp_impl[impl]; 845eda14cbcSMatt Macy break; 846eda14cbcSMatt Macy } 847eda14cbcSMatt Macy 848eda14cbcSMatt Macy ASSERT3P(ops, !=, NULL); 849eda14cbcSMatt Macy 850eda14cbcSMatt Macy return (ops); 851eda14cbcSMatt Macy } 852eda14cbcSMatt Macy 853eda14cbcSMatt Macy /* 854eda14cbcSMatt Macy * Initialize all supported implementations. 855eda14cbcSMatt Macy */ 856eda14cbcSMatt Macy void 857eda14cbcSMatt Macy gcm_impl_init(void) 858eda14cbcSMatt Macy { 859eda14cbcSMatt Macy gcm_impl_ops_t *curr_impl; 860eda14cbcSMatt Macy int i, c; 861eda14cbcSMatt Macy 862eda14cbcSMatt Macy /* Move supported implementations into gcm_supp_impls */ 863eda14cbcSMatt Macy for (i = 0, c = 0; i < ARRAY_SIZE(gcm_all_impl); i++) { 864eda14cbcSMatt Macy curr_impl = (gcm_impl_ops_t *)gcm_all_impl[i]; 865eda14cbcSMatt Macy 866eda14cbcSMatt Macy if (curr_impl->is_supported()) 867eda14cbcSMatt Macy gcm_supp_impl[c++] = (gcm_impl_ops_t *)curr_impl; 868eda14cbcSMatt Macy } 869eda14cbcSMatt Macy gcm_supp_impl_cnt = c; 870eda14cbcSMatt Macy 871eda14cbcSMatt Macy /* 872eda14cbcSMatt Macy * Set the fastest implementation given the assumption that the 873eda14cbcSMatt Macy * hardware accelerated version is the fastest. 874eda14cbcSMatt Macy */ 875eda14cbcSMatt Macy #if defined(__x86_64) && defined(HAVE_PCLMULQDQ) 876eda14cbcSMatt Macy if (gcm_pclmulqdq_impl.is_supported()) { 877eda14cbcSMatt Macy memcpy(&gcm_fastest_impl, &gcm_pclmulqdq_impl, 878eda14cbcSMatt Macy sizeof (gcm_fastest_impl)); 879eda14cbcSMatt Macy } else 880eda14cbcSMatt Macy #endif 881eda14cbcSMatt Macy { 882eda14cbcSMatt Macy memcpy(&gcm_fastest_impl, &gcm_generic_impl, 883eda14cbcSMatt Macy sizeof (gcm_fastest_impl)); 884eda14cbcSMatt Macy } 885eda14cbcSMatt Macy 886eda14cbcSMatt Macy strlcpy(gcm_fastest_impl.name, "fastest", GCM_IMPL_NAME_MAX); 887eda14cbcSMatt Macy 888eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 889eda14cbcSMatt Macy /* 890eda14cbcSMatt Macy * Use the avx implementation if it's available and the implementation 891eda14cbcSMatt Macy * hasn't changed from its default value of fastest on module load. 892eda14cbcSMatt Macy */ 893eda14cbcSMatt Macy if (gcm_avx_will_work()) { 894eda14cbcSMatt Macy #ifdef HAVE_MOVBE 895eda14cbcSMatt Macy if (zfs_movbe_available() == B_TRUE) { 896eda14cbcSMatt Macy atomic_swap_32(&gcm_avx_can_use_movbe, B_TRUE); 897eda14cbcSMatt Macy } 898eda14cbcSMatt Macy #endif 899eda14cbcSMatt Macy if (GCM_IMPL_READ(user_sel_impl) == IMPL_FASTEST) { 900eda14cbcSMatt Macy gcm_set_avx(B_TRUE); 901eda14cbcSMatt Macy } 902eda14cbcSMatt Macy } 903eda14cbcSMatt Macy #endif 904eda14cbcSMatt Macy /* Finish initialization */ 905eda14cbcSMatt Macy atomic_swap_32(&icp_gcm_impl, user_sel_impl); 906eda14cbcSMatt Macy gcm_impl_initialized = B_TRUE; 907eda14cbcSMatt Macy } 908eda14cbcSMatt Macy 909eda14cbcSMatt Macy static const struct { 910*a0b956f5SMartin Matuska const char *name; 911eda14cbcSMatt Macy uint32_t sel; 912eda14cbcSMatt Macy } gcm_impl_opts[] = { 913eda14cbcSMatt Macy { "cycle", IMPL_CYCLE }, 914eda14cbcSMatt Macy { "fastest", IMPL_FASTEST }, 915eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 916eda14cbcSMatt Macy { "avx", IMPL_AVX }, 917eda14cbcSMatt Macy #endif 918eda14cbcSMatt Macy }; 919eda14cbcSMatt Macy 920eda14cbcSMatt Macy /* 921eda14cbcSMatt Macy * Function sets desired gcm implementation. 922eda14cbcSMatt Macy * 923eda14cbcSMatt Macy * If we are called before init(), user preference will be saved in 924eda14cbcSMatt Macy * user_sel_impl, and applied in later init() call. This occurs when module 925eda14cbcSMatt Macy * parameter is specified on module load. Otherwise, directly update 926eda14cbcSMatt Macy * icp_gcm_impl. 927eda14cbcSMatt Macy * 928eda14cbcSMatt Macy * @val Name of gcm implementation to use 929eda14cbcSMatt Macy * @param Unused. 930eda14cbcSMatt Macy */ 931eda14cbcSMatt Macy int 932eda14cbcSMatt Macy gcm_impl_set(const char *val) 933eda14cbcSMatt Macy { 934eda14cbcSMatt Macy int err = -EINVAL; 935eda14cbcSMatt Macy char req_name[GCM_IMPL_NAME_MAX]; 936eda14cbcSMatt Macy uint32_t impl = GCM_IMPL_READ(user_sel_impl); 937eda14cbcSMatt Macy size_t i; 938eda14cbcSMatt Macy 939eda14cbcSMatt Macy /* sanitize input */ 940eda14cbcSMatt Macy i = strnlen(val, GCM_IMPL_NAME_MAX); 941eda14cbcSMatt Macy if (i == 0 || i >= GCM_IMPL_NAME_MAX) 942eda14cbcSMatt Macy return (err); 943eda14cbcSMatt Macy 944eda14cbcSMatt Macy strlcpy(req_name, val, GCM_IMPL_NAME_MAX); 945eda14cbcSMatt Macy while (i > 0 && isspace(req_name[i-1])) 946eda14cbcSMatt Macy i--; 947eda14cbcSMatt Macy req_name[i] = '\0'; 948eda14cbcSMatt Macy 949eda14cbcSMatt Macy /* Check mandatory options */ 950eda14cbcSMatt Macy for (i = 0; i < ARRAY_SIZE(gcm_impl_opts); i++) { 951eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 952eda14cbcSMatt Macy /* Ignore avx implementation if it won't work. */ 953eda14cbcSMatt Macy if (gcm_impl_opts[i].sel == IMPL_AVX && !gcm_avx_will_work()) { 954eda14cbcSMatt Macy continue; 955eda14cbcSMatt Macy } 956eda14cbcSMatt Macy #endif 957eda14cbcSMatt Macy if (strcmp(req_name, gcm_impl_opts[i].name) == 0) { 958eda14cbcSMatt Macy impl = gcm_impl_opts[i].sel; 959eda14cbcSMatt Macy err = 0; 960eda14cbcSMatt Macy break; 961eda14cbcSMatt Macy } 962eda14cbcSMatt Macy } 963eda14cbcSMatt Macy 964eda14cbcSMatt Macy /* check all supported impl if init() was already called */ 965eda14cbcSMatt Macy if (err != 0 && gcm_impl_initialized) { 966eda14cbcSMatt Macy /* check all supported implementations */ 967eda14cbcSMatt Macy for (i = 0; i < gcm_supp_impl_cnt; i++) { 968eda14cbcSMatt Macy if (strcmp(req_name, gcm_supp_impl[i]->name) == 0) { 969eda14cbcSMatt Macy impl = i; 970eda14cbcSMatt Macy err = 0; 971eda14cbcSMatt Macy break; 972eda14cbcSMatt Macy } 973eda14cbcSMatt Macy } 974eda14cbcSMatt Macy } 975eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 976eda14cbcSMatt Macy /* 977eda14cbcSMatt Macy * Use the avx implementation if available and the requested one is 978eda14cbcSMatt Macy * avx or fastest. 979eda14cbcSMatt Macy */ 980eda14cbcSMatt Macy if (gcm_avx_will_work() == B_TRUE && 981eda14cbcSMatt Macy (impl == IMPL_AVX || impl == IMPL_FASTEST)) { 982eda14cbcSMatt Macy gcm_set_avx(B_TRUE); 983eda14cbcSMatt Macy } else { 984eda14cbcSMatt Macy gcm_set_avx(B_FALSE); 985eda14cbcSMatt Macy } 986eda14cbcSMatt Macy #endif 987eda14cbcSMatt Macy 988eda14cbcSMatt Macy if (err == 0) { 989eda14cbcSMatt Macy if (gcm_impl_initialized) 990eda14cbcSMatt Macy atomic_swap_32(&icp_gcm_impl, impl); 991eda14cbcSMatt Macy else 992eda14cbcSMatt Macy atomic_swap_32(&user_sel_impl, impl); 993eda14cbcSMatt Macy } 994eda14cbcSMatt Macy 995eda14cbcSMatt Macy return (err); 996eda14cbcSMatt Macy } 997eda14cbcSMatt Macy 998eda14cbcSMatt Macy #if defined(_KERNEL) && defined(__linux__) 999eda14cbcSMatt Macy 1000eda14cbcSMatt Macy static int 1001eda14cbcSMatt Macy icp_gcm_impl_set(const char *val, zfs_kernel_param_t *kp) 1002eda14cbcSMatt Macy { 1003eda14cbcSMatt Macy return (gcm_impl_set(val)); 1004eda14cbcSMatt Macy } 1005eda14cbcSMatt Macy 1006eda14cbcSMatt Macy static int 1007eda14cbcSMatt Macy icp_gcm_impl_get(char *buffer, zfs_kernel_param_t *kp) 1008eda14cbcSMatt Macy { 1009eda14cbcSMatt Macy int i, cnt = 0; 1010eda14cbcSMatt Macy char *fmt; 1011eda14cbcSMatt Macy const uint32_t impl = GCM_IMPL_READ(icp_gcm_impl); 1012eda14cbcSMatt Macy 1013eda14cbcSMatt Macy ASSERT(gcm_impl_initialized); 1014eda14cbcSMatt Macy 1015eda14cbcSMatt Macy /* list mandatory options */ 1016eda14cbcSMatt Macy for (i = 0; i < ARRAY_SIZE(gcm_impl_opts); i++) { 1017eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 1018eda14cbcSMatt Macy /* Ignore avx implementation if it won't work. */ 1019eda14cbcSMatt Macy if (gcm_impl_opts[i].sel == IMPL_AVX && !gcm_avx_will_work()) { 1020eda14cbcSMatt Macy continue; 1021eda14cbcSMatt Macy } 1022eda14cbcSMatt Macy #endif 1023eda14cbcSMatt Macy fmt = (impl == gcm_impl_opts[i].sel) ? "[%s] " : "%s "; 1024eda14cbcSMatt Macy cnt += sprintf(buffer + cnt, fmt, gcm_impl_opts[i].name); 1025eda14cbcSMatt Macy } 1026eda14cbcSMatt Macy 1027eda14cbcSMatt Macy /* list all supported implementations */ 1028eda14cbcSMatt Macy for (i = 0; i < gcm_supp_impl_cnt; i++) { 1029eda14cbcSMatt Macy fmt = (i == impl) ? "[%s] " : "%s "; 1030eda14cbcSMatt Macy cnt += sprintf(buffer + cnt, fmt, gcm_supp_impl[i]->name); 1031eda14cbcSMatt Macy } 1032eda14cbcSMatt Macy 1033eda14cbcSMatt Macy return (cnt); 1034eda14cbcSMatt Macy } 1035eda14cbcSMatt Macy 1036eda14cbcSMatt Macy module_param_call(icp_gcm_impl, icp_gcm_impl_set, icp_gcm_impl_get, 1037eda14cbcSMatt Macy NULL, 0644); 1038eda14cbcSMatt Macy MODULE_PARM_DESC(icp_gcm_impl, "Select gcm implementation."); 1039eda14cbcSMatt Macy #endif /* defined(__KERNEL) */ 1040eda14cbcSMatt Macy 1041eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM 1042eda14cbcSMatt Macy #define GCM_BLOCK_LEN 16 1043eda14cbcSMatt Macy /* 1044eda14cbcSMatt Macy * The openssl asm routines are 6x aggregated and need that many bytes 1045eda14cbcSMatt Macy * at minimum. 1046eda14cbcSMatt Macy */ 1047eda14cbcSMatt Macy #define GCM_AVX_MIN_DECRYPT_BYTES (GCM_BLOCK_LEN * 6) 1048eda14cbcSMatt Macy #define GCM_AVX_MIN_ENCRYPT_BYTES (GCM_BLOCK_LEN * 6 * 3) 1049eda14cbcSMatt Macy /* 1050eda14cbcSMatt Macy * Ensure the chunk size is reasonable since we are allocating a 1051eda14cbcSMatt Macy * GCM_AVX_MAX_CHUNK_SIZEd buffer and disabling preemption and interrupts. 1052eda14cbcSMatt Macy */ 1053eda14cbcSMatt Macy #define GCM_AVX_MAX_CHUNK_SIZE \ 1054eda14cbcSMatt Macy (((128*1024)/GCM_AVX_MIN_DECRYPT_BYTES) * GCM_AVX_MIN_DECRYPT_BYTES) 1055eda14cbcSMatt Macy 1056eda14cbcSMatt Macy /* Clear the FPU registers since they hold sensitive internal state. */ 1057eda14cbcSMatt Macy #define clear_fpu_regs() clear_fpu_regs_avx() 1058eda14cbcSMatt Macy #define GHASH_AVX(ctx, in, len) \ 10597877fdebSMatt Macy gcm_ghash_avx((ctx)->gcm_ghash, (const uint64_t *)(ctx)->gcm_Htable, \ 1060eda14cbcSMatt Macy in, len) 1061eda14cbcSMatt Macy 1062eda14cbcSMatt Macy #define gcm_incr_counter_block(ctx) gcm_incr_counter_block_by(ctx, 1) 1063eda14cbcSMatt Macy 1064e92ffd9bSMartin Matuska /* Get the chunk size module parameter. */ 1065e92ffd9bSMartin Matuska #define GCM_CHUNK_SIZE_READ *(volatile uint32_t *) &gcm_avx_chunk_size 1066e92ffd9bSMartin Matuska 1067eda14cbcSMatt Macy /* 1068eda14cbcSMatt Macy * Module parameter: number of bytes to process at once while owning the FPU. 1069eda14cbcSMatt Macy * Rounded down to the next GCM_AVX_MIN_DECRYPT_BYTES byte boundary and is 1070eda14cbcSMatt Macy * ensured to be greater or equal than GCM_AVX_MIN_DECRYPT_BYTES. 1071eda14cbcSMatt Macy */ 1072eda14cbcSMatt Macy static uint32_t gcm_avx_chunk_size = 1073eda14cbcSMatt Macy ((32 * 1024) / GCM_AVX_MIN_DECRYPT_BYTES) * GCM_AVX_MIN_DECRYPT_BYTES; 1074eda14cbcSMatt Macy 1075eda14cbcSMatt Macy extern void clear_fpu_regs_avx(void); 1076eda14cbcSMatt Macy extern void gcm_xor_avx(const uint8_t *src, uint8_t *dst); 1077eda14cbcSMatt Macy extern void aes_encrypt_intel(const uint32_t rk[], int nr, 1078eda14cbcSMatt Macy const uint32_t pt[4], uint32_t ct[4]); 1079eda14cbcSMatt Macy 10807877fdebSMatt Macy extern void gcm_init_htab_avx(uint64_t *Htable, const uint64_t H[2]); 10817877fdebSMatt Macy extern void gcm_ghash_avx(uint64_t ghash[2], const uint64_t *Htable, 1082eda14cbcSMatt Macy const uint8_t *in, size_t len); 1083eda14cbcSMatt Macy 1084eda14cbcSMatt Macy extern size_t aesni_gcm_encrypt(const uint8_t *, uint8_t *, size_t, 1085eda14cbcSMatt Macy const void *, uint64_t *, uint64_t *); 1086eda14cbcSMatt Macy 1087eda14cbcSMatt Macy extern size_t aesni_gcm_decrypt(const uint8_t *, uint8_t *, size_t, 1088eda14cbcSMatt Macy const void *, uint64_t *, uint64_t *); 1089eda14cbcSMatt Macy 1090eda14cbcSMatt Macy static inline boolean_t 1091eda14cbcSMatt Macy gcm_avx_will_work(void) 1092eda14cbcSMatt Macy { 1093eda14cbcSMatt Macy /* Avx should imply aes-ni and pclmulqdq, but make sure anyhow. */ 1094eda14cbcSMatt Macy return (kfpu_allowed() && 1095eda14cbcSMatt Macy zfs_avx_available() && zfs_aes_available() && 1096eda14cbcSMatt Macy zfs_pclmulqdq_available()); 1097eda14cbcSMatt Macy } 1098eda14cbcSMatt Macy 1099eda14cbcSMatt Macy static inline void 1100eda14cbcSMatt Macy gcm_set_avx(boolean_t val) 1101eda14cbcSMatt Macy { 1102eda14cbcSMatt Macy if (gcm_avx_will_work() == B_TRUE) { 1103eda14cbcSMatt Macy atomic_swap_32(&gcm_use_avx, val); 1104eda14cbcSMatt Macy } 1105eda14cbcSMatt Macy } 1106eda14cbcSMatt Macy 1107eda14cbcSMatt Macy static inline boolean_t 1108eda14cbcSMatt Macy gcm_toggle_avx(void) 1109eda14cbcSMatt Macy { 1110eda14cbcSMatt Macy if (gcm_avx_will_work() == B_TRUE) { 1111eda14cbcSMatt Macy return (atomic_toggle_boolean_nv(&GCM_IMPL_USE_AVX)); 1112eda14cbcSMatt Macy } else { 1113eda14cbcSMatt Macy return (B_FALSE); 1114eda14cbcSMatt Macy } 1115eda14cbcSMatt Macy } 1116eda14cbcSMatt Macy 11177877fdebSMatt Macy static inline size_t 11187877fdebSMatt Macy gcm_simd_get_htab_size(boolean_t simd_mode) 11197877fdebSMatt Macy { 11207877fdebSMatt Macy switch (simd_mode) { 11217877fdebSMatt Macy case B_TRUE: 11227877fdebSMatt Macy return (2 * 6 * 2 * sizeof (uint64_t)); 11237877fdebSMatt Macy 11247877fdebSMatt Macy default: 11257877fdebSMatt Macy return (0); 11267877fdebSMatt Macy } 11277877fdebSMatt Macy } 11287877fdebSMatt Macy 1129eda14cbcSMatt Macy /* 1130eda14cbcSMatt Macy * Clear sensitive data in the context. 1131eda14cbcSMatt Macy * 1132eda14cbcSMatt Macy * ctx->gcm_remainder may contain a plaintext remainder. ctx->gcm_H and 1133eda14cbcSMatt Macy * ctx->gcm_Htable contain the hash sub key which protects authentication. 1134eda14cbcSMatt Macy * 1135eda14cbcSMatt Macy * Although extremely unlikely, ctx->gcm_J0 and ctx->gcm_tmp could be used for 1136eda14cbcSMatt Macy * a known plaintext attack, they consists of the IV and the first and last 1137eda14cbcSMatt Macy * counter respectively. If they should be cleared is debatable. 1138eda14cbcSMatt Macy */ 1139eda14cbcSMatt Macy static inline void 1140eda14cbcSMatt Macy gcm_clear_ctx(gcm_ctx_t *ctx) 1141eda14cbcSMatt Macy { 1142da5137abSMartin Matuska memset(ctx->gcm_remainder, 0, sizeof (ctx->gcm_remainder)); 1143da5137abSMartin Matuska memset(ctx->gcm_H, 0, sizeof (ctx->gcm_H)); 1144da5137abSMartin Matuska memset(ctx->gcm_J0, 0, sizeof (ctx->gcm_J0)); 1145da5137abSMartin Matuska memset(ctx->gcm_tmp, 0, sizeof (ctx->gcm_tmp)); 1146eda14cbcSMatt Macy } 1147eda14cbcSMatt Macy 1148eda14cbcSMatt Macy /* Increment the GCM counter block by n. */ 1149eda14cbcSMatt Macy static inline void 1150eda14cbcSMatt Macy gcm_incr_counter_block_by(gcm_ctx_t *ctx, int n) 1151eda14cbcSMatt Macy { 1152eda14cbcSMatt Macy uint64_t counter_mask = ntohll(0x00000000ffffffffULL); 1153eda14cbcSMatt Macy uint64_t counter = ntohll(ctx->gcm_cb[1] & counter_mask); 1154eda14cbcSMatt Macy 1155eda14cbcSMatt Macy counter = htonll(counter + n); 1156eda14cbcSMatt Macy counter &= counter_mask; 1157eda14cbcSMatt Macy ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter; 1158eda14cbcSMatt Macy } 1159eda14cbcSMatt Macy 1160eda14cbcSMatt Macy /* 1161eda14cbcSMatt Macy * Encrypt multiple blocks of data in GCM mode. 1162eda14cbcSMatt Macy * This is done in gcm_avx_chunk_size chunks, utilizing AVX assembler routines 1163eda14cbcSMatt Macy * if possible. While processing a chunk the FPU is "locked". 1164eda14cbcSMatt Macy */ 1165eda14cbcSMatt Macy static int 1166eda14cbcSMatt Macy gcm_mode_encrypt_contiguous_blocks_avx(gcm_ctx_t *ctx, char *data, 1167eda14cbcSMatt Macy size_t length, crypto_data_t *out, size_t block_size) 1168eda14cbcSMatt Macy { 1169eda14cbcSMatt Macy size_t bleft = length; 1170eda14cbcSMatt Macy size_t need = 0; 1171eda14cbcSMatt Macy size_t done = 0; 1172eda14cbcSMatt Macy uint8_t *datap = (uint8_t *)data; 1173eda14cbcSMatt Macy size_t chunk_size = (size_t)GCM_CHUNK_SIZE_READ; 1174eda14cbcSMatt Macy const aes_key_t *key = ((aes_key_t *)ctx->gcm_keysched); 1175eda14cbcSMatt Macy uint64_t *ghash = ctx->gcm_ghash; 1176eda14cbcSMatt Macy uint64_t *cb = ctx->gcm_cb; 1177eda14cbcSMatt Macy uint8_t *ct_buf = NULL; 1178eda14cbcSMatt Macy uint8_t *tmp = (uint8_t *)ctx->gcm_tmp; 1179eda14cbcSMatt Macy int rv = CRYPTO_SUCCESS; 1180eda14cbcSMatt Macy 1181eda14cbcSMatt Macy ASSERT(block_size == GCM_BLOCK_LEN); 1182eda14cbcSMatt Macy /* 1183eda14cbcSMatt Macy * If the last call left an incomplete block, try to fill 1184eda14cbcSMatt Macy * it first. 1185eda14cbcSMatt Macy */ 1186eda14cbcSMatt Macy if (ctx->gcm_remainder_len > 0) { 1187eda14cbcSMatt Macy need = block_size - ctx->gcm_remainder_len; 1188eda14cbcSMatt Macy if (length < need) { 1189eda14cbcSMatt Macy /* Accumulate bytes here and return. */ 1190da5137abSMartin Matuska memcpy((uint8_t *)ctx->gcm_remainder + 1191da5137abSMartin Matuska ctx->gcm_remainder_len, datap, length); 1192eda14cbcSMatt Macy 1193eda14cbcSMatt Macy ctx->gcm_remainder_len += length; 1194eda14cbcSMatt Macy if (ctx->gcm_copy_to == NULL) { 1195eda14cbcSMatt Macy ctx->gcm_copy_to = datap; 1196eda14cbcSMatt Macy } 1197eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 1198eda14cbcSMatt Macy } else { 1199eda14cbcSMatt Macy /* Complete incomplete block. */ 1200da5137abSMartin Matuska memcpy((uint8_t *)ctx->gcm_remainder + 1201da5137abSMartin Matuska ctx->gcm_remainder_len, datap, need); 1202eda14cbcSMatt Macy 1203eda14cbcSMatt Macy ctx->gcm_copy_to = NULL; 1204eda14cbcSMatt Macy } 1205eda14cbcSMatt Macy } 1206eda14cbcSMatt Macy 1207eda14cbcSMatt Macy /* Allocate a buffer to encrypt to if there is enough input. */ 1208eda14cbcSMatt Macy if (bleft >= GCM_AVX_MIN_ENCRYPT_BYTES) { 1209c03c5b1cSMartin Matuska ct_buf = vmem_alloc(chunk_size, KM_SLEEP); 1210eda14cbcSMatt Macy if (ct_buf == NULL) { 1211eda14cbcSMatt Macy return (CRYPTO_HOST_MEMORY); 1212eda14cbcSMatt Macy } 1213eda14cbcSMatt Macy } 1214eda14cbcSMatt Macy 1215eda14cbcSMatt Macy /* If we completed an incomplete block, encrypt and write it out. */ 1216eda14cbcSMatt Macy if (ctx->gcm_remainder_len > 0) { 1217eda14cbcSMatt Macy kfpu_begin(); 1218eda14cbcSMatt Macy aes_encrypt_intel(key->encr_ks.ks32, key->nr, 1219eda14cbcSMatt Macy (const uint32_t *)cb, (uint32_t *)tmp); 1220eda14cbcSMatt Macy 1221eda14cbcSMatt Macy gcm_xor_avx((const uint8_t *) ctx->gcm_remainder, tmp); 1222eda14cbcSMatt Macy GHASH_AVX(ctx, tmp, block_size); 1223eda14cbcSMatt Macy clear_fpu_regs(); 1224eda14cbcSMatt Macy kfpu_end(); 1225eda14cbcSMatt Macy rv = crypto_put_output_data(tmp, out, block_size); 1226eda14cbcSMatt Macy out->cd_offset += block_size; 1227eda14cbcSMatt Macy gcm_incr_counter_block(ctx); 1228eda14cbcSMatt Macy ctx->gcm_processed_data_len += block_size; 1229eda14cbcSMatt Macy bleft -= need; 1230eda14cbcSMatt Macy datap += need; 1231eda14cbcSMatt Macy ctx->gcm_remainder_len = 0; 1232eda14cbcSMatt Macy } 1233eda14cbcSMatt Macy 1234eda14cbcSMatt Macy /* Do the bulk encryption in chunk_size blocks. */ 1235eda14cbcSMatt Macy for (; bleft >= chunk_size; bleft -= chunk_size) { 1236eda14cbcSMatt Macy kfpu_begin(); 1237eda14cbcSMatt Macy done = aesni_gcm_encrypt( 1238eda14cbcSMatt Macy datap, ct_buf, chunk_size, key, cb, ghash); 1239eda14cbcSMatt Macy 1240eda14cbcSMatt Macy clear_fpu_regs(); 1241eda14cbcSMatt Macy kfpu_end(); 1242eda14cbcSMatt Macy if (done != chunk_size) { 1243eda14cbcSMatt Macy rv = CRYPTO_FAILED; 1244eda14cbcSMatt Macy goto out_nofpu; 1245eda14cbcSMatt Macy } 1246eda14cbcSMatt Macy rv = crypto_put_output_data(ct_buf, out, chunk_size); 1247eda14cbcSMatt Macy if (rv != CRYPTO_SUCCESS) { 1248eda14cbcSMatt Macy goto out_nofpu; 1249eda14cbcSMatt Macy } 1250eda14cbcSMatt Macy out->cd_offset += chunk_size; 1251eda14cbcSMatt Macy datap += chunk_size; 1252eda14cbcSMatt Macy ctx->gcm_processed_data_len += chunk_size; 1253eda14cbcSMatt Macy } 1254eda14cbcSMatt Macy /* Check if we are already done. */ 1255eda14cbcSMatt Macy if (bleft == 0) { 1256eda14cbcSMatt Macy goto out_nofpu; 1257eda14cbcSMatt Macy } 1258eda14cbcSMatt Macy /* Bulk encrypt the remaining data. */ 1259eda14cbcSMatt Macy kfpu_begin(); 1260eda14cbcSMatt Macy if (bleft >= GCM_AVX_MIN_ENCRYPT_BYTES) { 1261eda14cbcSMatt Macy done = aesni_gcm_encrypt(datap, ct_buf, bleft, key, cb, ghash); 1262eda14cbcSMatt Macy if (done == 0) { 1263eda14cbcSMatt Macy rv = CRYPTO_FAILED; 1264eda14cbcSMatt Macy goto out; 1265eda14cbcSMatt Macy } 1266eda14cbcSMatt Macy rv = crypto_put_output_data(ct_buf, out, done); 1267eda14cbcSMatt Macy if (rv != CRYPTO_SUCCESS) { 1268eda14cbcSMatt Macy goto out; 1269eda14cbcSMatt Macy } 1270eda14cbcSMatt Macy out->cd_offset += done; 1271eda14cbcSMatt Macy ctx->gcm_processed_data_len += done; 1272eda14cbcSMatt Macy datap += done; 1273eda14cbcSMatt Macy bleft -= done; 1274eda14cbcSMatt Macy 1275eda14cbcSMatt Macy } 1276eda14cbcSMatt Macy /* Less than GCM_AVX_MIN_ENCRYPT_BYTES remain, operate on blocks. */ 1277eda14cbcSMatt Macy while (bleft > 0) { 1278eda14cbcSMatt Macy if (bleft < block_size) { 1279da5137abSMartin Matuska memcpy(ctx->gcm_remainder, datap, bleft); 1280eda14cbcSMatt Macy ctx->gcm_remainder_len = bleft; 1281eda14cbcSMatt Macy ctx->gcm_copy_to = datap; 1282eda14cbcSMatt Macy goto out; 1283eda14cbcSMatt Macy } 1284eda14cbcSMatt Macy /* Encrypt, hash and write out. */ 1285eda14cbcSMatt Macy aes_encrypt_intel(key->encr_ks.ks32, key->nr, 1286eda14cbcSMatt Macy (const uint32_t *)cb, (uint32_t *)tmp); 1287eda14cbcSMatt Macy 1288eda14cbcSMatt Macy gcm_xor_avx(datap, tmp); 1289eda14cbcSMatt Macy GHASH_AVX(ctx, tmp, block_size); 1290eda14cbcSMatt Macy rv = crypto_put_output_data(tmp, out, block_size); 1291eda14cbcSMatt Macy if (rv != CRYPTO_SUCCESS) { 1292eda14cbcSMatt Macy goto out; 1293eda14cbcSMatt Macy } 1294eda14cbcSMatt Macy out->cd_offset += block_size; 1295eda14cbcSMatt Macy gcm_incr_counter_block(ctx); 1296eda14cbcSMatt Macy ctx->gcm_processed_data_len += block_size; 1297eda14cbcSMatt Macy datap += block_size; 1298eda14cbcSMatt Macy bleft -= block_size; 1299eda14cbcSMatt Macy } 1300eda14cbcSMatt Macy out: 1301eda14cbcSMatt Macy clear_fpu_regs(); 1302eda14cbcSMatt Macy kfpu_end(); 1303eda14cbcSMatt Macy out_nofpu: 1304eda14cbcSMatt Macy if (ct_buf != NULL) { 1305eda14cbcSMatt Macy vmem_free(ct_buf, chunk_size); 1306eda14cbcSMatt Macy } 1307eda14cbcSMatt Macy return (rv); 1308eda14cbcSMatt Macy } 1309eda14cbcSMatt Macy 1310eda14cbcSMatt Macy /* 1311eda14cbcSMatt Macy * Finalize the encryption: Zero fill, encrypt, hash and write out an eventual 1312eda14cbcSMatt Macy * incomplete last block. Encrypt the ICB. Calculate the tag and write it out. 1313eda14cbcSMatt Macy */ 1314eda14cbcSMatt Macy static int 1315eda14cbcSMatt Macy gcm_encrypt_final_avx(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size) 1316eda14cbcSMatt Macy { 1317eda14cbcSMatt Macy uint8_t *ghash = (uint8_t *)ctx->gcm_ghash; 1318eda14cbcSMatt Macy uint32_t *J0 = (uint32_t *)ctx->gcm_J0; 1319eda14cbcSMatt Macy uint8_t *remainder = (uint8_t *)ctx->gcm_remainder; 1320eda14cbcSMatt Macy size_t rem_len = ctx->gcm_remainder_len; 1321eda14cbcSMatt Macy const void *keysched = ((aes_key_t *)ctx->gcm_keysched)->encr_ks.ks32; 1322eda14cbcSMatt Macy int aes_rounds = ((aes_key_t *)keysched)->nr; 1323eda14cbcSMatt Macy int rv; 1324eda14cbcSMatt Macy 1325eda14cbcSMatt Macy ASSERT(block_size == GCM_BLOCK_LEN); 1326eda14cbcSMatt Macy 1327eda14cbcSMatt Macy if (out->cd_length < (rem_len + ctx->gcm_tag_len)) { 1328eda14cbcSMatt Macy return (CRYPTO_DATA_LEN_RANGE); 1329eda14cbcSMatt Macy } 1330eda14cbcSMatt Macy 1331eda14cbcSMatt Macy kfpu_begin(); 1332eda14cbcSMatt Macy /* Pad last incomplete block with zeros, encrypt and hash. */ 1333eda14cbcSMatt Macy if (rem_len > 0) { 1334eda14cbcSMatt Macy uint8_t *tmp = (uint8_t *)ctx->gcm_tmp; 1335eda14cbcSMatt Macy const uint32_t *cb = (uint32_t *)ctx->gcm_cb; 1336eda14cbcSMatt Macy 1337eda14cbcSMatt Macy aes_encrypt_intel(keysched, aes_rounds, cb, (uint32_t *)tmp); 1338da5137abSMartin Matuska memset(remainder + rem_len, 0, block_size - rem_len); 1339eda14cbcSMatt Macy for (int i = 0; i < rem_len; i++) { 1340eda14cbcSMatt Macy remainder[i] ^= tmp[i]; 1341eda14cbcSMatt Macy } 1342eda14cbcSMatt Macy GHASH_AVX(ctx, remainder, block_size); 1343eda14cbcSMatt Macy ctx->gcm_processed_data_len += rem_len; 1344eda14cbcSMatt Macy /* No need to increment counter_block, it's the last block. */ 1345eda14cbcSMatt Macy } 1346eda14cbcSMatt Macy /* Finish tag. */ 1347eda14cbcSMatt Macy ctx->gcm_len_a_len_c[1] = 1348eda14cbcSMatt Macy htonll(CRYPTO_BYTES2BITS(ctx->gcm_processed_data_len)); 1349eda14cbcSMatt Macy GHASH_AVX(ctx, (const uint8_t *)ctx->gcm_len_a_len_c, block_size); 1350eda14cbcSMatt Macy aes_encrypt_intel(keysched, aes_rounds, J0, J0); 1351eda14cbcSMatt Macy 1352eda14cbcSMatt Macy gcm_xor_avx((uint8_t *)J0, ghash); 1353eda14cbcSMatt Macy clear_fpu_regs(); 1354eda14cbcSMatt Macy kfpu_end(); 1355eda14cbcSMatt Macy 1356eda14cbcSMatt Macy /* Output remainder. */ 1357eda14cbcSMatt Macy if (rem_len > 0) { 1358eda14cbcSMatt Macy rv = crypto_put_output_data(remainder, out, rem_len); 1359eda14cbcSMatt Macy if (rv != CRYPTO_SUCCESS) 1360eda14cbcSMatt Macy return (rv); 1361eda14cbcSMatt Macy } 1362eda14cbcSMatt Macy out->cd_offset += rem_len; 1363eda14cbcSMatt Macy ctx->gcm_remainder_len = 0; 1364eda14cbcSMatt Macy rv = crypto_put_output_data(ghash, out, ctx->gcm_tag_len); 1365eda14cbcSMatt Macy if (rv != CRYPTO_SUCCESS) 1366eda14cbcSMatt Macy return (rv); 1367eda14cbcSMatt Macy 1368eda14cbcSMatt Macy out->cd_offset += ctx->gcm_tag_len; 1369eda14cbcSMatt Macy /* Clear sensitive data in the context before returning. */ 1370eda14cbcSMatt Macy gcm_clear_ctx(ctx); 1371eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 1372eda14cbcSMatt Macy } 1373eda14cbcSMatt Macy 1374eda14cbcSMatt Macy /* 1375eda14cbcSMatt Macy * Finalize decryption: We just have accumulated crypto text, so now we 1376eda14cbcSMatt Macy * decrypt it here inplace. 1377eda14cbcSMatt Macy */ 1378eda14cbcSMatt Macy static int 1379eda14cbcSMatt Macy gcm_decrypt_final_avx(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size) 1380eda14cbcSMatt Macy { 1381eda14cbcSMatt Macy ASSERT3U(ctx->gcm_processed_data_len, ==, ctx->gcm_pt_buf_len); 1382eda14cbcSMatt Macy ASSERT3U(block_size, ==, 16); 1383eda14cbcSMatt Macy 1384eda14cbcSMatt Macy size_t chunk_size = (size_t)GCM_CHUNK_SIZE_READ; 1385eda14cbcSMatt Macy size_t pt_len = ctx->gcm_processed_data_len - ctx->gcm_tag_len; 1386eda14cbcSMatt Macy uint8_t *datap = ctx->gcm_pt_buf; 1387eda14cbcSMatt Macy const aes_key_t *key = ((aes_key_t *)ctx->gcm_keysched); 1388eda14cbcSMatt Macy uint32_t *cb = (uint32_t *)ctx->gcm_cb; 1389eda14cbcSMatt Macy uint64_t *ghash = ctx->gcm_ghash; 1390eda14cbcSMatt Macy uint32_t *tmp = (uint32_t *)ctx->gcm_tmp; 1391eda14cbcSMatt Macy int rv = CRYPTO_SUCCESS; 1392eda14cbcSMatt Macy size_t bleft, done; 1393eda14cbcSMatt Macy 1394eda14cbcSMatt Macy /* 1395eda14cbcSMatt Macy * Decrypt in chunks of gcm_avx_chunk_size, which is asserted to be 1396eda14cbcSMatt Macy * greater or equal than GCM_AVX_MIN_ENCRYPT_BYTES, and a multiple of 1397eda14cbcSMatt Macy * GCM_AVX_MIN_DECRYPT_BYTES. 1398eda14cbcSMatt Macy */ 1399eda14cbcSMatt Macy for (bleft = pt_len; bleft >= chunk_size; bleft -= chunk_size) { 1400eda14cbcSMatt Macy kfpu_begin(); 1401eda14cbcSMatt Macy done = aesni_gcm_decrypt(datap, datap, chunk_size, 1402eda14cbcSMatt Macy (const void *)key, ctx->gcm_cb, ghash); 1403eda14cbcSMatt Macy clear_fpu_regs(); 1404eda14cbcSMatt Macy kfpu_end(); 1405eda14cbcSMatt Macy if (done != chunk_size) { 1406eda14cbcSMatt Macy return (CRYPTO_FAILED); 1407eda14cbcSMatt Macy } 1408eda14cbcSMatt Macy datap += done; 1409eda14cbcSMatt Macy } 141016038816SMartin Matuska /* Decrypt remainder, which is less than chunk size, in one go. */ 1411eda14cbcSMatt Macy kfpu_begin(); 1412eda14cbcSMatt Macy if (bleft >= GCM_AVX_MIN_DECRYPT_BYTES) { 1413eda14cbcSMatt Macy done = aesni_gcm_decrypt(datap, datap, bleft, 1414eda14cbcSMatt Macy (const void *)key, ctx->gcm_cb, ghash); 1415eda14cbcSMatt Macy if (done == 0) { 1416eda14cbcSMatt Macy clear_fpu_regs(); 1417eda14cbcSMatt Macy kfpu_end(); 1418eda14cbcSMatt Macy return (CRYPTO_FAILED); 1419eda14cbcSMatt Macy } 1420eda14cbcSMatt Macy datap += done; 1421eda14cbcSMatt Macy bleft -= done; 1422eda14cbcSMatt Macy } 1423eda14cbcSMatt Macy ASSERT(bleft < GCM_AVX_MIN_DECRYPT_BYTES); 1424eda14cbcSMatt Macy 1425eda14cbcSMatt Macy /* 142616038816SMartin Matuska * Now less than GCM_AVX_MIN_DECRYPT_BYTES bytes remain, 1427eda14cbcSMatt Macy * decrypt them block by block. 1428eda14cbcSMatt Macy */ 1429eda14cbcSMatt Macy while (bleft > 0) { 1430eda14cbcSMatt Macy /* Incomplete last block. */ 1431eda14cbcSMatt Macy if (bleft < block_size) { 1432eda14cbcSMatt Macy uint8_t *lastb = (uint8_t *)ctx->gcm_remainder; 1433eda14cbcSMatt Macy 1434da5137abSMartin Matuska memset(lastb, 0, block_size); 1435da5137abSMartin Matuska memcpy(lastb, datap, bleft); 1436eda14cbcSMatt Macy /* The GCM processing. */ 1437eda14cbcSMatt Macy GHASH_AVX(ctx, lastb, block_size); 1438eda14cbcSMatt Macy aes_encrypt_intel(key->encr_ks.ks32, key->nr, cb, tmp); 1439eda14cbcSMatt Macy for (size_t i = 0; i < bleft; i++) { 1440eda14cbcSMatt Macy datap[i] = lastb[i] ^ ((uint8_t *)tmp)[i]; 1441eda14cbcSMatt Macy } 1442eda14cbcSMatt Macy break; 1443eda14cbcSMatt Macy } 1444eda14cbcSMatt Macy /* The GCM processing. */ 1445eda14cbcSMatt Macy GHASH_AVX(ctx, datap, block_size); 1446eda14cbcSMatt Macy aes_encrypt_intel(key->encr_ks.ks32, key->nr, cb, tmp); 1447eda14cbcSMatt Macy gcm_xor_avx((uint8_t *)tmp, datap); 1448eda14cbcSMatt Macy gcm_incr_counter_block(ctx); 1449eda14cbcSMatt Macy 1450eda14cbcSMatt Macy datap += block_size; 1451eda14cbcSMatt Macy bleft -= block_size; 1452eda14cbcSMatt Macy } 1453eda14cbcSMatt Macy if (rv != CRYPTO_SUCCESS) { 1454eda14cbcSMatt Macy clear_fpu_regs(); 1455eda14cbcSMatt Macy kfpu_end(); 1456eda14cbcSMatt Macy return (rv); 1457eda14cbcSMatt Macy } 1458eda14cbcSMatt Macy /* Decryption done, finish the tag. */ 1459eda14cbcSMatt Macy ctx->gcm_len_a_len_c[1] = htonll(CRYPTO_BYTES2BITS(pt_len)); 1460eda14cbcSMatt Macy GHASH_AVX(ctx, (uint8_t *)ctx->gcm_len_a_len_c, block_size); 1461eda14cbcSMatt Macy aes_encrypt_intel(key->encr_ks.ks32, key->nr, (uint32_t *)ctx->gcm_J0, 1462eda14cbcSMatt Macy (uint32_t *)ctx->gcm_J0); 1463eda14cbcSMatt Macy 1464eda14cbcSMatt Macy gcm_xor_avx((uint8_t *)ctx->gcm_J0, (uint8_t *)ghash); 1465eda14cbcSMatt Macy 1466eda14cbcSMatt Macy /* We are done with the FPU, restore its state. */ 1467eda14cbcSMatt Macy clear_fpu_regs(); 1468eda14cbcSMatt Macy kfpu_end(); 1469eda14cbcSMatt Macy 1470eda14cbcSMatt Macy /* Compare the input authentication tag with what we calculated. */ 1471da5137abSMartin Matuska if (memcmp(&ctx->gcm_pt_buf[pt_len], ghash, ctx->gcm_tag_len)) { 1472eda14cbcSMatt Macy /* They don't match. */ 1473eda14cbcSMatt Macy return (CRYPTO_INVALID_MAC); 1474eda14cbcSMatt Macy } 1475eda14cbcSMatt Macy rv = crypto_put_output_data(ctx->gcm_pt_buf, out, pt_len); 1476eda14cbcSMatt Macy if (rv != CRYPTO_SUCCESS) { 1477eda14cbcSMatt Macy return (rv); 1478eda14cbcSMatt Macy } 1479eda14cbcSMatt Macy out->cd_offset += pt_len; 1480eda14cbcSMatt Macy gcm_clear_ctx(ctx); 1481eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 1482eda14cbcSMatt Macy } 1483eda14cbcSMatt Macy 1484eda14cbcSMatt Macy /* 1485eda14cbcSMatt Macy * Initialize the GCM params H, Htabtle and the counter block. Save the 1486eda14cbcSMatt Macy * initial counter block. 1487eda14cbcSMatt Macy */ 1488eda14cbcSMatt Macy static int 1489eda14cbcSMatt Macy gcm_init_avx(gcm_ctx_t *ctx, unsigned char *iv, size_t iv_len, 1490eda14cbcSMatt Macy unsigned char *auth_data, size_t auth_data_len, size_t block_size) 1491eda14cbcSMatt Macy { 1492eda14cbcSMatt Macy uint8_t *cb = (uint8_t *)ctx->gcm_cb; 1493eda14cbcSMatt Macy uint64_t *H = ctx->gcm_H; 1494eda14cbcSMatt Macy const void *keysched = ((aes_key_t *)ctx->gcm_keysched)->encr_ks.ks32; 1495eda14cbcSMatt Macy int aes_rounds = ((aes_key_t *)ctx->gcm_keysched)->nr; 1496eda14cbcSMatt Macy uint8_t *datap = auth_data; 1497eda14cbcSMatt Macy size_t chunk_size = (size_t)GCM_CHUNK_SIZE_READ; 1498eda14cbcSMatt Macy size_t bleft; 1499eda14cbcSMatt Macy 1500eda14cbcSMatt Macy ASSERT(block_size == GCM_BLOCK_LEN); 1501eda14cbcSMatt Macy 1502eda14cbcSMatt Macy /* Init H (encrypt zero block) and create the initial counter block. */ 1503da5137abSMartin Matuska memset(ctx->gcm_ghash, 0, sizeof (ctx->gcm_ghash)); 1504da5137abSMartin Matuska memset(H, 0, sizeof (ctx->gcm_H)); 1505eda14cbcSMatt Macy kfpu_begin(); 1506eda14cbcSMatt Macy aes_encrypt_intel(keysched, aes_rounds, 1507eda14cbcSMatt Macy (const uint32_t *)H, (uint32_t *)H); 1508eda14cbcSMatt Macy 1509eda14cbcSMatt Macy gcm_init_htab_avx(ctx->gcm_Htable, H); 1510eda14cbcSMatt Macy 1511eda14cbcSMatt Macy if (iv_len == 12) { 1512da5137abSMartin Matuska memcpy(cb, iv, 12); 1513eda14cbcSMatt Macy cb[12] = 0; 1514eda14cbcSMatt Macy cb[13] = 0; 1515eda14cbcSMatt Macy cb[14] = 0; 1516eda14cbcSMatt Macy cb[15] = 1; 1517eda14cbcSMatt Macy /* We need the ICB later. */ 1518da5137abSMartin Matuska memcpy(ctx->gcm_J0, cb, sizeof (ctx->gcm_J0)); 1519eda14cbcSMatt Macy } else { 1520eda14cbcSMatt Macy /* 1521eda14cbcSMatt Macy * Most consumers use 12 byte IVs, so it's OK to use the 1522eda14cbcSMatt Macy * original routines for other IV sizes, just avoid nesting 1523eda14cbcSMatt Macy * kfpu_begin calls. 1524eda14cbcSMatt Macy */ 1525eda14cbcSMatt Macy clear_fpu_regs(); 1526eda14cbcSMatt Macy kfpu_end(); 1527eda14cbcSMatt Macy gcm_format_initial_blocks(iv, iv_len, ctx, block_size, 1528eda14cbcSMatt Macy aes_copy_block, aes_xor_block); 1529eda14cbcSMatt Macy kfpu_begin(); 1530eda14cbcSMatt Macy } 1531eda14cbcSMatt Macy 1532eda14cbcSMatt Macy /* Openssl post increments the counter, adjust for that. */ 1533eda14cbcSMatt Macy gcm_incr_counter_block(ctx); 1534eda14cbcSMatt Macy 1535eda14cbcSMatt Macy /* Ghash AAD in chunk_size blocks. */ 1536eda14cbcSMatt Macy for (bleft = auth_data_len; bleft >= chunk_size; bleft -= chunk_size) { 1537eda14cbcSMatt Macy GHASH_AVX(ctx, datap, chunk_size); 1538eda14cbcSMatt Macy datap += chunk_size; 1539eda14cbcSMatt Macy clear_fpu_regs(); 1540eda14cbcSMatt Macy kfpu_end(); 1541eda14cbcSMatt Macy kfpu_begin(); 1542eda14cbcSMatt Macy } 1543eda14cbcSMatt Macy /* Ghash the remainder and handle possible incomplete GCM block. */ 1544eda14cbcSMatt Macy if (bleft > 0) { 1545eda14cbcSMatt Macy size_t incomp = bleft % block_size; 1546eda14cbcSMatt Macy 1547eda14cbcSMatt Macy bleft -= incomp; 1548eda14cbcSMatt Macy if (bleft > 0) { 1549eda14cbcSMatt Macy GHASH_AVX(ctx, datap, bleft); 1550eda14cbcSMatt Macy datap += bleft; 1551eda14cbcSMatt Macy } 1552eda14cbcSMatt Macy if (incomp > 0) { 1553eda14cbcSMatt Macy /* Zero pad and hash incomplete last block. */ 1554eda14cbcSMatt Macy uint8_t *authp = (uint8_t *)ctx->gcm_tmp; 1555eda14cbcSMatt Macy 1556da5137abSMartin Matuska memset(authp, 0, block_size); 1557da5137abSMartin Matuska memcpy(authp, datap, incomp); 1558eda14cbcSMatt Macy GHASH_AVX(ctx, authp, block_size); 1559eda14cbcSMatt Macy } 1560eda14cbcSMatt Macy } 1561eda14cbcSMatt Macy clear_fpu_regs(); 1562eda14cbcSMatt Macy kfpu_end(); 1563eda14cbcSMatt Macy return (CRYPTO_SUCCESS); 1564eda14cbcSMatt Macy } 1565eda14cbcSMatt Macy 1566eda14cbcSMatt Macy #if defined(_KERNEL) 1567eda14cbcSMatt Macy static int 1568eda14cbcSMatt Macy icp_gcm_avx_set_chunk_size(const char *buf, zfs_kernel_param_t *kp) 1569eda14cbcSMatt Macy { 1570eda14cbcSMatt Macy unsigned long val; 1571eda14cbcSMatt Macy char val_rounded[16]; 1572eda14cbcSMatt Macy int error = 0; 1573eda14cbcSMatt Macy 1574eda14cbcSMatt Macy error = kstrtoul(buf, 0, &val); 1575eda14cbcSMatt Macy if (error) 1576eda14cbcSMatt Macy return (error); 1577eda14cbcSMatt Macy 1578eda14cbcSMatt Macy val = (val / GCM_AVX_MIN_DECRYPT_BYTES) * GCM_AVX_MIN_DECRYPT_BYTES; 1579eda14cbcSMatt Macy 1580eda14cbcSMatt Macy if (val < GCM_AVX_MIN_ENCRYPT_BYTES || val > GCM_AVX_MAX_CHUNK_SIZE) 1581eda14cbcSMatt Macy return (-EINVAL); 1582eda14cbcSMatt Macy 1583eda14cbcSMatt Macy snprintf(val_rounded, 16, "%u", (uint32_t)val); 1584eda14cbcSMatt Macy error = param_set_uint(val_rounded, kp); 1585eda14cbcSMatt Macy return (error); 1586eda14cbcSMatt Macy } 1587eda14cbcSMatt Macy 1588eda14cbcSMatt Macy module_param_call(icp_gcm_avx_chunk_size, icp_gcm_avx_set_chunk_size, 1589eda14cbcSMatt Macy param_get_uint, &gcm_avx_chunk_size, 0644); 1590eda14cbcSMatt Macy 1591eda14cbcSMatt Macy MODULE_PARM_DESC(icp_gcm_avx_chunk_size, 1592eda14cbcSMatt Macy "How many bytes to process while owning the FPU"); 1593eda14cbcSMatt Macy 1594eda14cbcSMatt Macy #endif /* defined(__KERNEL) */ 1595eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */ 1596