def test_hide_mean(self): for l in range(0, 40): n = 100 # works with 10000 but takes time sum = [0 for _ in range(n)] for _ in range(n): rs = _random_bytes(l) assert len(rs) == l for (i, r) in enumerate(rs): sum[i] += r for i in range(l): mean = sum[i] / (127.5 * n) assert abs(mean - 1) < 3.3 / sqrt(n), "length %d sum %d for %d samples, norm to %f" % (l, sum[i], n, mean)
def test_top_cipher_new_salt(self): cs = create_cipher_storage("pw") _salt = bytes(_random_bytes(SALT_LEN[LATEST] // 8)) curr_time = datetime.datetime.now() cs.get_cipher(_salt) time_diff_1 = datetime.datetime.now() - curr_time curr_time = datetime.datetime.now() cs.get_cipher(_salt) time_diff_2 = datetime.datetime.now() - curr_time print time_diff_1 print time_diff_2 self.assertTrue(time_diff_1 > time_diff_2 * 100, "Second access too slow.")
def test_hide_mean(self): for l in range(0, 40): n = 100 # works with 10000 but takes time sum = [0 for _ in range(n)] for _ in range(n): rs = _random_bytes(l) assert len(rs) == l for (i, r) in enumerate(rs): sum[i] += r for i in range(l): mean = sum[i] / (127.5 * n) assert abs(mean - 1) < 3.3 / sqrt( n), "length %d sum %d for %d samples, norm to %f" % ( l, sum[i], n, mean)
def test_hide_bits(self): # this fails about 1 in 256 times per test (at size 1 byte) # so we make sure no combination of (l, i, j) fails twice bad = [] for retry in range(8): for l in range(1, 40): rs = _random_bytes(l) h1 = _hide(rs) for i in range(l): for j in range(8): flipped = bytearray(rs) assert h1 == _hide(flipped) flipped[i] ^= 2 ** j h2 = _hide(flipped) if h1 == h2: state = (l, i, j) assert state not in bad, "%s %s / %s" % (state, hexlify(h1), hexlify(h2)) bad.append(state)
def test_hide_bits(self): # this fails about 1 in 256 times per test (at size 1 byte) # so we make sure no combination of (l, i, j) fails twice bad = [] for retry in range(8): for l in range(1, 40): rs = _random_bytes(l) h1 = _hide(rs) for i in range(l): for j in range(8): flipped = bytearray(rs) assert h1 == _hide(flipped) flipped[i] ^= 2**j h2 = _hide(flipped) if h1 == h2: state = (l, i, j) assert state not in bad, "%s %s / %s" % ( state, hexlify(h1), hexlify(h2)) bad.append(state)
def test_all_values(self): b = _random_bytes(255*10) assert reduce(lambda a, b: a and b, (n in b for n in range(256)), True) b = _random_bytes(255) assert not reduce(lambda a, b: a and b, (n in b for n in range(256)), True)
def test_bits(self): b = _random_bytes(100) # test will fail ~ 1 in 2^100/8 times assert len(b) == 100 assert 0 == reduce(lambda x, y: x & y, bytearray(b)), b assert 255 == reduce(lambda x, y: x | y, bytearray(b)), b
def test_prefix(self): salt = _random_bytes(SALT_LEN[LATEST]//8) ctr = Counter.new(HALF_BLOCK, prefix=salt[:HALF_BLOCK//8]) count = ctr() assert len(count) == AES.block_size, count