xref: /onnv-gate/usr/src/common/crypto/aes/aes_impl.h (revision 6877:4caf76f7d912)
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 /*
226125Sbubbva  * Copyright 2008 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Common definitions used by AES.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #ifdef	__cplusplus
360Sstevel@tonic-gate extern "C" {
370Sstevel@tonic-gate #endif
380Sstevel@tonic-gate 
39*6877Sda73024 #include <sys/types.h>
40*6877Sda73024 
41*6877Sda73024 /* Similar to sysmacros.h IS_P2ALIGNED, but checks two pointers: */
42*6877Sda73024 #define	IS_P2ALIGNED2(v, w, a) \
43*6877Sda73024 	((((uintptr_t)(v) | (uintptr_t)(w)) & ((uintptr_t)(a) - 1)) == 0)
44*6877Sda73024 
45*6877Sda73024 #define	AES_BLOCK_LEN	16	/* bytes */
46*6877Sda73024 /* Round constant length, in number of 32-bit elements: */
47*6877Sda73024 #define	RC_LENGTH	(5 * ((AES_BLOCK_LEN) / 4 - 2))
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #define	AES_COPY_BLOCK(src, dst) \
500Sstevel@tonic-gate 	(dst)[0] = (src)[0]; \
510Sstevel@tonic-gate 	(dst)[1] = (src)[1]; \
520Sstevel@tonic-gate 	(dst)[2] = (src)[2]; \
530Sstevel@tonic-gate 	(dst)[3] = (src)[3]; \
540Sstevel@tonic-gate 	(dst)[4] = (src)[4]; \
550Sstevel@tonic-gate 	(dst)[5] = (src)[5]; \
560Sstevel@tonic-gate 	(dst)[6] = (src)[6]; \
570Sstevel@tonic-gate 	(dst)[7] = (src)[7]; \
580Sstevel@tonic-gate 	(dst)[8] = (src)[8]; \
590Sstevel@tonic-gate 	(dst)[9] = (src)[9]; \
600Sstevel@tonic-gate 	(dst)[10] = (src)[10]; \
610Sstevel@tonic-gate 	(dst)[11] = (src)[11]; \
620Sstevel@tonic-gate 	(dst)[12] = (src)[12]; \
630Sstevel@tonic-gate 	(dst)[13] = (src)[13]; \
640Sstevel@tonic-gate 	(dst)[14] = (src)[14]; \
650Sstevel@tonic-gate 	(dst)[15] = (src)[15]
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	AES_XOR_BLOCK(src, dst) \
680Sstevel@tonic-gate 	(dst)[0] ^= (src)[0]; \
690Sstevel@tonic-gate 	(dst)[1] ^= (src)[1]; \
700Sstevel@tonic-gate 	(dst)[2] ^= (src)[2]; \
710Sstevel@tonic-gate 	(dst)[3] ^= (src)[3]; \
720Sstevel@tonic-gate 	(dst)[4] ^= (src)[4]; \
730Sstevel@tonic-gate 	(dst)[5] ^= (src)[5]; \
740Sstevel@tonic-gate 	(dst)[6] ^= (src)[6]; \
750Sstevel@tonic-gate 	(dst)[7] ^= (src)[7]; \
760Sstevel@tonic-gate 	(dst)[8] ^= (src)[8]; \
770Sstevel@tonic-gate 	(dst)[9] ^= (src)[9]; \
780Sstevel@tonic-gate 	(dst)[10] ^= (src)[10]; \
790Sstevel@tonic-gate 	(dst)[11] ^= (src)[11]; \
800Sstevel@tonic-gate 	(dst)[12] ^= (src)[12]; \
810Sstevel@tonic-gate 	(dst)[13] ^= (src)[13]; \
820Sstevel@tonic-gate 	(dst)[14] ^= (src)[14]; \
830Sstevel@tonic-gate 	(dst)[15] ^= (src)[15]
840Sstevel@tonic-gate 
85*6877Sda73024 /* AES key size definitions */
860Sstevel@tonic-gate #define	AES_MINBITS		128
87*6877Sda73024 #define	AES_MINBYTES		((AES_MINBITS) >> 3)
880Sstevel@tonic-gate #define	AES_MAXBITS		256
89*6877Sda73024 #define	AES_MAXBYTES		((AES_MAXBITS) >> 3)
900Sstevel@tonic-gate 
91*6877Sda73024 #define	AES_MIN_KEY_BYTES	((AES_MINBITS) >> 3)
92*6877Sda73024 #define	AES_MAX_KEY_BYTES	((AES_MAXBITS) >> 3)
930Sstevel@tonic-gate #define	AES_192_KEY_BYTES	24
940Sstevel@tonic-gate #define	AES_IV_LEN		16
950Sstevel@tonic-gate 
96*6877Sda73024 /* AES key schedule may be implemented with 32- or 64-bit elements: */
970Sstevel@tonic-gate #define	AES_32BIT_KS		32
980Sstevel@tonic-gate #define	AES_64BIT_KS		64
990Sstevel@tonic-gate 
100*6877Sda73024 #define	MAX_AES_NR		14 /* Maximum number of rounds */
101*6877Sda73024 #define	MAX_AES_NB		4  /* Number of columns comprising a state */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate typedef union {
104*6877Sda73024 #ifdef	sun4u
105*6877Sda73024 	uint64_t	ks64[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
106*6877Sda73024 #endif
107*6877Sda73024 	uint32_t	ks32[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
1080Sstevel@tonic-gate } aes_ks_t;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate typedef struct aes_key aes_key_t;
1110Sstevel@tonic-gate struct aes_key {
1120Sstevel@tonic-gate 	int		nr;
1130Sstevel@tonic-gate 	int		type;
1140Sstevel@tonic-gate 	aes_ks_t	encr_ks;
1150Sstevel@tonic-gate 	aes_ks_t	decr_ks;
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate 
118*6877Sda73024 extern void aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct);
119*6877Sda73024 extern void aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt);
120*6877Sda73024 extern void aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits,
121*6877Sda73024 	void *keysched);
122*6877Sda73024 extern void *aes_alloc_keysched(size_t *size, int kmflag);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #ifdef	__cplusplus
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate #endif
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate #endif	/* _AES_IMPL_H */
129