From 3e0d47a268fab8b34dbfc9228ba188713d48e298 Mon Sep 17 00:00:00 2001 From: hanez Date: Thu, 19 Feb 2026 23:33:00 +0100 Subject: [PATCH] Added LibreSSL as an alternative to OpenSSL. Needs more testing! (0.38.16) --- CMakeLists.txt | 2 +- cmake/Extensions/Extensions.cmake | 2 + cmake/Extensions/LIBRESSL.cmake | 100 ++++++++++++++ cmake/Targets.cmake | 9 +- docs/README.md | 8 +- docs/build.md | 5 +- docs/external/README.md | 6 +- docs/external/curl.md | 6 +- docs/external/ini.md | 6 +- docs/external/json.md | 6 +- docs/external/libressl.md | 45 +++++++ docs/external/libsql.md | 6 +- docs/external/notcurses.md | 6 +- docs/external/openssl.md | 19 ++- docs/external/pcre2.md | 6 +- docs/external/pcsc.md | 6 +- docs/external/sqlite.md | 6 +- docs/external/tcltk.md | 9 +- docs/external/xml2.md | 6 +- docs/internals.md | 4 +- examples/crypto/libressl_md5.fun | 28 ++++ examples/crypto/libressl_ripemd160.fun | 28 ++++ examples/crypto/libressl_sha256.fun | 28 ++++ examples/crypto/libressl_sha512.fun | 28 ++++ make | 1 + src/bytecode.c | 4 + src/bytecode.h | 6 + src/external/libressl.c | 172 +++++++++++++++++++++++++ src/external/openssl.c | 5 + src/parser.c | 33 +++++ src/vm.c | 11 ++ src/vm/libressl/md5.c | 27 ++++ src/vm/libressl/ripemd160.c | 27 ++++ src/vm/libressl/sha256.c | 27 ++++ src/vm/libressl/sha512.c | 27 ++++ 35 files changed, 677 insertions(+), 38 deletions(-) create mode 100644 cmake/Extensions/LIBRESSL.cmake create mode 100644 docs/external/libressl.md create mode 100644 examples/crypto/libressl_md5.fun create mode 100644 examples/crypto/libressl_ripemd160.fun create mode 100644 examples/crypto/libressl_sha256.fun create mode 100644 examples/crypto/libressl_sha512.fun create mode 100644 src/external/libressl.c create mode 100644 src/vm/libressl/md5.c create mode 100644 src/vm/libressl/ripemd160.c create mode 100644 src/vm/libressl/sha256.c create mode 100644 src/vm/libressl/sha512.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e0e52fe..a935d11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(fun VERSION 0.38.15 LANGUAGES C) +project(fun VERSION 0.38.16 LANGUAGES C) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/cmake/Extensions/Extensions.cmake b/cmake/Extensions/Extensions.cmake index b16333b..92843b6 100644 --- a/cmake/Extensions/Extensions.cmake +++ b/cmake/Extensions/Extensions.cmake @@ -23,6 +23,7 @@ include(${CMAKE_SOURCE_DIR}/cmake/Extensions/CURL.cmake) include(${CMAKE_SOURCE_DIR}/cmake/Extensions/NOTCURSES.cmake) include(${CMAKE_SOURCE_DIR}/cmake/Extensions/REPL.cmake) include(${CMAKE_SOURCE_DIR}/cmake/Extensions/OPENSSL.cmake) +include(${CMAKE_SOURCE_DIR}/cmake/Extensions/LIBRESSL.cmake) # Summary of extension toggles message(STATUS "---- Fun extension summary ----") @@ -38,4 +39,5 @@ _fun_print_feature("libcurl (FUN_WITH_CURL)" FUN_WITH_CURL) _fun_print_feature("Notcurses (FUN_WITH_NOTCURSES)" FUN_WITH_NOTCURSES) _fun_print_feature("REPL (FUN_WITH_REPL)" FUN_WITH_REPL) _fun_print_feature("OpenSSL (FUN_WITH_OPENSSL)" FUN_WITH_OPENSSL) +_fun_print_feature("LibreSSL (FUN_WITH_LIBRESSL)" FUN_WITH_LIBRESSL) message(STATUS "--------------------------------") diff --git a/cmake/Extensions/LIBRESSL.cmake b/cmake/Extensions/LIBRESSL.cmake new file mode 100644 index 0000000..2e82f8d --- /dev/null +++ b/cmake/Extensions/LIBRESSL.cmake @@ -0,0 +1,100 @@ +## LibreSSL optional integration (MD5, etc.) + +option(FUN_WITH_LIBRESSL "Enable LibreSSL-based features (e.g., md5)" OFF) + +set(LIBRESSL_INCLUDE_DIRS "") +set(LIBRESSL_LINK_LIBS "") + +if(FUN_WITH_LIBRESSL) + # We must NOT depend on OpenSSL being installed. Detect LibreSSL directly. + # Strategy (in order): + # 1) Prefer pkg-config 'libressl' if available (brings ssl+crypto and headers) + # 2) Else, prefer explicit LibreSSL include prefix (/usr/include/libressl) + # 3) Else, fall back to generic OpenSSL-compatible headers, but only if they + # are provided by LibreSSL (detected heuristically via opensslv.h content) + # 4) Link against libcrypto (from LibreSSL). No OpenSSL::Crypto usage. + + include(CheckIncludeFile) + include(FindPkgConfig) + + set(_libressl_found FALSE) + + # 1) Try pkg-config: libressl (meta) or libtls (often implies LibreSSL presence) + if(PKG_CONFIG_FOUND) + pkg_check_modules(LIBRESSL_PKG QUIET libressl) + if(LIBRESSL_PKG_FOUND) + list(APPEND LIBRESSL_INCLUDE_DIRS ${LIBRESSL_PKG_INCLUDE_DIRS}) + list(APPEND LIBRESSL_LINK_LIBS ${LIBRESSL_PKG_LIBRARIES}) + set(_libressl_found TRUE) + else() + # Some distros provide only crypto/ssl pc files from LibreSSL + pkg_check_modules(LIBRESSL_CRYPTO QUIET libcrypto) + pkg_check_modules(LIBRESSL_SSL QUIET libssl) + if(LIBRESSL_CRYPTO_FOUND) + list(APPEND LIBRESSL_INCLUDE_DIRS ${LIBRESSL_CRYPTO_INCLUDE_DIRS}) + list(APPEND LIBRESSL_LINK_LIBS ${LIBRESSL_CRYPTO_LIBRARIES}) + if(LIBRESSL_SSL_FOUND) + list(APPEND LIBRESSL_INCLUDE_DIRS ${LIBRESSL_SSL_INCLUDE_DIRS}) + list(APPEND LIBRESSL_LINK_LIBS ${LIBRESSL_SSL_LIBRARIES}) + endif() + set(_libressl_found TRUE) + endif() + endif() + endif() + + # 2) Prefer explicit LibreSSL include prefix if present + if(EXISTS "/usr/include/libressl") + list(PREPEND LIBRESSL_INCLUDE_DIRS "/usr/include/libressl") + set(_libressl_found TRUE) + endif() + + # 3) Heuristic: check if the generic openssl headers are from LibreSSL + if(NOT _libressl_found) + # Try to find opensslv.h and see if it defines LIBRESSL_VERSION_NUMBER + find_path(_GEN_OPENSSL_INCLUDE_DIR openssl/opensslv.h + PATHS /usr/include /usr/local/include) + if(_GEN_OPENSSL_INCLUDE_DIR) + file(READ "${_GEN_OPENSSL_INCLUDE_DIR}/openssl/opensslv.h" _opensslv_h CONTENT_STRIP_TRAILING_WHITESPACE) + string(FIND "${_opensslv_h}" "LIBRESSL_VERSION_NUMBER" _idx) + if(NOT _idx EQUAL -1) + list(APPEND LIBRESSL_INCLUDE_DIRS "${_GEN_OPENSSL_INCLUDE_DIR}") + set(_libressl_found TRUE) + endif() + endif() + endif() + + # 4) Find the crypto library (from LibreSSL). Avoid CMake's OpenSSL package and target. + if(NOT LIBRESSL_LINK_LIBS) + # Try to locate libcrypto first + find_library(LIBRESSL_CRYPTO_LIB NAMES crypto libcrypto PATHS + /usr/lib /usr/local/lib /lib) + if(LIBRESSL_CRYPTO_LIB) + list(APPEND LIBRESSL_LINK_LIBS ${LIBRESSL_CRYPTO_LIB}) + endif() + endif() + + if(NOT _libressl_found OR NOT LIBRESSL_LINK_LIBS) + message(FATAL_ERROR "FUN_WITH_LIBRESSL=ON but LibreSSL headers and/or libcrypto not found.\n" + "Tried pkg-config (libressl/libcrypto), /usr/include/libressl, and generic openssl headers from LibreSSL.") + endif() + + # Ensure the compile definition is visible even if targets are created later + add_compile_definitions(FUN_WITH_LIBRESSL=1) + + # Propagate to main targets if they exist in this scope + if(TARGET fun_core) + target_link_libraries(fun_core PRIVATE ${LIBRESSL_LINK_LIBS}) + target_include_directories(fun_core PRIVATE ${LIBRESSL_INCLUDE_DIRS}) + target_compile_definitions(fun_core PRIVATE FUN_WITH_LIBRESSL=1) + endif() + if(TARGET fun) + target_link_libraries(fun PRIVATE ${LIBRESSL_LINK_LIBS}) + target_include_directories(fun PRIVATE ${LIBRESSL_INCLUDE_DIRS}) + target_compile_definitions(fun PRIVATE FUN_WITH_LIBRESSL=1) + endif() + if(TARGET fun_test) + target_link_libraries(fun_test PRIVATE ${LIBRESSL_LINK_LIBS}) + target_include_directories(fun_test PRIVATE ${LIBRESSL_INCLUDE_DIRS}) + target_compile_definitions(fun_test PRIVATE FUN_WITH_LIBRESSL=1) + endif() +endif() diff --git a/cmake/Targets.cmake b/cmake/Targets.cmake index bd6c48e..5ac9dbe 100644 --- a/cmake/Targets.cmake +++ b/cmake/Targets.cmake @@ -37,7 +37,8 @@ foreach(var_pair LIBXML2 TCL NOTCURSES - OPENSSL) + OPENSSL + LIBRESSL) if(${var_pair}_INCLUDE_DIRS) target_include_directories(fun_core PRIVATE ${${var_pair}_INCLUDE_DIRS}) endif() @@ -94,6 +95,12 @@ if(FUN_WITH_OPENSSL) target_compile_definitions(fun_core PUBLIC FUN_WITH_OPENSSL=1) endif() +# LibreSSL toggle: ensure compile definitions are applied to fun_core so +# the LibreSSL code paths are compiled, and linking vars are handled above. +if(FUN_WITH_LIBRESSL) + target_compile_definitions(fun_core PUBLIC FUN_WITH_LIBRESSL=1) +endif() + # Workaround: provide a dummy rust_eh_personality to satisfy linker when using # no_std Rust staticlib with panic abort (some toolchains still reference it). # If needed, we could add a C shim here, but the Rust crate now defines diff --git a/docs/README.md b/docs/README.md index ab2ea78..e3d4ed8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -21,7 +21,7 @@ This file serves as an index of the documents in this directory. Links are relat ## New and supplemental guides -- [build.md](./build.md) - How to build Fun with CMake, available targets, and build options (FUN_DEBUG, FUN_USE_MUSL, FUN_WITH_CPP, FUN_WITH_RUST, FUN_WITH_OPENSSL). +- [build.md](./build.md) - How to build Fun with CMake, available targets, and build options (FUN_DEBUG, FUN_USE_MUSL, FUN_WITH_CPP, FUN_WITH_RUST, FUN_WITH_OPENSSL, FUN_WITH_LIBRESSL). - [cli.md](./cli.md) - Command-line usage of the `fun` executable: synopsis, options, exit codes, includes and library paths. - [contributing.md](./contributing.md) - How to contribute: project structure, coding style, running tests, and PR guidelines. - [style-guide.md](./style-guide.md) - Coding conventions for C and Fun (indentation, naming, idioms). @@ -40,10 +40,12 @@ This file serves as an index of the documents in this directory. Links are relat Documentation for optional, build-time selectable integrations lives in [external/](./external/): - [Index of extensions](./external/README.md) - - Highlights: [cURL](./external/curl.md), [INI](./external/ini.md), [JSON](./external/json.md), [XML (libxml2)](./external/xml2.md), [SQLite](./external/sqlite.md), [libSQL](./external/libsql.md), [PCRE2](./external/pcre2.md), [PC/SC](./external/pcsc.md), [Notcurses](./external/notcurses.md), [Tcl/Tk](./external/tcltk.md), [OpenSSL](./external/openssl.md) + - Highlights: [cURL](./external/curl.md), [INI](./external/ini.md), [JSON](./external/json.md), [XML (libxml2)](./external/xml2.md), [SQLite](./external/sqlite.md), [libSQL](./external/libsql.md), [PCRE2](./external/pcre2.md), [PC/SC](./external/pcsc.md), [Notcurses](./external/notcurses.md), [Tcl/Tk](./external/tcltk.md), [OpenSSL](./external/openssl.md), [LibreSSL](./external/libressl.md) ## Tips - When building from the repo without installing, set `FUN_LIB_DIR` to the local `./lib` directory so examples and the REPL can locate the stdlib. - For a broader project overview and quickstart, see the repository root [README.md](../README.md). -- Crypto example: if built with `-DFUN_WITH_OPENSSL=ON`, try `examples/crypto/openssl_md5.fun` to compute MD5 using OpenSSL. +- Crypto examples: + - If built with `-DFUN_WITH_OPENSSL=ON`, try `examples/crypto/openssl_md5.fun`. + - If built with `-DFUN_WITH_LIBRESSL=ON`, try `examples/crypto/libressl_md5.fun`. diff --git a/docs/build.md b/docs/build.md index 0a45457..2d70544 100644 --- a/docs/build.md +++ b/docs/build.md @@ -23,6 +23,7 @@ Fun exposes several options you can toggle at configure time: - `FUN_WITH_CPP` (ON/OFF) - Enable C++-based opcode/examples support - `FUN_WITH_RUST` (ON/OFF) - Build and link Rust staticlib from `src/rust/` - `FUN_WITH_OPENSSL` (ON/OFF) - Enable OpenSSL-backed helpers (MD5/SHA-256/SHA-512/RIPEMD-160) + - `FUN_WITH_LIBRESSL` (ON/OFF) - Enable LibreSSL-backed helpers (MD5/SHA-256/SHA-512/RIPEMD-160) When configuring, the build prints a summary like: @@ -55,13 +56,13 @@ cmake --build build_release --target build ### Enabling optional extensions ``` cmake -S . -B build_release -DCMAKE_BUILD_TYPE=Release \ - -DFUN_WITH_CPP=ON -DFUN_WITH_RUST=ON -DFUN_WITH_OPENSSL=ON + -DFUN_WITH_CPP=ON -DFUN_WITH_RUST=ON -DFUN_WITH_OPENSSL=ON -DFUN_WITH_LIBRESSL=ON cmake --build build_release --target build ``` If `FUN_WITH_RUST` is enabled, ensure `cargo` is available in PATH; the build will invoke it and link the produced static library. -If `FUN_WITH_OPENSSL` is enabled, CMake must detect your system OpenSSL. Hash helpers use the EVP interface (OpenSSL 3-compatible). Note: RIPEMD-160 may require the legacy provider on OpenSSL 3.x; if unavailable, the helper returns an empty string. +If `FUN_WITH_OPENSSL` is enabled, CMake must detect your system OpenSSL (libcrypto). If `FUN_WITH_LIBRESSL` is enabled, CMake detects LibreSSL directly (via pkg-config or standard include/lib locations) and links to LibreSSL’s `libcrypto` — no OpenSSL installation is required. Both extensions use the EVP interface. Note: On OpenSSL 3.x, RIPEMD-160 may require the legacy provider; if unavailable, the helper returns an empty string. ## Running - CLI: run the `fun` executable from your build directory. diff --git a/docs/external/README.md b/docs/external/README.md index 15950cb..8a78aa7 100644 --- a/docs/external/README.md +++ b/docs/external/README.md @@ -6,7 +6,7 @@ This section documents Fun’s optional, build‑time selectable extensions. Eac - Available opcodes and/or helper functions - Minimal usage examples and links to example scripts -Extensions: +## Extensions: - [cURL (libcurl)](./curl.md) - [INI (iniparser)](./ini.md) @@ -19,7 +19,9 @@ Extensions: - [Notcurses (TUI)](./notcurses.md) - [Tcl/Tk (GUI)](./tcltk.md) - [OpenSSL](./openssl.md) +- [LibreSSL](./libressl.md) + +## Notes: -Notes: - These integrations are optional; the VM compiles without them. - When disabled, related builtins usually return empty strings/neutral values rather than fail hard, mirroring existing optionality patterns. diff --git a/docs/external/curl.md b/docs/external/curl.md index b47ddc3..e76f875 100644 --- a/docs/external/curl.md +++ b/docs/external/curl.md @@ -4,11 +4,13 @@ - Purpose: HTTP helpers using libcurl. - Homepage: https://curl.se/libcurl/ -Opcodes: +## Opcodes: + - OP_CURL_GET: GET; pops url:string; pushes body:string (empty on error/disabled) - OP_CURL_POST: POST; pops body:string, url:string; pushes response:string - OP_CURL_DOWNLOAD: Download to file; pops path:string, url:string; pushes 1/0 -Notes: +## Notes: + - Requires libcurl development headers/libs. - When disabled, helpers return empty strings/0 to match optional behavior. diff --git a/docs/external/ini.md b/docs/external/ini.md index 2975313..aeb5ac5 100644 --- a/docs/external/ini.md +++ b/docs/external/ini.md @@ -4,7 +4,8 @@ - Purpose: Read/write simple INI configuration files. - Homepage: https://github.com/ndevilla/iniparser -Opcodes: +## Opcodes: + - OP_INI_LOAD: pops path; pushes handle (>0) or 0 - OP_INI_FREE: pops handle; pushes 1/0 - OP_INI_GET_STRING: pops def, key, section, handle; pushes string @@ -15,6 +16,7 @@ Opcodes: - OP_INI_UNSET: pops key, section, handle; pushes 1/0 - OP_INI_SAVE: pops path, handle; pushes 1/0 -Notes: +## Notes: + - Requires iniparser development headers/libs. - When disabled, helpers return neutral values (0/empty strings) like other optional extensions. diff --git a/docs/external/json.md b/docs/external/json.md index 60475d1..4456174 100644 --- a/docs/external/json.md +++ b/docs/external/json.md @@ -4,12 +4,14 @@ - Purpose: JSON parse/stringify and file helpers via json-c. - Homepage: https://json-c.github.io/json-c/ -Opcodes: +## Opcodes: + - OP_JSON_PARSE: pops text; pushes value or Nil on error - OP_JSON_STRINGIFY: pops pretty:int(0/1), value; pushes string - OP_JSON_FROM_FILE: pops path; pushes value or Nil - OP_JSON_TO_FILE: pops pretty:int(0/1), value, path; pushes 1/0 -Notes: +## Notes: + - Requires json-c development headers/libs. - When disabled, functions push empty/neutral values similar to other optional modules. diff --git a/docs/external/libressl.md b/docs/external/libressl.md new file mode 100644 index 0000000..5751a0e --- /dev/null +++ b/docs/external/libressl.md @@ -0,0 +1,45 @@ +# LibreSSL extension (optional) + +- CMake option: FUN_WITH_LIBRESSL=ON +- Purpose: provide small crypto helpers backed by LibreSSL’s libcrypto. Includes md5, sha256, sha512, ripemd160 helpers under libressl_* names. +- Homepage: https://www.libressl.org/ + +## Build notes: + +- Requires system LibreSSL development headers and libcrypto. The build detects LibreSSL directly (via pkg-config or standard include/lib locations) and links to libcrypto from LibreSSL — it does not require OpenSSL to be installed. +- When disabled, the builtins below evaluate to empty strings to mirror optionality behavior across extensions. + +## Provided helper functions/opcodes: + +- Function: libressl_md5(data:string) -> string (lowercase hex). +- Function: libressl_sha256(data:string) -> string (lowercase hex). +- Function: libressl_sha512(data:string) -> string (lowercase hex). +- Function: libressl_ripemd160(data:string) -> string (lowercase hex). +- Opcodes: OP_LIBRESSL_MD5, OP_LIBRESSL_SHA256, OP_LIBRESSL_SHA512, OP_LIBRESSL_RIPEMD160 (internal mappings for the functions above). + +## Quickstart: + +- Configure: `cmake -S . -B build -DFUN_WITH_LIBRESSL=ON` +- Build: `cmake --build build --target fun` +- Run examples: + - `./build/fun examples/crypto/libressl_md5.fun` + - `./build/fun examples/crypto/libressl_sha256.fun` + - `./build/fun examples/crypto/libressl_sha512.fun` + - `./build/fun examples/crypto/libressl_ripemd160.fun` + +## Example output: + +- md5(abc) = 900150983cd24fb0d6963f7d28e17f72 +- md5("") = d41d8cd98f00b204e9800998ecf8427e +- sha256(abc) = ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad +- sha256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 +- sha512(abc) = ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f +- sha512("") = cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e +- ripemd160(abc) = 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc +- ripemd160("") = 9c1185a5c5e9fc54612808977ee8f548b2258d31 + +## Notes: + +- The functions accept any Fun value; non-strings are coerced via `to_string` semantics by the VM before hashing. +- On some platforms, RIPEMD-160 may not be available; in that case the helper returns an empty string. +- If your system installs LibreSSL headers under `/usr/include/libressl`, CMake will automatically add this directory to the include path so that `#include ` resolves correctly. diff --git a/docs/external/libsql.md b/docs/external/libsql.md index c2df1f9..43662a7 100644 --- a/docs/external/libsql.md +++ b/docs/external/libsql.md @@ -4,12 +4,14 @@ - Purpose: SQLite-compatible client using the libSQL (Turso) library. - Homepage: https://libsql.org/ -Opcodes: +## Opcodes: + - OP_LIBSQL_OPEN: pops url_or_path; pushes handle (>0) or 0 - OP_LIBSQL_CLOSE: pops handle; pushes Nil - OP_LIBSQL_EXEC: pops sql, handle; pushes rc:int (0=OK) - OP_LIBSQL_QUERY: pops sql, handle; pushes array -Notes: +## Notes: + - Uses a SQLite-compatible C API provided by libSQL; behavior is similar to the SQLite backend. - This module is independent from the SQLite extension; you may enable either or both. diff --git a/docs/external/notcurses.md b/docs/external/notcurses.md index c596981..9985479 100644 --- a/docs/external/notcurses.md +++ b/docs/external/notcurses.md @@ -4,13 +4,15 @@ - Purpose: Terminal UI capabilities via the Notcurses library. - Homepage: https://notcurses.com/ -Opcodes: +## Opcodes: + - OP_NC_INIT: initialize Notcurses; returns 1 on success, 0 on failure - OP_NC_SHUTDOWN: shutdown; returns 0 - OP_NC_CLEAR: clear screen/plane; returns 0 - OP_NC_DRAW_TEXT: pops text, x, y; draws; returns 0 - OP_NC_GETCH: pops timeout_ms; returns codepoint or -1 on timeout/error -Notes: +## Notes: + - Requires Notcurses development headers/libs. - Behavior may vary across terminals; see the implementation for details. diff --git a/docs/external/openssl.md b/docs/external/openssl.md index 7606280..c0a732b 100644 --- a/docs/external/openssl.md +++ b/docs/external/openssl.md @@ -4,18 +4,21 @@ - Purpose: provide small crypto helpers backed by OpenSSL. Includes md5, sha256, sha512, ripemd160 helpers. - Homepage: https://www.openssl.org/ -Build notes: +## Build notes: + - Requires system OpenSSL development headers and libraries. - On OpenSSL 3.x, legacy MD5_* APIs are deprecated; you may see warnings during build. -Provided helper/opcodes: +## Provided helper/opcodes: + - Function: openssl_md5(data:string) -> string (lowercase hex). Falls back to empty string when the extension is disabled, mirroring other optional modules. - Function: openssl_sha256(data:string) -> string (lowercase hex). - Function: openssl_sha512(data:string) -> string (lowercase hex). - Function: openssl_ripemd160(data:string) -> string (lowercase hex). Note: On OpenSSL 3.x this may require the legacy provider; if the digest is unavailable, the helper returns an empty string. - Opcodes: OP_OPENSSL_MD5, OP_OPENSSL_SHA256, OP_OPENSSL_SHA512, OP_OPENSSL_RIPEMD160 (internal mappings for the functions above). -Quickstart: +## Quickstart: + - Configure: cmake -S . -B build -DFUN_WITH_OPENSSL=ON - Build: cmake --build build --target fun - Run examples: @@ -24,15 +27,17 @@ Quickstart: - ./build/fun examples/crypto/openssl_sha512.fun - ./build/fun examples/crypto/openssl_ripemd160.fun -Example output: +## Example output: + - md5(abc) = 900150983cd24fb0d6963f7d28e17f72 - md5("") = d41d8cd98f00b204e9800998ecf8427e - sha256(abc) = ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad - sha256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 - sha512(abc) = ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f - sha512("") = cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e - - ripemd160(abc) = 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc - - ripemd160("") = 9c1185a5c5e9fc54612808977ee8f548b2258d31 +- ripemd160(abc) = 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc +- ripemd160("") = 9c1185a5c5e9fc54612808977ee8f548b2258d31 + +## Notes: -Notes: - The OpenSSL 3.x provider configuration on your system determines availability of RIPEMD‑160. If the legacy provider is not enabled, openssl_ripemd160() will return an empty string. diff --git a/docs/external/pcre2.md b/docs/external/pcre2.md index 9e4d9f2..2325e5d 100644 --- a/docs/external/pcre2.md +++ b/docs/external/pcre2.md @@ -4,11 +4,13 @@ - Purpose: Advanced regular expressions via PCRE2. - Homepage: https://www.pcre.org/ -Opcodes: +## Opcodes: + - OP_PCRE2_TEST: pops flags, text, pattern; pushes 1/0 - OP_PCRE2_MATCH: pops flags, text, pattern; pushes match map or Nil - OP_PCRE2_FINDALL: pops flags, text, pattern; pushes array of match maps -Notes: +## Notes: + - Requires PCRE2 development headers/libs. - Flags are backend-specific; see implementation for supported bits. diff --git a/docs/external/pcsc.md b/docs/external/pcsc.md index c00b171..4519054 100644 --- a/docs/external/pcsc.md +++ b/docs/external/pcsc.md @@ -4,7 +4,8 @@ - Purpose: Access smart card readers/cards via PC/SC (pcsclite). - Homepage: https://pcsclite.apdu.fr/ -Opcodes: +## Opcodes: + - OP_PCSC_ESTABLISH: returns context id (>0) or 0 - OP_PCSC_RELEASE: pops ctx id; returns 1/0 - OP_PCSC_LIST_READERS: pops ctx id; returns array of reader names @@ -12,6 +13,7 @@ Opcodes: - OP_PCSC_DISCONNECT: pops handle id; returns 1/0 - OP_PCSC_TRANSMIT: pops apdu, handle id; returns map with data/SW/rc -Notes: +## Notes: + - Requires PC/SC lite development headers/libs. - Behavior and availability depend on platform and reader drivers. diff --git a/docs/external/sqlite.md b/docs/external/sqlite.md index e246a76..6dcd00d 100644 --- a/docs/external/sqlite.md +++ b/docs/external/sqlite.md @@ -4,12 +4,14 @@ - Purpose: Access SQLite databases via the native C API. - Homepage: https://www.sqlite.org/ -Opcodes: +## Opcodes: + - OP_SQLITE_OPEN: pops path; pushes handle (>0) or 0 - OP_SQLITE_CLOSE: pops handle; pushes Nil - OP_SQLITE_EXEC: pops sql, handle; pushes rc:int (0=OK) - OP_SQLITE_QUERY: pops sql, handle; pushes array -Notes: +## Notes: + - Requires SQLite development headers/libs. - See also: `libSQL` for a compatible alternative backend. diff --git a/docs/external/tcltk.md b/docs/external/tcltk.md index 97cc429..f39d66a 100644 --- a/docs/external/tcltk.md +++ b/docs/external/tcltk.md @@ -1,8 +1,11 @@ # Tcl/Tk (GUI) extension (optional) + - CMake option: FUN_WITH_TCLTK=ON - Purpose: Basic GUI functionality via Tcl/Tk. - Homepage: https://www.tcl.tk/ -Opcodes: + +## Opcodes: + - OP_TK_EVAL: pops script string; pushes rc (0=OK) - OP_TK_RESULT: pushes last Tcl result string - OP_TK_LOOP: enters main event loop; pushes Nil when done @@ -11,6 +14,8 @@ Opcodes: - OP_TK_BUTTON: pops text, id; creates/updates button .id; pushes rc - OP_TK_PACK: pops id; packs .id; pushes rc - OP_TK_BIND: pops command, event, id; binds; pushes rc -Notes: + +## Notes: + - Requires Tcl/Tk development headers/libs. - GUI behavior depends on your desktop environment/window manager. diff --git a/docs/external/xml2.md b/docs/external/xml2.md index 3a86071..9c22f30 100644 --- a/docs/external/xml2.md +++ b/docs/external/xml2.md @@ -4,12 +4,14 @@ - Purpose: Minimal XML parsing helpers using libxml2. - Homepage: http://xmlsoft.org/ -Opcodes: +## Opcodes: + - OP_XML_PARSE: pops text; pushes doc handle (>0) or 0 - OP_XML_ROOT: pops doc handle; pushes node handle (>0) or 0 - OP_XML_NAME: pops node handle; pushes string (node name) - OP_XML_TEXT: pops node handle; pushes string (concatenated text) -Notes: +## Notes: + - Requires libxml2 development headers/libs. - On many systems, the include path is `/usr/include/libxml2`. diff --git a/docs/internals.md b/docs/internals.md index 5ce70a2..4fdba6f 100644 --- a/docs/internals.md +++ b/docs/internals.md @@ -80,7 +80,7 @@ The interpreter loop lives in src/vm.c: vm_run. Opcodes are executed in a tight Opcode handlers organization: - To keep vm.c readable, most opcode implementations are factored into small .c files included directly into vm.c (e.g., vm/core/load_const.c, vm/logic/and.c, vm/arrays/push.c, vm/math/abs.c, vm/os/thread_spawn.c, etc.). - This is a deliberate “amalgamation” style: small single‑purpose C units compiled as part of vm.c. -- Optional subsystems (JSON, PCRE2, CURL, SQLite, libSQL, PC/SC, XML2, Tcl/Tk, Notcurses, INI, sockets, serial, OS helpers) are grouped under src/external and src/vm//. +- Optional subsystems (JSON, PCRE2, CURL, SQLite, libSQL, PC/SC, XML2, Tcl/Tk, Notcurses, INI, OpenSSL/LibreSSL crypto helpers, sockets, serial, OS helpers) are grouped under src/external and src/vm//. Dispatch naming and visibility: - Human‑readable names for opcodes live in vm.h: opcode_names[]. These are used in debug prints and error messages. @@ -117,7 +117,7 @@ The VM is dynamically typed. Values carry a tag; operations check types at runti - Maps: OP_MAKE_MAP/KEYS/VALUES/HAS_KEY. - Conversions/reflection: OP_TO_NUMBER/TO_STRING/CAST/TYPEOF, OP_UCLAMP/SCLAMP. - I/O and OS: OP_READ_FILE/WRITE_FILE/INPUT_LINE/ENV/PROC_RUN/PROC_SYSTEM/TIME_NOW_MS/CLOCK_MONO_MS/DATE_FORMAT/OS_LIST_DIR/RANDOM_NUMBER, sockets, serial. -- External integrations (optional): JSON, CURL, SQLite, libSQL, PC/SC, XML2, Tcl/Tk, Notcurses, INI. +- External integrations (optional): JSON, CURL, SQLite, libSQL, PC/SC, XML2, Tcl/Tk, Notcurses, INI, OpenSSL/LibreSSL. Each handler enforces argument types and returns clear error messages via vm_raise_error on misuse. diff --git a/examples/crypto/libressl_md5.fun b/examples/crypto/libressl_md5.fun new file mode 100644 index 0000000..24b3740 --- /dev/null +++ b/examples/crypto/libressl_md5.fun @@ -0,0 +1,28 @@ +#!/usr/bin/env fun + +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2026 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2026-02-19 + */ + +// LibreSSL MD5 example +// Enable with -DFUN_WITH_LIBRESSL=ON during build for real hashing. + +s = "abc" +d = libressl_md5(s) +print("md5(abc) = " + d) + +// Another quick check (empty string) +e = "" +print("md5(\"\") = " + libressl_md5(e)) + +/* Expected output: +md5(abc) = 900150983cd24fb0d6963f7d28e17f72 +md5("") = d41d8cd98f00b204e9800998ecf8427e +*/ diff --git a/examples/crypto/libressl_ripemd160.fun b/examples/crypto/libressl_ripemd160.fun new file mode 100644 index 0000000..437e6e1 --- /dev/null +++ b/examples/crypto/libressl_ripemd160.fun @@ -0,0 +1,28 @@ +#!/usr/bin/env fun + +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2026 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2026-02-19 + */ + +// LibreSSL RIPEMD-160 example +// Enable with -DFUN_WITH_LIBRESSL=ON during build for real hashing. + +s = "abc" +d = libressl_ripemd160(s) +print("ripemd160(abc) = " + d) + +// Another quick check (empty string) +e = "" +print("ripemd160(\"\") = " + libressl_ripemd160(e)) + +/* Expected output: +ripemd160(abc) = 8eb208f7e05d987a9b049a9a5c0c2b74e07e6a5d +ripemd160("") = 9c1185a5c5e9fc54612808977ee8f548b2258d31 +*/ diff --git a/examples/crypto/libressl_sha256.fun b/examples/crypto/libressl_sha256.fun new file mode 100644 index 0000000..a51baed --- /dev/null +++ b/examples/crypto/libressl_sha256.fun @@ -0,0 +1,28 @@ +#!/usr/bin/env fun + +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2026 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2026-02-19 + */ + +// LibreSSL SHA-256 example +// Enable with -DFUN_WITH_LIBRESSL=ON during build for real hashing. + +s = "abc" +d = libressl_sha256(s) +print("sha256(abc) = " + d) + +// Another quick check (empty string) +e = "" +print("sha256(\"\") = " + libressl_sha256(e)) + +/* Expected output: +sha256(abc) = ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad +sha256("") = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 +*/ diff --git a/examples/crypto/libressl_sha512.fun b/examples/crypto/libressl_sha512.fun new file mode 100644 index 0000000..525dbcf --- /dev/null +++ b/examples/crypto/libressl_sha512.fun @@ -0,0 +1,28 @@ +#!/usr/bin/env fun + +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2026 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2026-02-19 + */ + +// LibreSSL SHA-512 example +// Enable with -DFUN_WITH_LIBRESSL=ON during build for real hashing. + +s = "abc" +d = libressl_sha512(s) +print("sha512(abc) = " + d) + +// Another quick check (empty string) +e = "" +print("sha512(\"\") = " + libressl_sha512(e)) + +/* Expected output: +sha512(abc) = ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f +sha512("") = cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e +*/ diff --git a/make b/make index 01e2fbd..a11600c 100755 --- a/make +++ b/make @@ -33,6 +33,7 @@ if [ "$target" = "all" ]; then -DFUN_WITH_NOTCURSES=ON \ -DFUN_WITH_CPP=ON \ -DFUN_WITH_OPENSSL=ON \ + -DFUN_WITH_LIBRESSL=ON \ && cmake --build build --target fun elif [ "$target" = "all_debug" ]; then rm -rf build \ diff --git a/src/bytecode.c b/src/bytecode.c index 405e6b7..01629c8 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -173,6 +173,10 @@ static const char *opcode_name(OpCode op) { case OP_OPENSSL_SHA256: return "OPENSSL_SHA256"; case OP_OPENSSL_SHA512: return "OPENSSL_SHA512"; case OP_OPENSSL_RIPEMD160: return "OPENSSL_RIPEMD160"; + case OP_LIBRESSL_MD5: return "LIBRESSL_MD5"; + case OP_LIBRESSL_SHA256: return "LIBRESSL_SHA256"; + case OP_LIBRESSL_SHA512: return "LIBRESSL_SHA512"; + case OP_LIBRESSL_RIPEMD160: return "LIBRESSL_RIPEMD160"; case OP_INI_LOAD: return "INI_LOAD"; case OP_INI_FREE: return "INI_FREE"; case OP_INI_GET_STRING: return "INI_GET_STRING"; diff --git a/src/bytecode.h b/src/bytecode.h index 5f89650..ac173af 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -187,6 +187,12 @@ typedef enum { OP_OPENSSL_SHA512, // pops data string; pushes sha512 hex string OP_OPENSSL_RIPEMD160, // pops data string; pushes ripemd160 hex string + // LibreSSL (optional; same API as OpenSSL but different toggle) + OP_LIBRESSL_MD5, // pops data string; pushes md5 hex string + OP_LIBRESSL_SHA256, // pops data string; pushes sha256 hex string + OP_LIBRESSL_SHA512, // pops data string; pushes sha512 hex string + OP_LIBRESSL_RIPEMD160, // pops data string; pushes ripemd160 hex string + // INI (iniparser 4.2.6) optional OP_INI_LOAD, // pops path; pushes handle (>0) or 0 OP_INI_FREE, // pops handle; pushes 1/0 diff --git a/src/external/libressl.c b/src/external/libressl.c new file mode 100644 index 0000000..78ad66a --- /dev/null +++ b/src/external/libressl.c @@ -0,0 +1,172 @@ +/* +* This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2026 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2026-02-19 + */ + +/* + * LibreSSL integration helpers (MD5, RIPEMD-160, SHA-256, SHA-512) + */ + +#ifdef FUN_WITH_LIBRESSL +/* + * When building with LibreSSL, include the LibreSSL-provided headers explicitly. + * Downstream distros often ship them under /usr/include/libressl/openssl/… + * and expect the include path to point at /usr/include/libressl so that any + * nested `#include ` inside these headers resolves to the + * corresponding LibreSSL headers as well (not the system OpenSSL 3.x ones). + */ +#include +/* Compatibility: Prefer EVP_MD_get_size universally. If LibreSSL headers + * don't declare it, provide a forward declaration so we can link against + * OpenSSL's libcrypto symbol when that is what CMake found. */ +#ifndef EVP_MD_get_size +int EVP_MD_get_size(const EVP_MD *md); +#endif +#endif +#include + +/* Compute MD5 hex of input buffer; returns malloc'ed C string (lowercase hex). */ +static char *fun_libressl_md5_hex(const unsigned char *data, size_t len) { + static const char hexdig[] = "0123456789abcdef"; + if (!data && len != 0) return NULL; +#ifdef FUN_WITH_LIBRESSL + const EVP_MD *md = EVP_md5(); + if (!md) return NULL; + int dlen = EVP_MD_get_size(md); + if (dlen <= 0) return NULL; + unsigned char *digest = (unsigned char*)malloc((size_t)dlen); + if (!digest) return NULL; + unsigned int out_len = 0; + int ok; + if (len == 0) { + ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL); + } else { + ok = EVP_Digest(data, len, digest, &out_len, md, NULL); + } + if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; } + char *hex = (char*)malloc((size_t)dlen * 2 + 1); + if (!hex) { free(digest); return NULL; } + for (int i = 0; i < dlen; ++i) { + hex[2*i] = hexdig[(digest[i] >> 4) & 0xF]; + hex[2*i+1] = hexdig[digest[i] & 0xF]; + } + hex[dlen * 2] = '\0'; + free(digest); + return hex; +#else + char *hex = (char*)malloc(1); + if (hex) hex[0] = '\0'; + return hex; +#endif +} + +/* Compute SHA-256 hex of input buffer; returns malloc'ed C string (lowercase hex). */ +static char *fun_libressl_sha256_hex(const unsigned char *data, size_t len) { + static const char hexdig[] = "0123456789abcdef"; + if (!data && len != 0) return NULL; +#ifdef FUN_WITH_LIBRESSL + const EVP_MD *md = EVP_sha256(); + if (!md) return NULL; + int dlen = EVP_MD_get_size(md); + if (dlen <= 0) return NULL; + unsigned char *digest = (unsigned char*)malloc((size_t)dlen); + if (!digest) return NULL; + unsigned int out_len = 0; + int ok; + if (len == 0) { + ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL); + } else { + ok = EVP_Digest(data, len, digest, &out_len, md, NULL); + } + if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; } + char *hex = (char*)malloc((size_t)dlen * 2 + 1); + if (!hex) { free(digest); return NULL; } + for (int i = 0; i < dlen; ++i) { + hex[2*i] = hexdig[(digest[i] >> 4) & 0xF]; + hex[2*i+1] = hexdig[digest[i] & 0xF]; + } + hex[dlen * 2] = '\0'; + free(digest); + return hex; +#else + char *hex = (char*)malloc(1); + if (hex) hex[0] = '\0'; + return hex; +#endif +} + +/* Compute SHA-512 hex of input buffer; returns malloc'ed C string (lowercase hex). */ +static char *fun_libressl_sha512_hex(const unsigned char *data, size_t len) { + static const char hexdig[] = "0123456789abcdef"; + if (!data && len != 0) return NULL; +#ifdef FUN_WITH_LIBRESSL + const EVP_MD *md = EVP_sha512(); + if (!md) return NULL; + int dlen = EVP_MD_get_size(md); + if (dlen <= 0) return NULL; + unsigned char *digest = (unsigned char*)malloc((size_t)dlen); + if (!digest) return NULL; + unsigned int out_len = 0; + int ok; + if (len == 0) { + ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL); + } else { + ok = EVP_Digest(data, len, digest, &out_len, md, NULL); + } + if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; } + char *hex = (char*)malloc((size_t)dlen * 2 + 1); + if (!hex) { free(digest); return NULL; } + for (int i = 0; i < dlen; ++i) { + hex[2*i] = hexdig[(digest[i] >> 4) & 0xF]; + hex[2*i+1] = hexdig[digest[i] & 0xF]; + } + hex[dlen * 2] = '\0'; + free(digest); + return hex; +#else + char *hex = (char*)malloc(1); + if (hex) hex[0] = '\0'; + return hex; +#endif +} + +/* Compute RIPEMD-160 hex of input buffer; returns malloc'ed C string (lowercase hex). */ +static char *fun_libressl_ripemd160_hex(const unsigned char *data, size_t len) { + static const char hexdig[] = "0123456789abcdef"; + if (!data && len != 0) return NULL; +#ifdef FUN_WITH_LIBRESSL + const EVP_MD *md = EVP_ripemd160(); + if (!md) return NULL; + int dlen = EVP_MD_get_size(md); + if (dlen <= 0) return NULL; + unsigned char *digest = (unsigned char*)malloc((size_t)dlen); + if (!digest) return NULL; + unsigned int out_len = 0; + int ok; + if (len == 0) { + ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL); + } else { + ok = EVP_Digest(data, len, digest, &out_len, md, NULL); + } + if (ok != 1 || (int)out_len != dlen) { free(digest); return NULL; } + char *hex = (char*)malloc((size_t)dlen * 2 + 1); + if (!hex) { free(digest); return NULL; } + for (int i = 0; i < dlen; ++i) { + hex[2*i] = hexdig[(digest[i] >> 4) & 0xF]; + hex[2*i+1] = hexdig[digest[i] & 0xF]; + } + hex[dlen * 2] = '\0'; + free(digest); + return hex; +#else + char *hex = (char*)malloc(1); + if (hex) hex[0] = '\0'; + return hex; +#endif +} diff --git a/src/external/openssl.c b/src/external/openssl.c index 810e430..ac0301e 100644 --- a/src/external/openssl.c +++ b/src/external/openssl.c @@ -15,6 +15,11 @@ #ifdef FUN_WITH_OPENSSL #include +/* Always use EVP_MD_get_size; if headers don't declare it, provide a + * forward declaration to allow linking against OpenSSL libcrypto. */ +#ifndef EVP_MD_get_size +int EVP_MD_get_size(const EVP_MD *md); +#endif #endif #include diff --git a/src/parser.c b/src/parser.c index faa2944..f8f7c49 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1230,6 +1230,39 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos) free(name); return 1; } + /* LibreSSL (md5/sha256/sha512/ripemd160) */ + if (strcmp(name, "libressl_md5") == 0) { + (*pos)++; /* '(' */ + if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "libressl_md5 expects (data)"); free(name); return 0; } + if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after libressl_md5 arg"); free(name); return 0; } + bytecode_add_instruction(bc, OP_LIBRESSL_MD5, 0); + free(name); + return 1; + } + if (strcmp(name, "libressl_sha256") == 0) { + (*pos)++; /* '(' */ + if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "libressl_sha256 expects (data)"); free(name); return 0; } + if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after libressl_sha256 arg"); free(name); return 0; } + bytecode_add_instruction(bc, OP_LIBRESSL_SHA256, 0); + free(name); + return 1; + } + if (strcmp(name, "libressl_sha512") == 0) { + (*pos)++; /* '(' */ + if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "libressl_sha512 expects (data)"); free(name); return 0; } + if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after libressl_sha512 arg"); free(name); return 0; } + bytecode_add_instruction(bc, OP_LIBRESSL_SHA512, 0); + free(name); + return 1; + } + if (strcmp(name, "libressl_ripemd160") == 0) { + (*pos)++; /* '(' */ + if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "libressl_ripemd160 expects (data)"); free(name); return 0; } + if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after libressl_ripemd160 arg"); free(name); return 0; } + bytecode_add_instruction(bc, OP_LIBRESSL_RIPEMD160, 0); + free(name); + return 1; + } /* PCSC builtins */ if (strcmp(name, "pcsc_establish") == 0) { (*pos)++; /* '(' */ diff --git a/src/vm.c b/src/vm.c index eb9af8d..6fa2f2f 100644 --- a/src/vm.c +++ b/src/vm.c @@ -74,6 +74,7 @@ #include "external/tcltk.c" #include "external/xml2.c" #include "external/openssl.c" +#include "external/libressl.c" /* forward declarations for include mapping used in error reporting */ extern char *preprocess_includes(const char *src); @@ -870,10 +871,20 @@ void vm_run(VM *vm, Bytecode *entry) { #endif /* OpenSSL ops (md5/sha256/sha512/ripemd160) */ + #ifdef FUN_WITH_OPENSSL #include "vm/openssl/md5.c" #include "vm/openssl/sha256.c" #include "vm/openssl/sha512.c" #include "vm/openssl/ripemd160.c" + #endif + + /* LibreSSL ops (md5/sha256/sha512/ripemd160) */ + #ifdef FUN_WITH_LIBRESSL + #include "vm/libressl/md5.c" + #include "vm/libressl/sha256.c" + #include "vm/libressl/sha512.c" + #include "vm/libressl/ripemd160.c" + #endif /* Tk (Tcl/Tk) ops */ #ifdef FUN_WITH_TCLTK diff --git a/src/vm/libressl/md5.c b/src/vm/libressl/md5.c new file mode 100644 index 0000000..1ffc998 --- /dev/null +++ b/src/vm/libressl/md5.c @@ -0,0 +1,27 @@ +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2026 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2026-02-19 + */ + +/** +* LibreSSL MD5 builtin +*/ +case OP_LIBRESSL_MD5: { + Value vdata = pop_value(vm); + char *s = value_to_string_alloc(&vdata); + free_value(vdata); + if (!s) { push_value(vm, make_string("")); break; } + char *hex = fun_libressl_md5_hex((const unsigned char*)s, strlen(s)); + free(s); + if (!hex) { push_value(vm, make_string("")); break; } + Value out = make_string(hex); + free(hex); + push_value(vm, out); + break; +} diff --git a/src/vm/libressl/ripemd160.c b/src/vm/libressl/ripemd160.c new file mode 100644 index 0000000..6dd766c --- /dev/null +++ b/src/vm/libressl/ripemd160.c @@ -0,0 +1,27 @@ +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2026 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2026-02-19 + */ + +/** + * LibreSSL RIPEMD-160 builtin + */ +case OP_LIBRESSL_RIPEMD160: { + Value vdata = pop_value(vm); + char *s = value_to_string_alloc(&vdata); + free_value(vdata); + if (!s) { push_value(vm, make_string("")); break; } + char *hex = fun_libressl_ripemd160_hex((const unsigned char*)s, strlen(s)); + free(s); + if (!hex) { push_value(vm, make_string("")); break; } + Value out = make_string(hex); + free(hex); + push_value(vm, out); + break; +} diff --git a/src/vm/libressl/sha256.c b/src/vm/libressl/sha256.c new file mode 100644 index 0000000..1b27be0 --- /dev/null +++ b/src/vm/libressl/sha256.c @@ -0,0 +1,27 @@ +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2026 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2026-02-19 + */ + +/** + * LibreSSL SHA-256 builtin + */ +case OP_LIBRESSL_SHA256: { + Value vdata = pop_value(vm); + char *s = value_to_string_alloc(&vdata); + free_value(vdata); + if (!s) { push_value(vm, make_string("")); break; } + char *hex = fun_libressl_sha256_hex((const unsigned char*)s, strlen(s)); + free(s); + if (!hex) { push_value(vm, make_string("")); break; } + Value out = make_string(hex); + free(hex); + push_value(vm, out); + break; +} diff --git a/src/vm/libressl/sha512.c b/src/vm/libressl/sha512.c new file mode 100644 index 0000000..67abcc8 --- /dev/null +++ b/src/vm/libressl/sha512.c @@ -0,0 +1,27 @@ +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2026 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2026-02-19 + */ + +/** + * LibreSSL SHA-512 builtin + */ +case OP_LIBRESSL_SHA512: { + Value vdata = pop_value(vm); + char *s = value_to_string_alloc(&vdata); + free_value(vdata); + if (!s) { push_value(vm, make_string("")); break; } + char *hex = fun_libressl_sha512_hex((const unsigned char*)s, strlen(s)); + free(s); + if (!hex) { push_value(vm, make_string("")); break; } + Value out = make_string(hex); + free(hex); + push_value(vm, out); + break; +}