-
Notifications
You must be signed in to change notification settings - Fork 29
Add initial support for crypto callbacks. #114
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # test_cryptocb.py | ||
| # | ||
| # Copyright (C) 2026 wolfSSL Inc. | ||
| # | ||
| # This file is part of wolfSSL. (formerly known as CyaSSL) | ||
| # | ||
| # wolfSSL is free software; you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 2 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # wolfSSL is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program; if not, write to the Free Software | ||
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
|
|
||
| import pytest | ||
|
|
||
| from wolfcrypt._ffi import lib as _lib | ||
| from wolfcrypt.random import Random | ||
|
|
||
|
|
||
| if not _lib.CRYPTO_CB_ENABLED: | ||
| pytest.skip("Crypto Callbacks not supported", allow_module_level=True) | ||
|
|
||
| from wolfcrypt.cryptocb import CryptoCallback | ||
|
|
||
|
|
||
| def test_default_device_id(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 [Medium] Hash callbacks and error paths are untested; default-device-id test asserts nothing Only the RNG callback path is exercised. There is no coverage for Fix: Add tests for the hash update/finalize paths, the length-mismatch/error paths, and the unknown-algo fallback; make |
||
| print(f"Default device ID = {CryptoCallback.default_device_id()}") | ||
|
|
||
| class RngCryptoCallback(CryptoCallback): | ||
| def rng_callback(self, _device_id: int, _rng, size: int) -> bytes: | ||
| # Generate fake random data for testing purposes. | ||
| return bytes(range(1, 1 + size)) | ||
|
|
||
|
|
||
| def test_rng_callback(): | ||
| with RngCryptoCallback(10): | ||
| rng = Random(device_id=10) | ||
|
|
||
| random = rng.byte() | ||
| assert random == b"\01" | ||
|
|
||
| random = rng.bytes(1) | ||
| assert random == b"\01" | ||
|
|
||
| random = rng.bytes(3) | ||
| assert random == b"\01\02\03" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ | |
| __all__ = [ | ||
| "__title__", "__summary__", "__uri__", "__version__", "__wolfssl_version__", | ||
| "__author__", "__email__", "__license__", "__copyright__", | ||
| "ciphers", "hashes", "random", "pwdbased" | ||
| "ciphers", "hashes", "random", "pwdbased", "cryptocb" | ||
| ] | ||
|
|
||
| import os | ||
|
|
@@ -46,8 +46,22 @@ | |
| if top_level_py not in ["setup.py", "build_ffi.py"]: | ||
| from wolfcrypt._ffi import ffi as _ffi | ||
| from wolfcrypt._ffi import lib as _lib | ||
| if _lib.CRYPTO_CB_ENABLED: | ||
| from wolfcrypt.cryptocb import CryptoCallback | ||
| from wolfcrypt.exceptions import WolfCryptApiError | ||
|
|
||
| ret = _lib.wolfCrypt_Init() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 [Low] wolfCrypt_Init() added with no corresponding cleanup
Fix: Confirm the omission of |
||
| if ret < 0: | ||
| raise WolfCryptApiError("WolfCrypt_Init failed", ret) | ||
|
|
||
| if _lib.CRYPTO_CB_ENABLED: | ||
| @_ffi.def_extern() | ||
| def py_wc_crypto_callback(device_id: int, info: _ffi.CData, ctx: _ffi.CData) -> int: | ||
| if ctx == _ffi.NULL: | ||
| return _lib.CRYPTOCB_UNAVAILABLE | ||
| crypto_cb: CryptoCallback = _ffi.from_handle(ctx) | ||
| return crypto_cb.callback(device_id, info) | ||
|
|
||
| if hasattr(_lib, 'WC_RNG_SEED_CB_ENABLED'): | ||
| if _lib.WC_RNG_SEED_CB_ENABLED: | ||
| ret = _lib.wc_SetSeed_Cb(_ffi.addressof(_lib, "wc_GenerateSeed")) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 [Medium] CRYPTO_CB cdef hash union depends on SHA/SHA256/SHA384/SHA512/SHA3 cdefs being present
The
wc_CryptoInfo.hash.uunion added underif features["CRYPTO_CB"]references the C typeswc_Sha,wc_Sha256,wc_Sha384,wc_Sha512, andwc_Sha3*. Thosetypedef struct {...}declarations are emitted only under their own feature guards (if features["SHA"],["SHA256"], etc., lines 845-908). If a build enables CRYPTO_CB while any of those hash features is disabled, the cdef will reference an undeclared type and ffibuilder.cdef()/compile will fail. The default build enables all of them so it works today, but the implicit coupling is fragile and undocumented.Fix: Make the CRYPTO_CB hash-union cdef robust to disabled hash features, or document/assert the dependency so a non-default feature combination fails with a clear message instead of an opaque cdef error.