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