cryptodev.h revision 1.11 1 /* $NetBSD: cryptodev.h,v 1.11 2008/02/02 02:39:00 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_MD5_HMAC_96 19
103 #define CRYPTO_SHA1_HMAC_96 20
104 #define CRYPTO_RIPEMD160_HMAC_96 21
105 #define CRYPTO_ALGORITHM_MAX 22 /* Keep updated - see below */
106
107 /* Algorithm flags */
108 #define CRYPTO_ALG_FLAG_SUPPORTED 0x01 /* Algorithm is supported */
109 #define CRYPTO_ALG_FLAG_RNG_ENABLE 0x02 /* Has HW RNG for DH/DSA */
110 #define CRYPTO_ALG_FLAG_DSA_SHA 0x04 /* Can do SHA on msg */
111
112 struct session_op {
113 u_int32_t cipher; /* ie. CRYPTO_DES_CBC */
114 u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */
115
116 u_int32_t keylen; /* cipher key */
117 void * key;
118 int mackeylen; /* mac key */
119 void * mackey;
120
121 u_int32_t ses; /* returns: session # */
122 };
123
124 struct crypt_op {
125 u_int32_t ses;
126 u_int16_t op; /* i.e. COP_ENCRYPT */
127 #define COP_ENCRYPT 1
128 #define COP_DECRYPT 2
129 u_int16_t flags;
130 #define COP_F_BATCH 0x0008 /* Dispatch as quickly as possible */
131 u_int len;
132 void * src, *dst; /* become iov[] inside kernel */
133 void * mac; /* must be big enough for chosen MAC */
134 void * iv;
135 };
136
137 #define CRYPTO_MAX_MAC_LEN 20
138
139 /* bignum parameter, in packed bytes, ... */
140 struct crparam {
141 void * crp_p;
142 u_int crp_nbits;
143 };
144
145 #define CRK_MAXPARAM 8
146
147 struct crypt_kop {
148 u_int crk_op; /* ie. CRK_MOD_EXP or other */
149 u_int crk_status; /* return status */
150 u_short crk_iparams; /* # of input parameters */
151 u_short crk_oparams; /* # of output parameters */
152 u_int crk_pad1;
153 struct crparam crk_param[CRK_MAXPARAM];
154 };
155 #define CRK_ALGORITM_MIN 0
156 #define CRK_MOD_EXP 0
157 #define CRK_MOD_EXP_CRT 1
158 #define CRK_DSA_SIGN 2
159 #define CRK_DSA_VERIFY 3
160 #define CRK_DH_COMPUTE_KEY 4
161 #define CRK_MOD_ADD 5
162 #define CRK_MOD_ADDINV 6
163 #define CRK_MOD_SUB 7
164 #define CRK_MOD_MULT 8
165 #define CRK_MOD_MULTINV 9
166 #define CRK_MOD 10
167 #define CRK_ALGORITHM_MAX 10 /* Keep updated - see below */
168
169 #define CRF_MOD_EXP (1 << CRK_MOD_EXP)
170 #define CRF_MOD_EXP_CRT (1 << CRK_MOD_EXP_CRT)
171 #define CRF_DSA_SIGN (1 << CRK_DSA_SIGN)
172 #define CRF_DSA_VERIFY (1 << CRK_DSA_VERIFY)
173 #define CRF_DH_COMPUTE_KEY (1 << CRK_DH_COMPUTE_KEY)
174 #define CRF_MOD_ADD (1 << CRK_MOD_ADD)
175 #define CRF_MOD_ADDINV (1 << CRK_MOD_ADDINV)
176 #define CRF_MOD_SUB (1 << CRK_MOD_SUB)
177 #define CRF_MOD_MULT (1 << CRK_MOD_MULT)
178 #define CRF_MOD_MULTINV (1 << CRK_MOD_MULTINV)
179 #define CRF_MOD (1 << CRK_MOD)
180
181 /*
182 * done against open of /dev/crypto, to get a cloned descriptor.
183 * Please use F_SETFD against the cloned descriptor.
184 */
185 #define CRIOGET _IOWR('c', 100, u_int32_t)
186
187 /* the following are done against the cloned descriptor */
188 #define CIOCGSESSION _IOWR('c', 101, struct session_op)
189 #define CIOCFSESSION _IOW('c', 102, u_int32_t)
190 #define CIOCCRYPT _IOWR('c', 103, struct crypt_op)
191 #define CIOCKEY _IOWR('c', 104, struct crypt_kop)
192
193 #define CIOCASYMFEAT _IOR('c', 105, u_int32_t)
194
195 struct cryptotstat {
196 struct timespec acc; /* total accumulated time */
197 struct timespec min; /* max time */
198 struct timespec max; /* max time */
199 u_int32_t count; /* number of observations */
200 };
201
202 struct cryptostats {
203 u_int32_t cs_ops; /* symmetric crypto ops submitted */
204 u_int32_t cs_errs; /* symmetric crypto ops that failed */
205 u_int32_t cs_kops; /* asymetric/key ops submitted */
206 u_int32_t cs_kerrs; /* asymetric/key ops that failed */
207 u_int32_t cs_intrs; /* crypto swi thread activations */
208 u_int32_t cs_rets; /* crypto return thread activations */
209 u_int32_t cs_blocks; /* symmetric op driver block */
210 u_int32_t cs_kblocks; /* symmetric op driver block */
211 /*
212 * When CRYPTO_TIMING is defined at compile time and the
213 * sysctl debug.crypto is set to 1, the crypto system will
214 * accumulate statistics about how long it takes to process
215 * crypto requests at various points during processing.
216 */
217 struct cryptotstat cs_invoke; /* crypto_dipsatch -> crypto_invoke */
218 struct cryptotstat cs_done; /* crypto_invoke -> crypto_done */
219 struct cryptotstat cs_cb; /* crypto_done -> callback */
220 struct cryptotstat cs_finis; /* callback -> callback return */
221 };
222
223 #ifdef _KERNEL
224 /* Standard initialization structure beginning */
225 struct cryptoini {
226 int cri_alg; /* Algorithm to use */
227 int cri_klen; /* Key length, in bits */
228 int cri_rnd; /* Algorithm rounds, where relevant */
229 char *cri_key; /* key to use */
230 u_int8_t cri_iv[EALG_MAX_BLOCK_LEN]; /* IV to use */
231 struct cryptoini *cri_next;
232 };
233
234 /* Describe boundaries of a single crypto operation */
235 struct cryptodesc {
236 int crd_skip; /* How many bytes to ignore from start */
237 int crd_len; /* How many bytes to process */
238 int crd_inject; /* Where to inject results, if applicable */
239 int crd_flags;
240
241 #define CRD_F_ENCRYPT 0x01 /* Set when doing encryption */
242 #define CRD_F_IV_PRESENT 0x02 /* When encrypting, IV is already in
243 place, so don't copy. */
244 #define CRD_F_IV_EXPLICIT 0x04 /* IV explicitly provided */
245 #define CRD_F_DSA_SHA_NEEDED 0x08 /* Compute SHA-1 of buffer for DSA */
246 #define CRD_F_COMP 0x0f /* Set when doing compression */
247
248 struct cryptoini CRD_INI; /* Initialization/context data */
249 #define crd_iv CRD_INI.cri_iv
250 #define crd_key CRD_INI.cri_key
251 #define crd_rnd CRD_INI.cri_rnd
252 #define crd_alg CRD_INI.cri_alg
253 #define crd_klen CRD_INI.cri_klen
254
255 struct cryptodesc *crd_next;
256 };
257
258 /* Structure describing complete operation */
259 struct cryptop {
260 TAILQ_ENTRY(cryptop) crp_next;
261
262 u_int64_t crp_sid; /* Session ID */
263 int crp_ilen; /* Input data total length */
264 int crp_olen; /* Result total length */
265
266 int crp_etype; /*
267 * Error type (zero means no error).
268 * All error codes except EAGAIN
269 * indicate possible data corruption (as in,
270 * the data have been touched). On all
271 * errors, the crp_sid may have changed
272 * (reset to a new one), so the caller
273 * should always check and use the new
274 * value on future requests.
275 */
276 int crp_flags;
277
278 #define CRYPTO_F_IMBUF 0x0001 /* Input/output are mbuf chains */
279 #define CRYPTO_F_IOV 0x0002 /* Input/output are uio */
280 #define CRYPTO_F_REL 0x0004 /* Must return data in same place */
281 #define CRYPTO_F_BATCH 0x0008 /* Batch op if possible possible */
282 #define CRYPTO_F_CBIMM 0x0010 /* Do callback immediately */
283 #define CRYPTO_F_DONE 0x0020 /* Operation completed */
284 #define CRYPTO_F_CBIFSYNC 0x0040 /* Do CBIMM if op is synchronous */
285
286 void * crp_buf; /* Data to be processed */
287 void * crp_opaque; /* Opaque pointer, passed along */
288 struct cryptodesc *crp_desc; /* Linked list of processing descriptors */
289
290 int (*crp_callback)(struct cryptop *); /* Callback function */
291
292 void * crp_mac;
293 struct timespec crp_tstamp; /* performance time stamp */
294 };
295
296 #define CRYPTO_BUF_CONTIG 0x0
297 #define CRYPTO_BUF_IOV 0x1
298 #define CRYPTO_BUF_MBUF 0x2
299
300 #define CRYPTO_OP_DECRYPT 0x0
301 #define CRYPTO_OP_ENCRYPT 0x1
302
303 /*
304 * Hints passed to process methods.
305 */
306 #define CRYPTO_HINT_MORE 0x1 /* more ops coming shortly */
307
308 struct cryptkop {
309 TAILQ_ENTRY(cryptkop) krp_next;
310
311 u_int krp_op; /* ie. CRK_MOD_EXP or other */
312 u_int krp_status; /* return status */
313 u_short krp_iparams; /* # of input parameters */
314 u_short krp_oparams; /* # of output parameters */
315 u_int32_t krp_hid;
316 struct crparam krp_param[CRK_MAXPARAM]; /* kvm */
317 int (*krp_callback)(struct cryptkop *);
318 };
319
320 /* Crypto capabilities structure */
321 struct cryptocap {
322 u_int32_t cc_sessions;
323
324 /*
325 * Largest possible operator length (in bits) for each type of
326 * encryption algorithm.
327 */
328 u_int16_t cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
329
330 u_int8_t cc_alg[CRYPTO_ALGORITHM_MAX + 1];
331
332 u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1];
333
334 u_int8_t cc_flags;
335 u_int8_t cc_qblocked; /* symmetric q blocked */
336 u_int8_t cc_kqblocked; /* asymmetric q blocked */
337 #define CRYPTOCAP_F_CLEANUP 0x01 /* needs resource cleanup */
338 #define CRYPTOCAP_F_SOFTWARE 0x02 /* software implementation */
339 #define CRYPTOCAP_F_SYNC 0x04 /* operates synchronously */
340
341 void *cc_arg; /* callback argument */
342 int (*cc_newsession)(void*, u_int32_t*, struct cryptoini*);
343 int (*cc_process) (void*, struct cryptop *, int);
344 int (*cc_freesession) (void*, u_int64_t);
345 void *cc_karg; /* callback argument */
346 int (*cc_kprocess) (void*, struct cryptkop *, int);
347 };
348
349 /*
350 * Session ids are 64 bits. The lower 32 bits contain a "local id" which
351 * is a driver-private session identifier. The upper 32 bits contain a
352 * "hardware id" used by the core crypto code to identify the driver and
353 * a copy of the driver's capabilities that can be used by client code to
354 * optimize operation.
355 */
356 #define CRYPTO_SESID2HID(_sid) (((_sid) >> 32) & 0xffffff)
357 #define CRYPTO_SESID2CAPS(_sid) (((_sid) >> 56) & 0xff)
358 #define CRYPTO_SESID2LID(_sid) (((u_int32_t) (_sid)) & 0xffffffff)
359
360 MALLOC_DECLARE(M_CRYPTO_DATA);
361
362 extern int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard);
363 extern int crypto_freesession(u_int64_t sid);
364 extern int32_t crypto_get_driverid(u_int32_t flags);
365 extern int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
366 u_int32_t flags,
367 int (*newses)(void*, u_int32_t*, struct cryptoini*),
368 int (*freeses)(void*, u_int64_t),
369 int (*process)(void*, struct cryptop *, int),
370 void *arg);
371 extern int crypto_kregister(u_int32_t, int, u_int32_t,
372 int (*)(void*, struct cryptkop *, int),
373 void *arg);
374 extern int crypto_unregister(u_int32_t driverid, int alg);
375 extern int crypto_unregister_all(u_int32_t driverid);
376 extern int crypto_dispatch(struct cryptop *crp);
377 extern int crypto_kdispatch(struct cryptkop *);
378 #define CRYPTO_SYMQ 0x1
379 #define CRYPTO_ASYMQ 0x2
380 extern int crypto_unblock(u_int32_t, int);
381 extern void crypto_done(struct cryptop *crp);
382 extern void crypto_kdone(struct cryptkop *);
383 extern int crypto_getfeat(int *);
384
385 void cuio_copydata(struct uio *, int, int, void *);
386 void cuio_copyback(struct uio *, int, int, void *);
387 int cuio_apply(struct uio *, int, int,
388 int (*f)(void *, void *, unsigned int), void *);
389
390 extern void crypto_freereq(struct cryptop *crp);
391 extern struct cryptop *crypto_getreq(int num);
392
393 extern int crypto_usercrypto; /* userland may do crypto requests */
394 extern int crypto_userasymcrypto; /* userland may do asym crypto reqs */
395 extern int crypto_devallowsoft; /* only use hardware crypto */
396
397
398 /*
399 * initialize the crypto framework subsystem (not the pseudo-device).
400 * This must be called very early in boot, so the framework is ready
401 * to handle registration requests when crpto hardware is autoconfigured.
402 * (This declaration doesnt really belong here but there's no header
403 * for the raw framework.)
404 */
405 void crypto_init(void);
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 */
413 struct mbuf;
414 struct mbuf *m_getptr(struct mbuf *, int, int *);
415
416 struct uio;
417 extern void cuio_copydata(struct uio* uio, int off, int len, void *cp);
418 extern void cuio_copyback(struct uio* uio, int off, int len, void *cp);
419 extern int cuio_getptr(struct uio *, int loc, int *off);
420
421
422 #endif /* _KERNEL */
423 #endif /* _CRYPTO_CRYPTO_H_ */
424