xref: /illumos-gate/usr/src/common/crypto/aes/aes_impl.h (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2018, Joyent, Inc.
26  */
27 
28 #ifndef	_AES_IMPL_H
29 #define	_AES_IMPL_H
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 /* aes_key.flags value: */
112 #define	INTEL_AES_NI_CAPABLE	0x1	/* AES-NI instructions present */
113 
114 typedef struct aes_key aes_key_t;
115 struct aes_key {
116 	aes_ks_t	encr_ks;  /* encryption key schedule */
117 	aes_ks_t	decr_ks;  /* decryption key schedule */
118 #ifdef __amd64
119 	long double	align128; /* Align fields above for Intel AES-NI */
120 	int		flags;	  /* implementation-dependent flags */
121 #endif	/* __amd64 */
122 	int		nr;	  /* number of rounds (10, 12, or 14) */
123 	int		type;	  /* key schedule size (32 or 64 bits) */
124 };
125 
126 /*
127  * Core AES functions.
128  * ks and keysched are pointers to aes_key_t.
129  * They are declared void* as they are intended to be opaque types.
130  * Use function aes_alloc_keysched() to allocate memory for ks and keysched.
131  */
132 extern void *aes_alloc_keysched(size_t *size, int kmflag);
133 extern void aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits,
134 	void *keysched);
135 extern int aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct);
136 extern int aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt);
137 
138 /*
139  * AES mode functions.
140  * The first 3 functions operate on 16-byte AES blocks.
141  */
142 extern void aes_copy_block(uint8_t *in, uint8_t *out);
143 extern void aes_copy_block64(uint8_t *in, uint64_t *out);
144 extern void aes_xor_block(uint8_t *data, uint8_t *dst);
145 
146 /* Note: ctx is a pointer to aes_ctx_t defined in modes.h */
147 extern int aes_encrypt_contiguous_blocks(void *ctx, char *data, size_t length,
148     crypto_data_t *out);
149 extern int aes_decrypt_contiguous_blocks(void *ctx, char *data, size_t length,
150     crypto_data_t *out);
151 
152 /*
153  * The following definitions and declarations are only used by AES FIPS POST
154  */
155 #ifdef _AES_IMPL
156 
157 #ifdef _KERNEL
158 typedef enum aes_mech_type {
159 	AES_ECB_MECH_INFO_TYPE,		/* SUN_CKM_AES_ECB */
160 	AES_CBC_MECH_INFO_TYPE,		/* SUN_CKM_AES_CBC */
161 	AES_CBC_PAD_MECH_INFO_TYPE,	/* SUN_CKM_AES_CBC_PAD */
162 	AES_CTR_MECH_INFO_TYPE,		/* SUN_CKM_AES_CTR */
163 	AES_CCM_MECH_INFO_TYPE,		/* SUN_CKM_AES_CCM */
164 	AES_GCM_MECH_INFO_TYPE,		/* SUN_CKM_AES_GCM */
165 	AES_GMAC_MECH_INFO_TYPE,	/* SUN_CKM_AES_GMAC */
166 	AES_CMAC_MECH_INFO_TYPE		/* SUN_CKM_AES_CMAC */
167 } aes_mech_type_t;
168 
169 #endif	/* _KERNEL */
170 #endif /* _AES_IMPL */
171 
172 #ifdef	__cplusplus
173 }
174 #endif
175 
176 #endif	/* _AES_IMPL_H */
177