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