550145e4948b2d1638ae842f502e5af64d08bcb9
[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 <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
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>
64 #include <assert.h>
65
66 /* longest key supported in hardware */
67 #define MAX_HW_KEY      24
68 #define MAX_HW_IV       8
69
70 #define MD5_DIGEST_LENGTH       16
71 #define MD5_CBLOCK              64
72
73 static int fd;
74 static int dev_failed;
75
76 typedef struct session_op session_op;
77
78 #define CDATA(ctx) EVP_C_DATA(session_op,ctx)
79
80 static void err(const char *str)
81     {
82     fprintf(stderr,"%s: errno %d\n",str,errno);
83     }
84
85 static int dev_crypto_init(session_op *ses)
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         }
108     assert(ses);
109     memset(ses,'\0',sizeof *ses);
110
111     return 1;
112     }
113
114 static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
115     {
116     if(ioctl(fd,CIOCFSESSION,&CDATA(ctx)->ses) == -1)
117         err("CIOCFSESSION failed");
118
119     OPENSSL_free(CDATA(ctx)->key);
120
121     return 1;
122     }
123
124 static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher,
125                                const unsigned char *key,int klen)
126     {
127     if(!dev_crypto_init(CDATA(ctx)))
128         return 0;
129
130     CDATA(ctx)->key=OPENSSL_malloc(MAX_HW_KEY);
131
132     assert(ctx->cipher->iv_len <= MAX_HW_IV);
133
134     memcpy(CDATA(ctx)->key,key,klen);
135     
136     CDATA(ctx)->cipher=cipher;
137     CDATA(ctx)->keylen=klen;
138
139     if (ioctl(fd,CIOCGSESSION,CDATA(ctx)) == -1)
140         {
141         err("CIOCGSESSION failed");
142         return 0;
143         }
144     return 1;
145     }
146
147 static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
148                              const unsigned char *in,unsigned int inl)
149     {
150     struct crypt_op cryp;
151     unsigned char lb[MAX_HW_IV];
152
153     if(!inl)
154         return 1;
155
156     assert(CDATA(ctx));
157     assert(!dev_failed);
158
159     memset(&cryp,'\0',sizeof cryp);
160     cryp.ses=CDATA(ctx)->ses;
161     cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
162     cryp.flags=0;
163     cryp.len=inl;
164     assert((inl&(ctx->cipher->block_size-1)) == 0);
165     cryp.src=(caddr_t)in;
166     cryp.dst=(caddr_t)out;
167     cryp.mac=0;
168     if(ctx->cipher->iv_len)
169         cryp.iv=(caddr_t)ctx->iv;
170
171     if(!ctx->encrypt)
172         memcpy(lb,&in[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
173
174     if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
175         {
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             }
222         }
223
224     if(ctx->encrypt)
225         memcpy(ctx->iv,&out[cryp.len-ctx->cipher->iv_len],ctx->cipher->iv_len);
226     else
227         memcpy(ctx->iv,lb,ctx->cipher->iv_len);
228
229     return 1;
230     }
231
232 static 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
239 BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, session_op, NID_des_ede3, 8, 24, 8,
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)
245
246 static 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
251 static 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
265 const EVP_CIPHER *EVP_dev_crypto_rc4(void)
266     { return &r4_cipher; }
267
268 typedef struct
269     {
270     session_op sess;
271     char *data;
272     int len;
273     unsigned char md[EVP_MAX_MD_SIZE];
274     } MD_DATA;
275
276 static int dev_crypto_init_digest(MD_DATA *md_data,int mac)
277     {
278     if(!dev_crypto_init(&md_data->sess))
279         return 0;
280
281     md_data->len=0;
282     md_data->data=NULL;
283
284     md_data->sess.mac=mac;
285
286     if (ioctl(fd,CIOCGSESSION,&md_data->sess) == -1)
287         {
288         err("CIOCGSESSION failed");
289         return 0;
290         }
291     return 1;
292     }
293
294 /* FIXME: if device can do chained MACs, then don't accumulate */
295 /* FIXME: move accumulation to the framework */
296 static int dev_crypto_md5_init(EVP_MD_CTX *ctx)
297     { return dev_crypto_init_digest(ctx->md_data,CRYPTO_MD5); }
298
299 static int do_digest(int ses,unsigned char *md,const void *data,int len)
300     {
301     struct crypt_op cryp;
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         }
314
315     memset(&cryp,'\0',sizeof cryp);
316     cryp.ses=ses;
317     cryp.op=COP_ENCRYPT;/* required to do the MAC rather than check it */
318     cryp.len=len;
319     cryp.src=(caddr_t)data;
320     cryp.dst=(caddr_t)data; // FIXME!!!
321     cryp.mac=(caddr_t)md;
322
323     if(ioctl(fd, CIOCCRYPT, &cryp) == -1)
324         {
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             }
348         }
349     printf("done\n");
350
351     return 1;
352     }
353
354 static 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
369 static 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
388 static 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);
398
399     return 1;
400     }
401
402 static const EVP_MD md5_md=
403     {
404     NID_md5,
405     NID_md5WithRSAEncryption,
406     MD5_DIGEST_LENGTH,
407     EVP_MD_FLAG_ONESHOT,        // XXX: set according to device info...
408     dev_crypto_md5_init,
409     dev_crypto_md5_update,
410     dev_crypto_md5_final,
411     dev_crypto_md5_copy,
412     EVP_PKEY_RSA_method,
413     MD5_CBLOCK,
414     sizeof(MD_DATA),
415     };
416
417 const EVP_MD *EVP_dev_crypto_md5(void)
418     { return &md5_md; }
419
420 #endif