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 /* Some initial values */ 68 #define CRYPTO_DRIVERS_INITIAL 4 69 #define CRYPTO_SW_SESSIONS 32 70 71 /* Hash values */ 72 #define NULL_HASH_LEN 16 73 #define MD5_HASH_LEN 16 74 #define SHA1_HASH_LEN 20 75 #define RIPEMD160_HASH_LEN 20 76 #define SHA2_256_HASH_LEN 32 77 #define SHA2_384_HASH_LEN 48 78 #define SHA2_512_HASH_LEN 64 79 #define MD5_KPDK_HASH_LEN 16 80 #define SHA1_KPDK_HASH_LEN 20 81 /* Maximum hash algorithm result length */ 82 #define HASH_MAX_LEN SHA2_512_HASH_LEN /* Keep this updated */ 83 84 /* HMAC values */ 85 #define NULL_HMAC_BLOCK_LEN 64 86 #define MD5_HMAC_BLOCK_LEN 64 87 #define SHA1_HMAC_BLOCK_LEN 64 88 #define RIPEMD160_HMAC_BLOCK_LEN 64 89 #define SHA2_256_HMAC_BLOCK_LEN 64 90 #define SHA2_384_HMAC_BLOCK_LEN 128 91 #define SHA2_512_HMAC_BLOCK_LEN 128 92 /* Maximum HMAC block length */ 93 #define HMAC_MAX_BLOCK_LEN SHA2_512_HMAC_BLOCK_LEN /* Keep this updated */ 94 #define HMAC_IPAD_VAL 0x36 95 #define HMAC_OPAD_VAL 0x5C 96 97 /* Encryption algorithm block sizes */ 98 #define NULL_BLOCK_LEN 4 99 #define DES_BLOCK_LEN 8 100 #define DES3_BLOCK_LEN 8 101 #define BLOWFISH_BLOCK_LEN 8 102 #define SKIPJACK_BLOCK_LEN 8 103 #define CAST128_BLOCK_LEN 8 104 #define RIJNDAEL128_BLOCK_LEN 16 105 #define AES_BLOCK_LEN RIJNDAEL128_BLOCK_LEN 106 #define CAMELLIA_BLOCK_LEN 16 107 #define EALG_MAX_BLOCK_LEN AES_BLOCK_LEN /* Keep this updated */ 108 109 /* Maximum hash algorithm result length */ 110 #define AALG_MAX_RESULT_LEN 64 /* Keep this updated */ 111 112 #define CRYPTO_ALGORITHM_MIN 1 113 #define CRYPTO_DES_CBC 1 114 #define CRYPTO_3DES_CBC 2 115 #define CRYPTO_BLF_CBC 3 116 #define CRYPTO_CAST_CBC 4 117 #define CRYPTO_SKIPJACK_CBC 5 118 #define CRYPTO_MD5_HMAC 6 119 #define CRYPTO_SHA1_HMAC 7 120 #define CRYPTO_RIPEMD160_HMAC 8 121 #define CRYPTO_MD5_KPDK 9 122 #define CRYPTO_SHA1_KPDK 10 123 #define CRYPTO_RIJNDAEL128_CBC 11 /* 128 bit blocksize */ 124 #define CRYPTO_AES_CBC 11 /* 128 bit blocksize -- the same as above */ 125 #define CRYPTO_ARC4 12 126 #define CRYPTO_MD5 13 127 #define CRYPTO_SHA1 14 128 #define CRYPTO_NULL_HMAC 15 129 #define CRYPTO_NULL_CBC 16 130 #define CRYPTO_DEFLATE_COMP 17 /* Deflate compression algorithm */ 131 #define CRYPTO_SHA2_256_HMAC 18 132 #define CRYPTO_SHA2_384_HMAC 19 133 #define CRYPTO_SHA2_512_HMAC 20 134 #define CRYPTO_CAMELLIA_CBC 21 135 #define CRYPTO_AES_XTS 22 136 #define CRYPTO_AES_ICM 23 /* commonly known as CTR mode */ 137 #define CRYPTO_AES_NIST_GMAC 24 /* cipher side */ 138 #define CRYPTO_AES_NIST_GCM_16 25 /* 16 byte ICV */ 139 #define CRYPTO_AES_128_NIST_GMAC 26 /* auth side */ 140 #define CRYPTO_AES_192_NIST_GMAC 27 /* auth side */ 141 #define CRYPTO_AES_256_NIST_GMAC 28 /* auth side */ 142 #define CRYPTO_ALGORITHM_MAX 28 /* Keep updated - see below */ 143 144 #define CRYPTO_ALGO_VALID(x) ((x) >= CRYPTO_ALGORITHM_MIN && \ 145 (x) <= CRYPTO_ALGORITHM_MAX) 146 147 /* Algorithm flags */ 148 #define CRYPTO_ALG_FLAG_SUPPORTED 0x01 /* Algorithm is supported */ 149 #define CRYPTO_ALG_FLAG_RNG_ENABLE 0x02 /* Has HW RNG for DH/DSA */ 150 #define CRYPTO_ALG_FLAG_DSA_SHA 0x04 /* Can do SHA on msg */ 151 152 /* 153 * Crypto driver/device flags. They can set in the crid 154 * parameter when creating a session or submitting a key 155 * op to affect the device/driver assigned. If neither 156 * of these are specified then the crid is assumed to hold 157 * the driver id of an existing (and suitable) device that 158 * must be used to satisfy the request. 159 */ 160 #define CRYPTO_FLAG_HARDWARE 0x01000000 /* hardware accelerated */ 161 #define CRYPTO_FLAG_SOFTWARE 0x02000000 /* software implementation */ 162 163 /* NB: deprecated */ 164 struct session_op { 165 u_int32_t cipher; /* ie. CRYPTO_DES_CBC */ 166 u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */ 167 168 u_int32_t keylen; /* cipher key */ 169 caddr_t key; 170 int mackeylen; /* mac key */ 171 caddr_t mackey; 172 173 u_int32_t ses; /* returns: session # */ 174 }; 175 176 struct session2_op { 177 u_int32_t cipher; /* ie. CRYPTO_DES_CBC */ 178 u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */ 179 180 u_int32_t keylen; /* cipher key */ 181 caddr_t key; 182 int mackeylen; /* mac key */ 183 caddr_t mackey; 184 185 u_int32_t ses; /* returns: session # */ 186 int crid; /* driver id + flags (rw) */ 187 int pad[4]; /* for future expansion */ 188 }; 189 190 struct crypt_op { 191 u_int32_t ses; 192 u_int16_t op; /* i.e. COP_ENCRYPT */ 193 #define COP_ENCRYPT 1 194 #define COP_DECRYPT 2 195 u_int16_t flags; 196 #define COP_F_BATCH 0x0008 /* Batch op if possible */ 197 u_int len; 198 caddr_t src, dst; /* become iov[] inside kernel */ 199 caddr_t mac; /* must be big enough for chosen MAC */ 200 caddr_t iv; 201 }; 202 203 /* op and flags the same as crypt_op */ 204 struct crypt_aead { 205 u_int32_t ses; 206 u_int16_t op; /* i.e. COP_ENCRYPT */ 207 u_int16_t flags; 208 u_int len; 209 u_int aadlen; 210 u_int ivlen; 211 caddr_t src, dst; /* become iov[] inside kernel */ 212 caddr_t aad; /* additional authenticated data */ 213 caddr_t tag; /* must fit for chosen TAG length */ 214 caddr_t iv; 215 }; 216 217 /* 218 * Parameters for looking up a crypto driver/device by 219 * device name or by id. The latter are returned for 220 * created sessions (crid) and completed key operations. 221 */ 222 struct crypt_find_op { 223 int crid; /* driver id + flags */ 224 char name[32]; /* device/driver name */ 225 }; 226 227 /* bignum parameter, in packed bytes, ... */ 228 struct crparam { 229 caddr_t crp_p; 230 u_int crp_nbits; 231 }; 232 233 #define CRK_MAXPARAM 8 234 235 struct crypt_kop { 236 u_int crk_op; /* ie. CRK_MOD_EXP or other */ 237 u_int crk_status; /* return status */ 238 u_short crk_iparams; /* # of input parameters */ 239 u_short crk_oparams; /* # of output parameters */ 240 u_int crk_crid; /* NB: only used by CIOCKEY2 (rw) */ 241 struct crparam crk_param[CRK_MAXPARAM]; 242 }; 243 #define CRK_ALGORITM_MIN 0 244 #define CRK_MOD_EXP 0 245 #define CRK_MOD_EXP_CRT 1 246 #define CRK_DSA_SIGN 2 247 #define CRK_DSA_VERIFY 3 248 #define CRK_DH_COMPUTE_KEY 4 249 #define CRK_ALGORITHM_MAX 4 /* Keep updated - see below */ 250 251 #define CRF_MOD_EXP (1 << CRK_MOD_EXP) 252 #define CRF_MOD_EXP_CRT (1 << CRK_MOD_EXP_CRT) 253 #define CRF_DSA_SIGN (1 << CRK_DSA_SIGN) 254 #define CRF_DSA_VERIFY (1 << CRK_DSA_VERIFY) 255 #define CRF_DH_COMPUTE_KEY (1 << CRK_DH_COMPUTE_KEY) 256 257 /* 258 * done against open of /dev/crypto, to get a cloned descriptor. 259 * Please use F_SETFD against the cloned descriptor. 260 */ 261 #define CRIOGET _IOWR('c', 100, u_int32_t) 262 #define CRIOASYMFEAT CIOCASYMFEAT 263 #define CRIOFINDDEV CIOCFINDDEV 264 265 /* the following are done against the cloned descriptor */ 266 #define CIOCGSESSION _IOWR('c', 101, struct session_op) 267 #define CIOCFSESSION _IOW('c', 102, u_int32_t) 268 #define CIOCCRYPT _IOWR('c', 103, struct crypt_op) 269 #define CIOCKEY _IOWR('c', 104, struct crypt_kop) 270 #define CIOCASYMFEAT _IOR('c', 105, u_int32_t) 271 #define CIOCGSESSION2 _IOWR('c', 106, struct session2_op) 272 #define CIOCKEY2 _IOWR('c', 107, struct crypt_kop) 273 #define CIOCFINDDEV _IOWR('c', 108, struct crypt_find_op) 274 #define CIOCCRYPTAEAD _IOWR('c', 109, struct crypt_aead) 275 276 struct cryptotstat { 277 struct timespec acc; /* total accumulated time */ 278 struct timespec min; /* min time */ 279 struct timespec max; /* max time */ 280 u_int32_t count; /* number of observations */ 281 }; 282 283 struct cryptostats { 284 u_int32_t cs_ops; /* symmetric crypto ops submitted */ 285 u_int32_t cs_errs; /* symmetric crypto ops that failed */ 286 u_int32_t cs_kops; /* asymetric/key ops submitted */ 287 u_int32_t cs_kerrs; /* asymetric/key ops that failed */ 288 u_int32_t cs_intrs; /* crypto swi thread activations */ 289 u_int32_t cs_rets; /* crypto return thread activations */ 290 u_int32_t cs_blocks; /* symmetric op driver block */ 291 u_int32_t cs_kblocks; /* symmetric op driver block */ 292 /* 293 * When CRYPTO_TIMING is defined at compile time and the 294 * sysctl debug.crypto is set to 1, the crypto system will 295 * accumulate statistics about how long it takes to process 296 * crypto requests at various points during processing. 297 */ 298 struct cryptotstat cs_invoke; /* crypto_dipsatch -> crypto_invoke */ 299 struct cryptotstat cs_done; /* crypto_invoke -> crypto_done */ 300 struct cryptotstat cs_cb; /* crypto_done -> callback */ 301 struct cryptotstat cs_finis; /* callback -> callback return */ 302 }; 303 304 #ifdef _KERNEL 305 306 #if 0 307 #define CRYPTDEB(s) do { printf("%s:%d: %s\n", __FILE__, __LINE__, s); \ 308 } while (0) 309 #else 310 #define CRYPTDEB(s) do { } while (0) 311 #endif 312 313 /* Standard initialization structure beginning */ 314 struct cryptoini { 315 int cri_alg; /* Algorithm to use */ 316 int cri_klen; /* Key length, in bits */ 317 int cri_mlen; /* Number of bytes we want from the 318 entire hash. 0 means all. */ 319 caddr_t cri_key; /* key to use */ 320 u_int8_t cri_iv[EALG_MAX_BLOCK_LEN]; /* IV to use */ 321 struct cryptoini *cri_next; 322 }; 323 324 /* Describe boundaries of a single crypto operation */ 325 struct cryptodesc { 326 int crd_skip; /* How many bytes to ignore from start */ 327 int crd_len; /* How many bytes to process */ 328 int crd_inject; /* Where to inject results, if applicable */ 329 int crd_flags; 330 331 #define CRD_F_ENCRYPT 0x01 /* Set when doing encryption */ 332 #define CRD_F_IV_PRESENT 0x02 /* When encrypting, IV is already in 333 place, so don't copy. */ 334 #define CRD_F_IV_EXPLICIT 0x04 /* IV explicitly provided */ 335 #define CRD_F_DSA_SHA_NEEDED 0x08 /* Compute SHA-1 of buffer for DSA */ 336 #define CRD_F_COMP 0x0f /* Set when doing compression */ 337 #define CRD_F_KEY_EXPLICIT 0x10 /* Key explicitly provided */ 338 339 struct cryptoini CRD_INI; /* Initialization/context data */ 340 #define crd_esn CRD_INI.cri_esn 341 #define crd_iv CRD_INI.cri_iv 342 #define crd_key CRD_INI.cri_key 343 #define crd_alg CRD_INI.cri_alg 344 #define crd_klen CRD_INI.cri_klen 345 346 struct cryptodesc *crd_next; 347 }; 348 349 /* Structure describing complete operation */ 350 struct cryptop { 351 TAILQ_ENTRY(cryptop) crp_next; 352 353 u_int64_t crp_sid; /* Session ID */ 354 int crp_ilen; /* Input data total length */ 355 int crp_olen; /* Result total length */ 356 357 int crp_etype; /* 358 * Error type (zero means no error). 359 * All error codes except EAGAIN 360 * indicate possible data corruption (as in, 361 * the data have been touched). On all 362 * errors, the crp_sid may have changed 363 * (reset to a new one), so the caller 364 * should always check and use the new 365 * value on future requests. 366 */ 367 int crp_flags; 368 369 #define CRYPTO_F_IMBUF 0x0001 /* Input/output are mbuf chains */ 370 #define CRYPTO_F_IOV 0x0002 /* Input/output are uio */ 371 #define CRYPTO_F_BATCH 0x0008 /* Batch op if possible */ 372 #define CRYPTO_F_CBIMM 0x0010 /* Do callback immediately */ 373 #define CRYPTO_F_DONE 0x0020 /* Operation completed */ 374 #define CRYPTO_F_CBIFSYNC 0x0040 /* Do CBIMM if op is synchronous */ 375 376 caddr_t crp_buf; /* Data to be processed */ 377 caddr_t crp_opaque; /* Opaque pointer, passed along */ 378 struct cryptodesc *crp_desc; /* Linked list of processing descriptors */ 379 380 int (*crp_callback)(struct cryptop *); /* Callback function */ 381 382 struct bintime crp_tstamp; /* performance time stamp */ 383 }; 384 385 #define CRYPTO_BUF_CONTIG 0x0 386 #define CRYPTO_BUF_IOV 0x1 387 #define CRYPTO_BUF_MBUF 0x2 388 389 #define CRYPTO_OP_DECRYPT 0x0 390 #define CRYPTO_OP_ENCRYPT 0x1 391 392 /* 393 * Hints passed to process methods. 394 */ 395 #define CRYPTO_HINT_MORE 0x1 /* more ops coming shortly */ 396 397 struct cryptkop { 398 TAILQ_ENTRY(cryptkop) krp_next; 399 400 u_int krp_op; /* ie. CRK_MOD_EXP or other */ 401 u_int krp_status; /* return status */ 402 u_short krp_iparams; /* # of input parameters */ 403 u_short krp_oparams; /* # of output parameters */ 404 u_int krp_crid; /* desired device, etc. */ 405 u_int32_t krp_hid; 406 struct crparam krp_param[CRK_MAXPARAM]; /* kvm */ 407 int (*krp_callback)(struct cryptkop *); 408 }; 409 410 /* 411 * Session ids are 64 bits. The lower 32 bits contain a "local id" which 412 * is a driver-private session identifier. The upper 32 bits contain a 413 * "hardware id" used by the core crypto code to identify the driver and 414 * a copy of the driver's capabilities that can be used by client code to 415 * optimize operation. 416 */ 417 #define CRYPTO_SESID2HID(_sid) (((_sid) >> 32) & 0x00ffffff) 418 #define CRYPTO_SESID2CAPS(_sid) (((_sid) >> 32) & 0xff000000) 419 #define CRYPTO_SESID2LID(_sid) (((u_int32_t) (_sid)) & 0xffffffff) 420 421 MALLOC_DECLARE(M_CRYPTO_DATA); 422 423 extern int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard); 424 extern int crypto_freesession(u_int64_t sid); 425 #define CRYPTOCAP_F_HARDWARE CRYPTO_FLAG_HARDWARE 426 #define CRYPTOCAP_F_SOFTWARE CRYPTO_FLAG_SOFTWARE 427 #define CRYPTOCAP_F_SYNC 0x04000000 /* operates synchronously */ 428 extern int32_t crypto_get_driverid(device_t dev, int flags); 429 extern int crypto_find_driver(const char *); 430 extern device_t crypto_find_device_byhid(int hid); 431 extern int crypto_getcaps(int hid); 432 extern int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen, 433 u_int32_t flags); 434 extern int crypto_kregister(u_int32_t, int, u_int32_t); 435 extern int crypto_unregister(u_int32_t driverid, int alg); 436 extern int crypto_unregister_all(u_int32_t driverid); 437 extern int crypto_dispatch(struct cryptop *crp); 438 extern int crypto_kdispatch(struct cryptkop *); 439 #define CRYPTO_SYMQ 0x1 440 #define CRYPTO_ASYMQ 0x2 441 extern int crypto_unblock(u_int32_t, int); 442 extern void crypto_done(struct cryptop *crp); 443 extern void crypto_kdone(struct cryptkop *); 444 extern int crypto_getfeat(int *); 445 446 extern void crypto_freereq(struct cryptop *crp); 447 extern struct cryptop *crypto_getreq(int num); 448 449 extern int crypto_usercrypto; /* userland may do crypto requests */ 450 extern int crypto_userasymcrypto; /* userland may do asym crypto reqs */ 451 extern int crypto_devallowsoft; /* only use hardware crypto */ 452 453 /* 454 * Crypto-related utility routines used mainly by drivers. 455 * 456 * XXX these don't really belong here; but for now they're 457 * kept apart from the rest of the system. 458 */ 459 struct uio; 460 extern void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp); 461 extern void cuio_copyback(struct uio* uio, int off, int len, caddr_t cp); 462 extern int cuio_getptr(struct uio *uio, int loc, int *off); 463 extern int cuio_apply(struct uio *uio, int off, int len, 464 int (*f)(void *, void *, u_int), void *arg); 465 466 struct mbuf; 467 struct iovec; 468 extern void crypto_mbuftoiov(struct mbuf *mbuf, struct iovec **iovptr, 469 int *cnt, int *allocated); 470 471 extern void crypto_copyback(int flags, caddr_t buf, int off, int size, 472 caddr_t in); 473 extern void crypto_copydata(int flags, caddr_t buf, int off, int size, 474 caddr_t out); 475 extern int crypto_apply(int flags, caddr_t buf, int off, int len, 476 int (*f)(void *, void *, u_int), void *arg); 477 #endif /* _KERNEL */ 478 #endif /* _CRYPTO_CRYPTO_H_ */ 479