~ubuntu-branches/ubuntu/hardy/openssl/hardy-security

« back to all changes in this revision

Viewing changes to crypto/bn/bn_exp.c

  • Committer: Bazaar Package Importer
  • Author(s): Kurt Roeckx
  • Date: 2005-12-13 21:37:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051213213742-7em5nrw5c7ceegyd
Tags: 0.9.8a-5
Stop ssh from crashing randomly on sparc (Closes: #335912)
Patch from upstream cvs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
 * [including the GNU Public Licence.]
57
57
 */
58
58
/* ====================================================================
59
 
 * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
 
59
 * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
60
60
 *
61
61
 * Redistribution and use in source and binary forms, with or without
62
62
 * modification, are permitted provided that the following conditions
113
113
#include "cryptlib.h"
114
114
#include "bn_lcl.h"
115
115
 
 
116
/* maximum precomputation table size for *variable* sliding windows */
116
117
#define TABLE_SIZE      32
117
118
 
118
119
/* this one works - simple but works */
121
122
        int i,bits,ret=0;
122
123
        BIGNUM *v,*rr;
123
124
 
 
125
        if (BN_get_flags(p, BN_FLG_EXP_CONSTTIME) != 0)
 
126
                {
 
127
                /* BN_FLG_EXP_CONSTTIME only supported by BN_mod_exp_mont() */
 
128
                BNerr(BN_F_BN_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
 
129
                return -1;
 
130
                }
 
131
 
124
132
        BN_CTX_start(ctx);
125
133
        if ((r == a) || (r == p))
126
134
                rr = BN_CTX_get(ctx);
147
155
err:
148
156
        if (r != rr) BN_copy(r,rr);
149
157
        BN_CTX_end(ctx);
 
158
        bn_check_top(r);
150
159
        return(ret);
151
160
        }
152
161
 
204
213
        if (BN_is_odd(m))
205
214
                {
206
215
#  ifdef MONT_EXP_WORD
207
 
                if (a->top == 1 && !a->neg)
 
216
                if (a->top == 1 && !a->neg && (BN_get_flags(p, BN_FLG_EXP_CONSTTIME) == 0))
208
217
                        {
209
218
                        BN_ULONG A = a->d[0];
210
219
                        ret=BN_mod_exp_mont_word(r,A,p,m,ctx,NULL);
221
230
                { ret=BN_mod_exp_simple(r,a,p,m,ctx); }
222
231
#endif
223
232
 
 
233
        bn_check_top(r);
224
234
        return(ret);
225
235
        }
226
236
 
229
239
                    const BIGNUM *m, BN_CTX *ctx)
230
240
        {
231
241
        int i,j,bits,ret=0,wstart,wend,window,wvalue;
232
 
        int start=1,ts=0;
 
242
        int start=1;
233
243
        BIGNUM *aa;
234
 
        BIGNUM val[TABLE_SIZE];
 
244
        /* Table of variables obtained from 'ctx' */
 
245
        BIGNUM *val[TABLE_SIZE];
235
246
        BN_RECP_CTX recp;
236
247
 
 
248
        if (BN_get_flags(p, BN_FLG_EXP_CONSTTIME) != 0)
 
249
                {
 
250
                /* BN_FLG_EXP_CONSTTIME only supported by BN_mod_exp_mont() */
 
251
                BNerr(BN_F_BN_MOD_EXP_RECP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
 
252
                return -1;
 
253
                }
 
254
 
237
255
        bits=BN_num_bits(p);
238
256
 
239
257
        if (bits == 0)
243
261
                }
244
262
 
245
263
        BN_CTX_start(ctx);
246
 
        if ((aa = BN_CTX_get(ctx)) == NULL) goto err;
 
264
        aa = BN_CTX_get(ctx);
 
265
        val[0] = BN_CTX_get(ctx);
 
266
        if(!aa || !val[0]) goto err;
247
267
 
248
268
        BN_RECP_CTX_init(&recp);
249
269
        if (m->neg)
258
278
                if (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;
259
279
                }
260
280
 
261
 
        BN_init(&(val[0]));
262
 
        ts=1;
263
 
 
264
 
        if (!BN_nnmod(&(val[0]),a,m,ctx)) goto err;             /* 1 */
265
 
        if (BN_is_zero(&(val[0])))
 
281
        if (!BN_nnmod(val[0],a,m,ctx)) goto err;                /* 1 */
 
282
        if (BN_is_zero(val[0]))
266
283
                {
267
 
                ret = BN_zero(r);
 
284
                BN_zero(r);
 
285
                ret = 1;
268
286
                goto err;
269
287
                }
270
288
 
271
289
        window = BN_window_bits_for_exponent_size(bits);
272
290
        if (window > 1)
273
291
                {
274
 
                if (!BN_mod_mul_reciprocal(aa,&(val[0]),&(val[0]),&recp,ctx))
 
292
                if (!BN_mod_mul_reciprocal(aa,val[0],val[0],&recp,ctx))
275
293
                        goto err;                               /* 2 */
276
294
                j=1<<(window-1);
277
295
                for (i=1; i<j; i++)
278
296
                        {
279
 
                        BN_init(&val[i]);
280
 
                        if (!BN_mod_mul_reciprocal(&(val[i]),&(val[i-1]),aa,&recp,ctx))
 
297
                        if(((val[i] = BN_CTX_get(ctx)) == NULL) ||
 
298
                                        !BN_mod_mul_reciprocal(val[i],val[i-1],
 
299
                                                aa,&recp,ctx))
281
300
                                goto err;
282
301
                        }
283
 
                ts=i;
284
302
                }
285
303
                
286
304
        start=1;        /* This is used to avoid multiplication etc
332
350
                                }
333
351
                
334
352
                /* wvalue will be an odd number < 2^window */
335
 
                if (!BN_mod_mul_reciprocal(r,r,&(val[wvalue>>1]),&recp,ctx))
 
353
                if (!BN_mod_mul_reciprocal(r,r,val[wvalue>>1],&recp,ctx))
336
354
                        goto err;
337
355
 
338
356
                /* move the 'window' down further */
344
362
        ret=1;
345
363
err:
346
364
        BN_CTX_end(ctx);
347
 
        for (i=0; i<ts; i++)
348
 
                BN_clear_free(&(val[i]));
349
365
        BN_RECP_CTX_free(&recp);
 
366
        bn_check_top(r);
350
367
        return(ret);
351
368
        }
352
369
 
355
372
                    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
356
373
        {
357
374
        int i,j,bits,ret=0,wstart,wend,window,wvalue;
358
 
        int start=1,ts=0;
 
375
        int start=1;
359
376
        BIGNUM *d,*r;
360
377
        const BIGNUM *aa;
361
 
        BIGNUM val[TABLE_SIZE];
 
378
        /* Table of variables obtained from 'ctx' */
 
379
        BIGNUM *val[TABLE_SIZE];
362
380
        BN_MONT_CTX *mont=NULL;
363
381
 
 
382
        if (BN_get_flags(p, BN_FLG_EXP_CONSTTIME) != 0)
 
383
                {
 
384
                return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);
 
385
                }
 
386
 
364
387
        bn_check_top(a);
365
388
        bn_check_top(p);
366
389
        bn_check_top(m);
367
390
 
368
 
        if (!(m->d[0] & 1))
 
391
        if (!BN_is_odd(m))
369
392
                {
370
393
                BNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);
371
394
                return(0);
380
403
        BN_CTX_start(ctx);
381
404
        d = BN_CTX_get(ctx);
382
405
        r = BN_CTX_get(ctx);
383
 
        if (d == NULL || r == NULL) goto err;
 
406
        val[0] = BN_CTX_get(ctx);
 
407
        if (!d || !r || !val[0]) goto err;
384
408
 
385
409
        /* If this is not done, things will break in the montgomery
386
410
         * part */
393
417
                if (!BN_MONT_CTX_set(mont,m,ctx)) goto err;
394
418
                }
