Path: blob/main/sys/contrib/openzfs/module/icp/include/modes/modes.h
48676 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2009 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#ifndef _COMMON_CRYPTO_MODES_H27#define _COMMON_CRYPTO_MODES_H2829#ifdef __cplusplus30extern "C" {31#endif3233#include <sys/zfs_context.h>34#include <sys/crypto/common.h>35#include <sys/crypto/impl.h>3637/*38* Does the build chain support all instructions needed for the GCM assembler39* routines. AVX support should imply AES-NI and PCLMULQDQ, but make sure40* anyhow.41*/42#if defined(__x86_64__) && defined(HAVE_AVX) && \43defined(HAVE_AES) && defined(HAVE_PCLMULQDQ)44#define CAN_USE_GCM_ASM (HAVE_VAES && HAVE_VPCLMULQDQ ? 2 : 1)45extern boolean_t gcm_avx_can_use_movbe;46#endif4748#define CCM_MODE 0x0000001049#define GCM_MODE 0x000000205051/*52* cc_keysched: Pointer to key schedule.53*54* cc_keysched_len: Length of the key schedule.55*56* cc_remainder: This is for residual data, i.e. data that can't57* be processed because there are too few bytes.58* Must wait until more data arrives.59*60* cc_remainder_len: Number of bytes in cc_remainder.61*62* cc_iv: Scratch buffer that sometimes contains the IV.63*64* cc_lastp: Pointer to previous block of ciphertext.65*66* cc_copy_to: Pointer to where encrypted residual data needs67* to be copied.68*69* cc_flags: PROVIDER_OWNS_KEY_SCHEDULE70* When a context is freed, it is necessary71* to know whether the key schedule was allocated72* by the caller, or internally, e.g. an init routine.73* If allocated by the latter, then it needs to be freed.74*75* CCM_MODE76*/77struct common_ctx {78void *cc_keysched;79size_t cc_keysched_len;80uint64_t cc_iv[2];81uint64_t cc_remainder[2];82size_t cc_remainder_len;83uint8_t *cc_lastp;84uint8_t *cc_copy_to;85uint32_t cc_flags;86};8788typedef struct common_ctx common_ctx_t;8990/*91*92* ccm_mac_len: Stores length of the MAC in CCM mode.93* ccm_mac_buf: Stores the intermediate value for MAC in CCM encrypt.94* In CCM decrypt, stores the input MAC value.95* ccm_data_len: Length of the plaintext for CCM mode encrypt, or96* length of the ciphertext for CCM mode decrypt.97* ccm_processed_data_len:98* Length of processed plaintext in CCM mode encrypt,99* or length of processed ciphertext for CCM mode decrypt.100* ccm_processed_mac_len:101* Length of MAC data accumulated in CCM mode decrypt.102*103* ccm_pt_buf: Only used in CCM mode decrypt. It stores the104* decrypted plaintext to be returned when105* MAC verification succeeds in decrypt_final.106* Memory for this should be allocated in the AES module.107*108*/109typedef struct ccm_ctx {110struct common_ctx ccm_common;111uint32_t ccm_tmp[4];112size_t ccm_mac_len;113uint64_t ccm_mac_buf[2];114size_t ccm_data_len;115size_t ccm_processed_data_len;116size_t ccm_processed_mac_len;117uint8_t *ccm_pt_buf;118uint64_t ccm_mac_input_buf[2];119uint64_t ccm_counter_mask;120} ccm_ctx_t;121122#define ccm_keysched ccm_common.cc_keysched123#define ccm_keysched_len ccm_common.cc_keysched_len124#define ccm_cb ccm_common.cc_iv125#define ccm_remainder ccm_common.cc_remainder126#define ccm_remainder_len ccm_common.cc_remainder_len127#define ccm_lastp ccm_common.cc_lastp128#define ccm_copy_to ccm_common.cc_copy_to129#define ccm_flags ccm_common.cc_flags130131#ifdef CAN_USE_GCM_ASM132typedef enum gcm_impl {133GCM_IMPL_GENERIC = 0,134GCM_IMPL_AVX,135GCM_IMPL_AVX2,136GCM_IMPL_MAX,137} gcm_impl;138#endif139140/*141* gcm_tag_len: Length of authentication tag.142*143* gcm_ghash: Stores output from the GHASH function.144*145* gcm_processed_data_len:146* Length of processed plaintext (encrypt) or147* length of processed ciphertext (decrypt).148*149* gcm_pt_buf: Stores the decrypted plaintext returned by150* decrypt_final when the computed authentication151* tag matches the user supplied tag.152*153* gcm_pt_buf_len: Length of the plaintext buffer.154*155* gcm_H: Subkey.156*157* gcm_Htable: Pre-computed and pre-shifted H, H^2, ... H^6 for the158* Karatsuba Algorithm in host byte order.159*160* gcm_J0: Pre-counter block generated from the IV.161*162* gcm_len_a_len_c: 64-bit representations of the bit lengths of163* AAD and ciphertext.164*/165typedef struct gcm_ctx {166struct common_ctx gcm_common;167size_t gcm_tag_len;168size_t gcm_processed_data_len;169size_t gcm_pt_buf_len;170uint32_t gcm_tmp[4];171/*172* The offset of gcm_Htable relative to gcm_ghash, (32), is hard coded173* in aesni-gcm-x86_64.S, so please don't change (or adjust there).174*/175uint64_t gcm_ghash[2];176uint64_t gcm_H[2];177#ifdef CAN_USE_GCM_ASM178uint64_t *gcm_Htable;179size_t gcm_htab_len;180#endif181uint64_t gcm_J0[2];182uint64_t gcm_len_a_len_c[2];183uint8_t *gcm_pt_buf;184#ifdef CAN_USE_GCM_ASM185enum gcm_impl impl;186#endif187} gcm_ctx_t;188189#define gcm_keysched gcm_common.cc_keysched190#define gcm_keysched_len gcm_common.cc_keysched_len191#define gcm_cb gcm_common.cc_iv192#define gcm_remainder gcm_common.cc_remainder193#define gcm_remainder_len gcm_common.cc_remainder_len194#define gcm_lastp gcm_common.cc_lastp195#define gcm_copy_to gcm_common.cc_copy_to196#define gcm_flags gcm_common.cc_flags197198void gcm_clear_ctx(gcm_ctx_t *ctx);199200typedef struct aes_ctx {201union {202ccm_ctx_t acu_ccm;203gcm_ctx_t acu_gcm;204} acu;205} aes_ctx_t;206207#define ac_flags acu.acu_ccm.ccm_common.cc_flags208#define ac_remainder_len acu.acu_ccm.ccm_common.cc_remainder_len209#define ac_keysched acu.acu_ccm.ccm_common.cc_keysched210#define ac_keysched_len acu.acu_ccm.ccm_common.cc_keysched_len211#define ac_iv acu.acu_ccm.ccm_common.cc_iv212#define ac_lastp acu.acu_ccm.ccm_common.cc_lastp213#define ac_pt_buf acu.acu_ccm.ccm_pt_buf214#define ac_mac_len acu.acu_ccm.ccm_mac_len215#define ac_data_len acu.acu_ccm.ccm_data_len216#define ac_processed_mac_len acu.acu_ccm.ccm_processed_mac_len217#define ac_processed_data_len acu.acu_ccm.ccm_processed_data_len218#define ac_tag_len acu.acu_gcm.gcm_tag_len219220extern int ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t,221crypto_data_t *, size_t,222int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),223void (*copy_block)(uint8_t *, uint8_t *),224void (*xor_block)(uint8_t *, uint8_t *));225226extern int ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t,227crypto_data_t *, size_t,228int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),229void (*copy_block)(uint8_t *, uint8_t *),230void (*xor_block)(uint8_t *, uint8_t *));231232extern int gcm_mode_encrypt_contiguous_blocks(gcm_ctx_t *, char *, size_t,233crypto_data_t *, size_t,234int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),235void (*copy_block)(uint8_t *, uint8_t *),236void (*xor_block)(uint8_t *, uint8_t *));237238extern int gcm_mode_decrypt_contiguous_blocks(gcm_ctx_t *, char *, size_t,239crypto_data_t *, size_t,240int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),241void (*copy_block)(uint8_t *, uint8_t *),242void (*xor_block)(uint8_t *, uint8_t *));243244int ccm_encrypt_final(ccm_ctx_t *, crypto_data_t *, size_t,245int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),246void (*xor_block)(uint8_t *, uint8_t *));247248int gcm_encrypt_final(gcm_ctx_t *, crypto_data_t *, size_t,249int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),250void (*copy_block)(uint8_t *, uint8_t *),251void (*xor_block)(uint8_t *, uint8_t *));252253extern int ccm_decrypt_final(ccm_ctx_t *, crypto_data_t *, size_t,254int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),255void (*copy_block)(uint8_t *, uint8_t *),256void (*xor_block)(uint8_t *, uint8_t *));257258extern int gcm_decrypt_final(gcm_ctx_t *, crypto_data_t *, size_t,259int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),260void (*xor_block)(uint8_t *, uint8_t *));261262extern int ccm_init_ctx(ccm_ctx_t *, char *, int, boolean_t, size_t,263int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),264void (*xor_block)(uint8_t *, uint8_t *));265266extern int gcm_init_ctx(gcm_ctx_t *, char *, size_t,267int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),268void (*copy_block)(uint8_t *, uint8_t *),269void (*xor_block)(uint8_t *, uint8_t *));270271extern void calculate_ccm_mac(ccm_ctx_t *, uint8_t *,272int (*encrypt_block)(const void *, const uint8_t *, uint8_t *));273274extern void gcm_mul(uint64_t *, uint64_t *, uint64_t *);275276extern void crypto_init_ptrs(crypto_data_t *, void **, offset_t *);277extern void crypto_get_ptrs(crypto_data_t *, void **, offset_t *,278uint8_t **, size_t *, uint8_t **, size_t);279280extern void *ccm_alloc_ctx(int);281extern void *gcm_alloc_ctx(int);282extern void crypto_free_mode_ctx(void *);283284#ifdef __cplusplus285}286#endif287288#endif /* _COMMON_CRYPTO_MODES_H */289290291