Path: blob/main/sys/contrib/openzfs/module/icp/include/aes/aes_impl.h
48775 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 _AES_IMPL_H27#define _AES_IMPL_H2829/*30* Common definitions used by AES.31*/3233#ifdef __cplusplus34extern "C" {35#endif3637#include <sys/zfs_context.h>38#include <sys/crypto/common.h>39#include <sys/asm_linkage.h>4041/* Similar to sysmacros.h IS_P2ALIGNED, but checks two pointers: */42#define IS_P2ALIGNED2(v, w, a) \43((((uintptr_t)(v) | (uintptr_t)(w)) & ((uintptr_t)(a) - 1)) == 0)4445#define AES_BLOCK_LEN 16 /* bytes */46/* Round constant length, in number of 32-bit elements: */47#define RC_LENGTH (5 * ((AES_BLOCK_LEN) / 4 - 2))4849#define AES_COPY_BLOCK(src, dst) \50(dst)[0] = (src)[0]; \51(dst)[1] = (src)[1]; \52(dst)[2] = (src)[2]; \53(dst)[3] = (src)[3]; \54(dst)[4] = (src)[4]; \55(dst)[5] = (src)[5]; \56(dst)[6] = (src)[6]; \57(dst)[7] = (src)[7]; \58(dst)[8] = (src)[8]; \59(dst)[9] = (src)[9]; \60(dst)[10] = (src)[10]; \61(dst)[11] = (src)[11]; \62(dst)[12] = (src)[12]; \63(dst)[13] = (src)[13]; \64(dst)[14] = (src)[14]; \65(dst)[15] = (src)[15]6667#define AES_XOR_BLOCK(src, dst) \68(dst)[0] ^= (src)[0]; \69(dst)[1] ^= (src)[1]; \70(dst)[2] ^= (src)[2]; \71(dst)[3] ^= (src)[3]; \72(dst)[4] ^= (src)[4]; \73(dst)[5] ^= (src)[5]; \74(dst)[6] ^= (src)[6]; \75(dst)[7] ^= (src)[7]; \76(dst)[8] ^= (src)[8]; \77(dst)[9] ^= (src)[9]; \78(dst)[10] ^= (src)[10]; \79(dst)[11] ^= (src)[11]; \80(dst)[12] ^= (src)[12]; \81(dst)[13] ^= (src)[13]; \82(dst)[14] ^= (src)[14]; \83(dst)[15] ^= (src)[15]8485/* AES key size definitions */86#define AES_MINBITS 12887#define AES_MAXBITS 2568889/* AES key schedule may be implemented with 32- or 64-bit elements: */90#define AES_32BIT_KS 3291#define AES_64BIT_KS 649293#define MAX_AES_NR 14 /* Maximum number of rounds */94#define MAX_AES_NB 4 /* Number of columns comprising a state */9596typedef union {97#ifdef sun4u98uint64_t ks64[((MAX_AES_NR) + 1) * (MAX_AES_NB)];99#endif100uint32_t ks32[((MAX_AES_NR) + 1) * (MAX_AES_NB)];101} aes_ks_t;102103typedef struct aes_impl_ops aes_impl_ops_t;104105/*106* The absolute offset of the encr_ks (0) and the nr (504) fields are hard107* coded in aesni-gcm-x86_64, so please don't change (or adjust accordingly).108*/109typedef struct aes_key aes_key_t;110struct aes_key {111aes_ks_t encr_ks; /* encryption key schedule */112aes_ks_t decr_ks; /* decryption key schedule */113#ifdef __amd64114long double align128; /* Align fields above for Intel AES-NI */115#endif /* __amd64 */116const aes_impl_ops_t *ops; /* ops associated with this schedule */117int nr; /* number of rounds (10, 12, or 14) */118int type; /* key schedule size (32 or 64 bits) */119};120121/*122* Core AES functions.123* ks and keysched are pointers to aes_key_t.124* They are declared void* as they are intended to be opaque types.125* Use function aes_alloc_keysched() to allocate memory for ks and keysched.126*/127extern void *aes_alloc_keysched(size_t *size, int kmflag);128extern void aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits,129void *keysched);130extern int aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct);131extern int aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt);132133/*134* AES mode functions.135* The first 2 functions operate on 16-byte AES blocks.136*/137extern void aes_copy_block(uint8_t *in, uint8_t *out);138extern void aes_xor_block(uint8_t *data, uint8_t *dst);139140/* Note: ctx is a pointer to aes_ctx_t defined in modes.h */141extern int aes_encrypt_contiguous_blocks(void *ctx, char *data, size_t length,142crypto_data_t *out);143extern int aes_decrypt_contiguous_blocks(void *ctx, char *data, size_t length,144crypto_data_t *out);145146/*147* The following definitions and declarations are only used by AES FIPS POST148*/149#ifdef _AES_IMPL150151typedef enum aes_mech_type {152AES_CCM_MECH_INFO_TYPE, /* SUN_CKM_AES_CCM */153AES_GCM_MECH_INFO_TYPE, /* SUN_CKM_AES_GCM */154} aes_mech_type_t;155156#endif /* _AES_IMPL */157158/*159* Methods used to define AES implementation160*161* @aes_gen_f Key generation162* @aes_enc_f Function encrypts one block163* @aes_dec_f Function decrypts one block164* @aes_will_work_f Function tests whether method will function165*/166typedef void (*aes_generate_f)(aes_key_t *, const uint32_t *, int);167typedef void (*aes_encrypt_f)(const uint32_t[], int,168const uint32_t[4], uint32_t[4]);169typedef void (*aes_decrypt_f)(const uint32_t[], int,170const uint32_t[4], uint32_t[4]);171typedef boolean_t (*aes_will_work_f)(void);172173#define AES_IMPL_NAME_MAX (16)174175struct aes_impl_ops {176aes_generate_f generate;177aes_encrypt_f encrypt;178aes_decrypt_f decrypt;179aes_will_work_f is_supported;180boolean_t needs_byteswap;181char name[AES_IMPL_NAME_MAX];182};183184extern const aes_impl_ops_t aes_generic_impl;185#if defined(__x86_64)186extern const aes_impl_ops_t aes_x86_64_impl;187188/* These functions are used to execute amd64 instructions for AMD or Intel: */189extern ASMABI int rijndael_key_setup_enc_amd64(uint32_t rk[],190const uint32_t cipherKey[], int keyBits);191extern ASMABI int rijndael_key_setup_dec_amd64(uint32_t rk[],192const uint32_t cipherKey[], int keyBits);193extern ASMABI void aes_encrypt_amd64(const uint32_t rk[], int Nr,194const uint32_t pt[4], uint32_t ct[4]);195extern ASMABI void aes_decrypt_amd64(const uint32_t rk[], int Nr,196const uint32_t ct[4], uint32_t pt[4]);197#endif198#if defined(__x86_64) && defined(HAVE_AES)199extern const aes_impl_ops_t aes_aesni_impl;200#endif201202/*203* Initializes fastest implementation204*/205void aes_impl_init(void);206207/*208* Returns optimal allowed AES implementation209*/210const struct aes_impl_ops *aes_impl_get_ops(void);211212#ifdef __cplusplus213}214#endif215216#endif /* _AES_IMPL_H */217218219