1ebfedea0SLionel Sambuc /* crypto/engine/e_gmp.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4*0a6a1f1dSLionel Sambuc * 2003.
5ebfedea0SLionel Sambuc */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc * are met:
12ebfedea0SLionel Sambuc *
13ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc * distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc * software must display the following acknowledgment:
23ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc *
26ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc * licensing@OpenSSL.org.
30ebfedea0SLionel Sambuc *
31ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc *
35ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc * acknowledgment:
37ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc *
40ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc * ====================================================================
53ebfedea0SLionel Sambuc *
54ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
56ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc *
58ebfedea0SLionel Sambuc */
59ebfedea0SLionel Sambuc
60*0a6a1f1dSLionel Sambuc /*
61*0a6a1f1dSLionel Sambuc * This engine is not (currently) compiled in by default. Do enable it,
62*0a6a1f1dSLionel Sambuc * reconfigure OpenSSL with "enable-gmp -lgmp". The GMP libraries and headers
63*0a6a1f1dSLionel Sambuc * must reside in one of the paths searched by the compiler/linker, otherwise
64*0a6a1f1dSLionel Sambuc * paths must be specified - eg. try configuring with "enable-gmp
65*0a6a1f1dSLionel Sambuc * -I<includepath> -L<libpath> -lgmp". YMMV.
66*0a6a1f1dSLionel Sambuc */
67ebfedea0SLionel Sambuc
68*0a6a1f1dSLionel Sambuc /*-
69*0a6a1f1dSLionel Sambuc * As for what this does - it's a largely unoptimised implementation of an
70ebfedea0SLionel Sambuc * ENGINE that uses the GMP library to perform RSA private key operations. To
71ebfedea0SLionel Sambuc * obtain more information about what "unoptimised" means, see my original mail
72ebfedea0SLionel Sambuc * on the subject (though ignore the build instructions which have since
73ebfedea0SLionel Sambuc * changed);
74ebfedea0SLionel Sambuc *
75ebfedea0SLionel Sambuc * http://www.mail-archive.com/openssl-dev@openssl.org/msg12227.html
76ebfedea0SLionel Sambuc *
77ebfedea0SLionel Sambuc * On my athlon system at least, it appears the builtin OpenSSL code is now
78ebfedea0SLionel Sambuc * slightly faster, which is to say that the RSA-related MPI performance
79ebfedea0SLionel Sambuc * between OpenSSL's BIGNUM and GMP's mpz implementations is probably pretty
80ebfedea0SLionel Sambuc * balanced for this chip, and so the performance degradation in this ENGINE by
81ebfedea0SLionel Sambuc * having to convert to/from GMP formats (and not being able to cache
82ebfedea0SLionel Sambuc * montgomery forms) is probably the difference. However, if some unconfirmed
83ebfedea0SLionel Sambuc * reports from users is anything to go by, the situation on some other
84ebfedea0SLionel Sambuc * chipsets might be a good deal more favourable to the GMP version (eg. PPC).
85ebfedea0SLionel Sambuc * Feedback welcome. */
86ebfedea0SLionel Sambuc
87ebfedea0SLionel Sambuc #include <stdio.h>
88ebfedea0SLionel Sambuc #include <string.h>
89ebfedea0SLionel Sambuc #include <openssl/crypto.h>
90ebfedea0SLionel Sambuc #include <openssl/buffer.h>
91ebfedea0SLionel Sambuc #include <openssl/engine.h>
92ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
93ebfedea0SLionel Sambuc # include <openssl/rsa.h>
94ebfedea0SLionel Sambuc #endif
95ebfedea0SLionel Sambuc #include <openssl/bn.h>
96ebfedea0SLionel Sambuc
97ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HW
98ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_GMP
99ebfedea0SLionel Sambuc
100ebfedea0SLionel Sambuc # include <gmp.h>
101ebfedea0SLionel Sambuc
102ebfedea0SLionel Sambuc # define E_GMP_LIB_NAME "gmp engine"
103ebfedea0SLionel Sambuc # include "e_gmp_err.c"
104ebfedea0SLionel Sambuc
105ebfedea0SLionel Sambuc static int e_gmp_destroy(ENGINE *e);
106ebfedea0SLionel Sambuc static int e_gmp_init(ENGINE *e);
107ebfedea0SLionel Sambuc static int e_gmp_finish(ENGINE *e);
108ebfedea0SLionel Sambuc static int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
109ebfedea0SLionel Sambuc
110ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
111ebfedea0SLionel Sambuc /* RSA stuff */
112*0a6a1f1dSLionel Sambuc static int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa,
113*0a6a1f1dSLionel Sambuc BN_CTX *ctx);
114ebfedea0SLionel Sambuc static int e_gmp_rsa_finish(RSA *r);
115ebfedea0SLionel Sambuc # endif
116ebfedea0SLionel Sambuc
117ebfedea0SLionel Sambuc /* The definitions for control commands specific to this engine */
118ebfedea0SLionel Sambuc /* #define E_GMP_CMD_SO_PATH ENGINE_CMD_BASE */
119ebfedea0SLionel Sambuc static const ENGINE_CMD_DEFN e_gmp_cmd_defns[] = {
120ebfedea0SLionel Sambuc # if 0
121ebfedea0SLionel Sambuc {E_GMP_CMD_SO_PATH,
122ebfedea0SLionel Sambuc "SO_PATH",
123ebfedea0SLionel Sambuc "Specifies the path to the 'e_gmp' shared library",
124ebfedea0SLionel Sambuc ENGINE_CMD_FLAG_STRING},
125ebfedea0SLionel Sambuc # endif
126ebfedea0SLionel Sambuc {0, NULL, NULL, 0}
127ebfedea0SLionel Sambuc };
128ebfedea0SLionel Sambuc
129ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
130ebfedea0SLionel Sambuc /* Our internal RSA_METHOD that we provide pointers to */
131*0a6a1f1dSLionel Sambuc static RSA_METHOD e_gmp_rsa = {
132ebfedea0SLionel Sambuc "GMP RSA method",
133ebfedea0SLionel Sambuc NULL,
134ebfedea0SLionel Sambuc NULL,
135ebfedea0SLionel Sambuc NULL,
136ebfedea0SLionel Sambuc NULL,
137ebfedea0SLionel Sambuc e_gmp_rsa_mod_exp,
138ebfedea0SLionel Sambuc NULL,
139ebfedea0SLionel Sambuc NULL,
140ebfedea0SLionel Sambuc e_gmp_rsa_finish,
141*0a6a1f1dSLionel Sambuc /*
142*0a6a1f1dSLionel Sambuc * These flags initialise montgomery crud that GMP ignores, however it
143ebfedea0SLionel Sambuc * makes sure the public key ops (which are done in openssl) don't seem
144*0a6a1f1dSLionel Sambuc * *slower* than usual :-)
145*0a6a1f1dSLionel Sambuc */
146ebfedea0SLionel Sambuc RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
147ebfedea0SLionel Sambuc NULL,
148ebfedea0SLionel Sambuc NULL,
149ebfedea0SLionel Sambuc NULL
150ebfedea0SLionel Sambuc };
151ebfedea0SLionel Sambuc # endif
152ebfedea0SLionel Sambuc
153ebfedea0SLionel Sambuc /* Constants used when creating the ENGINE */
154ebfedea0SLionel Sambuc static const char *engine_e_gmp_id = "gmp";
155ebfedea0SLionel Sambuc static const char *engine_e_gmp_name = "GMP engine support";
156ebfedea0SLionel Sambuc
157*0a6a1f1dSLionel Sambuc /*
158*0a6a1f1dSLionel Sambuc * This internal function is used by ENGINE_gmp() and possibly by the
159*0a6a1f1dSLionel Sambuc * "dynamic" ENGINE support too
160*0a6a1f1dSLionel Sambuc */
bind_helper(ENGINE * e)161ebfedea0SLionel Sambuc static int bind_helper(ENGINE *e)
162ebfedea0SLionel Sambuc {
163ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
164ebfedea0SLionel Sambuc const RSA_METHOD *meth1;
165ebfedea0SLionel Sambuc # endif
166ebfedea0SLionel Sambuc if (!ENGINE_set_id(e, engine_e_gmp_id) ||
167ebfedea0SLionel Sambuc !ENGINE_set_name(e, engine_e_gmp_name) ||
168ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
169ebfedea0SLionel Sambuc !ENGINE_set_RSA(e, &e_gmp_rsa) ||
170ebfedea0SLionel Sambuc # endif
171ebfedea0SLionel Sambuc !ENGINE_set_destroy_function(e, e_gmp_destroy) ||
172ebfedea0SLionel Sambuc !ENGINE_set_init_function(e, e_gmp_init) ||
173ebfedea0SLionel Sambuc !ENGINE_set_finish_function(e, e_gmp_finish) ||
174ebfedea0SLionel Sambuc !ENGINE_set_ctrl_function(e, e_gmp_ctrl) ||
175ebfedea0SLionel Sambuc !ENGINE_set_cmd_defns(e, e_gmp_cmd_defns))
176ebfedea0SLionel Sambuc return 0;
177ebfedea0SLionel Sambuc
178ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
179ebfedea0SLionel Sambuc meth1 = RSA_PKCS1_SSLeay();
180ebfedea0SLionel Sambuc e_gmp_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
181ebfedea0SLionel Sambuc e_gmp_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
182ebfedea0SLionel Sambuc e_gmp_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
183ebfedea0SLionel Sambuc e_gmp_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
184ebfedea0SLionel Sambuc e_gmp_rsa.bn_mod_exp = meth1->bn_mod_exp;
185ebfedea0SLionel Sambuc # endif
186ebfedea0SLionel Sambuc
187ebfedea0SLionel Sambuc /* Ensure the e_gmp error handling is set up */
188ebfedea0SLionel Sambuc ERR_load_GMP_strings();
189ebfedea0SLionel Sambuc return 1;
190ebfedea0SLionel Sambuc }
191ebfedea0SLionel Sambuc
engine_gmp(void)192ebfedea0SLionel Sambuc static ENGINE *engine_gmp(void)
193ebfedea0SLionel Sambuc {
194ebfedea0SLionel Sambuc ENGINE *ret = ENGINE_new();
195ebfedea0SLionel Sambuc if (!ret)
196ebfedea0SLionel Sambuc return NULL;
197*0a6a1f1dSLionel Sambuc if (!bind_helper(ret)) {
198ebfedea0SLionel Sambuc ENGINE_free(ret);
199ebfedea0SLionel Sambuc return NULL;
200ebfedea0SLionel Sambuc }
201ebfedea0SLionel Sambuc return ret;
202ebfedea0SLionel Sambuc }
203ebfedea0SLionel Sambuc
ENGINE_load_gmp(void)204ebfedea0SLionel Sambuc void ENGINE_load_gmp(void)
205ebfedea0SLionel Sambuc {
206ebfedea0SLionel Sambuc /* Copied from eng_[openssl|dyn].c */
207ebfedea0SLionel Sambuc ENGINE *toadd = engine_gmp();
208*0a6a1f1dSLionel Sambuc if (!toadd)
209*0a6a1f1dSLionel Sambuc return;
210ebfedea0SLionel Sambuc ENGINE_add(toadd);
211ebfedea0SLionel Sambuc ENGINE_free(toadd);
212ebfedea0SLionel Sambuc ERR_clear_error();
213ebfedea0SLionel Sambuc }
214ebfedea0SLionel Sambuc
215ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
216ebfedea0SLionel Sambuc /* Used to attach our own key-data to an RSA structure */
217ebfedea0SLionel Sambuc static int hndidx_rsa = -1;
218ebfedea0SLionel Sambuc # endif
219ebfedea0SLionel Sambuc
e_gmp_destroy(ENGINE * e)220ebfedea0SLionel Sambuc static int e_gmp_destroy(ENGINE *e)
221ebfedea0SLionel Sambuc {
222ebfedea0SLionel Sambuc ERR_unload_GMP_strings();
223ebfedea0SLionel Sambuc return 1;
224ebfedea0SLionel Sambuc }
225ebfedea0SLionel Sambuc
226ebfedea0SLionel Sambuc /* (de)initialisation functions. */
e_gmp_init(ENGINE * e)227ebfedea0SLionel Sambuc static int e_gmp_init(ENGINE *e)
228ebfedea0SLionel Sambuc {
229ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
230ebfedea0SLionel Sambuc if (hndidx_rsa == -1)
231ebfedea0SLionel Sambuc hndidx_rsa = RSA_get_ex_new_index(0,
232ebfedea0SLionel Sambuc "GMP-based RSA key handle",
233ebfedea0SLionel Sambuc NULL, NULL, NULL);
234ebfedea0SLionel Sambuc # endif
235ebfedea0SLionel Sambuc if (hndidx_rsa == -1)
236ebfedea0SLionel Sambuc return 0;
237ebfedea0SLionel Sambuc return 1;
238ebfedea0SLionel Sambuc }
239ebfedea0SLionel Sambuc
e_gmp_finish(ENGINE * e)240ebfedea0SLionel Sambuc static int e_gmp_finish(ENGINE *e)
241ebfedea0SLionel Sambuc {
242ebfedea0SLionel Sambuc return 1;
243ebfedea0SLionel Sambuc }
244ebfedea0SLionel Sambuc
e_gmp_ctrl(ENGINE * e,int cmd,long i,void * p,void (* f)(void))245ebfedea0SLionel Sambuc static int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
246ebfedea0SLionel Sambuc {
247ebfedea0SLionel Sambuc int to_return = 1;
248ebfedea0SLionel Sambuc
249*0a6a1f1dSLionel Sambuc switch (cmd) {
250ebfedea0SLionel Sambuc # if 0
251ebfedea0SLionel Sambuc case E_GMP_CMD_SO_PATH:
252ebfedea0SLionel Sambuc /* ... */
253ebfedea0SLionel Sambuc # endif
254ebfedea0SLionel Sambuc /* The command isn't understood by this engine */
255ebfedea0SLionel Sambuc default:
256*0a6a1f1dSLionel Sambuc GMPerr(GMP_F_E_GMP_CTRL, GMP_R_CTRL_COMMAND_NOT_IMPLEMENTED);
257ebfedea0SLionel Sambuc to_return = 0;
258ebfedea0SLionel Sambuc break;
259ebfedea0SLionel Sambuc }
260ebfedea0SLionel Sambuc
261ebfedea0SLionel Sambuc return to_return;
262ebfedea0SLionel Sambuc }
263ebfedea0SLionel Sambuc
264*0a6a1f1dSLionel Sambuc /*
265*0a6a1f1dSLionel Sambuc * Most often limb sizes will be the same. If not, we use hex conversion
266*0a6a1f1dSLionel Sambuc * which is neat, but extremely inefficient.
267*0a6a1f1dSLionel Sambuc */
bn2gmp(const BIGNUM * bn,mpz_t g)268ebfedea0SLionel Sambuc static int bn2gmp(const BIGNUM *bn, mpz_t g)
269ebfedea0SLionel Sambuc {
270ebfedea0SLionel Sambuc bn_check_top(bn);
271ebfedea0SLionel Sambuc if (((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
272*0a6a1f1dSLionel Sambuc (BN_BITS2 == GMP_NUMB_BITS)) {
273ebfedea0SLionel Sambuc /* The common case */
274ebfedea0SLionel Sambuc if (!_mpz_realloc(g, bn->top))
275ebfedea0SLionel Sambuc return 0;
276ebfedea0SLionel Sambuc memcpy(&g->_mp_d[0], &bn->d[0], bn->top * sizeof(bn->d[0]));
277ebfedea0SLionel Sambuc g->_mp_size = bn->top;
278ebfedea0SLionel Sambuc if (bn->neg)
279ebfedea0SLionel Sambuc g->_mp_size = -g->_mp_size;
280ebfedea0SLionel Sambuc return 1;
281*0a6a1f1dSLionel Sambuc } else {
282ebfedea0SLionel Sambuc int toret;
283ebfedea0SLionel Sambuc char *tmpchar = BN_bn2hex(bn);
284*0a6a1f1dSLionel Sambuc if (!tmpchar)
285*0a6a1f1dSLionel Sambuc return 0;
286ebfedea0SLionel Sambuc toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
287ebfedea0SLionel Sambuc OPENSSL_free(tmpchar);
288ebfedea0SLionel Sambuc return toret;
289ebfedea0SLionel Sambuc }
290ebfedea0SLionel Sambuc }
291ebfedea0SLionel Sambuc
gmp2bn(mpz_t g,BIGNUM * bn)292ebfedea0SLionel Sambuc static int gmp2bn(mpz_t g, BIGNUM *bn)
293ebfedea0SLionel Sambuc {
294ebfedea0SLionel Sambuc if (((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
295*0a6a1f1dSLionel Sambuc (BN_BITS2 == GMP_NUMB_BITS)) {
296ebfedea0SLionel Sambuc /* The common case */
297ebfedea0SLionel Sambuc int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size;
298ebfedea0SLionel Sambuc BN_zero(bn);
299ebfedea0SLionel Sambuc if (bn_expand2(bn, s) == NULL)
300ebfedea0SLionel Sambuc return 0;
301ebfedea0SLionel Sambuc bn->top = s;
302ebfedea0SLionel Sambuc memcpy(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0]));
303ebfedea0SLionel Sambuc bn_correct_top(bn);
304ebfedea0SLionel Sambuc bn->neg = g->_mp_size >= 0 ? 0 : 1;
305ebfedea0SLionel Sambuc return 1;
306*0a6a1f1dSLionel Sambuc } else {
307ebfedea0SLionel Sambuc int toret;
308ebfedea0SLionel Sambuc char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
309*0a6a1f1dSLionel Sambuc if (!tmpchar)
310*0a6a1f1dSLionel Sambuc return 0;
311ebfedea0SLionel Sambuc mpz_get_str(tmpchar, 16, g);
312ebfedea0SLionel Sambuc toret = BN_hex2bn(&bn, tmpchar);
313ebfedea0SLionel Sambuc OPENSSL_free(tmpchar);
314ebfedea0SLionel Sambuc return toret;
315ebfedea0SLionel Sambuc }
316ebfedea0SLionel Sambuc }
317ebfedea0SLionel Sambuc
318ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
319*0a6a1f1dSLionel Sambuc typedef struct st_e_gmp_rsa_ctx {
320ebfedea0SLionel Sambuc int public_only;
321ebfedea0SLionel Sambuc mpz_t n;
322ebfedea0SLionel Sambuc mpz_t d;
323ebfedea0SLionel Sambuc mpz_t e;
324ebfedea0SLionel Sambuc mpz_t p;
325ebfedea0SLionel Sambuc mpz_t q;
326ebfedea0SLionel Sambuc mpz_t dmp1;
327ebfedea0SLionel Sambuc mpz_t dmq1;
328ebfedea0SLionel Sambuc mpz_t iqmp;
329ebfedea0SLionel Sambuc mpz_t r0, r1, I0, m1;
330ebfedea0SLionel Sambuc } E_GMP_RSA_CTX;
331ebfedea0SLionel Sambuc
e_gmp_get_rsa(RSA * rsa)332ebfedea0SLionel Sambuc static E_GMP_RSA_CTX *e_gmp_get_rsa(RSA *rsa)
333ebfedea0SLionel Sambuc {
334ebfedea0SLionel Sambuc E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
335*0a6a1f1dSLionel Sambuc if (hptr)
336*0a6a1f1dSLionel Sambuc return hptr;
337ebfedea0SLionel Sambuc hptr = OPENSSL_malloc(sizeof(E_GMP_RSA_CTX));
338*0a6a1f1dSLionel Sambuc if (!hptr)
339*0a6a1f1dSLionel Sambuc return NULL;
340*0a6a1f1dSLionel Sambuc /*
341*0a6a1f1dSLionel Sambuc * These inits could probably be replaced by more intelligent mpz_init2()
342*0a6a1f1dSLionel Sambuc * versions, to reduce malloc-thrashing.
343*0a6a1f1dSLionel Sambuc */
344ebfedea0SLionel Sambuc mpz_init(hptr->n);
345ebfedea0SLionel Sambuc mpz_init(hptr->d);
346ebfedea0SLionel Sambuc mpz_init(hptr->e);
347ebfedea0SLionel Sambuc mpz_init(hptr->p);
348ebfedea0SLionel Sambuc mpz_init(hptr->q);
349ebfedea0SLionel Sambuc mpz_init(hptr->dmp1);
350ebfedea0SLionel Sambuc mpz_init(hptr->dmq1);
351ebfedea0SLionel Sambuc mpz_init(hptr->iqmp);
352ebfedea0SLionel Sambuc mpz_init(hptr->r0);
353ebfedea0SLionel Sambuc mpz_init(hptr->r1);
354ebfedea0SLionel Sambuc mpz_init(hptr->I0);
355ebfedea0SLionel Sambuc mpz_init(hptr->m1);
356ebfedea0SLionel Sambuc if (!bn2gmp(rsa->n, hptr->n) || !bn2gmp(rsa->e, hptr->e))
357ebfedea0SLionel Sambuc goto err;
358*0a6a1f1dSLionel Sambuc if (!rsa->p || !rsa->q || !rsa->d || !rsa->dmp1 || !rsa->dmq1
359*0a6a1f1dSLionel Sambuc || !rsa->iqmp) {
360ebfedea0SLionel Sambuc hptr->public_only = 1;
361ebfedea0SLionel Sambuc return hptr;
362ebfedea0SLionel Sambuc }
363ebfedea0SLionel Sambuc if (!bn2gmp(rsa->d, hptr->d) || !bn2gmp(rsa->p, hptr->p) ||
364ebfedea0SLionel Sambuc !bn2gmp(rsa->q, hptr->q) || !bn2gmp(rsa->dmp1, hptr->dmp1) ||
365ebfedea0SLionel Sambuc !bn2gmp(rsa->dmq1, hptr->dmq1) || !bn2gmp(rsa->iqmp, hptr->iqmp))
366ebfedea0SLionel Sambuc goto err;
367ebfedea0SLionel Sambuc hptr->public_only = 0;
368ebfedea0SLionel Sambuc RSA_set_ex_data(rsa, hndidx_rsa, hptr);
369ebfedea0SLionel Sambuc return hptr;
370ebfedea0SLionel Sambuc err:
371ebfedea0SLionel Sambuc mpz_clear(hptr->n);
372ebfedea0SLionel Sambuc mpz_clear(hptr->d);
373ebfedea0SLionel Sambuc mpz_clear(hptr->e);
374ebfedea0SLionel Sambuc mpz_clear(hptr->p);
375ebfedea0SLionel Sambuc mpz_clear(hptr->q);
376ebfedea0SLionel Sambuc mpz_clear(hptr->dmp1);
377ebfedea0SLionel Sambuc mpz_clear(hptr->dmq1);
378ebfedea0SLionel Sambuc mpz_clear(hptr->iqmp);
379ebfedea0SLionel Sambuc mpz_clear(hptr->r0);
380ebfedea0SLionel Sambuc mpz_clear(hptr->r1);
381ebfedea0SLionel Sambuc mpz_clear(hptr->I0);
382ebfedea0SLionel Sambuc mpz_clear(hptr->m1);
383ebfedea0SLionel Sambuc OPENSSL_free(hptr);
384ebfedea0SLionel Sambuc return NULL;
385ebfedea0SLionel Sambuc }
386ebfedea0SLionel Sambuc
e_gmp_rsa_finish(RSA * rsa)387ebfedea0SLionel Sambuc static int e_gmp_rsa_finish(RSA *rsa)
388ebfedea0SLionel Sambuc {
389ebfedea0SLionel Sambuc E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
390*0a6a1f1dSLionel Sambuc if (!hptr)
391*0a6a1f1dSLionel Sambuc return 0;
392ebfedea0SLionel Sambuc mpz_clear(hptr->n);
393ebfedea0SLionel Sambuc mpz_clear(hptr->d);
394ebfedea0SLionel Sambuc mpz_clear(hptr->e);
395ebfedea0SLionel Sambuc mpz_clear(hptr->p);
396ebfedea0SLionel Sambuc mpz_clear(hptr->q);
397ebfedea0SLionel Sambuc mpz_clear(hptr->dmp1);
398ebfedea0SLionel Sambuc mpz_clear(hptr->dmq1);
399ebfedea0SLionel Sambuc mpz_clear(hptr->iqmp);
400ebfedea0SLionel Sambuc mpz_clear(hptr->r0);
401ebfedea0SLionel Sambuc mpz_clear(hptr->r1);
402ebfedea0SLionel Sambuc mpz_clear(hptr->I0);
403ebfedea0SLionel Sambuc mpz_clear(hptr->m1);
404ebfedea0SLionel Sambuc OPENSSL_free(hptr);
405ebfedea0SLionel Sambuc RSA_set_ex_data(rsa, hndidx_rsa, NULL);
406ebfedea0SLionel Sambuc return 1;
407ebfedea0SLionel Sambuc }
408ebfedea0SLionel Sambuc
e_gmp_rsa_mod_exp(BIGNUM * r,const BIGNUM * I,RSA * rsa,BN_CTX * ctx)409*0a6a1f1dSLionel Sambuc static int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa,
410*0a6a1f1dSLionel Sambuc BN_CTX *ctx)
411ebfedea0SLionel Sambuc {
412ebfedea0SLionel Sambuc E_GMP_RSA_CTX *hptr;
413ebfedea0SLionel Sambuc int to_return = 0;
414ebfedea0SLionel Sambuc
415ebfedea0SLionel Sambuc hptr = e_gmp_get_rsa(rsa);
416*0a6a1f1dSLionel Sambuc if (!hptr) {
417*0a6a1f1dSLionel Sambuc GMPerr(GMP_F_E_GMP_RSA_MOD_EXP, GMP_R_KEY_CONTEXT_ERROR);
418ebfedea0SLionel Sambuc return 0;
419ebfedea0SLionel Sambuc }
420*0a6a1f1dSLionel Sambuc if (hptr->public_only) {
421*0a6a1f1dSLionel Sambuc GMPerr(GMP_F_E_GMP_RSA_MOD_EXP, GMP_R_MISSING_KEY_COMPONENTS);
422ebfedea0SLionel Sambuc return 0;
423ebfedea0SLionel Sambuc }
424ebfedea0SLionel Sambuc
425ebfedea0SLionel Sambuc /* ugh!!! */
426ebfedea0SLionel Sambuc if (!bn2gmp(I, hptr->I0))
427ebfedea0SLionel Sambuc return 0;
428ebfedea0SLionel Sambuc
429*0a6a1f1dSLionel Sambuc /*
430*0a6a1f1dSLionel Sambuc * This is basically the CRT logic in crypto/rsa/rsa_eay.c reworded into
431ebfedea0SLionel Sambuc * GMP-speak. It may be that GMP's API facilitates cleaner formulations
432ebfedea0SLionel Sambuc * of this stuff, eg. better handling of negatives, or functions that
433*0a6a1f1dSLionel Sambuc * combine operations.
434*0a6a1f1dSLionel Sambuc */
435ebfedea0SLionel Sambuc
436ebfedea0SLionel Sambuc mpz_mod(hptr->r1, hptr->I0, hptr->q);
437ebfedea0SLionel Sambuc mpz_powm(hptr->m1, hptr->r1, hptr->dmq1, hptr->q);
438ebfedea0SLionel Sambuc
439ebfedea0SLionel Sambuc mpz_mod(hptr->r1, hptr->I0, hptr->p);
440ebfedea0SLionel Sambuc mpz_powm(hptr->r0, hptr->r1, hptr->dmp1, hptr->p);
441ebfedea0SLionel Sambuc
442ebfedea0SLionel Sambuc mpz_sub(hptr->r0, hptr->r0, hptr->m1);
443ebfedea0SLionel Sambuc
444ebfedea0SLionel Sambuc if (mpz_sgn(hptr->r0) < 0)
445ebfedea0SLionel Sambuc mpz_add(hptr->r0, hptr->r0, hptr->p);
446ebfedea0SLionel Sambuc mpz_mul(hptr->r1, hptr->r0, hptr->iqmp);
447ebfedea0SLionel Sambuc mpz_mod(hptr->r0, hptr->r1, hptr->p);
448ebfedea0SLionel Sambuc
449ebfedea0SLionel Sambuc if (mpz_sgn(hptr->r0) < 0)
450ebfedea0SLionel Sambuc mpz_add(hptr->r0, hptr->r0, hptr->p);
451ebfedea0SLionel Sambuc mpz_mul(hptr->r1, hptr->r0, hptr->q);
452ebfedea0SLionel Sambuc mpz_add(hptr->r0, hptr->r1, hptr->m1);
453ebfedea0SLionel Sambuc
454ebfedea0SLionel Sambuc /* ugh!!! */
455ebfedea0SLionel Sambuc if (gmp2bn(hptr->r0, r))
456ebfedea0SLionel Sambuc to_return = 1;
457ebfedea0SLionel Sambuc
458ebfedea0SLionel Sambuc return 1;
459ebfedea0SLionel Sambuc }
460ebfedea0SLionel Sambuc # endif
461ebfedea0SLionel Sambuc
462ebfedea0SLionel Sambuc # endif /* !OPENSSL_NO_GMP */
463ebfedea0SLionel Sambuc
464*0a6a1f1dSLionel Sambuc /*
465*0a6a1f1dSLionel Sambuc * This stuff is needed if this ENGINE is being compiled into a
466*0a6a1f1dSLionel Sambuc * self-contained shared-library.
467*0a6a1f1dSLionel Sambuc */
468ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DYNAMIC_ENGINE
IMPLEMENT_DYNAMIC_CHECK_FN()469ebfedea0SLionel Sambuc IMPLEMENT_DYNAMIC_CHECK_FN()
470ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_GMP
471ebfedea0SLionel Sambuc static int bind_fn(ENGINE *e, const char *id)
472ebfedea0SLionel Sambuc {
473ebfedea0SLionel Sambuc if (id && (strcmp(id, engine_e_gmp_id) != 0))
474ebfedea0SLionel Sambuc return 0;
475ebfedea0SLionel Sambuc if (!bind_helper(e))
476ebfedea0SLionel Sambuc return 0;
477ebfedea0SLionel Sambuc return 1;
478ebfedea0SLionel Sambuc }
479*0a6a1f1dSLionel Sambuc
480ebfedea0SLionel Sambuc IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
481ebfedea0SLionel Sambuc # else
482ebfedea0SLionel Sambuc OPENSSL_EXPORT
483ebfedea0SLionel Sambuc int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
484ebfedea0SLionel Sambuc OPENSSL_EXPORT
485*0a6a1f1dSLionel Sambuc int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
486*0a6a1f1dSLionel Sambuc {
487*0a6a1f1dSLionel Sambuc return 0;
488*0a6a1f1dSLionel Sambuc }
489ebfedea0SLionel Sambuc # endif
490ebfedea0SLionel Sambuc # endif /* !OPENSSL_NO_DYNAMIC_ENGINE */
491ebfedea0SLionel Sambuc
492ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_HW */
493