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