1160814Ssimon/* crypto/engine/e_gmp.c */
2280304Sjkim/*
3280304Sjkim * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4280304Sjkim * 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
14280304Sjkim *    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
60280304Sjkim/*
61280304Sjkim * This engine is not (currently) compiled in by default. Do enable it,
62280304Sjkim * reconfigure OpenSSL with "enable-gmp -lgmp". The GMP libraries and headers
63280304Sjkim * must reside in one of the paths searched by the compiler/linker, otherwise
64280304Sjkim * paths must be specified - eg. try configuring with "enable-gmp
65280304Sjkim * -I<includepath> -L<libpath> -lgmp". YMMV.
66280304Sjkim */
67160814Ssimon
68280304Sjkim/*-
69280304Sjkim * 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
93280304Sjkim# include <openssl/rsa.h>
94238405Sjkim#endif
95194206Ssimon#include <openssl/bn.h>
96160814Ssimon
97160814Ssimon#ifndef OPENSSL_NO_HW
98280304Sjkim# ifndef OPENSSL_NO_GMP
99160814Ssimon
100280304Sjkim#  include <gmp.h>
101160814Ssimon
102280304Sjkim#  define E_GMP_LIB_NAME "gmp engine"
103280304Sjkim#  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);
108280304Sjkimstatic int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
109160814Ssimon
110280304Sjkim#  ifndef OPENSSL_NO_RSA
111160814Ssimon/* RSA stuff */
112280304Sjkimstatic int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa,
113280304Sjkim                             BN_CTX *ctx);
114160814Ssimonstatic int e_gmp_rsa_finish(RSA *r);
115280304Sjkim#  endif
116160814Ssimon
117160814Ssimon/* The definitions for control commands specific to this engine */
118280304Sjkim/* #define E_GMP_CMD_SO_PATH            ENGINE_CMD_BASE */
119160814Ssimonstatic const ENGINE_CMD_DEFN e_gmp_cmd_defns[] = {
120280304Sjkim#  if 0
121280304Sjkim    {E_GMP_CMD_SO_PATH,
122280304Sjkim     "SO_PATH",
123280304Sjkim     "Specifies the path to the 'e_gmp' shared library",
124280304Sjkim     ENGINE_CMD_FLAG_STRING},
125280304Sjkim#  endif
126280304Sjkim    {0, NULL, NULL, 0}
127280304Sjkim};
128160814Ssimon
129280304Sjkim#  ifndef OPENSSL_NO_RSA
130160814Ssimon/* Our internal RSA_METHOD that we provide pointers to */
131280304Sjkimstatic RSA_METHOD e_gmp_rsa = {
132280304Sjkim    "GMP RSA method",
133280304Sjkim    NULL,
134280304Sjkim    NULL,
135280304Sjkim    NULL,
136280304Sjkim    NULL,
137280304Sjkim    e_gmp_rsa_mod_exp,
138280304Sjkim    NULL,
139280304Sjkim    NULL,
140280304Sjkim    e_gmp_rsa_finish,
141280304Sjkim    /*
142280304Sjkim     * These flags initialise montgomery crud that GMP ignores, however it
143280304Sjkim     * makes sure the public key ops (which are done in openssl) don't seem
144280304Sjkim     * *slower* than usual :-)
145280304Sjkim     */
146280304Sjkim    RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
147280304Sjkim    NULL,
148280304Sjkim    NULL,
149280304Sjkim    NULL
150280304Sjkim};
151280304Sjkim#  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
157280304Sjkim/*
158280304Sjkim * This internal function is used by ENGINE_gmp() and possibly by the
159280304Sjkim * "dynamic" ENGINE support too
160280304Sjkim */
161160814Ssimonstatic int bind_helper(ENGINE *e)
162280304Sjkim{
163280304Sjkim#  ifndef OPENSSL_NO_RSA
164280304Sjkim    const RSA_METHOD *meth1;
165280304Sjkim#  endif
166280304Sjkim    if (!ENGINE_set_id(e, engine_e_gmp_id) ||
167280304Sjkim        !ENGINE_set_name(e, engine_e_gmp_name) ||
168280304Sjkim#  ifndef OPENSSL_NO_RSA
169280304Sjkim        !ENGINE_set_RSA(e, &e_gmp_rsa) ||
170280304Sjkim#  endif
171280304Sjkim        !ENGINE_set_destroy_function(e, e_gmp_destroy) ||
172280304Sjkim        !ENGINE_set_init_function(e, e_gmp_init) ||
173280304Sjkim        !ENGINE_set_finish_function(e, e_gmp_finish) ||
174280304Sjkim        !ENGINE_set_ctrl_function(e, e_gmp_ctrl) ||
175280304Sjkim        !ENGINE_set_cmd_defns(e, e_gmp_cmd_defns))
176280304Sjkim        return 0;
177160814Ssimon
178280304Sjkim#  ifndef OPENSSL_NO_RSA
179280304Sjkim    meth1 = RSA_PKCS1_SSLeay();
180280304Sjkim    e_gmp_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
181280304Sjkim    e_gmp_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
182280304Sjkim    e_gmp_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
183280304Sjkim    e_gmp_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
184280304Sjkim    e_gmp_rsa.bn_mod_exp = meth1->bn_mod_exp;
185280304Sjkim#  endif
186160814Ssimon
187280304Sjkim    /* Ensure the e_gmp error handling is set up */
188280304Sjkim    ERR_load_GMP_strings();
189280304Sjkim    return 1;
190280304Sjkim}
191160814Ssimon
192160814Ssimonstatic ENGINE *engine_gmp(void)
193280304Sjkim{
194280304Sjkim    ENGINE *ret = ENGINE_new();
195280304Sjkim    if (!ret)
196280304Sjkim        return NULL;
197280304Sjkim    if (!bind_helper(ret)) {
198280304Sjkim        ENGINE_free(ret);
199280304Sjkim        return NULL;
200280304Sjkim    }
201280304Sjkim    return ret;
202280304Sjkim}
203160814Ssimon
204160814Ssimonvoid ENGINE_load_gmp(void)
205280304Sjkim{
206280304Sjkim    /* Copied from eng_[openssl|dyn].c */
207280304Sjkim    ENGINE *toadd = engine_gmp();
208280304Sjkim    if (!toadd)
209280304Sjkim        return;
210280304Sjkim    ENGINE_add(toadd);
211280304Sjkim    ENGINE_free(toadd);
212280304Sjkim    ERR_clear_error();
213280304Sjkim}
214160814Ssimon
215280304Sjkim#  ifndef OPENSSL_NO_RSA
216160814Ssimon/* Used to attach our own key-data to an RSA structure */
217160814Ssimonstatic int hndidx_rsa = -1;
218280304Sjkim#  endif
219160814Ssimon
220160814Ssimonstatic int e_gmp_destroy(ENGINE *e)
221280304Sjkim{
222280304Sjkim    ERR_unload_GMP_strings();
223280304Sjkim    return 1;
224280304Sjkim}
225160814Ssimon
226160814Ssimon/* (de)initialisation functions. */
227160814Ssimonstatic int e_gmp_init(ENGINE *e)
228280304Sjkim{
229280304Sjkim#  ifndef OPENSSL_NO_RSA
230280304Sjkim    if (hndidx_rsa == -1)
231280304Sjkim        hndidx_rsa = RSA_get_ex_new_index(0,
232280304Sjkim                                          "GMP-based RSA key handle",
233280304Sjkim                                          NULL, NULL, NULL);
234280304Sjkim#  endif
235280304Sjkim    if (hndidx_rsa == -1)
236280304Sjkim        return 0;
237280304Sjkim    return 1;
238280304Sjkim}
239160814Ssimon
240160814Ssimonstatic int e_gmp_finish(ENGINE *e)
241280304Sjkim{
242280304Sjkim    return 1;
243280304Sjkim}
244160814Ssimon
245280304Sjkimstatic int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
246280304Sjkim{
247280304Sjkim    int to_return = 1;
248160814Ssimon
249280304Sjkim    switch (cmd) {
250280304Sjkim#  if 0
251280304Sjkim    case E_GMP_CMD_SO_PATH:
252280304Sjkim        /* ... */
253280304Sjkim#  endif
254280304Sjkim        /* The command isn't understood by this engine */
255280304Sjkim    default:
256280304Sjkim        GMPerr(GMP_F_E_GMP_CTRL, GMP_R_CTRL_COMMAND_NOT_IMPLEMENTED);
257280304Sjkim        to_return = 0;
258280304Sjkim        break;
259280304Sjkim    }
260160814Ssimon
261280304Sjkim    return to_return;
262280304Sjkim}
263160814Ssimon
264280304Sjkim/*
265280304Sjkim * Most often limb sizes will be the same. If not, we use hex conversion
266280304Sjkim * which is neat, but extremely inefficient.
267280304Sjkim */
268160814Ssimonstatic int bn2gmp(const BIGNUM *bn, mpz_t g)
269280304Sjkim{
270280304Sjkim    bn_check_top(bn);
271280304Sjkim    if (((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
272280304Sjkim        (BN_BITS2 == GMP_NUMB_BITS)) {
273280304Sjkim        /* The common case */
274280304Sjkim        if (!_mpz_realloc(g, bn->top))
275280304Sjkim            return 0;
276280304Sjkim        memcpy(&g->_mp_d[0], &bn->d[0], bn->top * sizeof(bn->d[0]));
277280304Sjkim        g->_mp_size = bn->top;
278280304Sjkim        if (bn->neg)
279280304Sjkim            g->_mp_size = -g->_mp_size;
280280304Sjkim        return 1;
281280304Sjkim    } else {
282280304Sjkim        int toret;
283280304Sjkim        char *tmpchar = BN_bn2hex(bn);
284280304Sjkim        if (!tmpchar)
285280304Sjkim            return 0;
286280304Sjkim        toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
287280304Sjkim        OPENSSL_free(tmpchar);
288280304Sjkim        return toret;
289280304Sjkim    }
290280304Sjkim}
291160814Ssimon
292160814Ssimonstatic int gmp2bn(mpz_t g, BIGNUM *bn)
293280304Sjkim{
294280304Sjkim    if (((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
295280304Sjkim        (BN_BITS2 == GMP_NUMB_BITS)) {
296280304Sjkim        /* The common case */
297280304Sjkim        int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size;
298280304Sjkim        BN_zero(bn);
299280304Sjkim        if (bn_expand2(bn, s) == NULL)
300280304Sjkim            return 0;
301280304Sjkim        bn->top = s;
302280304Sjkim        memcpy(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0]));
303280304Sjkim        bn_correct_top(bn);
304280304Sjkim        bn->neg = g->_mp_size >= 0 ? 0 : 1;
305280304Sjkim        return 1;
306280304Sjkim    } else {
307280304Sjkim        int toret;
308280304Sjkim        char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
309280304Sjkim        if (!tmpchar)
310280304Sjkim            return 0;
311280304Sjkim        mpz_get_str(tmpchar, 16, g);
312280304Sjkim        toret = BN_hex2bn(&bn, tmpchar);
313280304Sjkim        OPENSSL_free(tmpchar);
314280304Sjkim        return toret;
315280304Sjkim    }
316280304Sjkim}
317160814Ssimon
318280304Sjkim#  ifndef OPENSSL_NO_RSA
319280304Sjkimtypedef struct st_e_gmp_rsa_ctx {
320280304Sjkim    int public_only;
321280304Sjkim    mpz_t n;
322280304Sjkim    mpz_t d;
323280304Sjkim    mpz_t e;
324280304Sjkim    mpz_t p;
325280304Sjkim    mpz_t q;
326280304Sjkim    mpz_t dmp1;
327280304Sjkim    mpz_t dmq1;
328280304Sjkim    mpz_t iqmp;
329280304Sjkim    mpz_t r0, r1, I0, m1;
330280304Sjkim} E_GMP_RSA_CTX;
331160814Ssimon
332160814Ssimonstatic E_GMP_RSA_CTX *e_gmp_get_rsa(RSA *rsa)
333280304Sjkim{
334280304Sjkim    E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
335280304Sjkim    if (hptr)
336280304Sjkim        return hptr;
337280304Sjkim    hptr = OPENSSL_malloc(sizeof(E_GMP_RSA_CTX));
338280304Sjkim    if (!hptr)
339280304Sjkim        return NULL;
340280304Sjkim    /*
341280304Sjkim     * These inits could probably be replaced by more intelligent mpz_init2()
342280304Sjkim     * versions, to reduce malloc-thrashing.
343280304Sjkim     */
344280304Sjkim    mpz_init(hptr->n);
345280304Sjkim    mpz_init(hptr->d);
346280304Sjkim    mpz_init(hptr->e);
347280304Sjkim    mpz_init(hptr->p);
348280304Sjkim    mpz_init(hptr->q);
349280304Sjkim    mpz_init(hptr->dmp1);
350280304Sjkim    mpz_init(hptr->dmq1);
351280304Sjkim    mpz_init(hptr->iqmp);
352280304Sjkim    mpz_init(hptr->r0);
353280304Sjkim    mpz_init(hptr->r1);
354280304Sjkim    mpz_init(hptr->I0);
355280304Sjkim    mpz_init(hptr->m1);
356280304Sjkim    if (!bn2gmp(rsa->n, hptr->n) || !bn2gmp(rsa->e, hptr->e))
357280304Sjkim        goto err;
358280304Sjkim    if (!rsa->p || !rsa->q || !rsa->d || !rsa->dmp1 || !rsa->dmq1
359280304Sjkim        || !rsa->iqmp) {
360280304Sjkim        hptr->public_only = 1;
361280304Sjkim        return hptr;
362280304Sjkim    }
363280304Sjkim    if (!bn2gmp(rsa->d, hptr->d) || !bn2gmp(rsa->p, hptr->p) ||
364280304Sjkim        !bn2gmp(rsa->q, hptr->q) || !bn2gmp(rsa->dmp1, hptr->dmp1) ||
365280304Sjkim        !bn2gmp(rsa->dmq1, hptr->dmq1) || !bn2gmp(rsa->iqmp, hptr->iqmp))
366280304Sjkim        goto err;
367280304Sjkim    hptr->public_only = 0;
368280304Sjkim    RSA_set_ex_data(rsa, hndidx_rsa, hptr);
369280304Sjkim    return hptr;
370280304Sjkim err:
371280304Sjkim    mpz_clear(hptr->n);
372280304Sjkim    mpz_clear(hptr->d);
373280304Sjkim    mpz_clear(hptr->e);
374280304Sjkim    mpz_clear(hptr->p);
375280304Sjkim    mpz_clear(hptr->q);
376280304Sjkim    mpz_clear(hptr->dmp1);
377280304Sjkim    mpz_clear(hptr->dmq1);
378280304Sjkim    mpz_clear(hptr->iqmp);
379280304Sjkim    mpz_clear(hptr->r0);
380280304Sjkim    mpz_clear(hptr->r1);
381280304Sjkim    mpz_clear(hptr->I0);
382280304Sjkim    mpz_clear(hptr->m1);
383280304Sjkim    OPENSSL_free(hptr);
384280304Sjkim    return NULL;
385280304Sjkim}
386160814Ssimon
387160814Ssimonstatic int e_gmp_rsa_finish(RSA *rsa)
388280304Sjkim{
389280304Sjkim    E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
390280304Sjkim    if (!hptr)
391280304Sjkim        return 0;
392280304Sjkim    mpz_clear(hptr->n);
393280304Sjkim    mpz_clear(hptr->d);
394280304Sjkim    mpz_clear(hptr->e);
395280304Sjkim    mpz_clear(hptr->p);
396280304Sjkim    mpz_clear(hptr->q);
397280304Sjkim    mpz_clear(hptr->dmp1);
398280304Sjkim    mpz_clear(hptr->dmq1);
399280304Sjkim    mpz_clear(hptr->iqmp);
400280304Sjkim    mpz_clear(hptr->r0);
401280304Sjkim    mpz_clear(hptr->r1);
402280304Sjkim    mpz_clear(hptr->I0);
403280304Sjkim    mpz_clear(hptr->m1);
404280304Sjkim    OPENSSL_free(hptr);
405280304Sjkim    RSA_set_ex_data(rsa, hndidx_rsa, NULL);
406280304Sjkim    return 1;
407280304Sjkim}
408160814Ssimon
409280304Sjkimstatic int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa,
410280304Sjkim                             BN_CTX *ctx)
411280304Sjkim{
412280304Sjkim    E_GMP_RSA_CTX *hptr;
413280304Sjkim    int to_return = 0;
414160814Ssimon
415280304Sjkim    hptr = e_gmp_get_rsa(rsa);
416280304Sjkim    if (!hptr) {
417280304Sjkim        GMPerr(GMP_F_E_GMP_RSA_MOD_EXP, GMP_R_KEY_CONTEXT_ERROR);
418280304Sjkim        return 0;
419280304Sjkim    }
420280304Sjkim    if (hptr->public_only) {
421280304Sjkim        GMPerr(GMP_F_E_GMP_RSA_MOD_EXP, GMP_R_MISSING_KEY_COMPONENTS);
422280304Sjkim        return 0;
423280304Sjkim    }
424160814Ssimon
425280304Sjkim    /* ugh!!! */
426280304Sjkim    if (!bn2gmp(I, hptr->I0))
427280304Sjkim        return 0;
428160814Ssimon
429280304Sjkim    /*
430280304Sjkim     * This is basically the CRT logic in crypto/rsa/rsa_eay.c reworded into
431280304Sjkim     * GMP-speak. It may be that GMP's API facilitates cleaner formulations
432280304Sjkim     * of this stuff, eg. better handling of negatives, or functions that
433280304Sjkim     * combine operations.
434280304Sjkim     */
435160814Ssimon
436280304Sjkim    mpz_mod(hptr->r1, hptr->I0, hptr->q);
437280304Sjkim    mpz_powm(hptr->m1, hptr->r1, hptr->dmq1, hptr->q);
438160814Ssimon
439280304Sjkim    mpz_mod(hptr->r1, hptr->I0, hptr->p);
440280304Sjkim    mpz_powm(hptr->r0, hptr->r1, hptr->dmp1, hptr->p);
441160814Ssimon
442280304Sjkim    mpz_sub(hptr->r0, hptr->r0, hptr->m1);
443160814Ssimon
444280304Sjkim    if (mpz_sgn(hptr->r0) < 0)
445280304Sjkim        mpz_add(hptr->r0, hptr->r0, hptr->p);
446280304Sjkim    mpz_mul(hptr->r1, hptr->r0, hptr->iqmp);
447280304Sjkim    mpz_mod(hptr->r0, hptr->r1, hptr->p);
448160814Ssimon
449280304Sjkim    if (mpz_sgn(hptr->r0) < 0)
450280304Sjkim        mpz_add(hptr->r0, hptr->r0, hptr->p);
451280304Sjkim    mpz_mul(hptr->r1, hptr->r0, hptr->q);
452280304Sjkim    mpz_add(hptr->r0, hptr->r1, hptr->m1);
453160814Ssimon
454280304Sjkim    /* ugh!!! */
455280304Sjkim    if (gmp2bn(hptr->r0, r))
456280304Sjkim        to_return = 1;
457160814Ssimon
458280304Sjkim    return 1;
459280304Sjkim}
460280304Sjkim#  endif
461160814Ssimon
462280304Sjkim# endif                         /* !OPENSSL_NO_GMP */
463194206Ssimon
464280304Sjkim/*
465280304Sjkim * This stuff is needed if this ENGINE is being compiled into a
466280304Sjkim * self-contained shared-library.
467280304Sjkim */
468280304Sjkim# ifndef OPENSSL_NO_DYNAMIC_ENGINE
469194206SsimonIMPLEMENT_DYNAMIC_CHECK_FN()
470280304Sjkim#  ifndef OPENSSL_NO_GMP
471160814Ssimonstatic int bind_fn(ENGINE *e, const char *id)
472280304Sjkim{
473280304Sjkim    if (id && (strcmp(id, engine_e_gmp_id) != 0))
474280304Sjkim        return 0;
475280304Sjkim    if (!bind_helper(e))
476280304Sjkim        return 0;
477280304Sjkim    return 1;
478280304Sjkim}
479280304Sjkim
480160814SsimonIMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
481280304Sjkim#  else
482194206SsimonOPENSSL_EXPORT
483280304Sjkim    int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
484238405SjkimOPENSSL_EXPORT
485280304Sjkim    int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
486280304Sjkim{
487280304Sjkim    return 0;
488280304Sjkim}
489280304Sjkim#  endif
490280304Sjkim# endif                         /* !OPENSSL_NO_DYNAMIC_ENGINE */
491160814Ssimon
492280304Sjkim#endif                          /* !OPENSSL_NO_HW */
493