Esempio n. 1
0
    def test_backend(self):
        """backend management"""
        # clobber backend
        scrypt_mod.backend = None
        scrypt_mod._scrypt = None
        self.assertRaises(TypeError, scrypt_mod.scrypt, 's', 's', 2, 2, 2, 16)

        # reload backend
        scrypt_mod._set_backend(self.backend)
        self.assertEqual(scrypt_mod.backend, self.backend)
        scrypt_mod.scrypt('s', 's', 2, 2, 2, 16)

        # throw error for unknown backend
        self.assertRaises(ValueError, scrypt_mod._set_backend, 'xxx')
        self.assertEqual(scrypt_mod.backend, self.backend)
Esempio n. 2
0
    def test_backend(self):
        """backend management"""
        # clobber backend
        scrypt_mod.backend = None
        scrypt_mod._scrypt = None
        self.assertRaises(TypeError, scrypt_mod.scrypt, 's', 's', 2, 2, 2, 16)

        # reload backend
        scrypt_mod._set_backend(self.backend)
        self.assertEqual(scrypt_mod.backend, self.backend)
        scrypt_mod.scrypt('s', 's', 2, 2, 2, 16)

        # throw error for unknown backend
        self.assertRaises(ValueError, scrypt_mod._set_backend, 'xxx')
        self.assertEqual(scrypt_mod.backend, self.backend)
Esempio n. 3
0
 def _calc_checksum(self, secret):
     secret = to_bytes(secret, param="secret")
     return _scrypt.scrypt(secret,
                           self.salt,
                           n=(1 << self.rounds),
                           r=self.block_size,
                           p=self.parallelism,
                           keylen=self.checksum_size)
Esempio n. 4
0
 def test_reference_vectors(self):
     """reference vectors"""
     for secret, salt, n, r, p, keylen, result in self.reference_vectors:
         if n >= 1024 and TEST_MODE(max="default"):
             # skip large values unless we're running full test suite
             continue
         if n > 16384 and self.backend == "builtin":
             # skip largest vector for builtin, takes WAAY too long
             # (46s under pypy, ~5m under cpython)
             continue
         log.debug("scrypt reference vector: %r %r n=%r r=%r p=%r", secret, salt, n, r, p)
         self.assertEqual(scrypt_mod.scrypt(secret, salt, n, r, p, keylen), result)
Esempio n. 5
0
 def test_reference_vectors(self):
     """reference vectors"""
     for secret, salt, n, r, p, keylen, result in self.reference_vectors:
         if n >= 1024 and TEST_MODE(max="default"):
             # skip large values unless we're running full test suite
             continue
         if n > 16384 and self.backend == "builtin":
             # skip largest vector for builtin, takes WAAY too long
             # (46s under pypy, ~5m under cpython)
             continue
         log.debug("scrypt reference vector: %r %r n=%r r=%r p=%r", secret, salt, n, r, p)
         self.assertEqual(scrypt_mod.scrypt(secret, salt, n, r, p, keylen), result)
    def test_other_backends(self):
        """compare output to other backends"""
        # only run once, since test is symetric.
        # maybe this means it should go somewhere else?
        if self._already_tested_others:
            raise self.skipTest("already run under %r backend test" %
                                self._already_tested_others)
        self._already_tested_others = self.backend
        rng = self.getRandom()

        # get available backends
        orig = scrypt_mod.backend
        available = set(name for name in scrypt_mod.backend_values
                        if scrypt_mod._has_backend(name))
        scrypt_mod._set_backend(orig)
        available.discard(self.backend)
        if not available:
            raise self.skipTest("no other backends found")

        warnings.filterwarnings(
            "ignore",
            "(?i)using builtin scrypt backend",
            category=exc.PasslibSecurityWarning,
        )

        # generate some random options, and cross-check output
        for _ in range(10):
            # NOTE: keeping values low due to builtin test
            secret = getrandbytes(rng, rng.randint(0, 64))
            salt = getrandbytes(rng, rng.randint(0, 64))
            n = 1 << rng.randint(1, 10)
            r = rng.randint(1, 8)
            p = rng.randint(1, 3)
            ks = rng.randint(1, 64)
            previous = None
            backends = set()
            for name in available:
                scrypt_mod._set_backend(name)
                self.assertNotIn(scrypt_mod._scrypt, backends)
                backends.add(scrypt_mod._scrypt)
                result = hexstr(scrypt_mod.scrypt(secret, salt, n, r, p, ks))
                self.assertEqual(len(result), 2 * ks)
                if previous is not None:
                    self.assertEqual(
                        result,
                        previous,
                        msg="%r output differs from others %r: %r" %
                        (name, available, [secret, salt, n, r, p, ks]),
                    )
