]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/opencrypto/cryptodev.h
bhyve: return FEATURE_NOT_CHANGEABLE for unimplemented feature of NVMe controller
[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-2021 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  * Portions of this software were developed by Ararat River
34  * Consulting, LLC under sponsorship of the FreeBSD Foundation.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  * 1. Redistributions of source code must retain the above copyright
41  *   notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *   notice, this list of conditions and the following disclaimer in the
44  *   documentation and/or other materials provided with the distribution.
45  * 3. The name of the author may not be used to endorse or promote products
46  *   derived from this software without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  *
59  * Effort sponsored in part by the Defense Advanced Research Projects
60  * Agency (DARPA) and Air Force Research Laboratory, Air Force
61  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
62  *
63  */
64
65 #ifndef _CRYPTO_CRYPTO_H_
66 #define _CRYPTO_CRYPTO_H_
67
68 #include <sys/ioccom.h>
69
70 #ifdef _KERNEL
71 #include <opencrypto/_cryptodev.h>
72 #include <sys/_task.h>
73 #endif
74
75 /* Some initial values */
76 #define CRYPTO_DRIVERS_INITIAL  4
77
78 /* Hash values */
79 #define NULL_HASH_LEN           16
80 #define SHA1_HASH_LEN           20
81 #define RIPEMD160_HASH_LEN      20
82 #define SHA2_224_HASH_LEN       28
83 #define SHA2_256_HASH_LEN       32
84 #define SHA2_384_HASH_LEN       48
85 #define SHA2_512_HASH_LEN       64
86 #define AES_GMAC_HASH_LEN       16
87 #define POLY1305_HASH_LEN       16
88 #define AES_CBC_MAC_HASH_LEN    16
89 /* Maximum hash algorithm result length */
90 #define HASH_MAX_LEN            SHA2_512_HASH_LEN /* Keep this updated */
91
92 #define SHA1_BLOCK_LEN          64
93 #define RIPEMD160_BLOCK_LEN     64
94 #define SHA2_224_BLOCK_LEN      64
95 #define SHA2_256_BLOCK_LEN      64
96 #define SHA2_384_BLOCK_LEN      128
97 #define SHA2_512_BLOCK_LEN      128
98 #define POLY1305_BLOCK_LEN      16
99
100 /* HMAC values */
101 #define NULL_HMAC_BLOCK_LEN             64
102 /* Maximum HMAC block length */
103 #define HMAC_MAX_BLOCK_LEN      SHA2_512_BLOCK_LEN /* Keep this updated */
104 #define HMAC_IPAD_VAL                   0x36
105 #define HMAC_OPAD_VAL                   0x5C
106 /* HMAC Key Length */
107 #define AES_128_GMAC_KEY_LEN            16
108 #define AES_192_GMAC_KEY_LEN            24
109 #define AES_256_GMAC_KEY_LEN            32
110 #define AES_128_CBC_MAC_KEY_LEN         16
111 #define AES_192_CBC_MAC_KEY_LEN         24
112 #define AES_256_CBC_MAC_KEY_LEN         32
113
114 #define POLY1305_KEY_LEN                32
115
116 /* Encryption algorithm block sizes */
117 #define NULL_BLOCK_LEN          4       /* IPsec to maintain alignment */
118 #define RIJNDAEL128_BLOCK_LEN   16
119 #define AES_BLOCK_LEN           16
120 #define AES_ICM_BLOCK_LEN       1
121 #define CAMELLIA_BLOCK_LEN      16
122 #define CHACHA20_NATIVE_BLOCK_LEN       64
123 #define EALG_MAX_BLOCK_LEN      CHACHA20_NATIVE_BLOCK_LEN /* Keep this updated */
124
125 /* IV Lengths */
126
127 #define AES_GCM_IV_LEN          12
128 #define AES_CCM_IV_LEN          12
129 #define AES_XTS_IV_LEN          8
130 #define AES_XTS_ALPHA           0x87    /* GF(2^128) generator polynomial */
131 #define CHACHA20_POLY1305_IV_LEN        12
132
133 /* Min and Max Encryption Key Sizes */
134 #define NULL_MIN_KEY            0
135 #define NULL_MAX_KEY            256 /* 2048 bits, max key */
136 #define RIJNDAEL_MIN_KEY        16
137 #define RIJNDAEL_MAX_KEY        32
138 #define AES_MIN_KEY             RIJNDAEL_MIN_KEY
139 #define AES_MAX_KEY             RIJNDAEL_MAX_KEY
140 #define AES_XTS_MIN_KEY         (2 * AES_MIN_KEY)
141 #define AES_XTS_MAX_KEY         (2 * AES_MAX_KEY)
142 #define CAMELLIA_MIN_KEY        16
143 #define CAMELLIA_MAX_KEY        32
144 #define CHACHA20_POLY1305_KEY   32
145
146 /* Maximum hash algorithm result length */
147 #define AALG_MAX_RESULT_LEN     64 /* Keep this updated */
148
149 #define CRYPTO_ALGORITHM_MIN    1
150 #define CRYPTO_DES_CBC          1
151 #define CRYPTO_3DES_CBC         2
152 #define CRYPTO_BLF_CBC          3
153 #define CRYPTO_CAST_CBC         4
154 #define CRYPTO_SKIPJACK_CBC     5
155 #define CRYPTO_MD5_HMAC         6
156 #define CRYPTO_SHA1_HMAC        7
157 #define CRYPTO_RIPEMD160_HMAC   8
158 #define CRYPTO_MD5_KPDK         9
159 #define CRYPTO_SHA1_KPDK        10
160 #define CRYPTO_RIJNDAEL128_CBC  11 /* 128 bit blocksize */
161 #define CRYPTO_AES_CBC          11 /* 128 bit blocksize -- the same as above */
162 #define CRYPTO_ARC4             12
163 #define CRYPTO_MD5              13
164 #define CRYPTO_SHA1             14
165 #define CRYPTO_NULL_HMAC        15
166 #define CRYPTO_NULL_CBC         16
167 #define CRYPTO_DEFLATE_COMP     17 /* Deflate compression algorithm */
168 #define CRYPTO_SHA2_256_HMAC    18
169 #define CRYPTO_SHA2_384_HMAC    19
170 #define CRYPTO_SHA2_512_HMAC    20
171 #define CRYPTO_CAMELLIA_CBC     21
172 #define CRYPTO_AES_XTS          22
173 #define CRYPTO_AES_ICM          23 /* commonly known as CTR mode */
174 #define CRYPTO_AES_NIST_GMAC    24 /* GMAC only */
175 #define CRYPTO_AES_NIST_GCM_16  25 /* 16 byte ICV */
176 #ifdef _KERNEL
177 #define CRYPTO_AES_128_NIST_GMAC 26 /* auth side */
178 #define CRYPTO_AES_192_NIST_GMAC 27 /* auth side */
179 #define CRYPTO_AES_256_NIST_GMAC 28 /* auth side */
180 #endif
181 #define CRYPTO_BLAKE2B          29 /* Blake2b hash */
182 #define CRYPTO_BLAKE2S          30 /* Blake2s hash */
183 #define CRYPTO_CHACHA20         31 /* Chacha20 stream cipher */
184 #define CRYPTO_SHA2_224_HMAC    32
185 #define CRYPTO_RIPEMD160        33
186 #define CRYPTO_SHA2_224         34
187 #define CRYPTO_SHA2_256         35
188 #define CRYPTO_SHA2_384         36
189 #define CRYPTO_SHA2_512         37
190 #define CRYPTO_POLY1305         38
191 #define CRYPTO_AES_CCM_CBC_MAC  39      /* auth side */
192 #define CRYPTO_AES_CCM_16       40      /* cipher side */
193 #define CRYPTO_CHACHA20_POLY1305 41     /* combined AEAD cipher per RFC 8439 */
194 #define CRYPTO_ALGORITHM_MAX    41      /* Keep updated - see below */
195
196 #define CRYPTO_ALGO_VALID(x)    ((x) >= CRYPTO_ALGORITHM_MIN && \
197                                  (x) <= CRYPTO_ALGORITHM_MAX)
198
199 /* Algorithm flags */
200 #define CRYPTO_ALG_FLAG_SUPPORTED       0x01 /* Algorithm is supported */
201 #define CRYPTO_ALG_FLAG_RNG_ENABLE      0x02 /* Has HW RNG for DH/DSA */
202 #define CRYPTO_ALG_FLAG_DSA_SHA         0x04 /* Can do SHA on msg */
203
204 /*
205  * Crypto driver/device flags.  They can set in the crid
206  * parameter when creating a session or submitting a key
207  * op to affect the device/driver assigned.  If neither
208  * of these are specified then the crid is assumed to hold
209  * the driver id of an existing (and suitable) device that
210  * must be used to satisfy the request.
211  */
212 #define CRYPTO_FLAG_HARDWARE    0x01000000      /* hardware accelerated */
213 #define CRYPTO_FLAG_SOFTWARE    0x02000000      /* software implementation */
214
215 /* Does the kernel support vmpage buffers on this platform? */
216 #ifdef __powerpc__
217 #define CRYPTO_MAY_HAVE_VMPAGE  1
218 #else
219 #define CRYPTO_MAY_HAVE_VMPAGE  ( PMAP_HAS_DMAP )
220 #endif
221 /* Does the currently running system support vmpage buffers on this platform? */
222 #define CRYPTO_HAS_VMPAGE       ( PMAP_HAS_DMAP )
223
224 /* NB: deprecated */
225 struct session_op {
226         uint32_t        cipher;         /* ie. CRYPTO_AES_CBC */
227         uint32_t        mac;            /* ie. CRYPTO_SHA2_256_HMAC */
228
229         uint32_t        keylen;         /* cipher key */
230         const void      *key;
231         int             mackeylen;      /* mac key */
232         const void      *mackey;
233
234         uint32_t        ses;            /* returns: session # */ 
235 };
236
237 /*
238  * session and crypt _op structs are used by userspace programs to interact
239  * with /dev/crypto.  Confusingly, the internal kernel interface is named
240  * "cryptop" (no underscore).
241  */
242 struct session2_op {
243         uint32_t        cipher;         /* ie. CRYPTO_AES_CBC */
244         uint32_t        mac;            /* ie. CRYPTO_SHA2_256_HMAC */
245
246         uint32_t        keylen;         /* cipher key */
247         const void      *key;
248         int             mackeylen;      /* mac key */
249         const void      *mackey;
250
251         uint32_t        ses;            /* returns: session # */ 
252         int             crid;           /* driver id + flags (rw) */
253         int             ivlen;          /* length of nonce/IV */
254         int             maclen;         /* length of MAC/tag */
255         int             pad[2];         /* for future expansion */
256 };
257
258 struct crypt_op {
259         uint32_t        ses;
260         uint16_t        op;             /* i.e. COP_ENCRYPT */
261 #define COP_ENCRYPT     1
262 #define COP_DECRYPT     2
263         uint16_t        flags;
264 #define COP_F_CIPHER_FIRST      0x0001  /* Cipher before MAC. */
265 #define COP_F_BATCH             0x0008  /* Batch op if possible */
266         u_int           len;
267         const void      *src;           /* become iov[] inside kernel */
268         void            *dst;
269         void            *mac;           /* must be big enough for chosen MAC */
270         const void      *iv;
271 };
272
273 /* op and flags the same as crypt_op */
274 struct crypt_aead {
275         uint32_t        ses;
276         uint16_t        op;             /* i.e. COP_ENCRYPT */
277         uint16_t        flags;
278         u_int           len;
279         u_int           aadlen;
280         u_int           ivlen;
281         const void      *src;           /* become iov[] inside kernel */
282         void            *dst;
283         const void      *aad;           /* additional authenticated data */
284         void            *tag;           /* must fit for chosen TAG length */
285         const void      *iv;
286 };
287
288 /*
289  * Parameters for looking up a crypto driver/device by
290  * device name or by id.  The latter are returned for
291  * created sessions (crid) and completed key operations.
292  */
293 struct crypt_find_op {
294         int             crid;           /* driver id + flags */
295         char            name[32];       /* device/driver name */
296 };
297
298 /* bignum parameter, in packed bytes, ... */
299 struct crparam {
300         void            *crp_p;
301         u_int           crp_nbits;
302 };
303
304 #define CRK_MAXPARAM    8
305
306 struct crypt_kop {
307         u_int           crk_op;         /* ie. CRK_MOD_EXP or other */
308         u_int           crk_status;     /* return status */
309         u_short         crk_iparams;    /* # of input parameters */
310         u_short         crk_oparams;    /* # of output parameters */
311         u_int           crk_crid;       /* NB: only used by CIOCKEY2 (rw) */
312         struct crparam  crk_param[CRK_MAXPARAM];
313 };
314 #define CRK_ALGORITM_MIN        0
315 #define CRK_MOD_EXP             0
316 #define CRK_MOD_EXP_CRT         1
317 #define CRK_DSA_SIGN            2
318 #define CRK_DSA_VERIFY          3
319 #define CRK_DH_COMPUTE_KEY      4
320 #define CRK_ALGORITHM_MAX       4 /* Keep updated - see below */
321
322 #define CRF_MOD_EXP             (1 << CRK_MOD_EXP)
323 #define CRF_MOD_EXP_CRT         (1 << CRK_MOD_EXP_CRT)
324 #define CRF_DSA_SIGN            (1 << CRK_DSA_SIGN)
325 #define CRF_DSA_VERIFY          (1 << CRK_DSA_VERIFY)
326 #define CRF_DH_COMPUTE_KEY      (1 << CRK_DH_COMPUTE_KEY)
327
328 #define CIOCGSESSION    _IOWR('c', 101, struct session_op)
329 #define CIOCFSESSION    _IOW('c', 102, uint32_t)
330 #define CIOCCRYPT       _IOWR('c', 103, struct crypt_op)
331 #define CIOCKEY         _IOWR('c', 104, struct crypt_kop)
332 #define CIOCASYMFEAT    _IOR('c', 105, uint32_t)
333 #define CIOCGSESSION2   _IOWR('c', 106, struct session2_op)
334 #define CIOCKEY2        _IOWR('c', 107, struct crypt_kop)
335 #define CIOCFINDDEV     _IOWR('c', 108, struct crypt_find_op)
336 #define CIOCCRYPTAEAD   _IOWR('c', 109, struct crypt_aead)
337
338 struct cryptostats {
339         uint64_t        cs_ops;         /* symmetric crypto ops submitted */
340         uint64_t        cs_errs;        /* symmetric crypto ops that failed */
341         uint64_t        cs_kops;        /* asymetric/key ops submitted */
342         uint64_t        cs_kerrs;       /* asymetric/key ops that failed */
343         uint64_t        cs_intrs;       /* crypto swi thread activations */
344         uint64_t        cs_rets;        /* crypto return thread activations */
345         uint64_t        cs_blocks;      /* symmetric op driver block */
346         uint64_t        cs_kblocks;     /* symmetric op driver block */
347 };
348
349 #ifdef _KERNEL
350
351 /*
352  * Return values for cryptodev_probesession methods.
353  */
354 #define CRYPTODEV_PROBE_HARDWARE        (-100)
355 #define CRYPTODEV_PROBE_ACCEL_SOFTWARE  (-200)
356 #define CRYPTODEV_PROBE_SOFTWARE        (-500)
357
358 #if 0
359 #define CRYPTDEB(s, ...) do {                                           \
360         printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__);   \
361 } while (0)
362 #else
363 #define CRYPTDEB(...)   do { } while (0)
364 #endif
365
366 struct crypto_session_params {
367         int             csp_mode;       /* Type of operations to perform. */
368
369 #define CSP_MODE_NONE           0
370 #define CSP_MODE_COMPRESS       1       /* Compression/decompression. */
371 #define CSP_MODE_CIPHER         2       /* Encrypt/decrypt. */
372 #define CSP_MODE_DIGEST         3       /* Compute/verify digest. */
373 #define CSP_MODE_AEAD           4       /* Combined auth/encryption. */
374 #define CSP_MODE_ETA            5       /* IPsec style encrypt-then-auth */
375
376         int             csp_flags;
377
378 #define CSP_F_SEPARATE_OUTPUT   0x0001  /* Requests can use separate output */
379 #define CSP_F_SEPARATE_AAD      0x0002  /* Requests can use separate AAD */
380 #define CSP_F_ESN               0x0004  /* Requests can use seperate ESN field */ 
381
382         int             csp_ivlen;      /* IV length in bytes. */
383
384         int             csp_cipher_alg;
385         int             csp_cipher_klen; /* Key length in bytes. */
386         const void      *csp_cipher_key;
387
388         int             csp_auth_alg;
389         int             csp_auth_klen;  /* Key length in bytes. */
390         const void      *csp_auth_key;
391         int             csp_auth_mlen;  /* Number of digest bytes to use.
392                                            0 means all. */
393 };
394
395 enum crypto_buffer_type {
396         CRYPTO_BUF_NONE = 0,
397         CRYPTO_BUF_CONTIG,
398         CRYPTO_BUF_UIO,
399         CRYPTO_BUF_MBUF,
400         CRYPTO_BUF_VMPAGE,
401         CRYPTO_BUF_SINGLE_MBUF,
402         CRYPTO_BUF_LAST = CRYPTO_BUF_SINGLE_MBUF
403 };
404
405 /*
406  * Description of a data buffer for a request.  Requests can either
407  * have a single buffer that is modified in place or separate input
408  * and output buffers.
409  */
410 struct crypto_buffer {
411         union {
412                 struct {
413                         char    *cb_buf;
414                         int     cb_buf_len;
415                 };
416                 struct mbuf *cb_mbuf;
417                 struct {
418                         vm_page_t *cb_vm_page;
419                         int cb_vm_page_len;
420                         int cb_vm_page_offset;
421                 };
422                 struct uio *cb_uio;
423         };
424         enum crypto_buffer_type cb_type;
425 };
426
427 /*
428  * A cursor is used to iterate through a crypto request data buffer.
429  */
430 struct crypto_buffer_cursor {
431         union {
432                 char *cc_buf;
433                 struct mbuf *cc_mbuf;
434                 struct iovec *cc_iov;
435                 vm_page_t *cc_vmpage;
436         };
437         /* Optional bytes of valid data remaining */
438         int cc_buf_len;
439         /* 
440          * Optional offset within the current buffer segment where
441          * valid data begins
442          */
443         size_t cc_offset;
444         enum crypto_buffer_type cc_type;
445 };
446
447 /* Structure describing complete operation */
448 struct cryptop {
449         TAILQ_ENTRY(cryptop) crp_next;
450
451         struct task     crp_task;
452
453         crypto_session_t crp_session;   /* Session */
454         int             crp_olen;       /* Result total length */
455
456         int             crp_etype;      /*
457                                          * Error type (zero means no error).
458                                          * All error codes except EAGAIN
459                                          * indicate possible data corruption (as in,
460                                          * the data have been touched). On all
461                                          * errors, the crp_session may have changed
462                                          * (reset to a new one), so the caller
463                                          * should always check and use the new
464                                          * value on future requests.
465                                          */
466 #define crp_startcopy   crp_flags
467         int             crp_flags;
468
469 #define CRYPTO_F_BATCH          0x0008  /* Batch op if possible */
470 #define CRYPTO_F_CBIMM          0x0010  /* Do callback immediately */
471 #define CRYPTO_F_DONE           0x0020  /* Operation completed */
472 #define CRYPTO_F_CBIFSYNC       0x0040  /* Do CBIMM if op is synchronous */
473 #define CRYPTO_F_ASYNC          0x0080  /* Dispatch crypto jobs on several threads
474                                          * if op is synchronous
475                                          */
476 #define CRYPTO_F_ASYNC_KEEPORDER        0x0100  /*
477                                          * Dispatch the crypto jobs in the same
478                                          * order there are submitted. Applied only
479                                          * if CRYPTO_F_ASYNC flags is set
480                                          */
481 #define CRYPTO_F_IV_SEPARATE    0x0200  /* Use crp_iv[] as IV. */
482
483         int             crp_op;
484
485         struct crypto_buffer crp_buf;
486         struct crypto_buffer crp_obuf;
487
488         void            *crp_aad;       /* AAD buffer. */
489         int             crp_aad_start;  /* Location of AAD. */
490         int             crp_aad_length; /* 0 => no AAD. */
491         uint8_t         crp_esn[4];     /* high-order ESN */
492
493         int             crp_iv_start;   /* Location of IV.  IV length is from
494                                          * the session.
495                                          */
496         int             crp_payload_start; /* Location of ciphertext. */
497         int             crp_payload_output_start;
498         int             crp_payload_length;
499         int             crp_digest_start; /* Location of MAC/tag.  Length is
500                                            * from the session.
501                                            */
502
503         uint8_t         crp_iv[EALG_MAX_BLOCK_LEN]; /* IV if IV_SEPARATE. */
504
505         const void      *crp_cipher_key; /* New cipher key if non-NULL. */
506         const void      *crp_auth_key;  /* New auth key if non-NULL. */
507 #define crp_endcopy     crp_opaque
508
509         void            *crp_opaque;    /* Opaque pointer, passed along */
510
511         int (*crp_callback)(struct cryptop *); /* Callback function */
512
513         struct bintime  crp_tstamp;     /* performance time stamp */
514         uint32_t        crp_seq;        /* used for ordered dispatch */
515         uint32_t        crp_retw_id;    /*
516                                          * the return worker to be used,
517                                          *  used for ordered dispatch
518                                          */
519 };
520
521 static __inline void
522 _crypto_use_buf(struct crypto_buffer *cb, void *buf, int len)
523 {
524         cb->cb_buf = buf;
525         cb->cb_buf_len = len;
526         cb->cb_type = CRYPTO_BUF_CONTIG;
527 }
528
529 static __inline void
530 _crypto_use_mbuf(struct crypto_buffer *cb, struct mbuf *m)
531 {
532         cb->cb_mbuf = m;
533         cb->cb_type = CRYPTO_BUF_MBUF;
534 }
535
536 static __inline void
537 _crypto_use_single_mbuf(struct crypto_buffer *cb, struct mbuf *m)
538 {
539         cb->cb_mbuf = m;
540         cb->cb_type = CRYPTO_BUF_SINGLE_MBUF;
541 }
542
543 static __inline void
544 _crypto_use_vmpage(struct crypto_buffer *cb, vm_page_t *pages, int len,
545     int offset)
546 {
547         cb->cb_vm_page = pages;
548         cb->cb_vm_page_len = len;
549         cb->cb_vm_page_offset = offset;
550         cb->cb_type = CRYPTO_BUF_VMPAGE;
551 }
552
553 static __inline void
554 _crypto_use_uio(struct crypto_buffer *cb, struct uio *uio)
555 {
556         cb->cb_uio = uio;
557         cb->cb_type = CRYPTO_BUF_UIO;
558 }
559
560 static __inline void
561 crypto_use_buf(struct cryptop *crp, void *buf, int len)
562 {
563         _crypto_use_buf(&crp->crp_buf, buf, len);
564 }
565
566 static __inline void
567 crypto_use_mbuf(struct cryptop *crp, struct mbuf *m)
568 {
569         _crypto_use_mbuf(&crp->crp_buf, m);
570 }
571
572 static __inline void
573 crypto_use_single_mbuf(struct cryptop *crp, struct mbuf *m)
574 {
575         _crypto_use_single_mbuf(&crp->crp_buf, m);
576 }
577
578 static __inline void
579 crypto_use_vmpage(struct cryptop *crp, vm_page_t *pages, int len, int offset)
580 {
581         _crypto_use_vmpage(&crp->crp_buf, pages, len, offset);
582 }
583
584 static __inline void
585 crypto_use_uio(struct cryptop *crp, struct uio *uio)
586 {
587         _crypto_use_uio(&crp->crp_buf, uio);
588 }
589
590 static __inline void
591 crypto_use_output_buf(struct cryptop *crp, void *buf, int len)
592 {
593         _crypto_use_buf(&crp->crp_obuf, buf, len);
594 }
595
596 static __inline void
597 crypto_use_output_mbuf(struct cryptop *crp, struct mbuf *m)
598 {
599         _crypto_use_mbuf(&crp->crp_obuf, m);
600 }
601
602 static __inline void
603 crypto_use_output_single_mbuf(struct cryptop *crp, struct mbuf *m)
604 {
605         _crypto_use_single_mbuf(&crp->crp_obuf, m);
606 }
607
608 static __inline void
609 crypto_use_output_vmpage(struct cryptop *crp, vm_page_t *pages, int len,
610     int offset)
611 {
612         _crypto_use_vmpage(&crp->crp_obuf, pages, len, offset);
613 }
614
615 static __inline void
616 crypto_use_output_uio(struct cryptop *crp, struct uio *uio)
617 {
618         _crypto_use_uio(&crp->crp_obuf, uio);
619 }
620
621 #define CRYPTOP_ASYNC(crp)                      \
622         (((crp)->crp_flags & CRYPTO_F_ASYNC) && \
623         crypto_ses2caps((crp)->crp_session) & CRYPTOCAP_F_SYNC)
624 #define CRYPTOP_ASYNC_KEEPORDER(crp) \
625         (CRYPTOP_ASYNC(crp) && \
626         (crp)->crp_flags & CRYPTO_F_ASYNC_KEEPORDER)
627 #define CRYPTO_HAS_OUTPUT_BUFFER(crp)                                   \
628         ((crp)->crp_obuf.cb_type != CRYPTO_BUF_NONE)
629
630 /* Flags in crp_op. */
631 #define CRYPTO_OP_DECRYPT               0x0
632 #define CRYPTO_OP_ENCRYPT               0x1
633 #define CRYPTO_OP_IS_ENCRYPT(op)        ((op) & CRYPTO_OP_ENCRYPT)
634 #define CRYPTO_OP_COMPUTE_DIGEST        0x0
635 #define CRYPTO_OP_VERIFY_DIGEST         0x2
636 #define CRYPTO_OP_DECOMPRESS            CRYPTO_OP_DECRYPT
637 #define CRYPTO_OP_COMPRESS              CRYPTO_OP_ENCRYPT
638 #define CRYPTO_OP_IS_COMPRESS(op)       ((op) & CRYPTO_OP_COMPRESS)
639
640 /*
641  * Hints passed to process methods.
642  */
643 #define CRYPTO_HINT_MORE        0x1     /* more ops coming shortly */
644
645 struct cryptkop {
646         TAILQ_ENTRY(cryptkop) krp_next;
647
648         u_int           krp_op;         /* ie. CRK_MOD_EXP or other */
649         u_int           krp_status;     /* return status */
650         u_short         krp_iparams;    /* # of input parameters */
651         u_short         krp_oparams;    /* # of output parameters */
652         u_int           krp_crid;       /* desired device, etc. */
653         uint32_t        krp_hid;        /* device used */
654         struct crparam  krp_param[CRK_MAXPARAM];        /* kvm */
655         void            (*krp_callback)(struct cryptkop *);
656         struct cryptocap *krp_cap;
657 };
658
659 uint32_t crypto_ses2hid(crypto_session_t crypto_session);
660 uint32_t crypto_ses2caps(crypto_session_t crypto_session);
661 void    *crypto_get_driver_session(crypto_session_t crypto_session);
662 const struct crypto_session_params *crypto_get_params(
663     crypto_session_t crypto_session);
664 struct auth_hash *crypto_auth_hash(const struct crypto_session_params *csp);
665 struct enc_xform *crypto_cipher(const struct crypto_session_params *csp);
666
667 MALLOC_DECLARE(M_CRYPTO_DATA);
668
669 int     crypto_newsession(crypto_session_t *cses,
670     const struct crypto_session_params *params, int crid);
671 void    crypto_freesession(crypto_session_t cses);
672 #define CRYPTOCAP_F_HARDWARE    CRYPTO_FLAG_HARDWARE
673 #define CRYPTOCAP_F_SOFTWARE    CRYPTO_FLAG_SOFTWARE
674 #define CRYPTOCAP_F_SYNC        0x04000000      /* operates synchronously */
675 #define CRYPTOCAP_F_ACCEL_SOFTWARE 0x08000000
676 int32_t crypto_get_driverid(device_t dev, size_t session_size, int flags);
677 int     crypto_find_driver(const char *);
678 device_t crypto_find_device_byhid(int hid);
679 int     crypto_getcaps(int hid);
680 int     crypto_kregister(uint32_t, int, uint32_t);
681 int     crypto_unregister_all(uint32_t driverid);
682 int     crypto_dispatch(struct cryptop *crp);
683 int     crypto_kdispatch(struct cryptkop *);
684 #define CRYPTO_SYMQ     0x1
685 #define CRYPTO_ASYMQ    0x2
686 int     crypto_unblock(uint32_t, int);
687 void    crypto_done(struct cryptop *crp);
688 void    crypto_kdone(struct cryptkop *);
689 int     crypto_getfeat(int *);
690
691 struct cryptop *crypto_clonereq(struct cryptop *crp, crypto_session_t cses,
692     int how);
693 void    crypto_destroyreq(struct cryptop *crp);
694 void    crypto_initreq(struct cryptop *crp, crypto_session_t cses);
695 void    crypto_freereq(struct cryptop *crp);
696 struct cryptop *crypto_getreq(crypto_session_t cses, int how);
697
698 extern  int crypto_usercrypto;          /* userland may do crypto requests */
699 extern  int crypto_userasymcrypto;      /* userland may do asym crypto reqs */
700 extern  int crypto_devallowsoft;        /* only use hardware crypto */
701
702 #ifdef SYSCTL_DECL
703 SYSCTL_DECL(_kern_crypto);
704 #endif
705
706 /* Helper routines for drivers to initialize auth contexts for HMAC. */
707 struct auth_hash;
708
709 void    hmac_init_ipad(const struct auth_hash *axf, const char *key, int klen,
710     void *auth_ctx);
711 void    hmac_init_opad(const struct auth_hash *axf, const char *key, int klen,
712     void *auth_ctx);
713
714 /*
715  * Crypto-related utility routines used mainly by drivers.
716  *
717  * Similar to m_copyback/data, *_copyback copy data from the 'src'
718  * buffer into the crypto request's data buffer while *_copydata copy
719  * data from the crypto request's data buffer into the the 'dst'
720  * buffer.
721  */
722 void    crypto_copyback(struct cryptop *crp, int off, int size,
723             const void *src);
724 void    crypto_copydata(struct cryptop *crp, int off, int size, void *dst);
725 int     crypto_apply(struct cryptop *crp, int off, int len,
726             int (*f)(void *, const void *, u_int), void *arg);
727 void    *crypto_contiguous_subsegment(struct cryptop *crp, size_t skip,
728             size_t len);
729
730 int     crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
731             int (*f)(void *, const void *, u_int), void *arg);
732 void    *crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb,
733             size_t skip, size_t len);
734 size_t  crypto_buffer_len(struct crypto_buffer *cb);
735 void    crypto_cursor_init(struct crypto_buffer_cursor *cc,
736             const struct crypto_buffer *cb);
737 void    crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount);
738 void    *crypto_cursor_segment(struct crypto_buffer_cursor *cc, size_t *len);
739 void    *crypto_cursor_segbase(struct crypto_buffer_cursor *cc);
740 size_t  crypto_cursor_seglen(struct crypto_buffer_cursor *cc);
741 void    crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
742             const void *vsrc);
743 void    crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size,
744             void *vdst);
745 void    crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
746             void *vdst);
747
748 static __inline void
749 crypto_cursor_copy(const struct crypto_buffer_cursor *fromc,
750     struct crypto_buffer_cursor *toc)
751 {
752         memcpy(toc, fromc, sizeof(*toc));
753 }
754
755 static __inline void
756 crypto_read_iv(struct cryptop *crp, void *iv)
757 {
758         const struct crypto_session_params *csp;
759
760         csp = crypto_get_params(crp->crp_session);
761         if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
762                 memcpy(iv, crp->crp_iv, csp->csp_ivlen);
763         else
764                 crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen, iv);
765 }
766
767 static __inline size_t
768 ccm_max_payload_length(const struct crypto_session_params *csp)
769 {
770         /* RFC 3160 */
771         const u_int L = 15 - csp->csp_ivlen;
772
773         switch (L) {
774         case 2:
775                 return (0xffff);
776         case 3:
777                 return (0xffffff);
778 #ifdef __LP64__
779         case 4:
780                 return (0xffffffff);
781         case 5:
782                 return (0xffffffffff);
783         case 6:
784                 return (0xffffffffffff);
785         case 7:
786                 return (0xffffffffffffff);
787         default:
788                 return (0xffffffffffffffff);
789 #else
790         default:
791                 return (0xffffffff);
792 #endif
793         }
794 }
795
796 #endif /* _KERNEL */
797 #endif /* _CRYPTO_CRYPTO_H_ */