012856adda63c616ca73006b023003eb159d68bb
[oweals/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 #include <fcntl.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include <sys/ioctl.h>
53 #include <crypto/cryptodev.h>
54 #include <unistd.h>
55 #include <openssl/evp.h>
56 #include <openssl/objects.h>
57 #include "evp_locl.h"
58 #include <assert.h>
59
60 // longest key supported in hardware
61 #define MAX_HW_KEY      24
62
63 static int fd;
64 static int dev_failed;
65
66 static void err(const char *str)
67     {
68     fprintf(stderr,"%s: errno %d\n",str,errno);
69     }
70
71 static int dev_crypto_init(EVP_CIPHER_CTX *ctx)
72     {
73     if(dev_failed)
74         return 0;
75     if(!fd)
76         {
77         int cryptodev_fd;
78
79         if ((cryptodev_fd=open("/dev/crypto",O_RDWR,0)) < 0)
80             {
81             err("/dev/crypto");
82             dev_failed=1;
83             return 0;
84             }
85         if (ioctl(cryptodev_fd,CRIOGET,&fd) == -1)
86             {
87             err("CRIOGET failed");
88             close(cryptodev_fd);
89             dev_failed=1;
90             return 0;
91             }
92         close(cryptodev_fd);
93         }
94     if(!ctx->c.dev_crypto)
95         {
96         ctx->c.dev_crypto=OPENSSL_malloc(sizeof *ctx->c.dev_crypto);
97         memset(ctx->c.dev_crypto,'\0',sizeof *ctx->c.dev_crypto);
98         ctx->c.dev_crypto->key=OPENSSL_malloc(MAX_HW_KEY);
99         }
100     
101     return 1;
102     }
103
104 static int dev_crypto_cleanup(EVP_CIPHER_CTX *ctx)
105     {
106     if(ioctl(fd,CIOCFSESSION,ctx->c.dev_crypto->ses) == -1)
107         err("CIOCFSESSION failed");
108
109     OPENSSL_free(ctx->c.dev_crypto->key);
110     OPENSSL_free(ctx->c.dev_crypto);
111     ctx->c.dev_crypto=NULL;
112
113     return 1;
114     }
115
116 // FIXME: there should be some non-fatal way to report we fell back to s/w?
117 static int dev_crypto_des_ede3_init_key(EVP_CIPHER_CTX *ctx,
118                                         const unsigned char *key,
119                                         const unsigned char *iv, int enc)
120     {
121     if(!dev_crypto_init(ctx))
122         {
123         // fall back to using software...
124         ctx->cipher=EVP_des_ede3_cbc();
125         return ctx->cipher->init(ctx,key,iv,enc);
126         }
127     memcpy(ctx->c.dev_crypto->key,key,24);
128     
129     ctx->c.dev_crypto->cipher=CRYPTO_3DES_CBC;
130     ctx->c.dev_crypto->mac=0;
131     ctx->c.dev_crypto->keylen=24;
132
133     if (ioctl(fd,CIOCGSESSION,ctx->c.dev_crypto) == -1)
134         {
135         err("CIOCGSESSION failed");
136         // fall back to using software...
137         dev_crypto_cleanup(ctx);
138         ctx->cipher=EVP_des_ede3_cbc();
139         return ctx->cipher->init(ctx,key,iv,enc);
140         }
141     return 1;
142     }
143
144 static int dev_crypto_des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, 
145                                           unsigned char *out,
146                                           const unsigned char *in,
147                                           unsigned int inl)
148     {
149     struct crypt_op cryp;
150     unsigned char lb[8];
151
152     assert(ctx->c.dev_crypto);
153     assert(!dev_failed);
154
155     memset(&cryp,'\0',sizeof cryp);
156     cryp.ses=ctx->c.dev_crypto->ses;
157     cryp.op=ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
158     cryp.flags=0;
159     //    cryp.len=((inl+7)/8)*8;
160     cryp.len=inl;
161     assert((inl&7) == 0);
162     cryp.src=(caddr_t)in;
163     cryp.dst=(caddr_t)out;
164     cryp.mac=0;
165     cryp.iv=(caddr_t)ctx->iv;
166
167     if(!ctx->encrypt)
168         memcpy(lb,&in[cryp.len-8],8);
169
170     if (ioctl(fd, CIOCCRYPT, &cryp) == -1)
171         {
172         err("CIOCCRYPT failed");
173         abort();
174         return 0;
175         }
176
177     if(ctx->encrypt)
178         memcpy(ctx->iv,&out[cryp.len-8],8);
179     else
180         memcpy(ctx->iv,lb,8);
181
182     return 1;
183     }
184
185 BLOCK_CIPHER_def_cbc(dev_crypto_des_ede3, des_ede,NID_des_ede3, 8, 24, 8,
186                      0, dev_crypto_des_ede3_init_key,
187                      dev_crypto_cleanup, 
188                      EVP_CIPHER_set_asn1_iv,
189                      EVP_CIPHER_get_asn1_iv,
190                      NULL)