1160814Ssimon/* crypto/engine/hw_atalla.c */
2296465Sdelphij/*
3296465Sdelphij * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4296465Sdelphij * 2000.
5160814Ssimon */
6160814Ssimon/* ====================================================================
7160814Ssimon * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
8160814Ssimon *
9160814Ssimon * Redistribution and use in source and binary forms, with or without
10160814Ssimon * modification, are permitted provided that the following conditions
11160814Ssimon * are met:
12160814Ssimon *
13160814Ssimon * 1. Redistributions of source code must retain the above copyright
14296465Sdelphij *    notice, this list of conditions and the following disclaimer.
15160814Ssimon *
16160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
17160814Ssimon *    notice, this list of conditions and the following disclaimer in
18160814Ssimon *    the documentation and/or other materials provided with the
19160814Ssimon *    distribution.
20160814Ssimon *
21160814Ssimon * 3. All advertising materials mentioning features or use of this
22160814Ssimon *    software must display the following acknowledgment:
23160814Ssimon *    "This product includes software developed by the OpenSSL Project
24160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25160814Ssimon *
26160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27160814Ssimon *    endorse or promote products derived from this software without
28160814Ssimon *    prior written permission. For written permission, please contact
29160814Ssimon *    licensing@OpenSSL.org.
30160814Ssimon *
31160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
32160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
33160814Ssimon *    permission of the OpenSSL Project.
34160814Ssimon *
35160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
36160814Ssimon *    acknowledgment:
37160814Ssimon *    "This product includes software developed by the OpenSSL Project
38160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39160814Ssimon *
40160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
52160814Ssimon * ====================================================================
53160814Ssimon *
54160814Ssimon * This product includes cryptographic software written by Eric Young
55160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
56160814Ssimon * Hudson (tjh@cryptsoft.com).
57160814Ssimon *
58160814Ssimon */
59160814Ssimon
60160814Ssimon#include <stdio.h>
61160814Ssimon#include <string.h>
62160814Ssimon#include <openssl/crypto.h>
63160814Ssimon#include <openssl/buffer.h>
64160814Ssimon#include <openssl/dso.h>
65160814Ssimon#include <openssl/engine.h>
66160814Ssimon#ifndef OPENSSL_NO_RSA
67296465Sdelphij# include <openssl/rsa.h>
68160814Ssimon#endif
69160814Ssimon#ifndef OPENSSL_NO_DSA
70296465Sdelphij# include <openssl/dsa.h>
71160814Ssimon#endif
72160814Ssimon#ifndef OPENSSL_NO_DH
73296465Sdelphij# include <openssl/dh.h>
74160814Ssimon#endif
75160814Ssimon#include <openssl/bn.h>
76160814Ssimon
77160814Ssimon#ifndef OPENSSL_NO_HW
78296465Sdelphij# ifndef OPENSSL_NO_HW_ATALLA
79160814Ssimon
80296465Sdelphij#  ifdef FLAT_INC
81296465Sdelphij#   include "atalla.h"
82296465Sdelphij#  else
83296465Sdelphij#   include "vendor_defns/atalla.h"
84296465Sdelphij#  endif
85160814Ssimon
86296465Sdelphij#  define ATALLA_LIB_NAME "atalla engine"
87296465Sdelphij#  include "e_atalla_err.c"
88160814Ssimon
89160814Ssimonstatic int atalla_destroy(ENGINE *e);
90160814Ssimonstatic int atalla_init(ENGINE *e);
91160814Ssimonstatic int atalla_finish(ENGINE *e);
92296465Sdelphijstatic int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
93160814Ssimon
94160814Ssimon/* BIGNUM stuff */
95160814Ssimonstatic int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
96296465Sdelphij                          const BIGNUM *m, BN_CTX *ctx);
97160814Ssimon
98296465Sdelphij#  ifndef OPENSSL_NO_RSA
99160814Ssimon/* RSA stuff */
100296465Sdelphijstatic int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
101296465Sdelphij                              BN_CTX *ctx);
102160814Ssimon/* This function is aliased to mod_exp (with the mont stuff dropped). */
103160814Ssimonstatic int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
104296465Sdelphij                               const BIGNUM *m, BN_CTX *ctx,
105296465Sdelphij                               BN_MONT_CTX *m_ctx);
106296465Sdelphij#  endif
107160814Ssimon
108296465Sdelphij#  ifndef OPENSSL_NO_DSA
109160814Ssimon/* DSA stuff */
110160814Ssimonstatic int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
111296465Sdelphij                              BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
112296465Sdelphij                              BN_CTX *ctx, BN_MONT_CTX *in_mont);
113160814Ssimonstatic int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
114296465Sdelphij                              const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
115296465Sdelphij                              BN_MONT_CTX *m_ctx);
116296465Sdelphij#  endif
117160814Ssimon
118296465Sdelphij#  ifndef OPENSSL_NO_DH
119160814Ssimon/* DH stuff */
120160814Ssimon/* This function is alised to mod_exp (with the DH and mont dropped). */
121160814Ssimonstatic int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
122296465Sdelphij                             const BIGNUM *a, const BIGNUM *p,
123296465Sdelphij                             const BIGNUM *m, BN_CTX *ctx,
124296465Sdelphij                             BN_MONT_CTX *m_ctx);
125296465Sdelphij#  endif
126160814Ssimon
127160814Ssimon/* The definitions for control commands specific to this engine */
128296465Sdelphij#  define ATALLA_CMD_SO_PATH              ENGINE_CMD_BASE
129160814Ssimonstatic const ENGINE_CMD_DEFN atalla_cmd_defns[] = {
130296465Sdelphij    {ATALLA_CMD_SO_PATH,
131296465Sdelphij     "SO_PATH",
132296465Sdelphij     "Specifies the path to the 'atasi' shared library",
133296465Sdelphij     ENGINE_CMD_FLAG_STRING},
134296465Sdelphij    {0, NULL, NULL, 0}
135296465Sdelphij};
136160814Ssimon
137296465Sdelphij#  ifndef OPENSSL_NO_RSA
138160814Ssimon/* Our internal RSA_METHOD that we provide pointers to */
139296465Sdelphijstatic RSA_METHOD atalla_rsa = {
140296465Sdelphij    "Atalla RSA method",
141296465Sdelphij    NULL,
142296465Sdelphij    NULL,
143296465Sdelphij    NULL,
144296465Sdelphij    NULL,
145296465Sdelphij    atalla_rsa_mod_exp,
146296465Sdelphij    atalla_mod_exp_mont,
147296465Sdelphij    NULL,
148296465Sdelphij    NULL,
149296465Sdelphij    0,
150296465Sdelphij    NULL,
151296465Sdelphij    NULL,
152296465Sdelphij    NULL,
153296465Sdelphij    NULL
154296465Sdelphij};
155296465Sdelphij#  endif
156160814Ssimon
157296465Sdelphij#  ifndef OPENSSL_NO_DSA
158160814Ssimon/* Our internal DSA_METHOD that we provide pointers to */
159296465Sdelphijstatic DSA_METHOD atalla_dsa = {
160296465Sdelphij    "Atalla DSA method",
161296465Sdelphij    NULL,                       /* dsa_do_sign */
162296465Sdelphij    NULL,                       /* dsa_sign_setup */
163296465Sdelphij    NULL,                       /* dsa_do_verify */
164296465Sdelphij    atalla_dsa_mod_exp,         /* dsa_mod_exp */
165296465Sdelphij    atalla_mod_exp_dsa,         /* bn_mod_exp */
166296465Sdelphij    NULL,                       /* init */
167296465Sdelphij    NULL,                       /* finish */
168296465Sdelphij    0,                          /* flags */
169296465Sdelphij    NULL,                       /* app_data */
170296465Sdelphij    NULL,                       /* dsa_paramgen */
171296465Sdelphij    NULL                        /* dsa_keygen */
172296465Sdelphij};
173296465Sdelphij#  endif
174160814Ssimon
175296465Sdelphij#  ifndef OPENSSL_NO_DH
176160814Ssimon/* Our internal DH_METHOD that we provide pointers to */
177296465Sdelphijstatic DH_METHOD atalla_dh = {
178296465Sdelphij    "Atalla DH method",
179296465Sdelphij    NULL,
180296465Sdelphij    NULL,
181296465Sdelphij    atalla_mod_exp_dh,
182296465Sdelphij    NULL,
183296465Sdelphij    NULL,
184296465Sdelphij    0,
185296465Sdelphij    NULL,
186296465Sdelphij    NULL
187296465Sdelphij};
188296465Sdelphij#  endif
189160814Ssimon
190160814Ssimon/* Constants used when creating the ENGINE */
191160814Ssimonstatic const char *engine_atalla_id = "atalla";
192160814Ssimonstatic const char *engine_atalla_name = "Atalla hardware engine support";
193160814Ssimon
194296465Sdelphij/*
195296465Sdelphij * This internal function is used by ENGINE_atalla() and possibly by the
196296465Sdelphij * "dynamic" ENGINE support too
197296465Sdelphij */
198160814Ssimonstatic int bind_helper(ENGINE *e)
199296465Sdelphij{
200296465Sdelphij#  ifndef OPENSSL_NO_RSA
201296465Sdelphij    const RSA_METHOD *meth1;
202296465Sdelphij#  endif
203296465Sdelphij#  ifndef OPENSSL_NO_DSA
204296465Sdelphij    const DSA_METHOD *meth2;
205296465Sdelphij#  endif
206296465Sdelphij#  ifndef OPENSSL_NO_DH
207296465Sdelphij    const DH_METHOD *meth3;
208296465Sdelphij#  endif
209296465Sdelphij    if (!ENGINE_set_id(e, engine_atalla_id) ||
210296465Sdelphij        !ENGINE_set_name(e, engine_atalla_name) ||
211296465Sdelphij#  ifndef OPENSSL_NO_RSA
212296465Sdelphij        !ENGINE_set_RSA(e, &atalla_rsa) ||
213296465Sdelphij#  endif
214296465Sdelphij#  ifndef OPENSSL_NO_DSA
215296465Sdelphij        !ENGINE_set_DSA(e, &atalla_dsa) ||
216296465Sdelphij#  endif
217296465Sdelphij#  ifndef OPENSSL_NO_DH
218296465Sdelphij        !ENGINE_set_DH(e, &atalla_dh) ||
219296465Sdelphij#  endif
220296465Sdelphij        !ENGINE_set_destroy_function(e, atalla_destroy) ||
221296465Sdelphij        !ENGINE_set_init_function(e, atalla_init) ||
222296465Sdelphij        !ENGINE_set_finish_function(e, atalla_finish) ||
223296465Sdelphij        !ENGINE_set_ctrl_function(e, atalla_ctrl) ||
224296465Sdelphij        !ENGINE_set_cmd_defns(e, atalla_cmd_defns))
225296465Sdelphij        return 0;
226160814Ssimon
227296465Sdelphij#  ifndef OPENSSL_NO_RSA
228296465Sdelphij    /*
229296465Sdelphij     * We know that the "PKCS1_SSLeay()" functions hook properly to the
230296465Sdelphij     * atalla-specific mod_exp and mod_exp_crt so we use those functions. NB:
231296465Sdelphij     * We don't use ENGINE_openssl() or anything "more generic" because
232296465Sdelphij     * something like the RSAref code may not hook properly, and if you own
233296465Sdelphij     * one of these cards then you have the right to do RSA operations on it
234296465Sdelphij     * anyway!
235296465Sdelphij     */
236296465Sdelphij    meth1 = RSA_PKCS1_SSLeay();
237296465Sdelphij    atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
238296465Sdelphij    atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
239296465Sdelphij    atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
240296465Sdelphij    atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
241296465Sdelphij#  endif
242160814Ssimon
243296465Sdelphij#  ifndef OPENSSL_NO_DSA
244296465Sdelphij    /*
245296465Sdelphij     * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits.
246296465Sdelphij     */
247296465Sdelphij    meth2 = DSA_OpenSSL();
248296465Sdelphij    atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
249296465Sdelphij    atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
250296465Sdelphij    atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
251296465Sdelphij#  endif
252160814Ssimon
253296465Sdelphij#  ifndef OPENSSL_NO_DH
254296465Sdelphij    /* Much the same for Diffie-Hellman */
255296465Sdelphij    meth3 = DH_OpenSSL();
256296465Sdelphij    atalla_dh.generate_key = meth3->generate_key;
257296465Sdelphij    atalla_dh.compute_key = meth3->compute_key;
258296465Sdelphij#  endif
259160814Ssimon
260296465Sdelphij    /* Ensure the atalla error handling is set up */
261296465Sdelphij    ERR_load_ATALLA_strings();
262296465Sdelphij    return 1;
263296465Sdelphij}
264160814Ssimon
265296465Sdelphij#  ifdef OPENSSL_NO_DYNAMIC_ENGINE
266160814Ssimonstatic ENGINE *engine_atalla(void)
267296465Sdelphij{
268296465Sdelphij    ENGINE *ret = ENGINE_new();
269296465Sdelphij    if (!ret)
270296465Sdelphij        return NULL;
271296465Sdelphij    if (!bind_helper(ret)) {
272296465Sdelphij        ENGINE_free(ret);
273296465Sdelphij        return NULL;
274296465Sdelphij    }
275296465Sdelphij    return ret;
276296465Sdelphij}
277160814Ssimon
278160814Ssimonvoid ENGINE_load_atalla(void)
279296465Sdelphij{
280296465Sdelphij    /* Copied from eng_[openssl|dyn].c */
281296465Sdelphij    ENGINE *toadd = engine_atalla();
282296465Sdelphij    if (!toadd)
283296465Sdelphij        return;
284296465Sdelphij    ENGINE_add(toadd);
285296465Sdelphij    ENGINE_free(toadd);
286296465Sdelphij    ERR_clear_error();
287296465Sdelphij}
288296465Sdelphij#  endif
289160814Ssimon
290296465Sdelphij/*
291296465Sdelphij * This is a process-global DSO handle used for loading and unloading the
292296465Sdelphij * Atalla library. NB: This is only set (or unset) during an init() or
293296465Sdelphij * finish() call (reference counts permitting) and they're operating with
294296465Sdelphij * global locks, so this should be thread-safe implicitly.
295296465Sdelphij */
296160814Ssimonstatic DSO *atalla_dso = NULL;
297160814Ssimon
298296465Sdelphij/*
299296465Sdelphij * These are the function pointers that are (un)set when the library has
300296465Sdelphij * successfully (un)loaded.
301296465Sdelphij */
302160814Ssimonstatic tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
303160814Ssimonstatic tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
304296465Sdelphijstatic tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics =
305296465Sdelphij    NULL;
306160814Ssimon
307296465Sdelphij/*
308296465Sdelphij * These are the static string constants for the DSO file name and the
309296465Sdelphij * function symbol names to bind to. Regrettably, the DSO name on *nix
310296465Sdelphij * appears to be "atasi.so" rather than something more consistent like
311296465Sdelphij * "libatasi.so". At the time of writing, I'm not sure what the file name on
312296465Sdelphij * win32 is but clearly native name translation is not possible (eg
313296465Sdelphij * libatasi.so on *nix, and atasi.dll on win32). For the purposes of testing,
314296465Sdelphij * I have created a symbollic link called "libatasi.so" so that we can use
315296465Sdelphij * native name-translation - a better solution will be needed.
316296465Sdelphij */
317160814Ssimonstatic const char *ATALLA_LIBNAME = NULL;
318160814Ssimonstatic const char *get_ATALLA_LIBNAME(void)
319296465Sdelphij{
320296465Sdelphij    if (ATALLA_LIBNAME)
321296465Sdelphij        return ATALLA_LIBNAME;
322296465Sdelphij    return "atasi";
323296465Sdelphij}
324296465Sdelphij
325160814Ssimonstatic void free_ATALLA_LIBNAME(void)
326296465Sdelphij{
327296465Sdelphij    if (ATALLA_LIBNAME)
328296465Sdelphij        OPENSSL_free((void *)ATALLA_LIBNAME);
329296465Sdelphij    ATALLA_LIBNAME = NULL;
330296465Sdelphij}
331296465Sdelphij
332160814Ssimonstatic long set_ATALLA_LIBNAME(const char *name)
333296465Sdelphij{
334296465Sdelphij    free_ATALLA_LIBNAME();
335296465Sdelphij    return (((ATALLA_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
336296465Sdelphij}
337296465Sdelphij
338160814Ssimonstatic const char *ATALLA_F1 = "ASI_GetHardwareConfig";
339160814Ssimonstatic const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
340160814Ssimonstatic const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
341160814Ssimon
342160814Ssimon/* Destructor (complements the "ENGINE_atalla()" constructor) */
343160814Ssimonstatic int atalla_destroy(ENGINE *e)
344296465Sdelphij{
345296465Sdelphij    free_ATALLA_LIBNAME();
346296465Sdelphij    /*
347296465Sdelphij     * Unload the atalla error strings so any error state including our
348296465Sdelphij     * functs or reasons won't lead to a segfault (they simply get displayed
349296465Sdelphij     * without corresponding string data because none will be found).
350296465Sdelphij     */
351296465Sdelphij    ERR_unload_ATALLA_strings();
352296465Sdelphij    return 1;
353296465Sdelphij}
354160814Ssimon
355160814Ssimon/* (de)initialisation functions. */
356160814Ssimonstatic int atalla_init(ENGINE *e)
357296465Sdelphij{
358296465Sdelphij    tfnASI_GetHardwareConfig *p1;
359296465Sdelphij    tfnASI_RSAPrivateKeyOpFn *p2;
360296465Sdelphij    tfnASI_GetPerformanceStatistics *p3;
361296465Sdelphij    /*
362296465Sdelphij     * Not sure of the origin of this magic value, but Ben's code had it and
363296465Sdelphij     * it seemed to have been working for a few people. :-)
364296465Sdelphij     */
365296465Sdelphij    unsigned int config_buf[1024];
366160814Ssimon
367296465Sdelphij    if (atalla_dso != NULL) {
368296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_ALREADY_LOADED);
369296465Sdelphij        goto err;
370296465Sdelphij    }
371296465Sdelphij    /*
372296465Sdelphij     * Attempt to load libatasi.so/atasi.dll/whatever. Needs to be changed
373296465Sdelphij     * unfortunately because the Atalla drivers don't have standard library
374296465Sdelphij     * names that can be platform-translated well.
375296465Sdelphij     */
376296465Sdelphij    /*
377296465Sdelphij     * TODO: Work out how to actually map to the names the Atalla drivers
378296465Sdelphij     * really use - for now a symbollic link needs to be created on the host
379296465Sdelphij     * system from libatasi.so to atasi.so on unix variants.
380296465Sdelphij     */
381296465Sdelphij    atalla_dso = DSO_load(NULL, get_ATALLA_LIBNAME(), NULL, 0);
382296465Sdelphij    if (atalla_dso == NULL) {
383296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_NOT_LOADED);
384296465Sdelphij        goto err;
385296465Sdelphij    }
386296465Sdelphij    if (!
387296465Sdelphij        (p1 =
388296465Sdelphij         (tfnASI_GetHardwareConfig *) DSO_bind_func(atalla_dso, ATALLA_F1))
389296465Sdelphij|| !(p2 = (tfnASI_RSAPrivateKeyOpFn *) DSO_bind_func(atalla_dso, ATALLA_F2))
390296465Sdelphij|| !(p3 =
391296465Sdelphij     (tfnASI_GetPerformanceStatistics *) DSO_bind_func(atalla_dso,
392296465Sdelphij                                                       ATALLA_F3))) {
393296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_NOT_LOADED);
394296465Sdelphij        goto err;
395296465Sdelphij    }
396296465Sdelphij    /* Copy the pointers */
397296465Sdelphij    p_Atalla_GetHardwareConfig = p1;
398296465Sdelphij    p_Atalla_RSAPrivateKeyOpFn = p2;
399296465Sdelphij    p_Atalla_GetPerformanceStatistics = p3;
400296465Sdelphij    /*
401296465Sdelphij     * Perform a basic test to see if there's actually any unit running.
402296465Sdelphij     */
403296465Sdelphij    if (p1(0L, config_buf) != 0) {
404296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_UNIT_FAILURE);
405296465Sdelphij        goto err;
406296465Sdelphij    }
407296465Sdelphij    /* Everything's fine. */
408296465Sdelphij    return 1;
409296465Sdelphij err:
410296465Sdelphij    if (atalla_dso)
411296465Sdelphij        DSO_free(atalla_dso);
412296465Sdelphij    atalla_dso = NULL;
413296465Sdelphij    p_Atalla_GetHardwareConfig = NULL;
414296465Sdelphij    p_Atalla_RSAPrivateKeyOpFn = NULL;
415296465Sdelphij    p_Atalla_GetPerformanceStatistics = NULL;
416296465Sdelphij    return 0;
417296465Sdelphij}
418160814Ssimon
419160814Ssimonstatic int atalla_finish(ENGINE *e)
420296465Sdelphij{
421296465Sdelphij    free_ATALLA_LIBNAME();
422296465Sdelphij    if (atalla_dso == NULL) {
423296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_FINISH, ATALLA_R_NOT_LOADED);
424296465Sdelphij        return 0;
425296465Sdelphij    }
426296465Sdelphij    if (!DSO_free(atalla_dso)) {
427296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_FINISH, ATALLA_R_UNIT_FAILURE);
428296465Sdelphij        return 0;
429296465Sdelphij    }
430296465Sdelphij    atalla_dso = NULL;
431296465Sdelphij    p_Atalla_GetHardwareConfig = NULL;
432296465Sdelphij    p_Atalla_RSAPrivateKeyOpFn = NULL;
433296465Sdelphij    p_Atalla_GetPerformanceStatistics = NULL;
434296465Sdelphij    return 1;
435296465Sdelphij}
436160814Ssimon
437296465Sdelphijstatic int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
438296465Sdelphij{
439296465Sdelphij    int initialised = ((atalla_dso == NULL) ? 0 : 1);
440296465Sdelphij    switch (cmd) {
441296465Sdelphij    case ATALLA_CMD_SO_PATH:
442296465Sdelphij        if (p == NULL) {
443296465Sdelphij            ATALLAerr(ATALLA_F_ATALLA_CTRL, ERR_R_PASSED_NULL_PARAMETER);
444296465Sdelphij            return 0;
445296465Sdelphij        }
446296465Sdelphij        if (initialised) {
447296465Sdelphij            ATALLAerr(ATALLA_F_ATALLA_CTRL, ATALLA_R_ALREADY_LOADED);
448296465Sdelphij            return 0;
449296465Sdelphij        }
450296465Sdelphij        return set_ATALLA_LIBNAME((const char *)p);
451296465Sdelphij    default:
452296465Sdelphij        break;
453296465Sdelphij    }
454296465Sdelphij    ATALLAerr(ATALLA_F_ATALLA_CTRL, ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
455296465Sdelphij    return 0;
456296465Sdelphij}
457160814Ssimon
458160814Ssimonstatic int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
459296465Sdelphij                          const BIGNUM *m, BN_CTX *ctx)
460296465Sdelphij{
461296465Sdelphij    /*
462296465Sdelphij     * I need somewhere to store temporary serialised values for use with the
463296465Sdelphij     * Atalla API calls. A neat cheat - I'll use BIGNUMs from the BN_CTX but
464296465Sdelphij     * access their arrays directly as byte arrays <grin>. This way I don't
465296465Sdelphij     * have to clean anything up.
466296465Sdelphij     */
467296465Sdelphij    BIGNUM *modulus;
468296465Sdelphij    BIGNUM *exponent;
469296465Sdelphij    BIGNUM *argument;
470296465Sdelphij    BIGNUM *result;
471296465Sdelphij    RSAPrivateKey keydata;
472296465Sdelphij    int to_return, numbytes;
473160814Ssimon
474296465Sdelphij    modulus = exponent = argument = result = NULL;
475296465Sdelphij    to_return = 0;              /* expect failure */
476160814Ssimon
477296465Sdelphij    if (!atalla_dso) {
478296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_NOT_LOADED);
479296465Sdelphij        goto err;
480296465Sdelphij    }
481296465Sdelphij    /* Prepare the params */
482296465Sdelphij    BN_CTX_start(ctx);
483296465Sdelphij    modulus = BN_CTX_get(ctx);
484296465Sdelphij    exponent = BN_CTX_get(ctx);
485296465Sdelphij    argument = BN_CTX_get(ctx);
486296465Sdelphij    result = BN_CTX_get(ctx);
487296465Sdelphij    if (!result) {
488296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_BN_CTX_FULL);
489296465Sdelphij        goto err;
490296465Sdelphij    }
491296465Sdelphij    if (!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
492296465Sdelphij        !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top)) {
493296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_BN_EXPAND_FAIL);
494296465Sdelphij        goto err;
495296465Sdelphij    }
496296465Sdelphij    /* Prepare the key-data */
497296465Sdelphij    memset(&keydata, 0, sizeof keydata);
498296465Sdelphij    numbytes = BN_num_bytes(m);
499296465Sdelphij    memset(exponent->d, 0, numbytes);
500296465Sdelphij    memset(modulus->d, 0, numbytes);
501296465Sdelphij    BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
502296465Sdelphij    BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
503296465Sdelphij    keydata.privateExponent.data = (unsigned char *)exponent->d;
504296465Sdelphij    keydata.privateExponent.len = numbytes;
505296465Sdelphij    keydata.modulus.data = (unsigned char *)modulus->d;
506296465Sdelphij    keydata.modulus.len = numbytes;
507296465Sdelphij    /* Prepare the argument */
508296465Sdelphij    memset(argument->d, 0, numbytes);
509296465Sdelphij    memset(result->d, 0, numbytes);
510296465Sdelphij    BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
511296465Sdelphij    /* Perform the operation */
512296465Sdelphij    if (p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
513296465Sdelphij                                   (unsigned char *)argument->d,
514296465Sdelphij                                   keydata.modulus.len) != 0) {
515296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_REQUEST_FAILED);
516296465Sdelphij        goto err;
517296465Sdelphij    }
518296465Sdelphij    /* Convert the response */
519296465Sdelphij    BN_bin2bn((unsigned char *)result->d, numbytes, r);
520296465Sdelphij    to_return = 1;
521296465Sdelphij err:
522296465Sdelphij    BN_CTX_end(ctx);
523296465Sdelphij    return to_return;
524296465Sdelphij}
525160814Ssimon
526296465Sdelphij#  ifndef OPENSSL_NO_RSA
527296465Sdelphijstatic int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
528296465Sdelphij                              BN_CTX *ctx)
529296465Sdelphij{
530296465Sdelphij    int to_return = 0;
531160814Ssimon
532296465Sdelphij    if (!atalla_dso) {
533296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP, ATALLA_R_NOT_LOADED);
534296465Sdelphij        goto err;
535296465Sdelphij    }
536296465Sdelphij    if (!rsa->d || !rsa->n) {
537296465Sdelphij        ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,
538296465Sdelphij                  ATALLA_R_MISSING_KEY_COMPONENTS);
539296465Sdelphij        goto err;
540296465Sdelphij    }
541296465Sdelphij    to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
542296465Sdelphij err:
543296465Sdelphij    return to_return;
544296465Sdelphij}
545296465Sdelphij#  endif
546160814Ssimon
547296465Sdelphij#  ifndef OPENSSL_NO_DSA
548296465Sdelphij/*
549296465Sdelphij * This code was liberated and adapted from the commented-out code in
550296465Sdelphij * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration (it
551296465Sdelphij * doesn't have a CRT form for RSA), this function means that an Atalla
552296465Sdelphij * system running with a DSA server certificate can handshake around 5 or 6
553296465Sdelphij * times faster/more than an equivalent system running with RSA. Just check
554296465Sdelphij * out the "signs" statistics from the RSA and DSA parts of "openssl speed
555296465Sdelphij * -engine atalla dsa1024 rsa1024".
556296465Sdelphij */
557160814Ssimonstatic int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
558296465Sdelphij                              BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
559296465Sdelphij                              BN_CTX *ctx, BN_MONT_CTX *in_mont)
560296465Sdelphij{
561296465Sdelphij    BIGNUM t;
562296465Sdelphij    int to_return = 0;
563160814Ssimon
564296465Sdelphij    BN_init(&t);
565296465Sdelphij    /* let rr = a1 ^ p1 mod m */
566296465Sdelphij    if (!atalla_mod_exp(rr, a1, p1, m, ctx))
567296465Sdelphij        goto end;
568296465Sdelphij    /* let t = a2 ^ p2 mod m */
569296465Sdelphij    if (!atalla_mod_exp(&t, a2, p2, m, ctx))
570296465Sdelphij        goto end;
571296465Sdelphij    /* let rr = rr * t mod m */
572296465Sdelphij    if (!BN_mod_mul(rr, rr, &t, m, ctx))
573296465Sdelphij        goto end;
574296465Sdelphij    to_return = 1;
575296465Sdelphij end:
576296465Sdelphij    BN_free(&t);
577296465Sdelphij    return to_return;
578296465Sdelphij}
579296465Sdelphij
580160814Ssimonstatic int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
581296465Sdelphij                              const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
582296465Sdelphij                              BN_MONT_CTX *m_ctx)
583296465Sdelphij{
584296465Sdelphij    return atalla_mod_exp(r, a, p, m, ctx);
585296465Sdelphij}
586296465Sdelphij#  endif
587160814Ssimon
588296465Sdelphij#  ifndef OPENSSL_NO_RSA
589160814Ssimon/* This function is aliased to mod_exp (with the mont stuff dropped). */
590160814Ssimonstatic int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
591296465Sdelphij                               const BIGNUM *m, BN_CTX *ctx,
592296465Sdelphij                               BN_MONT_CTX *m_ctx)
593296465Sdelphij{
594296465Sdelphij    return atalla_mod_exp(r, a, p, m, ctx);
595296465Sdelphij}
596296465Sdelphij#  endif
597160814Ssimon
598296465Sdelphij#  ifndef OPENSSL_NO_DH
599160814Ssimon/* This function is aliased to mod_exp (with the dh and mont dropped). */
600160814Ssimonstatic int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
601296465Sdelphij                             const BIGNUM *a, const BIGNUM *p,
602296465Sdelphij                             const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
603296465Sdelphij{
604296465Sdelphij    return atalla_mod_exp(r, a, p, m, ctx);
605296465Sdelphij}
606296465Sdelphij#  endif
607160814Ssimon
608296465Sdelphij/*
609296465Sdelphij * This stuff is needed if this ENGINE is being compiled into a
610296465Sdelphij * self-contained shared-library.
611296465Sdelphij */
612296465Sdelphij#  ifndef OPENSSL_NO_DYNAMIC_ENGINE
613160814Ssimonstatic int bind_fn(ENGINE *e, const char *id)
614296465Sdelphij{
615296465Sdelphij    if (id && (strcmp(id, engine_atalla_id) != 0))
616296465Sdelphij        return 0;
617296465Sdelphij    if (!bind_helper(e))
618296465Sdelphij        return 0;
619296465Sdelphij    return 1;
620296465Sdelphij}
621296465Sdelphij
622160814SsimonIMPLEMENT_DYNAMIC_CHECK_FN()
623296465Sdelphij    IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
624296465Sdelphij#  endif                        /* OPENSSL_NO_DYNAMIC_ENGINE */
625296465Sdelphij# endif                         /* !OPENSSL_NO_HW_ATALLA */
626296465Sdelphij#endif                          /* !OPENSSL_NO_HW */
627