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