Clean-up GAS targets: get rid of "cpp" stuff and replace it with "purified"
[openssl.git] / crypto / bn / bn_kron.c
index aba48dda4a22a2f96ba8878468522abaa3df9d6c..740359b7520de3609e825801d96a5f41df81ea98 100644 (file)
@@ -1,5 +1,3 @@
-/* totally untested */
-
 /* crypto/bn/bn_kron.c */
 /* ====================================================================
  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
@@ -55,9 +53,9 @@
  *
  */
 
+#include "cryptlib.h"
 #include "bn_lcl.h"
 
-
 /* least significant word */
 #define BN_lsw(n) (((n)->top == 0) ? (BN_ULONG) 0 : (n)->d[0])
 
@@ -65,7 +63,7 @@
 int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
        {
        int i;
-       int ret;
+       int ret = -2; /* avoid 'uninitialized' warning */
        int err = 0;
        BIGNUM *A, *B, *tmp;
        /* In 'tab', only odd-indexed entries are relevant:
@@ -76,6 +74,9 @@ int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
         */
        static const int tab[8] = {0, 1, 0, -1, 0, -1, 0, 1};
 
+       bn_check_top(a);
+       bn_check_top(b);
+
        BN_CTX_start(ctx);
        A = BN_CTX_get(ctx);
        B = BN_CTX_get(ctx);
@@ -146,7 +147,7 @@ int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
 
                if (BN_is_zero(A))
                        {
-                       ret = BN_is_one(B);
+                       ret = BN_is_one(B) ? ret : 0;
                        goto end;
                        }
 
@@ -165,7 +166,7 @@ int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
        
                /* Cohen's step 4: */
                /* multiply 'ret' by  $(-1)^{(A-1)(B-1)/4}$ */
-               if (BN_lsw(A) & BN_lsw(B) & 2)
+               if ((A->neg ? ~BN_lsw(A) : BN_lsw(A)) & BN_lsw(B) & 2)
                        ret = -ret;
                
                /* (A, B) := (B mod |A|, |A|) */
@@ -174,8 +175,7 @@ int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
                tmp = A; A = B; B = tmp;
                tmp->neg = 0;
                }
-       
- end:
+end:
        BN_CTX_end(ctx);
        if (err)
                return -2;