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