don't write beyond buffer
[openssl.git] / crypto / bn / bn_gf2m.c
index ed8e7043831bc8ae27e13b00eb4017ba2d8d7e38..dea1fd3b873c66d758aac81f2c9a7e88721eb503 100644 (file)
@@ -284,7 +284,7 @@ int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
        if (a->top < b->top) { at = b; bt = a; }
        else { at = a; bt = b; }
 
-       bn_expand2(r, at->top);
+       bn_wexpand(r, at->top);
 
        for (i = 0; i < bt->top; i++)
                {
@@ -316,10 +316,10 @@ int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[])
        int n, dN, d0, d1;
        BN_ULONG zz, *z;
        
-       /* Since the algorithm does reduction in place, if a == r, copy the
+       /* Since the algorithm does reduction in the r value, if a != r, copy the
         * contents of a into r so we can do reduction in r. 
         */
-       if ((a != NULL) && (a->d != r->d))
+       if (a != r)
                {
                if (!bn_wexpand(r, a->top)) return 0;
                for (j = 0; j < a->top; j++)
@@ -370,12 +370,16 @@ int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[])
 
                for (k = 1; p[k] > 0; k++)
                        {
+                       BN_ULONG tmp_ulong;
+
                        /* reducing component t^p[k]*/
                        n = p[k] / BN_BITS2;   
                        d0 = p[k] % BN_BITS2;
                        d1 = BN_BITS2 - d0;
                        z[n] ^= (zz << d0);
-                       if (d0) z[n+1] ^= (zz >> d1);
+                       tmp_ulong = zz >> d1;
+                        if (d0 && tmp_ulong)
+                                z[n+1] ^= tmp_ulong;
                        }
 
                
@@ -427,7 +431,7 @@ int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const unsig
        BN_CTX_start(ctx);
        if ((s = BN_CTX_get(ctx)) == NULL) goto err;
        
-       zlen = a->top + b->top;
+       zlen = a->top + b->top + 4;
        if (!bn_wexpand(s, zlen)) goto err;
        s->top = zlen;
 
@@ -613,7 +617,7 @@ int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const unsigned int p[], BN_
        }
 
 
-#ifdef OPENSSL_NO_SUN_DIV
+#ifndef OPENSSL_SUN_GF2M_DIV
 /* Divide y by x, reduce modulo p, and store the result in r. r could be x 
  * or y, x could equal y.
  */