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
31 changes: 29 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ dev = [
"mypy",
"pytest",
"pytest-cov",
"ruff",
# ruff preview rules/behaviour (used for linting) can change in any release
"ruff==0.17.4",
"sphinx",
"sphinx-rtd-theme",
"ty",
Expand Down Expand Up @@ -89,9 +90,33 @@ indent-width = 4
target-version = "py310"

[tool.ruff.lint]
select = ["E", "F", "B", "UP", "ANN", "C4", "DTZ", "EXE", "FA", "INT", "ISC", "ICN", "LOG", "G", "RSE", "SLOT", "TID", "TC", "FLY", "PERF", "W", "FURB"]
select = [
"E", "F", "B", "UP", "ANN", "C4", "DTZ", "EXE", "FA", "INT", "ISC", "ICN",
"LOG", "G", "RSE", "SLOT", "TID", "TC", "FLY", "PERF", "W", "FURB",
# pycodestyle whitespace/blank-line checks.
# These are listed by exact code because of explicit-preview-rules below.
"E111", "E112", "E113", "E114", "E115", "E116", "E117",
"E201", "E202", "E203", "E204",
"E211",
"E221", "E222", "E223", "E224", "E225", "E227", "E228",
"E231",
"E241", "E242",
"E251", "E252",
"E261", "E262", "E265", "E266",
"E271", "E272", "E273", "E274", "E275",
"E301", "E302", "E303", "E304", "E305", "E306",
"W391",
]
# E501: line too long
ignore = ["E501"]

# Preview is needed for the pycodestyle rules selected above.
# explicit-preview-rules limits new preview rules to the exact codes listed, but
# preview mode still changes the behaviour of some stable rules everywhere. Ruff
# is pinned in the dev group for this reason.
preview = true
explicit-preview-rules = true

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []
Expand All @@ -102,6 +127,8 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
[tool.ruff.lint.per-file-ignores]
"scripts/*.py" = ["ANN"]
"tests/*.py" = ["ANN"]
# Sphinx's generated conf.py keeps its commented-out defaults as "#name = ...".
"docs/conf.py" = ["E265"]

[tool.ruff.format]
# Like Black, use double quotes for strings.
Expand Down
17 changes: 13 additions & 4 deletions scripts/build_ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
import shutil
from wolfcrypt._version import __wolfssl_version__ as version


def local_path(path):
""" Return path relative to the root of this project
"""
current = os.path.abspath(os.getcwd())
return os.path.abspath(os.path.join(current, path))


WOLFSSL_SRC_PATH = local_path("lib/wolfssl")


def wolfssl_inc_dirs(local_wolfssl=None, fips=False):
"""Returns the wolfSSL include directories needed to build the CFFI.
"""
Expand Down Expand Up @@ -66,6 +69,7 @@ def wolfssl_inc_dirs(local_wolfssl=None, fips=False):

return include_paths


def wolfssl_lib_dir(local_wolfssl=None, fips=False):
"""Returns the directory containg the wolfSSL library.
"""
Expand Down Expand Up @@ -100,6 +104,7 @@ def wolfssl_lib_dir(local_wolfssl=None, fips=False):

return lib_dir


def call(cmd):
print(f"Calling: '{cmd}' from working directory {os.getcwd()}")

Expand All @@ -108,6 +113,7 @@ def call(cmd):
subprocess.check_call(cmd, shell=True, env=os.environ)
os.environ["PATH"] = old_env


@contextmanager
def chdir(new_path, mkdir=False):
old_path = os.getcwd()
Expand Down Expand Up @@ -284,6 +290,7 @@ def make(configure_flags, fips=False):
call("make")
call("make install")


def get_libwolfssl():
if sys.platform == "win32":
libwolfssl_path = os.path.join(wolfssl_lib_dir(), "wolfssl.lib")
Expand All @@ -302,11 +309,13 @@ def get_libwolfssl():
else:
return True


def generate_libwolfssl(fips):
ensure_wolfssl_src(version)
prefix = os.path.join(WOLFSSL_SRC_PATH, get_platform(), version)
make(make_flags(prefix, fips))


def get_features(local_wolfssl, features):
fips = False
fips_file = None
Expand Down Expand Up @@ -395,7 +404,7 @@ def get_features(local_wolfssl, features):
]
have_mldsa_no_context_support = re.search(r'#define\s+(' + '|'.join(mldsa_no_context_defines) + r')\s+', '\n'.join(defines))
features["ML_DSA_NO_CTX"] = 1 if have_mldsa_no_context_support else 0
features["ML_KEM"] = 1 if '#define WOLFSSL_HAVE_MLKEM' in defines else 0
features["ML_KEM"] = 1 if '#define WOLFSSL_HAVE_MLKEM' in defines else 0
features["HKDF"] = 1 if "#define HAVE_HKDF" in defines else 0
# Unlike the other fatures, HASHDRBG is enabled by default in random.h, unless WC_NO_HASHDRBG or
# CUSTOM_RAND_GENERATE_BLOCK is defined.
Expand Down Expand Up @@ -428,6 +437,7 @@ def get_features(local_wolfssl, features):

return features


def build_ffi(local_wolfssl, features):
cffi_include_dirs = wolfssl_inc_dirs(local_wolfssl, features["FIPS"])
cffi_libraries = []
Expand Down Expand Up @@ -540,7 +550,7 @@ def build_ffi(local_wolfssl, features):
int HMAC_COPY_ENABLED = {features["HMAC_COPY"]};
"""

ffibuilder.set_source( "wolfcrypt._ffi", init_source_string,
ffibuilder.set_source("wolfcrypt._ffi", init_source_string,
include_dirs=cffi_include_dirs,
library_dirs=[wolfssl_lib_dir(local_wolfssl, features["FIPS"])],
libraries=cffi_libraries)
Expand Down Expand Up @@ -1186,7 +1196,6 @@ def build_ffi(local_wolfssl, features):
int wc_ecc_set_rng(ecc_key* key, WC_RNG* rng);
"""


if features["ED25519"]:
cdef += """
typedef struct {...; } ed25519_key;
Expand Down Expand Up @@ -1418,7 +1427,6 @@ def build_ffi(local_wolfssl, features):
static const int WC_HASH_TYPE_SHA3_512;
"""


cdef += """
typedef struct {
int algo_type; /* enum wc_AlgoType */
Expand Down Expand Up @@ -1544,6 +1552,7 @@ def build_ffi(local_wolfssl, features):

ffibuilder.cdef(cdef)


def main(ffibuilder):
# Default features.
features = {
Expand Down
4 changes: 4 additions & 0 deletions tests/test_asn.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

certs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "certs")


@pytest.fixture
def pem_der_conversion_vectors():
TestVector = namedtuple("TestVector", "pem der type")
Expand All @@ -61,6 +62,7 @@ def pem_der_conversion_vectors():

return vectors


@pytest.fixture
def signature_vectors():
TestVector = namedtuple("TestVector", """data signature hash_cls pub_key
Expand Down Expand Up @@ -97,6 +99,7 @@ def signature_vectors():

return vectors


def test_pem_der_conversion(pem_der_conversion_vectors):
for vector in pem_der_conversion_vectors:
computed_der = pem_to_der(vector.pem, vector.type)
Expand All @@ -105,6 +108,7 @@ def test_pem_der_conversion(pem_der_conversion_vectors):
computed_pem = der_to_pem(vector.der, vector.type)
assert computed_pem == vector.pem


def test_signature(signature_vectors):
for vector in signature_vectors:
assert make_signature(vector.data, vector.hash_cls, vector.priv_key) == vector.signature
Expand Down
Loading
Loading