Skip to content
Open
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
26 changes: 16 additions & 10 deletions src/nacl/bindings/crypto_generichash.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
from __future__ import annotations

import threading
from typing import TYPE_CHECKING, NoReturn

from nacl import exceptions as exc
Expand Down Expand Up @@ -156,13 +157,15 @@ class Blake2State:
Python-level wrapper for the crypto_generichash_blake2b state buffer
"""

__slots__ = ["_statebuf", "digest_size"]
__slots__ = ["_lock", "_statebuf", "digest_size"]

def __init__(self, digest_size: int):
self._statebuf = ffi.new(
"unsigned char[]", crypto_generichash_STATEBYTES
)
self.digest_size = digest_size
# libsodium runs without the GIL, so serialize access to the state
self._lock = threading.Lock()

def __reduce__(self) -> NoReturn:
"""
Expand All @@ -173,9 +176,10 @@ def __reduce__(self) -> NoReturn:

def copy(self) -> Self:
_st = self.__class__(self.digest_size)
ffi.memmove(
_st._statebuf, self._statebuf, crypto_generichash_STATEBYTES
)
with self._lock:
ffi.memmove(
_st._statebuf, self._statebuf, crypto_generichash_STATEBYTES
)
return _st


Expand Down Expand Up @@ -249,9 +253,10 @@ def generichash_blake2b_update(state: Blake2State, data: bytes) -> None:
raising=exc.TypeError,
)

rc = lib.crypto_generichash_blake2b_update(
state._statebuf, data, len(data)
)
with state._lock:
rc = lib.crypto_generichash_blake2b_update(
state._statebuf, data, len(data)
)
ensure(rc == 0, "Unexpected failure", raising=exc.RuntimeError)


Expand All @@ -272,9 +277,10 @@ def generichash_blake2b_final(state: Blake2State) -> bytes:
)

_digest = ffi.new("unsigned char[]", crypto_generichash_BYTES_MAX)
rc = lib.crypto_generichash_blake2b_final(
state._statebuf, _digest, state.digest_size
)
with state._lock:
rc = lib.crypto_generichash_blake2b_final(
state._statebuf, _digest, state.digest_size
)

ensure(rc == 0, "Unexpected failure", raising=exc.RuntimeError)
return ffi.buffer(_digest, state.digest_size)[:]
25 changes: 25 additions & 0 deletions tests/test_generichash.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

import binascii
import copy
import hashlib
import json
import os
import threading
from typing import AnyStr

import pytest
Expand Down Expand Up @@ -245,3 +247,26 @@ def test_blake2_digest_size_descriptor_coherence():
assert h.name == "blake2b"
assert h.block_size == 128
assert h.digest_size == 64


def test_hashlib_blake2_concurrent_update():
# libsodium is called with the GIL released, so concurrent updates on a
# shared state must be serialized by the binding.
chunk = b"\xaa" * 100003
nthreads, nupdates = 8, 20
h = nacl.hashlib.blake2b()
barrier = threading.Barrier(nthreads)

def worker() -> None:
barrier.wait()
for _ in range(nupdates):
h.update(chunk)

threads = [threading.Thread(target=worker) for _ in range(nthreads)]
for t in threads:
t.start()
for t in threads:
t.join()

expected = hashlib.blake2b(chunk * nthreads * nupdates, digest_size=32)
assert h.digest() == expected.digest()
Loading