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 /* 22*9392Sopensolaris@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 109*9392Sopensolaris@drydog.com /* aes_key.flags value: */ 110*9392Sopensolaris@drydog.com #define INTEL_AES_NI_CAPABLE 0x1 /* AES-NI instructions present */ 111*9392Sopensolaris@drydog.com 1120Sstevel@tonic-gate typedef struct aes_key aes_key_t; 1130Sstevel@tonic-gate struct aes_key { 114*9392Sopensolaris@drydog.com aes_ks_t encr_ks; /* encryption key schedule */ 115*9392Sopensolaris@drydog.com aes_ks_t decr_ks; /* decryption key schedule */ 116*9392Sopensolaris@drydog.com #ifdef __amd64 117*9392Sopensolaris@drydog.com long double align128; /* Align fields above for Intel AES-NI */ 118*9392Sopensolaris@drydog.com int flags; /* implementation-dependent flags */ 119*9392Sopensolaris@drydog.com #endif /* __amd64 */ 120*9392Sopensolaris@drydog.com int nr; /* number of rounds (10, 12, or 14) */ 121*9392Sopensolaris@drydog.com int type; /* key schedule size (32 or 64 bits) */ 1220Sstevel@tonic-gate }; 1230Sstevel@tonic-gate 124*9392Sopensolaris@drydog.com /* 125*9392Sopensolaris@drydog.com * Core AES functions. 126*9392Sopensolaris@drydog.com * ks and keysched are pointers to aes_key_t. 127*9392Sopensolaris@drydog.com * They are declared void* as they are intended to be opaque types. 128*9392Sopensolaris@drydog.com * Use function aes_alloc_keysched() to allocate memory for ks and keysched. 129*9392Sopensolaris@drydog.com */ 130*9392Sopensolaris@drydog.com extern void *aes_alloc_keysched(size_t *size, int kmflag); 131*9392Sopensolaris@drydog.com extern void aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits, 132*9392Sopensolaris@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); 135*9392Sopensolaris@drydog.com 136*9392Sopensolaris@drydog.com /* 137*9392Sopensolaris@drydog.com * AES mode functions. 138*9392Sopensolaris@drydog.com * The first 2 functions operate on 16-byte AES blocks. 139*9392Sopensolaris@drydog.com */ 140*9392Sopensolaris@drydog.com extern void aes_copy_block(uint8_t *in, uint8_t *out); 141*9392Sopensolaris@drydog.com extern void aes_xor_block(uint8_t *data, uint8_t *dst); 142*9392Sopensolaris@drydog.com 143*9392Sopensolaris@drydog.com /* Note: ctx is a pointer to aes_ctx_t defined in modes.h */ 144*9392Sopensolaris@drydog.com extern int aes_encrypt_contiguous_blocks(void *ctx, char *data, size_t length, 145*9392Sopensolaris@drydog.com crypto_data_t *out); 146*9392Sopensolaris@drydog.com extern int aes_decrypt_contiguous_blocks(void *ctx, char *data, size_t length, 147*9392Sopensolaris@drydog.com crypto_data_t *out); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate #ifdef __cplusplus 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate #endif 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate #endif /* _AES_IMPL_H */ 154