1160814Ssimon/* crypto/engine/e_gmp.c */
2296341Sdelphij/*
3296341Sdelphij * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4296341Sdelphij * 2003.
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
14296341Sdelphij *    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
60296341Sdelphij/*
61296341Sdelphij * This engine is not (currently) compiled in by default. Do enable it,
62296341Sdelphij * reconfigure OpenSSL with "enable-gmp -lgmp". The GMP libraries and headers
63296341Sdelphij * must reside in one of the paths searched by the compiler/linker, otherwise
64296341Sdelphij * paths must be specified - eg. try configuring with "enable-gmp
65296341Sdelphij * -I<includepath> -L<libpath> -lgmp". YMMV.
66296341Sdelphij */
67160814Ssimon
68296341Sdelphij/*-
69296341Sdelphij * As for what this does - it's a largely unoptimised implementation of an
70160814Ssimon * ENGINE that uses the GMP library to perform RSA private key operations. To
71160814Ssimon * obtain more information about what "unoptimised" means, see my original mail
72160814Ssimon * on the subject (though ignore the build instructions which have since
73160814Ssimon * changed);
74160814Ssimon *
75160814Ssimon *    http://www.mail-archive.com/openssl-dev@openssl.org/msg12227.html
76160814Ssimon *
77160814Ssimon * On my athlon system at least, it appears the builtin OpenSSL code is now
78160814Ssimon * slightly faster, which is to say that the RSA-related MPI performance
79160814Ssimon * between OpenSSL's BIGNUM and GMP's mpz implementations is probably pretty
80160814Ssimon * balanced for this chip, and so the performance degradation in this ENGINE by
81160814Ssimon * having to convert to/from GMP formats (and not being able to cache
82160814Ssimon * montgomery forms) is probably the difference. However, if some unconfirmed
83160814Ssimon * reports from users is anything to go by, the situation on some other
84160814Ssimon * chipsets might be a good deal more favourable to the GMP version (eg. PPC).
85160814Ssimon * Feedback welcome. */
86160814Ssimon
87160814Ssimon#include <stdio.h>
88160814Ssimon#include <string.h>
89160814Ssimon#include <openssl/crypto.h>
90160814Ssimon#include <openssl/buffer.h>
91160814Ssimon#include <openssl/engine.h>
92238405Sjkim#ifndef OPENSSL_NO_RSA
93296341Sdelphij# include <openssl/rsa.h>
94238405Sjkim#endif
95194206Ssimon#include <openssl/bn.h>
96160814Ssimon
97160814Ssimon#ifndef OPENSSL_NO_HW
98296341Sdelphij# ifndef OPENSSL_NO_GMP
99160814Ssimon
100296341Sdelphij#  include <gmp.h>
101160814Ssimon
102296341Sdelphij#  define E_GMP_LIB_NAME "gmp engine"
103296341Sdelphij#  include "e_gmp_err.c"
104160814Ssimon
105160814Ssimonstatic int e_gmp_destroy(ENGINE *e);
106160814Ssimonstatic int e_gmp_init(ENGINE *e);
107160814Ssimonstatic int e_gmp_finish(ENGINE *e);
108296341Sdelphijstatic int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
109160814Ssimon
110296341Sdelphij#  ifndef OPENSSL_NO_RSA
111160814Ssimon/* RSA stuff */
112296341Sdelphijstatic int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa,
113296341Sdelphij                             BN_CTX *ctx);
114160814Ssimonstatic int e_gmp_rsa_finish(RSA *r);
115296341Sdelphij#  endif
116160814Ssimon
117160814Ssimon/* The definitions for control commands specific to this engine */
118296341Sdelphij/* #define E_GMP_CMD_SO_PATH            ENGINE_CMD_BASE */
119160814Ssimonstatic const ENGINE_CMD_DEFN e_gmp_cmd_defns[] = {
120296341Sdelphij#  if 0
121296341Sdelphij    {E_GMP_CMD_SO_PATH,
122296341Sdelphij     "SO_PATH",
123296341Sdelphij     "Specifies the path to the 'e_gmp' shared library",
124296341Sdelphij     ENGINE_CMD_FLAG_STRING},
125296341Sdelphij#  endif
126296341Sdelphij    {0, NULL, NULL, 0}
127296341Sdelphij};
128160814Ssimon
129296341Sdelphij#  ifndef OPENSSL_NO_RSA
130160814Ssimon/* Our internal RSA_METHOD that we provide pointers to */
131296341Sdelphijstatic RSA_METHOD e_gmp_rsa = {
132296341Sdelphij    "GMP RSA method",
133296341Sdelphij    NULL,
134296341Sdelphij    NULL,
135296341Sdelphij    NULL,
136296341Sdelphij    NULL,
137296341Sdelphij    e_gmp_rsa_mod_exp,
138296341Sdelphij    NULL,
139296341Sdelphij    NULL,
140296341Sdelphij    e_gmp_rsa_finish,
141296341Sdelphij    /*
142296341Sdelphij     * These flags initialise montgomery crud that GMP ignores, however it
143296341Sdelphij     * makes sure the public key ops (which are done in openssl) don't seem
144296341Sdelphij     * *slower* than usual :-)
145296341Sdelphij     */
146296341Sdelphij    RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
147296341Sdelphij    NULL,
148296341Sdelphij    NULL,
149296341Sdelphij    NULL
150296341Sdelphij};
151296341Sdelphij#  endif
152160814Ssimon
153160814Ssimon/* Constants used when creating the ENGINE */
154160814Ssimonstatic const char *engine_e_gmp_id = "gmp";
155160814Ssimonstatic const char *engine_e_gmp_name = "GMP engine support";
156160814Ssimon
157296341Sdelphij/*
158296341Sdelphij * This internal function is used by ENGINE_gmp() and possibly by the
159296341Sdelphij * "dynamic" ENGINE support too
160296341Sdelphij */
161160814Ssimonstatic int bind_helper(ENGINE *e)
162296341Sdelphij{
163296341Sdelphij#  ifndef OPENSSL_NO_RSA
164296341Sdelphij    const RSA_METHOD *meth1;
165296341Sdelphij#  endif
166296341Sdelphij    if (!ENGINE_set_id(e, engine_e_gmp_id) ||
167296341Sdelphij        !ENGINE_set_name(e, engine_e_gmp_name) ||
168296341Sdelphij#  ifndef OPENSSL_NO_RSA
169296341Sdelphij        !ENGINE_set_RSA(e, &e_gmp_rsa) ||
170296341Sdelphij#  endif
171296341Sdelphij        !ENGINE_set_destroy_function(e, e_gmp_destroy) ||
172296341Sdelphij        !ENGINE_set_init_function(e, e_gmp_init) ||
173296341Sdelphij        !ENGINE_set_finish_function(e, e_gmp_finish) ||
174296341Sdelphij        !ENGINE_set_ctrl_function(e, e_gmp_ctrl) ||
175296341Sdelphij        !ENGINE_set_cmd_defns(e, e_gmp_cmd_defns))
176296341Sdelphij        return 0;
177160814Ssimon
178296341Sdelphij#  ifndef OPENSSL_NO_RSA
179296341Sdelphij    meth1 = RSA_PKCS1_SSLeay();
180296341Sdelphij    e_gmp_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
181296341Sdelphij    e_gmp_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
182296341Sdelphij    e_gmp_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
183296341Sdelphij    e_gmp_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
184296341Sdelphij    e_gmp_rsa.bn_mod_exp = meth1->bn_mod_exp;
185296341Sdelphij#  endif
186160814Ssimon
187296341Sdelphij    /* Ensure the e_gmp error handling is set up */
188296341Sdelphij    ERR_load_GMP_strings();
189296341Sdelphij    return 1;
190296341Sdelphij}
191160814Ssimon
192160814Ssimonstatic ENGINE *engine_gmp(void)
193296341Sdelphij{
194296341Sdelphij    ENGINE *ret = ENGINE_new();
195296341Sdelphij    if (!ret)
196296341Sdelphij        return NULL;
197296341Sdelphij    if (!bind_helper(ret)) {
198296341Sdelphij        ENGINE_free(ret);
199296341Sdelphij        return NULL;
200296341Sdelphij    }
201296341Sdelphij    return ret;
202296341Sdelphij}
203160814Ssimon
204160814Ssimonvoid ENGINE_load_gmp(void)
205296341Sdelphij{
206296341Sdelphij    /* Copied from eng_[openssl|dyn].c */
207296341Sdelphij    ENGINE *toadd = engine_gmp();
208296341Sdelphij    if (!toadd)
209296341Sdelphij        return;
210296341Sdelphij    ENGINE_add(toadd);
211296341Sdelphij    ENGINE_free(toadd);
212296341Sdelphij    ERR_clear_error();
213296341Sdelphij}
214160814Ssimon
215296341Sdelphij#  ifndef OPENSSL_NO_RSA
216160814Ssimon/* Used to attach our own key-data to an RSA structure */
217160814Ssimonstatic int hndidx_rsa = -1;
218296341Sdelphij#  endif
219160814Ssimon
220160814Ssimonstatic int e_gmp_destroy(ENGINE *e)
221296341Sdelphij{
222296341Sdelphij    ERR_unload_GMP_strings();
223296341Sdelphij    return 1;
224296341Sdelphij}
225160814Ssimon
226160814Ssimon/* (de)initialisation functions. */
227160814Ssimonstatic int e_gmp_init(ENGINE *e)
228296341Sdelphij{
229296341Sdelphij#  ifndef OPENSSL_NO_RSA
230296341Sdelphij    if (hndidx_rsa == -1)
231296341Sdelphij        hndidx_rsa = RSA_get_ex_new_index(0,
232296341Sdelphij                                          "GMP-based RSA key handle",
233296341Sdelphij                                          NULL, NULL, NULL);
234296341Sdelphij#  endif
235296341Sdelphij    if (hndidx_rsa == -1)
236296341Sdelphij        return 0;
237296341Sdelphij    return 1;
238296341Sdelphij}
239160814Ssimon
240160814Ssimonstatic int e_gmp_finish(ENGINE *e)
241296341Sdelphij{
242296341Sdelphij    return 1;
243296341Sdelphij}
244160814Ssimon
245296341Sdelphijstatic int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
246296341Sdelphij{
247296341Sdelphij    int to_return = 1;
248160814Ssimon
249296341Sdelphij    switch (cmd) {
250296341Sdelphij#  if 0
251296341Sdelphij    case E_GMP_CMD_SO_PATH:
252296341Sdelphij        /* ... */
253296341Sdelphij#  endif
254296341Sdelphij        /* The command isn't understood by this engine */
255296341Sdelphij    default:
256296341Sdelphij        GMPerr(GMP_F_E_GMP_CTRL, GMP_R_CTRL_COMMAND_NOT_IMPLEMENTED);
257296341Sdelphij        to_return = 0;
258296341Sdelphij        break;
259296341Sdelphij    }
260160814Ssimon
261296341Sdelphij    return to_return;
262296341Sdelphij}
263160814Ssimon
264296341Sdelphij/*
265296341Sdelphij * Most often limb sizes will be the same. If not, we use hex conversion
266296341Sdelphij * which is neat, but extremely inefficient.
267296341Sdelphij */
268160814Ssimonstatic int bn2gmp(const BIGNUM *bn, mpz_t g)
269296341Sdelphij{
270296341Sdelphij    bn_check_top(bn);
271296341Sdelphij    if (((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
272296341Sdelphij        (BN_BITS2 == GMP_NUMB_BITS)) {
273296341Sdelphij        /* The common case */
274296341Sdelphij        if (!_mpz_realloc(g, bn->top))
275296341Sdelphij            return 0;
276296341Sdelphij        memcpy(&g->_mp_d[0], &bn->d[0], bn->top * sizeof(bn->d[0]));
277296341Sdelphij        g->_mp_size = bn->top;
278296341Sdelphij        if (bn->neg)
279296341Sdelphij            g->_mp_size = -g->_mp_size;
280296341Sdelphij        return 1;
281296341Sdelphij    } else {
282296341Sdelphij        int toret;
283296341Sdelphij        char *tmpchar = BN_bn2hex(bn);
284296341Sdelphij        if (!tmpchar)
285296341Sdelphij            return 0;
286296341Sdelphij        toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
287296341Sdelphij        OPENSSL_free(tmpchar);
288296341Sdelphij        return toret;
289296341Sdelphij    }
290296341Sdelphij}
291160814Ssimon
292160814Ssimonstatic int gmp2bn(mpz_t g, BIGNUM *bn)
293296341Sdelphij{
294296341Sdelphij    if (((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
295296341Sdelphij        (BN_BITS2 == GMP_NUMB_BITS)) {
296296341Sdelphij        /* The common case */
297296341Sdelphij        int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size;
298296341Sdelphij        BN_zero(bn);
299296341Sdelphij        if (bn_expand2(bn, s) == NULL)
300296341Sdelphij            return 0;
301296341Sdelphij        bn->top = s;
302296341Sdelphij        memcpy(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0]));
303296341Sdelphij        bn_correct_top(bn);
304296341Sdelphij        bn->neg = g->_mp_size >= 0 ? 0 : 1;
305296341Sdelphij        return 1;
306296341Sdelphij    } else {
307296341Sdelphij        int toret;
308296341Sdelphij        char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
309296341Sdelphij        if (!tmpchar)
310296341Sdelphij            return 0;
311296341Sdelphij        mpz_get_str(tmpchar, 16, g);
312296341Sdelphij        toret = BN_hex2bn(&bn, tmpchar);
313296341Sdelphij        OPENSSL_free(tmpchar);
314296341Sdelphij        return toret;
315296341Sdelphij    }
316296341Sdelphij}
317160814Ssimon
318296341Sdelphij#  ifndef OPENSSL_NO_RSA
319296341Sdelphijtypedef struct st_e_gmp_rsa_ctx {
320296341Sdelphij    int public_only;
321296341Sdelphij    mpz_t n;
322296341Sdelphij    mpz_t d;
323296341Sdelphij    mpz_t e;
324296341Sdelphij    mpz_t p;
325296341Sdelphij    mpz_t q;
326296341Sdelphij    mpz_t dmp1;
327296341Sdelphij    mpz_t dmq1;
328296341Sdelphij    mpz_t iqmp;
329296341Sdelphij    mpz_t r0, r1, I0, m1;
330296341Sdelphij} E_GMP_RSA_CTX;
331160814Ssimon
332160814Ssimonstatic E_GMP_RSA_CTX *e_gmp_get_rsa(RSA *rsa)
333296341Sdelphij{
334296341Sdelphij    E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
335296341Sdelphij    if (hptr)
336296341Sdelphij        return hptr;
337296341Sdelphij    hptr = OPENSSL_malloc(sizeof(E_GMP_RSA_CTX));
338296341Sdelphij    if (!hptr)
339296341Sdelphij        return NULL;
340296341Sdelphij    /*
341296341Sdelphij     * These inits could probably be replaced by more intelligent mpz_init2()
342296341Sdelphij     * versions, to reduce malloc-thrashing.
343296341Sdelphij     */
344296341Sdelphij    mpz_init(hptr->n);
345296341Sdelphij    mpz_init(hptr->d);
346296341Sdelphij    mpz_init(hptr->e);
347296341Sdelphij    mpz_init(hptr->p);
348296341Sdelphij    mpz_init(hptr->q);
349296341Sdelphij    mpz_init(hptr->dmp1);
350296341Sdelphij    mpz_init(hptr->dmq1);
351296341Sdelphij    mpz_init(hptr->iqmp);
352296341Sdelphij    mpz_init(hptr->r0);
353296341Sdelphij    mpz_init(hptr->r1);
354296341Sdelphij    mpz_init(hptr->I0);
355296341Sdelphij    mpz_init(hptr->m1);
356296341Sdelphij    if (!bn2gmp(rsa->n, hptr->n) || !bn2gmp(rsa->e, hptr->e))
357296341Sdelphij        goto err;
358296341Sdelphij    if (!rsa->p || !rsa->q || !rsa->d || !rsa->dmp1 || !rsa->dmq1
359296341Sdelphij        || !rsa->iqmp) {
360296341Sdelphij        hptr->public_only = 1;
361296341Sdelphij        return hptr;
362296341Sdelphij    }
363296341Sdelphij    if (!bn2gmp(rsa->d, hptr->d) || !bn2gmp(rsa->p, hptr->p) ||
364296341Sdelphij        !bn2gmp(rsa->q, hptr->q) || !bn2gmp(rsa->dmp1, hptr->dmp1) ||
365296341Sdelphij        !bn2gmp(rsa->dmq1, hptr->dmq1) || !bn2gmp(rsa->iqmp, hptr->iqmp))
366296341Sdelphij        goto err;
367296341Sdelphij    hptr->public_only = 0;
368296341Sdelphij    RSA_set_ex_data(rsa, hndidx_rsa, hptr);
369296341Sdelphij    return hptr;
370296341Sdelphij err:
371296341Sdelphij    mpz_clear(hptr->n);
372296341Sdelphij    mpz_clear(hptr->d);
373296341Sdelphij    mpz_clear(hptr->e);
374296341Sdelphij    mpz_clear(hptr->p);
375296341Sdelphij    mpz_clear(hptr->q);
376296341Sdelphij    mpz_clear(hptr->dmp1);
377296341Sdelphij    mpz_clear(hptr->dmq1);
378296341Sdelphij    mpz_clear(hptr->iqmp);
379296341Sdelphij    mpz_clear(hptr->r0);
380296341Sdelphij    mpz_clear(hptr->r1);
381296341Sdelphij    mpz_clear(hptr->I0);
382296341Sdelphij    mpz_clear(hptr->m1);
383296341Sdelphij    OPENSSL_free(hptr);
384296341Sdelphij    return NULL;
385296341Sdelphij}
386160814Ssimon
387160814Ssimonstatic int e_gmp_rsa_finish(RSA *rsa)
388296341Sdelphij{
389296341Sdelphij    E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
390296341Sdelphij    if (!hptr)
391296341Sdelphij        return 0;
392296341Sdelphij    mpz_clear(hptr->n);
393296341Sdelphij    mpz_clear(hptr->d);
394296341Sdelphij    mpz_clear(hptr->e);
395296341Sdelphij    mpz_clear(hptr->p);
396296341Sdelphij    mpz_clear(hptr->q);
397296341Sdelphij    mpz_clear(hptr->dmp1);
398296341Sdelphij    mpz_clear(hptr->dmq1);
399296341Sdelphij    mpz_clear(hptr->iqmp);
400296341Sdelphij    mpz_clear(hptr->r0);
401296341Sdelphij    mpz_clear(hptr->r1);
402296341Sdelphij    mpz_clear(hptr->I0);
403296341Sdelphij    mpz_clear(hptr->m1);
404296341Sdelphij    OPENSSL_free(hptr);
405296341Sdelphij    RSA_set_ex_data(rsa, hndidx_rsa, NULL);
406296341Sdelphij    return 1;
407296341Sdelphij}
408160814Ssimon
409296341Sdelphijstatic int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa,
410296341Sdelphij                             BN_CTX *ctx)
411296341Sdelphij{
412296341Sdelphij    E_GMP_RSA_CTX *hptr;
413296341Sdelphij    int to_return = 0;
414160814Ssimon
415296341Sdelphij    hptr = e_gmp_get_rsa(rsa);
416296341Sdelphij    if (!hptr) {
417296341Sdelphij        GMPerr(GMP_F_E_GMP_RSA_MOD_EXP, GMP_R_KEY_CONTEXT_ERROR);
418296341Sdelphij        return 0;
419296341Sdelphij    }
420296341Sdelphij    if (hptr->public_only) {
421296341Sdelphij        GMPerr(GMP_F_E_GMP_RSA_MOD_EXP, GMP_R_MISSING_KEY_COMPONENTS);
422296341Sdelphij        return 0;
423296341Sdelphij    }
424160814Ssimon
425296341Sdelphij    /* ugh!!! */
426296341Sdelphij    if (!bn2gmp(I, hptr->I0))
427296341Sdelphij        return 0;
428160814Ssimon
429296341Sdelphij    /*
430296341Sdelphij     * This is basically the CRT logic in crypto/rsa/rsa_eay.c reworded into
431296341Sdelphij     * GMP-speak. It may be that GMP's API facilitates cleaner formulations
432296341Sdelphij     * of this stuff, eg. better handling of negatives, or functions that
433296341Sdelphij     * combine operations.
434296341Sdelphij     */
435160814Ssimon
436296341Sdelphij    mpz_mod(hptr->r1, hptr->I0, hptr->q);
437296341Sdelphij    mpz_powm(hptr->m1, hptr->r1, hptr->dmq1, hptr->q);
438160814Ssimon
439296341Sdelphij    mpz_mod(hptr->r1, hptr->I0, hptr->p);
440296341Sdelphij    mpz_powm(hptr->r0, hptr->r1, hptr->dmp1, hptr->p);
441160814Ssimon
442296341Sdelphij    mpz_sub(hptr->r0, hptr->r0, hptr->m1);
443160814Ssimon
444296341Sdelphij    if (mpz_sgn(hptr->r0) < 0)
445296341Sdelphij        mpz_add(hptr->r0, hptr->r0, hptr->p);
446296341Sdelphij    mpz_mul(hptr->r1, hptr->r0, hptr->iqmp);
447296341Sdelphij    mpz_mod(hptr->r0, hptr->r1, hptr->p);
448160814Ssimon
449296341Sdelphij    if (mpz_sgn(hptr->r0) < 0)
450296341Sdelphij        mpz_add(hptr->r0, hptr->r0, hptr->p);
451296341Sdelphij    mpz_mul(hptr->r1, hptr->r0, hptr->q);
452296341Sdelphij    mpz_add(hptr->r0, hptr->r1, hptr->m1);
453160814Ssimon
454296341Sdelphij    /* ugh!!! */
455296341Sdelphij    if (gmp2bn(hptr->r0, r))
456296341Sdelphij        to_return = 1;
457160814Ssimon
458296341Sdelphij    return 1;
459296341Sdelphij}
460296341Sdelphij#  endif
461160814Ssimon
462296341Sdelphij# endif                         /* !OPENSSL_NO_GMP */
463194206Ssimon
464296341Sdelphij/*
465296341Sdelphij * This stuff is needed if this ENGINE is being compiled into a
466296341Sdelphij * self-contained shared-library.
467296341Sdelphij */
468296341Sdelphij# ifndef OPENSSL_NO_DYNAMIC_ENGINE
469194206SsimonIMPLEMENT_DYNAMIC_CHECK_FN()
470296341Sdelphij#  ifndef OPENSSL_NO_GMP
471160814Ssimonstatic int bind_fn(ENGINE *e, const char *id)
472296341Sdelphij{
473296341Sdelphij    if (id && (strcmp(id, engine_e_gmp_id) != 0))
474296341Sdelphij        return 0;
475296341Sdelphij    if (!bind_helper(e))
476296341Sdelphij        return 0;
477296341Sdelphij    return 1;
478296341Sdelphij}
479296341Sdelphij
480160814SsimonIMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
481296341Sdelphij#  else
482194206SsimonOPENSSL_EXPORT
483296341Sdelphij    int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
484238405SjkimOPENSSL_EXPORT
485296341Sdelphij    int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
486296341Sdelphij{
487296341Sdelphij    return 0;
488296341Sdelphij}
489296341Sdelphij#  endif
490296341Sdelphij# endif                         /* !OPENSSL_NO_DYNAMIC_ENGINE */
491160814Ssimon
492296341Sdelphij#endif                          /* !OPENSSL_NO_HW */
493