1 /* $FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.5 2003/06/03 00:09:02 sam Exp $ */
2 /* $DragonFly: src/sys/opencrypto/cryptodev.h,v 1.3 2007/12/04 09:11:12 hasso Exp $ */
3 /* $OpenBSD: cryptodev.h,v 1.31 2002/06/11 11:14:29 beck Exp $ */
6 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
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 HMAC_BLOCK_LEN 64
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_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_ALGORITHM_MAX 18 /* Keep updated - see below */
104 /* Algorithm flags */
105 #define CRYPTO_ALG_FLAG_SUPPORTED 0x01 /* Algorithm is supported */
106 #define CRYPTO_ALG_FLAG_RNG_ENABLE 0x02 /* Has HW RNG for DH/DSA */
107 #define CRYPTO_ALG_FLAG_DSA_SHA 0x04 /* Can do SHA on msg */
110 u_int32_t cipher
; /* ie. CRYPTO_DES_CBC */
111 u_int32_t mac
; /* ie. CRYPTO_MD5_HMAC */
113 u_int32_t keylen
; /* cipher key */
115 int mackeylen
; /* mac key */
118 u_int32_t ses
; /* returns: session # */
123 u_int16_t op
; /* i.e. COP_ENCRYPT */
124 #define COP_ENCRYPT 1
125 #define COP_DECRYPT 2
127 #define COP_F_BATCH 0x0008 /* Dispatch as quickly as possible */
129 caddr_t src
, dst
; /* become iov[] inside kernel */
130 caddr_t mac
; /* must be big enough for chosen MAC */
134 #define CRYPTO_MAX_MAC_LEN 20
136 /* bignum parameter, in packed bytes, ... */
142 #define CRK_MAXPARAM 8
145 u_int crk_op
; /* ie. CRK_MOD_EXP or other */
146 u_int crk_status
; /* return status */
147 u_short crk_iparams
; /* # of input parameters */
148 u_short crk_oparams
; /* # of output parameters */
150 struct crparam crk_param
[CRK_MAXPARAM
];
152 #define CRK_ALGORITM_MIN 0
153 #define CRK_MOD_EXP 0
154 #define CRK_MOD_EXP_CRT 1
155 #define CRK_DSA_SIGN 2
156 #define CRK_DSA_VERIFY 3
157 #define CRK_DH_COMPUTE_KEY 4
158 #define CRK_ALGORITHM_MAX 4 /* Keep updated - see below */
160 #define CRF_MOD_EXP (1 << CRK_MOD_EXP)
161 #define CRF_MOD_EXP_CRT (1 << CRK_MOD_EXP_CRT)
162 #define CRF_DSA_SIGN (1 << CRK_DSA_SIGN)
163 #define CRF_DSA_VERIFY (1 << CRK_DSA_VERIFY)
164 #define CRF_DH_COMPUTE_KEY (1 << CRK_DH_COMPUTE_KEY)
167 * done against open of /dev/crypto, to get a cloned descriptor.
168 * Please use F_SETFD against the cloned descriptor.
170 #define CRIOGET _IOWR('c', 100, u_int32_t)
172 /* the following are done against the cloned descriptor */
173 #define CIOCGSESSION _IOWR('c', 101, struct session_op)
174 #define CIOCFSESSION _IOW('c', 102, u_int32_t)
175 #define CIOCCRYPT _IOWR('c', 103, struct crypt_op)
176 #define CIOCKEY _IOWR('c', 104, struct crypt_kop)
178 #define CIOCASYMFEAT _IOR('c', 105, u_int32_t)
181 struct timespec acc
; /* total accumulated time */
182 struct timespec min
; /* max time */
183 struct timespec max
; /* max time */
184 u_int32_t count
; /* number of observations */
188 u_int32_t cs_ops
; /* symmetric crypto ops submitted */
189 u_int32_t cs_errs
; /* symmetric crypto ops that failed */
190 u_int32_t cs_kops
; /* asymetric/key ops submitted */
191 u_int32_t cs_kerrs
; /* asymetric/key ops that failed */
192 u_int32_t cs_intrs
; /* crypto swi thread activations */
193 u_int32_t cs_rets
; /* crypto return thread activations */
194 u_int32_t cs_blocks
; /* symmetric op driver block */
195 u_int32_t cs_kblocks
; /* symmetric op driver block */
197 * When CRYPTO_TIMING is defined at compile time and the
198 * sysctl debug.crypto is set to 1, the crypto system will
199 * accumulate statistics about how long it takes to process
200 * crypto requests at various points during processing.
202 struct cryptotstat cs_invoke
; /* crypto_dipsatch -> crypto_invoke */
203 struct cryptotstat cs_done
; /* crypto_invoke -> crypto_done */
204 struct cryptotstat cs_cb
; /* crypto_done -> callback */
205 struct cryptotstat cs_finis
; /* callback -> callback return */
209 /* Standard initialization structure beginning */
211 int cri_alg
; /* Algorithm to use */
212 int cri_klen
; /* Key length, in bits */
213 int cri_rnd
; /* Algorithm rounds, where relevant */
214 caddr_t cri_key
; /* key to use */
215 u_int8_t cri_iv
[EALG_MAX_BLOCK_LEN
]; /* IV to use */
216 struct cryptoini
*cri_next
;
219 /* Describe boundaries of a single crypto operation */
221 int crd_skip
; /* How many bytes to ignore from start */
222 int crd_len
; /* How many bytes to process */
223 int crd_inject
; /* Where to inject results, if applicable */
226 #define CRD_F_ENCRYPT 0x01 /* Set when doing encryption */
227 #define CRD_F_IV_PRESENT 0x02 /* When encrypting, IV is already in
228 place, so don't copy. */
229 #define CRD_F_IV_EXPLICIT 0x04 /* IV explicitly provided */
230 #define CRD_F_DSA_SHA_NEEDED 0x08 /* Compute SHA-1 of buffer for DSA */
231 #define CRD_F_COMP 0x0f /* Set when doing compression */
232 #define CRD_F_KEY_EXPLICIT 0x10 /* Key explicitly provided */
234 struct cryptoini CRD_INI
; /* Initialization/context data */
235 #define crd_iv CRD_INI.cri_iv
236 #define crd_key CRD_INI.cri_key
237 #define crd_rnd CRD_INI.cri_rnd
238 #define crd_alg CRD_INI.cri_alg
239 #define crd_klen CRD_INI.cri_klen
241 struct cryptodesc
*crd_next
;
244 /* Structure describing complete operation */
246 TAILQ_ENTRY(cryptop
) crp_next
;
248 u_int64_t crp_sid
; /* Session ID */
249 int crp_ilen
; /* Input data total length */
250 int crp_olen
; /* Result total length */
253 * Error type (zero means no error).
254 * All error codes except EAGAIN
255 * indicate possible data corruption (as in,
256 * the data have been touched). On all
257 * errors, the crp_sid may have changed
258 * (reset to a new one), so the caller
259 * should always check and use the new
260 * value on future requests.
264 #define CRYPTO_F_IMBUF 0x0001 /* Input/output are mbuf chains, otherwise contig */
265 #define CRYPTO_F_IOV 0x0002 /* Input/output are uio */
266 #define CRYPTO_F_REL 0x0004 /* Must return data in same place */
267 #define CRYPTO_F_BATCH 0x0008 /* Batch op if possible possible */
268 #define CRYPTO_F_CBIMM 0x0010 /* Do callback immediately */
269 #define CRYPTO_F_DONE 0x0020 /* Operation completed */
271 caddr_t crp_buf
; /* Data to be processed */
272 caddr_t crp_opaque
; /* Opaque pointer, passed along */
273 struct cryptodesc
*crp_desc
; /* Linked list of processing descriptors */
275 int (*crp_callback
)(struct cryptop
*); /* Callback function */
278 struct timespec crp_tstamp
; /* performance time stamp */
281 #define CRYPTO_BUF_CONTIG 0x0
282 #define CRYPTO_BUF_IOV 0x1
283 #define CRYPTO_BUF_MBUF 0x2
285 #define CRYPTO_OP_DECRYPT 0x0
286 #define CRYPTO_OP_ENCRYPT 0x1
289 * Hints passed to process methods.
291 #define CRYPTO_HINT_MORE 0x1 /* more ops coming shortly */
294 TAILQ_ENTRY(cryptkop
) krp_next
;
296 u_int krp_op
; /* ie. CRK_MOD_EXP or other */
297 u_int krp_status
; /* return status */
298 u_short krp_iparams
; /* # of input parameters */
299 u_short krp_oparams
; /* # of output parameters */
301 struct crparam krp_param
[CRK_MAXPARAM
]; /* kvm */
302 int (*krp_callback
)(struct cryptkop
*);
305 /* Crypto capabilities structure */
307 u_int32_t cc_sessions
;
310 * Largest possible operator length (in bits) for each type of
311 * encryption algorithm.
313 u_int16_t cc_max_op_len
[CRYPTO_ALGORITHM_MAX
+ 1];
315 u_int8_t cc_alg
[CRYPTO_ALGORITHM_MAX
+ 1];
317 u_int8_t cc_kalg
[CRK_ALGORITHM_MAX
+ 1];
320 u_int8_t cc_qblocked
; /* symmetric q blocked */
321 u_int8_t cc_kqblocked
; /* asymmetric q blocked */
322 #define CRYPTOCAP_F_CLEANUP 0x1
323 #define CRYPTOCAP_F_SOFTWARE 0x02
325 void *cc_arg
; /* callback argument */
326 int (*cc_newsession
)(void*, u_int32_t
*, struct cryptoini
*);
327 int (*cc_process
)(void*, struct cryptop
*, int);
328 int (*cc_freesession
)(void*, u_int64_t
);
329 void *cc_karg
; /* callback argument */
330 int (*cc_kprocess
) (void*, struct cryptkop
*, int);
333 MALLOC_DECLARE(M_CRYPTO_DATA
);
335 extern int crypto_newsession(u_int64_t
*sid
, struct cryptoini
*cri
, int hard
);
336 extern int crypto_freesession(u_int64_t sid
);
337 extern int32_t crypto_get_driverid(u_int32_t flags
);
338 extern int crypto_register(u_int32_t driverid
, int alg
, u_int16_t maxoplen
,
340 int (*newses
)(void*, u_int32_t
*, struct cryptoini
*),
341 int (*freeses
)(void*, u_int64_t
),
342 int (*process
)(void*, struct cryptop
*, int),
344 extern int crypto_kregister(u_int32_t
, int, u_int32_t
,
345 int (*)(void*, struct cryptkop
*, int),
347 extern int crypto_unregister(u_int32_t driverid
, int alg
);
348 extern int crypto_unregister_all(u_int32_t driverid
);
349 extern int crypto_dispatch(struct cryptop
*crp
);
350 extern int crypto_kdispatch(struct cryptkop
*);
351 #define CRYPTO_SYMQ 0x1
352 #define CRYPTO_ASYMQ 0x2
353 extern int crypto_unblock(u_int32_t
, int);
354 extern void crypto_done(struct cryptop
*crp
);
355 extern void crypto_kdone(struct cryptkop
*);
356 extern int crypto_getfeat(int *);
358 extern void crypto_freereq(struct cryptop
*crp
);
359 extern struct cryptop
*crypto_getreq(int num
);
361 extern int crypto_usercrypto
; /* userland may do crypto requests */
362 extern int crypto_userasymcrypto
; /* userland may do asym crypto reqs */
363 extern int crypto_devallowsoft
; /* only use hardware crypto */
366 * Crypto-related utility routines used mainly by drivers.
368 * XXX these don't really belong here; but for now they're
369 * kept apart from the rest of the system.
372 struct mbuf
*m_getptr(struct mbuf
*, int, int *);
375 extern void cuio_copydata(struct uio
* uio
, int off
, int len
, caddr_t cp
);
376 extern void cuio_copyback(struct uio
* uio
, int off
, int len
, caddr_t cp
);
377 extern struct iovec
*cuio_getptr(struct uio
*uio
, int loc
, int *off
);
379 #endif /* _CRYPTO_CRYPTO_H_ */