]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/opencrypto/cryptodev.h
bsddialog: import snapshot 2021-12-05
[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
99 /* HMAC values */
100 #define NULL_HMAC_BLOCK_LEN             64
101 /* Maximum HMAC block length */
102 #define HMAC_MAX_BLOCK_LEN      SHA2_512_BLOCK_LEN /* Keep this updated */
103 #define HMAC_IPAD_VAL                   0x36
104 #define HMAC_OPAD_VAL                   0x5C
105 /* HMAC Key Length */
106 #define AES_128_GMAC_KEY_LEN            16
107 #define AES_192_GMAC_KEY_LEN            24
108 #define AES_256_GMAC_KEY_LEN            32
109 #define AES_128_CBC_MAC_KEY_LEN         16
110 #define AES_192_CBC_MAC_KEY_LEN         24
111 #define AES_256_CBC_MAC_KEY_LEN         32
112
113 #define POLY1305_KEY_LEN                32
114
115 /* Encryption algorithm block sizes */
116 #define NULL_BLOCK_LEN          4       /* IPsec to maintain alignment */
117 #define RIJNDAEL128_BLOCK_LEN   16
118 #define AES_BLOCK_LEN           16
119 #define AES_ICM_BLOCK_LEN       1
120 #define CAMELLIA_BLOCK_LEN      16
121 #define CHACHA20_NATIVE_BLOCK_LEN       64
122 #define EALG_MAX_BLOCK_LEN      CHACHA20_NATIVE_BLOCK_LEN /* Keep this updated */
123
124 /* IV Lengths */
125
126 #define AES_GCM_IV_LEN          12
127 #define AES_CCM_IV_LEN          12
128 #define AES_XTS_IV_LEN          8
129 #define AES_XTS_ALPHA           0x87    /* GF(2^128) generator polynomial */
130 #define CHACHA20_POLY1305_IV_LEN        12
131
132 /* Min and Max Encryption Key Sizes */
133 #define NULL_MIN_KEY            0
134 #define NULL_MAX_KEY            256 /* 2048 bits, max key */
135 #define RIJNDAEL_MIN_KEY        16
136 #define RIJNDAEL_MAX_KEY        32
137 #define AES_MIN_KEY             RIJNDAEL_MIN_KEY
138 #define AES_MAX_KEY             RIJNDAEL_MAX_KEY
139 #define AES_XTS_MIN_KEY         (2 * AES_MIN_KEY)
140 #define AES_XTS_MAX_KEY         (2 * AES_MAX_KEY)
141 #define CAMELLIA_MIN_KEY        16
142 #define CAMELLIA_MAX_KEY        32
143 #define CHACHA20_POLY1305_KEY   32
144
145 /* Maximum hash algorithm result length */
146 #define AALG_MAX_RESULT_LEN     64 /* Keep this updated */
147
148 #define CRYPTO_ALGORITHM_MIN    1
149 #define CRYPTO_DES_CBC          1
150 #define CRYPTO_3DES_CBC         2
151 #define CRYPTO_BLF_CBC          3
152 #define CRYPTO_CAST_CBC         4
153 #define CRYPTO_SKIPJACK_CBC     5
154 #define CRYPTO_MD5_HMAC         6
155 #define CRYPTO_SHA1_HMAC        7
156 #define CRYPTO_RIPEMD160_HMAC   8
157 #define CRYPTO_MD5_KPDK         9
158 #define CRYPTO_SHA1_KPDK        10
159 #define CRYPTO_RIJNDAEL128_CBC  11 /* 128 bit blocksize */
160 #define CRYPTO_AES_CBC          11 /* 128 bit blocksize -- the same as above */
161 #define CRYPTO_ARC4             12
162 #define CRYPTO_MD5              13
163 #define CRYPTO_SHA1             14
164 #define CRYPTO_NULL_HMAC        15
165 #define CRYPTO_NULL_CBC         16
166 #define CRYPTO_DEFLATE_COMP     17 /* Deflate compression algorithm */
167 #define CRYPTO_SHA2_256_HMAC    18
168 #define CRYPTO_SHA2_384_HMAC    19
169 #define CRYPTO_SHA2_512_HMAC    20
170 #define CRYPTO_CAMELLIA_CBC     21
171 #define CRYPTO_AES_XTS          22
172 #define CRYPTO_AES_ICM          23 /* commonly known as CTR mode */
173 #define CRYPTO_AES_NIST_GMAC    24 /* GMAC only */
174 #define CRYPTO_AES_NIST_GCM_16  25 /* 16 byte ICV */
175 #ifdef _KERNEL
176 #define CRYPTO_AES_128_NIST_GMAC 26 /* auth side */
177 #define CRYPTO_AES_192_NIST_GMAC 27 /* auth side */
178 #define CRYPTO_AES_256_NIST_GMAC 28 /* auth side */
179 #endif
180 #define CRYPTO_BLAKE2B          29 /* Blake2b hash */
181 #define CRYPTO_BLAKE2S          30 /* Blake2s hash */
182 #define CRYPTO_CHACHA20         31 /* Chacha20 stream cipher */
183 #define CRYPTO_SHA2_224_HMAC    32
184 #define CRYPTO_RIPEMD160        33
185 #define CRYPTO_SHA2_224         34
186 #define CRYPTO_SHA2_256         35
187 #define CRYPTO_SHA2_384         36
188 #define CRYPTO_SHA2_512         37
189 #define CRYPTO_POLY1305         38
190 #define CRYPTO_AES_CCM_CBC_MAC  39      /* auth side */
191 #define CRYPTO_AES_CCM_16       40      /* cipher side */
192 #define CRYPTO_CHACHA20_POLY1305 41     /* combined AEAD cipher per RFC 8439 */
193 #define CRYPTO_ALGORITHM_MAX    41      /* Keep updated - see below */
194
195 #define CRYPTO_ALGO_VALID(x)    ((x) >= CRYPTO_ALGORITHM_MIN && \
196                                  (x) <= CRYPTO_ALGORITHM_MAX)
197
198 /*
199  * Crypto driver/device flags.  They can set in the crid
200  * parameter when creating a session or submitting a key
201  * op to affect the device/driver assigned.  If neither
202  * of these are specified then the crid is assumed to hold
203  * the driver id of an existing (and suitable) device that
204  * must be used to satisfy the request.
205  */
206 #define CRYPTO_FLAG_HARDWARE    0x01000000      /* hardware accelerated */
207 #define CRYPTO_FLAG_SOFTWARE    0x02000000      /* software implementation */
208
209 /* Does the kernel support vmpage buffers on this platform? */
210 #ifdef __powerpc__
211 #define CRYPTO_MAY_HAVE_VMPAGE  1
212 #else
213 #define CRYPTO_MAY_HAVE_VMPAGE  ( PMAP_HAS_DMAP )
214 #endif
215 /* Does the currently running system support vmpage buffers on this platform? */
216 #define CRYPTO_HAS_VMPAGE       ( PMAP_HAS_DMAP )
217
218 /* NB: deprecated */
219 struct session_op {
220         uint32_t        cipher;         /* ie. CRYPTO_AES_CBC */
221         uint32_t        mac;            /* ie. CRYPTO_SHA2_256_HMAC */
222
223         uint32_t        keylen;         /* cipher key */
224         const void      *key;
225         int             mackeylen;      /* mac key */
226         const void      *mackey;
227
228         uint32_t        ses;            /* returns: session # */ 
229 };
230
231 /*
232  * session and crypt _op structs are used by userspace programs to interact
233  * with /dev/crypto.  Confusingly, the internal kernel interface is named
234  * "cryptop" (no underscore).
235  */
236 struct session2_op {
237         uint32_t        cipher;         /* ie. CRYPTO_AES_CBC */
238         uint32_t        mac;            /* ie. CRYPTO_SHA2_256_HMAC */
239
240         uint32_t        keylen;         /* cipher key */
241         const void      *key;
242         int             mackeylen;      /* mac key */
243         const void      *mackey;
244
245         uint32_t        ses;            /* returns: session # */ 
246         int             crid;           /* driver id + flags (rw) */
247         int             ivlen;          /* length of nonce/IV */
248         int             maclen;         /* length of MAC/tag */
249         int             pad[2];         /* for future expansion */
250 };
251
252 struct crypt_op {
253         uint32_t        ses;
254         uint16_t        op;             /* i.e. COP_ENCRYPT */
255 #define COP_ENCRYPT     1
256 #define COP_DECRYPT     2
257         uint16_t        flags;
258 #define COP_F_CIPHER_FIRST      0x0001  /* Cipher before MAC. */
259 #define COP_F_BATCH             0x0008  /* Batch op if possible */
260         u_int           len;
261         const void      *src;           /* become iov[] inside kernel */
262         void            *dst;
263         void            *mac;           /* must be big enough for chosen MAC */
264         const void      *iv;
265 };
266
267 /* op and flags the same as crypt_op */
268 struct crypt_aead {
269         uint32_t        ses;
270         uint16_t        op;             /* i.e. COP_ENCRYPT */
271         uint16_t        flags;
272         u_int           len;
273         u_int           aadlen;
274         u_int           ivlen;
275         const void      *src;           /* become iov[] inside kernel */
276         void            *dst;
277         const void      *aad;           /* additional authenticated data */
278         void            *tag;           /* must fit for chosen TAG length */
279         const void      *iv;
280 };
281
282 /*
283  * Parameters for looking up a crypto driver/device by
284  * device name or by id.  The latter are returned for
285  * created sessions (crid) and completed key operations.
286  */
287 struct crypt_find_op {
288         int             crid;           /* driver id + flags */
289         char            name[32];       /* device/driver name */
290 };
291
292 #define CIOCGSESSION    _IOWR('c', 101, struct session_op)
293 #define CIOCFSESSION    _IOW('c', 102, uint32_t)
294 #define CIOCCRYPT       _IOWR('c', 103, struct crypt_op)
295 #define CIOCGSESSION2   _IOWR('c', 106, struct session2_op)
296 #define CIOCFINDDEV     _IOWR('c', 108, struct crypt_find_op)
297 #define CIOCCRYPTAEAD   _IOWR('c', 109, struct crypt_aead)
298
299 struct cryptostats {
300         uint64_t        cs_ops;         /* symmetric crypto ops submitted */
301         uint64_t        cs_errs;        /* symmetric crypto ops that failed */
302         uint64_t        cs_kops;        /* asymetric/key ops submitted */
303         uint64_t        cs_kerrs;       /* asymetric/key ops that failed */
304         uint64_t        cs_intrs;       /* crypto swi thread activations */
305         uint64_t        cs_rets;        /* crypto return thread activations */
306         uint64_t        cs_blocks;      /* symmetric op driver block */
307         uint64_t        cs_kblocks;     /* symmetric op driver block */
308 };
309
310 #ifdef _KERNEL
311
312 /*
313  * Return values for cryptodev_probesession methods.
314  */
315 #define CRYPTODEV_PROBE_HARDWARE        (-100)
316 #define CRYPTODEV_PROBE_ACCEL_SOFTWARE  (-200)
317 #define CRYPTODEV_PROBE_SOFTWARE        (-500)
318
319 #if 0
320 #define CRYPTDEB(s, ...) do {                                           \
321         printf("%s:%d: " s "\n", __FILE__, __LINE__, ## __VA_ARGS__);   \
322 } while (0)
323 #else
324 #define CRYPTDEB(...)   do { } while (0)
325 #endif
326
327 struct crypto_session_params {
328         int             csp_mode;       /* Type of operations to perform. */
329
330 #define CSP_MODE_NONE           0
331 #define CSP_MODE_COMPRESS       1       /* Compression/decompression. */
332 #define CSP_MODE_CIPHER         2       /* Encrypt/decrypt. */
333 #define CSP_MODE_DIGEST         3       /* Compute/verify digest. */
334 #define CSP_MODE_AEAD           4       /* Combined auth/encryption. */
335 #define CSP_MODE_ETA            5       /* IPsec style encrypt-then-auth */
336
337         int             csp_flags;
338
339 #define CSP_F_SEPARATE_OUTPUT   0x0001  /* Requests can use separate output */
340 #define CSP_F_SEPARATE_AAD      0x0002  /* Requests can use separate AAD */
341 #define CSP_F_ESN               0x0004  /* Requests can use seperate ESN field */ 
342
343         int             csp_ivlen;      /* IV length in bytes. */
344
345         int             csp_cipher_alg;
346         int             csp_cipher_klen; /* Key length in bytes. */
347         const void      *csp_cipher_key;
348
349         int             csp_auth_alg;
350         int             csp_auth_klen;  /* Key length in bytes. */
351         const void      *csp_auth_key;
352         int             csp_auth_mlen;  /* Number of digest bytes to use.
353                                            0 means all. */
354 };
355
356 enum crypto_buffer_type {
357         CRYPTO_BUF_NONE = 0,
358         CRYPTO_BUF_CONTIG,
359         CRYPTO_BUF_UIO,
360         CRYPTO_BUF_MBUF,
361         CRYPTO_BUF_VMPAGE,
362         CRYPTO_BUF_SINGLE_MBUF,
363         CRYPTO_BUF_LAST = CRYPTO_BUF_SINGLE_MBUF
364 };
365
366 /*
367  * Description of a data buffer for a request.  Requests can either
368  * have a single buffer that is modified in place or separate input
369  * and output buffers.
370  */
371 struct crypto_buffer {
372         union {
373                 struct {
374                         char    *cb_buf;
375                         int     cb_buf_len;
376                 };
377                 struct mbuf *cb_mbuf;
378                 struct {
379                         vm_page_t *cb_vm_page;
380                         int cb_vm_page_len;
381                         int cb_vm_page_offset;
382                 };
383                 struct uio *cb_uio;
384         };
385         enum crypto_buffer_type cb_type;
386 };
387
388 /*
389  * A cursor is used to iterate through a crypto request data buffer.
390  */
391 struct crypto_buffer_cursor {
392         union {
393                 char *cc_buf;
394                 struct mbuf *cc_mbuf;
395                 struct iovec *cc_iov;
396                 vm_page_t *cc_vmpage;
397         };
398         /* Optional bytes of valid data remaining */
399         int cc_buf_len;
400         /* 
401          * Optional offset within the current buffer segment where
402          * valid data begins
403          */
404         size_t cc_offset;
405         enum crypto_buffer_type cc_type;
406 };
407
408 /* Structure describing complete operation */
409 struct cryptop {
410         TAILQ_ENTRY(cryptop) crp_next;
411
412         struct task     crp_task;
413
414         crypto_session_t crp_session;   /* Session */
415         int             crp_olen;       /* Result total length */
416
417         int             crp_etype;      /*
418                                          * Error type (zero means no error).
419                                          * All error codes except EAGAIN
420                                          * indicate possible data corruption (as in,
421                                          * the data have been touched). On all
422                                          * errors, the crp_session may have changed
423                                          * (reset to a new one), so the caller
424                                          * should always check and use the new
425                                          * value on future requests.
426                                          */
427         int             crp_flags;
428
429 #define CRYPTO_F_CBIMM          0x0010  /* Do callback immediately */
430 #define CRYPTO_F_DONE           0x0020  /* Operation completed */
431 #define CRYPTO_F_CBIFSYNC       0x0040  /* Do CBIMM if op is synchronous */
432 #define CRYPTO_F_ASYNC_ORDERED  0x0100  /* Completions must happen in order */
433 #define CRYPTO_F_IV_SEPARATE    0x0200  /* Use crp_iv[] as IV. */
434
435         int             crp_op;
436
437         struct crypto_buffer crp_buf;
438         struct crypto_buffer crp_obuf;
439
440         void            *crp_aad;       /* AAD buffer. */
441         int             crp_aad_start;  /* Location of AAD. */
442         int             crp_aad_length; /* 0 => no AAD. */
443         uint8_t         crp_esn[4];     /* high-order ESN */
444
445         int             crp_iv_start;   /* Location of IV.  IV length is from
446                                          * the session.
447                                          */
448         int             crp_payload_start; /* Location of ciphertext. */
449         int             crp_payload_output_start;
450         int             crp_payload_length;
451         int             crp_digest_start; /* Location of MAC/tag.  Length is
452                                            * from the session.
453                                            */
454
455         uint8_t         crp_iv[EALG_MAX_BLOCK_LEN]; /* IV if IV_SEPARATE. */
456
457         const void      *crp_cipher_key; /* New cipher key if non-NULL. */
458         const void      *crp_auth_key;  /* New auth key if non-NULL. */
459
460         void            *crp_opaque;    /* Opaque pointer, passed along */
461
462         int (*crp_callback)(struct cryptop *); /* Callback function */
463
464         struct bintime  crp_tstamp;     /* performance time stamp */
465         uint32_t        crp_seq;        /* used for ordered dispatch */
466         uint32_t        crp_retw_id;    /*
467                                          * the return worker to be used,
468                                          *  used for ordered dispatch
469                                          */
470 };
471
472 TAILQ_HEAD(cryptopq, cryptop);
473
474 static __inline void
475 _crypto_use_buf(struct crypto_buffer *cb, void *buf, int len)
476 {
477         cb->cb_buf = buf;
478         cb->cb_buf_len = len;
479         cb->cb_type = CRYPTO_BUF_CONTIG;
480 }
481
482 static __inline void
483 _crypto_use_mbuf(struct crypto_buffer *cb, struct mbuf *m)
484 {
485         cb->cb_mbuf = m;
486         cb->cb_type = CRYPTO_BUF_MBUF;
487 }
488
489 static __inline void
490 _crypto_use_single_mbuf(struct crypto_buffer *cb, struct mbuf *m)
491 {
492         cb->cb_mbuf = m;
493         cb->cb_type = CRYPTO_BUF_SINGLE_MBUF;
494 }
495
496 static __inline void
497 _crypto_use_vmpage(struct crypto_buffer *cb, vm_page_t *pages, int len,
498     int offset)
499 {
500         cb->cb_vm_page = pages;
501         cb->cb_vm_page_len = len;
502         cb->cb_vm_page_offset = offset;
503         cb->cb_type = CRYPTO_BUF_VMPAGE;
504 }
505
506 static __inline void
507 _crypto_use_uio(struct crypto_buffer *cb, struct uio *uio)
508 {
509         cb->cb_uio = uio;
510         cb->cb_type = CRYPTO_BUF_UIO;
511 }
512
513 static __inline void
514 crypto_use_buf(struct cryptop *crp, void *buf, int len)
515 {
516         _crypto_use_buf(&crp->crp_buf, buf, len);
517 }
518
519 static __inline void
520 crypto_use_mbuf(struct cryptop *crp, struct mbuf *m)
521 {
522         _crypto_use_mbuf(&crp->crp_buf, m);
523 }
524
525 static __inline void
526 crypto_use_single_mbuf(struct cryptop *crp, struct mbuf *m)
527 {
528         _crypto_use_single_mbuf(&crp->crp_buf, m);
529 }
530
531 static __inline void
532 crypto_use_vmpage(struct cryptop *crp, vm_page_t *pages, int len, int offset)
533 {
534         _crypto_use_vmpage(&crp->crp_buf, pages, len, offset);
535 }
536
537 static __inline void
538 crypto_use_uio(struct cryptop *crp, struct uio *uio)
539 {
540         _crypto_use_uio(&crp->crp_buf, uio);
541 }
542
543 static __inline void
544 crypto_use_output_buf(struct cryptop *crp, void *buf, int len)
545 {
546         _crypto_use_buf(&crp->crp_obuf, buf, len);
547 }
548
549 static __inline void
550 crypto_use_output_mbuf(struct cryptop *crp, struct mbuf *m)
551 {
552         _crypto_use_mbuf(&crp->crp_obuf, m);
553 }
554
555 static __inline void
556 crypto_use_output_single_mbuf(struct cryptop *crp, struct mbuf *m)
557 {
558         _crypto_use_single_mbuf(&crp->crp_obuf, m);
559 }
560
561 static __inline void
562 crypto_use_output_vmpage(struct cryptop *crp, vm_page_t *pages, int len,
563     int offset)
564 {
565         _crypto_use_vmpage(&crp->crp_obuf, pages, len, offset);
566 }
567
568 static __inline void
569 crypto_use_output_uio(struct cryptop *crp, struct uio *uio)
570 {
571         _crypto_use_uio(&crp->crp_obuf, uio);
572 }
573
574 #define CRYPTO_HAS_OUTPUT_BUFFER(crp)                                   \
575         ((crp)->crp_obuf.cb_type != CRYPTO_BUF_NONE)
576
577 /* Flags in crp_op. */
578 #define CRYPTO_OP_DECRYPT               0x0
579 #define CRYPTO_OP_ENCRYPT               0x1
580 #define CRYPTO_OP_IS_ENCRYPT(op)        ((op) & CRYPTO_OP_ENCRYPT)
581 #define CRYPTO_OP_COMPUTE_DIGEST        0x0
582 #define CRYPTO_OP_VERIFY_DIGEST         0x2
583 #define CRYPTO_OP_DECOMPRESS            CRYPTO_OP_DECRYPT
584 #define CRYPTO_OP_COMPRESS              CRYPTO_OP_ENCRYPT
585 #define CRYPTO_OP_IS_COMPRESS(op)       ((op) & CRYPTO_OP_COMPRESS)
586
587 /*
588  * Hints passed to process methods.
589  */
590 #define CRYPTO_HINT_MORE        0x1     /* more ops coming shortly */
591
592 uint32_t crypto_ses2hid(crypto_session_t crypto_session);
593 uint32_t crypto_ses2caps(crypto_session_t crypto_session);
594 void *crypto_get_driver_session(crypto_session_t crypto_session);
595 const struct crypto_session_params *crypto_get_params(
596     crypto_session_t crypto_session);
597 const struct auth_hash *crypto_auth_hash(const struct crypto_session_params *csp);
598 const struct enc_xform *crypto_cipher(const struct crypto_session_params *csp);
599
600 MALLOC_DECLARE(M_CRYPTO_DATA);
601
602 extern  int crypto_newsession(crypto_session_t *cses,
603     const struct crypto_session_params *params, int hard);
604 extern  void crypto_freesession(crypto_session_t cses);
605 #define CRYPTOCAP_F_HARDWARE    CRYPTO_FLAG_HARDWARE
606 #define CRYPTOCAP_F_SOFTWARE    CRYPTO_FLAG_SOFTWARE
607 #define CRYPTOCAP_F_SYNC        0x04000000      /* operates synchronously */
608 #define CRYPTOCAP_F_ACCEL_SOFTWARE 0x08000000
609 #define CRYPTO_SESS_SYNC(sess)  \
610         ((crypto_ses2caps(sess) & CRYPTOCAP_F_SYNC) != 0)
611 extern  int32_t crypto_get_driverid(device_t dev, size_t session_size,
612     int flags);
613 extern  int crypto_find_driver(const char *);
614 extern  device_t crypto_find_device_byhid(int hid);
615 extern  int crypto_getcaps(int hid);
616 extern  int crypto_unregister_all(uint32_t driverid);
617 extern  int crypto_dispatch(struct cryptop *crp);
618 #define CRYPTO_ASYNC_ORDERED    0x1     /* complete in order dispatched */
619 extern  int crypto_dispatch_async(struct cryptop *crp, int flags);
620 extern  void crypto_dispatch_batch(struct cryptopq *crpq, int flags);
621 #define CRYPTO_SYMQ     0x1
622 extern  int crypto_unblock(uint32_t, int);
623 extern  void crypto_done(struct cryptop *crp);
624
625 extern  void crypto_destroyreq(struct cryptop *crp);
626 extern  void crypto_initreq(struct cryptop *crp, crypto_session_t cses);
627 extern  void crypto_freereq(struct cryptop *crp);
628 extern  struct cryptop *crypto_getreq(crypto_session_t cses, int how);
629
630 extern  int crypto_usercrypto;          /* userland may do crypto requests */
631 extern  int crypto_devallowsoft;        /* only use hardware crypto */
632
633 #ifdef SYSCTL_DECL
634 SYSCTL_DECL(_kern_crypto);
635 #endif
636
637 /* Helper routines for drivers to initialize auth contexts for HMAC. */
638 struct auth_hash;
639
640 void    hmac_init_ipad(const struct auth_hash *axf, const char *key, int klen,
641     void *auth_ctx);
642 void    hmac_init_opad(const struct auth_hash *axf, const char *key, int klen,
643     void *auth_ctx);
644
645 /*
646  * Crypto-related utility routines used mainly by drivers.
647  *
648  * Similar to m_copyback/data, *_copyback copy data from the 'src'
649  * buffer into the crypto request's data buffer while *_copydata copy
650  * data from the crypto request's data buffer into the the 'dst'
651  * buffer.
652  */
653 void    crypto_copyback(struct cryptop *crp, int off, int size,
654             const void *src);
655 void    crypto_copydata(struct cryptop *crp, int off, int size, void *dst);
656 int     crypto_apply(struct cryptop *crp, int off, int len,
657             int (*f)(void *, const void *, u_int), void *arg);
658 void    *crypto_contiguous_subsegment(struct cryptop *crp, size_t skip,
659             size_t len);
660
661 int     crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
662             int (*f)(void *, const void *, u_int), void *arg);
663 void    *crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb,
664             size_t skip, size_t len);
665 size_t  crypto_buffer_len(struct crypto_buffer *cb);
666 void    crypto_cursor_init(struct crypto_buffer_cursor *cc,
667             const struct crypto_buffer *cb);
668 void    crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount);
669 void    *crypto_cursor_segment(struct crypto_buffer_cursor *cc, size_t *len);
670 void    crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
671             const void *vsrc);
672 void    crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size,
673             void *vdst);
674 void    crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
675             void *vdst);
676
677 static __inline void
678 crypto_read_iv(struct cryptop *crp, void *iv)
679 {
680         const struct crypto_session_params *csp;
681
682         csp = crypto_get_params(crp->crp_session);
683         if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
684                 memcpy(iv, crp->crp_iv, csp->csp_ivlen);
685         else
686                 crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen, iv);
687 }
688
689 static __inline size_t
690 ccm_max_payload_length(const struct crypto_session_params *csp)
691 {
692         /* RFC 3160 */
693         const u_int L = 15 - csp->csp_ivlen;
694
695         switch (L) {
696         case 2:
697                 return (0xffff);
698         case 3:
699                 return (0xffffff);
700 #ifdef __LP64__
701         case 4:
702                 return (0xffffffff);
703         case 5:
704                 return (0xffffffffff);
705         case 6:
706                 return (0xffffffffffff);
707         case 7:
708                 return (0xffffffffffffff);
709         default:
710                 return (0xffffffffffffffff);
711 #else
712         default:
713                 return (0xffffffff);
714 #endif
715         }
716 }
717
718 #endif /* _KERNEL */
719 #endif /* _CRYPTO_CRYPTO_H_ */