395
419
 
396
 
        BN_init(&val[0]);
397
 
        ts=1;
398
420
        if (a->neg || BN_ucmp(a,m) >= 0)
399
421
                {
400
 
                if (!BN_nnmod(&(val[0]),a,m,ctx))
 
422
                if (!BN_nnmod(val[0],a,m,ctx))
401
423
                        goto err;
402
 
                aa= &(val[0]);
 
424
                aa= val[0];
403
425
                }
404
426
        else
405
427
                aa=a;
406
428
        if (BN_is_zero(aa))
407
429
                {
408
 
                ret = BN_zero(rr);
 
430
                BN_zero(rr);
 
431
                ret = 1;
409
432
                goto err;
410
433
                }
411
 
        if (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err; /* 1 */
 
434
        if (!BN_to_montgomery(val[0],aa,mont,ctx)) goto err; /* 1 */
412
435
 
413
436
        window = BN_window_bits_for_exponent_size(bits);
414
437
        if (window > 1)
415
438
                {
416
 
                if (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err; /* 2 */
 
439
                if (!BN_mod_mul_montgomery(d,val[0],val[0],mont,ctx)) goto err; /* 2 */
417
440
                j=1<<(window-1);
418
441
                for (i=1; i<j; i++)
419
442
                        {
420
 
                        BN_init(&(val[i]));
421
 
                        if (!BN_mod_mul_montgomery(&(val[i]),&(val[i-1]),d,mont,ctx))
 
443
                        if(((val[i] = BN_CTX_get(ctx)) == NULL) ||
 
444
                                        !BN_mod_mul_montgomery(val[i],val[i-1],
 
445
                                                d,mont,ctx))
422
446
                                goto err;
423
447
                        }
424
 
                ts=i;
425
448
                }
426
449
 
427
450
        start=1;        /* This is used to avoid multiplication etc
474
497
                                }
475
498
                
476
499
                /* wvalue will be an odd number < 2^window */
477
 
                if (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,ctx))
 
500
                if (!BN_mod_mul_montgomery(r,r,val[wvalue>>1],mont,ctx))
478
501
                        goto err;
479
502
 
480
503
                /* move the 'window' down further */
488
511
err:
489
512
        if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
490
513
        BN_CTX_end(ctx);
491
 
        for (i=0; i<ts; i++)
492
 
                BN_clear_free(&(val[i]));
 
514
        bn_check_top(rr);
 
515
        return(ret);
 
516
        }
 
