xref: /freebsd/sys/contrib/openzfs/include/sys/crypto/common.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 /*
26  * Copyright 2013 Saso Kiselkov.  All rights reserved.
27  */
28 
29 #ifndef _SYS_CRYPTO_COMMON_H
30 #define	_SYS_CRYPTO_COMMON_H
31 
32 /*
33  * Header file for the common data structures of the cryptographic framework
34  */
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #include <sys/zfs_context.h>
41 
42 /* Cryptographic Mechanisms */
43 
44 #define	CRYPTO_MAX_MECH_NAME 32
45 typedef char crypto_mech_name_t[CRYPTO_MAX_MECH_NAME];
46 
47 typedef uint64_t crypto_mech_type_t;
48 
49 typedef struct crypto_mechanism {
50 	crypto_mech_type_t	cm_type;	/* mechanism type */
51 	caddr_t			cm_param;	/* mech. parameter */
52 	size_t			cm_param_len;	/* mech. parameter len */
53 } crypto_mechanism_t;
54 
55 /* CK_AES_CCM_PARAMS provides parameters to the CKM_AES_CCM mechanism */
56 typedef struct CK_AES_CCM_PARAMS {
57 	ulong_t ulMACSize;
58 	ulong_t ulNonceSize;
59 	ulong_t ulAuthDataSize;
60 	ulong_t ulDataSize; /* used for plaintext or ciphertext */
61 	uchar_t *nonce;
62 	uchar_t *authData;
63 } CK_AES_CCM_PARAMS;
64 
65 /* CK_AES_GCM_PARAMS provides parameters to the CKM_AES_GCM mechanism */
66 typedef struct CK_AES_GCM_PARAMS {
67 	uchar_t *pIv;
68 	ulong_t ulIvLen;
69 	ulong_t ulIvBits;
70 	uchar_t *pAAD;
71 	ulong_t ulAADLen;
72 	ulong_t ulTagBits;
73 } CK_AES_GCM_PARAMS;
74 
75 /*
76  * The measurement unit bit flag for a mechanism's minimum or maximum key size.
77  * The unit are mechanism dependent.  It can be in bits or in bytes.
78  */
79 typedef uint32_t crypto_keysize_unit_t;
80 
81 
82 /* Mechanisms supported out-of-the-box */
83 #define	SUN_CKM_SHA512_HMAC		"CKM_SHA512_HMAC"
84 #define	SUN_CKM_AES_CCM			"CKM_AES_CCM"
85 #define	SUN_CKM_AES_GCM			"CKM_AES_GCM"
86 
87 /* Data arguments of cryptographic operations */
88 
89 typedef enum crypto_data_format {
90 	CRYPTO_DATA_RAW = 1,
91 	CRYPTO_DATA_UIO,
92 } crypto_data_format_t;
93 
94 typedef struct crypto_data {
95 	crypto_data_format_t	cd_format;	/* Format identifier	*/
96 	off_t			cd_offset;	/* Offset from the beginning */
97 	size_t			cd_length;	/* # of bytes in use */
98 	union {
99 		/* Raw format */
100 		iovec_t cd_raw;		/* Pointer and length	    */
101 
102 		/* uio scatter-gather format */
103 		zfs_uio_t	*cd_uio;
104 	};	/* Crypto Data Union */
105 } crypto_data_t;
106 
107 /* The keys, and their contents */
108 
109 typedef struct {
110 	uint_t	ck_length;	/* # of bits in ck_data   */
111 	void	*ck_data;	/* ptr to key value */
112 } crypto_key_t;
113 
114 /*
115  * Raw key lengths are expressed in number of bits.
116  * The following macro returns the minimum number of
117  * bytes that can contain the specified number of bits.
118  * Round up without overflowing the integer type.
119  */
120 #define	CRYPTO_BITS2BYTES(n) ((n) == 0 ? 0 : (((n) - 1) >> 3) + 1)
121 #define	CRYPTO_BYTES2BITS(n) ((n) << 3)
122 
123 /* Providers */
124 
125 typedef uint32_t 	crypto_provider_id_t;
126 #define	KCF_PROVID_INVALID	((uint32_t)-1)
127 
128 /* session data structure opaque to the consumer */
129 typedef void *crypto_session_t;
130 
131 #define	PROVIDER_OWNS_KEY_SCHEDULE	0x00000001
132 
133 /*
134  * Common cryptographic status and error codes.
135  */
136 #define	CRYPTO_SUCCESS				0x00000000
137 #define	CRYPTO_HOST_MEMORY			0x00000002
138 #define	CRYPTO_FAILED				0x00000004
139 #define	CRYPTO_ARGUMENTS_BAD			0x00000005
140 #define	CRYPTO_DATA_LEN_RANGE			0x0000000C
141 #define	CRYPTO_ENCRYPTED_DATA_LEN_RANGE		0x00000011
142 #define	CRYPTO_KEY_SIZE_RANGE			0x00000013
143 #define	CRYPTO_KEY_TYPE_INCONSISTENT		0x00000014
144 #define	CRYPTO_MECHANISM_INVALID		0x0000001C
145 #define	CRYPTO_MECHANISM_PARAM_INVALID		0x0000001D
146 #define	CRYPTO_SIGNATURE_INVALID		0x0000002D
147 #define	CRYPTO_BUFFER_TOO_SMALL			0x00000042
148 #define	CRYPTO_NOT_SUPPORTED			0x00000044
149 
150 #define	CRYPTO_INVALID_CONTEXT			0x00000047
151 #define	CRYPTO_INVALID_MAC			0x00000048
152 #define	CRYPTO_MECH_NOT_SUPPORTED		0x00000049
153 #define	CRYPTO_INVALID_PROVIDER_ID		0x0000004C
154 #define	CRYPTO_BUSY				0x0000004E
155 #define	CRYPTO_UNKNOWN_PROVIDER			0x0000004F
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
161 #endif /* _SYS_CRYPTO_COMMON_H */
162