| 1 | /*	$NetBSD: cryptodev.h,v 1.51.8.1 2026/05/07 14:40:13 martin Exp $ */ |
| 2 | /*	$FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $	*/ |
| 3 | /*	$OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $	*/ |
| 4 | |
| 5 | /*- |
| 6 | * Copyright (c) 2008 The NetBSD Foundation, Inc. |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * This code is derived from software contributed to The NetBSD Foundation |
| 10 | * by Coyote Point Systems, Inc. |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 22 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 23 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | * POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) |
| 36 | * |
| 37 | * This code was written by Angelos D. Keromytis in Athens, Greece, in |
| 38 | * February 2000. Network Security Technologies Inc. (NSTI) kindly |
| 39 | * supported the development of this code. |
| 40 | * |
| 41 | * Copyright (c) 2000 Angelos D. Keromytis |
| 42 | * |
| 43 | * Permission to use, copy, and modify this software with or without fee |
| 44 | * is hereby granted, provided that this entire notice is included in |
| 45 | * all source code copies of any software which is or includes a copy or |
| 46 | * modification of this software. |
| 47 | * |
| 48 | * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR |
| 49 | * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY |
| 50 | * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE |
| 51 | * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR |
| 52 | * PURPOSE. |
| 53 | * |
| 54 | * Copyright (c) 2001 Theo de Raadt |
| 55 | * |
| 56 | * Redistribution and use in source and binary forms, with or without |
| 57 | * modification, are permitted provided that the following conditions |
| 58 | * are met: |
| 59 | * |
| 60 | * 1. Redistributions of source code must retain the above copyright |
| 61 | * notice, this list of conditions and the following disclaimer. |
| 62 | * 2. Redistributions in binary form must reproduce the above copyright |
| 63 | * notice, this list of conditions and the following disclaimer in the |
| 64 | * documentation and/or other materials provided with the distribution. |
| 65 | * 3. The name of the author may not be used to endorse or promote products |
| 66 | * derived from this software without specific prior written permission. |
| 67 | * |
| 68 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 69 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 70 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 71 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 72 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 73 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 74 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 75 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 76 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 77 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 78 | * |
| 79 | * Effort sponsored in part by the Defense Advanced Research Projects |
| 80 | * Agency (DARPA) and Air Force Research Laboratory, Air Force |
| 81 | * Materiel Command, USAF, under agreement number F30602-01-2-0537. |
| 82 | * |
| 83 | */ |
| 84 | |
| 85 | #ifndef _CRYPTO_CRYPTO_H_ |
| 86 | #define _CRYPTO_CRYPTO_H_ |
| 87 | |
| 88 | #include <sys/types.h> |
| 89 | |
| 90 | #include <sys/ioccom.h> |
| 91 | #include <sys/time.h> |
| 92 | |
| 93 | #if defined(_KERNEL_OPT) |
| 94 | #include "opt_ocf.h" |
| 95 | #endif |
| 96 | |
| 97 | /* Some initial values */ |
| 98 | #define CRYPTO_DRIVERS_INITIAL	4 |
| 99 | #define CRYPTO_SW_SESSIONS	32 |
| 100 | |
| 101 | /* HMAC values */ |
| 102 | #define HMAC_BLOCK_LEN		64 /* for compatibility */ |
| 103 | #define HMAC_IPAD_VAL		0x36 |
| 104 | #define HMAC_OPAD_VAL		0x5C |
| 105 | |
| 106 | /* Encryption algorithm block sizes */ |
| 107 | #define DES_BLOCK_LEN		8 |
| 108 | #define DES3_BLOCK_LEN		8 |
| 109 | #define BLOWFISH_BLOCK_LEN	8 |
| 110 | #define SKIPJACK_BLOCK_LEN	8 |
| 111 | #define CAST128_BLOCK_LEN	8 |
| 112 | #define RIJNDAEL128_BLOCK_LEN	16 |
| 113 | #define EALG_MAX_BLOCK_LEN	16 /* Keep this updated */ |
| 114 | |
| 115 | /* Maximum hash algorithm result length */ |
| 116 | #define AALG_MAX_RESULT_LEN	64 /* Keep this updated */ |
| 117 | |
| 118 | #define	CRYPTO_ALGORITHM_MIN	1 |
| 119 | #define CRYPTO_DES_CBC		1 |
| 120 | #define CRYPTO_3DES_CBC		2 |
| 121 | #define CRYPTO_BLF_CBC		3 |
| 122 | #define CRYPTO_CAST_CBC		4 |
| 123 | #define CRYPTO_SKIPJACK_CBC	5 |
| 124 | #define CRYPTO_MD5_HMAC		6 |
| 125 | #define CRYPTO_SHA1_HMAC	7 |
| 126 | #define CRYPTO_RIPEMD160_HMAC	8 |
| 127 | #define CRYPTO_MD5_KPDK		9 |
| 128 | #define CRYPTO_SHA1_KPDK	10 |
| 129 | #define CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */ |
| 130 | #define CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */ |
| 131 | #define CRYPTO_ARC4		12 |
| 132 | #define CRYPTO_MD5		13 |
| 133 | #define CRYPTO_SHA1		14 |
| 134 | #define CRYPTO_SHA2_256_HMAC	15 |
| 135 | #define CRYPTO_SHA2_HMAC	CRYPTO_SHA2_256_HMAC /* for compatibility */ |
| 136 | #define CRYPTO_NULL_HMAC	16 |
| 137 | #define CRYPTO_NULL_CBC		17 |
| 138 | #define CRYPTO_DEFLATE_COMP	18 /* Deflate compression algorithm */ |
| 139 | #define CRYPTO_MD5_HMAC_96	19 |
| 140 | #define CRYPTO_SHA1_HMAC_96	20 |
| 141 | #define CRYPTO_RIPEMD160_HMAC_96	21 |
| 142 | #define CRYPTO_GZIP_COMP	22 /* gzip compression algorithm */ |
| 143 | #define CRYPTO_DEFLATE_COMP_NOGROW 23 /* Deflate, fail if not compressible */ |
| 144 | #define CRYPTO_SHA2_384_HMAC	24 |
| 145 | #define CRYPTO_SHA2_512_HMAC	25 |
| 146 | #define CRYPTO_CAMELLIA_CBC	26 |
| 147 | #define CRYPTO_AES_CTR		27 |
| 148 | #define CRYPTO_AES_XCBC_MAC_96	28 |
| 149 | #define CRYPTO_AES_GCM_16	29 |
| 150 | #define CRYPTO_AES_128_GMAC	30 |
| 151 | #define CRYPTO_AES_192_GMAC	31 |
| 152 | #define CRYPTO_AES_256_GMAC	32 |
| 153 | #define CRYPTO_AES_GMAC		33 |
| 154 | #define CRYPTO_ALGORITHM_MAX	33 /* Keep updated - see below */ |
| 155 | |
| 156 | /* Algorithm flags */ |
| 157 | #define	CRYPTO_ALG_FLAG_SUPPORTED	0x01 /* Algorithm is supported */ |
| 158 | #define	CRYPTO_ALG_FLAG_RNG_ENABLE	0x02 /* Has HW RNG for DH/DSA */ |
| 159 | #define	CRYPTO_ALG_FLAG_DSA_SHA		0x04 /* Can do SHA on msg */ |
| 160 | |
| 161 | struct session_op { |
| 162 | 	uint32_t	cipher;		/* ie. CRYPTO_DES_CBC */ |
| 163 | 	uint32_t	mac;		/* ie. CRYPTO_MD5_HMAC */ |
| 164 | 	uint32_t	comp_alg;	/* ie. CRYPTO_GZIP_COMP */ |
| 165 | |
| 166 | 	uint32_t	keylen;		/* cipher key */ |
| 167 | 	void *		key; |
| 168 | 	uint32_t	mackeylen;	/* mac key */ |
| 169 | 	void *		mackey; |
| 170 | |
| 171 | 	uint32_t	ses;		/* returns: session # */ |
| 172 | }; |
| 173 | |
| 174 | /* to support multiple session creation */ |
| 175 | |
| 176 | struct session_n_op { |
| 177 | 	uint32_t	cipher;		/* ie. CRYPTO_DES_CBC */ |
| 178 | 	uint32_t	mac;		/* ie. CRYPTO_MD5_HMAC */ |
| 179 | 	uint32_t	comp_alg;	/* ie. CRYPTO_GZIP_COMP */ |
| 180 | |
| 181 | 	uint32_t	keylen;		/* cipher key */ |
| 182 | 	void *		key; |
| 183 | 	uint32_t	mackeylen;	/* mac key */ |
| 184 | 	void *		mackey; |
| 185 | |
| 186 | 	uint32_t	ses;		/* returns: session # */ |
| 187 | 	int		status; |
| 188 | }; |
| 189 | |
| 190 | struct crypt_op { |
| 191 | 	uint32_t	ses; |
| 192 | 	uint16_t	op;		/* i.e. COP_ENCRYPT */ |
| 193 | #define COP_ENCRYPT	1 |
| 194 | #define COP_DECRYPT	2 |
| 195 | #define COP_COMP	3 |
| 196 | #define COP_DECOMP	4 |
| 197 | 	uint16_t	flags; |
| 198 | #define	COP_F_BATCH 	0x0008		/* Dispatch as quickly as possible */ |
| 199 | 	uint32_t	len;		/* src len */ |
| 200 | 	void *		src, *dst;	/* become iov[] inside kernel */ |
| 201 | 	void *		mac;		/* must be big enough for chosen MAC */ |
| 202 | 	void *		iv; |
| 203 | 	uint32_t	dst_len;	/* dst len if not 0 */ |
| 204 | }; |
| 205 | |
| 206 | /* to support multiple session creation */ |
| 207 | /* |
| 208 | * |
| 209 | * The reqid field is filled when the operation has |
| 210 | * been accepted and started, and can be used to later retrieve |
| 211 | * the operation results via CIOCNCRYPTRET or identify the |
| 212 | * request in the completion list returned by CIOCNCRYPTRETM. |
| 213 | * |
| 214 | * The opaque pointer can be set arbitrarily by the user |
| 215 | * and it is passed back in the crypt_result structure |
| 216 | * when the request completes. This field can be used for example |
| 217 | * to track context for the request and avoid lookups in the |
| 218 | * user application. |
| 219 | */ |
| 220 | |
| 221 | struct crypt_n_op { |
| 222 | 	uint32_t	ses; |
| 223 | 	uint16_t	op;		/* i.e. COP_ENCRYPT */ |
| 224 | #define COP_ENCRYPT	1 |
| 225 | #define COP_DECRYPT	2 |
| 226 | 	uint16_t	flags; |
| 227 | #define COP_F_BATCH	0x0008		/* Dispatch as quickly as possible */ |
| 228 | #define COP_F_MORE	0x0010		/* more data to follow */ |
| 229 | 	uint32_t	len;		/* src len */ |
| 230 | |
| 231 | 	uint32_t	reqid;		/* request id */ |
| 232 | 	int		status;		/* status of request -accepted or not */	 |
| 233 | 	void		*opaque;	/* opaque pointer returned to user */ |
| 234 | 	uint32_t	keylen;		/* cipher key - optional */ |
| 235 | 	void *		key; |
| 236 | 	uint32_t	mackeylen;	/* also optional */ |
| 237 | 	void *		mackey; |
| 238 | |
| 239 | 	void *		src, *dst;	/* become iov[] inside kernel */ |
| 240 | 	void *		mac;		/* must be big enough for chosen MAC */ |
| 241 | 	void *		iv; |
| 242 | 	uint32_t	dst_len;	/* dst len if not 0 */ |
| 243 | }; |
| 244 | |
| 245 | /* CIOCNCRYPTM ioctl argument, supporting one or more asynchronous |
| 246 | * crypt_n_op operations. |
| 247 | * Each crypt_n_op will receive a request id which can be used to check its |
| 248 | * status via CIOCNCRYPTRET, or to watch for its completion in the list |
| 249 | * obtained via CIOCNCRYPTRETM. |
| 250 | */ |
| 251 | struct crypt_mop { |
| 252 | 	size_t 		count;		/* how many */ |
| 253 | 	struct crypt_n_op *	reqs;	/* where to get them */ |
| 254 | }; |
| 255 | |
| 256 | struct crypt_sfop { |
| 257 | 	size_t		count; |
| 258 | 	uint32_t	*sesid; |
| 259 | }; |
| 260 | |
| 261 | struct crypt_sgop { |
| 262 | 	size_t		count; |
| 263 | 	struct session_n_op * sessions; |
| 264 | }; |
| 265 | |
| 266 | #define CRYPTO_MAX_MAC_LEN	32 /* Keep this updated */ |
| 267 | |
| 268 | /* bignum parameter, in packed bytes, ... */ |
| 269 | struct crparam { |
| 270 | 	void *		crp_p; |
| 271 | 	uint32_t	crp_nbits; |
| 272 | }; |
| 273 | |
| 274 | #define CRK_MAXPARAM	8 |
| 275 | |
| 276 | struct crypt_kop { |
| 277 | 	uint32_t	crk_op;		/* ie. CRK_MOD_EXP or other */ |
| 278 | 	uint32_t	crk_status;	/* return status */ |
| 279 | 	uint16_t	crk_iparams;	/* # of input parameters */ |
| 280 | 	uint16_t	crk_oparams;	/* # of output parameters */ |
| 281 | 	uint32_t	crk_pad1; |
| 282 | 	struct crparam	crk_param[CRK_MAXPARAM]; |
| 283 | }; |
| 284 | |
| 285 | /* |
| 286 | * Used with the CIOCNFKEYM ioctl. |
| 287 | * |
| 288 | * This structure allows the OCF to return a request id |
| 289 | * for each of the kop operations specified in the CIOCNFKEYM call. |
| 290 | * |
| 291 | * The crk_opaque pointer can be arbitrarily set by the user |
| 292 | * and it is passed back in the crypt_result structure |
| 293 | * when the request completes. This field can be used for example |
| 294 | * to track context for the request and avoid lookups in the |
| 295 | * user application. |
| 296 | */ |
| 297 | struct crypt_n_kop { |
| 298 | 	uint32_t	crk_op;		/* ie. CRK_MOD_EXP or other */ |
| 299 | 	uint32_t	crk_status;	/* return status */ |
| 300 | 	uint16_t	crk_iparams;	/* # of input parameters */ |
| 301 | 	uint16_t	crk_oparams;	/* # of output parameters */ |
| 302 | uint32_t	crk_reqid;	/* request id */ |
| 303 | 	struct crparam	crk_param[CRK_MAXPARAM]; |
| 304 | 	void		*crk_opaque;	/* opaque pointer returned to user */ |
| 305 | }; |
| 306 | |
| 307 | struct crypt_mkop { |
| 308 | 	size_t	count;			/* how many */ |
| 309 | 	struct crypt_n_kop *	reqs;	/* where to get them */ |
| 310 | }; |
| 311 | |
| 312 | /* Asynchronous key or crypto result. |
| 313 | * Note that the status will be set in the crypt_result structure, |
| 314 | * not in the original crypt_kop structure (crk_status). |
| 315 | */ |
| 316 | struct crypt_result { |
| 317 | 	uint32_t	reqid;		/* request id */ |
| 318 | 	uint32_t	status;		/* status of request: 0 if successful */ |
| 319 | 	void *		opaque;		/* Opaque pointer from the user, passed along */ |
| 320 | }; |
| 321 | |
| 322 | struct cryptret { |
| 323 | 	size_t		count;		/* space for how many */ |
| 324 | 	struct crypt_result *	results;	/* where to put them */ |
| 325 | }; |
| 326 | |
| 327 | |
| 328 | /* Asymmetric key operations */ |
| 329 | #define	CRK_ALGORITHM_MIN	0 |
| 330 | #define CRK_MOD_EXP		0 |
| 331 | #define CRK_MOD_EXP_CRT		1 |
| 332 | #define CRK_DSA_SIGN		2 |
| 333 | #define CRK_DSA_VERIFY		3 |
| 334 | #define CRK_DH_COMPUTE_KEY	4 |
| 335 | #define CRK_MOD_ADD		5 |
| 336 | #define CRK_MOD_ADDINV		6 |
| 337 | #define CRK_MOD_SUB		7 |
| 338 | #define CRK_MOD_MULT		8 |
| 339 | #define CRK_MOD_MULTINV		9 |
| 340 | #define CRK_MOD			10 |
| 341 | #define CRK_ALGORITHM_MAX	10 /* Keep updated - see below */ |
| 342 | |
| 343 | #define CRF_MOD_EXP		(1 << CRK_MOD_EXP) |
| 344 | #define CRF_MOD_EXP_CRT		(1 << CRK_MOD_EXP_CRT) |
| 345 | #define CRF_DSA_SIGN		(1 << CRK_DSA_SIGN) |
| 346 | #define CRF_DSA_VERIFY		(1 << CRK_DSA_VERIFY) |
| 347 | #define CRF_DH_COMPUTE_KEY	(1 << CRK_DH_COMPUTE_KEY) |
| 348 | #define CRF_MOD_ADD		(1 << CRK_MOD_ADD) |
| 349 | #define CRF_MOD_ADDINV		(1 << CRK_MOD_ADDINV) |
| 350 | #define CRF_MOD_SUB		(1 << CRK_MOD_SUB) |
| 351 | #define CRF_MOD_MULT		(1 << CRK_MOD_MULT) |
| 352 | #define CRF_MOD_MULTINV		(1 << CRK_MOD_MULTINV) |
| 353 | #define CRF_MOD			(1 << CRK_MOD) |
| 354 | |
| 355 | /* |
| 356 | * A large comment here once held descriptions of the ioctl |
| 357 | * requests implemented by the device. This text has been moved |
| 358 | * to the crypto(4) manual page and, later, removed from this file |
| 359 | * as it was always a step behind the times. |
| 360 | */ |
| 361 | |
| 362 | /* |
| 363 | * done against open of /dev/crypto, to get a cloned descriptor. |
| 364 | * Please use F_SETFD against the cloned descriptor. But this ioctl |
| 365 | * is obsolete (the device now clones): please, just don't use it. |
| 366 | */ |
| 367 | #define	CRIOGET		_IOWR('c', 100, uint32_t) |
| 368 | |
| 369 | /* the following are done against the cloned descriptor */ |
| 370 | #define	CIOCFSESSION	_IOW('c', 102, uint32_t) |
| 371 | #define CIOCKEY		_IOWR('c', 104, struct crypt_kop) |
| 372 | #define CIOCNFKEYM	_IOWR('c', 108, struct crypt_mkop) |
| 373 | #define CIOCNFSESSION	_IOW('c', 109, struct crypt_sfop) |
| 374 | #define CIOCNCRYPTRETM	_IOWR('c', 110, struct cryptret) |
| 375 | #define CIOCNCRYPTRET	_IOWR('c', 111, struct crypt_result) |
| 376 | |
| 377 | #define	CIOCGSESSION	_IOWR('c', 112, struct session_op) |
| 378 | #define	CIOCNGSESSION	_IOWR('c', 113, struct crypt_sgop) |
| 379 | #define CIOCCRYPT	_IOWR('c', 114, struct crypt_op) |
| 380 | #define CIOCNCRYPTM	_IOWR('c', 115, struct crypt_mop) |
| 381 | |
| 382 | #define CIOCASYMFEAT	_IOR('c', 105, uint32_t) |
| 383 | |
| 384 | struct cryptotstat { |
| 385 | 	struct timespec	acc;		/* total accumulated time */ |
| 386 | 	struct timespec	min;		/* max time */ |
| 387 | 	struct timespec	max;		/* max time */ |
| 388 | 	uint32_t	count;		/* number of observations */ |
| 389 | }; |
| 390 | |
| 391 | struct cryptostats { |
| 392 | 	uint32_t	cs_ops;		/* symmetric crypto ops submitted */ |
| 393 | 	uint32_t	cs_errs;	/* symmetric crypto ops that failed */ |
| 394 | 	uint32_t	cs_kops;	/* asymmetric/key ops submitted */ |
| 395 | 	uint32_t	cs_kerrs;	/* asymmetric/key ops that failed */ |
| 396 | 	uint32_t	cs_intrs;	/* crypto swi thread activations */ |
| 397 | 	uint32_t	cs_rets;	/* crypto return thread activations */ |
| 398 | 	uint32_t	cs_blocks;	/* symmetric op driver block */ |
| 399 | 	uint32_t	cs_kblocks;	/* symmetric op driver block */ |
| 400 | 	/* |
| 401 | 	 * When CRYPTO_TIMING is defined at compile time and the |
| 402 | 	 * sysctl debug.crypto is set to 1, the crypto system will |
| 403 | 	 * accumulate statistics about how long it takes to process |
| 404 | 	 * crypto requests at various points during processing. |
| 405 | 	 */ |
| 406 | 	struct cryptotstat cs_invoke;	/* crypto_dispatch -> crypto_invoke */ |
| 407 | 	struct cryptotstat cs_done;	/* crypto_invoke -> crypto_done */ |
| 408 | 	struct cryptotstat cs_cb;	/* crypto_done -> callback */ |
| 409 | 	struct cryptotstat cs_finis;	/* callback -> callback return */ |
| 410 | }; |
| 411 | |
| 412 | #ifdef _KERNEL |
| 413 | |
| 414 | #include <sys/condvar.h> |
| 415 | #include <sys/malloc.h> |
| 416 | #include <sys/mutex.h> |
| 417 | #include <sys/queue.h> |
| 418 | #include <sys/systm.h> |
| 419 | |
| 420 | struct cpu_info; |
| 421 | struct uio; |
| 422 | |
| 423 | /* Standard initialization structure beginning */ |
| 424 | struct cryptoini { |
| 425 | 	int		cri_alg;	/* Algorithm to use */ |
| 426 | 	uint32_t	cri_klen;	/* Key length, in bits */ |
| 427 | 	uint32_t	cri_rnd;	/* Algorithm rounds, where relevant */ |
| 428 | 	char	 *cri_key;	/* key to use */ |
| 429 | 	uint8_t	cri_iv[EALG_MAX_BLOCK_LEN];	/* IV to use */ |
| 430 | 	struct cryptoini *cri_next; |
| 431 | }; |
| 432 | |
| 433 | /* Describe boundaries of a single crypto operation */ |
| 434 | struct cryptodesc { |
| 435 | 	uint32_t	crd_skip;	/* How many bytes to ignore from start */ |
| 436 | 	uint32_t	crd_len;	/* How many bytes to process */ |
| 437 | 	uint32_t	crd_inject;	/* Where to inject results, if applicable */ |
| 438 | 	int		crd_flags; |
| 439 | |
| 440 | #define	CRD_F_ENCRYPT		0x01	/* Set when doing encryption */ |
| 441 | #define	CRD_F_IV_PRESENT	0x02	/* When encrypting, IV is already in |
| 442 | 					 place, so don't copy. */ |
| 443 | #define	CRD_F_IV_EXPLICIT	0x04	/* IV explicitly provided */ |
| 444 | #define	CRD_F_DSA_SHA_NEEDED	0x08	/* Compute SHA-1 of buffer for DSA */ |
| 445 | #define CRD_F_COMP		0x10 /* Set when doing compression */ |
| 446 | |
| 447 | 	struct cryptoini	CRD_INI; /* Initialization/context data */ |
| 448 | #define crd_iv		CRD_INI.cri_iv |
| 449 | #define crd_key		CRD_INI.cri_key |
| 450 | #define crd_rnd		CRD_INI.cri_rnd |
| 451 | #define crd_alg		CRD_INI.cri_alg |
| 452 | #define crd_klen	CRD_INI.cri_klen |
| 453 | |
| 454 | 	struct cryptodesc *crd_next; |
| 455 | }; |
| 456 | |
| 457 | struct cryptop_data { |
| 458 | 	struct csession *cse; |
| 459 | 	struct iovec	iovec[1];	/* user requests never have more */ |
| 460 | 	struct uio	uio; |
| 461 | 	size_t		iov_len; |
| 462 | }; |
| 463 | |
| 464 | /* Structure describing complete operation */ |
| 465 | struct cryptop { |
| 466 | 	TAILQ_ENTRY(cryptop) crp_next; |
| 467 | 	uint64_t	crp_sid;	/* Session ID */ |
| 468 | |
| 469 | 	uint32_t	crp_ilen;	/* Input data total length */ |
| 470 | 	uint32_t	crp_olen;	/* Result total length */ |
| 471 | |
| 472 | 	int		crp_etype;	/* |
| 473 | 					 * Error type (zero means no error). |
| 474 | 					 * All error codes |
| 475 | 					 * indicate possible data corruption (as in, |
| 476 | 					 * the data have been touched). On all |
| 477 | 					 * errors, the crp_sid may have changed |
| 478 | 					 * (reset to a new one), so the caller |
| 479 | 					 * should always check and use the new |
| 480 | 					 * value on future requests. |
| 481 | 					 */ |
| 482 | 	int		crp_flags;	/* |
| 483 | 					 * other than crypto.c must not write |
| 484 | 					 * after crypto_dispatch(). |
| 485 | 					 */ |
| 486 | #define CRYPTO_F_IMBUF		0x0001	/* Input/output are mbuf chains */ |
| 487 | #define CRYPTO_F_IOV		0x0002	/* Input/output are uio */ |
| 488 | #define CRYPTO_F_REL		0x0004	/* Must return data in same place */ |
| 489 | #define	CRYPTO_F_BATCH		0x0008	/* Batch op if possible possible */ |
| 490 | #define	CRYPTO_F_UNUSED0	0x0010	/* was CRYPTO_F_CBIMM */ |
| 491 | #define	CRYPTO_F_UNUSED1	0x0020	/* was CRYPTO_F_DONE */ |
| 492 | #define	CRYPTO_F_UNUSED2	0x0040	/* was CRYPTO_F_CBIFSYNC */ |
| 493 | #define	CRYPTO_F_ONRETQ		0x0080	/* Request is on return queue */ |
| 494 | #define	CRYPTO_F_UNUSED3	0x0100	/* was CRYPTO_F_USER */ |
| 495 | #define	CRYPTO_F_MORE		0x0200	/* more data to follow */ |
| 496 | |
| 497 | 	int		crp_devflags;	/* other than cryptodev.c must not use. */ |
| 498 | #define	CRYPTODEV_F_RET		0x0001	/* return from crypto.c to cryptodev.c */ |
| 499 | |
| 500 | 	void *		crp_buf;	/* Data to be processed */ |
| 501 | 	void *		crp_opaque;	/* Opaque pointer, passed along */ |
| 502 | 	struct cryptodesc *crp_desc;	/* Linked list of processing descriptors */ |
| 503 | |
| 504 | 	void (*crp_callback)(struct cryptop *); /* |
| 505 | 						* Callback function. |
| 506 | 						* That must not sleep as it is |
| 507 | 						* called in softint context. |
| 508 | 						*/ |
| 509 | |
| 510 | 	void *		crp_mac; |
| 511 | |
| 512 | 	/* |
| 513 | 	 * everything below is private to crypto(4) |
| 514 | 	 */ |
| 515 | 	uint32_t	crp_reqid;	/* request id */ |
| 516 | 	void *		crp_usropaque;	/* Opaque pointer from user, passed along */ |
| 517 | 	struct timespec	crp_tstamp;	/* performance time stamp */ |
| 518 | 	kcondvar_t	crp_cv; |
| 519 | 	struct fcrypt 	*fcrp; |
| 520 | 	void * 		dst; |
| 521 | 	void *		mac; |
| 522 | 	uint32_t	len; |
| 523 | 	u_char		tmp_iv[EALG_MAX_BLOCK_LEN]; |
| 524 | 	u_char		tmp_mac[CRYPTO_MAX_MAC_LEN]; |
| 525 | 	 |
| 526 | 	struct cryptop_data cod; |
| 527 | 	uint32_t	magic; |
| 528 | 	struct cpu_info	*reqcpu;	/* |
| 529 | 					 * save requested CPU to do cryptoret |
| 530 | 					 * softint in the same CPU. |
| 531 | 					 */ |
| 532 | }; |
| 533 | |
| 534 | #define CRYPTO_BUF_CONTIG	0x0 |
| 535 | #define CRYPTO_BUF_IOV		0x1 |
| 536 | #define CRYPTO_BUF_MBUF		0x2 |
| 537 | |
| 538 | #define CRYPTO_OP_DECRYPT	0x0 |
| 539 | #define CRYPTO_OP_ENCRYPT	0x1 |
| 540 | |
| 541 | /* |
| 542 | * Hints passed to process methods. |
| 543 | */ |
| 544 | #define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */ |
| 545 | |
| 546 | struct cryptkop { |
| 547 | 	TAILQ_ENTRY(cryptkop) krp_next; |
| 548 | |
| 549 | 	uint32_t	krp_reqid;	/* request id */ |
| 550 | 	void *		krp_usropaque;	/* Opaque pointer from user, passed along */ |
| 551 | |
| 552 | 	uint32_t	krp_op;		/* ie. CRK_MOD_EXP or other */ |
| 553 | 	uint32_t	krp_status;	/* return status */ |
| 554 | 	uint16_t	krp_iparams;	/* # of input parameters */ |
| 555 | 	uint16_t	krp_oparams;	/* # of output parameters */ |
| 556 | 	uint32_t	krp_hid; |
| 557 | 	kmutex_t	krp_lock; |
| 558 | 	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */ |
| 559 | 	void		(*krp_callback)(struct cryptkop *); /* |
| 560 | 							 * Callback function. |
| 561 | 							 * That must not sleep as it is |
| 562 | 							 * called in softint context. |
| 563 | 							 */ |
| 564 | 	int		krp_flags;	/* same values as crp_flags */ |
| 565 | 	int		krp_devflags;	/* same values as crp_devflags */ |
| 566 | 	kcondvar_t	krp_cv; |
| 567 | 	struct fcrypt 	*fcrp; |
| 568 | 	struct crparam	crk_param[CRK_MAXPARAM]; |
| 569 | 	struct cpu_info	*reqcpu; |
| 570 | }; |
| 571 | |
| 572 | /* Crypto capabilities structure */ |
| 573 | struct cryptocap { |
| 574 | 	uint32_t	cc_sessions; |
| 575 | |
| 576 | 	/* |
| 577 | 	 * Largest possible operator length (in bits) for each type of |
| 578 | 	 * encryption algorithm. |
| 579 | 	 */ |
| 580 | 	uint16_t	cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1]; |
| 581 | |
| 582 | 	uint8_t	cc_alg[CRYPTO_ALGORITHM_MAX + 1]; |
| 583 | |
| 584 | 	uint8_t	cc_kalg[CRK_ALGORITHM_MAX + 1]; |
| 585 | |
| 586 | 	uint8_t	cc_flags; |
| 587 | 	uint8_t	cc_qblocked;		/* symmetric q blocked */ |
| 588 | 	uint8_t	cc_kqblocked;		/* asymmetric q blocked */ |
| 589 | #define CRYPTOCAP_F_CLEANUP	0x01		/* needs resource cleanup */ |
| 590 | #define CRYPTOCAP_F_SOFTWARE	0x02		/* software implementation */ |
| 591 | #define CRYPTOCAP_F_SYNC	0x04		/* operates synchronously */ |
| 592 | |
| 593 | 	void		*cc_arg;		/* callback argument */ |
| 594 | 	int		(*cc_newsession)(void*, uint32_t*, struct cryptoini*); |
| 595 | 	int		(*cc_process) (void*, struct cryptop *, int); |
| 596 | 	void		(*cc_freesession) (void *, uint64_t); |
| 597 | 	void		*cc_karg;		/* callback argument */ |
| 598 | 	int		(*cc_kprocess) (void*, struct cryptkop *, int); |
| 599 | |
| 600 | 	kmutex_t	cc_lock; |
| 601 | }; |
| 602 | |
| 603 | /* |
| 604 | * Session ids are 64 bits. The lower 32 bits contain a "local id" which |
| 605 | * is a driver-private session identifier. The upper 32 bits contain a |
| 606 | * "hardware id" used by the core crypto code to identify the driver and |
| 607 | * a copy of the driver's capabilities that can be used by client code to |
| 608 | * optimize operation. |
| 609 | */ |
| 610 | #define	CRYPTO_SESID2HID(_sid)	((((_sid) >> 32) & 0xffffff) - 1) |
| 611 | #define	CRYPTO_SESID2CAPS(_sid)	(((_sid) >> 56) & 0xff) |
| 612 | #define	CRYPTO_SESID2LID(_sid)	(((uint32_t) (_sid)) & 0xffffffff) |
| 613 | |
| 614 | MALLOC_DECLARE(M_CRYPTO_DATA); |
| 615 | |
| 616 | extern	int crypto_newsession(uint64_t *sid, struct cryptoini *cri, int hard); |
| 617 | extern	void crypto_freesession(uint64_t sid); |
| 618 | extern	int32_t crypto_get_driverid(uint32_t flags); |
| 619 | extern	int crypto_register(uint32_t driverid, int alg, uint16_t maxoplen, |
| 620 | 	 uint32_t flags, |
| 621 | 	 int (*newses)(void*, uint32_t*, struct cryptoini*), |
| 622 | 	 void (*freeses)(void *, uint64_t), |
| 623 | 	 int (*process)(void*, struct cryptop *, int), |
| 624 | 	 void *arg); |
| 625 | extern	int crypto_kregister(uint32_t, int, uint32_t, |
| 626 | 	 int (*)(void*, struct cryptkop *, int), |
| 627 | 	 void *arg); |
| 628 | extern	int crypto_unregister(uint32_t driverid, int alg); |
| 629 | extern	int crypto_unregister_all(uint32_t driverid); |
| 630 | extern	void crypto_dispatch(struct cryptop *crp); |
| 631 | extern	void crypto_kdispatch(struct cryptkop *); |
| 632 | #define	CRYPTO_SYMQ	0x1 |
| 633 | #define	CRYPTO_ASYMQ	0x2 |
| 634 | extern	int crypto_unblock(uint32_t, int); |
| 635 | extern	void crypto_done(struct cryptop *crp); |
| 636 | extern	void crypto_kdone(struct cryptkop *); |
| 637 | extern	int crypto_getfeat(int *); |
| 638 | |
| 639 | void	cuio_copydata(struct uio *, int, int, void *); |
| 640 | void	cuio_copyback(struct uio *, int, int, void *); |
| 641 | int	cuio_apply(struct uio *, int, int, |
| 642 | 	 int (*f)(void *, void *, unsigned int), void *); |
| 643 | |
| 644 | extern	void crypto_freereq(struct cryptop *crp); |
| 645 | extern	struct cryptop *crypto_getreq(int num); |
| 646 | |
| 647 | extern	void crypto_kfreereq(struct cryptkop *); |
| 648 | extern	struct cryptkop *crypto_kgetreq(int, int); |
| 649 | |
| 650 | extern	int crypto_usercrypto;		/* userland may do crypto requests */ |
| 651 | extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */ |
| 652 | extern	int crypto_devallowsoft;	/* only use hardware crypto */ |
| 653 | |
| 654 | /* |
| 655 | * initialize the crypto framework subsystem (not the pseudo-device). |
| 656 | * This must be called very early in boot, so the framework is ready |
| 657 | * to handle registration requests when crypto hardware is autoconfigured. |
| 658 | * (This declaration doesn't really belong here but there's no header |
| 659 | * for the raw framework.) |
| 660 | */ |
| 661 | int	crypto_init(void); |
| 662 | |
| 663 | /* |
| 664 | * Crypto-related utility routines used mainly by drivers. |
| 665 | * |
| 666 | * XXX these don't really belong here; but for now they're |
| 667 | * kept apart from the rest of the system. |
| 668 | */ |
| 669 | struct uio; |
| 670 | extern	void cuio_copydata(struct uio* uio, int off, int len, void *cp); |
| 671 | extern	void cuio_copyback(struct uio* uio, int off, int len, void *cp); |
| 672 | extern int	cuio_getptr(struct uio *, int loc, int *off); |
| 673 | |
| 674 | #ifdef CRYPTO_DEBUG	/* yuck, netipsec defines these differently */ |
| 675 | #ifndef DPRINTF |
| 676 | #define DPRINTF(a, ...)	printf("%s: " a, __func__, ##__VA_ARGS__) |
| 677 | #endif |
| 678 | #else |
| 679 | #ifndef DPRINTF |
| 680 | #define DPRINTF(a, ...) |
| 681 | #endif |
| 682 | #endif |
| 683 | |
| 684 | #endif /* _KERNEL */ |
| 685 | /* |
| 686 | * Locking notes: |
| 687 | * + crypto_drivers itself is protected by crypto_drv_mtx (an adaptive lock) |
| 688 | * + crypto_drivers[i] and its all members are protected by |
| 689 | * crypto_drivers[i].cc_lock (a spin lock) |
| 690 | * spin lock as crypto_unblock() can be called in interrupt context |
| 691 | * + percpu'ed crp_q and crp_kq are procted by splsoftnet. |
| 692 | * + crp_ret_q, crp_ret_kq and crypto_exit_flag that are members of |
| 693 | * struct crypto_crp_ret_qs are protected by crypto_crp_ret_qs.crp_ret_q_mtx |
| 694 | * (a spin lock) |
| 695 | * spin lock as crypto_done() can be called in interrupt context |
| 696 | * NOTE: |
| 697 | * It is not known whether crypto_done()(in interrupt context) is called |
| 698 | * in the same CPU as crypto_dispatch() is called. |
| 699 | * So, struct crypto_crp_ret_qs cannot be percpu(9). |
| 700 | * |
| 701 | * Locking order: |
| 702 | * - crypto_drv_mtx => crypto_drivers[i].cc_lock |
| 703 | */ |
| 704 | #endif /* _CRYPTO_CRYPTO_H_ */ |