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