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