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