1 /* $NetBSD: cryptodev.h,v 1.42 2021/08/14 20:43:05 andvar Exp $ */ 2 /* $FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $ */ 3 /* $OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $ */ 4 5 /*- 6 * Copyright (c) 2008 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Coyote Point Systems, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) 36 * 37 * This code was written by Angelos D. Keromytis in Athens, Greece, in 38 * February 2000. Network Security Technologies Inc. (NSTI) kindly 39 * supported the development of this code. 40 * 41 * Copyright (c) 2000 Angelos D. Keromytis 42 * 43 * Permission to use, copy, and modify this software with or without fee 44 * is hereby granted, provided that this entire notice is included in 45 * all source code copies of any software which is or includes a copy or 46 * modification of this software. 47 * 48 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 49 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 50 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 51 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 52 * PURPOSE. 53 * 54 * Copyright (c) 2001 Theo de Raadt 55 * 56 * Redistribution and use in source and binary forms, with or without 57 * modification, are permitted provided that the following conditions 58 * are met: 59 * 60 * 1. Redistributions of source code must retain the above copyright 61 * notice, this list of conditions and the following disclaimer. 62 * 2. Redistributions in binary form must reproduce the above copyright 63 * notice, this list of conditions and the following disclaimer in the 64 * documentation and/or other materials provided with the distribution. 65 * 3. The name of the author may not be used to endorse or promote products 66 * derived from this software without specific prior written permission. 67 * 68 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 69 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 70 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 71 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 72 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 73 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 74 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 75 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 76 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 77 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 78 * 79 * Effort sponsored in part by the Defense Advanced Research Projects 80 * Agency (DARPA) and Air Force Research Laboratory, Air Force 81 * Materiel Command, USAF, under agreement number F30602-01-2-0537. 82 * 83 */ 84 85 #ifndef _CRYPTO_CRYPTO_H_ 86 #define _CRYPTO_CRYPTO_H_ 87 88 #include <sys/ioccom.h> 89 #include <sys/condvar.h> 90 #include <sys/time.h> 91 92 #if defined(_KERNEL_OPT) 93 #include "opt_ocf.h" 94 #endif 95 96 /* Some initial values */ 97 #define CRYPTO_DRIVERS_INITIAL 4 98 #define CRYPTO_SW_SESSIONS 32 99 100 /* HMAC values */ 101 #define HMAC_BLOCK_LEN 64 /* for compatibility */ 102 #define HMAC_IPAD_VAL 0x36 103 #define HMAC_OPAD_VAL 0x5C 104 105 /* Encryption algorithm block sizes */ 106 #define DES_BLOCK_LEN 8 107 #define DES3_BLOCK_LEN 8 108 #define BLOWFISH_BLOCK_LEN 8 109 #define SKIPJACK_BLOCK_LEN 8 110 #define CAST128_BLOCK_LEN 8 111 #define RIJNDAEL128_BLOCK_LEN 16 112 #define EALG_MAX_BLOCK_LEN 16 /* Keep this updated */ 113 114 /* Maximum hash algorithm result length */ 115 #define AALG_MAX_RESULT_LEN 64 /* Keep this updated */ 116 117 #define CRYPTO_ALGORITHM_MIN 1 118 #define CRYPTO_DES_CBC 1 119 #define CRYPTO_3DES_CBC 2 120 #define CRYPTO_BLF_CBC 3 121 #define CRYPTO_CAST_CBC 4 122 #define CRYPTO_SKIPJACK_CBC 5 123 #define CRYPTO_MD5_HMAC 6 124 #define CRYPTO_SHA1_HMAC 7 125 #define CRYPTO_RIPEMD160_HMAC 8 126 #define CRYPTO_MD5_KPDK 9 127 #define CRYPTO_SHA1_KPDK 10 128 #define CRYPTO_RIJNDAEL128_CBC 11 /* 128 bit blocksize */ 129 #define CRYPTO_AES_CBC 11 /* 128 bit blocksize -- the same as above */ 130 #define CRYPTO_ARC4 12 131 #define CRYPTO_MD5 13 132 #define CRYPTO_SHA1 14 133 #define CRYPTO_SHA2_256_HMAC 15 134 #define CRYPTO_SHA2_HMAC CRYPTO_SHA2_256_HMAC /* for compatibility */ 135 #define CRYPTO_NULL_HMAC 16 136 #define CRYPTO_NULL_CBC 17 137 #define CRYPTO_DEFLATE_COMP 18 /* Deflate compression algorithm */ 138 #define CRYPTO_MD5_HMAC_96 19 139 #define CRYPTO_SHA1_HMAC_96 20 140 #define CRYPTO_RIPEMD160_HMAC_96 21 141 #define CRYPTO_GZIP_COMP 22 /* gzip compression algorithm */ 142 #define CRYPTO_DEFLATE_COMP_NOGROW 23 /* Deflate, fail if not compressible */ 143 #define CRYPTO_SHA2_384_HMAC 24 144 #define CRYPTO_SHA2_512_HMAC 25 145 #define CRYPTO_CAMELLIA_CBC 26 146 #define CRYPTO_AES_CTR 27 147 #define CRYPTO_AES_XCBC_MAC_96 28 148 #define CRYPTO_AES_GCM_16 29 149 #define CRYPTO_AES_128_GMAC 30 150 #define CRYPTO_AES_192_GMAC 31 151 #define CRYPTO_AES_256_GMAC 32 152 #define CRYPTO_AES_GMAC 33 153 #define CRYPTO_ALGORITHM_MAX 33 /* Keep updated - see below */ 154 155 /* Algorithm flags */ 156 #define CRYPTO_ALG_FLAG_SUPPORTED 0x01 /* Algorithm is supported */ 157 #define CRYPTO_ALG_FLAG_RNG_ENABLE 0x02 /* Has HW RNG for DH/DSA */ 158 #define CRYPTO_ALG_FLAG_DSA_SHA 0x04 /* Can do SHA on msg */ 159 160 struct session_op { 161 u_int32_t cipher; /* ie. CRYPTO_DES_CBC */ 162 u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */ 163 u_int32_t comp_alg; /* ie. CRYPTO_GZIP_COMP */ 164 165 u_int32_t keylen; /* cipher key */ 166 void * key; 167 int mackeylen; /* mac key */ 168 void * mackey; 169 170 u_int32_t ses; /* returns: session # */ 171 }; 172 173 /* to support multiple session creation */ 174 175 struct session_n_op { 176 u_int32_t cipher; /* ie. CRYPTO_DES_CBC */ 177 u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */ 178 u_int32_t comp_alg; /* ie. CRYPTO_GZIP_COMP */ 179 180 u_int32_t keylen; /* cipher key */ 181 void * key; 182 int mackeylen; /* mac key */ 183 void * mackey; 184 185 u_int32_t ses; /* returns: session # */ 186 int status; 187 }; 188 189 struct crypt_op { 190 u_int32_t ses; 191 u_int16_t op; /* i.e. COP_ENCRYPT */ 192 #define COP_ENCRYPT 1 193 #define COP_DECRYPT 2 194 #define COP_COMP 3 195 #define COP_DECOMP 4 196 u_int16_t flags; 197 #define COP_F_BATCH 0x0008 /* Dispatch as quickly as possible */ 198 u_int len; /* src len */ 199 void * src, *dst; /* become iov[] inside kernel */ 200 void * mac; /* must be big enough for chosen MAC */ 201 void * iv; 202 u_int dst_len; /* dst len if not 0 */ 203 }; 204 205 /* to support multiple session creation */ 206 /* 207 * 208 * The reqid field is filled when the operation has 209 * been accepted and started, and can be used to later retrieve 210 * the operation results via CIOCNCRYPTRET or identify the 211 * request in the completion list returned by CIOCNCRYPTRETM. 212 * 213 * The opaque pointer can be set arbitrarily by the user 214 * and it is passed back in the crypt_result structure 215 * when the request completes. This field can be used for example 216 * to track context for the request and avoid lookups in the 217 * user application. 218 */ 219 220 struct crypt_n_op { 221 u_int32_t ses; 222 u_int16_t op; /* i.e. COP_ENCRYPT */ 223 #define COP_ENCRYPT 1 224 #define COP_DECRYPT 2 225 u_int16_t flags; 226 #define COP_F_BATCH 0x0008 /* Dispatch as quickly as possible */ 227 #define COP_F_MORE 0x0010 /* more data to follow */ 228 u_int len; /* src len */ 229 230 u_int32_t reqid; /* request id */ 231 int status; /* status of request -accepted or not */ 232 void *opaque; /* opaque pointer returned to user */ 233 u_int32_t keylen; /* cipher key - optional */ 234 void * key; 235 u_int32_t mackeylen; /* also optional */ 236 void * mackey; 237 238 void * src, *dst; /* become iov[] inside kernel */ 239 void * mac; /* must be big enough for chosen MAC */ 240 void * iv; 241 u_int dst_len; /* dst len if not 0 */ 242 }; 243 244 /* CIOCNCRYPTM ioctl argument, supporting one or more asynchronous 245 * crypt_n_op operations. 246 * Each crypt_n_op will receive a request id which can be used to check its 247 * status via CIOCNCRYPTRET, or to watch for its completion in the list 248 * obtained via CIOCNCRYPTRETM. 249 */ 250 struct crypt_mop { 251 size_t count; /* how many */ 252 struct crypt_n_op * reqs; /* where to get them */ 253 }; 254 255 struct crypt_sfop { 256 size_t count; 257 u_int32_t *sesid; 258 }; 259 260 struct crypt_sgop { 261 size_t count; 262 struct session_n_op * sessions; 263 }; 264 265 #define CRYPTO_MAX_MAC_LEN 32 /* Keep this updated */ 266 267 /* bignum parameter, in packed bytes, ... */ 268 struct crparam { 269 void * crp_p; 270 u_int crp_nbits; 271 }; 272 273 #define CRK_MAXPARAM 8 274 275 struct crypt_kop { 276 u_int crk_op; /* ie. CRK_MOD_EXP or other */ 277 u_int crk_status; /* return status */ 278 u_short crk_iparams; /* # of input parameters */ 279 u_short crk_oparams; /* # of output parameters */ 280 u_int crk_pad1; 281 struct crparam crk_param[CRK_MAXPARAM]; 282 }; 283 284 /* 285 * Used with the CIOCNFKEYM ioctl. 286 * 287 * This structure allows the OCF to return a request id 288 * for each of the kop operations specified in the CIOCNFKEYM call. 289 * 290 * The crk_opaque pointer can be arbitrarily set by the user 291 * and it is passed back in the crypt_result structure 292 * when the request completes. This field can be used for example 293 * to track context for the request and avoid lookups in the 294 * user application. 295 */ 296 struct crypt_n_kop { 297 u_int crk_op; /* ie. CRK_MOD_EXP or other */ 298 u_int crk_status; /* return status */ 299 u_short crk_iparams; /* # of input parameters */ 300 u_short crk_oparams; /* # of output parameters */ 301 u_int32_t crk_reqid; /* request id */ 302 struct crparam crk_param[CRK_MAXPARAM]; 303 void *crk_opaque; /* opaque pointer returned to user */ 304 }; 305 306 struct crypt_mkop { 307 size_t count; /* how many */ 308 struct crypt_n_kop * reqs; /* where to get them */ 309 }; 310 311 /* Asynchronous key or crypto result. 312 * Note that the status will be set in the crypt_result structure, 313 * not in the original crypt_kop structure (crk_status). 314 */ 315 struct crypt_result { 316 u_int32_t reqid; /* request id */ 317 u_int32_t status; /* status of request: 0 if successful */ 318 void * opaque; /* Opaque pointer from the user, passed along */ 319 }; 320 321 struct cryptret { 322 size_t count; /* space for how many */ 323 struct crypt_result * results; /* where to put them */ 324 }; 325 326 327 /* Asymmetric key operations */ 328 #define CRK_ALGORITHM_MIN 0 329 #define CRK_MOD_EXP 0 330 #define CRK_MOD_EXP_CRT 1 331 #define CRK_DSA_SIGN 2 332 #define CRK_DSA_VERIFY 3 333 #define CRK_DH_COMPUTE_KEY 4 334 #define CRK_MOD_ADD 5 335 #define CRK_MOD_ADDINV 6 336 #define CRK_MOD_SUB 7 337 #define CRK_MOD_MULT 8 338 #define CRK_MOD_MULTINV 9 339 #define CRK_MOD 10 340 #define CRK_ALGORITHM_MAX 10 /* Keep updated - see below */ 341 342 #define CRF_MOD_EXP (1 << CRK_MOD_EXP) 343 #define CRF_MOD_EXP_CRT (1 << CRK_MOD_EXP_CRT) 344 #define CRF_DSA_SIGN (1 << CRK_DSA_SIGN) 345 #define CRF_DSA_VERIFY (1 << CRK_DSA_VERIFY) 346 #define CRF_DH_COMPUTE_KEY (1 << CRK_DH_COMPUTE_KEY) 347 #define CRF_MOD_ADD (1 << CRK_MOD_ADD) 348 #define CRF_MOD_ADDINV (1 << CRK_MOD_ADDINV) 349 #define CRF_MOD_SUB (1 << CRK_MOD_SUB) 350 #define CRF_MOD_MULT (1 << CRK_MOD_MULT) 351 #define CRF_MOD_MULTINV (1 << CRK_MOD_MULTINV) 352 #define CRF_MOD (1 << CRK_MOD) 353 354 /* 355 * A large comment here once held descriptions of the ioctl 356 * requests implemented by the device. This text has been moved 357 * to the crypto(4) manual page and, later, removed from this file 358 * as it was always a step behind the times. 359 */ 360 361 /* 362 * done against open of /dev/crypto, to get a cloned descriptor. 363 * Please use F_SETFD against the cloned descriptor. But this ioctl 364 * is obsolete (the device now clones): please, just don't use it. 365 */ 366 #define CRIOGET _IOWR('c', 100, u_int32_t) 367 368 /* the following are done against the cloned descriptor */ 369 #define CIOCFSESSION _IOW('c', 102, u_int32_t) 370 #define CIOCKEY _IOWR('c', 104, struct crypt_kop) 371 #define CIOCNFKEYM _IOWR('c', 108, struct crypt_mkop) 372 #define CIOCNFSESSION _IOW('c', 109, struct crypt_sfop) 373 #define CIOCNCRYPTRETM _IOWR('c', 110, struct cryptret) 374 #define CIOCNCRYPTRET _IOWR('c', 111, struct crypt_result) 375 376 #define CIOCGSESSION _IOWR('c', 112, struct session_op) 377 #define CIOCNGSESSION _IOWR('c', 113, struct crypt_sgop) 378 #define CIOCCRYPT _IOWR('c', 114, struct crypt_op) 379 #define CIOCNCRYPTM _IOWR('c', 115, struct crypt_mop) 380 381 #define CIOCASYMFEAT _IOR('c', 105, u_int32_t) 382 383 struct cryptotstat { 384 struct timespec acc; /* total accumulated time */ 385 struct timespec min; /* max time */ 386 struct timespec max; /* max time */ 387 u_int32_t count; /* number of observations */ 388 }; 389 390 struct cryptostats { 391 u_int32_t cs_ops; /* symmetric crypto ops submitted */ 392 u_int32_t cs_errs; /* symmetric crypto ops that failed */ 393 u_int32_t cs_kops; /* asymmetric/key ops submitted */ 394 u_int32_t cs_kerrs; /* asymmetric/key ops that failed */ 395 u_int32_t cs_intrs; /* crypto swi thread activations */ 396 u_int32_t cs_rets; /* crypto return thread activations */ 397 u_int32_t cs_blocks; /* symmetric op driver block */ 398 u_int32_t cs_kblocks; /* symmetric op driver block */ 399 /* 400 * When CRYPTO_TIMING is defined at compile time and the 401 * sysctl debug.crypto is set to 1, the crypto system will 402 * accumulate statistics about how long it takes to process 403 * crypto requests at various points during processing. 404 */ 405 struct cryptotstat cs_invoke; /* crypto_dispatch -> crypto_invoke */ 406 struct cryptotstat cs_done; /* crypto_invoke -> crypto_done */ 407 struct cryptotstat cs_cb; /* crypto_done -> callback */ 408 struct cryptotstat cs_finis; /* callback -> callback return */ 409 }; 410 411 #ifdef _KERNEL 412 /* Standard initialization structure beginning */ 413 struct cryptoini { 414 int cri_alg; /* Algorithm to use */ 415 int cri_klen; /* Key length, in bits */ 416 int cri_rnd; /* Algorithm rounds, where relevant */ 417 char *cri_key; /* key to use */ 418 u_int8_t cri_iv[EALG_MAX_BLOCK_LEN]; /* IV to use */ 419 struct cryptoini *cri_next; 420 }; 421 422 /* Describe boundaries of a single crypto operation */ 423 struct cryptodesc { 424 int crd_skip; /* How many bytes to ignore from start */ 425 int crd_len; /* How many bytes to process */ 426 int crd_inject; /* Where to inject results, if applicable */ 427 int crd_flags; 428 429 #define CRD_F_ENCRYPT 0x01 /* Set when doing encryption */ 430 #define CRD_F_IV_PRESENT 0x02 /* When encrypting, IV is already in 431 place, so don't copy. */ 432 #define CRD_F_IV_EXPLICIT 0x04 /* IV explicitly provided */ 433 #define CRD_F_DSA_SHA_NEEDED 0x08 /* Compute SHA-1 of buffer for DSA */ 434 #define CRD_F_COMP 0x10 /* Set when doing compression */ 435 436 struct cryptoini CRD_INI; /* Initialization/context data */ 437 #define crd_iv CRD_INI.cri_iv 438 #define crd_key CRD_INI.cri_key 439 #define crd_rnd CRD_INI.cri_rnd 440 #define crd_alg CRD_INI.cri_alg 441 #define crd_klen CRD_INI.cri_klen 442 443 struct cryptodesc *crd_next; 444 }; 445 446 /* Structure describing complete operation */ 447 struct cryptop { 448 TAILQ_ENTRY(cryptop) crp_next; 449 u_int64_t crp_sid; /* Session ID */ 450 451 int crp_ilen; /* Input data total length */ 452 int crp_olen; /* Result total length */ 453 454 int crp_etype; /* 455 * Error type (zero means no error). 456 * All error codes except EAGAIN 457 * indicate possible data corruption (as in, 458 * the data have been touched). On all 459 * errors, the crp_sid may have changed 460 * (reset to a new one), so the caller 461 * should always check and use the new 462 * value on future requests. 463 */ 464 int crp_flags; /* 465 * other than crypto.c must not write 466 * after crypto_dispatch(). 467 */ 468 #define CRYPTO_F_IMBUF 0x0001 /* Input/output are mbuf chains */ 469 #define CRYPTO_F_IOV 0x0002 /* Input/output are uio */ 470 #define CRYPTO_F_REL 0x0004 /* Must return data in same place */ 471 #define CRYPTO_F_BATCH 0x0008 /* Batch op if possible possible */ 472 #define CRYPTO_F_CBIMM 0x0010 /* Do callback immediately */ 473 #define CRYPTO_F_DONE 0x0020 /* Operation completed */ 474 #define CRYPTO_F_CBIFSYNC 0x0040 /* Do CBIMM if op is synchronous */ 475 #define CRYPTO_F_ONRETQ 0x0080 /* Request is on return queue */ 476 #define CRYPTO_F_USER 0x0100 /* Request is in user context */ 477 #define CRYPTO_F_MORE 0x0200 /* more data to follow */ 478 479 int crp_devflags; /* other than cryptodev.c must not use. */ 480 #define CRYPTODEV_F_RET 0x0001 /* return from crypto.c to cryptodev.c */ 481 482 void * crp_buf; /* Data to be processed */ 483 void * crp_opaque; /* Opaque pointer, passed along */ 484 struct cryptodesc *crp_desc; /* Linked list of processing descriptors */ 485 486 int (*crp_callback)(struct cryptop *); /* 487 * Callback function. 488 * That must not sleep as it is 489 * called in softint context. 490 */ 491 492 void * crp_mac; 493 494 /* 495 * everything below is private to crypto(4) 496 */ 497 u_int32_t crp_reqid; /* request id */ 498 void * crp_usropaque; /* Opaque pointer from user, passed along */ 499 struct timespec crp_tstamp; /* performance time stamp */ 500 kcondvar_t crp_cv; 501 struct fcrypt *fcrp; 502 void * dst; 503 void * mac; 504 u_int len; 505 u_char tmp_iv[EALG_MAX_BLOCK_LEN]; 506 u_char tmp_mac[CRYPTO_MAX_MAC_LEN]; 507 508 struct iovec iovec[1]; 509 struct uio uio; 510 uint32_t magic; 511 struct cpu_info *reqcpu; /* 512 * save requested CPU to do cryptoret 513 * softint in the same CPU. 514 */ 515 }; 516 517 #define CRYPTO_BUF_CONTIG 0x0 518 #define CRYPTO_BUF_IOV 0x1 519 #define CRYPTO_BUF_MBUF 0x2 520 521 #define CRYPTO_OP_DECRYPT 0x0 522 #define CRYPTO_OP_ENCRYPT 0x1 523 524 /* 525 * Hints passed to process methods. 526 */ 527 #define CRYPTO_HINT_MORE 0x1 /* more ops coming shortly */ 528 529 struct cryptkop { 530 TAILQ_ENTRY(cryptkop) krp_next; 531 532 u_int32_t krp_reqid; /* request id */ 533 void * krp_usropaque; /* Opaque pointer from user, passed along */ 534 535 u_int krp_op; /* ie. CRK_MOD_EXP or other */ 536 u_int krp_status; /* return status */ 537 u_short krp_iparams; /* # of input parameters */ 538 u_short krp_oparams; /* # of output parameters */ 539 u_int32_t krp_hid; 540 struct crparam krp_param[CRK_MAXPARAM]; /* kvm */ 541 int (*krp_callback)(struct cryptkop *); /* 542 * Callback function. 543 * That must not sleep as it is 544 * called in softint context. 545 */ 546 int krp_flags; /* same values as crp_flags */ 547 int krp_devflags; /* same values as crp_devflags */ 548 kcondvar_t krp_cv; 549 struct fcrypt *fcrp; 550 struct crparam crk_param[CRK_MAXPARAM]; 551 struct cpu_info *reqcpu; 552 }; 553 554 /* Crypto capabilities structure */ 555 struct cryptocap { 556 u_int32_t cc_sessions; 557 558 /* 559 * Largest possible operator length (in bits) for each type of 560 * encryption algorithm. 561 */ 562 u_int16_t cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1]; 563 564 u_int8_t cc_alg[CRYPTO_ALGORITHM_MAX + 1]; 565 566 u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1]; 567 568 u_int8_t cc_flags; 569 u_int8_t cc_qblocked; /* symmetric q blocked */ 570 u_int8_t cc_kqblocked; /* asymmetric q blocked */ 571 #define CRYPTOCAP_F_CLEANUP 0x01 /* needs resource cleanup */ 572 #define CRYPTOCAP_F_SOFTWARE 0x02 /* software implementation */ 573 #define CRYPTOCAP_F_SYNC 0x04 /* operates synchronously */ 574 575 void *cc_arg; /* callback argument */ 576 int (*cc_newsession)(void*, u_int32_t*, struct cryptoini*); 577 int (*cc_process) (void*, struct cryptop *, int); 578 int (*cc_freesession) (void*, u_int64_t); 579 void *cc_karg; /* callback argument */ 580 int (*cc_kprocess) (void*, struct cryptkop *, int); 581 582 kmutex_t cc_lock; 583 }; 584 585 /* 586 * Session ids are 64 bits. The lower 32 bits contain a "local id" which 587 * is a driver-private session identifier. The upper 32 bits contain a 588 * "hardware id" used by the core crypto code to identify the driver and 589 * a copy of the driver's capabilities that can be used by client code to 590 * optimize operation. 591 */ 592 #define CRYPTO_SESID2HID(_sid) (((_sid) >> 32) & 0xffffff) 593 #define CRYPTO_SESID2CAPS(_sid) (((_sid) >> 56) & 0xff) 594 #define CRYPTO_SESID2LID(_sid) (((u_int32_t) (_sid)) & 0xffffffff) 595 596 MALLOC_DECLARE(M_CRYPTO_DATA); 597 598 extern int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard); 599 extern int crypto_freesession(u_int64_t sid); 600 extern int32_t crypto_get_driverid(u_int32_t flags); 601 extern int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen, 602 u_int32_t flags, 603 int (*newses)(void*, u_int32_t*, struct cryptoini*), 604 int (*freeses)(void*, u_int64_t), 605 int (*process)(void*, struct cryptop *, int), 606 void *arg); 607 extern int crypto_kregister(u_int32_t, int, u_int32_t, 608 int (*)(void*, struct cryptkop *, int), 609 void *arg); 610 extern int crypto_unregister(u_int32_t driverid, int alg); 611 extern int crypto_unregister_all(u_int32_t driverid); 612 extern int crypto_dispatch(struct cryptop *crp); 613 extern int crypto_kdispatch(struct cryptkop *); 614 #define CRYPTO_SYMQ 0x1 615 #define CRYPTO_ASYMQ 0x2 616 extern int crypto_unblock(u_int32_t, int); 617 extern void crypto_done(struct cryptop *crp); 618 extern void crypto_kdone(struct cryptkop *); 619 extern int crypto_getfeat(int *); 620 621 void cuio_copydata(struct uio *, int, int, void *); 622 void cuio_copyback(struct uio *, int, int, void *); 623 int cuio_apply(struct uio *, int, int, 624 int (*f)(void *, void *, unsigned int), void *); 625 626 extern void crypto_freereq(struct cryptop *crp); 627 extern struct cryptop *crypto_getreq(int num); 628 629 extern void crypto_kfreereq(struct cryptkop *); 630 extern struct cryptkop *crypto_kgetreq(int, int); 631 632 extern int crypto_usercrypto; /* userland may do crypto requests */ 633 extern int crypto_userasymcrypto; /* userland may do asym crypto reqs */ 634 extern int crypto_devallowsoft; /* only use hardware crypto */ 635 636 /* 637 * initialize the crypto framework subsystem (not the pseudo-device). 638 * This must be called very early in boot, so the framework is ready 639 * to handle registration requests when crypto hardware is autoconfigured. 640 * (This declaration doesn't really belong here but there's no header 641 * for the raw framework.) 642 */ 643 int crypto_init(void); 644 645 /* 646 * Crypto-related utility routines used mainly by drivers. 647 * 648 * XXX these don't really belong here; but for now they're 649 * kept apart from the rest of the system. 650 */ 651 struct uio; 652 extern void cuio_copydata(struct uio* uio, int off, int len, void *cp); 653 extern void cuio_copyback(struct uio* uio, int off, int len, void *cp); 654 extern int cuio_getptr(struct uio *, int loc, int *off); 655 656 #ifdef CRYPTO_DEBUG /* yuck, netipsec defines these differently */ 657 #ifndef DPRINTF 658 #define DPRINTF(a, ...) printf("%s: " a, __func__, ##__VA_ARGS__) 659 #endif 660 #else 661 #ifndef DPRINTF 662 #define DPRINTF(a, ...) 663 #endif 664 #endif 665 666 #endif /* _KERNEL */ 667 /* 668 * Locking notes: 669 * + crypto_drivers itself is protected by crypto_drv_mtx (an adaptive lock) 670 * + crypto_drivers[i] and its all members are protected by 671 * crypto_drivers[i].cc_lock (a spin lock) 672 * spin lock as crypto_unblock() can be called in interrupt context 673 * + percpu'ed crp_q and crp_kq are procted by splsoftnet. 674 * + crp_ret_q, crp_ret_kq and crypto_exit_flag that are members of 675 * struct crypto_crp_ret_qs are protected by crypto_crp_ret_qs.crp_ret_q_mtx 676 * (a spin lock) 677 * spin lock as crypto_done() can be called in interrupt context 678 * NOTE: 679 * It is not known whether crypto_done()(in interrupt context) is called 680 * in the same CPU as crypto_dispatch() is called. 681 * So, struct crypto_crp_ret_qs cannot be percpu(9). 682 * 683 * Locking order: 684 * - crypto_drv_mtx => crypto_drivers[i].cc_lock 685 */ 686 #endif /* _CRYPTO_CRYPTO_H_ */ 687