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