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)
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.
11 * Copyright (c) 2000 Angelos D. Keromytis
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.
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
24 * Copyright (c) 2001 Theo de Raadt
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
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.
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.
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.
55 #ifndef _CRYPTO_CRYPTO_H_
56 #define _CRYPTO_CRYPTO_H_
58 #include <sys/ioccom.h>
60 /* Some initial values */
61 #define CRYPTO_DRIVERS_INITIAL 4
62 #define CRYPTO_SW_SESSIONS 32
65 #define HMAC_BLOCK_LEN 64
66 #define HMAC_BLOCK_MAXLEN 128
67 #define HMAC_IPAD_VAL 0x36
68 #define HMAC_OPAD_VAL 0x5C
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 */
79 /* Maximum hash algorithm result length */
80 #define AALG_MAX_RESULT_LEN 64 /* Keep this updated */
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
97 #define CRYPTO_SHA1 14
98 #define CRYPTO_NULL_HMAC 15
99 #define CRYPTO_NULL_CBC 16
100 #define CRYPTO_DEFLATE_COMP 17 /* Deflate compression algorithm */
101 #define CRYPTO_SHA2_256_HMAC 18
102 #define CRYPTO_SHA2_384_HMAC 19
103 #define CRYPTO_SHA2_512_HMAC 20
104 #define CRYPTO_ALGORITHM_MAX 20 /* Keep updated - see below */
106 /* Algorithm flags */
107 #define CRYPTO_ALG_FLAG_SUPPORTED 0x01 /* Algorithm is supported */
108 #define CRYPTO_ALG_FLAG_RNG_ENABLE 0x02 /* Has HW RNG for DH/DSA */
109 #define CRYPTO_ALG_FLAG_DSA_SHA 0x04 /* Can do SHA on msg */
112 u_int32_t cipher; /* ie. CRYPTO_DES_CBC */
113 u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */
115 u_int32_t keylen; /* cipher key */
117 int mackeylen; /* mac key */
120 u_int32_t ses; /* returns: session # */
125 u_int16_t op; /* i.e. COP_ENCRYPT */
126 #define COP_ENCRYPT 1
127 #define COP_DECRYPT 2
129 #define COP_F_BATCH 0x0008 /* Batch op if possible */
131 caddr_t src, dst; /* become iov[] inside kernel */
132 caddr_t mac; /* must be big enough for chosen MAC */
136 #define CRYPTO_MAX_MAC_LEN 20
138 /* bignum parameter, in packed bytes, ... */
144 #define CRK_MAXPARAM 8
147 u_int crk_op; /* ie. CRK_MOD_EXP or other */
148 u_int crk_status; /* return status */
149 u_short crk_iparams; /* # of input parameters */
150 u_short crk_oparams; /* # of output parameters */
152 struct crparam crk_param[CRK_MAXPARAM];
154 #define CRK_ALGORITM_MIN 0
155 #define CRK_MOD_EXP 0
156 #define CRK_MOD_EXP_CRT 1
157 #define CRK_DSA_SIGN 2
158 #define CRK_DSA_VERIFY 3
159 #define CRK_DH_COMPUTE_KEY 4
160 #define CRK_ALGORITHM_MAX 4 /* Keep updated - see below */
162 #define CRF_MOD_EXP (1 << CRK_MOD_EXP)
163 #define CRF_MOD_EXP_CRT (1 << CRK_MOD_EXP_CRT)
164 #define CRF_DSA_SIGN (1 << CRK_DSA_SIGN)
165 #define CRF_DSA_VERIFY (1 << CRK_DSA_VERIFY)
166 #define CRF_DH_COMPUTE_KEY (1 << CRK_DH_COMPUTE_KEY)
169 * done against open of /dev/crypto, to get a cloned descriptor.
170 * Please use F_SETFD against the cloned descriptor.
172 #define CRIOGET _IOWR('c', 100, u_int32_t)
174 /* the following are done against the cloned descriptor */
175 #define CIOCGSESSION _IOWR('c', 101, struct session_op)
176 #define CIOCFSESSION _IOW('c', 102, u_int32_t)
177 #define CIOCCRYPT _IOWR('c', 103, struct crypt_op)
178 #define CIOCKEY _IOWR('c', 104, struct crypt_kop)
180 #define CIOCASYMFEAT _IOR('c', 105, u_int32_t)
183 struct timespec acc; /* total accumulated time */
184 struct timespec min; /* min time */
185 struct timespec max; /* max time */
186 u_int32_t count; /* number of observations */
190 u_int32_t cs_ops; /* symmetric crypto ops submitted */
191 u_int32_t cs_errs; /* symmetric crypto ops that failed */
192 u_int32_t cs_kops; /* asymetric/key ops submitted */
193 u_int32_t cs_kerrs; /* asymetric/key ops that failed */
194 u_int32_t cs_intrs; /* crypto swi thread activations */
195 u_int32_t cs_rets; /* crypto return thread activations */
196 u_int32_t cs_blocks; /* symmetric op driver block */
197 u_int32_t cs_kblocks; /* symmetric op driver block */
199 * When CRYPTO_TIMING is defined at compile time and the
200 * sysctl debug.crypto is set to 1, the crypto system will
201 * accumulate statistics about how long it takes to process
202 * crypto requests at various points during processing.
204 struct cryptotstat cs_invoke; /* crypto_dipsatch -> crypto_invoke */
205 struct cryptotstat cs_done; /* crypto_invoke -> crypto_done */
206 struct cryptotstat cs_cb; /* crypto_done -> callback */
207 struct cryptotstat cs_finis; /* callback -> callback return */
211 /* Standard initialization structure beginning */
213 int cri_alg; /* Algorithm to use */
214 int cri_klen; /* Key length, in bits */
215 int cri_mlen; /* Number of bytes we want from the
216 entire hash. 0 means all. */
217 caddr_t cri_key; /* key to use */
218 u_int8_t cri_iv[EALG_MAX_BLOCK_LEN]; /* IV to use */
219 struct cryptoini *cri_next;
222 /* Describe boundaries of a single crypto operation */
224 int crd_skip; /* How many bytes to ignore from start */
225 int crd_len; /* How many bytes to process */
226 int crd_inject; /* Where to inject results, if applicable */
229 #define CRD_F_ENCRYPT 0x01 /* Set when doing encryption */
230 #define CRD_F_IV_PRESENT 0x02 /* When encrypting, IV is already in
231 place, so don't copy. */
232 #define CRD_F_IV_EXPLICIT 0x04 /* IV explicitly provided */
233 #define CRD_F_DSA_SHA_NEEDED 0x08 /* Compute SHA-1 of buffer for DSA */
234 #define CRD_F_KEY_EXPLICIT 0x10 /* Key explicitly provided */
235 #define CRD_F_COMP 0x0f /* Set when doing compression */
237 struct cryptoini CRD_INI; /* Initialization/context data */
238 #define crd_iv CRD_INI.cri_iv
239 #define crd_key CRD_INI.cri_key
240 #define crd_alg CRD_INI.cri_alg
241 #define crd_klen CRD_INI.cri_klen
243 struct cryptodesc *crd_next;
246 /* Structure describing complete operation */
248 TAILQ_ENTRY(cryptop) crp_next;
250 u_int64_t crp_sid; /* Session ID */
251 int crp_ilen; /* Input data total length */
252 int crp_olen; /* Result total length */
255 * Error type (zero means no error).
256 * All error codes except EAGAIN
257 * indicate possible data corruption (as in,
258 * the data have been touched). On all
259 * errors, the crp_sid may have changed
260 * (reset to a new one), so the caller
261 * should always check and use the new
262 * value on future requests.
266 #define CRYPTO_F_IMBUF 0x0001 /* Input/output are mbuf chains */
267 #define CRYPTO_F_IOV 0x0002 /* Input/output are uio */
268 #define CRYPTO_F_REL 0x0004 /* Must return data in same place */
269 #define CRYPTO_F_BATCH 0x0008 /* Batch op if possible */
270 #define CRYPTO_F_CBIMM 0x0010 /* Do callback immediately */
271 #define CRYPTO_F_DONE 0x0020 /* Operation completed */
272 #define CRYPTO_F_CBIFSYNC 0x0040 /* Do CBIMM if op is synchronous */
274 caddr_t crp_buf; /* Data to be processed */
275 caddr_t crp_opaque; /* Opaque pointer, passed along */
276 struct cryptodesc *crp_desc; /* Linked list of processing descriptors */
278 int (*crp_callback)(struct cryptop *); /* Callback function */
280 struct bintime crp_tstamp; /* performance time stamp */
283 #define CRYPTO_BUF_CONTIG 0x0
284 #define CRYPTO_BUF_IOV 0x1
285 #define CRYPTO_BUF_MBUF 0x2
287 #define CRYPTO_OP_DECRYPT 0x0
288 #define CRYPTO_OP_ENCRYPT 0x1
291 * Hints passed to process methods.
293 #define CRYPTO_HINT_MORE 0x1 /* more ops coming shortly */
296 TAILQ_ENTRY(cryptkop) krp_next;
298 u_int krp_op; /* ie. CRK_MOD_EXP or other */
299 u_int krp_status; /* return status */
300 u_short krp_iparams; /* # of input parameters */
301 u_short krp_oparams; /* # of output parameters */
303 struct crparam krp_param[CRK_MAXPARAM]; /* kvm */
304 int (*krp_callback)(struct cryptkop *);
308 * Crypto capabilities structure.
311 * (d) - protected by CRYPTO_DRIVER_LOCK()
312 * (q) - protected by CRYPTO_Q_LOCK()
313 * Not tagged fields are read-only.
316 u_int32_t cc_sessions; /* (d) number of sessions */
317 u_int32_t cc_koperations; /* (d) number os asym operations */
320 * Largest possible operator length (in bits) for each type of
321 * encryption algorithm.
323 u_int16_t cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
325 u_int8_t cc_alg[CRYPTO_ALGORITHM_MAX + 1];
327 u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1];
329 u_int8_t cc_flags; /* (d) flags */
330 #define CRYPTOCAP_F_CLEANUP 0x01 /* needs resource cleanup */
331 #define CRYPTOCAP_F_SOFTWARE 0x02 /* software implementation */
332 #define CRYPTOCAP_F_SYNC 0x04 /* operates synchronously */
333 u_int8_t cc_qblocked; /* (q) symmetric q blocked */
334 u_int8_t cc_kqblocked; /* (q) asymmetric q blocked */
336 void *cc_arg; /* callback argument */
337 int (*cc_newsession)(void*, u_int32_t*, struct cryptoini*);
338 int (*cc_process)(void*, struct cryptop *, int);
339 int (*cc_freesession)(void*, u_int64_t);
340 void *cc_karg; /* callback argument */
341 int (*cc_kprocess) (void*, struct cryptkop *, int);
345 * Session ids are 64 bits. The lower 32 bits contain a "local id" which
346 * is a driver-private session identifier. The upper 32 bits contain a
347 * "hardware id" used by the core crypto code to identify the driver and
348 * a copy of the driver's capabilities that can be used by client code to
349 * optimize operation.
351 #define CRYPTO_SESID2HID(_sid) (((_sid) >> 32) & 0xffffff)
352 #define CRYPTO_SESID2CAPS(_sid) (((_sid) >> 56) & 0xff)
353 #define CRYPTO_SESID2LID(_sid) (((u_int32_t) (_sid)) & 0xffffffff)
355 MALLOC_DECLARE(M_CRYPTO_DATA);
357 extern int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard);
358 extern int crypto_freesession(u_int64_t sid);
359 extern int32_t crypto_get_driverid(u_int32_t flags);
360 extern int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
362 int (*newses)(void*, u_int32_t*, struct cryptoini*),
363 int (*freeses)(void*, u_int64_t),
364 int (*process)(void*, struct cryptop *, int),
366 extern int crypto_kregister(u_int32_t, int, u_int32_t,
367 int (*)(void*, struct cryptkop *, int),
369 extern int crypto_unregister(u_int32_t driverid, int alg);
370 extern int crypto_unregister_all(u_int32_t driverid);
371 extern int crypto_dispatch(struct cryptop *crp);
372 extern int crypto_kdispatch(struct cryptkop *);
373 #define CRYPTO_SYMQ 0x1
374 #define CRYPTO_ASYMQ 0x2
375 extern int crypto_unblock(u_int32_t, int);
376 extern void crypto_done(struct cryptop *crp);
377 extern void crypto_kdone(struct cryptkop *);
378 extern int crypto_getfeat(int *);
380 extern void crypto_freereq(struct cryptop *crp);
381 extern struct cryptop *crypto_getreq(int num);
383 extern int crypto_usercrypto; /* userland may do crypto requests */
384 extern int crypto_userasymcrypto; /* userland may do asym crypto reqs */
385 extern int crypto_devallowsoft; /* only use hardware crypto */
388 * Crypto-related utility routines used mainly by drivers.
390 * XXX these don't really belong here; but for now they're
391 * kept apart from the rest of the system.
394 extern void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp);
395 extern void cuio_copyback(struct uio* uio, int off, int len, caddr_t cp);
396 extern struct iovec *cuio_getptr(struct uio *uio, int loc, int *off);
397 extern int cuio_apply(struct uio *uio, int off, int len,
398 int (*f)(void *, void *, u_int), void *arg);
400 #endif /* _CRYPTO_CRYPTO_H_ */