Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ def __init__(self, *args, **kwargs):
algorithms.add(algorithm.lower())

_blake2 = self._conditional_import_module('_blake2')
blake2_hashes = {'blake2b', 'blake2s'}
if _blake2:
algorithms.update({'blake2b', 'blake2s'})
algorithms.update(blake2_hashes)
else:
algorithms.difference_update(blake2_hashes)

self.constructors_to_test = {}
for algorithm in algorithms:
Expand Down Expand Up @@ -220,7 +223,12 @@ def test_algorithms_available(self):
# all available algorithms must be loadable, bpo-47101
self.assertNotIn("undefined", hashlib.algorithms_available)
for name in hashlib.algorithms_available:
digest = hashlib.new(name, usedforsecurity=False)
with self.subTest(name):
try:
_ = hashlib.new(name, usedforsecurity=False)
except ValueError as exc:
self.skip_if_blake2_not_builtin(name, exc)
raise

def test_usedforsecurity_true(self):
hashlib.new("sha256", usedforsecurity=True)
Expand Down Expand Up @@ -444,6 +452,7 @@ def test_sha3_256_update_over_4gb(self):
self.assertEqual(h.hexdigest(), "e2d4535e3b613135c14f2fe4e026d7ad8d569db44901740beffa30d430acb038")

@requires_resource('cpu')
@requires_blake2
def test_blake2_update_over_4gb(self):
# blake2s or blake2b doesn't matter based on how our C code is structured, this tests the
# common loop macro logic.
Expand Down Expand Up @@ -734,6 +743,12 @@ def test_case_sha512_3(self):
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")

def skip_if_blake2_not_builtin(self, name, skip_reason):
# blake2 builtins may be absent if python built with
# a subset of --with-builtin-hashlib-hashes or none.
if "blake2" in name and "blake2" not in builtin_hashes:
self.skipTest(skip_reason)

def check_blake2(self, constructor, salt_size, person_size, key_size,
digest_size, max_offset):
self.assertEqual(constructor.SALT_SIZE, salt_size)
Expand Down
27 changes: 27 additions & 0 deletions crates/stdlib/src/blake2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ mod _blake2 {
use crate::hashlib::_hashlib::{BlakeHashArgs, local_blake2b, local_blake2s};
use crate::vm::{PyPayload, PyResult, VirtualMachine};

#[pyattr(name = "_GIL_MINSIZE")]
const GIL_MINSIZE: u16 = 2048;

#[pyattr]
const BLAKE2B_SALT_SIZE: u8 = 16;

#[pyattr]
const BLAKE2B_PERSON_SIZE: u8 = 16;

#[pyattr]
const BLAKE2B_MAX_KEY_SIZE: u8 = 64;

#[pyattr]
const BLAKE2B_MAX_DIGEST_SIZE: u8 = 64;

#[pyattr]
const BLAKE2S_SALT_SIZE: u8 = 8;

#[pyattr]
const BLAKE2S_PERSON_SIZE: u8 = 8;

#[pyattr]
const BLAKE2S_MAX_KEY_SIZE: u8 = 32;

#[pyattr]
const BLAKE2S_MAX_DIGEST_SIZE: u8 = 32;

#[pyfunction]
fn blake2b(args: BlakeHashArgs, vm: &VirtualMachine) -> PyResult {
Ok(local_blake2b(args, vm)?.into_pyobject(vm))
Expand Down
Loading