Home | History | Annotate | Line # | Download | only in opencrypto
cryptodev.h revision 1.9
      1 /*	$NetBSD: cryptodev.h,v 1.9 2008/01/19 08:08:20 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  * The author of this code is Angelos D. Keromytis (angelos (at) cis.upenn.edu)
      7  *
      8  * This code was written by Angelos D. Keromytis in Athens, Greece, in
      9  * February 2000. Network Security Technologies Inc. (NSTI) kindly
     10  * supported the development of this code.
     11  *
     12  * Copyright (c) 2000 Angelos D. Keromytis
     13  *
     14  * Permission to use, copy, and modify this software with or without fee
     15  * is hereby granted, provided that this entire notice is included in
     16  * all source code copies of any software which is or includes a copy or
     17  * modification of this software.
     18  *
     19  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
     20  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
     21  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
     22  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
     23  * PURPOSE.
     24  *
     25  * Copyright (c) 2001 Theo de Raadt
     26  *
     27  * Redistribution and use in source and binary forms, with or without
     28  * modification, are permitted provided that the following conditions
     29  * are met:
     30  *
     31  * 1. Redistributions of source code must retain the above copyright
     32  *   notice, this list of conditions and the following disclaimer.
     33  * 2. Redistributions in binary form must reproduce the above copyright
     34  *   notice, this list of conditions and the following disclaimer in the
     35  *   documentation and/or other materials provided with the distribution.
     36  * 3. The name of the author may not be used to endorse or promote products
     37  *   derived from this software without specific prior written permission.
     38  *
     39  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     40  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     42  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     43  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     45  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     46  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     47  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     48  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     49  *
     50  * Effort sponsored in part by the Defense Advanced Research Projects
     51  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     52  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     53  *
     54  */
     55 
     56 #ifndef _CRYPTO_CRYPTO_H_
     57 #define _CRYPTO_CRYPTO_H_
     58 
     59 #include <sys/ioccom.h>
     60 
     61 /* Some initial values */
     62 #define CRYPTO_DRIVERS_INITIAL	4
     63 #define CRYPTO_SW_SESSIONS	32
     64 
     65 /* HMAC values */
     66 #define HMAC_BLOCK_LEN		64
     67 #define HMAC_IPAD_VAL		0x36
     68 #define HMAC_OPAD_VAL		0x5C
     69 
     70 /* Encryption algorithm block sizes */
     71 #define DES_BLOCK_LEN		8
     72 #define DES3_BLOCK_LEN		8
     73 #define BLOWFISH_BLOCK_LEN	8
     74 #define SKIPJACK_BLOCK_LEN	8
     75 #define CAST128_BLOCK_LEN	8
     76 #define RIJNDAEL128_BLOCK_LEN	16
     77 #define EALG_MAX_BLOCK_LEN	16 /* Keep this updated */
     78 
     79 /* Maximum hash algorithm result length */
     80 #define AALG_MAX_RESULT_LEN	64 /* Keep this updated */
     81 
     82 #define	CRYPTO_ALGORITHM_MIN	1
     83 #define CRYPTO_DES_CBC		1
     84 #define CRYPTO_3DES_CBC		2
     85 #define CRYPTO_BLF_CBC		3
     86 #define CRYPTO_CAST_CBC		4
     87 #define CRYPTO_SKIPJACK_CBC	5
     88 #define CRYPTO_MD5_HMAC		6
     89 #define CRYPTO_SHA1_HMAC	7
     90 #define CRYPTO_RIPEMD160_HMAC	8
     91 #define CRYPTO_MD5_KPDK		9
     92 #define CRYPTO_SHA1_KPDK	10
     93 #define CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */
     94 #define CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */
     95 #define CRYPTO_ARC4		12
     96 #define CRYPTO_MD5		13
     97 #define CRYPTO_SHA1		14
     98 #define	CRYPTO_SHA2_HMAC	15
     99 #define CRYPTO_NULL_HMAC	16
    100 #define CRYPTO_NULL_CBC		17
    101 #define CRYPTO_DEFLATE_COMP	18 /* Deflate compression algorithm */
    102 #define CRYPTO_ALGORITHM_MAX	18 /* Keep updated - see below */
    103 
    104 /* Algorithm flags */
    105 #define	CRYPTO_ALG_FLAG_SUPPORTED	0x01 /* Algorithm is supported */
    106 #define	CRYPTO_ALG_FLAG_RNG_ENABLE	0x02 /* Has HW RNG for DH/DSA */
    107 #define	CRYPTO_ALG_FLAG_DSA_SHA		0x04 /* Can do SHA on msg */
    108 
    109 struct session_op {
    110 	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
    111 	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
    112 
    113 	u_int32_t	keylen;		/* cipher key */
    114 	void *		key;
    115 	int		mackeylen;	/* mac key */
    116 	void *		mackey;
    117 
    118   	u_int32_t	ses;		/* returns: session # */
    119 };
    120 
    121 struct crypt_op {
    122 	u_int32_t	ses;
    123 	u_int16_t	op;		/* i.e. COP_ENCRYPT */
    124 #define COP_ENCRYPT	1
    125 #define COP_DECRYPT	2
    126 	u_int16_t	flags;
    127 #define	COP_F_BATCH 	0x0008		/* Dispatch as quickly as possible */
    128 	u_int		len;
    129 	void *		src, *dst;	/* become iov[] inside kernel */
    130 	void *		mac;		/* must be big enough for chosen MAC */
    131 	void *		iv;
    132 };
    133 
    134 #define CRYPTO_MAX_MAC_LEN	20
    135 
    136 /* bignum parameter, in packed bytes, ... */
    137 struct crparam {
    138 	void *		crp_p;
    139 	u_int		crp_nbits;
    140 };
    141 
    142 #define CRK_MAXPARAM	8
    143 
    144 struct crypt_kop {
    145 	u_int		crk_op;		/* ie. CRK_MOD_EXP or other */
    146 	u_int		crk_status;	/* return status */
    147 	u_short		crk_iparams;	/* # of input parameters */
    148 	u_short		crk_oparams;	/* # of output parameters */
    149 	u_int		crk_pad1;
    150 	struct crparam	crk_param[CRK_MAXPARAM];
    151 };
    152 #define	CRK_ALGORITM_MIN	0
    153 #define CRK_MOD_EXP		0
    154 #define CRK_MOD_EXP_CRT		1
    155 #define CRK_DSA_SIGN		2
    156 #define CRK_DSA_VERIFY		3
    157 #define CRK_DH_COMPUTE_KEY	4
    158 #define CRK_MOD_ADD		5
    159 #define CRK_MOD_ADDINV		6
    160 #define CRK_MOD_SUB		7
    161 #define CRK_MOD_MULT		8
    162 #define CRK_MOD_MULTINV		9
    163 #define CRK_MOD			10
    164 #define CRK_ALGORITHM_MAX	10 /* Keep updated - see below */
    165 
    166 #define CRF_MOD_EXP		(1 << CRK_MOD_EXP)
    167 #define CRF_MOD_EXP_CRT		(1 << CRK_MOD_EXP_CRT)
    168 #define CRF_DSA_SIGN		(1 << CRK_DSA_SIGN)
    169 #define CRF_DSA_VERIFY		(1 << CRK_DSA_VERIFY)
    170 #define CRF_DH_COMPUTE_KEY	(1 << CRK_DH_COMPUTE_KEY)
    171 #define CRF_MOD_ADD		(1 << CRK_MOD_ADD)
    172 #define CRF_MOD_ADDINV		(1 << CRK_MOD_ADDINV)
    173 #define CRF_MOD_SUB		(1 << CRK_MOD_SUB)
    174 #define CRF_MOD_MULT		(1 << CRK_MOD_MULT)
    175 #define CRF_MOD_MULTINV		(1 << CRK_MOD_MULTINV)
    176 #define CRF_MOD			(1 << CRK_MOD)
    177 
    178 /*
    179  * done against open of /dev/crypto, to get a cloned descriptor.
    180  * Please use F_SETFD against the cloned descriptor.
    181  */
    182 #define	CRIOGET		_IOWR('c', 100, u_int32_t)
    183 
    184 /* the following are done against the cloned descriptor */
    185 #define	CIOCGSESSION	_IOWR('c', 101, struct session_op)
    186 #define	CIOCFSESSION	_IOW('c', 102, u_int32_t)
    187 #define CIOCCRYPT	_IOWR('c', 103, struct crypt_op)
    188 #define CIOCKEY		_IOWR('c', 104, struct crypt_kop)
    189 
    190 #define CIOCASYMFEAT	_IOR('c', 105, u_int32_t)
    191 
    192 struct cryptotstat {
    193 	struct timespec	acc;		/* total accumulated time */
    194 	struct timespec	min;		/* max time */
    195 	struct timespec	max;		/* max time */
    196 	u_int32_t	count;		/* number of observations */
    197 };
    198 
    199 struct cryptostats {
    200 	u_int32_t	cs_ops;		/* symmetric crypto ops submitted */
    201 	u_int32_t	cs_errs;	/* symmetric crypto ops that failed */
    202 	u_int32_t	cs_kops;	/* asymetric/key ops submitted */
    203 	u_int32_t	cs_kerrs;	/* asymetric/key ops that failed */
    204 	u_int32_t	cs_intrs;	/* crypto swi thread activations */
    205 	u_int32_t	cs_rets;	/* crypto return thread activations */
    206 	u_int32_t	cs_blocks;	/* symmetric op driver block */
    207 	u_int32_t	cs_kblocks;	/* symmetric op driver block */
    208 	/*
    209 	 * When CRYPTO_TIMING is defined at compile time and the
    210 	 * sysctl debug.crypto is set to 1, the crypto system will
    211 	 * accumulate statistics about how long it takes to process
    212 	 * crypto requests at various points during processing.
    213 	 */
    214 	struct cryptotstat cs_invoke;	/* crypto_dipsatch -> crypto_invoke */
    215 	struct cryptotstat cs_done;	/* crypto_invoke -> crypto_done */
    216 	struct cryptotstat cs_cb;	/* crypto_done -> callback */
    217 	struct cryptotstat cs_finis;	/* callback -> callback return */
    218 };
    219 
    220 #ifdef _KERNEL
    221 /* Standard initialization structure beginning */
    222 struct cryptoini {
    223 	int		cri_alg;	/* Algorithm to use */
    224 	int		cri_klen;	/* Key length, in bits */
    225 	int		cri_rnd;	/* Algorithm rounds, where relevant */
    226 	char	       *cri_key;	/* key to use */
    227 	u_int8_t	cri_iv[EALG_MAX_BLOCK_LEN];	/* IV to use */
    228 	struct cryptoini *cri_next;
    229 };
    230 
    231 /* Describe boundaries of a single crypto operation */
    232 struct cryptodesc {
    233 	int		crd_skip;	/* How many bytes to ignore from start */
    234 	int		crd_len;	/* How many bytes to process */
    235 	int		crd_inject;	/* Where to inject results, if applicable */
    236 	int		crd_flags;
    237 
    238 #define	CRD_F_ENCRYPT		0x01	/* Set when doing encryption */
    239 #define	CRD_F_IV_PRESENT	0x02	/* When encrypting, IV is already in
    240 					   place, so don't copy. */
    241 #define	CRD_F_IV_EXPLICIT	0x04	/* IV explicitly provided */
    242 #define	CRD_F_DSA_SHA_NEEDED	0x08	/* Compute SHA-1 of buffer for DSA */
    243 #define CRD_F_COMP		0x0f    /* Set when doing compression */
    244 
    245 	struct cryptoini	CRD_INI; /* Initialization/context data */
    246 #define crd_iv		CRD_INI.cri_iv
    247 #define crd_key		CRD_INI.cri_key
    248 #define crd_rnd		CRD_INI.cri_rnd
    249 #define crd_alg		CRD_INI.cri_alg
    250 #define crd_klen	CRD_INI.cri_klen
    251 
    252 	struct cryptodesc *crd_next;
    253 };
    254 
    255 /* Structure describing complete operation */
    256 struct cryptop {
    257 	TAILQ_ENTRY(cryptop) crp_next;
    258 
    259 	u_int64_t	crp_sid;	/* Session ID */
    260 	int		crp_ilen;	/* Input data total length */
    261 	int		crp_olen;	/* Result total length */
    262 
    263 	int		crp_etype;	/*
    264 					 * Error type (zero means no error).
    265 					 * All error codes except EAGAIN
    266 					 * indicate possible data corruption (as in,
    267 					 * the data have been touched). On all
    268 					 * errors, the crp_sid may have changed
    269 					 * (reset to a new one), so the caller
    270 					 * should always check and use the new
    271 					 * value on future requests.
    272 					 */
    273 	int		crp_flags;
    274 
    275 #define CRYPTO_F_IMBUF		0x0001	/* Input/output are mbuf chains */
    276 #define CRYPTO_F_IOV		0x0002	/* Input/output are uio */
    277 #define CRYPTO_F_REL		0x0004	/* Must return data in same place */
    278 #define	CRYPTO_F_BATCH		0x0008	/* Batch op if possible possible */
    279 #define	CRYPTO_F_CBIMM		0x0010	/* Do callback immediately */
    280 #define	CRYPTO_F_DONE		0x0020	/* Operation completed */
    281 #define	CRYPTO_F_CBIFSYNC	0x0040	/* Do CBIMM if op is synchronous */
    282 
    283 	void *		crp_buf;	/* Data to be processed */
    284 	void *		crp_opaque;	/* Opaque pointer, passed along */
    285 	struct cryptodesc *crp_desc;	/* Linked list of processing descriptors */
    286 
    287 	int (*crp_callback)(struct cryptop *); /* Callback function */
    288 
    289 	void *		crp_mac;
    290 	struct timespec	crp_tstamp;	/* performance time stamp */
    291 };
    292 
    293 #define CRYPTO_BUF_CONTIG	0x0
    294 #define CRYPTO_BUF_IOV		0x1
    295 #define CRYPTO_BUF_MBUF		0x2
    296 
    297 #define CRYPTO_OP_DECRYPT	0x0
    298 #define CRYPTO_OP_ENCRYPT	0x1
    299 
    300 /*
    301  * Hints passed to process methods.
    302  */
    303 #define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */
    304 
    305 struct cryptkop {
    306 	TAILQ_ENTRY(cryptkop) krp_next;
    307 
    308 	u_int		krp_op;		/* ie. CRK_MOD_EXP or other */
    309 	u_int		krp_status;	/* return status */
    310 	u_short		krp_iparams;	/* # of input parameters */
    311 	u_short		krp_oparams;	/* # of output parameters */
    312 	u_int32_t	krp_hid;
    313 	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */
    314 	int		(*krp_callback)(struct cryptkop *);
    315 };
    316 
    317 /* Crypto capabilities structure */
    318 struct cryptocap {
    319 	u_int32_t	cc_sessions;
    320 
    321 	/*
    322 	 * Largest possible operator length (in bits) for each type of
    323 	 * encryption algorithm.
    324 	 */
    325 	u_int16_t	cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
    326 
    327 	u_int8_t	cc_alg[CRYPTO_ALGORITHM_MAX + 1];
    328 
    329 	u_int8_t	cc_kalg[CRK_ALGORITHM_MAX + 1];
    330 
    331 	u_int8_t	cc_flags;
    332 	u_int8_t	cc_qblocked;		/* symmetric q blocked */
    333 	u_int8_t	cc_kqblocked;		/* asymmetric q blocked */
    334 #define CRYPTOCAP_F_CLEANUP	0x01		/* needs resource cleanup */
    335 #define CRYPTOCAP_F_SOFTWARE	0x02		/* software implementation */
    336 #define CRYPTOCAP_F_SYNC	0x04		/* operates synchronously */
    337 
    338 	void		*cc_arg;		/* callback argument */
    339 	int		(*cc_newsession)(void*, u_int32_t*, struct cryptoini*);
    340 	int		(*cc_process) (void*, struct cryptop *, int);
    341 	int		(*cc_freesession) (void*, u_int64_t);
    342 	void		*cc_karg;		/* callback argument */
    343 	int		(*cc_kprocess) (void*, struct cryptkop *, int);
    344 };
    345 
    346 /*
    347  * Session ids are 64 bits.  The lower 32 bits contain a "local id" which
    348  * is a driver-private session identifier.  The upper 32 bits contain a
    349  * "hardware id" used by the core crypto code to identify the driver and
    350  * a copy of the driver's capabilities that can be used by client code to
    351  * optimize operation.
    352  */
    353 #define	CRYPTO_SESID2HID(_sid)	(((_sid) >> 32) & 0xffffff)
    354 #define	CRYPTO_SESID2CAPS(_sid)	(((_sid) >> 56) & 0xff)
    355 #define	CRYPTO_SESID2LID(_sid)	(((u_int32_t) (_sid)) & 0xffffffff)
    356 
    357 MALLOC_DECLARE(M_CRYPTO_DATA);
    358 
    359 extern	int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard);
    360 extern	int crypto_freesession(u_int64_t sid);
    361 extern	int32_t crypto_get_driverid(u_int32_t flags);
    362 extern	int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
    363 	    u_int32_t flags,
    364 	    int (*newses)(void*, u_int32_t*, struct cryptoini*),
    365 	    int (*freeses)(void*, u_int64_t),
    366 	    int (*process)(void*, struct cryptop *, int),
    367 	    void *arg);
    368 extern	int crypto_kregister(u_int32_t, int, u_int32_t,
    369 	    int (*)(void*, struct cryptkop *, int),
    370 	    void *arg);
    371 extern	int crypto_unregister(u_int32_t driverid, int alg);
    372 extern	int crypto_unregister_all(u_int32_t driverid);
    373 extern	int crypto_dispatch(struct cryptop *crp);
    374 extern	int crypto_kdispatch(struct cryptkop *);
    375 #define	CRYPTO_SYMQ	0x1
    376 #define	CRYPTO_ASYMQ	0x2
    377 extern	int crypto_unblock(u_int32_t, int);
    378 extern	void crypto_done(struct cryptop *crp);
    379 extern	void crypto_kdone(struct cryptkop *);
    380 extern	int crypto_getfeat(int *);
    381 
    382 void	cuio_copydata(struct uio *, int, int, void *);
    383 void	cuio_copyback(struct uio *, int, int, void *);
    384 int	cuio_apply(struct uio *, int, int,
    385 	    int (*f)(void *, void *, unsigned int), void *);
    386 
    387 extern	void crypto_freereq(struct cryptop *crp);
    388 extern	struct cryptop *crypto_getreq(int num);
    389 
    390 extern	int crypto_usercrypto;		/* userland may do crypto requests */
    391 extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */
    392 extern	int crypto_devallowsoft;	/* only use hardware crypto */
    393 
    394 
    395 /*
    396  * initialize the crypto framework subsystem (not the pseudo-device).
    397  * This must be called very early in boot, so the framework is ready
    398  * to handle registration requests when crpto hardware is autoconfigured.
    399  * (This declaration doesnt really belong here but there's no header
    400  * for the raw framework.)
    401  */
    402 void	crypto_init(void);
    403 
    404 /*
    405  * Crypto-related utility routines used mainly by drivers.
    406  *
    407  * XXX these don't really belong here; but for now they're
    408  *     kept apart from the rest of the system.
    409  */
    410 struct mbuf;
    411 struct	mbuf	*m_getptr(struct mbuf *, int, int *);
    412 
    413 struct uio;
    414 extern	void cuio_copydata(struct uio* uio, int off, int len, void *cp);
    415 extern	void cuio_copyback(struct uio* uio, int off, int len, void *cp);
    416 #ifdef __FreeBSD__
    417 extern struct iovec *cuio_getptr(struct uio *uio, int loc, int *off);
    418 #else
    419 extern int	cuio_getptr(struct uio *, int loc, int *off);
    420 #endif
    421 
    422 #ifdef __FreeBSD__	/* Standalone m_apply()/m_getptr() */
    423 extern  int m_apply(struct mbuf *m, int off, int len,
    424                     int (*f)(void *, void *, unsigned int), void *fstate);
    425 extern  struct mbuf * m_getptr(struct mbuf *m, int loc, int *off);
    426 #endif	/* Standalone m_apply()/m_getptr() */
    427 
    428 #endif /* _KERNEL */
    429 #endif /* _CRYPTO_CRYPTO_H_ */
    430