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 * 27 * Redistribution and use in source and binary forms, with or without 28 * modification, are permitted provided that the following conditions 29 * are met: 30 * 31 * 1. Redistributions of source code must retain the above copyright 32 * notice, this list of conditions and the following disclaimer. 33 * 2. Redistributions in binary form must reproduce the above copyright 34 * notice, this list of conditions and the following disclaimer in the 35 * documentation and/or other materials provided with the distribution. 36 * 3. The name of the author may not be used to endorse or promote products 37 * derived from this software without specific prior written permission. 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 40 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 41 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 42 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 43 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 45 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 46 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 47 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 48 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 49 * 50 * Effort sponsored in part by the Defense Advanced Research Projects 51 * Agency (DARPA) and Air Force Research Laboratory, Air Force 52 * Materiel Command, USAF, under agreement number F30602-01-2-0537. 53 * 54 */ 55 56 #ifndef _CRYPTO_CRYPTO_H_ 57 #define _CRYPTO_CRYPTO_H_ 58 59 #include <sys/ioccom.h> 60 61 /* Some initial values */ 62 #define CRYPTO_DRIVERS_INITIAL 4 63 #define CRYPTO_SW_SESSIONS 32 64 65 /* Hash values */ 66 #define NULL_HASH_LEN 16 67 #define MD5_HASH_LEN 16 68 #define SHA1_HASH_LEN 20 69 #define RIPEMD160_HASH_LEN 20 70 #define SHA2_256_HASH_LEN 32 71 #define SHA2_384_HASH_LEN 48 72 #define SHA2_512_HASH_LEN 64 73 #define MD5_KPDK_HASH_LEN 16 74 #define SHA1_KPDK_HASH_LEN 20 75 /* Maximum hash algorithm result length */ 76 #define HASH_MAX_LEN SHA2_512_HASH_LEN /* Keep this updated */ 77 78 /* HMAC values */ 79 #define NULL_HMAC_BLOCK_LEN 64 80 #define MD5_HMAC_BLOCK_LEN 64 81 #define SHA1_HMAC_BLOCK_LEN 64 82 #define RIPEMD160_HMAC_BLOCK_LEN 64 83 #define SHA2_256_HMAC_BLOCK_LEN 64 84 #define SHA2_384_HMAC_BLOCK_LEN 128 85 #define SHA2_512_HMAC_BLOCK_LEN 128 86 /* Maximum HMAC block length */ 87 #define HMAC_MAX_BLOCK_LEN SHA2_512_HMAC_BLOCK_LEN /* Keep this updated */ 88 #define HMAC_IPAD_VAL 0x36 89 #define HMAC_OPAD_VAL 0x5C 90 91 /* Encryption algorithm block sizes */ 92 #define NULL_BLOCK_LEN 4 93 #define DES_BLOCK_LEN 8 94 #define DES3_BLOCK_LEN 8 95 #define BLOWFISH_BLOCK_LEN 8 96 #define SKIPJACK_BLOCK_LEN 8 97 #define CAST128_BLOCK_LEN 8 98 #define RIJNDAEL128_BLOCK_LEN 16 99 #define AES_BLOCK_LEN RIJNDAEL128_BLOCK_LEN 100 #define EALG_MAX_BLOCK_LEN AES_BLOCK_LEN /* Keep this updated */ 101 102 #define CRYPTO_ALGORITHM_MIN 1 103 #define CRYPTO_DES_CBC 1 104 #define CRYPTO_3DES_CBC 2 105 #define CRYPTO_BLF_CBC 3 106 #define CRYPTO_CAST_CBC 4 107 #define CRYPTO_SKIPJACK_CBC 5 108 #define CRYPTO_MD5_HMAC 6 109 #define CRYPTO_SHA1_HMAC 7 110 #define CRYPTO_RIPEMD160_HMAC 8 111 #define CRYPTO_MD5_KPDK 9 112 #define CRYPTO_SHA1_KPDK 10 113 #define CRYPTO_RIJNDAEL128_CBC 11 /* 128 bit blocksize */ 114 #define CRYPTO_AES_CBC 11 /* 128 bit blocksize -- the same as above */ 115 #define CRYPTO_ARC4 12 116 #define CRYPTO_MD5 13 117 #define CRYPTO_SHA1 14 118 #define CRYPTO_NULL_HMAC 15 119 #define CRYPTO_NULL_CBC 16 120 #define CRYPTO_DEFLATE_COMP 17 /* Deflate compression algorithm */ 121 #define CRYPTO_SHA2_256_HMAC 18 122 #define CRYPTO_SHA2_384_HMAC 19 123 #define CRYPTO_SHA2_512_HMAC 20 124 #define CRYPTO_ALGORITHM_MAX 20 /* Keep updated - see below */ 125 126 /* Algorithm flags */ 127 #define CRYPTO_ALG_FLAG_SUPPORTED 0x01 /* Algorithm is supported */ 128 #define CRYPTO_ALG_FLAG_RNG_ENABLE 0x02 /* Has HW RNG for DH/DSA */ 129 #define CRYPTO_ALG_FLAG_DSA_SHA 0x04 /* Can do SHA on msg */ 130 131 /* 132 * Crypto driver/device flags. They can set in the crid 133 * parameter when creating a session or submitting a key 134 * op to affect the device/driver assigned. If neither 135 * of these are specified then the crid is assumed to hold 136 * the driver id of an existing (and suitable) device that 137 * must be used to satisfy the request. 138 */ 139 #define CRYPTO_FLAG_HARDWARE 0x01000000 /* hardware accelerated */ 140 #define CRYPTO_FLAG_SOFTWARE 0x02000000 /* software implementation */ 141 142 /* NB: deprecated */ 143 struct session_op { 144 u_int32_t cipher; /* ie. CRYPTO_DES_CBC */ 145 u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */ 146 147 u_int32_t keylen; /* cipher key */ 148 caddr_t key; 149 int mackeylen; /* mac key */ 150 caddr_t mackey; 151 152 u_int32_t ses; /* returns: session # */ 153 }; 154 155 struct session2_op { 156 u_int32_t cipher; /* ie. CRYPTO_DES_CBC */ 157 u_int32_t mac; /* ie. CRYPTO_MD5_HMAC */ 158 159 u_int32_t keylen; /* cipher key */ 160 caddr_t key; 161 int mackeylen; /* mac key */ 162 caddr_t mackey; 163 164 u_int32_t ses; /* returns: session # */ 165 int crid; /* driver id + flags (rw) */ 166 int pad[4]; /* for future expansion */ 167 }; 168 169 struct crypt_op { 170 u_int32_t ses; 171 u_int16_t op; /* i.e. COP_ENCRYPT */ 172 #define COP_ENCRYPT 1 173 #define COP_DECRYPT 2 174 u_int16_t flags; 175 #define COP_F_BATCH 0x0008 /* Batch op if possible */ 176 u_int len; 177 caddr_t src, dst; /* become iov[] inside kernel */ 178 caddr_t mac; /* must be big enough for chosen MAC */ 179 caddr_t iv; 180 }; 181 182 /* 183 * Parameters for looking up a crypto driver/device by 184 * device name or by id. The latter are returned for 185 * created sessions (crid) and completed key operations. 186 */ 187 struct crypt_find_op { 188 int crid; /* driver id + flags */ 189 char name[32]; /* device/driver name */ 190 }; 191 192 /* bignum parameter, in packed bytes, ... */ 193 struct crparam { 194 caddr_t crp_p; 195 u_int crp_nbits; 196 }; 197 198 #define CRK_MAXPARAM 8 199 200 struct crypt_kop { 201 u_int crk_op; /* ie. CRK_MOD_EXP or other */ 202 u_int crk_status; /* return status */ 203 u_short crk_iparams; /* # of input parameters */ 204 u_short crk_oparams; /* # of output parameters */ 205 u_int crk_crid; /* NB: only used by CIOCKEY2 (rw) */ 206 struct crparam crk_param[CRK_MAXPARAM]; 207 }; 208 #define CRK_ALGORITM_MIN 0 209 #define CRK_MOD_EXP 0 210 #define CRK_MOD_EXP_CRT 1 211 #define CRK_DSA_SIGN 2 212 #define CRK_DSA_VERIFY 3 213 #define CRK_DH_COMPUTE_KEY 4 214 #define CRK_ALGORITHM_MAX 4 /* Keep updated - see below */ 215 216 #define CRF_MOD_EXP (1 << CRK_MOD_EXP) 217 #define CRF_MOD_EXP_CRT (1 << CRK_MOD_EXP_CRT) 218 #define CRF_DSA_SIGN (1 << CRK_DSA_SIGN) 219 #define CRF_DSA_VERIFY (1 << CRK_DSA_VERIFY) 220 #define CRF_DH_COMPUTE_KEY (1 << CRK_DH_COMPUTE_KEY) 221 222 /* 223 * done against open of /dev/crypto, to get a cloned descriptor. 224 * Please use F_SETFD against the cloned descriptor. 225 */ 226 #define CRIOGET _IOWR('c', 100, u_int32_t) 227 #define CRIOASYMFEAT CIOCASYMFEAT 228 #define CRIOFINDDEV CIOCFINDDEV 229 230 /* the following are done against the cloned descriptor */ 231 #define CIOCGSESSION _IOWR('c', 101, struct session_op) 232 #define CIOCFSESSION _IOW('c', 102, u_int32_t) 233 #define CIOCCRYPT _IOWR('c', 103, struct crypt_op) 234 #define CIOCKEY _IOWR('c', 104, struct crypt_kop) 235 #define CIOCASYMFEAT _IOR('c', 105, u_int32_t) 236 #define CIOCGSESSION2 _IOWR('c', 106, struct session2_op) 237 #define CIOCKEY2 _IOWR('c', 107, struct crypt_kop) 238 #define CIOCFINDDEV _IOWR('c', 108, struct crypt_find_op) 239 240 struct cryptotstat { 241 struct timespec acc; /* total accumulated time */ 242 struct timespec min; /* min time */ 243 struct timespec max; /* max time */ 244 u_int32_t count; /* number of observations */ 245 }; 246 247 struct cryptostats { 248 u_int32_t cs_ops; /* symmetric crypto ops submitted */ 249 u_int32_t cs_errs; /* symmetric crypto ops that failed */ 250 u_int32_t cs_kops; /* asymetric/key ops submitted */ 251 u_int32_t cs_kerrs; /* asymetric/key ops that failed */ 252 u_int32_t cs_intrs; /* crypto swi thread activations */ 253 u_int32_t cs_rets; /* crypto return thread activations */ 254 u_int32_t cs_blocks; /* symmetric op driver block */ 255 u_int32_t cs_kblocks; /* symmetric op driver block */ 256 /* 257 * When CRYPTO_TIMING is defined at compile time and the 258 * sysctl debug.crypto is set to 1, the crypto system will 259 * accumulate statistics about how long it takes to process 260 * crypto requests at various points during processing. 261 */ 262 struct cryptotstat cs_invoke; /* crypto_dipsatch -> crypto_invoke */ 263 struct cryptotstat cs_done; /* crypto_invoke -> crypto_done */ 264 struct cryptotstat cs_cb; /* crypto_done -> callback */ 265 struct cryptotstat cs_finis; /* callback -> callback return */ 266 }; 267 268 #ifdef _KERNEL 269 /* Standard initialization structure beginning */ 270 struct cryptoini { 271 int cri_alg; /* Algorithm to use */ 272 int cri_klen; /* Key length, in bits */ 273 int cri_mlen; /* Number of bytes we want from the 274 entire hash. 0 means all. */ 275 caddr_t cri_key; /* key to use */ 276 u_int8_t cri_iv[EALG_MAX_BLOCK_LEN]; /* IV to use */ 277 struct cryptoini *cri_next; 278 }; 279 280 /* Describe boundaries of a single crypto operation */ 281 struct cryptodesc { 282 int crd_skip; /* How many bytes to ignore from start */ 283 int crd_len; /* How many bytes to process */ 284 int crd_inject; /* Where to inject results, if applicable */ 285 int crd_flags; 286 287 #define CRD_F_ENCRYPT 0x01 /* Set when doing encryption */ 288 #define CRD_F_IV_PRESENT 0x02 /* When encrypting, IV is already in 289 place, so don't copy. */ 290 #define CRD_F_IV_EXPLICIT 0x04 /* IV explicitly provided */ 291 #define CRD_F_DSA_SHA_NEEDED 0x08 /* Compute SHA-1 of buffer for DSA */ 292 #define CRD_F_KEY_EXPLICIT 0x10 /* Key explicitly provided */ 293 #define CRD_F_COMP 0x0f /* Set when doing compression */ 294 295 struct cryptoini CRD_INI; /* Initialization/context data */ 296 #define crd_iv CRD_INI.cri_iv 297 #define crd_key CRD_INI.cri_key 298 #define crd_alg CRD_INI.cri_alg 299 #define crd_klen CRD_INI.cri_klen 300 301 struct cryptodesc *crd_next; 302 }; 303 304 /* Structure describing complete operation */ 305 struct cryptop { 306 TAILQ_ENTRY(cryptop) crp_next; 307 308 u_int64_t crp_sid; /* Session ID */ 309 int crp_ilen; /* Input data total length */ 310 int crp_olen; /* Result total length */ 311 312 int crp_etype; /* 313 * Error type (zero means no error). 314 * All error codes except EAGAIN 315 * indicate possible data corruption (as in, 316 * the data have been touched). On all 317 * errors, the crp_sid may have changed 318 * (reset to a new one), so the caller 319 * should always check and use the new 320 * value on future requests. 321 */ 322 int crp_flags; 323 324 #define CRYPTO_F_IMBUF 0x0001 /* Input/output are mbuf chains */ 325 #define CRYPTO_F_IOV 0x0002 /* Input/output are uio */ 326 #define CRYPTO_F_REL 0x0004 /* Must return data in same place */ 327 #define CRYPTO_F_BATCH 0x0008 /* Batch op if possible */ 328 #define CRYPTO_F_CBIMM 0x0010 /* Do callback immediately */ 329 #define CRYPTO_F_DONE 0x0020 /* Operation completed */ 330 #define CRYPTO_F_CBIFSYNC 0x0040 /* Do CBIMM if op is synchronous */ 331 332 caddr_t crp_buf; /* Data to be processed */ 333 caddr_t crp_opaque; /* Opaque pointer, passed along */ 334 struct cryptodesc *crp_desc; /* Linked list of processing descriptors */ 335 336 int (*crp_callback)(struct cryptop *); /* Callback function */ 337 338 struct bintime crp_tstamp; /* performance time stamp */ 339 }; 340 341 #define CRYPTO_BUF_CONTIG 0x0 342 #define CRYPTO_BUF_IOV 0x1 343 #define CRYPTO_BUF_MBUF 0x2 344 345 #define CRYPTO_OP_DECRYPT 0x0 346 #define CRYPTO_OP_ENCRYPT 0x1 347 348 /* 349 * Hints passed to process methods. 350 */ 351 #define CRYPTO_HINT_MORE 0x1 /* more ops coming shortly */ 352 353 struct cryptkop { 354 TAILQ_ENTRY(cryptkop) krp_next; 355 356 u_int krp_op; /* ie. CRK_MOD_EXP or other */ 357 u_int krp_status; /* return status */ 358 u_short krp_iparams; /* # of input parameters */ 359 u_short krp_oparams; /* # of output parameters */ 360 u_int krp_crid; /* desired device, etc. */ 361 u_int32_t krp_hid; 362 struct crparam krp_param[CRK_MAXPARAM]; /* kvm */ 363 int (*krp_callback)(struct cryptkop *); 364 }; 365 366 /* 367 * Session ids are 64 bits. The lower 32 bits contain a "local id" which 368 * is a driver-private session identifier. The upper 32 bits contain a 369 * "hardware id" used by the core crypto code to identify the driver and 370 * a copy of the driver's capabilities that can be used by client code to 371 * optimize operation. 372 */ 373 #define CRYPTO_SESID2HID(_sid) (((_sid) >> 32) & 0x00ffffff) 374 #define CRYPTO_SESID2CAPS(_sid) (((_sid) >> 32) & 0xff000000) 375 #define CRYPTO_SESID2LID(_sid) (((u_int32_t) (_sid)) & 0xffffffff) 376 377 MALLOC_DECLARE(M_CRYPTO_DATA); 378 379 extern int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard); 380 extern int crypto_freesession(u_int64_t sid); 381 #define CRYPTOCAP_F_HARDWARE CRYPTO_FLAG_HARDWARE 382 #define CRYPTOCAP_F_SOFTWARE CRYPTO_FLAG_SOFTWARE 383 #define CRYPTOCAP_F_SYNC 0x04000000 /* operates synchronously */ 384 extern int32_t crypto_get_driverid(device_t dev, int flags); 385 extern int crypto_find_driver(const char *); 386 extern device_t crypto_find_device_byhid(int hid); 387 extern int crypto_getcaps(int hid); 388 extern int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen, 389 u_int32_t flags); 390 extern int crypto_kregister(u_int32_t, int, u_int32_t); 391 extern int crypto_unregister(u_int32_t driverid, int alg); 392 extern int crypto_unregister_all(u_int32_t driverid); 393 extern int crypto_dispatch(struct cryptop *crp); 394 extern int crypto_kdispatch(struct cryptkop *); 395 #define CRYPTO_SYMQ 0x1 396 #define CRYPTO_ASYMQ 0x2 397 extern int crypto_unblock(u_int32_t, int); 398 extern void crypto_done(struct cryptop *crp); 399 extern void crypto_kdone(struct cryptkop *); 400 extern int crypto_getfeat(int *); 401 402 extern void crypto_freereq(struct cryptop *crp); 403 extern struct cryptop *crypto_getreq(int num); 404 405 extern int crypto_usercrypto; /* userland may do crypto requests */ 406 extern int crypto_userasymcrypto; /* userland may do asym crypto reqs */ 407 extern int crypto_devallowsoft; /* only use hardware crypto */ 408 409 /* 410 * Crypto-related utility routines used mainly by drivers. 411 * 412 * XXX these don't really belong here; but for now they're 413 * kept apart from the rest of the system. 414 */ 415 struct uio; 416 extern void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp); 417 extern void cuio_copyback(struct uio* uio, int off, int len, caddr_t cp); 418 extern struct iovec *cuio_getptr(struct uio *uio, int loc, int *off); 419 extern int cuio_apply(struct uio *uio, int off, int len, 420 int (*f)(void *, void *, u_int), void *arg); 421 422 extern void crypto_copyback(int flags, caddr_t buf, int off, int size, 423 caddr_t in); 424 extern void crypto_copydata(int flags, caddr_t buf, int off, int size, 425 caddr_t out); 426 extern int crypto_apply(int flags, caddr_t buf, int off, int len, 427 int (*f)(void *, void *, u_int), void *arg); 428 #endif /* _KERNEL */ 429 #endif /* _CRYPTO_CRYPTO_H_ */ 430