1 /* crypto/engine/e_gmp.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 /* This engine is not (currently) compiled in by default. Do enable it,
60 * reconfigure OpenSSL with "-DOPENSSL_USE_GMP -lgmp". The GMP libraries and
61 * headers must reside in one of the paths searched by the compiler/linker,
62 * otherwise paths must be specified - eg. try configuring with
63 * "-DOPENSSL_USE_GMP -I<includepath> -L<libpath> -lgmp". YMMV. */
65 /* As for what this does - it's a largely unoptimised implementation of an
66 * ENGINE that uses the GMP library to perform RSA private key operations. To
67 * obtain more information about what "unoptimised" means, see my original mail
68 * on the subject (though ignore the build instructions which have since
71 * http://www.mail-archive.com/openssl-dev@openssl.org/msg12227.html
73 * On my athlon system at least, it appears the builtin OpenSSL code is now
74 * slightly faster, which is to say that the RSA-related MPI performance
75 * between OpenSSL's BIGNUM and GMP's mpz implementations is probably pretty
76 * balanced for this chip, and so the performance degradation in this ENGINE by
77 * having to convert to/from GMP formats (and not being able to cache
78 * montgomery forms) is probably the difference. However, if some unconfirmed
79 * reports from users is anything to go by, the situation on some other
80 * chipsets might be a good deal more favourable to the GMP version (eg. PPC).
81 * Feedback welcome. */
85 #include <openssl/crypto.h>
86 #include <openssl/buffer.h>
87 #include <openssl/engine.h>
90 #if defined(OPENSSL_USE_GMP) && !defined(OPENSSL_NO_HW_GMP)
94 #define E_GMP_LIB_NAME "gmp engine"
95 #include "e_gmp_err.c"
97 static int e_gmp_destroy(ENGINE *e);
98 static int e_gmp_init(ENGINE *e);
99 static int e_gmp_finish(ENGINE *e);
100 static int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
102 #ifndef OPENSSL_NO_RSA
104 static int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa);
105 static int e_gmp_rsa_finish(RSA *r);
108 /* The definitions for control commands specific to this engine */
109 /* #define E_GMP_CMD_SO_PATH ENGINE_CMD_BASE */
110 static const ENGINE_CMD_DEFN e_gmp_cmd_defns[] = {
114 "Specifies the path to the 'e_gmp' shared library",
115 ENGINE_CMD_FLAG_STRING},
120 #ifndef OPENSSL_NO_RSA
121 /* Our internal RSA_METHOD that we provide pointers to */
122 static RSA_METHOD e_gmp_rsa =
133 /* These flags initialise montgomery crud that GMP ignores, however it
134 * makes sure the public key ops (which are done in openssl) don't seem
135 * *slower* than usual :-) */
136 RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE,
143 /* Constants used when creating the ENGINE */
144 static const char *engine_e_gmp_id = "gmp";
145 static const char *engine_e_gmp_name = "GMP engine support";
147 /* This internal function is used by ENGINE_gmp() and possibly by the
148 * "dynamic" ENGINE support too */
149 static int bind_helper(ENGINE *e)
151 #ifndef OPENSSL_NO_RSA
152 const RSA_METHOD *meth1;
154 if(!ENGINE_set_id(e, engine_e_gmp_id) ||
155 !ENGINE_set_name(e, engine_e_gmp_name) ||
156 #ifndef OPENSSL_NO_RSA
157 !ENGINE_set_RSA(e, &e_gmp_rsa) ||
159 !ENGINE_set_destroy_function(e, e_gmp_destroy) ||
160 !ENGINE_set_init_function(e, e_gmp_init) ||
161 !ENGINE_set_finish_function(e, e_gmp_finish) ||
162 !ENGINE_set_ctrl_function(e, e_gmp_ctrl) ||
163 !ENGINE_set_cmd_defns(e, e_gmp_cmd_defns))
166 #ifndef OPENSSL_NO_RSA
167 meth1 = RSA_PKCS1_SSLeay();
168 e_gmp_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
169 e_gmp_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
170 e_gmp_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
171 e_gmp_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
172 e_gmp_rsa.bn_mod_exp = meth1->bn_mod_exp;
175 /* Ensure the e_gmp error handling is set up */
176 ERR_load_GMP_strings();
180 static ENGINE *engine_gmp(void)
182 ENGINE *ret = ENGINE_new();
185 if(!bind_helper(ret))
193 void ENGINE_load_gmp(void)
195 /* Copied from eng_[openssl|dyn].c */
196 ENGINE *toadd = engine_gmp();
203 #ifndef OPENSSL_NO_RSA
204 /* Used to attach our own key-data to an RSA structure */
205 static int hndidx_rsa = -1;
208 static int e_gmp_destroy(ENGINE *e)
210 ERR_unload_GMP_strings();
214 /* (de)initialisation functions. */
215 static int e_gmp_init(ENGINE *e)
217 #ifndef OPENSSL_NO_RSA
218 if (hndidx_rsa == -1)
219 hndidx_rsa = RSA_get_ex_new_index(0,
220 "GMP-based RSA key handle",
223 if (hndidx_rsa == -1)
228 static int e_gmp_finish(ENGINE *e)
233 static int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
240 case E_GMP_CMD_SO_PATH:
243 /* The command isn't understood by this engine */
245 GMPerr(GMP_F_E_GMP_CTRL,
246 GMP_R_CTRL_COMMAND_NOT_IMPLEMENTED);
254 /* HACK - use text I/O functions in openssl and GMP to handle conversions. This
256 static int bn2gmp(const BIGNUM *bn, mpz_t g)
259 char *tmpchar = BN_bn2hex(bn);
260 if(!tmpchar) return 0;
261 toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
262 OPENSSL_free(tmpchar);
266 static int gmp2bn(mpz_t g, BIGNUM *bn)
269 char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
270 if(!tmpchar) return 0;
271 mpz_get_str(tmpchar, 16, g);
272 toret = BN_hex2bn(&bn, tmpchar);
273 OPENSSL_free(tmpchar);
277 #ifndef OPENSSL_NO_RSA
278 typedef struct st_e_gmp_rsa_ctx
289 mpz_t r0, r1, I0, m1;
292 static E_GMP_RSA_CTX *e_gmp_get_rsa(RSA *rsa)
294 E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
295 if(hptr) return hptr;
296 hptr = OPENSSL_malloc(sizeof(E_GMP_RSA_CTX));
297 if(!hptr) return NULL;
298 /* These inits could probably be replaced by more intelligent
299 * mpz_init2() versions, to reduce malloc-thrashing. */
305 mpz_init(hptr->dmp1);
306 mpz_init(hptr->dmq1);
307 mpz_init(hptr->iqmp);
312 if(!bn2gmp(rsa->n, hptr->n) || !bn2gmp(rsa->e, hptr->e))
314 if(!rsa->p || !rsa->q || !rsa->d || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
316 hptr->public_only = 1;
319 if(!bn2gmp(rsa->d, hptr->d) || !bn2gmp(rsa->p, hptr->p) ||
320 !bn2gmp(rsa->q, hptr->q) || !bn2gmp(rsa->dmp1, hptr->dmp1) ||
321 !bn2gmp(rsa->dmq1, hptr->dmq1) || !bn2gmp(rsa->iqmp, hptr->iqmp))
323 hptr->public_only = 0;
324 RSA_set_ex_data(rsa, hndidx_rsa, hptr);
332 mpz_clear(hptr->dmp1);
333 mpz_clear(hptr->dmq1);
334 mpz_clear(hptr->iqmp);
343 static int e_gmp_rsa_finish(RSA *rsa)
345 E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
352 mpz_clear(hptr->dmp1);
353 mpz_clear(hptr->dmq1);
354 mpz_clear(hptr->iqmp);
360 RSA_set_ex_data(rsa, hndidx_rsa, NULL);
364 static int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa)
369 hptr = e_gmp_get_rsa(rsa);
372 GMPerr(GMP_F_E_GMP_RSA_MOD_EXP,
373 GMP_R_KEY_CONTEXT_ERROR);
376 if(hptr->public_only)
378 GMPerr(GMP_F_E_GMP_RSA_MOD_EXP,
379 GMP_R_MISSING_KEY_COMPONENTS);
384 if(!bn2gmp(I, hptr->I0))
387 /* This is basically the CRT logic in crypto/rsa/rsa_eay.c reworded into
388 * GMP-speak. It may be that GMP's API facilitates cleaner formulations
389 * of this stuff, eg. better handling of negatives, or functions that
390 * combine operations. */
392 mpz_mod(hptr->r1, hptr->I0, hptr->q);
393 mpz_powm(hptr->m1, hptr->r1, hptr->dmq1, hptr->q);
395 mpz_mod(hptr->r1, hptr->I0, hptr->p);
396 mpz_powm(hptr->r0, hptr->r1, hptr->dmp1, hptr->p);
398 mpz_sub(hptr->r0, hptr->r0, hptr->m1);
400 if(mpz_sgn(hptr->r0) < 0)
401 mpz_add(hptr->r0, hptr->r0, hptr->p);
402 mpz_mul(hptr->r1, hptr->r0, hptr->iqmp);
403 mpz_mod(hptr->r0, hptr->r1, hptr->p);
405 if(mpz_sgn(hptr->r0) < 0)
406 mpz_add(hptr->r0, hptr->r0, hptr->p);
407 mpz_mul(hptr->r1, hptr->r0, hptr->q);
408 mpz_add(hptr->r0, hptr->r1, hptr->m1);
411 if(gmp2bn(hptr->r0, r))
418 /* This stuff is needed if this ENGINE is being compiled into a self-contained
420 #ifdef ENGINE_DYNAMIC_SUPPORT
421 static int bind_fn(ENGINE *e, const char *id)
423 if(id && (strcmp(id, engine_e_gmp_id) != 0))
429 IMPLEMENT_DYNAMIC_CHECK_FN()
430 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
431 #endif /* ENGINE_DYNAMIC_SUPPORT */
433 #endif /* !OPENSSL_NO_HW_GMP */
434 #endif /* !OPENSSL_NO_HW */