517
 
 
518
 
 
519
/* BN_mod_exp_mont_consttime() stores the precomputed powers in a specific layout
 
520
 * so that accessing any of these table values shows the same access pattern as far
 
521
 * as cache lines are concerned.  The following functions are used to transfer a BIGNUM
 
522
 * from/to that table. */
 
523
 
 
524
static int MOD_EXP_CTIME_COPY_TO_PREBUF(BIGNUM *b, int top, unsigned char *buf, int idx, int width)
 
525
        {
 
526
        size_t i, j;
 
527
 
 
528
        if (bn_wexpand(b, top) == NULL)
 
529
                return 0;
 
530
        while (b->top < top)
 
531
                {
 
532
                b->d[b->top++] = 0;
 
533
                }
 
534
        
 
535
        for (i = 0, j=idx; i < top * sizeof b->d[0]; i++, j+=width)
 
536
                {
 
537
                buf[j] = ((unsigned char*)b->d)[i];
 
538
                }
 
539
 
 
540
        bn_correct_top(b);
 
541
        return 1;
 
542
        }
 
543
 
 
544
static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top, unsigned char *buf, int idx, int width)
 
545
        {
 
546
        size_t i, j;
 
547
 
 
548
        if (bn_wexpand(b, top) == NULL)
 
549
                return 0;
 
550
 
 
551
        for (i=0, j=idx; i < top * sizeof b->d[0]; i++, j+=width)
 
552
                {
 
553
                ((unsigned char*)b->d)[i] = buf[j];
 
554
                }
 
555
 
 
556
        b->top = top;
 
557
        bn_correct_top(b);
 
558
        return 1;
 
559
        }       
 
560
 
 
561
/* Given a pointer value, compute the next address that is a cache line multiple. */
 
