]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/openbsd_hw.c
Look up MD5 by name.
[thirdparty/openssl.git] / crypto / evp / openbsd_hw.c
CommitLineData
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
931a23a5
BM
50#include <openssl/evp.h>
51#include <openssl/objects.h>
52#include <openssl/rsa.h>
53#include "evp_locl.h"
54
55/* check flag after OpenSSL headers to ensure make depend works */
56#ifdef OPENSSL_OPENBSD_DEV_CRYPTO
57
c518ade1
BL
58#include <fcntl.h>
59#include <stdio.h>
60#include <errno.h>
61#include <sys/ioctl.h>
62#include <crypto/cryptodev.h>
63#include <unistd.h>
c518ade1
BL
64#include <assert.h>
65
dbfc0f8c 66/* longest key supported in hardware */
c518ade1 67#define MAX_HW_KEY 24
82b22305 68#define MAX_HW_IV 8
c518ade1 69
35e33f0e
BL
70#define MD5_DIGEST_LENGTH 16
71#define MD5_CBLOCK 64
72
c518ade1
BL
73static int fd;
74static int dev_failed;
75
1ba01caa
BL
76typedef struct session_op session_op;
77
26188931 78#define CDATA(ctx) EVP_C_DATA(session_op,ctx)
1ba01caa 79
c518ade1
BL
80static void err(const char *str)
81 {
82 fprintf(stderr,"%s: errno %d\n",str,errno);
83 }
84
35e33f0e 85static int dev_crypto_init(session_op *ses)
c518ade1
BL
86 {
87 if(dev_failed)
88 return 0;
89 if(!fd)
90 {
91 int cryptodev_fd;
92
93 if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0)
94 {
95 err("/dev/crypto");
96 dev_failed=1;
97 return 0;
98 }
99 if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1)
100 {
101 err("CRIOGET failed");
102 close(cryptodev_fd);
103 dev_failed=1;
104 return 0;
105 }
106 close(cryptodev_fd);
107 }
35e33f0e
BL
108 assert(ses);
109 memset(ses,'\0',sizeof *ses);
1ba01caa 110
c518ade1
BL
111 return 1;
112 }
113
114static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
115 {
26188931 116 if(ioctl(fd,CIOCFSESSION,&CDATA(ctx)->ses) == -1)
c518ade1
BL
117 err("CIOCFSESSION failed");
118
26188931 119 OPENSSL_free(CDATA(ctx)->key);
c518ade1
BL
120
121 return 1;
122 }
123
82b22305
BL
124static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher,
125 const unsigned char *key,int klen)
c518ade1 126 {
26188931 127 if(!dev_crypto_init(CDATA(ctx)))
82b22305
BL
128 return 0;
129
26188931 130 CDATA(ctx)->key=OPENSSL_malloc(MAX_HW_KEY);
35e33f0e 131
82b22305
BL
132 assert(ctx->cipher->iv_len <= MAX_HW_IV);
133
26188931 134 memcpy(CDATA(ctx)->key,key,klen);
c518ade1 135
26188931
BL
136 CDATA(ctx)->cipher=cipher;
137 CDATA(ctx)->keylen=klen;
35e33f0e 138
26188931 139 if (ioctl(fd,CIOCGSESSION,CDATA(ctx)) == -1)
35e33f0e
BL
140 {
141 err("CIOCGSESSION failed");
142 return 0;
143 }
35e33f0e
BL
144 return 1;
145 }
146
82b22305
BL
147static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
148 const unsigned char *in,unsigned int inl)
c518ade1
BL
149 {
150 struct crypt_op cryp;
82b22305 151 unsigned char lb[MAX_HW_IV];
c518ade1 152
35e33f0e
BL
153 if(!inl)
154 return 1;
155
26188931 156 assert(CDATA(ctx));
c518ade1
BL
157 assert(!dev_failed);
158
159 memset(&cryp,'\0',sizeof cryp);
26188931 160 cryp.ses=CDATA(ctx)->ses;
c518ade1
BL
161 cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
162 cryp.flags=0;
c518ade1 163 cryp.len=inl;
35e33f0e 164 assert((inl&(ctx->cipher->block_size-1)) == 0);
c518ade1
BL
165 cryp.src=(caddr_t)in;
166 cryp.dst=(caddr_t)out;
167 cryp.mac=0;
82b22305
BL
168 if(ctx->cipher->iv_len)
169 cryp.iv=(caddr_t)ctx->iv;
c518ade1
BL
170
171 if(!ctx->encrypt)
82b22305 172 memcpy(lb,&in[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
c518ade1 173
35e33f0e 174 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
c518ade1 175 {
35e33f0e
BL
176 if(errno == EINVAL) /* buffers are misaligned */
177 {
178 unsigned int cinl=0;
179 char *cin=NULL;
180 char *cout=NULL;
181
182 /* NB: this can only make cinl != inl with stream ciphers */
183 cinl=(inl+3)/4*4;
184
185 if(((unsigned long)in&3) || cinl != inl)
186 {
187 cin=OPENSSL_malloc(cinl);
188 memcpy(cin,in,inl);
189 cryp.src=cin;
190 }
191
192 if(((unsigned long)out&3) || cinl != inl)
193 {
194 cout=OPENSSL_malloc(cinl);
195 cryp.dst=cout;
196 }
197
198 cryp.len=cinl;
199
200 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
201 {
202 err("CIOCCRYPT(2) failed");
203 printf("src=%p dst=%p\n",cryp.src,cryp.dst);
204 abort();
205 return 0;
206 }
207
208 if(cout)
209 {
210 memcpy(out,cout,inl);
211 OPENSSL_free(cout);
212 }
213 if(cin)
214 OPENSSL_free(cin);
215 }
216 else
217 {
218 err("CIOCCRYPT failed");
219 abort();
220 return 0;
221 }
c518ade1
BL
222 }
223
224 if(ctx->encrypt)
82b22305 225 memcpy(ctx->iv,&out[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
c518ade1 226 else
82b22305 227 memcpy(ctx->iv,lb,ctx->cipher->iv_len);
c518ade1
BL
228
229 return 1;
230 }
231
82b22305
BL
232static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
233 const unsigned char *key,
234 const unsigned char *iv, int enc)
235 { return dev_crypto_init_key(ctx,CRYPTO_3DES_CBC,key,24); }
236
237#define dev_crypto_des_ede3_cbc_cipher dev_crypto_cipher
238
1ba01caa 239BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8,
c518ade1
BL
240 0, dev_crypto_des_ede3_init_key,
241 dev_crypto_cleanup,
242 EVP_CIPHER_set_asn1_iv,
243 EVP_CIPHER_get_asn1_iv,
244 NULL)
82b22305
BL
245
246static int dev_crypto_rc4_init_key(EVP_CIPHER_CTX *ctx,
247 const unsigned char *key,
248 const unsigned char *iv, int enc)
249 { return dev_crypto_init_key(ctx,CRYPTO_ARC4,key,16); }
250
251static const EVP_CIPHER r4_cipher=
252 {
253 NID_rc4,
254 1,16,0, /* FIXME: key should be up to 256 bytes */
255 EVP_CIPH_VARIABLE_LENGTH,
256 dev_crypto_rc4_init_key,
257 dev_crypto_cipher,
258 dev_crypto_cleanup,
259 sizeof(session_op),
260 NULL,
261 NULL,
262 NULL
263 };
264
265const EVP_CIPHER *EVP_dev_crypto_rc4(void)
266 { return &r4_cipher; }
267
26188931
BL
268typedef struct
269 {
270 session_op sess;
271 char *data;
272 int len;
273 unsigned char md[EVP_MAX_MD_SIZE];
274 } MD_DATA;
35e33f0e 275
26188931 276static int dev_crypto_init_digest(MD_DATA *md_data,int mac)
35e33f0e 277 {
26188931
BL
278 if(!dev_crypto_init(&md_data->sess))
279 return 0;
35e33f0e 280
26188931
BL
281 md_data->len=0;
282 md_data->data=NULL;
35e33f0e 283
26188931
BL
284 md_data->sess.mac=mac;
285
286 if (ioctl(fd,CIOCGSESSION,&md_data->sess) == -1)
35e33f0e 287 {
26188931 288 err("CIOCGSESSION failed");
35e33f0e
BL
289 return 0;
290 }
35e33f0e
BL
291 return 1;
292 }
293
26188931
BL
294/* FIXME: if device can do chained MACs, then don't accumulate */
295/* FIXME: move accumulation to the framework */
296static int dev_crypto_md5_init(EVP_MD_CTX *ctx)
297 { return dev_crypto_init_digest(ctx->md_data,CRYPTO_MD5); }
298
299static int do_digest(int ses,unsigned char *md,const void *data,int len)
35e33f0e
BL
300 {
301 struct crypt_op cryp;
26188931
BL
302 static unsigned char md5zero[16]=
303 {
304 0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,
305 0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e
306 };
307
308 /* some cards can't do zero length */
309 if(!len)
310 {
311 memcpy(md,md5zero,16);
312 return 1;
313 }
35e33f0e 314
35e33f0e 315 memset(&cryp,'\0',sizeof cryp);
26188931 316 cryp.ses=ses;
35e33f0e 317 cryp.op=COP_ENCRYPT;/* required to do the MAC rather than check it */
26188931
BL
318 cryp.len=len;
319 cryp.src=(caddr_t)data;
320 cryp.dst=(caddr_t)data; // FIXME!!!
321 cryp.mac=(caddr_t)md;
35e33f0e
BL
322
323 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
324 {
26188931
BL
325 if(errno == EINVAL) /* buffer is misaligned */
326 {
327 char *dcopy;
328
329 dcopy=OPENSSL_malloc(len);
330 memcpy(dcopy,data,len);
331 cryp.src=dcopy;
332 cryp.dst=cryp.src; // FIXME!!!
333
334 if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
335 {
336 err("CIOCCRYPT(MAC2) failed");
337 abort();
338 return 0;
339 }
340 OPENSSL_free(dcopy);
341 }
342 else
343 {
344 err("CIOCCRYPT(MAC) failed");
345 abort();
346 return 0;
347 }
35e33f0e 348 }
26188931
BL
349 printf("done\n");
350
351 return 1;
352 }
353
354static int dev_crypto_md5_update(EVP_MD_CTX *ctx,const void *data,
355 unsigned long len)
356 {
357 MD_DATA *md_data=ctx->md_data;
358
359 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
360 return do_digest(md_data->sess.ses,md_data->md,data,len);
361
362 md_data->data=OPENSSL_realloc(md_data->data,md_data->len+len);
363 memcpy(md_data->data+md_data->len,data,len);
364 md_data->len+=len;
365
366 return 1;
367 }
368
369static int dev_crypto_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
370 {
371 int ret;
372 MD_DATA *md_data=ctx->md_data;
373
374 if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
375 {
376 memcpy(md,md_data->md,MD5_DIGEST_LENGTH);
377 return 1;
378 }
379
380 ret=do_digest(md_data->sess.ses,md,md_data->data,md_data->len);
381 OPENSSL_free(md_data->data);
382 md_data->data=NULL;
383 md_data->len=0;
384
385 return ret;
386 }
387
388static int dev_crypto_md5_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
389 {
390 const MD_DATA *from_md=from->md_data;
391 MD_DATA *to_md=to->md_data;
392
393 // How do we copy sessions?
394 assert(from->digest->flags&EVP_MD_FLAG_ONESHOT);
395
396 to_md->data=OPENSSL_malloc(from_md->len);
397 memcpy(to_md->data,from_md->data,from_md->len);
35e33f0e 398
35e33f0e
BL
399 return 1;
400 }
401
402static const EVP_MD md5_md=
403 {
404 NID_md5,
405 NID_md5WithRSAEncryption,
406 MD5_DIGEST_LENGTH,
26188931 407 EVP_MD_FLAG_ONESHOT, // XXX: set according to device info...
35e33f0e
BL
408 dev_crypto_md5_init,
409 dev_crypto_md5_update,
410 dev_crypto_md5_final,
26188931 411 dev_crypto_md5_copy,
35e33f0e
BL
412 EVP_PKEY_RSA_method,
413 MD5_CBLOCK,
26188931 414 sizeof(MD_DATA),
35e33f0e
BL
415 };
416
417const EVP_MD *EVP_dev_crypto_md5(void)
418 { return &md5_md; }
419
47c3448a 420#endif