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