acb432109f44dc34f088699c274c0065de5b7ca5
[openssl.git] / crypto / engine / hw_atalla.c
1 /* crypto/engine/hw_atalla.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60 #include <openssl/crypto.h>
61 #include "cryptlib.h"
62 #include <openssl/dso.h>
63 #include "engine_int.h"
64 #include <openssl/engine.h>
65
66 #ifndef OPENSSL_NO_HW
67 #ifndef OPENSSL_NO_HW_ATALLA
68
69 #ifdef FLAT_INC
70 #include "atalla.h"
71 #else
72 #include "vendor_defns/atalla.h"
73 #endif
74
75 static int atalla_init(ENGINE *e);
76 static int atalla_finish(ENGINE *e);
77
78 /* BIGNUM stuff */
79 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
80                 const BIGNUM *m, BN_CTX *ctx);
81
82 /* RSA stuff */
83 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
84 /* This function is aliased to mod_exp (with the mont stuff dropped). */
85 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
86                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
87
88 /* DSA stuff */
89 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
90                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
91                 BN_CTX *ctx, BN_MONT_CTX *in_mont);
92 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
93                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
94                 BN_MONT_CTX *m_ctx);
95
96 /* DH stuff */
97 /* This function is alised to mod_exp (with the DH and mont dropped). */
98 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
99                 const BIGNUM *a, const BIGNUM *p,
100                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
101
102
103 /* Our internal RSA_METHOD that we provide pointers to */
104 static RSA_METHOD atalla_rsa =
105         {
106         "Atalla RSA method",
107         NULL,
108         NULL,
109         NULL,
110         NULL,
111         atalla_rsa_mod_exp,
112         atalla_mod_exp_mont,
113         NULL,
114         NULL,
115         0,
116         NULL,
117         NULL,
118         NULL
119         };
120
121 /* Our internal DSA_METHOD that we provide pointers to */
122 static DSA_METHOD atalla_dsa =
123         {
124         "Atalla DSA method",
125         NULL, /* dsa_do_sign */
126         NULL, /* dsa_sign_setup */
127         NULL, /* dsa_do_verify */
128         atalla_dsa_mod_exp, /* dsa_mod_exp */
129         atalla_mod_exp_dsa, /* bn_mod_exp */
130         NULL, /* init */
131         NULL, /* finish */
132         0, /* flags */
133         NULL /* app_data */
134         };
135
136 /* Our internal DH_METHOD that we provide pointers to */
137 static DH_METHOD atalla_dh =
138         {
139         "Atalla DH method",
140         NULL,
141         NULL,
142         atalla_mod_exp_dh,
143         NULL,
144         NULL,
145         0,
146         NULL
147         };
148
149 /* Our ENGINE structure. */
150 static ENGINE engine_atalla =
151         {
152         "atalla",
153         "Atalla hardware engine support",
154         &atalla_rsa,
155         &atalla_dsa,
156         &atalla_dh,
157         NULL,
158         atalla_mod_exp,
159         NULL,
160         atalla_init,
161         atalla_finish,
162         NULL, /* no ctrl() */
163         NULL, /* no load_privkey() */
164         NULL, /* no load_pubkey() */
165         0, /* no flags */
166         0, 0, /* no references */
167         NULL, NULL /* unlinked */
168         };
169
170 /* As this is only ever called once, there's no need for locking
171  * (indeed - the lock will already be held by our caller!!!) */
172 ENGINE *ENGINE_atalla()
173         {
174         const RSA_METHOD *meth1;
175         const DSA_METHOD *meth2;
176         const DH_METHOD *meth3;
177
178         /* We know that the "PKCS1_SSLeay()" functions hook properly
179          * to the atalla-specific mod_exp and mod_exp_crt so we use
180          * those functions. NB: We don't use ENGINE_openssl() or
181          * anything "more generic" because something like the RSAref
182          * code may not hook properly, and if you own one of these
183          * cards then you have the right to do RSA operations on it
184          * anyway! */ 
185         meth1 = RSA_PKCS1_SSLeay();
186         atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
187         atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
188         atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
189         atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
190
191         /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
192          * bits. */
193         meth2 = DSA_OpenSSL();
194         atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
195         atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
196         atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
197
198         /* Much the same for Diffie-Hellman */
199         meth3 = DH_OpenSSL();
200         atalla_dh.generate_key = meth3->generate_key;
201         atalla_dh.compute_key = meth3->compute_key;
202         return &engine_atalla;
203         }
204
205 /* This is a process-global DSO handle used for loading and unloading
206  * the Atalla library. NB: This is only set (or unset) during an
207  * init() or finish() call (reference counts permitting) and they're
208  * operating with global locks, so this should be thread-safe
209  * implicitly. */
210 static DSO *atalla_dso = NULL;
211
212 /* These are the function pointers that are (un)set when the library has
213  * successfully (un)loaded. */
214 static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
215 static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
216 static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = NULL;
217
218 /* These are the static string constants for the DSO file name and the function
219  * symbol names to bind to. Regrettably, the DSO name on *nix appears to be
220  * "atasi.so" rather than something more consistent like "libatasi.so". At the
221  * time of writing, I'm not sure what the file name on win32 is but clearly
222  * native name translation is not possible (eg libatasi.so on *nix, and
223  * atasi.dll on win32). For the purposes of testing, I have created a symbollic
224  * link called "libatasi.so" so that we can use native name-translation - a
225  * better solution will be needed. */
226 static const char *ATALLA_LIBNAME = "atasi";
227 static const char *ATALLA_F1 = "ASI_GetHardwareConfig";
228 static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
229 static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
230
231 /* (de)initialisation functions. */
232 static int atalla_init(ENGINE *e)
233         {
234         tfnASI_GetHardwareConfig *p1;
235         tfnASI_RSAPrivateKeyOpFn *p2;
236         tfnASI_GetPerformanceStatistics *p3;
237         /* Not sure of the origin of this magic value, but Ben's code had it
238          * and it seemed to have been working for a few people. :-) */
239         unsigned int config_buf[1024];
240
241         if(atalla_dso != NULL)
242                 {
243                 ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_ALREADY_LOADED);
244                 goto err;
245                 }
246         /* Attempt to load libatasi.so/atasi.dll/whatever. Needs to be
247          * changed unfortunately because the Atalla drivers don't have
248          * standard library names that can be platform-translated well. */
249         /* TODO: Work out how to actually map to the names the Atalla
250          * drivers really use - for now a symbollic link needs to be
251          * created on the host system from libatasi.so to atasi.so on
252          * unix variants. */
253         atalla_dso = DSO_load(NULL, ATALLA_LIBNAME, NULL, 0);
254         if(atalla_dso == NULL)
255                 {
256                 ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_DSO_FAILURE);
257                 goto err;
258                 }
259         if(!(p1 = (tfnASI_GetHardwareConfig *)DSO_bind_func(
260                                 atalla_dso, ATALLA_F1)) ||
261                         !(p2 = (tfnASI_RSAPrivateKeyOpFn *)DSO_bind_func(
262                                 atalla_dso, ATALLA_F2)) ||
263                         !(p3 = (tfnASI_GetPerformanceStatistics *)DSO_bind_func(
264                                 atalla_dso, ATALLA_F3)))
265                 {
266                 ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_DSO_FAILURE);
267                 goto err;
268                 }
269         /* Copy the pointers */
270         p_Atalla_GetHardwareConfig = p1;
271         p_Atalla_RSAPrivateKeyOpFn = p2;
272         p_Atalla_GetPerformanceStatistics = p3;
273         /* Perform a basic test to see if there's actually any unit
274          * running. */
275         if(p1(0L, config_buf) != 0)
276                 {
277                 ENGINEerr(ENGINE_F_ATALLA_INIT,ENGINE_R_UNIT_FAILURE);
278                 goto err;
279                 }
280         /* Everything's fine. */
281         return 1;
282 err:
283         if(atalla_dso)
284                 DSO_free(atalla_dso);
285         p_Atalla_GetHardwareConfig = NULL;
286         p_Atalla_RSAPrivateKeyOpFn = NULL;
287         p_Atalla_GetPerformanceStatistics = NULL;
288         return 0;
289         }
290
291 static int atalla_finish(ENGINE *e)
292         {
293         if(atalla_dso == NULL)
294                 {
295                 ENGINEerr(ENGINE_F_ATALLA_FINISH,ENGINE_R_NOT_LOADED);
296                 return 0;
297                 }
298         if(!DSO_free(atalla_dso))
299                 {
300                 ENGINEerr(ENGINE_F_ATALLA_FINISH,ENGINE_R_DSO_FAILURE);
301                 return 0;
302                 }
303         atalla_dso = NULL;
304         p_Atalla_GetHardwareConfig = NULL;
305         p_Atalla_RSAPrivateKeyOpFn = NULL;
306         p_Atalla_GetPerformanceStatistics = NULL;
307         return 1;
308         }
309
310 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
311                         const BIGNUM *m, BN_CTX *ctx)
312         {
313         /* I need somewhere to store temporary serialised values for
314          * use with the Atalla API calls. A neat cheat - I'll use
315          * BIGNUMs from the BN_CTX but access their arrays directly as
316          * byte arrays <grin>. This way I don't have to clean anything
317          * up. */
318         BIGNUM *modulus;
319         BIGNUM *exponent;
320         BIGNUM *argument;
321         BIGNUM *result;
322         RSAPrivateKey keydata;
323         int to_return, numbytes;
324
325         modulus = exponent = argument = result = NULL;
326         to_return = 0; /* expect failure */
327
328         if(!atalla_dso)
329         {
330                 ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_NOT_LOADED);
331                 goto err;
332         }
333         /* Prepare the params */
334         BN_CTX_start(ctx);
335         modulus = BN_CTX_get(ctx);
336         exponent = BN_CTX_get(ctx);
337         argument = BN_CTX_get(ctx);
338         result = BN_CTX_get(ctx);
339         if (!result)
340         {
341                 ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_BN_CTX_FULL);
342                 goto err;
343         }
344         if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
345            !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))
346         {
347                 ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_BN_EXPAND_FAIL);
348                 goto err;
349         }
350         /* Prepare the key-data */
351         memset(&keydata, 0,sizeof keydata);
352         numbytes = BN_num_bytes(m);
353         memset(exponent->d, 0, numbytes);
354         memset(modulus->d, 0, numbytes);
355         BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
356         BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
357         keydata.privateExponent.data = (unsigned char *)exponent->d;
358         keydata.privateExponent.len = numbytes;
359         keydata.modulus.data = (unsigned char *)modulus->d;
360         keydata.modulus.len = numbytes;
361         /* Prepare the argument */
362         memset(argument->d, 0, numbytes);
363         memset(result->d, 0, numbytes);
364         BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
365         /* Perform the operation */
366         if(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
367                         (unsigned char *)argument->d,
368                         keydata.modulus.len) != 0)
369         {
370                 ENGINEerr(ENGINE_F_ATALLA_MOD_EXP,ENGINE_R_REQUEST_FAILED);
371                 goto err;
372         }
373         /* Convert the response */
374         BN_bin2bn((unsigned char *)result->d, numbytes, r);
375         to_return = 1;
376 err:
377         BN_CTX_end(ctx);
378         return to_return;
379         }
380
381 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
382         {
383         BN_CTX *ctx = NULL;
384         int to_return = 0;
385
386         if(!atalla_dso)
387         {
388                 ENGINEerr(ENGINE_F_ATALLA_RSA_MOD_EXP,ENGINE_R_NOT_LOADED);
389                 goto err;
390         }
391         if((ctx = BN_CTX_new()) == NULL)
392                 goto err;
393         if(!rsa->d || !rsa->n)
394                 {
395                 ENGINEerr(ENGINE_F_ATALLA_RSA_MOD_EXP,ENGINE_R_MISSING_KEY_COMPONENTS);
396                 goto err;
397                 }
398         to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
399 err:
400         if(ctx)
401                 BN_CTX_free(ctx);
402         return to_return;
403         }
404
405 /* This code was liberated and adapted from the commented-out code in
406  * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
407  * (it doesn't have a CRT form for RSA), this function means that an
408  * Atalla system running with a DSA server certificate can handshake
409  * around 5 or 6 times faster/more than an equivalent system running with
410  * RSA. Just check out the "signs" statistics from the RSA and DSA parts
411  * of "openssl speed -engine atalla dsa1024 rsa1024". */
412 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
413                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
414                 BN_CTX *ctx, BN_MONT_CTX *in_mont)
415         {
416         BIGNUM t;
417         int to_return = 0;
418  
419         BN_init(&t);
420         /* let rr = a1 ^ p1 mod m */
421         if (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end;
422         /* let t = a2 ^ p2 mod m */
423         if (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end;
424         /* let rr = rr * t mod m */
425         if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
426         to_return = 1;
427 end:
428         BN_free(&t);
429         return to_return;
430         }
431
432
433 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
434                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
435                 BN_MONT_CTX *m_ctx)
436         {
437         return atalla_mod_exp(r, a, p, m, ctx);
438         }
439
440 /* This function is aliased to mod_exp (with the mont stuff dropped). */
441 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
442                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
443         {
444         return atalla_mod_exp(r, a, p, m, ctx);
445         }
446
447 /* This function is aliased to mod_exp (with the dh and mont dropped). */
448 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
449                 const BIGNUM *a, const BIGNUM *p,
450                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
451         {
452         return atalla_mod_exp(r, a, p, m, ctx);
453         }
454
455 #endif /* !OPENSSL_NO_HW_ATALLA */
456 #endif /* !OPENSSL_NO_HW */