Fallback to normal multiply if n2 == 8 and dna or dnb is not zero
[openssl.git] / crypto / bn / bn_mul.c
index eb5d5256137ea5861f3cf71d2f07ae83b75afbe9..7bffc9c16a515d87eb1fa79d84205753972e3b5e 100644 (file)
@@ -66,6 +66,7 @@
 #include "cryptlib.h"
 #include "bn_lcl.h"
 
+#if defined(OPENSSL_NO_ASM) || !(defined(__i386) || defined(__i386__))/* Assembler implementation exists only for x86 */
 /* Here follows specialised variants of bn_add_words() and
    bn_sub_words().  They have the property performing operations on
    arrays of different sizes.  The sizes of those arrays is expressed through
@@ -201,6 +202,7 @@ BN_ULONG bn_sub_part_words(BN_ULONG *r,
                }
        return c;
        }
+#endif
 
 BN_ULONG bn_add_part_words(BN_ULONG *r,
        const BN_ULONG *a, const BN_ULONG *b,
@@ -406,16 +408,22 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
                return;
                }
 #  endif
-       if (n2 == 8)
+       /* Only call bn_mul_comba 8 if n2 == 8 and the
+        * two arrays are complete [steve]
+        */
+       if (n2 == 8 && dna == 0 && dnb == 0)
                {
                bn_mul_comba8(r,a,b);
                return; 
                }
 # endif /* BN_MUL_COMBA */
+       /* Else do normal multiply */
        if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)
                {
-               /* This should not happen */
-               bn_mul_normal(r,a,n2,b,n2);
+               bn_mul_normal(r,a,n2+dna,b,n2+dnb);
+               if ((dna + dnb) < 0)
+                       memset(&r[2*n2 + dna + dnb], 0,
+                               sizeof(BN_ULONG) * -(dna + dnb));
                return;
                }
        /* r=(a[0]-a[1])*(b[1]-b[0]) */
@@ -456,7 +464,8 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
                }
 
 # ifdef BN_MUL_COMBA
-       if (n == 4)
+       if (n == 4 && dna == 0 && dnb == 0) /* XXX: bn_mul_comba4 could take
+                                              extra args to do this well */
                {
                if (!zero)
                        bn_mul_comba4(&(t[n2]),t,&(t[n]));
@@ -466,7 +475,9 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
                bn_mul_comba4(r,a,b);
                bn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));
                }
-       else if (n == 8)
+       else if (n == 8 && dna == 0 && dnb == 0) /* XXX: bn_mul_comba8 could
+                                                   take extra args to do this
+                                                   well */
                {
                if (!zero)
                        bn_mul_comba8(&(t[n2]),t,&(t[n]));
@@ -936,8 +947,8 @@ int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
        int i;
 #endif
 #ifdef BN_RECURSION
-       BIGNUM *t;
-       int j,k;
+       BIGNUM *t=NULL;
+       int j=0,k;
 #endif
 
 #ifdef BN_COUNT
@@ -1105,7 +1116,13 @@ void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
 
                }
        rr= &(r[na]);
-       rr[0]=bn_mul_words(r,a,na,b[0]);
+       if (nb <= 0)
+               {
+               (void)bn_mul_words(r,a,na,0);
+               return;
+               }
+       else
+               rr[0]=bn_mul_words(r,a,na,b[0]);
 
        for (;;)
                {