2 /* $OpenBSD: cryptodev.h,v 1.31 2002/06/11 11:14:29 beck Exp $ */
5 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
6 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
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.
12 * Copyright (c) 2000 Angelos D. Keromytis
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.
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
25 * Copyright (c) 2001 Theo de Raadt
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
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.
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.
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.
56 #ifndef _CRYPTO_CRYPTO_H_
57 #define _CRYPTO_CRYPTO_H_
59 #include <sys/ioccom.h>
61 /* Some initial values */
62 #define CRYPTO_DRIVERS_INITIAL 4
63 #define CRYPTO_SW_SESSIONS 32
66 #define NULL_HASH_LEN 16
67 #define MD5_HASH_LEN 16
68 #define SHA1_HASH_LEN 20
69 #define RIPEMD160_HASH_LEN 20
70 #define SHA2_256_HASH_LEN 32
71 #define SHA2_384_HASH_LEN 48
72 #define SHA2_512_HASH_LEN 64
73 #define MD5_KPDK_HASH_LEN 16
74 #define SHA1_KPDK_HASH_LEN 20
75 /* Maximum hash algorithm result length */
76 #define HASH_MAX_LEN SHA2_512_HASH_LEN /* Keep this updated */
79 #define NULL_HMAC_BLOCK_LEN 64
80 #define MD5_HMAC_BLOCK_LEN 64
81 #define SHA1_HMAC_BLOCK_LEN 64
82 #define RIPEMD160_HMAC_BLOCK_LEN 64
83 #define SHA2_256_HMAC_BLOCK_LEN 64
84 #define SHA2_384_HMAC_BLOCK_LEN 128
85 #define SHA2_512_HMAC_BLOCK_LEN 128
86 /* Maximum HMAC block length */
87 #define HMAC_MAX_BLOCK_LEN SHA2_512_HMAC_BLOCK_LEN /* Keep this updated */
88 #define HMAC_IPAD_VAL 0x36
89 #define HMAC_OPAD_VAL 0x5C
91 /* Encryption algorithm block sizes */
92 #define NULL_BLOCK_LEN 4
93 #define DES_BLOCK_LEN 8
94 #define DES3_BLOCK_LEN 8
95 #define BLOWFISH_BLOCK_LEN 8
96 #define SKIPJACK_BLOCK_LEN 8
97 #define CAST128_BLOCK_LEN 8
98 #define RIJNDAEL128_BLOCK_LEN 16
99 #define AES_BLOCK_LEN RIJNDAEL128_BLOCK_LEN
100 #define CAMELLIA_BLOCK_LEN 16
101 #define EALG_MAX_BLOCK_LEN AES_BLOCK_LEN /* Keep this updated */
103 #define CRYPTO_ALGORITHM_MIN 1
104 #define CRYPTO_DES_CBC 1
105 #define CRYPTO_3DES_CBC 2
106 #define CRYPTO_BLF_CBC 3
107 #define CRYPTO_CAST_CBC 4
108 #define CRYPTO_SKIPJACK_CBC 5
109 #define CRYPTO_MD5_HMAC 6
110 #define CRYPTO_SHA1_HMAC 7
111 #define CRYPTO_RIPEMD160_HMAC 8
112 #define CRYPTO_MD5_KPDK 9
113 #define CRYPTO_SHA1_KPDK 10
114 #define CRYPTO_RIJNDAEL128_CBC 11 /* 128 bit blocksize */
115 #define CRYPTO_AES_CBC 11 /* 128 bit blocksize -- the same as above */
116 #define CRYPTO_ARC4 12
117 #define CRYPTO_MD5 13
118 #define CRYPTO_SHA1 14
119 #define CRYPTO_NULL_HMAC 15
120 #define CRYPTO_NULL_CBC 16
121 #define CRYPTO_DEFLATE_COMP 17 /* Deflate compression algorithm */
122 #define CRYPTO_SHA2_256_HMAC 18
123 #define CRYPTO_SHA2_384_HMAC 19
124 #define CRYPTO_SHA2_512_HMAC 20
125 #define CRYPTO_CAMELLIA_CBC 21
126 #define CRYPTO_ALGORITHM_MAX 21 /* Keep updated - see below */
128 /* Algorithm flags */
129 #define CRYPTO_ALG_FLAG_SUPPORTED 0x01 /* Algorithm is supported */
130 #define CRYPTO_ALG_FLAG_RNG_ENABLE 0x02 /* Has HW RNG for DH/DSA */
131 #define CRYPTO_ALG_FLAG_DSA_SHA 0x04 /* Can do SHA on msg */
134 * Crypto driver/device flags. They can set in the crid
135 * parameter when creating a session or submitting a key
136 * op to affect the device/driver assigned. If neither
137 * of these are specified then the crid is assumed to hold
138 * the driver id of an existing (and suitable) device that
139 * must be used to satisfy the request.
141 #define CRYPTO_FLAG_HARDWARE 0x01000000 /* hardware accelerated */
142 #define CRYPTO_FLAG_SOFTWARE 0x02000000 /* software implementation */
146 u_int32_t cipher; /* ie. CRYPTO_DES_CBC */
147 u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */
149 u_int32_t keylen; /* cipher key */
151 int mackeylen; /* mac key */
154 u_int32_t ses; /* returns: session # */
158 u_int32_t cipher; /* ie. CRYPTO_DES_CBC */
159 u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */
161 u_int32_t keylen; /* cipher key */
163 int mackeylen; /* mac key */
166 u_int32_t ses; /* returns: session # */
167 int crid; /* driver id + flags (rw) */
168 int pad[4]; /* for future expansion */
173 u_int16_t op; /* i.e. COP_ENCRYPT */
174 #define COP_ENCRYPT 1
175 #define COP_DECRYPT 2
177 #define COP_F_BATCH 0x0008 /* Batch op if possible */
179 caddr_t src, dst; /* become iov[] inside kernel */
180 caddr_t mac; /* must be big enough for chosen MAC */
185 * Parameters for looking up a crypto driver/device by
186 * device name or by id. The latter are returned for
187 * created sessions (crid) and completed key operations.
189 struct crypt_find_op {
190 int crid; /* driver id + flags */
191 char name[32]; /* device/driver name */
194 /* bignum parameter, in packed bytes, ... */
200 #define CRK_MAXPARAM 8
203 u_int crk_op; /* ie. CRK_MOD_EXP or other */
204 u_int crk_status; /* return status */
205 u_short crk_iparams; /* # of input parameters */
206 u_short crk_oparams; /* # of output parameters */
207 u_int crk_crid; /* NB: only used by CIOCKEY2 (rw) */
208 struct crparam crk_param[CRK_MAXPARAM];
210 #define CRK_ALGORITM_MIN 0
211 #define CRK_MOD_EXP 0
212 #define CRK_MOD_EXP_CRT 1
213 #define CRK_DSA_SIGN 2
214 #define CRK_DSA_VERIFY 3
215 #define CRK_DH_COMPUTE_KEY 4
216 #define CRK_ALGORITHM_MAX 4 /* Keep updated - see below */
218 #define CRF_MOD_EXP (1 << CRK_MOD_EXP)
219 #define CRF_MOD_EXP_CRT (1 << CRK_MOD_EXP_CRT)
220 #define CRF_DSA_SIGN (1 << CRK_DSA_SIGN)
221 #define CRF_DSA_VERIFY (1 << CRK_DSA_VERIFY)
222 #define CRF_DH_COMPUTE_KEY (1 << CRK_DH_COMPUTE_KEY)
225 * done against open of /dev/crypto, to get a cloned descriptor.
226 * Please use F_SETFD against the cloned descriptor.
228 #define CRIOGET _IOWR('c', 100, u_int32_t)
229 #define CRIOASYMFEAT CIOCASYMFEAT
230 #define CRIOFINDDEV CIOCFINDDEV
232 /* the following are done against the cloned descriptor */
233 #define CIOCGSESSION _IOWR('c', 101, struct session_op)
234 #define CIOCFSESSION _IOW('c', 102, u_int32_t)
235 #define CIOCCRYPT _IOWR('c', 103, struct crypt_op)
236 #define CIOCKEY _IOWR('c', 104, struct crypt_kop)
237 #define CIOCASYMFEAT _IOR('c', 105, u_int32_t)
238 #define CIOCGSESSION2 _IOWR('c', 106, struct session2_op)
239 #define CIOCKEY2 _IOWR('c', 107, struct crypt_kop)
240 #define CIOCFINDDEV _IOWR('c', 108, struct crypt_find_op)
243 struct timespec acc; /* total accumulated time */
244 struct timespec min; /* min time */
245 struct timespec max; /* max time */
246 u_int32_t count; /* number of observations */
250 u_int32_t cs_ops; /* symmetric crypto ops submitted */
251 u_int32_t cs_errs; /* symmetric crypto ops that failed */
252 u_int32_t cs_kops; /* asymetric/key ops submitted */
253 u_int32_t cs_kerrs; /* asymetric/key ops that failed */
254 u_int32_t cs_intrs; /* crypto swi thread activations */
255 u_int32_t cs_rets; /* crypto return thread activations */
256 u_int32_t cs_blocks; /* symmetric op driver block */
257 u_int32_t cs_kblocks; /* symmetric op driver block */
259 * When CRYPTO_TIMING is defined at compile time and the
260 * sysctl debug.crypto is set to 1, the crypto system will
261 * accumulate statistics about how long it takes to process
262 * crypto requests at various points during processing.
264 struct cryptotstat cs_invoke; /* crypto_dipsatch -> crypto_invoke */
265 struct cryptotstat cs_done; /* crypto_invoke -> crypto_done */
266 struct cryptotstat cs_cb; /* crypto_done -> callback */
267 struct cryptotstat cs_finis; /* callback -> callback return */
271 /* Standard initialization structure beginning */
273 int cri_alg; /* Algorithm to use */
274 int cri_klen; /* Key length, in bits */
275 int cri_mlen; /* Number of bytes we want from the
276 entire hash. 0 means all. */
277 caddr_t cri_key; /* key to use */
278 u_int8_t cri_iv[EALG_MAX_BLOCK_LEN]; /* IV to use */
279 struct cryptoini *cri_next;
282 /* Describe boundaries of a single crypto operation */
284 int crd_skip; /* How many bytes to ignore from start */
285 int crd_len; /* How many bytes to process */
286 int crd_inject; /* Where to inject results, if applicable */
289 #define CRD_F_ENCRYPT 0x01 /* Set when doing encryption */
290 #define CRD_F_IV_PRESENT 0x02 /* When encrypting, IV is already in
291 place, so don't copy. */
292 #define CRD_F_IV_EXPLICIT 0x04 /* IV explicitly provided */
293 #define CRD_F_DSA_SHA_NEEDED 0x08 /* Compute SHA-1 of buffer for DSA */
294 #define CRD_F_KEY_EXPLICIT 0x10 /* Key explicitly provided */
295 #define CRD_F_COMP 0x0f /* Set when doing compression */
297 struct cryptoini CRD_INI; /* Initialization/context data */
298 #define crd_iv CRD_INI.cri_iv
299 #define crd_key CRD_INI.cri_key
300 #define crd_alg CRD_INI.cri_alg
301 #define crd_klen CRD_INI.cri_klen
303 struct cryptodesc *crd_next;
306 /* Structure describing complete operation */
308 TAILQ_ENTRY(cryptop) crp_next;
310 u_int64_t crp_sid; /* Session ID */
311 int crp_ilen; /* Input data total length */
312 int crp_olen; /* Result total length */
315 * Error type (zero means no error).
316 * All error codes except EAGAIN
317 * indicate possible data corruption (as in,
318 * the data have been touched). On all
319 * errors, the crp_sid may have changed
320 * (reset to a new one), so the caller
321 * should always check and use the new
322 * value on future requests.
326 #define CRYPTO_F_IMBUF 0x0001 /* Input/output are mbuf chains */
327 #define CRYPTO_F_IOV 0x0002 /* Input/output are uio */
328 #define CRYPTO_F_REL 0x0004 /* Must return data in same place */
329 #define CRYPTO_F_BATCH 0x0008 /* Batch op if possible */
330 #define CRYPTO_F_CBIMM 0x0010 /* Do callback immediately */
331 #define CRYPTO_F_DONE 0x0020 /* Operation completed */
332 #define CRYPTO_F_CBIFSYNC 0x0040 /* Do CBIMM if op is synchronous */
334 caddr_t crp_buf; /* Data to be processed */
335 caddr_t crp_opaque; /* Opaque pointer, passed along */
336 struct cryptodesc *crp_desc; /* Linked list of processing descriptors */
338 int (*crp_callback)(struct cryptop *); /* Callback function */
340 struct bintime crp_tstamp; /* performance time stamp */
343 #define CRYPTO_BUF_CONTIG 0x0
344 #define CRYPTO_BUF_IOV 0x1
345 #define CRYPTO_BUF_MBUF 0x2
347 #define CRYPTO_OP_DECRYPT 0x0
348 #define CRYPTO_OP_ENCRYPT 0x1
351 * Hints passed to process methods.
353 #define CRYPTO_HINT_MORE 0x1 /* more ops coming shortly */
356 TAILQ_ENTRY(cryptkop) krp_next;
358 u_int krp_op; /* ie. CRK_MOD_EXP or other */
359 u_int krp_status; /* return status */
360 u_short krp_iparams; /* # of input parameters */
361 u_short krp_oparams; /* # of output parameters */
362 u_int krp_crid; /* desired device, etc. */
364 struct crparam krp_param[CRK_MAXPARAM]; /* kvm */
365 int (*krp_callback)(struct cryptkop *);
369 * Session ids are 64 bits. The lower 32 bits contain a "local id" which
370 * is a driver-private session identifier. The upper 32 bits contain a
371 * "hardware id" used by the core crypto code to identify the driver and
372 * a copy of the driver's capabilities that can be used by client code to
373 * optimize operation.
375 #define CRYPTO_SESID2HID(_sid) (((_sid) >> 32) & 0x00ffffff)
376 #define CRYPTO_SESID2CAPS(_sid) (((_sid) >> 32) & 0xff000000)
377 #define CRYPTO_SESID2LID(_sid) (((u_int32_t) (_sid)) & 0xffffffff)
379 MALLOC_DECLARE(M_CRYPTO_DATA);
381 extern int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard);
382 extern int crypto_freesession(u_int64_t sid);
383 #define CRYPTOCAP_F_HARDWARE CRYPTO_FLAG_HARDWARE
384 #define CRYPTOCAP_F_SOFTWARE CRYPTO_FLAG_SOFTWARE
385 #define CRYPTOCAP_F_SYNC 0x04000000 /* operates synchronously */
386 extern int32_t crypto_get_driverid(device_t dev, int flags);
387 extern int crypto_find_driver(const char *);
388 extern device_t crypto_find_device_byhid(int hid);
389 extern int crypto_getcaps(int hid);
390 extern int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
392 extern int crypto_kregister(u_int32_t, int, u_int32_t);
393 extern int crypto_unregister(u_int32_t driverid, int alg);
394 extern int crypto_unregister_all(u_int32_t driverid);
395 extern int crypto_dispatch(struct cryptop *crp);
396 extern int crypto_kdispatch(struct cryptkop *);
397 #define CRYPTO_SYMQ 0x1
398 #define CRYPTO_ASYMQ 0x2
399 extern int crypto_unblock(u_int32_t, int);
400 extern void crypto_done(struct cryptop *crp);
401 extern void crypto_kdone(struct cryptkop *);
402 extern int crypto_getfeat(int *);
404 extern void crypto_freereq(struct cryptop *crp);
405 extern struct cryptop *crypto_getreq(int num);
407 extern int crypto_usercrypto; /* userland may do crypto requests */
408 extern int crypto_userasymcrypto; /* userland may do asym crypto reqs */
409 extern int crypto_devallowsoft; /* only use hardware crypto */
412 * Crypto-related utility routines used mainly by drivers.
414 * XXX these don't really belong here; but for now they're
415 * kept apart from the rest of the system.
418 extern void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp);
419 extern void cuio_copyback(struct uio* uio, int off, int len, caddr_t cp);
420 extern struct iovec *cuio_getptr(struct uio *uio, int loc, int *off);
421 extern int cuio_apply(struct uio *uio, int off, int len,
422 int (*f)(void *, void *, u_int), void *arg);
424 extern void crypto_copyback(int flags, caddr_t buf, int off, int size,
426 extern void crypto_copydata(int flags, caddr_t buf, int off, int size,
428 extern int crypto_apply(int flags, caddr_t buf, int off, int len,
429 int (*f)(void *, void *, u_int), void *arg);
431 #endif /* _CRYPTO_CRYPTO_H_ */