cryptodev.h revision 159227
1/*	$FreeBSD: head/sys/opencrypto/cryptodev.h 159227 2006-06-04 14:20:47Z pjd $	*/
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/* Hash values */
65#define	NULL_HASH_LEN		16
66#define	MD5_HASH_LEN		16
67#define	SHA1_HASH_LEN		20
68#define	RIPEMD160_HASH_LEN	20
69#define	SHA2_256_HASH_LEN	32
70#define	SHA2_384_HASH_LEN	48
71#define	SHA2_512_HASH_LEN	64
72#define	MD5_KPDK_HASH_LEN	16
73#define	SHA1_KPDK_HASH_LEN	20
74
75/* HMAC values */
76#define	NULL_HMAC_BLOCK_LEN		64
77#define	MD5_HMAC_BLOCK_LEN		64
78#define	SHA1_HMAC_BLOCK_LEN		64
79#define	RIPEMD160_HMAC_BLOCK_LEN	64
80#define	SHA2_256_HMAC_BLOCK_LEN		64
81#define	SHA2_384_HMAC_BLOCK_LEN		128
82#define	SHA2_512_HMAC_BLOCK_LEN		128
83#define	HMAC_BLOCK_LEN			64
84#define	HMAC_BLOCK_MAXLEN		128
85#define	HMAC_IPAD_VAL			0x36
86#define	HMAC_OPAD_VAL			0x5C
87
88/* Encryption algorithm block sizes */
89#define NULL_BLOCK_LEN		4
90#define DES_BLOCK_LEN		8
91#define DES3_BLOCK_LEN		8
92#define BLOWFISH_BLOCK_LEN	8
93#define SKIPJACK_BLOCK_LEN	8
94#define CAST128_BLOCK_LEN	8
95#define RIJNDAEL128_BLOCK_LEN	16
96#define AES_BLOCK_LEN		RIJNDAEL128_BLOCK_LEN
97#define EALG_MAX_BLOCK_LEN	16 /* Keep this updated */
98
99/* Maximum hash algorithm result length */
100#define AALG_MAX_RESULT_LEN	64 /* Keep this updated */
101
102#define	CRYPTO_ALGORITHM_MIN	1
103#define CRYPTO_DES_CBC		1
104#define CRYPTO_3DES_CBC		2
105#define CRYPTO_BLF_CBC		3
106#define CRYPTO_CAST_CBC		4
107#define CRYPTO_SKIPJACK_CBC	5
108#define CRYPTO_MD5_HMAC		6
109#define CRYPTO_SHA1_HMAC	7
110#define CRYPTO_RIPEMD160_HMAC	8
111#define CRYPTO_MD5_KPDK		9
112#define CRYPTO_SHA1_KPDK	10
113#define CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */
114#define CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */
115#define CRYPTO_ARC4		12
116#define	CRYPTO_MD5		13
117#define	CRYPTO_SHA1		14
118#define	CRYPTO_NULL_HMAC	15
119#define	CRYPTO_NULL_CBC		16
120#define	CRYPTO_DEFLATE_COMP	17 /* Deflate compression algorithm */
121#define	CRYPTO_SHA2_256_HMAC	18
122#define	CRYPTO_SHA2_384_HMAC	19
123#define	CRYPTO_SHA2_512_HMAC	20
124#define	CRYPTO_ALGORITHM_MAX	20 /* Keep updated - see below */
125
126/* Algorithm flags */
127#define	CRYPTO_ALG_FLAG_SUPPORTED	0x01 /* Algorithm is supported */
128#define	CRYPTO_ALG_FLAG_RNG_ENABLE	0x02 /* Has HW RNG for DH/DSA */
129#define	CRYPTO_ALG_FLAG_DSA_SHA		0x04 /* Can do SHA on msg */
130
131struct session_op {
132	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
133	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
134
135	u_int32_t	keylen;		/* cipher key */
136	caddr_t		key;
137	int		mackeylen;	/* mac key */
138	caddr_t		mackey;
139
140  	u_int32_t	ses;		/* returns: session # */
141};
142
143struct crypt_op {
144	u_int32_t	ses;
145	u_int16_t	op;		/* i.e. COP_ENCRYPT */
146#define COP_ENCRYPT	1
147#define COP_DECRYPT	2
148	u_int16_t	flags;
149#define	COP_F_BATCH	0x0008		/* Batch op if possible */
150	u_int		len;
151	caddr_t		src, dst;	/* become iov[] inside kernel */
152	caddr_t		mac;		/* must be big enough for chosen MAC */
153	caddr_t		iv;
154};
155
156#define CRYPTO_MAX_MAC_LEN	20
157
158/* bignum parameter, in packed bytes, ... */
159struct crparam {
160	caddr_t		crp_p;
161	u_int		crp_nbits;
162};
163
164#define CRK_MAXPARAM	8
165
166struct crypt_kop {
167	u_int		crk_op;		/* ie. CRK_MOD_EXP or other */
168	u_int		crk_status;	/* return status */
169	u_short		crk_iparams;	/* # of input parameters */
170	u_short		crk_oparams;	/* # of output parameters */
171	u_int		crk_pad1;
172	struct crparam	crk_param[CRK_MAXPARAM];
173};
174#define	CRK_ALGORITM_MIN	0
175#define CRK_MOD_EXP		0
176#define CRK_MOD_EXP_CRT		1
177#define CRK_DSA_SIGN		2
178#define CRK_DSA_VERIFY		3
179#define CRK_DH_COMPUTE_KEY	4
180#define CRK_ALGORITHM_MAX	4 /* Keep updated - see below */
181
182#define CRF_MOD_EXP		(1 << CRK_MOD_EXP)
183#define CRF_MOD_EXP_CRT		(1 << CRK_MOD_EXP_CRT)
184#define CRF_DSA_SIGN		(1 << CRK_DSA_SIGN)
185#define CRF_DSA_VERIFY		(1 << CRK_DSA_VERIFY)
186#define CRF_DH_COMPUTE_KEY	(1 << CRK_DH_COMPUTE_KEY)
187
188/*
189 * done against open of /dev/crypto, to get a cloned descriptor.
190 * Please use F_SETFD against the cloned descriptor.
191 */
192#define	CRIOGET		_IOWR('c', 100, u_int32_t)
193
194/* the following are done against the cloned descriptor */
195#define	CIOCGSESSION	_IOWR('c', 101, struct session_op)
196#define	CIOCFSESSION	_IOW('c', 102, u_int32_t)
197#define CIOCCRYPT	_IOWR('c', 103, struct crypt_op)
198#define CIOCKEY		_IOWR('c', 104, struct crypt_kop)
199
200#define CIOCASYMFEAT	_IOR('c', 105, u_int32_t)
201
202struct cryptotstat {
203	struct timespec	acc;		/* total accumulated time */
204	struct timespec	min;		/* min time */
205	struct timespec	max;		/* max time */
206	u_int32_t	count;		/* number of observations */
207};
208
209struct cryptostats {
210	u_int32_t	cs_ops;		/* symmetric crypto ops submitted */
211	u_int32_t	cs_errs;	/* symmetric crypto ops that failed */
212	u_int32_t	cs_kops;	/* asymetric/key ops submitted */
213	u_int32_t	cs_kerrs;	/* asymetric/key ops that failed */
214	u_int32_t	cs_intrs;	/* crypto swi thread activations */
215	u_int32_t	cs_rets;	/* crypto return thread activations */
216	u_int32_t	cs_blocks;	/* symmetric op driver block */
217	u_int32_t	cs_kblocks;	/* symmetric op driver block */
218	/*
219	 * When CRYPTO_TIMING is defined at compile time and the
220	 * sysctl debug.crypto is set to 1, the crypto system will
221	 * accumulate statistics about how long it takes to process
222	 * crypto requests at various points during processing.
223	 */
224	struct cryptotstat cs_invoke;	/* crypto_dipsatch -> crypto_invoke */
225	struct cryptotstat cs_done;	/* crypto_invoke -> crypto_done */
226	struct cryptotstat cs_cb;	/* crypto_done -> callback */
227	struct cryptotstat cs_finis;	/* callback -> callback return */
228};
229
230#ifdef _KERNEL
231/* Standard initialization structure beginning */
232struct cryptoini {
233	int		cri_alg;	/* Algorithm to use */
234	int		cri_klen;	/* Key length, in bits */
235	int		cri_mlen;	/* Number of bytes we want from the
236					   entire hash. 0 means all. */
237	caddr_t		cri_key;	/* key to use */
238	u_int8_t	cri_iv[EALG_MAX_BLOCK_LEN];	/* IV to use */
239	struct cryptoini *cri_next;
240};
241
242/* Describe boundaries of a single crypto operation */
243struct cryptodesc {
244	int		crd_skip;	/* How many bytes to ignore from start */
245	int		crd_len;	/* How many bytes to process */
246	int		crd_inject;	/* Where to inject results, if applicable */
247	int		crd_flags;
248
249#define	CRD_F_ENCRYPT		0x01	/* Set when doing encryption */
250#define	CRD_F_IV_PRESENT	0x02	/* When encrypting, IV is already in
251					   place, so don't copy. */
252#define	CRD_F_IV_EXPLICIT	0x04	/* IV explicitly provided */
253#define	CRD_F_DSA_SHA_NEEDED	0x08	/* Compute SHA-1 of buffer for DSA */
254#define	CRD_F_KEY_EXPLICIT	0x10	/* Key explicitly provided */
255#define CRD_F_COMP		0x0f    /* Set when doing compression */
256
257	struct cryptoini	CRD_INI; /* Initialization/context data */
258#define crd_iv		CRD_INI.cri_iv
259#define crd_key		CRD_INI.cri_key
260#define crd_alg		CRD_INI.cri_alg
261#define crd_klen	CRD_INI.cri_klen
262
263	struct cryptodesc *crd_next;
264};
265
266/* Structure describing complete operation */
267struct cryptop {
268	TAILQ_ENTRY(cryptop) crp_next;
269
270	u_int64_t	crp_sid;	/* Session ID */
271	int		crp_ilen;	/* Input data total length */
272	int		crp_olen;	/* Result total length */
273
274	int		crp_etype;	/*
275					 * Error type (zero means no error).
276					 * All error codes except EAGAIN
277					 * indicate possible data corruption (as in,
278					 * the data have been touched). On all
279					 * errors, the crp_sid may have changed
280					 * (reset to a new one), so the caller
281					 * should always check and use the new
282					 * value on future requests.
283					 */
284	int		crp_flags;
285
286#define CRYPTO_F_IMBUF		0x0001	/* Input/output are mbuf chains */
287#define CRYPTO_F_IOV		0x0002	/* Input/output are uio */
288#define CRYPTO_F_REL		0x0004	/* Must return data in same place */
289#define	CRYPTO_F_BATCH		0x0008	/* Batch op if possible */
290#define	CRYPTO_F_CBIMM		0x0010	/* Do callback immediately */
291#define	CRYPTO_F_DONE		0x0020	/* Operation completed */
292#define	CRYPTO_F_CBIFSYNC	0x0040	/* Do CBIMM if op is synchronous */
293
294	caddr_t		crp_buf;	/* Data to be processed */
295	caddr_t		crp_opaque;	/* Opaque pointer, passed along */
296	struct cryptodesc *crp_desc;	/* Linked list of processing descriptors */
297
298	int (*crp_callback)(struct cryptop *); /* Callback function */
299
300	struct bintime	crp_tstamp;	/* performance time stamp */
301};
302
303#define CRYPTO_BUF_CONTIG	0x0
304#define CRYPTO_BUF_IOV		0x1
305#define CRYPTO_BUF_MBUF		0x2
306
307#define CRYPTO_OP_DECRYPT	0x0
308#define CRYPTO_OP_ENCRYPT	0x1
309
310/*
311 * Hints passed to process methods.
312 */
313#define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */
314
315struct cryptkop {
316	TAILQ_ENTRY(cryptkop) krp_next;
317
318	u_int		krp_op;		/* ie. CRK_MOD_EXP or other */
319	u_int		krp_status;	/* return status */
320	u_short		krp_iparams;	/* # of input parameters */
321	u_short		krp_oparams;	/* # of output parameters */
322	u_int32_t	krp_hid;
323	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */
324	int		(*krp_callback)(struct cryptkop *);
325};
326
327/*
328 * Crypto capabilities structure.
329 *
330 * Synchronization:
331 * (d) - protected by CRYPTO_DRIVER_LOCK()
332 * (q) - protected by CRYPTO_Q_LOCK()
333 * Not tagged fields are read-only.
334 */
335struct cryptocap {
336	u_int32_t	cc_sessions;		/* (d) number of sessions */
337	u_int32_t	cc_koperations;		/* (d) number os asym operations */
338
339	/*
340	 * Largest possible operator length (in bits) for each type of
341	 * encryption algorithm.
342	 */
343	u_int16_t	cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
344
345	u_int8_t	cc_alg[CRYPTO_ALGORITHM_MAX + 1];
346
347	u_int8_t	cc_kalg[CRK_ALGORITHM_MAX + 1];
348
349	u_int8_t	cc_flags;		/* (d) flags */
350#define CRYPTOCAP_F_CLEANUP	0x01		/* needs resource cleanup */
351#define CRYPTOCAP_F_SOFTWARE	0x02		/* software implementation */
352#define CRYPTOCAP_F_SYNC	0x04		/* operates synchronously */
353	u_int8_t	cc_qblocked;		/* (q) symmetric q blocked */
354	u_int8_t	cc_kqblocked;		/* (q) asymmetric q blocked */
355
356	void		*cc_arg;		/* callback argument */
357	int		(*cc_newsession)(void*, u_int32_t*, struct cryptoini*);
358	int		(*cc_process)(void*, struct cryptop *, int);
359	int		(*cc_freesession)(void*, u_int64_t);
360	void		*cc_karg;		/* callback argument */
361	int		(*cc_kprocess) (void*, struct cryptkop *, int);
362};
363
364/*
365 * Session ids are 64 bits.  The lower 32 bits contain a "local id" which
366 * is a driver-private session identifier.  The upper 32 bits contain a
367 * "hardware id" used by the core crypto code to identify the driver and
368 * a copy of the driver's capabilities that can be used by client code to
369 * optimize operation.
370 */
371#define	CRYPTO_SESID2HID(_sid)	(((_sid) >> 32) & 0xffffff)
372#define	CRYPTO_SESID2CAPS(_sid)	(((_sid) >> 56) & 0xff)
373#define	CRYPTO_SESID2LID(_sid)	(((u_int32_t) (_sid)) & 0xffffffff)
374
375MALLOC_DECLARE(M_CRYPTO_DATA);
376
377extern	int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard);
378extern	int crypto_freesession(u_int64_t sid);
379extern	int32_t crypto_get_driverid(u_int32_t flags);
380extern	int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
381	    u_int32_t flags,
382	    int (*newses)(void*, u_int32_t*, struct cryptoini*),
383	    int (*freeses)(void*, u_int64_t),
384	    int (*process)(void*, struct cryptop *, int),
385	    void *arg);
386extern	int crypto_kregister(u_int32_t, int, u_int32_t,
387	    int (*)(void*, struct cryptkop *, int),
388	    void *arg);
389extern	int crypto_unregister(u_int32_t driverid, int alg);
390extern	int crypto_unregister_all(u_int32_t driverid);
391extern	int crypto_dispatch(struct cryptop *crp);
392extern	int crypto_kdispatch(struct cryptkop *);
393#define	CRYPTO_SYMQ	0x1
394#define	CRYPTO_ASYMQ	0x2
395extern	int crypto_unblock(u_int32_t, int);
396extern	void crypto_done(struct cryptop *crp);
397extern	void crypto_kdone(struct cryptkop *);
398extern	int crypto_getfeat(int *);
399
400extern	void crypto_freereq(struct cryptop *crp);
401extern	struct cryptop *crypto_getreq(int num);
402
403extern	int crypto_usercrypto;		/* userland may do crypto requests */
404extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */
405extern	int crypto_devallowsoft;	/* only use hardware crypto */
406
407/*
408 * Crypto-related utility routines used mainly by drivers.
409 *
410 * XXX these don't really belong here; but for now they're
411 *     kept apart from the rest of the system.
412 */
413struct uio;
414extern	void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp);
415extern	void cuio_copyback(struct uio* uio, int off, int len, caddr_t cp);
416extern	struct iovec *cuio_getptr(struct uio *uio, int loc, int *off);
417extern	int cuio_apply(struct uio *uio, int off, int len,
418	    int (*f)(void *, void *, u_int), void *arg);
419#endif /* _KERNEL */
420#endif /* _CRYPTO_CRYPTO_H_ */
421