562
#define MOD_EXP_CTIME_ALIGN(x_) \
 
563
        ((unsigned char*)(x_) + (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - (((BN_ULONG)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK))))
 
564
 
 
565
/* This variant of BN_mod_exp_mont() uses fixed windows and the special
 
566
 * precomputation memory layout to limit data-dependency to a minimum
 
567
 * to protect secret exponents (cf. the hyper-threading timing attacks
 
568
 * pointed out by Colin Percival,
 
569
 * http://www.daemonology.net/hyperthreading-considered-harmful/)
 
570
 */
 
571
int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
 
572
                    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
 
573
        {
 
574
        int i,bits,ret=0,idx,window,wvalue;
 
575
        int top;
 
576
        BIGNUM *r;
 
577
        const BIGNUM *aa;
 
578
        BN_MONT_CTX *mont=NULL;
 
579
 
 
580
        int numPowers;
 
581
        unsigned char *powerbufFree=NULL;
 
582
        int powerbufLen = 0;
 
583
        unsigned char *powerbuf=NULL;
 
584
        BIGNUM *computeTemp=NULL, *am=NULL;
 
585
 
 
586
        bn_check_top(a);
 
587
        bn_check_top(p);
 
588
        bn_check_top(m);
 
589
 
 
590
        top = m->top;
 
591
 
 
592
        if (!(m->d[0] & 1))
 
593
                {
 
594
                BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME,BN_R_CALLED_WITH_EVEN_MODULUS);
 
595
                return(0);
 
596
                }
 
597
        bits=BN_num_bits(p);
 
598
        if (bits == 0)
 
599
                {
 
600
                ret = BN_one(rr);
 
601
                return ret;
 
602
                }
 
603
 
 
604
        /* Initialize BIGNUM context and allocate intermediate result */
 
605
        BN_CTX_start(ctx);
 
606
        r = BN_CTX_get(ctx);
 
607
        if (r == NULL) goto err;
 
608
 
 
609
        /* Allocate a montgomery context if it was not supplied by the caller.
 
610
         * If this is not done, things will break in the montgomery part.
 
611
         */
 
612
        if (in_mont != NULL)
 
613
                mont=in_mont;
 
614
        else
 
615
                {
 
616
                if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
 
617
                if (!BN_MONT_CTX_set(mont,m,ctx)) goto err;
 
618
                }
 
619
 
 
620
        /* Get the window size to use with size of p. */
 
621
        window = BN_window_bits_for_ctime_exponent_size(bits);
 
622
 
 
623
        /* Allocate a buffer large enough to hold all of the pre-computed
 
624
         * powers of a.
 
625
         */
 
626
        numPowers = 1 << window;
 
627
        powerbufLen = sizeof(m->d[0])*top*numPowers;
 
628
        if ((powerbufFree=(unsigned char*)OPENSSL_malloc(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL)
 
629
                goto err;
 
630
                
 
631
        powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);
 
632
        memset(powerbuf, 0, powerbufLen);
 
633
 
 
634
        /* Initialize the intermediate result. Do this early to save double conversion,
 
635
         * once each for a^0 and intermediate result.
 
636
         */
 
637
        if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;
 
638
        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(r, top, powerbuf, 0, numPowers)) goto err;
 
639
 
 
640
        /* Initialize computeTemp as a^1 with montgomery precalcs */
 
641
        computeTemp = BN_CTX_get(ctx);
 
642
        am = BN_CTX_get(ctx);
 
643
        if (computeTemp==NULL || am==NULL) goto err;
 
644
 
 
645
        if (a->neg || BN_ucmp(a,m) >= 0)
 
646
                {
 
647
                if (!BN_mod(am,a,m,ctx))
 
648
                        goto err;
 
649
                aa= am;
 
650
                }
 
651
        else
 
652
                aa=a;
 
653
        if (!BN_to_montgomery(am,aa,mont,ctx)) goto err;
 
654
        if (!BN_copy(computeTemp, am)) goto err;
 
655
        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(am, top, powerbuf, 1, numPowers)) goto err;
 
