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