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