xref: /illumos-gate/usr/src/common/crypto/aes/aes_impl.h (revision a73c0fe4e90b82a478f821ef3adb5cf34f6a9346)
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 #include <sys/crypto/common.h>
41 
42 /* Similar to sysmacros.h IS_P2ALIGNED, but checks two pointers: */
43 #define	IS_P2ALIGNED2(v, w, a) \
44 	((((uintptr_t)(v) | (uintptr_t)(w)) & ((uintptr_t)(a) - 1)) == 0)
45 
46 #define	AES_BLOCK_LEN	16	/* bytes */
47 /* Round constant length, in number of 32-bit elements: */
48 #define	RC_LENGTH	(5 * ((AES_BLOCK_LEN) / 4 - 2))
49 
50 #define	AES_COPY_BLOCK(src, dst) \
51 	(dst)[0] = (src)[0]; \
52 	(dst)[1] = (src)[1]; \
53 	(dst)[2] = (src)[2]; \
54 	(dst)[3] = (src)[3]; \
55 	(dst)[4] = (src)[4]; \
56 	(dst)[5] = (src)[5]; \
57 	(dst)[6] = (src)[6]; \
58 	(dst)[7] = (src)[7]; \
59 	(dst)[8] = (src)[8]; \
60 	(dst)[9] = (src)[9]; \
61 	(dst)[10] = (src)[10]; \
62 	(dst)[11] = (src)[11]; \
63 	(dst)[12] = (src)[12]; \
64 	(dst)[13] = (src)[13]; \
65 	(dst)[14] = (src)[14]; \
66 	(dst)[15] = (src)[15]
67 
68 #define	AES_XOR_BLOCK(src, dst) \
69 	(dst)[0] ^= (src)[0]; \
70 	(dst)[1] ^= (src)[1]; \
71 	(dst)[2] ^= (src)[2]; \
72 	(dst)[3] ^= (src)[3]; \
73 	(dst)[4] ^= (src)[4]; \
74 	(dst)[5] ^= (src)[5]; \
75 	(dst)[6] ^= (src)[6]; \
76 	(dst)[7] ^= (src)[7]; \
77 	(dst)[8] ^= (src)[8]; \
78 	(dst)[9] ^= (src)[9]; \
79 	(dst)[10] ^= (src)[10]; \
80 	(dst)[11] ^= (src)[11]; \
81 	(dst)[12] ^= (src)[12]; \
82 	(dst)[13] ^= (src)[13]; \
83 	(dst)[14] ^= (src)[14]; \
84 	(dst)[15] ^= (src)[15]
85 
86 /* AES key size definitions */
87 #define	AES_MINBITS		128
88 #define	AES_MINBYTES		((AES_MINBITS) >> 3)
89 #define	AES_MAXBITS		256
90 #define	AES_MAXBYTES		((AES_MAXBITS) >> 3)
91 
92 #define	AES_MIN_KEY_BYTES	((AES_MINBITS) >> 3)
93 #define	AES_MAX_KEY_BYTES	((AES_MAXBITS) >> 3)
94 #define	AES_192_KEY_BYTES	24
95 #define	AES_IV_LEN		16
96 
97 /* AES key schedule may be implemented with 32- or 64-bit elements: */
98 #define	AES_32BIT_KS		32
99 #define	AES_64BIT_KS		64
100 
101 #define	MAX_AES_NR		14 /* Maximum number of rounds */
102 #define	MAX_AES_NB		4  /* Number of columns comprising a state */
103 
104 typedef union {
105 #ifdef	sun4u
106 	uint64_t	ks64[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
107 #endif
108 	uint32_t	ks32[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
109 } aes_ks_t;
110 
111 typedef struct aes_key aes_key_t;
112 struct aes_key {
113 	int		nr;
114 	int		type;
115 	aes_ks_t	encr_ks;
116 	aes_ks_t	decr_ks;
117 };
118 
119 extern int aes_encrypt_contiguous_blocks(void *, char *, size_t,
120     crypto_data_t *);
121 extern int aes_decrypt_contiguous_blocks(void *, char *, size_t,
122     crypto_data_t *);
123 extern int aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct);
124 extern int aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt);
125 extern void aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits,
126 	void *keysched);
127 extern void *aes_alloc_keysched(size_t *size, int kmflag);
128 extern void aes_copy_block(uint8_t *, uint8_t *);
129 extern void aes_xor_block(uint8_t *, uint8_t *);
130 
131 #ifdef	__cplusplus
132 }
133 #endif
134 
135 #endif	/* _AES_IMPL_H */
136