~ubuntu-branches/ubuntu/lucid/openssl/lucid-proposed

« back to all changes in this revision

Viewing changes to engines/e_gmp.c

  • Committer: Bazaar Package Importer
  • Author(s): Nicolas Valcárcel Scerpella (Canonical)
  • Date: 2009-12-06 20:16:24 UTC
  • mfrom: (11.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20091206201624-u126qjpqm2n2uuhu
Tags: 0.9.8k-7ubuntu1
* Merge from debian unstable, remaining changes (LP: #493392):
  - Link using -Bsymbolic-functions
  - Add support for lpia
  - Disable SSLv2 during compile
  - Ship documentation in openssl-doc, suggested by the package.
  - Use a different priority for libssl0.9.8/restart-services
    depending on whether a desktop, or server dist-upgrade is being
    performed.
  - Display a system restart required notification bubble on libssl0.9.8
    upgrade.
  - Replace duplicate files in the doc directory with symlinks.
  - Move runtime libraries to /lib, for the benefit of wpasupplicant
* Strip the patches out of the source into quilt patches
* Disable CVE-2009-3555.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
 */
58
58
 
59
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
 
60
 * reconfigure OpenSSL with "enable-gmp -lgmp". The GMP libraries and
61
61
 * headers must reside in one of the paths searched by the compiler/linker,
62
62
 * otherwise paths must be specified - eg. try configuring with
63
 
 * "-DOPENSSL_USE_GMP -I<includepath> -L<libpath> -lgmp". YMMV. */
 
63
 * "enable-gmp -I<includepath> -L<libpath> -lgmp". YMMV. */
64
64
 
65
65
/* As for what this does - it's a largely unoptimised implementation of an
66
66
 * ENGINE that uses the GMP library to perform RSA private key operations. To
85
85
#include <openssl/crypto.h>
86
86
#include <openssl/buffer.h>
87
87
#include <openssl/engine.h>
 
88
#include <openssl/rsa.h>
 
89
#include <openssl/bn.h>
88
90
 
89
91
#ifndef OPENSSL_NO_HW
90
 
#if defined(OPENSSL_USE_GMP) && !defined(OPENSSL_NO_HW_GMP)
 
92
#ifndef OPENSSL_NO_GMP
91
93
 
92
94
#include <gmp.h>
93
95
 
251
253
        return to_return;
252
254
        }
253
255
 
254
 
/* HACK - use text I/O functions in openssl and GMP to handle conversions. This
255
 
 * is vile. */
 
256
 
 
257
/* Most often limb sizes will be the same. If not, we use hex conversion
 
258
 * which is neat, but extremely inefficient. */
256
259
static int bn2gmp(const BIGNUM *bn, mpz_t g)
257
260
        {
258
 
        int toret;
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);
263
 
        return toret;
 
261
        bn_check_top(bn);
 
262
        if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
 
263
                        (BN_BITS2 == GMP_NUMB_BITS)) 
 
264
                {
 
265
                /* The common case */
 
266
                if(!_mpz_realloc (g, bn->top))
 
267
                        return 0;
 
268
                memcpy(&g->_mp_d[0], &bn->d[0], bn->top * sizeof(bn->d[0]));
 
269
                g->_mp_size = bn->top;
 
270
                if(bn->neg)
 
271
                        g->_mp_size = -g->_mp_size;
 
272
                return 1;
 
273
                }
 
274
        else
 
275
                {
 
276
                int toret;
 
277
                char *tmpchar = BN_bn2hex(bn);
 
278
                if(!tmpchar) return 0;
 
279
                toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
 
280
                OPENSSL_free(tmpchar);
 
281
                return toret;
 
282
                }
264
283
        }
265
284
 
266
285
static int gmp2bn(mpz_t g, BIGNUM *bn)
267
286
        {
268
 
        int toret;
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);
274
 
        return toret;
 
287
        if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
 
288
                        (BN_BITS2 == GMP_NUMB_BITS))
 
289
                {
 
290
                /* The common case */
 
291
                int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size;
 
292
                BN_zero(bn);
 
293
                if(bn_expand2 (bn, s) == NULL)
 
294
                        return 0;
 
295
                bn->top = s;
 
296
                memcpy(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0]));
 
297
                bn_correct_top(bn);
 
298
                bn->neg = g->_mp_size >= 0 ? 0 : 1;
 
299
                return 1;
 
300
                }
 
301
        else
 
302
                {
 
303
                int toret;
 
304
                char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
 
305
                if(!tmpchar) return 0;
 
306
                mpz_get_str(tmpchar, 16, g);
 
307
                toret = BN_hex2bn(&bn, tmpchar);
 
308
                OPENSSL_free(tmpchar);
 
309
                return toret;
 
310
                }
275
311
        }
276
312
 
277
313
#ifndef OPENSSL_NO_RSA 
415
451
        }
416
452
#endif
417
453
 
 
454
#endif /* !OPENSSL_NO_GMP */
 
455
 
418
456
/* This stuff is needed if this ENGINE is being compiled into a self-contained
419
457
 * shared-library. */      
420
 
#ifdef ENGINE_DYNAMIC_SUPPORT
 
458
#ifndef OPENSSL_NO_DYNAMIC_ENGINE
 
459
IMPLEMENT_DYNAMIC_CHECK_FN()
 
460
#ifndef OPENSSL_NO_GMP
421
461
static int bind_fn(ENGINE *e, const char *id)
422
462
        {
423
463
        if(id && (strcmp(id, engine_e_gmp_id) != 0))
426
466
                return 0;
427
467
        return 1;
428
468
        }       
429
 
IMPLEMENT_DYNAMIC_CHECK_FN()
430
469
IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
431
 
#endif /* ENGINE_DYNAMIC_SUPPORT */
 
470
#else
 
471
OPENSSL_EXPORT
 
472
int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) { return 0; }
 
473
#endif
 
474
#endif /* OPENSSL_NO_DYNAMIC_ENGINE */
432
475
 
433
 
#endif /* !OPENSSL_NO_HW_GMP */
434
476
#endif /* !OPENSSL_NO_HW */
435