656
 
 
657
        /* If the window size is greater than 1, then calculate
 
658
         * val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1)
 
659
         * (even powers could instead be computed as (a^(i/2))^2
 
660
         * to use the slight performance advantage of sqr over mul).
 
661
         */
 
662
        if (window > 1)
 
663
                {
 
664
                for (i=2; i<numPowers; i++)
 
665
                        {
 
666
                        /* Calculate a^i = a^(i-1) * a */
 
667
                        if (!BN_mod_mul_montgomery(computeTemp,am,computeTemp,mont,ctx))
 
668
                                goto err;
 
669
                        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(computeTemp, top, powerbuf, i, numPowers)) goto err;
 
670
                        }
 
671
                }
 
672
 
 
673
        /* Adjust the number of bits up to a multiple of the window size.
 
674
         * If the exponent length is not a multiple of the window size, then
 
675
         * this pads the most significant bits with zeros to normalize the
 
676
         * scanning loop to there's no special cases.
 
677
         *
 
678
         * * NOTE: Making the window size a power of two less than the native
 
679
         * * word size ensures that the padded bits won't go past the last
 
680
         * * word in the internal BIGNUM structure. Going past the end will
 
681
         * * still produce the correct result, but causes a different branch
 
682
         * * to be taken in the BN_is_bit_set function.
 
683
         */
 
684
        bits = ((bits+window-1)/window)*window;
 
685
        idx=bits-1;     /* The top bit of the window */
 
686
 
 
687
        /* Scan the exponent one window at a time starting from the most
 
688
         * significant bits.
 
689
         */
 
690
        while (idx >= 0)
 
691
                {
 
692
                wvalue=0; /* The 'value' of the window */
 
693
                
 
694
                /* Scan the window, squaring the result as we go */
 
695
                for (i=0; i<window; i++,idx--)
 
696
                        {
 
697
                        if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))     goto err;
 
698
                        wvalue = (wvalue<<1)+BN_is_bit_set(p,idx);
 
699
                        }
 
700
                
 
701
                /* Fetch the appropriate pre-computed value from the pre-buf */
 
702
                if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(computeTemp, top, powerbuf, wvalue, numPowers)) goto err;
 
703
 
 
704
                /* Multiply the result into the intermediate result */
 
705
                if (!BN_mod_mul_montgomery(r,r,computeTemp,mont,ctx)) goto err;
 
706
                }
 
707
 
 
708
        /* Convert the final result from montgomery to standard format */
 
709
        if (!BN_from_montgomery(rr,r,mont,ctx)) goto err;
 
710
        ret=1;
 
711
err:
 
712
        if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
 
713
        if (powerbuf!=NULL)
 
714
                {
 
715
                OPENSSL_cleanse(powerbuf,powerbufLen);
 
716
                OPENSSL_free(powerbufFree);
 
717
                }
 
718
        if (am!=NULL) BN_clear(am);
 
719
        if (computeTemp!=NULL) BN_clear(computeTemp);
 
720
        BN_CTX_end(ctx);
493
721
        return(ret);
494
722
        }
495
723
 
517
745
#define BN_TO_MONTGOMERY_WORD(r, w, mont) \
518
746
                (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))
519
747
 
 
748
        if (BN_get_flags(p, BN_FLG_EXP_CONSTTIME) != 0)
 
749
                {
 
750
                /* BN_FLG_EXP_CONSTTIME only supported by BN_mod_exp_mont() */
 
751
                BNerr(BN_F_BN_MOD_EXP_MONT_WORD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
 
752
                return -1;
 
753
                }
 
754
 
520
755
        bn_check_top(p);
521
756
        bn_check_top(m);
522
757
 
523
 
        if (m->top == 0 || !(m->d[0] & 1))
 
758
        if (!BN_is_odd(m))
524
759
                {
525
760
                BNerr(BN_F_BN_MOD_EXP_MONT_WORD,BN_R_CALLED_WITH_EVEN_MODULUS);
526
761
                return(0);
536
771
                }
537
772
        if (a == 0)
538
773
                {
539
 
                ret = BN_zero(rr);
 
774
                BN_zero(rr);
 
775
                ret = 1;
540
776
                return ret;
541
777
                }
542
778
 
630
866
err:
631
867
        if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
632
868
        BN_CTX_end(ctx);
 
869
        bn_check_top(rr);
633
870
        return(ret);
634
871
        }
