summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_sqr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn/bn_sqr.c')
-rw-r--r--src/lib/libcrypto/bn/bn_sqr.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/lib/libcrypto/bn/bn_sqr.c b/src/lib/libcrypto/bn/bn_sqr.c
index c1d0cca438..270d0cd348 100644
--- a/src/lib/libcrypto/bn/bn_sqr.c
+++ b/src/lib/libcrypto/bn/bn_sqr.c
@@ -77,16 +77,16 @@ int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
77 if (al <= 0) 77 if (al <= 0)
78 { 78 {
79 r->top=0; 79 r->top=0;
80 return(1); 80 return 1;
81 } 81 }
82 82
83 BN_CTX_start(ctx); 83 BN_CTX_start(ctx);
84 rr=(a != r) ? r : BN_CTX_get(ctx); 84 rr=(a != r) ? r : BN_CTX_get(ctx);
85 tmp=BN_CTX_get(ctx); 85 tmp=BN_CTX_get(ctx);
86 if (tmp == NULL) goto err; 86 if (!rr || !tmp) goto err;
87 87
88 max=(al+al); 88 max = 2 * al; /* Non-zero (from above) */
89 if (bn_wexpand(rr,max+1) == NULL) goto err; 89 if (bn_wexpand(rr,max) == NULL) goto err;
90 90
91 if (al == 4) 91 if (al == 4)
92 { 92 {
@@ -138,12 +138,18 @@ int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
138#endif 138#endif
139 } 139 }
140 140
141 rr->top=max;
142 rr->neg=0; 141 rr->neg=0;
143 if ((max > 0) && (rr->d[max-1] == 0)) rr->top--; 142 /* If the most-significant half of the top word of 'a' is zero, then
143 * the square of 'a' will max-1 words. */
144 if(a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))
145 rr->top = max - 1;
146 else
147 rr->top = max;
144 if (rr != r) BN_copy(r,rr); 148 if (rr != r) BN_copy(r,rr);
145 ret = 1; 149 ret = 1;
146 err: 150 err:
151 bn_check_top(rr);
152 bn_check_top(tmp);
147 BN_CTX_end(ctx); 153 BN_CTX_end(ctx);
148 return(ret); 154 return(ret);
149 } 155 }