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