635
872
 
636
873
 
637
874
/* The old fallback, simple version :-) */
638
 
int BN_mod_exp_simple(BIGNUM *r,
639
 
        const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
640
 
        BN_CTX *ctx)
 
875
int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
 
876
                const BIGNUM *m, BN_CTX *ctx)
641
877
        {
642
 
        int i,j,bits,ret=0,wstart,wend,window,wvalue,ts=0;
 
878
        int i,j,bits,ret=0,wstart,wend,window,wvalue;
643
879
        int start=1;
644
880
        BIGNUM *d;
645
 
        BIGNUM val[TABLE_SIZE];
 
881
        /* Table of variables obtained from 'ctx' */
 
882
        BIGNUM *val[TABLE_SIZE];
 
883
 
 
884
        if (BN_get_flags(p, BN_FLG_EXP_CONSTTIME) != 0)
 
885
                {
 
886
                /* BN_FLG_EXP_CONSTTIME only supported by BN_mod_exp_mont() */
 
887
                BNerr(BN_F_BN_MOD_EXP_SIMPLE,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
 
888
                return -1;
 
889
                }
646
890
 
647
891
        bits=BN_num_bits(p);
648
892
 
653
897
                }
654
898
 
655
899
        BN_CTX_start(ctx);
656
 
        if ((d = BN_CTX_get(ctx)) == NULL) goto err;
 
900
        d = BN_CTX_get(ctx);
 
901
        val[0] = BN_CTX_get(ctx);
 
902
        if(!d || !val[0]) goto err;
657
903
 
658
 
        BN_init(&(val[0]));
659
 
        ts=1;
660
 
        if (!BN_nnmod(&(val[0]),a,m,ctx)) goto err;             /* 1 */
661
 
        if (BN_is_zero(&(val[0])))
 
904
        if (!BN_nnmod(val[0],a,m,ctx)) goto err;                /* 1 */
 
905
        if (BN_is_zero(val[0]))
662
906
                {
663
 
                ret = BN_zero(r);
 
907
                BN_zero(r);
 
908
                ret = 1;
664
909
                goto err;
665
910
                }
666
911
 
667
912
        window = BN_window_bits_for_exponent_size(bits);
668
913
        if (window > 1)
669
914
                {
670
 
                if (!BN_mod_mul(d,&(val[0]),&(val[0]),m,ctx))
 
915
                if (!BN_mod_mul(d,val[0],val[0],m,ctx))
671
916
                        goto err;                               /* 2 */
672
917
                j=1<<(window-1);
673
918
                for (i=1; i<j; i++)
674
919
                        {
675
 
                        BN_init(&(val[i]));
676
 
                        if (!BN_mod_mul(&(val[i]),&(val[i-1]),d,m,ctx))
 
920
                        if(((val[i] = BN_CTX_get(ctx)) == NULL) ||
 
921
                                        !BN_mod_mul(val[i],val[i-1],d,m,ctx))
677
922
                                goto err;
678
923
                        }
679
 
                ts=i;
680
924
                }
681
925
 
682
926
        start=1;        /* This is used to avoid multiplication etc
728
972
                                }
729
973
                
730
974
                /* wvalue will be an odd number < 2^window */
731
 
                if (!BN_mod_mul(r,r,&(val[wvalue>>1]),m,ctx))
 
975
                if (!BN_mod_mul(r,r,val[wvalue>>1],m,ctx))
732
976
                        goto err;
733
977
 
734
978
                /* move the 'window' down further */
740
984
        ret=1;
741
985
err:
742
986
        BN_CTX_end(ctx);
743
 
        for (i=0; i<ts; i++)
744
 
                BN_clear_free(&(val[i]));
 
987
        bn_check_top(r);
745
988
        return(ret);
746
989
        }
747
990