xref: /titanic_50/usr/src/uts/common/sys/crypto/common.h (revision d51e90740114c60620c0febffd4d3ce6e280a107)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_CRYPTO_COMMON_H
27 #define	_SYS_CRYPTO_COMMON_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * Header file for the common data structures of the cryptographic framework
33  */
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #include <sys/types.h>
40 #include <sys/uio.h>
41 #include <sys/stream.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 
45 
46 /* Cryptographic Mechanisms */
47 
48 #define	CRYPTO_MAX_MECH_NAME 32
49 typedef char crypto_mech_name_t[CRYPTO_MAX_MECH_NAME];
50 
51 typedef uint64_t crypto_mech_type_t;
52 
53 typedef struct crypto_mechanism {
54 	crypto_mech_type_t	cm_type;	/* mechanism type */
55 	caddr_t			cm_param;	/* mech. parameter */
56 	size_t			cm_param_len;	/* mech. parameter len */
57 } crypto_mechanism_t;
58 
59 #ifdef  _SYSCALL32
60 
61 typedef struct crypto_mechanism32 {
62 	crypto_mech_type_t	cm_type;	/* mechanism type */
63 	caddr32_t		cm_param;	/* mech. parameter */
64 	size32_t		cm_param_len;   /* mech. parameter len */
65 } crypto_mechanism32_t;
66 
67 #endif  /* _SYSCALL32 */
68 
69 /*
70  * The measurement unit flag for a mechanism's minimum or maximum key size.
71  * The unit are mechanism dependant.  It can be in bits or in bytes.
72  */
73 typedef uint32_t crypto_keysize_unit_t;
74 
75 #define	CRYPTO_KEYSIZE_UNIT_IN_BITS	0x00000001
76 #define	CRYPTO_KEYSIZE_UNIT_IN_BYTES	0x00000002
77 
78 
79 /* Mechanisms supported out-of-the-box */
80 #define	SUN_CKM_MD5			"CKM_MD5"
81 #define	SUN_CKM_MD5_HMAC		"CKM_MD5_HMAC"
82 #define	SUN_CKM_MD5_HMAC_GENERAL	"CKM_MD5_HMAC_GENERAL"
83 #define	SUN_CKM_SHA1			"CKM_SHA_1"
84 #define	SUN_CKM_SHA1_HMAC		"CKM_SHA_1_HMAC"
85 #define	SUN_CKM_SHA1_HMAC_GENERAL	"CKM_SHA_1_HMAC_GENERAL"
86 #define	SUN_CKM_SHA256			"CKM_SHA256"
87 #define	SUN_CKM_SHA256_HMAC		"CKM_SHA256_HMAC"
88 #define	SUN_CKM_SHA256_HMAC_GENERAL	"CKM_SHA256_HMAC_GENERAL"
89 #define	SUN_CKM_SHA384			"CKM_SHA384"
90 #define	SUN_CKM_SHA384_HMAC		"CKM_SHA384_HMAC"
91 #define	SUN_CKM_SHA384_HMAC_GENERAL	"CKM_SHA384_HMAC_GENERAL"
92 #define	SUN_CKM_SHA512			"CKM_SHA512"
93 #define	SUN_CKM_SHA512_HMAC		"CKM_SHA512_HMAC"
94 #define	SUN_CKM_SHA512_HMAC_GENERAL	"CKM_SHA512_HMAC_GENERAL"
95 #define	SUN_CKM_DES_CBC			"CKM_DES_CBC"
96 #define	SUN_CKM_DES3_CBC		"CKM_DES3_CBC"
97 #define	SUN_CKM_DES_ECB			"CKM_DES_ECB"
98 #define	SUN_CKM_DES3_ECB		"CKM_DES3_ECB"
99 #define	SUN_CKM_BLOWFISH_CBC		"CKM_BLOWFISH_CBC"
100 #define	SUN_CKM_BLOWFISH_ECB		"CKM_BLOWFISH_ECB"
101 #define	SUN_CKM_AES_CBC			"CKM_AES_CBC"
102 #define	SUN_CKM_AES_ECB			"CKM_AES_ECB"
103 #define	SUN_CKM_AES_CTR			"CKM_AES_CTR"
104 #define	SUN_CKM_RC4			"CKM_RC4"
105 #define	SUN_CKM_RSA_PKCS		"CKM_RSA_PKCS"
106 #define	SUN_CKM_RSA_X_509		"CKM_RSA_X_509"
107 #define	SUN_CKM_MD5_RSA_PKCS		"CKM_MD5_RSA_PKCS"
108 #define	SUN_CKM_SHA1_RSA_PKCS		"CKM_SHA1_RSA_PKCS"
109 #define	SUN_CKM_SHA256_RSA_PKCS		"CKM_SHA256_RSA_PKCS"
110 #define	SUN_CKM_SHA384_RSA_PKCS		"CKM_SHA384_RSA_PKCS"
111 #define	SUN_CKM_SHA512_RSA_PKCS		"CKM_SHA512_RSA_PKCS"
112 
113 
114 /* Data arguments of cryptographic operations */
115 
116 typedef enum crypto_data_format {
117 	CRYPTO_DATA_RAW = 1,
118 	CRYPTO_DATA_UIO,
119 	CRYPTO_DATA_MBLK
120 } crypto_data_format_t;
121 
122 typedef struct crypto_data {
123 	crypto_data_format_t	cd_format;	/* Format identifier	*/
124 	off_t			cd_offset;	/* Offset from the beginning */
125 	size_t			cd_length;	/* # of bytes in use */
126 	caddr_t			cd_miscdata;	/* ancillary data */
127 	union {
128 		/* Raw format */
129 		iovec_t cdu_raw;		/* Pointer and length	    */
130 
131 		/* uio scatter-gather format */
132 		uio_t	*cdu_uio;
133 
134 		/* mblk scatter-gather format */
135 		mblk_t	*cdu_mp;		/* The mblk chain */
136 
137 	} cdu;	/* Crypto Data Union */
138 } crypto_data_t;
139 
140 #define	cd_raw		cdu.cdu_raw
141 #define	cd_uio		cdu.cdu_uio
142 #define	cd_mp		cdu.cdu_mp
143 
144 typedef struct crypto_dual_data {
145 	crypto_data_t		dd_data;	/* The data */
146 	off_t			dd_offset2;	/* Used by dual operation */
147 	size_t			dd_len2;	/* # of bytes to take	*/
148 } crypto_dual_data_t;
149 
150 #define	dd_format	dd_data.cd_format
151 #define	dd_offset1	dd_data.cd_offset
152 #define	dd_len1		dd_data.cd_length
153 #define	dd_miscdata	dd_data.cd_miscdata
154 #define	dd_raw		dd_data.cd_raw
155 #define	dd_uio		dd_data.cd_uio
156 #define	dd_mp		dd_data.cd_mp
157 
158 /* The keys, and their contents */
159 
160 typedef enum {
161 	CRYPTO_KEY_RAW = 1,	/* ck_data is a cleartext key */
162 	CRYPTO_KEY_REFERENCE,	/* ck_obj_id is an opaque reference */
163 	CRYPTO_KEY_ATTR_LIST	/* ck_attrs is a list of object attributes */
164 } crypto_key_format_t;
165 
166 typedef uint64_t crypto_attr_type_t;
167 
168 /* Attribute types to use for passing a RSA public key or a private key. */
169 #define	SUN_CKA_MODULUS			0x00000120
170 #define	SUN_CKA_MODULUS_BITS		0x00000121
171 #define	SUN_CKA_PUBLIC_EXPONENT		0x00000122
172 #define	SUN_CKA_PRIVATE_EXPONENT	0x00000123
173 #define	SUN_CKA_PRIME_1			0x00000124
174 #define	SUN_CKA_PRIME_2			0x00000125
175 #define	SUN_CKA_EXPONENT_1		0x00000126
176 #define	SUN_CKA_EXPONENT_2		0x00000127
177 #define	SUN_CKA_COEFFICIENT		0x00000128
178 #define	SUN_CKA_PRIME			0x00000130
179 #define	SUN_CKA_SUBPRIME		0x00000131
180 #define	SUN_CKA_BASE			0x00000132
181 
182 typedef uint32_t	crypto_object_id_t;
183 
184 typedef struct crypto_object_attribute {
185 	crypto_attr_type_t	oa_type;	/* attribute type */
186 	caddr_t			oa_value;	/* attribute value */
187 	ssize_t			oa_value_len;	/* length of attribute value */
188 } crypto_object_attribute_t;
189 
190 typedef struct crypto_key {
191 	crypto_key_format_t	ck_format;	/* format identifier */
192 	union {
193 		/* for CRYPTO_KEY_RAW ck_format */
194 		struct {
195 			uint_t	cku_v_length;	/* # of bits in ck_data   */
196 			void	*cku_v_data;	/* ptr to key value */
197 		} cku_key_value;
198 
199 		/* for CRYPTO_KEY_REFERENCE ck_format */
200 		crypto_object_id_t cku_key_id;	/* reference to object key */
201 
202 		/* for CRYPTO_KEY_ATTR_LIST ck_format */
203 		struct {
204 			uint_t cku_a_count;	/* number of attributes */
205 			crypto_object_attribute_t *cku_a_oattr;
206 		} cku_key_attrs;
207 	} cku_data;				/* Crypto Key union */
208 } crypto_key_t;
209 
210 #ifdef  _SYSCALL32
211 
212 typedef struct crypto_object_attribute32 {
213 	uint64_t	oa_type;	/* attribute type */
214 	caddr32_t	oa_value;	/* attribute value */
215 	ssize32_t	oa_value_len;	/* length of attribute value */
216 } crypto_object_attribute32_t;
217 
218 typedef struct crypto_key32 {
219 	crypto_key_format_t	ck_format;	/* format identifier */
220 	union {
221 		/* for CRYPTO_KEY_RAW ck_format */
222 		struct {
223 			uint32_t cku_v_length;	/* # of bytes in ck_data */
224 			caddr32_t cku_v_data;	/* ptr to key value */
225 		} cku_key_value;
226 
227 		/* for CRYPTO_KEY_REFERENCE ck_format */
228 		crypto_object_id_t cku_key_id; /* reference to object key */
229 
230 		/* for CRYPTO_KEY_ATTR_LIST ck_format */
231 		struct {
232 			uint32_t cku_a_count;	/* number of attributes */
233 			caddr32_t cku_a_oattr;
234 		} cku_key_attrs;
235 	} cku_data;				/* Crypto Key union */
236 } crypto_key32_t;
237 
238 #endif  /* _SYSCALL32 */
239 
240 #define	ck_data		cku_data.cku_key_value.cku_v_data
241 #define	ck_length	cku_data.cku_key_value.cku_v_length
242 #define	ck_obj_id	cku_data.cku_key_id
243 #define	ck_count	cku_data.cku_key_attrs.cku_a_count
244 #define	ck_attrs	cku_data.cku_key_attrs.cku_a_oattr
245 
246 /*
247  * Raw key lengths are expressed in number of bits.
248  * The following macro returns the minimum number of
249  * bytes that can contain the specified number of bits.
250  */
251 #define	CRYPTO_BITS2BYTES(n) (((n) + 7) >> 3)
252 
253 /* Providers */
254 
255 typedef enum {
256 	CRYPTO_HW_PROVIDER = 0,
257 	CRYPTO_SW_PROVIDER,
258 	CRYPTO_LOGICAL_PROVIDER
259 } crypto_provider_type_t;
260 
261 typedef uint32_t 	crypto_provider_id_t;
262 #define	KCF_PROVID_INVALID	((uint32_t)-1)
263 
264 typedef struct crypto_provider_entry {
265 	crypto_provider_id_t	pe_provider_id;
266 	uint_t			pe_mechanism_count;
267 } crypto_provider_entry_t;
268 
269 typedef struct crypto_dev_list_entry {
270 	char			le_dev_name[MAXNAMELEN];
271 	uint_t			le_dev_instance;
272 	uint_t			le_mechanism_count;
273 } crypto_dev_list_entry_t;
274 
275 /* User type for authentication ioctls and SPI entry points */
276 
277 typedef enum crypto_user_type {
278 	CRYPTO_SO = 0,
279 	CRYPTO_USER
280 } crypto_user_type_t;
281 
282 /* Version for provider management ioctls and SPI entry points */
283 
284 typedef struct crypto_version {
285 	uchar_t	cv_major;
286 	uchar_t	cv_minor;
287 } crypto_version_t;
288 
289 /* session data structure opaque to the consumer */
290 typedef void *crypto_session_t;
291 
292 /* provider data structure opaque to the consumer */
293 typedef void *crypto_provider_t;
294 
295 /* Limits used by both consumers and providers */
296 #define	CRYPTO_EXT_SIZE_LABEL		32
297 #define	CRYPTO_EXT_SIZE_MANUF		32
298 #define	CRYPTO_EXT_SIZE_MODEL		16
299 #define	CRYPTO_EXT_SIZE_SERIAL		16
300 #define	CRYPTO_EXT_SIZE_TIME		16
301 
302 typedef struct crypto_provider_ext_info {
303 	uchar_t			ei_label[CRYPTO_EXT_SIZE_LABEL];
304 	uchar_t			ei_manufacturerID[CRYPTO_EXT_SIZE_MANUF];
305 	uchar_t			ei_model[CRYPTO_EXT_SIZE_MODEL];
306 	uchar_t			ei_serial_number[CRYPTO_EXT_SIZE_SERIAL];
307 	ulong_t			ei_flags;
308 	ulong_t			ei_max_session_count;
309 	ulong_t			ei_max_pin_len;
310 	ulong_t			ei_min_pin_len;
311 	ulong_t			ei_total_public_memory;
312 	ulong_t			ei_free_public_memory;
313 	ulong_t			ei_total_private_memory;
314 	ulong_t			ei_free_private_memory;
315 	crypto_version_t	ei_hardware_version;
316 	crypto_version_t	ei_firmware_version;
317 	uchar_t			ei_time[CRYPTO_EXT_SIZE_TIME];
318 } crypto_provider_ext_info_t;
319 
320 typedef uint_t		crypto_session_id_t;
321 
322 /*
323  * Common cryptographic status and error codes.
324  */
325 #define	CRYPTO_SUCCESS				0x00000000
326 #define	CRYPTO_CANCEL				0x00000001
327 #define	CRYPTO_HOST_MEMORY			0x00000002
328 #define	CRYPTO_GENERAL_ERROR			0x00000003
329 #define	CRYPTO_FAILED				0x00000004
330 #define	CRYPTO_ARGUMENTS_BAD			0x00000005
331 #define	CRYPTO_ATTRIBUTE_READ_ONLY		0x00000006
332 #define	CRYPTO_ATTRIBUTE_SENSITIVE		0x00000007
333 #define	CRYPTO_ATTRIBUTE_TYPE_INVALID		0x00000008
334 #define	CRYPTO_ATTRIBUTE_VALUE_INVALID		0x00000009
335 #define	CRYPTO_CANCELED				0x0000000A
336 #define	CRYPTO_DATA_INVALID			0x0000000B
337 #define	CRYPTO_DATA_LEN_RANGE			0x0000000C
338 #define	CRYPTO_DEVICE_ERROR			0x0000000D
339 #define	CRYPTO_DEVICE_MEMORY			0x0000000E
340 #define	CRYPTO_DEVICE_REMOVED			0x0000000F
341 #define	CRYPTO_ENCRYPTED_DATA_INVALID		0x00000010
342 #define	CRYPTO_ENCRYPTED_DATA_LEN_RANGE		0x00000011
343 #define	CRYPTO_KEY_HANDLE_INVALID		0x00000012
344 #define	CRYPTO_KEY_SIZE_RANGE			0x00000013
345 #define	CRYPTO_KEY_TYPE_INCONSISTENT		0x00000014
346 #define	CRYPTO_KEY_NOT_NEEDED			0x00000015
347 #define	CRYPTO_KEY_CHANGED			0x00000016
348 #define	CRYPTO_KEY_NEEDED			0x00000017
349 #define	CRYPTO_KEY_INDIGESTIBLE			0x00000018
350 #define	CRYPTO_KEY_FUNCTION_NOT_PERMITTED	0x00000019
351 #define	CRYPTO_KEY_NOT_WRAPPABLE		0x0000001A
352 #define	CRYPTO_KEY_UNEXTRACTABLE		0x0000001B
353 #define	CRYPTO_MECHANISM_INVALID		0x0000001C
354 #define	CRYPTO_MECHANISM_PARAM_INVALID		0x0000001D
355 #define	CRYPTO_OBJECT_HANDLE_INVALID		0x0000001E
356 #define	CRYPTO_OPERATION_IS_ACTIVE		0x0000001F
357 #define	CRYPTO_OPERATION_NOT_INITIALIZED	0x00000020
358 #define	CRYPTO_PIN_INCORRECT			0x00000021
359 #define	CRYPTO_PIN_INVALID			0x00000022
360 #define	CRYPTO_PIN_LEN_RANGE			0x00000023
361 #define	CRYPTO_PIN_EXPIRED			0x00000024
362 #define	CRYPTO_PIN_LOCKED			0x00000025
363 #define	CRYPTO_SESSION_CLOSED			0x00000026
364 #define	CRYPTO_SESSION_COUNT			0x00000027
365 #define	CRYPTO_SESSION_HANDLE_INVALID		0x00000028
366 #define	CRYPTO_SESSION_READ_ONLY		0x00000029
367 #define	CRYPTO_SESSION_EXISTS			0x0000002A
368 #define	CRYPTO_SESSION_READ_ONLY_EXISTS		0x0000002B
369 #define	CRYPTO_SESSION_READ_WRITE_SO_EXISTS	0x0000002C
370 #define	CRYPTO_SIGNATURE_INVALID		0x0000002D
371 #define	CRYPTO_SIGNATURE_LEN_RANGE		0x0000002E
372 #define	CRYPTO_TEMPLATE_INCOMPLETE		0x0000002F
373 #define	CRYPTO_TEMPLATE_INCONSISTENT		0x00000030
374 #define	CRYPTO_UNWRAPPING_KEY_HANDLE_INVALID	0x00000031
375 #define	CRYPTO_UNWRAPPING_KEY_SIZE_RANGE	0x00000032
376 #define	CRYPTO_UNWRAPPING_KEY_TYPE_INCONSISTENT	0x00000033
377 #define	CRYPTO_USER_ALREADY_LOGGED_IN		0x00000034
378 #define	CRYPTO_USER_NOT_LOGGED_IN		0x00000035
379 #define	CRYPTO_USER_PIN_NOT_INITIALIZED		0x00000036
380 #define	CRYPTO_USER_TYPE_INVALID		0x00000037
381 #define	CRYPTO_USER_ANOTHER_ALREADY_LOGGED_IN	0x00000038
382 #define	CRYPTO_USER_TOO_MANY_TYPES		0x00000039
383 #define	CRYPTO_WRAPPED_KEY_INVALID		0x0000003A
384 #define	CRYPTO_WRAPPED_KEY_LEN_RANGE		0x0000003B
385 #define	CRYPTO_WRAPPING_KEY_HANDLE_INVALID	0x0000003C
386 #define	CRYPTO_WRAPPING_KEY_SIZE_RANGE		0x0000003D
387 #define	CRYPTO_WRAPPING_KEY_TYPE_INCONSISTENT	0x0000003E
388 #define	CRYPTO_RANDOM_SEED_NOT_SUPPORTED	0x0000003F
389 #define	CRYPTO_RANDOM_NO_RNG			0x00000040
390 #define	CRYPTO_DOMAIN_PARAMS_INVALID		0x00000041
391 #define	CRYPTO_BUFFER_TOO_SMALL			0x00000042
392 #define	CRYPTO_INFORMATION_SENSITIVE		0x00000043
393 #define	CRYPTO_NOT_SUPPORTED			0x00000044
394 
395 #define	CRYPTO_QUEUED				0x00000045
396 #define	CRYPTO_BUFFER_TOO_BIG			0x00000046
397 #define	CRYPTO_INVALID_CONTEXT			0x00000047
398 #define	CRYPTO_INVALID_MAC			0x00000048
399 #define	CRYPTO_MECH_NOT_SUPPORTED		0x00000049
400 #define	CRYPTO_INCONSISTENT_ATTRIBUTE		0x0000004A
401 #define	CRYPTO_NO_PERMISSION			0x0000004B
402 #define	CRYPTO_INVALID_PROVIDER_ID		0x0000004C
403 #define	CRYPTO_VERSION_MISMATCH			0x0000004D
404 #define	CRYPTO_BUSY				0x0000004E
405 #define	CRYPTO_UNKNOWN_PROVIDER			0x0000004F
406 #define	CRYPTO_MODVERIFICATION_FAILED		0x00000050
407 #define	CRYPTO_OLD_CTX_TEMPLATE			0x00000051
408 #define	CRYPTO_WEAK_KEY				0x00000052
409 
410 /*
411  * Special values that can be used to indicate that information is unavailable
412  * or that there is not practical limit. These values can be used
413  * by fields of the SPI crypto_provider_ext_info(9S) structure.
414  * The value of CRYPTO_UNAVAILABLE_INFO should be the same as
415  * CK_UNAVAILABLE_INFO in the PKCS#11 spec.
416  */
417 #define	CRYPTO_UNAVAILABLE_INFO		((ulong_t)(-1))
418 #define	CRYPTO_EFFECTIVELY_INFINITE	0x0
419 
420 #ifdef __cplusplus
421 }
422 #endif
423 
424 #endif /* _SYS_CRYPTO_COMMON_H */
425