s390x assembler pack: add s390x-gf2m.pl and harmonize AES_xts_[en|de]crypt.
[openssl.git] / crypto / bn / bn_sqrt.c
index 463d4a81394fb57a3db51e3bc5c2abd13e497fc9..6beaf9e5e5ddfd6da6942c67b045049a7c979ddb 100644 (file)
@@ -1,4 +1,4 @@
-/* crypto/bn/bn_mod.c */
+/* crypto/bn/bn_sqrt.c */
 /* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
  * and Bodo Moeller for the OpenSSL project. */
 /* ====================================================================
@@ -83,9 +83,11 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
                                goto end;
                        if (!BN_set_word(ret, BN_is_bit_set(a, 0)))
                                {
-                               BN_free(ret);
+                               if (ret != in)
+                                       BN_free(ret);
                                return NULL;
                                }
+                       bn_check_top(ret);
                        return ret;
                        }
 
@@ -101,9 +103,11 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
                        goto end;
                if (!BN_set_word(ret, BN_is_one(a)))
                        {
-                       BN_free(ret);
+                       if (ret != in)
+                               BN_free(ret);
                        return NULL;
                        }
+               bn_check_top(ret);
                return ret;
                }
 
@@ -286,7 +290,7 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
                if (BN_is_zero(t))
                        {
                        /* special case: a == 0  (mod p) */
-                       if (!BN_zero(ret)) goto end;
+                       BN_zero(ret);
                        err = 0;
                        goto end;
                        }
@@ -299,7 +303,7 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
                if (BN_is_zero(x))
                        {
                        /* special case: a == 0  (mod p) */
-                       if (!BN_zero(ret)) goto end;
+                       BN_zero(ret);
                        err = 0;
                        goto end;
                        }
@@ -384,5 +388,6 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
                ret = NULL;
                }
        BN_CTX_end(ctx);
+       bn_check_top(ret);
        return ret;
        }