Esempio n. 7
0
    def test_other_backends(self):
        """compare output to other backends"""
        # only run once, since test is symetric.
        # maybe this means it should go somewhere else?
        if self._already_tested_others:
            raise self.skipTest("already run under %r backend test" % self._already_tested_others)
        self._already_tested_others = self.backend
        rng = self.getRandom()

        # get available backends
        orig = scrypt_mod.backend
        available = set(name for name in scrypt_mod.backend_values
                        if scrypt_mod._has_backend(name))
        scrypt_mod._set_backend(orig)
        available.discard(self.backend)
        if not available:
            raise self.skipTest("no other backends found")

        warnings.filterwarnings("ignore", "(?i)using builtin scrypt backend",
                                category=exc.PasslibSecurityWarning)

        # generate some random options, and cross-check output
        for _ in range(10):
            # NOTE: keeping values low due to builtin test
            secret = getrandbytes(rng, rng.randint(0, 64))
            salt = getrandbytes(rng, rng.randint(0, 64))
            n = 1 << rng.randint(1, 10)
            r = rng.randint(1, 8)
            p = rng.randint(1, 3)
            ks = rng.randint(1, 64)
            previous = None
            backends = set()
            for name in available:
                scrypt_mod._set_backend(name)
                self.assertNotIn(scrypt_mod._scrypt, backends)
                backends.add(scrypt_mod._scrypt)
                result = hexstr(scrypt_mod.scrypt(secret, salt, n, r, p, ks))
                self.assertEqual(len(result), 2*ks)
                if previous is not None:
                    self.assertEqual(result, previous,
                                     msg="%r output differs from others %r: %r" %
                                         (name, available, [secret, salt, n, r, p, ks]))
Esempio n. 8
0
 def run_scrypt(keylen):
     return hexstr(scrypt_mod.scrypt("secret", "salt", 2, 2, 2, keylen))
Esempio n. 9
0
 def run_scrypt(p, n=2, r=2):
     return hexstr(scrypt_mod.scrypt("secret", "salt", n, r, p, 16))
Esempio n. 10
0
 def run_scrypt(n):
     return hexstr(scrypt_mod.scrypt("secret", "salt", n, 2, 2, 16))
Esempio n. 11
0
 def run_scrypt(salt):
     return hexstr(scrypt_mod.scrypt("secret", salt, 2, 2, 2, 16))
Esempio n. 12
0
 def run_scrypt(secret):
     return hexstr(scrypt_mod.scrypt(secret, "salt", 2, 2, 2, 16))
Esempio n. 13
0
 def run_scrypt(secret):
     return hexstr(scrypt_mod.scrypt(secret, "salt", 2, 2, 2, 16))
Esempio n. 14
0
 def run_scrypt(keylen):
     return hexstr(scrypt_mod.scrypt("secret", "salt", 2, 2, 2, keylen))
Esempio n. 15
0
 def run_scrypt(p, n=2, r=2):
     return hexstr(scrypt_mod.scrypt("secret", "salt", n, r, p, 16))
Esempio n. 16
0
 def run_scrypt(n):
     return hexstr(scrypt_mod.scrypt("secret", "salt", n, 2, 2, 16))
Esempio n. 17
0
 def run_scrypt(salt):
     return hexstr(scrypt_mod.scrypt("secret", salt, 2, 2, 2, 16))