17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5b6c3f786Sbubbva * Common Development and Distribution License (the "License"). 6b6c3f786Sbubbva * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 2254034eb2SDan OpenSolaris Anderson * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #ifndef _AES_IMPL_H 277c478bd9Sstevel@tonic-gate #define _AES_IMPL_H 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Common definitions used by AES. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #ifdef __cplusplus 347c478bd9Sstevel@tonic-gate extern "C" { 357c478bd9Sstevel@tonic-gate #endif 367c478bd9Sstevel@tonic-gate 3790bcde94Sda73024 #include <sys/types.h> 3823c57df7Smcpowers #include <sys/crypto/common.h> 3990bcde94Sda73024 4090bcde94Sda73024 /* Similar to sysmacros.h IS_P2ALIGNED, but checks two pointers: */ 4190bcde94Sda73024 #define IS_P2ALIGNED2(v, w, a) \ 4290bcde94Sda73024 ((((uintptr_t)(v) | (uintptr_t)(w)) & ((uintptr_t)(a) - 1)) == 0) 4390bcde94Sda73024 4490bcde94Sda73024 #define AES_BLOCK_LEN 16 /* bytes */ 4590bcde94Sda73024 /* Round constant length, in number of 32-bit elements: */ 4690bcde94Sda73024 #define RC_LENGTH (5 * ((AES_BLOCK_LEN) / 4 - 2)) 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #define AES_COPY_BLOCK(src, dst) \ 497c478bd9Sstevel@tonic-gate (dst)[0] = (src)[0]; \ 507c478bd9Sstevel@tonic-gate (dst)[1] = (src)[1]; \ 517c478bd9Sstevel@tonic-gate (dst)[2] = (src)[2]; \ 527c478bd9Sstevel@tonic-gate (dst)[3] = (src)[3]; \ 537c478bd9Sstevel@tonic-gate (dst)[4] = (src)[4]; \ 547c478bd9Sstevel@tonic-gate (dst)[5] = (src)[5]; \ 557c478bd9Sstevel@tonic-gate (dst)[6] = (src)[6]; \ 567c478bd9Sstevel@tonic-gate (dst)[7] = (src)[7]; \ 577c478bd9Sstevel@tonic-gate (dst)[8] = (src)[8]; \ 587c478bd9Sstevel@tonic-gate (dst)[9] = (src)[9]; \ 597c478bd9Sstevel@tonic-gate (dst)[10] = (src)[10]; \ 607c478bd9Sstevel@tonic-gate (dst)[11] = (src)[11]; \ 617c478bd9Sstevel@tonic-gate (dst)[12] = (src)[12]; \ 627c478bd9Sstevel@tonic-gate (dst)[13] = (src)[13]; \ 637c478bd9Sstevel@tonic-gate (dst)[14] = (src)[14]; \ 647c478bd9Sstevel@tonic-gate (dst)[15] = (src)[15] 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #define AES_XOR_BLOCK(src, dst) \ 677c478bd9Sstevel@tonic-gate (dst)[0] ^= (src)[0]; \ 687c478bd9Sstevel@tonic-gate (dst)[1] ^= (src)[1]; \ 697c478bd9Sstevel@tonic-gate (dst)[2] ^= (src)[2]; \ 707c478bd9Sstevel@tonic-gate (dst)[3] ^= (src)[3]; \ 717c478bd9Sstevel@tonic-gate (dst)[4] ^= (src)[4]; \ 727c478bd9Sstevel@tonic-gate (dst)[5] ^= (src)[5]; \ 737c478bd9Sstevel@tonic-gate (dst)[6] ^= (src)[6]; \ 747c478bd9Sstevel@tonic-gate (dst)[7] ^= (src)[7]; \ 757c478bd9Sstevel@tonic-gate (dst)[8] ^= (src)[8]; \ 767c478bd9Sstevel@tonic-gate (dst)[9] ^= (src)[9]; \ 777c478bd9Sstevel@tonic-gate (dst)[10] ^= (src)[10]; \ 787c478bd9Sstevel@tonic-gate (dst)[11] ^= (src)[11]; \ 797c478bd9Sstevel@tonic-gate (dst)[12] ^= (src)[12]; \ 807c478bd9Sstevel@tonic-gate (dst)[13] ^= (src)[13]; \ 817c478bd9Sstevel@tonic-gate (dst)[14] ^= (src)[14]; \ 827c478bd9Sstevel@tonic-gate (dst)[15] ^= (src)[15] 837c478bd9Sstevel@tonic-gate 8490bcde94Sda73024 /* AES key size definitions */ 857c478bd9Sstevel@tonic-gate #define AES_MINBITS 128 8690bcde94Sda73024 #define AES_MINBYTES ((AES_MINBITS) >> 3) 877c478bd9Sstevel@tonic-gate #define AES_MAXBITS 256 8890bcde94Sda73024 #define AES_MAXBYTES ((AES_MAXBITS) >> 3) 897c478bd9Sstevel@tonic-gate 9090bcde94Sda73024 #define AES_MIN_KEY_BYTES ((AES_MINBITS) >> 3) 9190bcde94Sda73024 #define AES_MAX_KEY_BYTES ((AES_MAXBITS) >> 3) 927c478bd9Sstevel@tonic-gate #define AES_192_KEY_BYTES 24 937c478bd9Sstevel@tonic-gate #define AES_IV_LEN 16 947c478bd9Sstevel@tonic-gate 9590bcde94Sda73024 /* AES key schedule may be implemented with 32- or 64-bit elements: */ 967c478bd9Sstevel@tonic-gate #define AES_32BIT_KS 32 977c478bd9Sstevel@tonic-gate #define AES_64BIT_KS 64 987c478bd9Sstevel@tonic-gate 9990bcde94Sda73024 #define MAX_AES_NR 14 /* Maximum number of rounds */ 10090bcde94Sda73024 #define MAX_AES_NB 4 /* Number of columns comprising a state */ 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate typedef union { 10390bcde94Sda73024 #ifdef sun4u 10490bcde94Sda73024 uint64_t ks64[((MAX_AES_NR) + 1) * (MAX_AES_NB)]; 10590bcde94Sda73024 #endif 10690bcde94Sda73024 uint32_t ks32[((MAX_AES_NR) + 1) * (MAX_AES_NB)]; 1077c478bd9Sstevel@tonic-gate } aes_ks_t; 1087c478bd9Sstevel@tonic-gate 10954034eb2SDan OpenSolaris Anderson /* aes_key.flags value: */ 11054034eb2SDan OpenSolaris Anderson #define INTEL_AES_NI_CAPABLE 0x1 /* AES-NI instructions present */ 11154034eb2SDan OpenSolaris Anderson 1127c478bd9Sstevel@tonic-gate typedef struct aes_key aes_key_t; 1137c478bd9Sstevel@tonic-gate struct aes_key { 11454034eb2SDan OpenSolaris Anderson aes_ks_t encr_ks; /* encryption key schedule */ 11554034eb2SDan OpenSolaris Anderson aes_ks_t decr_ks; /* decryption key schedule */ 11654034eb2SDan OpenSolaris Anderson #ifdef __amd64 11754034eb2SDan OpenSolaris Anderson long double align128; /* Align fields above for Intel AES-NI */ 11854034eb2SDan OpenSolaris Anderson int flags; /* implementation-dependent flags */ 11954034eb2SDan OpenSolaris Anderson #endif /* __amd64 */ 12054034eb2SDan OpenSolaris Anderson int nr; /* number of rounds (10, 12, or 14) */ 12154034eb2SDan OpenSolaris Anderson int type; /* key schedule size (32 or 64 bits) */ 1227c478bd9Sstevel@tonic-gate }; 1237c478bd9Sstevel@tonic-gate 12454034eb2SDan OpenSolaris Anderson /* 12554034eb2SDan OpenSolaris Anderson * Core AES functions. 12654034eb2SDan OpenSolaris Anderson * ks and keysched are pointers to aes_key_t. 12754034eb2SDan OpenSolaris Anderson * They are declared void* as they are intended to be opaque types. 12854034eb2SDan OpenSolaris Anderson * Use function aes_alloc_keysched() to allocate memory for ks and keysched. 12954034eb2SDan OpenSolaris Anderson */ 13054034eb2SDan OpenSolaris Anderson extern void *aes_alloc_keysched(size_t *size, int kmflag); 13190bcde94Sda73024 extern void aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits, 13290bcde94Sda73024 void *keysched); 13354034eb2SDan OpenSolaris Anderson extern int aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct); 13454034eb2SDan OpenSolaris Anderson extern int aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt); 13554034eb2SDan OpenSolaris Anderson 13654034eb2SDan OpenSolaris Anderson /* 13754034eb2SDan OpenSolaris Anderson * AES mode functions. 13854034eb2SDan OpenSolaris Anderson * The first 2 functions operate on 16-byte AES blocks. 13954034eb2SDan OpenSolaris Anderson */ 14054034eb2SDan OpenSolaris Anderson extern void aes_copy_block(uint8_t *in, uint8_t *out); 14154034eb2SDan OpenSolaris Anderson extern void aes_xor_block(uint8_t *data, uint8_t *dst); 14254034eb2SDan OpenSolaris Anderson 14354034eb2SDan OpenSolaris Anderson /* Note: ctx is a pointer to aes_ctx_t defined in modes.h */ 14454034eb2SDan OpenSolaris Anderson extern int aes_encrypt_contiguous_blocks(void *ctx, char *data, size_t length, 14554034eb2SDan OpenSolaris Anderson crypto_data_t *out); 14654034eb2SDan OpenSolaris Anderson extern int aes_decrypt_contiguous_blocks(void *ctx, char *data, size_t length, 14754034eb2SDan OpenSolaris Anderson crypto_data_t *out); 1487c478bd9Sstevel@tonic-gate 149b5a2d845SHai-May Chao /* 150b5a2d845SHai-May Chao * The following definitions and declarations are only used by AES FIPS POST 151b5a2d845SHai-May Chao */ 152*6ea3c060SGarrett D'Amore #ifdef _AES_IMPL 153b5a2d845SHai-May Chao 154b5a2d845SHai-May Chao #ifdef _KERNEL 155b5a2d845SHai-May Chao typedef enum aes_mech_type { 156b5a2d845SHai-May Chao AES_ECB_MECH_INFO_TYPE, /* SUN_CKM_AES_ECB */ 157b5a2d845SHai-May Chao AES_CBC_MECH_INFO_TYPE, /* SUN_CKM_AES_CBC */ 158b5a2d845SHai-May Chao AES_CBC_PAD_MECH_INFO_TYPE, /* SUN_CKM_AES_CBC_PAD */ 159b5a2d845SHai-May Chao AES_CTR_MECH_INFO_TYPE, /* SUN_CKM_AES_CTR */ 160b5a2d845SHai-May Chao AES_CCM_MECH_INFO_TYPE, /* SUN_CKM_AES_CCM */ 161b5a2d845SHai-May Chao AES_GCM_MECH_INFO_TYPE, /* SUN_CKM_AES_GCM */ 162b5a2d845SHai-May Chao AES_GMAC_MECH_INFO_TYPE /* SUN_CKM_AES_GMAC */ 163b5a2d845SHai-May Chao } aes_mech_type_t; 164b5a2d845SHai-May Chao 165b5a2d845SHai-May Chao #endif /* _KERNEL */ 166b5a2d845SHai-May Chao #endif /* _AES_IMPL */ 167b5a2d845SHai-May Chao 1687c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate #endif 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate #endif /* _AES_IMPL_H */ 173