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