xref: /titanic_41/usr/src/common/crypto/aes/aes_impl.h (revision f5488aa822e08905cde61d596e965030a1dfffcd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * 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.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_AES_IMPL_H
27 #define	_AES_IMPL_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * Common definitions used by AES.
33  */
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #include <sys/types.h>
40 
41 /* 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)
44 
45 #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))
48 
49 #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]
66 
67 #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]
84 
85 /* AES key size definitions */
86 #define	AES_MINBITS		128
87 #define	AES_MINBYTES		((AES_MINBITS) >> 3)
88 #define	AES_MAXBITS		256
89 #define	AES_MAXBYTES		((AES_MAXBITS) >> 3)
90 
91 #define	AES_MIN_KEY_BYTES	((AES_MINBITS) >> 3)
92 #define	AES_MAX_KEY_BYTES	((AES_MAXBITS) >> 3)
93 #define	AES_192_KEY_BYTES	24
94 #define	AES_IV_LEN		16
95 
96 /* AES key schedule may be implemented with 32- or 64-bit elements: */
97 #define	AES_32BIT_KS		32
98 #define	AES_64BIT_KS		64
99 
100 #define	MAX_AES_NR		14 /* Maximum number of rounds */
101 #define	MAX_AES_NB		4  /* Number of columns comprising a state */
102 
103 typedef union {
104 #ifdef	sun4u
105 	uint64_t	ks64[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
106 #endif
107 	uint32_t	ks32[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
108 } aes_ks_t;
109 
110 typedef struct aes_key aes_key_t;
111 struct aes_key {
112 	int		nr;
113 	int		type;
114 	aes_ks_t	encr_ks;
115 	aes_ks_t	decr_ks;
116 };
117 
118 extern void aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct);
119 extern void aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt);
120 extern void aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits,
121 	void *keysched);
122 extern void *aes_alloc_keysched(size_t *size, int kmflag);
123 
124 #ifdef	__cplusplus
125 }
126 #endif
127 
128 #endif	/* _AES_IMPL_H */
129