Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster scrypt in testing #610

Merged
merged 1 commit into from
Sep 20, 2024
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
16 changes: 6 additions & 10 deletions hushline/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

# https://cryptography.io/en/latest/hazmat/primitives/key-derivation-functions/#scrypt
SCRYPT_LENGTH = 32 # The desired length of the derived key in bytes.
SCRYPT_N = 2**14 # CPU/Memory cost parameter. It must be larger than 1 and be a power of 2.
SCRYPT_R = 8 # Block size parameter.
SCRYPT_P = 1 # Parallelization parameter.
_SCRYPT_PARAMS = {
"n": 2**14, # CPU/Memory cost parameter. It must be larger than 1 and be a power of 2.
"r": 8, # Block size parameter.
"p": 1, # Parallelization parameter.
}


def generate_salt() -> str:
Expand Down Expand Up @@ -41,13 +43,7 @@ def get_encryption_key(scope: bytes | str | None = None, salt: str | None = None
salt_bytes = urlsafe_b64decode(salt)

# Use Scrypt to derive a unique encryption key based on the scope
kdf = Scrypt(
salt=salt_bytes,
length=SCRYPT_LENGTH,
n=SCRYPT_N,
r=SCRYPT_R,
p=SCRYPT_P,
)
kdf = Scrypt(salt=salt_bytes, length=SCRYPT_LENGTH, **_SCRYPT_PARAMS)

# Concatenate the encryption key with the scope
items = (encryption_key_bytes, scope_bytes)
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import pytest
from flask import Flask
from flask.testing import FlaskClient
from pytest_mock import MockFixture
from sqlalchemy import create_engine, text
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm import Session, sessionmaker

from hushline import create_app
from hushline.crypto import _SCRYPT_PARAMS
from hushline.db import db
from hushline.model import User, Username

Expand Down Expand Up @@ -103,6 +105,11 @@ def database(_db_template: None) -> str:
return db_name


@pytest.fixture(autouse=True)
def _insecure_scrypt_params(mocker: MockFixture) -> None:
mocker.patch.dict(_SCRYPT_PARAMS, {"n": 2, "r": 1, "p": 1}, clear=True)


@pytest.fixture()
def app(database: str) -> Generator[Flask, None, None]:
os.environ["REGISTRATION_CODES_REQUIRED"] = "False"
Expand Down
Loading