1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #ifndef _COMMON_CRYPTO_MODES_H 28 #define _COMMON_CRYPTO_MODES_H 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #include <sys/zfs_context.h> 35 #include <sys/crypto/common.h> 36 #include <sys/crypto/impl.h> 37 38 /* 39 * Does the build chain support all instructions needed for the GCM assembler 40 * routines. AVX support should imply AES-NI and PCLMULQDQ, but make sure 41 * anyhow. 42 */ 43 #if defined(__x86_64__) && defined(HAVE_AVX) && \ 44 defined(HAVE_AES) && defined(HAVE_PCLMULQDQ) 45 #define CAN_USE_GCM_ASM 46 extern boolean_t gcm_avx_can_use_movbe; 47 #endif 48 49 #define CCM_MODE 0x00000010 50 #define GCM_MODE 0x00000020 51 52 /* 53 * cc_keysched: Pointer to key schedule. 54 * 55 * cc_keysched_len: Length of the key schedule. 56 * 57 * cc_remainder: This is for residual data, i.e. data that can't 58 * be processed because there are too few bytes. 59 * Must wait until more data arrives. 60 * 61 * cc_remainder_len: Number of bytes in cc_remainder. 62 * 63 * cc_iv: Scratch buffer that sometimes contains the IV. 64 * 65 * cc_lastp: Pointer to previous block of ciphertext. 66 * 67 * cc_copy_to: Pointer to where encrypted residual data needs 68 * to be copied. 69 * 70 * cc_flags: PROVIDER_OWNS_KEY_SCHEDULE 71 * When a context is freed, it is necessary 72 * to know whether the key schedule was allocated 73 * by the caller, or internally, e.g. an init routine. 74 * If allocated by the latter, then it needs to be freed. 75 * 76 * CCM_MODE 77 */ 78 struct common_ctx { 79 void *cc_keysched; 80 size_t cc_keysched_len; 81 uint64_t cc_iv[2]; 82 uint64_t cc_remainder[2]; 83 size_t cc_remainder_len; 84 uint8_t *cc_lastp; 85 uint8_t *cc_copy_to; 86 uint32_t cc_flags; 87 }; 88 89 typedef struct common_ctx common_ctx_t; 90 91 /* 92 * 93 * ccm_mac_len: Stores length of the MAC in CCM mode. 94 * ccm_mac_buf: Stores the intermediate value for MAC in CCM encrypt. 95 * In CCM decrypt, stores the input MAC value. 96 * ccm_data_len: Length of the plaintext for CCM mode encrypt, or 97 * length of the ciphertext for CCM mode decrypt. 98 * ccm_processed_data_len: 99 * Length of processed plaintext in CCM mode encrypt, 100 * or length of processed ciphertext for CCM mode decrypt. 101 * ccm_processed_mac_len: 102 * Length of MAC data accumulated in CCM mode decrypt. 103 * 104 * ccm_pt_buf: Only used in CCM mode decrypt. It stores the 105 * decrypted plaintext to be returned when 106 * MAC verification succeeds in decrypt_final. 107 * Memory for this should be allocated in the AES module. 108 * 109 */ 110 typedef struct ccm_ctx { 111 struct common_ctx ccm_common; 112 uint32_t ccm_tmp[4]; 113 size_t ccm_mac_len; 114 uint64_t ccm_mac_buf[2]; 115 size_t ccm_data_len; 116 size_t ccm_processed_data_len; 117 size_t ccm_processed_mac_len; 118 uint8_t *ccm_pt_buf; 119 uint64_t ccm_mac_input_buf[2]; 120 uint64_t ccm_counter_mask; 121 } ccm_ctx_t; 122 123 #define ccm_keysched ccm_common.cc_keysched 124 #define ccm_keysched_len ccm_common.cc_keysched_len 125 #define ccm_cb ccm_common.cc_iv 126 #define ccm_remainder ccm_common.cc_remainder 127 #define ccm_remainder_len ccm_common.cc_remainder_len 128 #define ccm_lastp ccm_common.cc_lastp 129 #define ccm_copy_to ccm_common.cc_copy_to 130 #define ccm_flags ccm_common.cc_flags 131 132 /* 133 * gcm_tag_len: Length of authentication tag. 134 * 135 * gcm_ghash: Stores output from the GHASH function. 136 * 137 * gcm_processed_data_len: 138 * Length of processed plaintext (encrypt) or 139 * length of processed ciphertext (decrypt). 140 * 141 * gcm_pt_buf: Stores the decrypted plaintext returned by 142 * decrypt_final when the computed authentication 143 * tag matches the user supplied tag. 144 * 145 * gcm_pt_buf_len: Length of the plaintext buffer. 146 * 147 * gcm_H: Subkey. 148 * 149 * gcm_Htable: Pre-computed and pre-shifted H, H^2, ... H^6 for the 150 * Karatsuba Algorithm in host byte order. 151 * 152 * gcm_J0: Pre-counter block generated from the IV. 153 * 154 * gcm_len_a_len_c: 64-bit representations of the bit lengths of 155 * AAD and ciphertext. 156 */ 157 typedef struct gcm_ctx { 158 struct common_ctx gcm_common; 159 size_t gcm_tag_len; 160 size_t gcm_processed_data_len; 161 size_t gcm_pt_buf_len; 162 uint32_t gcm_tmp[4]; 163 /* 164 * The offset of gcm_Htable relative to gcm_ghash, (32), is hard coded 165 * in aesni-gcm-x86_64.S, so please don't change (or adjust there). 166 */ 167 uint64_t gcm_ghash[2]; 168 uint64_t gcm_H[2]; 169 #ifdef CAN_USE_GCM_ASM 170 uint64_t *gcm_Htable; 171 size_t gcm_htab_len; 172 #endif 173 uint64_t gcm_J0[2]; 174 uint64_t gcm_len_a_len_c[2]; 175 uint8_t *gcm_pt_buf; 176 #ifdef CAN_USE_GCM_ASM 177 boolean_t gcm_use_avx; 178 #endif 179 } gcm_ctx_t; 180 181 #define gcm_keysched gcm_common.cc_keysched 182 #define gcm_keysched_len gcm_common.cc_keysched_len 183 #define gcm_cb gcm_common.cc_iv 184 #define gcm_remainder gcm_common.cc_remainder 185 #define gcm_remainder_len gcm_common.cc_remainder_len 186 #define gcm_lastp gcm_common.cc_lastp 187 #define gcm_copy_to gcm_common.cc_copy_to 188 #define gcm_flags gcm_common.cc_flags 189 190 void gcm_clear_ctx(gcm_ctx_t *ctx); 191 192 typedef struct aes_ctx { 193 union { 194 ccm_ctx_t acu_ccm; 195 gcm_ctx_t acu_gcm; 196 } acu; 197 } aes_ctx_t; 198 199 #define ac_flags acu.acu_ccm.ccm_common.cc_flags 200 #define ac_remainder_len acu.acu_ccm.ccm_common.cc_remainder_len 201 #define ac_keysched acu.acu_ccm.ccm_common.cc_keysched 202 #define ac_keysched_len acu.acu_ccm.ccm_common.cc_keysched_len 203 #define ac_iv acu.acu_ccm.ccm_common.cc_iv 204 #define ac_lastp acu.acu_ccm.ccm_common.cc_lastp 205 #define ac_pt_buf acu.acu_ccm.ccm_pt_buf 206 #define ac_mac_len acu.acu_ccm.ccm_mac_len 207 #define ac_data_len acu.acu_ccm.ccm_data_len 208 #define ac_processed_mac_len acu.acu_ccm.ccm_processed_mac_len 209 #define ac_processed_data_len acu.acu_ccm.ccm_processed_data_len 210 #define ac_tag_len acu.acu_gcm.gcm_tag_len 211 212 extern int ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t, 213 crypto_data_t *, size_t, 214 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 215 void (*copy_block)(uint8_t *, uint8_t *), 216 void (*xor_block)(uint8_t *, uint8_t *)); 217 218 extern int ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t, 219 crypto_data_t *, size_t, 220 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 221 void (*copy_block)(uint8_t *, uint8_t *), 222 void (*xor_block)(uint8_t *, uint8_t *)); 223 224 extern int gcm_mode_encrypt_contiguous_blocks(gcm_ctx_t *, char *, size_t, 225 crypto_data_t *, size_t, 226 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 227 void (*copy_block)(uint8_t *, uint8_t *), 228 void (*xor_block)(uint8_t *, uint8_t *)); 229 230 extern int gcm_mode_decrypt_contiguous_blocks(gcm_ctx_t *, char *, size_t, 231 crypto_data_t *, size_t, 232 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 233 void (*copy_block)(uint8_t *, uint8_t *), 234 void (*xor_block)(uint8_t *, uint8_t *)); 235 236 int ccm_encrypt_final(ccm_ctx_t *, crypto_data_t *, size_t, 237 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 238 void (*xor_block)(uint8_t *, uint8_t *)); 239 240 int gcm_encrypt_final(gcm_ctx_t *, crypto_data_t *, size_t, 241 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 242 void (*copy_block)(uint8_t *, uint8_t *), 243 void (*xor_block)(uint8_t *, uint8_t *)); 244 245 extern int ccm_decrypt_final(ccm_ctx_t *, crypto_data_t *, size_t, 246 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 247 void (*copy_block)(uint8_t *, uint8_t *), 248 void (*xor_block)(uint8_t *, uint8_t *)); 249 250 extern int gcm_decrypt_final(gcm_ctx_t *, crypto_data_t *, size_t, 251 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 252 void (*xor_block)(uint8_t *, uint8_t *)); 253 254 extern int ccm_init_ctx(ccm_ctx_t *, char *, int, boolean_t, size_t, 255 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 256 void (*xor_block)(uint8_t *, uint8_t *)); 257 258 extern int gcm_init_ctx(gcm_ctx_t *, char *, size_t, 259 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 260 void (*copy_block)(uint8_t *, uint8_t *), 261 void (*xor_block)(uint8_t *, uint8_t *)); 262 263 extern void calculate_ccm_mac(ccm_ctx_t *, uint8_t *, 264 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *)); 265 266 extern void gcm_mul(uint64_t *, uint64_t *, uint64_t *); 267 268 extern void crypto_init_ptrs(crypto_data_t *, void **, offset_t *); 269 extern void crypto_get_ptrs(crypto_data_t *, void **, offset_t *, 270 uint8_t **, size_t *, uint8_t **, size_t); 271 272 extern void *ccm_alloc_ctx(int); 273 extern void *gcm_alloc_ctx(int); 274 extern void crypto_free_mode_ctx(void *); 275 276 #ifdef __cplusplus 277 } 278 #endif 279 280 #endif /* _COMMON_CRYPTO_MODES_H */ 281