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

Archive Download this file



interactive