]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/opencrypto/cryptodev.h
opencrypto: Add missing ioctl exit SDTs
[FreeBSD/FreeBSD.git] / sys / opencrypto / cryptodev.h
1 /*      $FreeBSD$       */
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  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
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  * Copyright (c) 2014 The FreeBSD Foundation
27  * All rights reserved.
28  *
29  * Portions of this software were developed by John-Mark Gurney
30  * under sponsorship of the FreeBSD Foundation and
31  * Rubicon Communications, LLC (Netgate).
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  *
37  * 1. Redistributions of source code must retain the above copyright
38  *   notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *   notice, this list of conditions and the following disclaimer in the
41  *   documentation and/or other materials provided with the distribution.
42  * 3. The name of the author may not be used to endorse or promote products
43  *   derived from this software without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55  *
56  * Effort sponsored in part by the Defense Advanced Research Projects
57  * Agency (DARPA) and Air Force Research Laboratory, Air Force
58  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
59  *
60  */
61
62 #ifndef _CRYPTO_CRYPTO_H_
63 #define _CRYPTO_CRYPTO_H_
64
65 #include <sys/ioccom.h>
66
67 #ifdef _KERNEL
68 #include <opencrypto/_cryptodev.h>
69 #include <sys/_task.h>
70 #endif
71
72 /* Some initial values */
73 #define CRYPTO_DRIVERS_INITIAL  4
74
75 /* Hash values */
76 #define NULL_HASH_LEN           16
77 #define MD5_HASH_LEN            16
78 #define SHA1_HASH_LEN           20
79 #define RIPEMD160_HASH_LEN      20
80 #define SHA2_224_HASH_LEN       28
81 #define SHA2_256_HASH_LEN       32
82 #define SHA2_384_HASH_LEN       48
83 #define SHA2_512_HASH_LEN       64
84 #define AES_GMAC_HASH_LEN       16
85 #define POLY1305_HASH_LEN       16
86 #define AES_CBC_MAC_HASH_LEN    16
87 /* Maximum hash algorithm result length */
88 #define HASH_MAX_LEN            SHA2_512_HASH_LEN /* Keep this updated */
89
90 #define MD5_BLOCK_LEN           64
91 #define SHA1_BLOCK_LEN          64
92 #define RIPEMD160_BLOCK_LEN     64
93 #define SHA2_224_BLOCK_LEN      64
94 #define SHA2_256_BLOCK_LEN      64
95 #define SHA2_384_BLOCK_LEN      128
96 #define SHA2_512_BLOCK_LEN      128
97
98 /* HMAC values */
99 #define NULL_HMAC_BLOCK_LEN             64
100 /* Maximum HMAC block length */
101 #define HMAC_MAX_BLOCK_LEN      SHA2_512_BLOCK_LEN /* Keep this updated */
102 #define HMAC_IPAD_VAL                   0x36
103 #define HMAC_OPAD_VAL                   0x5C
104 /* HMAC Key Length */
105 #define AES_128_GMAC_KEY_LEN            16
106 #define AES_192_GMAC_KEY_LEN            24
107 #define AES_256_GMAC_KEY_LEN            32
108 #define AES_128_CBC_MAC_KEY_LEN         16
109 #define AES_192_CBC_MAC_KEY_LEN         24
110 #define AES_256_CBC_MAC_KEY_LEN         32
111
112 #define POLY1305_KEY_LEN                32
113
114 /* Encryption algorithm block sizes */
115 #define NULL_BLOCK_LEN          4       /* IPsec to maintain alignment */
116 #define RIJNDAEL128_BLOCK_LEN   16
117 #define AES_BLOCK_LEN           16
118 #define AES_ICM_BLOCK_LEN       1
119 #define CAMELLIA_BLOCK_LEN      16
120 #define CHACHA20_NATIVE_BLOCK_LEN       64
121 #define EALG_MAX_BLOCK_LEN      CHACHA20_NATIVE_BLOCK_LEN /* Keep this updated */
122
123 /* IV Lengths */
124
125 #define AES_GCM_IV_LEN          12
126 #define AES_CCM_IV_LEN          12
127 #define AES_XTS_IV_LEN          8
128 #define AES_XTS_ALPHA           0x87    /* GF(2^128) generator polynomial */
129
130 /* Min and Max Encryption Key Sizes */
131 #define NULL_MIN_KEY            0
132 #define NULL_MAX_KEY            256 /* 2048 bits, max key */
133 #define RIJNDAEL_MIN_KEY        16
134 #define RIJNDAEL_MAX_KEY        32
135 #define AES_MIN_KEY             RIJNDAEL_MIN_KEY
136 #define AES_MAX_KEY             RIJNDAEL_MAX_KEY
137 #define AES_XTS_MIN_KEY         (2 * AES_MIN_KEY)
138 #define AES_XTS_MAX_KEY         (2 * AES_MAX_KEY)
139 #define CAMELLIA_MIN_KEY        8
140 #define CAMELLIA_MAX_KEY        32
141
142 /* Maximum hash algorithm result length */
143 #define AALG_MAX_RESULT_LEN     64 /* Keep this updated */
144
145 #define CRYPTO_ALGORITHM_MIN    1
146 #define CRYPTO_DES_CBC          1
147 #define CRYPTO_3DES_CBC         2
148 #define CRYPTO_BLF_CBC          3
149 #define CRYPTO_CAST_CBC         4
150 #define CRYPTO_SKIPJACK_CBC     5
151 #define CRYPTO_MD5_HMAC         6
152 #define CRYPTO_SHA1_HMAC        7
153 #define CRYPTO_RIPEMD160_HMAC   8
154 #define CRYPTO_MD5_KPDK         9
155 #define CRYPTO_SHA1_KPDK        10
156 #define CRYPTO_RIJNDAEL128_CBC  11 /* 128 bit blocksize */
157 #define CRYPTO_AES_CBC          11 /* 128 bit blocksize -- the same as above */
158 #define CRYPTO_ARC4             12
159 #define CRYPTO_MD5              13
160 #define CRYPTO_SHA1             14
161 #define CRYPTO_NULL_HMAC        15
162 #define CRYPTO_NULL_CBC         16
163 #define CRYPTO_DEFLATE_COMP     17 /* Deflate compression algorithm */
164 #define CRYPTO_SHA2_256_HMAC    18
165 #define CRYPTO_SHA2_384_HMAC    19
166 #define CRYPTO_SHA2_512_HMAC    20
167 #define CRYPTO_CAMELLIA_CBC     21
168 #define CRYPTO_AES_XTS          22
169 #define CRYPTO_AES_ICM          23 /* commonly known as CTR mode */
170 #define CRYPTO_AES_NIST_GMAC    24 /* GMAC only */
171 #define CRYPTO_AES_NIST_GCM_16  25 /* 16 byte ICV */
172 #ifdef _KERNEL
173 #define CRYPTO_AES_128_NIST_GMAC 26 /* auth side */
174 #define CRYPTO_AES_192_NIST_GMAC 27 /* auth side */
175 #define CRYPTO_AES_256_NIST_GMAC 28 /* auth side */
176 #endif
177 #define CRYPTO_BLAKE2B          29 /* Blake2b hash */
178 #define CRYPTO_BLAKE2S          30 /* Blake2s hash */
179 #define CRYPTO_CHACHA20         31 /* Chacha20 stream cipher */
180 #define CRYPTO_SHA2_224_HMAC    32
181 #define CRYPTO_RIPEMD160        33
182 #define CRYPTO_SHA2_224         34
183 #define CRYPTO_SHA2_256         35
184 #define CRYPTO_SHA2_384         36
185 #define CRYPTO_SHA2_512         37
186 #define CRYPTO_POLY1305         38
187 #define CRYPTO_AES_CCM_CBC_MAC  39      /* auth side */
188 #define CRYPTO_AES_CCM_16       40      /* cipher side */
189 #define CRYPTO_ALGORITHM_MAX    40      /* Keep updated - see below */
190
191 #define CRYPTO_ALGO_VALID(x)    ((x) >= CRYPTO_ALGORITHM_MIN && \
192                                  (x) <= CRYPTO_ALGORITHM_MAX)
193
194 /* Algorithm flags */
195 #define CRYPTO_ALG_FLAG_SUPPORTED       0x01 /* Algorithm is supported */
196 #define CRYPTO_ALG_FLAG_RNG_ENABLE      0x02 /* Has HW RNG for DH/DSA */
197 #define CRYPTO_ALG_FLAG_DSA_SHA         0x04 /* Can do SHA on msg */
198
199 /*
200  * Crypto driver/device flags.  They can set in the crid
201  * parameter when creating a session or submitting a key
202  * op to affect the device/driver assigned.  If neither
203  * of these are specified then the crid is assumed to hold
204  * the driver id of an existing (and suitable) device that
205  * must be used to satisfy the request.
206  */
207 #define CRYPTO_FLAG_HARDWARE    0x01000000      /* hardware accelerated */
208 #define CRYPTO_FLAG_SOFTWARE    0x02000000      /* software implementation */
209
210 /* NB: deprecated */
211 struct session_op {
212         u_int32_t       cipher;         /* ie. CRYPTO_AES_CBC */
213         u_int32_t       mac;            /* ie. CRYPTO_MD5_HMAC */
214
215         u_int32_t       keylen;         /* cipher key */
216         c_caddr_t       key;
217         int             mackeylen;      /* mac key */
218         c_caddr_t       mackey;
219
220         u_int32_t       ses;            /* returns: session # */ 
221 };
222
223 /*
224  * session and crypt _op structs are used by userspace programs to interact
225  * with /dev/crypto.  Confusingly, the internal kernel interface is named
226  * "cryptop" (no underscore).
227  */
228 struct session2_op {
229         u_int32_t       cipher;         /* ie. CRYPTO_AES_CBC */
230         u_int32_t       mac;            /* ie. CRYPTO_MD5_HMAC */
231
232         u_int32_t       keylen;         /* cipher key */
233         c_caddr_t       key;
234         int             mackeylen;      /* mac key */
235         c_caddr_t       mackey;
236
237         u_int32_t       ses;            /* returns: session # */ 
238         int             crid;           /* driver id + flags (rw) */
239         int             pad[4];         /* for future expansion */
240 };
241
242 struct crypt_op {
243         u_int32_t       ses;
244         u_int16_t       op;             /* i.e. COP_ENCRYPT */
245 #define COP_ENCRYPT     1
246 #define COP_DECRYPT     2
247         u_int16_t       flags;
248 #define COP_F_CIPHER_FIRST      0x0001  /* Cipher before MAC. */
249 #define COP_F_BATCH             0x0008  /* Batch op if possible */
250         u_int           len;
251         c_caddr_t       src;            /* become iov[] inside kernel */
252         caddr_t         dst;
253         caddr_t         mac;            /* must be big enough for chosen MAC */
254         c_caddr_t       iv;
255 };
256
257 /* op and flags the same as crypt_op */
258 struct crypt_aead {
259         u_int32_t       ses;
260         u_int16_t       op;             /* i.e. COP_ENCRYPT */
261         u_int16_t       flags;
262         u_int           len;
263         u_int           aadlen;
264         u_int           ivlen;
265         c_caddr_t       src;            /* become iov[] inside kernel */
266         caddr_t         dst;
267         c_caddr_t       aad;            /* additional authenticated data */
268         caddr_t         tag;            /* must fit for chosen TAG length */
269         c_caddr_t       iv;
270 };
271
272 /*
273  * Parameters for looking up a crypto driver/device by
274  * device name or by id.  The latter are returned for
275  * created sessions (crid) and completed key operations.
276  */
277 struct crypt_find_op {
278         int             crid;           /* driver id + flags */
279         char            name[32];       /* device/driver name */
280 };
281
282 /* bignum parameter, in packed bytes, ... */
283 struct crparam {
284         caddr_t         crp_p;
285         u_int           crp_nbits;
286 };
287
288 #define CRK_MAXPARAM    8
289
290 struct crypt_kop {
291         u_int           crk_op;         /* ie. CRK_MOD_EXP or other */
292         u_int           crk_status;     /* return status */
293         u_short         crk_iparams;    /* # of input parameters */
294         u_short         crk_oparams;    /* # of output parameters */
295         u_int           crk_crid;       /* NB: only used by CIOCKEY2 (rw) */
296         struct crparam  crk_param[CRK_MAXPARAM];
297 };
298 #define CRK_ALGORITM_MIN        0
299 #define CRK_MOD_EXP             0
300 #define CRK_MOD_EXP_CRT         1
301 #define CRK_DSA_SIGN            2
302 #define CRK_DSA_VERIFY          3
303 #define CRK_DH_COMPUTE_KEY      4
304 #define CRK_ALGORITHM_MAX       4 /* Keep updated - see below */
305
306 #define CRF_MOD_EXP             (1 << CRK_MOD_EXP)
307 #define CRF_MOD_EXP_CRT         (1 << CRK_MOD_EXP_CRT)
308 #define CRF_DSA_SIGN            (1 << CRK_DSA_SIGN)
309 #define CRF_DSA_VERIFY          (1 << CRK_DSA_VERIFY)
310 #define CRF_DH_COMPUTE_KEY      (1 << CRK_DH_COMPUTE_KEY)
311
312 /*
313  * done against open of /dev/crypto, to get a cloned descriptor.
314  * Please use F_SETFD against the cloned descriptor.
315  */
316 #define CRIOGET         _IOWR('c', 100, u_int32_t)
317 #define CRIOASYMFEAT    CIOCASYMFEAT
318 #define CRIOFINDDEV     CIOCFINDDEV
319
320 /* the following are done against the cloned descriptor */
321 #define CIOCGSESSION    _IOWR('c', 101, struct session_op)
322 #define CIOCFSESSION    _IOW('c', 102, u_int32_t)
323 #define CIOCCRYPT       _IOWR('c', 103, struct crypt_op)
324 #define CIOCKEY         _IOWR('c', 104, struct crypt_kop)
325 #define CIOCASYMFEAT    _IOR('c', 105, u_int32_t)
326 #define CIOCGSESSION2   _IOWR('c', 106, struct session2_op)
327 #define CIOCKEY2        _IOWR('c', 107, struct crypt_kop)
328 #define CIOCFINDDEV     _IOWR('c', 108, struct crypt_find_op)
329 #define CIOCCRYPTAEAD   _IOWR('c', 109, struct crypt_aead)
330
331 struct cryptotstat {
332         struct timespec acc;            /* total accumulated time */
333         struct timespec min;            /* min time */
334         struct timespec max;            /* max time */
335         u_int32_t       count;          /* number of observations */
336 };
337
338 struct cryptostats {
339         u_int32_t       cs_ops;         /* symmetric crypto ops submitted */
340         u_int32_t       cs_errs;        /* symmetric crypto ops that failed */
341         u_int32_t       cs_kops;        /* asymetric/key ops submitted */
342         u_int32_t       cs_kerrs;       /* asymetric/key ops that failed */
343         u_int32_t       cs_intrs;       /* crypto swi thread activations */
344         u_int32_t       cs_rets;        /* crypto return thread activations */
345         u_int32_t       cs_blocks;      /* symmetric op driver block */
346         u_int32_t       cs_kblocks;     /* symmetric op driver block */
347         /*
348          * When CRYPTO_TIMING is defined at compile time and the
349          * sysctl debug.crypto is set to 1, the crypto system will
350          * accumulate statistics about how long it takes to process
351          * crypto requests at various points during processing.
352          */
353         struct cryptotstat cs_invoke;   /* crypto_dipsatch -> crypto_invoke */
354         struct cryptotstat cs_done;     /* crypto_invoke -> crypto_done */
355         struct cryptotstat cs_cb;       /* crypto_done -> callback */
356         struct cryptotstat cs_finis;    /* callback -> callback return */
357 };
358
359 #ifdef _KERNEL
360
361 /*
362  * Return values for cryptodev_probesession methods.
363  */
364 #define CRYPTODEV_PROBE_HARDWARE        (-100)
365 #define CRYPTODEV_PROBE_ACCEL_SOFTWARE  (-200)
366 #define CRYPTODEV_PROBE_SOFTWARE        (-500)
367
368 #if 0
369 #define CRYPTDEB(s, ...) do {                                           \
370         printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__);   \
371 } while (0)
372 #else
373 #define CRYPTDEB(...)   do { } while (0)
374 #endif
375
376 struct crypto_session_params {
377         int             csp_mode;       /* Type of operations to perform. */
378
379 #define CSP_MODE_NONE           0
380 #define CSP_MODE_COMPRESS       1       /* Compression/decompression. */
381 #define CSP_MODE_CIPHER         2       /* Encrypt/decrypt. */
382 #define CSP_MODE_DIGEST         3       /* Compute/verify digest. */
383 #define CSP_MODE_AEAD           4       /* Combined auth/encryption. */
384 #define CSP_MODE_ETA            5       /* IPsec style encrypt-then-auth */
385
386         int             csp_flags;
387
388         int             csp_ivlen;       /* IV length in bytes. */
389
390         int             csp_cipher_alg;
391         int             csp_cipher_klen; /* Key length in bytes. */
392         const void      *csp_cipher_key;
393
394         int             csp_auth_alg;
395         int             csp_auth_klen;  /* Key length in bytes. */
396         const void      *csp_auth_key;
397         int             csp_auth_mlen;  /* Number of digest bytes to use.
398                                            0 means all. */
399 };
400
401 /* Structure describing complete operation */
402 struct cryptop {
403         TAILQ_ENTRY(cryptop) crp_next;
404
405         struct task     crp_task;
406
407         crypto_session_t crp_session;   /* Session */
408         int             crp_ilen;       /* Input data total length */
409         int             crp_olen;       /* Result total length */
410
411         int             crp_etype;      /*
412                                          * Error type (zero means no error).
413                                          * All error codes except EAGAIN
414                                          * indicate possible data corruption (as in,
415                                          * the data have been touched). On all
416                                          * errors, the crp_session may have changed
417                                          * (reset to a new one), so the caller
418                                          * should always check and use the new
419                                          * value on future requests.
420                                          */
421         int             crp_flags;
422
423 #define CRYPTO_F_BATCH          0x0008  /* Batch op if possible */
424 #define CRYPTO_F_CBIMM          0x0010  /* Do callback immediately */
425 #define CRYPTO_F_DONE           0x0020  /* Operation completed */
426 #define CRYPTO_F_CBIFSYNC       0x0040  /* Do CBIMM if op is synchronous */
427 #define CRYPTO_F_ASYNC          0x0080  /* Dispatch crypto jobs on several threads
428                                          * if op is synchronous
429                                          */
430 #define CRYPTO_F_ASYNC_KEEPORDER        0x0100  /*
431                                          * Dispatch the crypto jobs in the same
432                                          * order there are submitted. Applied only
433                                          * if CRYPTO_F_ASYNC flags is set
434                                          */
435 #define CRYPTO_F_IV_SEPARATE    0x0200  /* Use crp_iv[] as IV. */
436
437         int             crp_op;
438
439         union {
440                 caddr_t         crp_buf;        /* Data to be processed */
441                 struct mbuf     *crp_mbuf;
442                 struct uio      *crp_uio;
443         };
444         int             crp_buf_type;   /* Which union member describes data. */
445
446         int             crp_aad_start;  /* Location of AAD. */
447         int             crp_aad_length; /* 0 => no AAD. */
448         int             crp_iv_start;   /* Location of IV.  IV length is from
449                                          * the session.
450                                          */
451         int             crp_payload_start; /* Location of ciphertext. */
452         int             crp_payload_length;
453         int             crp_digest_start; /* Location of MAC/tag.  Length is
454                                            * from the session.
455                                            */
456
457         uint8_t         crp_iv[EALG_MAX_BLOCK_LEN]; /* IV if IV_SEPARATE. */
458
459         const void      *crp_cipher_key; /* New cipher key if non-NULL. */
460         const void      *crp_auth_key;  /* New auth key if non-NULL. */
461
462         void            *crp_opaque;    /* Opaque pointer, passed along */
463
464         int (*crp_callback)(struct cryptop *); /* Callback function */
465
466         struct bintime  crp_tstamp;     /* performance time stamp */
467         uint32_t        crp_seq;        /* used for ordered dispatch */
468         uint32_t        crp_retw_id;    /*
469                                          * the return worker to be used,
470                                          *  used for ordered dispatch
471                                          */
472 };
473
474 #define CRYPTOP_ASYNC(crp) \
475         (((crp)->crp_flags & CRYPTO_F_ASYNC) && \
476         crypto_ses2caps((crp)->crp_session) & CRYPTOCAP_F_SYNC)
477 #define CRYPTOP_ASYNC_KEEPORDER(crp) \
478         (CRYPTOP_ASYNC(crp) && \
479         (crp)->crp_flags & CRYPTO_F_ASYNC_KEEPORDER)
480
481 #define CRYPTO_BUF_CONTIG       0x0
482 #define CRYPTO_BUF_UIO          0x1
483 #define CRYPTO_BUF_MBUF         0x2
484
485 /* Flags in crp_op. */
486 #define CRYPTO_OP_DECRYPT               0x0
487 #define CRYPTO_OP_ENCRYPT               0x1
488 #define CRYPTO_OP_IS_ENCRYPT(op)        ((op) & CRYPTO_OP_ENCRYPT)
489 #define CRYPTO_OP_COMPUTE_DIGEST        0x0
490 #define CRYPTO_OP_VERIFY_DIGEST         0x2
491 #define CRYPTO_OP_DECOMPRESS            CRYPTO_OP_DECRYPT
492 #define CRYPTO_OP_COMPRESS              CRYPTO_OP_ENCRYPT
493 #define CRYPTO_OP_IS_COMPRESS(op)       ((op) & CRYPTO_OP_COMPRESS)
494
495 /*
496  * Hints passed to process methods.
497  */
498 #define CRYPTO_HINT_MORE        0x1     /* more ops coming shortly */
499
500 struct cryptkop {
501         TAILQ_ENTRY(cryptkop) krp_next;
502
503         u_int           krp_op;         /* ie. CRK_MOD_EXP or other */
504         u_int           krp_status;     /* return status */
505         u_short         krp_iparams;    /* # of input parameters */
506         u_short         krp_oparams;    /* # of output parameters */
507         u_int           krp_crid;       /* desired device, etc. */
508         uint32_t        krp_hid;        /* device used */
509         struct crparam  krp_param[CRK_MAXPARAM];        /* kvm */
510         void            (*krp_callback)(struct cryptkop *);
511         struct cryptocap *krp_cap;
512 };
513
514 uint32_t crypto_ses2hid(crypto_session_t crypto_session);
515 uint32_t crypto_ses2caps(crypto_session_t crypto_session);
516 void *crypto_get_driver_session(crypto_session_t crypto_session);
517 const struct crypto_session_params *crypto_get_params(
518     crypto_session_t crypto_session);
519 struct auth_hash *crypto_auth_hash(const struct crypto_session_params *csp);
520 struct enc_xform *crypto_cipher(const struct crypto_session_params *csp);
521
522 MALLOC_DECLARE(M_CRYPTO_DATA);
523
524 extern  int crypto_newsession(crypto_session_t *cses,
525     const struct crypto_session_params *params, int hard);
526 extern  void crypto_freesession(crypto_session_t cses);
527 #define CRYPTOCAP_F_HARDWARE    CRYPTO_FLAG_HARDWARE
528 #define CRYPTOCAP_F_SOFTWARE    CRYPTO_FLAG_SOFTWARE
529 #define CRYPTOCAP_F_SYNC        0x04000000      /* operates synchronously */
530 extern  int32_t crypto_get_driverid(device_t dev, size_t session_size,
531     int flags);
532 extern  int crypto_find_driver(const char *);
533 extern  device_t crypto_find_device_byhid(int hid);
534 extern  int crypto_getcaps(int hid);
535 extern  int crypto_kregister(u_int32_t, int, u_int32_t);
536 extern  int crypto_unregister_all(u_int32_t driverid);
537 extern  int crypto_dispatch(struct cryptop *crp);
538 extern  int crypto_kdispatch(struct cryptkop *);
539 #define CRYPTO_SYMQ     0x1
540 #define CRYPTO_ASYMQ    0x2
541 extern  int crypto_unblock(u_int32_t, int);
542 extern  void crypto_done(struct cryptop *crp);
543 extern  void crypto_kdone(struct cryptkop *);
544 extern  int crypto_getfeat(int *);
545
546 extern  void crypto_freereq(struct cryptop *crp);
547 extern  struct cryptop *crypto_getreq(crypto_session_t cses, int how);
548
549 extern  int crypto_usercrypto;          /* userland may do crypto requests */
550 extern  int crypto_userasymcrypto;      /* userland may do asym crypto reqs */
551 extern  int crypto_devallowsoft;        /* only use hardware crypto */
552
553 /* Helper routines for drivers to initialize auth contexts for HMAC. */
554 struct auth_hash;
555
556 void    hmac_init_ipad(struct auth_hash *axf, const char *key, int klen,
557     void *auth_ctx);
558 void    hmac_init_opad(struct auth_hash *axf, const char *key, int klen,
559     void *auth_ctx);
560
561 /*
562  * Crypto-related utility routines used mainly by drivers.
563  *
564  * XXX these don't really belong here; but for now they're
565  *     kept apart from the rest of the system.
566  *
567  * Similar to m_copyback/data, *_copyback copy data from the 'src'
568  * buffer into the crypto request's data buffer while *_copydata copy
569  * data from the crypto request's data buffer into the the 'dst'
570  * buffer.
571  */
572 struct uio;
573 extern  void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp);
574 extern  void cuio_copyback(struct uio* uio, int off, int len, c_caddr_t cp);
575 extern  int cuio_getptr(struct uio *uio, int loc, int *off);
576 extern  int cuio_apply(struct uio *uio, int off, int len,
577             int (*f)(void *, void *, u_int), void *arg);
578
579 struct mbuf;
580 struct iovec;
581 extern  int crypto_mbuftoiov(struct mbuf *mbuf, struct iovec **iovptr,
582             int *cnt, int *allocated);
583
584 void    crypto_copyback(struct cryptop *crp, int off, int size,
585             const void *src);
586 void    crypto_copydata(struct cryptop *crp, int off, int size, void *dst);
587 int     crypto_apply(struct cryptop *crp, int off, int len,
588             int (*f)(void *, void *, u_int), void *arg);
589 void    *crypto_contiguous_subsegment(struct cryptop *crp, size_t skip,
590             size_t len);
591
592 static __inline void
593 crypto_read_iv(struct cryptop *crp, void *iv)
594 {
595         const struct crypto_session_params *csp;
596
597         csp = crypto_get_params(crp->crp_session);
598         if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
599                 memcpy(iv, crp->crp_iv, csp->csp_ivlen);
600         else
601                 crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen, iv);
602 }
603
604 #endif /* _KERNEL */
605 #endif /* _CRYPTO_CRYPTO_H_ */