]>
Commit | Line | Data |
---|---|---|
0e360199 | 1 | /* Written by Ben Laurie, 2001 */ |
c518ade1 | 2 | /* |
0e360199 | 3 | * Copyright (c) 2001 The OpenSSL Project. All rights reserved. |
c518ade1 BL |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in | |
14 | * the documentation and/or other materials provided with the | |
15 | * distribution. | |
16 | * | |
17 | * 3. All advertising materials mentioning features or use of this | |
18 | * software must display the following acknowledgment: | |
19 | * "This product includes software developed by the OpenSSL Project | |
20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
21 | * | |
22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
23 | * endorse or promote products derived from this software without | |
24 | * prior written permission. For written permission, please contact | |
25 | * openssl-core@openssl.org. | |
26 | * | |
27 | * 5. Products derived from this software may not be called "OpenSSL" | |
28 | * nor may "OpenSSL" appear in their names without prior written | |
29 | * permission of the OpenSSL Project. | |
30 | * | |
31 | * 6. Redistributions of any form whatsoever must retain the following | |
32 | * acknowledgment: | |
33 | * "This product includes software developed by the OpenSSL Project | |
34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
35 | * | |
36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
48 | */ | |
49 | ||
47c3448a RL |
50 | #ifdef OPENSSL_OPENBSD_DEV_CRYPTO |
51 | ||
c518ade1 BL |
52 | #include <fcntl.h> |
53 | #include <stdio.h> | |
54 | #include <errno.h> | |
55 | #include <sys/ioctl.h> | |
56 | #include <crypto/cryptodev.h> | |
57 | #include <unistd.h> | |
58 | #include <openssl/evp.h> | |
59 | #include <openssl/objects.h> | |
60 | #include "evp_locl.h" | |
61 | #include <assert.h> | |
62 | ||
dbfc0f8c | 63 | /* longest key supported in hardware */ |
c518ade1 | 64 | #define MAX_HW_KEY 24 |
82b22305 | 65 | #define MAX_HW_IV 8 |
c518ade1 BL |
66 | |
67 | static int fd; | |
68 | static int dev_failed; | |
69 | ||
1ba01caa BL |
70 | typedef struct session_op session_op; |
71 | ||
72 | #define data(ctx) EVP_C_DATA(session_op,ctx) | |
73 | ||
c518ade1 BL |
74 | static void err(const char *str) |
75 | { | |
76 | fprintf(stderr,"%s: errno %d\n",str,errno); | |
77 | } | |
78 | ||
79 | static int dev_crypto_init(EVP_CIPHER_CTX *ctx) | |
80 | { | |
81 | if(dev_failed) | |
82 | return 0; | |
83 | if(!fd) | |
84 | { | |
85 | int cryptodev_fd; | |
86 | ||
87 | if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0) | |
88 | { | |
89 | err("/dev/crypto"); | |
90 | dev_failed=1; | |
91 | return 0; | |
92 | } | |
93 | if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1) | |
94 | { | |
95 | err("CRIOGET failed"); | |
96 | close(cryptodev_fd); | |
97 | dev_failed=1; | |
98 | return 0; | |
99 | } | |
100 | close(cryptodev_fd); | |
101 | } | |
1ba01caa BL |
102 | assert(data(ctx)); |
103 | memset(data(ctx),'\0',sizeof *data(ctx)); | |
104 | data(ctx)->key=OPENSSL_malloc(MAX_HW_KEY); | |
105 | ||
c518ade1 BL |
106 | return 1; |
107 | } | |
108 | ||
109 | static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx) | |
110 | { | |
82b22305 | 111 | printf("Cleanup %d\n",data(ctx)->ses); |
0713f8ab | 112 | if(ioctl(fd,CIOCFSESSION,&data(ctx)->ses) == -1) |
c518ade1 BL |
113 | err("CIOCFSESSION failed"); |
114 | ||
1ba01caa | 115 | OPENSSL_free(data(ctx)->key); |
c518ade1 BL |
116 | |
117 | return 1; | |
118 | } | |
119 | ||
dbfc0f8c | 120 | /* FIXME: there should be some non-fatal way to report we fell back to s/w? */ |
82b22305 BL |
121 | static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher, |
122 | const unsigned char *key,int klen) | |
c518ade1 BL |
123 | { |
124 | if(!dev_crypto_init(ctx)) | |
82b22305 BL |
125 | return 0; |
126 | ||
127 | assert(ctx->cipher->iv_len <= MAX_HW_IV); | |
128 | ||
129 | memcpy(data(ctx)->key,key,klen); | |
c518ade1 | 130 | |
82b22305 | 131 | data(ctx)->cipher=cipher; |
1ba01caa | 132 | data(ctx)->mac=0; |
82b22305 | 133 | data(ctx)->keylen=klen; |
c518ade1 | 134 | |
1ba01caa | 135 | if (ioctl(fd,CIOCGSESSION,data(ctx)) == -1) |
c518ade1 BL |
136 | { |
137 | err("CIOCGSESSION failed"); | |
82b22305 | 138 | return 0; |
c518ade1 | 139 | } |
82b22305 | 140 | printf("Init %d\n",data(ctx)->ses); |
c518ade1 BL |
141 | return 1; |
142 | } | |
143 | ||
82b22305 BL |
144 | static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out, |
145 | const unsigned char *in,unsigned int inl) | |
c518ade1 BL |
146 | { |
147 | struct crypt_op cryp; | |
82b22305 | 148 | unsigned char lb[MAX_HW_IV]; |
c518ade1 | 149 | |
1ba01caa | 150 | assert(data(ctx)); |
c518ade1 BL |
151 | assert(!dev_failed); |
152 | ||
153 | memset(&cryp,'\0',sizeof cryp); | |
1ba01caa | 154 | cryp.ses=data(ctx)->ses; |
c518ade1 BL |
155 | cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT; |
156 | cryp.flags=0; | |
c518ade1 | 157 | cryp.len=inl; |
82b22305 | 158 | assert((inl&ctx->cipher->block_size) == 0); |
c518ade1 BL |
159 | cryp.src=(caddr_t)in; |
160 | cryp.dst=(caddr_t)out; | |
161 | cryp.mac=0; | |
82b22305 BL |
162 | if(ctx->cipher->iv_len) |
163 | cryp.iv=(caddr_t)ctx->iv; | |
c518ade1 BL |
164 | |
165 | if(!ctx->encrypt) | |
82b22305 | 166 | memcpy(lb,&in[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len); |
c518ade1 BL |
167 | |
168 | if (ioctl(fd, CIOCCRYPT, &cryp) == -1) | |
169 | { | |
170 | err("CIOCCRYPT failed"); | |
171 | abort(); | |
172 | return 0; | |
173 | } | |
174 | ||
175 | if(ctx->encrypt) | |
82b22305 | 176 | memcpy(ctx->iv,&out[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len); |
c518ade1 | 177 | else |
82b22305 | 178 | memcpy(ctx->iv,lb,ctx->cipher->iv_len); |
c518ade1 BL |
179 | |
180 | return 1; | |
181 | } | |
182 | ||
82b22305 BL |
183 | static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx, |
184 | const unsigned char *key, | |
185 | const unsigned char *iv, int enc) | |
186 | { return dev_crypto_init_key(ctx,CRYPTO_3DES_CBC,key,24); } | |
187 | ||
188 | #define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher | |
189 | ||
1ba01caa | 190 | BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8, |
c518ade1 BL |
191 | 0, dev_crypto_des_ede3_init_key, |
192 | dev_crypto_cleanup, | |
193 | EVP_CIPHER_set_asn1_iv, | |
194 | EVP_CIPHER_get_asn1_iv, | |
195 | NULL) | |
82b22305 BL |
196 | |
197 | static int dev_crypto_rc4_init_key(EVP_CIPHER_CTX *ctx, | |
198 | const unsigned char *key, | |
199 | const unsigned char *iv, int enc) | |
200 | { return dev_crypto_init_key(ctx,CRYPTO_ARC4,key,16); } | |
201 | ||
202 | static const EVP_CIPHER r4_cipher= | |
203 | { | |
204 | NID_rc4, | |
205 | 1,16,0, /* FIXME: key should be up to 256 bytes */ | |
206 | EVP_CIPH_VARIABLE_LENGTH, | |
207 | dev_crypto_rc4_init_key, | |
208 | dev_crypto_cipher, | |
209 | dev_crypto_cleanup, | |
210 | sizeof(session_op), | |
211 | NULL, | |
212 | NULL, | |
213 | NULL | |
214 | }; | |
215 | ||
216 | const EVP_CIPHER *EVP_dev_crypto_rc4(void) | |
217 | { return &r4_cipher; } | |
218 | ||
0e063544 BL |
219 | #else |
220 | static void *dummy=&dummy; | |
47c3448a | 221 | #endif |