10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 56125Sbubbva * Common Development and Distribution License (the "License"). 66125Sbubbva * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 229392Sopensolaris@drydog.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _AES_IMPL_H 270Sstevel@tonic-gate #define _AES_IMPL_H 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * Common definitions used by AES. 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #ifdef __cplusplus 340Sstevel@tonic-gate extern "C" { 350Sstevel@tonic-gate #endif 360Sstevel@tonic-gate 376877Sda73024 #include <sys/types.h> 387188Smcpowers #include <sys/crypto/common.h> 396877Sda73024 406877Sda73024 /* Similar to sysmacros.h IS_P2ALIGNED, but checks two pointers: */ 416877Sda73024 #define IS_P2ALIGNED2(v, w, a) \ 426877Sda73024 ((((uintptr_t)(v) | (uintptr_t)(w)) & ((uintptr_t)(a) - 1)) == 0) 436877Sda73024 446877Sda73024 #define AES_BLOCK_LEN 16 /* bytes */ 456877Sda73024 /* Round constant length, in number of 32-bit elements: */ 466877Sda73024 #define RC_LENGTH (5 * ((AES_BLOCK_LEN) / 4 - 2)) 470Sstevel@tonic-gate 480Sstevel@tonic-gate #define AES_COPY_BLOCK(src, dst) \ 490Sstevel@tonic-gate (dst)[0] = (src)[0]; \ 500Sstevel@tonic-gate (dst)[1] = (src)[1]; \ 510Sstevel@tonic-gate (dst)[2] = (src)[2]; \ 520Sstevel@tonic-gate (dst)[3] = (src)[3]; \ 530Sstevel@tonic-gate (dst)[4] = (src)[4]; \ 540Sstevel@tonic-gate (dst)[5] = (src)[5]; \ 550Sstevel@tonic-gate (dst)[6] = (src)[6]; \ 560Sstevel@tonic-gate (dst)[7] = (src)[7]; \ 570Sstevel@tonic-gate (dst)[8] = (src)[8]; \ 580Sstevel@tonic-gate (dst)[9] = (src)[9]; \ 590Sstevel@tonic-gate (dst)[10] = (src)[10]; \ 600Sstevel@tonic-gate (dst)[11] = (src)[11]; \ 610Sstevel@tonic-gate (dst)[12] = (src)[12]; \ 620Sstevel@tonic-gate (dst)[13] = (src)[13]; \ 630Sstevel@tonic-gate (dst)[14] = (src)[14]; \ 640Sstevel@tonic-gate (dst)[15] = (src)[15] 650Sstevel@tonic-gate 660Sstevel@tonic-gate #define AES_XOR_BLOCK(src, dst) \ 670Sstevel@tonic-gate (dst)[0] ^= (src)[0]; \ 680Sstevel@tonic-gate (dst)[1] ^= (src)[1]; \ 690Sstevel@tonic-gate (dst)[2] ^= (src)[2]; \ 700Sstevel@tonic-gate (dst)[3] ^= (src)[3]; \ 710Sstevel@tonic-gate (dst)[4] ^= (src)[4]; \ 720Sstevel@tonic-gate (dst)[5] ^= (src)[5]; \ 730Sstevel@tonic-gate (dst)[6] ^= (src)[6]; \ 740Sstevel@tonic-gate (dst)[7] ^= (src)[7]; \ 750Sstevel@tonic-gate (dst)[8] ^= (src)[8]; \ 760Sstevel@tonic-gate (dst)[9] ^= (src)[9]; \ 770Sstevel@tonic-gate (dst)[10] ^= (src)[10]; \ 780Sstevel@tonic-gate (dst)[11] ^= (src)[11]; \ 790Sstevel@tonic-gate (dst)[12] ^= (src)[12]; \ 800Sstevel@tonic-gate (dst)[13] ^= (src)[13]; \ 810Sstevel@tonic-gate (dst)[14] ^= (src)[14]; \ 820Sstevel@tonic-gate (dst)[15] ^= (src)[15] 830Sstevel@tonic-gate 846877Sda73024 /* AES key size definitions */ 850Sstevel@tonic-gate #define AES_MINBITS 128 866877Sda73024 #define AES_MINBYTES ((AES_MINBITS) >> 3) 870Sstevel@tonic-gate #define AES_MAXBITS 256 886877Sda73024 #define AES_MAXBYTES ((AES_MAXBITS) >> 3) 890Sstevel@tonic-gate 906877Sda73024 #define AES_MIN_KEY_BYTES ((AES_MINBITS) >> 3) 916877Sda73024 #define AES_MAX_KEY_BYTES ((AES_MAXBITS) >> 3) 920Sstevel@tonic-gate #define AES_192_KEY_BYTES 24 930Sstevel@tonic-gate #define AES_IV_LEN 16 940Sstevel@tonic-gate 956877Sda73024 /* AES key schedule may be implemented with 32- or 64-bit elements: */ 960Sstevel@tonic-gate #define AES_32BIT_KS 32 970Sstevel@tonic-gate #define AES_64BIT_KS 64 980Sstevel@tonic-gate 996877Sda73024 #define MAX_AES_NR 14 /* Maximum number of rounds */ 1006877Sda73024 #define MAX_AES_NB 4 /* Number of columns comprising a state */ 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate typedef union { 1036877Sda73024 #ifdef sun4u 1046877Sda73024 uint64_t ks64[((MAX_AES_NR) + 1) * (MAX_AES_NB)]; 1056877Sda73024 #endif 1066877Sda73024 uint32_t ks32[((MAX_AES_NR) + 1) * (MAX_AES_NB)]; 1070Sstevel@tonic-gate } aes_ks_t; 1080Sstevel@tonic-gate 1099392Sopensolaris@drydog.com /* aes_key.flags value: */ 1109392Sopensolaris@drydog.com #define INTEL_AES_NI_CAPABLE 0x1 /* AES-NI instructions present */ 1119392Sopensolaris@drydog.com 1120Sstevel@tonic-gate typedef struct aes_key aes_key_t; 1130Sstevel@tonic-gate struct aes_key { 1149392Sopensolaris@drydog.com aes_ks_t encr_ks; /* encryption key schedule */ 1159392Sopensolaris@drydog.com aes_ks_t decr_ks; /* decryption key schedule */ 1169392Sopensolaris@drydog.com #ifdef __amd64 1179392Sopensolaris@drydog.com long double align128; /* Align fields above for Intel AES-NI */ 1189392Sopensolaris@drydog.com int flags; /* implementation-dependent flags */ 1199392Sopensolaris@drydog.com #endif /* __amd64 */ 1209392Sopensolaris@drydog.com int nr; /* number of rounds (10, 12, or 14) */ 1219392Sopensolaris@drydog.com int type; /* key schedule size (32 or 64 bits) */ 1220Sstevel@tonic-gate }; 1230Sstevel@tonic-gate 1249392Sopensolaris@drydog.com /* 1259392Sopensolaris@drydog.com * Core AES functions. 1269392Sopensolaris@drydog.com * ks and keysched are pointers to aes_key_t. 1279392Sopensolaris@drydog.com * They are declared void* as they are intended to be opaque types. 1289392Sopensolaris@drydog.com * Use function aes_alloc_keysched() to allocate memory for ks and keysched. 1299392Sopensolaris@drydog.com */ 1309392Sopensolaris@drydog.com extern void *aes_alloc_keysched(size_t *size, int kmflag); 1319392Sopensolaris@drydog.com extern void aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits, 1329392Sopensolaris@drydog.com void *keysched); 1337188Smcpowers extern int aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct); 1347188Smcpowers extern int aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt); 1359392Sopensolaris@drydog.com 1369392Sopensolaris@drydog.com /* 1379392Sopensolaris@drydog.com * AES mode functions. 1389392Sopensolaris@drydog.com * The first 2 functions operate on 16-byte AES blocks. 1399392Sopensolaris@drydog.com */ 1409392Sopensolaris@drydog.com extern void aes_copy_block(uint8_t *in, uint8_t *out); 1419392Sopensolaris@drydog.com extern void aes_xor_block(uint8_t *data, uint8_t *dst); 1429392Sopensolaris@drydog.com 1439392Sopensolaris@drydog.com /* Note: ctx is a pointer to aes_ctx_t defined in modes.h */ 1449392Sopensolaris@drydog.com extern int aes_encrypt_contiguous_blocks(void *ctx, char *data, size_t length, 1459392Sopensolaris@drydog.com crypto_data_t *out); 1469392Sopensolaris@drydog.com extern int aes_decrypt_contiguous_blocks(void *ctx, char *data, size_t length, 1479392Sopensolaris@drydog.com crypto_data_t *out); 1480Sstevel@tonic-gate 149*10500SHai-May.Chao@Sun.COM /* 150*10500SHai-May.Chao@Sun.COM * The following definitions and declarations are only used by AES FIPS POST 151*10500SHai-May.Chao@Sun.COM */ 152*10500SHai-May.Chao@Sun.COM #ifdef _AES_FIPS_POST 153*10500SHai-May.Chao@Sun.COM 154*10500SHai-May.Chao@Sun.COM #include <fips/fips_post.h> 155*10500SHai-May.Chao@Sun.COM 156*10500SHai-May.Chao@Sun.COM /* 157*10500SHai-May.Chao@Sun.COM * FIPS preprocessor directives for AES-ECB and AES-CBC. 158*10500SHai-May.Chao@Sun.COM */ 159*10500SHai-May.Chao@Sun.COM #define FIPS_AES_BLOCK_SIZE 16 /* 128-bits */ 160*10500SHai-May.Chao@Sun.COM #define FIPS_AES_ENCRYPT_LENGTH 16 /* 128-bits */ 161*10500SHai-May.Chao@Sun.COM #define FIPS_AES_DECRYPT_LENGTH 16 /* 128-bits */ 162*10500SHai-May.Chao@Sun.COM #define FIPS_AES_128_KEY_SIZE 16 /* 128-bits */ 163*10500SHai-May.Chao@Sun.COM #define FIPS_AES_192_KEY_SIZE 24 /* 192-bits */ 164*10500SHai-May.Chao@Sun.COM #define FIPS_AES_256_KEY_SIZE 32 /* 256-bits */ 165*10500SHai-May.Chao@Sun.COM 166*10500SHai-May.Chao@Sun.COM 167*10500SHai-May.Chao@Sun.COM #ifdef _KERNEL 168*10500SHai-May.Chao@Sun.COM typedef enum aes_mech_type { 169*10500SHai-May.Chao@Sun.COM AES_ECB_MECH_INFO_TYPE, /* SUN_CKM_AES_ECB */ 170*10500SHai-May.Chao@Sun.COM AES_CBC_MECH_INFO_TYPE, /* SUN_CKM_AES_CBC */ 171*10500SHai-May.Chao@Sun.COM AES_CBC_PAD_MECH_INFO_TYPE, /* SUN_CKM_AES_CBC_PAD */ 172*10500SHai-May.Chao@Sun.COM AES_CTR_MECH_INFO_TYPE, /* SUN_CKM_AES_CTR */ 173*10500SHai-May.Chao@Sun.COM AES_CCM_MECH_INFO_TYPE, /* SUN_CKM_AES_CCM */ 174*10500SHai-May.Chao@Sun.COM AES_GCM_MECH_INFO_TYPE, /* SUN_CKM_AES_GCM */ 175*10500SHai-May.Chao@Sun.COM AES_GMAC_MECH_INFO_TYPE /* SUN_CKM_AES_GMAC */ 176*10500SHai-May.Chao@Sun.COM } aes_mech_type_t; 177*10500SHai-May.Chao@Sun.COM 178*10500SHai-May.Chao@Sun.COM #undef CKM_AES_ECB 179*10500SHai-May.Chao@Sun.COM #undef CKM_AES_CBC 180*10500SHai-May.Chao@Sun.COM #undef CKM_AES_CTR 181*10500SHai-May.Chao@Sun.COM 182*10500SHai-May.Chao@Sun.COM #define CKM_AES_ECB AES_ECB_MECH_INFO_TYPE 183*10500SHai-May.Chao@Sun.COM #define CKM_AES_CBC AES_CBC_MECH_INFO_TYPE 184*10500SHai-May.Chao@Sun.COM #define CKM_AES_CTR AES_CTR_MECH_INFO_TYPE 185*10500SHai-May.Chao@Sun.COM 186*10500SHai-May.Chao@Sun.COM typedef struct soft_aes_ctx { 187*10500SHai-May.Chao@Sun.COM void *key_sched; /* pointer to key schedule */ 188*10500SHai-May.Chao@Sun.COM size_t keysched_len; /* Length of the key schedule */ 189*10500SHai-May.Chao@Sun.COM uint8_t ivec[AES_BLOCK_LEN]; /* initialization vector */ 190*10500SHai-May.Chao@Sun.COM uint8_t data[AES_BLOCK_LEN]; /* for use by update */ 191*10500SHai-May.Chao@Sun.COM size_t remain_len; /* for use by update */ 192*10500SHai-May.Chao@Sun.COM void *aes_cbc; /* to be used by CBC mode */ 193*10500SHai-May.Chao@Sun.COM } soft_aes_ctx_t; 194*10500SHai-May.Chao@Sun.COM #endif 195*10500SHai-May.Chao@Sun.COM 196*10500SHai-May.Chao@Sun.COM /* AES FIPS functions */ 197*10500SHai-May.Chao@Sun.COM extern int fips_aes_post(int); 198*10500SHai-May.Chao@Sun.COM 199*10500SHai-May.Chao@Sun.COM #ifdef _AES_IMPL 200*10500SHai-May.Chao@Sun.COM #ifndef _KERNEL 201*10500SHai-May.Chao@Sun.COM struct soft_aes_ctx; 202*10500SHai-May.Chao@Sun.COM extern void fips_aes_free_context(struct soft_aes_ctx *); 203*10500SHai-May.Chao@Sun.COM extern struct soft_aes_ctx *fips_aes_build_context(uint8_t *, int, 204*10500SHai-May.Chao@Sun.COM uint8_t *, CK_MECHANISM_TYPE); 205*10500SHai-May.Chao@Sun.COM extern CK_RV fips_aes_encrypt(struct soft_aes_ctx *, CK_BYTE_PTR, 206*10500SHai-May.Chao@Sun.COM CK_ULONG, CK_BYTE_PTR, CK_ULONG_PTR, CK_MECHANISM_TYPE); 207*10500SHai-May.Chao@Sun.COM extern CK_RV fips_aes_decrypt(struct soft_aes_ctx *, CK_BYTE_PTR, 208*10500SHai-May.Chao@Sun.COM CK_ULONG, CK_BYTE_PTR, CK_ULONG_PTR, CK_MECHANISM_TYPE); 209*10500SHai-May.Chao@Sun.COM 210*10500SHai-May.Chao@Sun.COM #else 211*10500SHai-May.Chao@Sun.COM extern void fips_aes_free_context(soft_aes_ctx_t *); 212*10500SHai-May.Chao@Sun.COM extern void *aes_cbc_ctx_init(void *, size_t, uint8_t *); 213*10500SHai-May.Chao@Sun.COM extern soft_aes_ctx_t *fips_aes_build_context(uint8_t *, int, 214*10500SHai-May.Chao@Sun.COM uint8_t *, aes_mech_type_t, boolean_t); 215*10500SHai-May.Chao@Sun.COM extern int fips_aes_encrypt(soft_aes_ctx_t *, uchar_t *, 216*10500SHai-May.Chao@Sun.COM ulong_t, uchar_t *, ulong_t *, aes_mech_type_t); 217*10500SHai-May.Chao@Sun.COM extern int fips_aes_decrypt(soft_aes_ctx_t *, uchar_t *, 218*10500SHai-May.Chao@Sun.COM ulong_t, uchar_t *, ulong_t *, aes_mech_type_t); 219*10500SHai-May.Chao@Sun.COM 220*10500SHai-May.Chao@Sun.COM #endif /* _KERNEL */ 221*10500SHai-May.Chao@Sun.COM #endif /* _AES_IMPL */ 222*10500SHai-May.Chao@Sun.COM #endif /* _AES_FIPS_POST */ 223*10500SHai-May.Chao@Sun.COM 2240Sstevel@tonic-gate #ifdef __cplusplus 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate #endif 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate #endif /* _AES_IMPL_H */ 229