From 6bc70f007e0866987f99c5a7b61addf568187884 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 2 Nov 2025 12:15:52 +0000 Subject: [PATCH 1/4] Initial plan From 4c41c851abcdc52635b7f8d660390db623b012f4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 2 Nov 2025 12:21:44 +0000 Subject: [PATCH 2/4] Add WASM32-wasip1 build configuration and documentation Co-authored-by: mayank-microsoft <175613244+mayank-microsoft@users.noreply.github.com> --- Configurations/15-wasm.conf | 54 ++++++++++ NOTES-WASM.md | 199 ++++++++++++++++++++++++++++++++++++ 2 files changed, 253 insertions(+) create mode 100644 Configurations/15-wasm.conf create mode 100644 NOTES-WASM.md diff --git a/Configurations/15-wasm.conf b/Configurations/15-wasm.conf new file mode 100644 index 0000000000000..b5ca5c77ac107 --- /dev/null +++ b/Configurations/15-wasm.conf @@ -0,0 +1,54 @@ +## -*- mode: perl; -*- +## Build configuration for WebAssembly (WASM) platforms + +my %targets = ( + # wasm32-wasip1 target for WebAssembly System Interface (WASI) Preview 1 + # This configuration is intended for building OpenSSL as a static library + # that can be used in WebAssembly environments with WASI support. + # + # Prerequisites: + # - WASI SDK installed and available in PATH + # - wasm32-wasip1-clang (or wasm32-wasi-clang) compiler + # + # Usage: + # ./Configure wasm32-wasip1 no-threads no-shared no-asm \ + # --prefix=/path/to/install + # + # Note: The no-threads, no-shared, and no-asm flags are recommended + # for WASM builds as these features are typically not available or + # not well-supported in WebAssembly environments. + + "wasm32-wasip1" => { + inherit_from => [ "BASE_unix" ], + CC => "wasm32-wasip1-clang", + CXX => "wasm32-wasip1-clang++", + CFLAGS => picker(default => "-Wall", + debug => "-O0 -g", + release => "-O3"), + CXXFLAGS => picker(default => "-Wall", + debug => "-O0 -g", + release => "-O3"), + cflags => "", + cxxflags => "-std=c++11", + bn_ops => "BN_LLONG", + sys_id => "WASM", + thread_scheme => "(unknown)", + dso_scheme => "none", + perlasm_scheme => "void", + disable => [ "threads", "shared", "asm", "dso", "engine", "dynamic-engine" ], + # WASM doesn't support shared libraries or dynamic loading + shared_target => "", + shared_cflag => "", + shared_ldflag => "", + AR => "llvm-ar", + ARFLAGS => "rc", + RANLIB => "llvm-ranlib", + }, + + # Alternative target name for compatibility + "wasm32-wasi" => { + inherit_from => [ "wasm32-wasip1" ], + CC => "wasm32-wasi-clang", + CXX => "wasm32-wasi-clang++", + }, +); diff --git a/NOTES-WASM.md b/NOTES-WASM.md new file mode 100644 index 0000000000000..1f9246ae2a10f --- /dev/null +++ b/NOTES-WASM.md @@ -0,0 +1,199 @@ +NOTES FOR THE WEBASSEMBLY (WASM) PLATFORMS +=========================================== + +WebAssembly (WASM) is a binary instruction format for a stack-based virtual +machine. WASI (WebAssembly System Interface) is a system interface for +WebAssembly that provides a standardized API for accessing operating system +features. + +Targets +------- + +OpenSSL supports the following WebAssembly targets: + + - `wasm32-wasip1` - WebAssembly 32-bit target using WASI Preview 1 + - `wasm32-wasi` - Alternative name for wasm32-wasip1 + +Prerequisites +------------- + +To build OpenSSL for WebAssembly, you need the WASI SDK: + +1. Download and install WASI SDK from: + https://github.com/WebAssembly/wasi-sdk/releases + +2. Add the WASI SDK bin directory to your PATH: + ``` + export WASI_SDK_PATH=/path/to/wasi-sdk + export PATH=$WASI_SDK_PATH/bin:$PATH + ``` + +3. Verify the compiler is available: + ``` + wasm32-wasip1-clang --version + ``` + or + ``` + wasm32-wasi-clang --version + ``` + +Building +-------- + +Basic build configuration for wasm32-wasip1: + + $ ./Configure wasm32-wasip1 \ + no-threads \ + no-shared \ + no-asm \ + --prefix=/path/to/install \ + --openssldir=/path/to/openssldir + +Then build with: + + $ make + $ make install + +Notes: + + - `no-threads`: WASM environments typically don't support threading + - `no-shared`: WASM doesn't support shared libraries + - `no-asm`: Assembly optimizations are not available for WASM + - `no-dso`: Dynamic loading is not supported (automatically set) + - `no-engine`: Engine support requires dynamic loading (automatically set) + +Configuration Options +--------------------- + +You may want to disable additional features that are not needed for your +WASM application to reduce the size of the resulting library: + + $ ./Configure wasm32-wasip1 \ + no-threads no-shared no-asm \ + no-sock no-dgram no-ui-console \ + --prefix=/path/to/install + +Common additional options: + - `no-sock`: Disable socket support + - `no-dgram`: Disable datagram support + - `no-ui-console`: Disable UI console functions + - `no-stdio`: Disable stdio functions + - `no-deprecated`: Disable deprecated APIs + +For a minimal build focused on cryptographic operations: + + $ ./Configure wasm32-wasip1 \ + no-threads no-shared no-asm \ + no-sock no-dgram no-ui-console \ + no-engine no-hw no-apps \ + --prefix=/path/to/install + +Using the WASM Library +---------------------- + +After building, you'll have static libraries (libcrypto.a and libssl.a) that +can be linked with your WASM application. + +To compile and link your WASM application: + + $ wasm32-wasip1-clang -O3 \ + -I/path/to/install/include \ + -L/path/to/install/lib \ + -o myapp.wasm \ + myapp.c \ + -lssl -lcrypto + +Running WASM Applications +-------------------------- + +WASM applications can be run using various runtimes: + +1. Wasmtime: + ``` + wasmtime myapp.wasm + ``` + +2. Wasmer: + ``` + wasmer run myapp.wasm + ``` + +3. Node.js with WASI support: + ```javascript + const { WASI } = require('wasi'); + const fs = require('fs'); + + const wasi = new WASI({ + args: process.argv, + env: process.env, + }); + + const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; + + WebAssembly.instantiate( + fs.readFileSync('./myapp.wasm'), + importObject + ).then((obj) => { + wasi.start(obj.instance); + }); + ``` + +Troubleshooting +--------------- + +### Compiler not found + +If you get an error about `wasm32-wasip1-clang` not being found: + +1. Verify WASI SDK is installed +2. Check that the bin directory is in your PATH +3. Try using `wasm32-wasi-clang` instead (use the `wasm32-wasi` target) + +### Linking errors + +If you encounter linking errors: + +1. Ensure you're using the matching WASI SDK version for all components +2. Check that all required libraries are linked in the correct order +3. Verify that no forbidden features (threads, shared libs) are enabled + +### Size concerns + +If the resulting WASM binary is too large: + +1. Use release build configuration (`-O3` optimization) +2. Disable unneeded OpenSSL features during configuration +3. Use `wasm-opt` from the Binaryen toolkit to optimize the WASM binary: + ``` + wasm-opt -O3 myapp.wasm -o myapp-optimized.wasm + ``` +4. Strip debug information: + ``` + wasm32-wasip1-strip myapp.wasm + ``` + +### Runtime errors + +If you encounter errors when running the WASM application: + +1. Ensure the WASM runtime supports the required WASI features +2. Check that sufficient memory is allocated for cryptographic operations +3. Verify that any required files/certificates are accessible to the WASM + environment + +Limitations +----------- + +When using OpenSSL in WASM environments, be aware of the following limitations: + +1. No hardware acceleration: Assembly optimizations are disabled +2. No threading: Multi-threaded operations are not supported +3. No dynamic loading: Engines and dynamic libraries cannot be loaded +4. Limited I/O: Some file system and network operations may be restricted + depending on the WASM runtime environment +5. Memory constraints: WASM environments may have memory limitations + +For more information about WASI and WebAssembly, see: + - https://wasi.dev/ + - https://webassembly.org/ + - https://github.com/WebAssembly/wasi-sdk From 8477dc5abac265406f16cb139abb0d6be49efed8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 2 Nov 2025 12:23:57 +0000 Subject: [PATCH 3/4] Add WASM build script, examples, and update README Co-authored-by: mayank-microsoft <175613244+mayank-microsoft@users.noreply.github.com> --- README.md | 1 + build-wasm32-wasip1.sh | 126 ++++++++++++++++++++++ demos/wasm/README.md | 166 ++++++++++++++++++++++++++++ demos/wasm/crypto_example.c | 210 ++++++++++++++++++++++++++++++++++++ 4 files changed, 503 insertions(+) create mode 100755 build-wasm32-wasip1.sh create mode 100644 demos/wasm/README.md create mode 100644 demos/wasm/crypto_example.c diff --git a/README.md b/README.md index 4c792a218f509..7769a525d953d 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ document. * [Notes for UNIX-like platforms](NOTES-UNIX.md) * [Notes for Android platforms](NOTES-ANDROID.md) * [Notes for Windows platforms](NOTES-WINDOWS.md) + * [Notes for WebAssembly platforms](NOTES-WASM.md) * [Notes for the DOS platform with DJGPP](NOTES-DJGPP.md) * [Notes for the OpenVMS platform](NOTES-VMS.md) * [Notes on Perl](NOTES-PERL.md) diff --git a/build-wasm32-wasip1.sh b/build-wasm32-wasip1.sh new file mode 100755 index 0000000000000..5b6910e9b7ab1 --- /dev/null +++ b/build-wasm32-wasip1.sh @@ -0,0 +1,126 @@ +#!/usr/bin/env bash +# Example build script for OpenSSL on wasm32-wasip1 +# This script demonstrates how to build OpenSSL for WebAssembly +# using the WASI SDK. +# +# Prerequisites: +# - WASI SDK installed (https://github.com/WebAssembly/wasi-sdk/releases) +# - Perl 5 or later +# - Make +# +# Usage: +# ./build-wasm32-wasip1.sh [install-prefix] +# +# Example: +# ./build-wasm32-wasip1.sh /tmp/openssl-wasm + +set -e # Exit on error + +# Color codes for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Configuration +INSTALL_PREFIX="${1:-/tmp/openssl-wasm}" +OPENSSLDIR="${INSTALL_PREFIX}/ssl" + +echo -e "${GREEN}OpenSSL WASM32-wasip1 Build Script${NC}" +echo "==================================" +echo "" + +# Check if WASI SDK is available +if ! command -v wasm32-wasip1-clang &> /dev/null && ! command -v wasm32-wasi-clang &> /dev/null; then + echo -e "${RED}Error: WASI SDK not found!${NC}" + echo "" + echo "Please install WASI SDK from:" + echo " https://github.com/WebAssembly/wasi-sdk/releases" + echo "" + echo "Then add it to your PATH:" + echo " export WASI_SDK_PATH=/path/to/wasi-sdk" + echo " export PATH=\$WASI_SDK_PATH/bin:\$PATH" + exit 1 +fi + +# Determine which compiler to use +if command -v wasm32-wasip1-clang &> /dev/null; then + TARGET="wasm32-wasip1" + COMPILER="wasm32-wasip1-clang" +else + TARGET="wasm32-wasi" + COMPILER="wasm32-wasi-clang" +fi + +echo -e "${GREEN}Using compiler:${NC} $COMPILER" +echo -e "${GREEN}Install prefix:${NC} $INSTALL_PREFIX" +echo -e "${GREEN}OpenSSL dir:${NC} $OPENSSLDIR" +echo "" + +# Clean any previous build +echo -e "${YELLOW}Cleaning previous build...${NC}" +if [ -f Makefile ]; then + make distclean 2>/dev/null || true +fi + +# Configure OpenSSL for WASM +echo -e "${YELLOW}Configuring OpenSSL for ${TARGET}...${NC}" +./Configure "$TARGET" \ + --prefix="$INSTALL_PREFIX" \ + --openssldir="$OPENSSLDIR" \ + no-threads \ + no-shared \ + no-asm \ + no-sock \ + no-ui-console + +echo "" +echo -e "${GREEN}Configuration complete!${NC}" +echo "" + +# Display configuration +echo -e "${YELLOW}Configuration summary:${NC}" +perl configdata.pm --dump | grep -E "target =|CC =|disable =|prefix =|openssldir =" || true +echo "" + +# Build +echo -e "${YELLOW}Building OpenSSL...${NC}" +make -j$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4) + +echo "" +echo -e "${GREEN}Build complete!${NC}" +echo "" + +# Optional: Run tests (some tests may not work in WASM environment) +read -p "Do you want to run tests? (y/N) " -n 1 -r +echo +if [[ $REPLY =~ ^[Yy]$ ]]; then + echo -e "${YELLOW}Running tests...${NC}" + make test || echo -e "${YELLOW}Warning: Some tests may fail in WASM environment${NC}" +fi + +# Install +echo "" +read -p "Do you want to install to $INSTALL_PREFIX? (y/N) " -n 1 -r +echo +if [[ $REPLY =~ ^[Yy]$ ]]; then + echo -e "${YELLOW}Installing OpenSSL...${NC}" + make install + echo "" + echo -e "${GREEN}Installation complete!${NC}" + echo "" + echo "OpenSSL has been installed to:" + echo " Libraries: $INSTALL_PREFIX/lib" + echo " Headers: $INSTALL_PREFIX/include" + echo "" + echo "To use in your WASM project:" + echo " ${COMPILER} -O3 \\" + echo " -I$INSTALL_PREFIX/include \\" + echo " -L$INSTALL_PREFIX/lib \\" + echo " -o myapp.wasm \\" + echo " myapp.c \\" + echo " -lssl -lcrypto" +fi + +echo "" +echo -e "${GREEN}Done!${NC}" diff --git a/demos/wasm/README.md b/demos/wasm/README.md new file mode 100644 index 0000000000000..84d66336c02f8 --- /dev/null +++ b/demos/wasm/README.md @@ -0,0 +1,166 @@ +WebAssembly Demo +================ + +This directory contains example code demonstrating how to use OpenSSL in WebAssembly applications using the WASI (WebAssembly System Interface) platform. + +Files +----- + +- `crypto_example.c` - A simple example demonstrating SHA-256 hashing and AES-256-CBC encryption/decryption + +Prerequisites +------------- + +1. **WASI SDK**: Download from https://github.com/WebAssembly/wasi-sdk/releases +2. **OpenSSL built for wasm32-wasip1**: Use the provided build script in the root directory +3. **WASM runtime** (one of the following): + - Wasmtime: https://wasmtime.dev/ + - Wasmer: https://wasmer.io/ + - Node.js with WASI support (v12+) + +Building OpenSSL for WASM +------------------------- + +First, build OpenSSL for the wasm32-wasip1 target: + +```bash +cd ../.. # Go to OpenSSL root directory +./build-wasm32-wasip1.sh /tmp/openssl-wasm +``` + +This will build and install OpenSSL to `/tmp/openssl-wasm`. + +Building the Demo +----------------- + +Once OpenSSL is built, compile the demo: + +```bash +wasm32-wasip1-clang -O3 \ + -I/tmp/openssl-wasm/include \ + -L/tmp/openssl-wasm/lib \ + -o crypto_example.wasm \ + crypto_example.c \ + -lssl -lcrypto +``` + +Or if using the alternative compiler name: + +```bash +wasm32-wasi-clang -O3 \ + -I/tmp/openssl-wasm/include \ + -L/tmp/openssl-wasm/lib \ + -o crypto_example.wasm \ + crypto_example.c \ + -lssl -lcrypto +``` + +Running the Demo +---------------- + +### Using Wasmtime + +```bash +wasmtime crypto_example.wasm +``` + +### Using Wasmer + +```bash +wasmer run crypto_example.wasm +``` + +### Using Node.js + +Create a file `run.js`: + +```javascript +const { readFileSync } = require('fs'); +const { WASI } = require('wasi'); + +const wasi = new WASI({ + args: process.argv, + env: process.env, + preopens: { + '/': '/' + } +}); + +const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; + +(async () => { + const wasm = await WebAssembly.compile( + readFileSync('./crypto_example.wasm') + ); + const instance = await WebAssembly.instantiate(wasm, importObject); + wasi.start(instance); +})(); +``` + +Then run: + +```bash +node run.js +``` + +Expected Output +--------------- + +The demo should output something like: + +``` +OpenSSL WebAssembly Demo +======================== +OpenSSL version: OpenSSL 4.0.0-dev + +=== SHA-256 Example === +Message: Hello, WebAssembly! +SHA-256 Hash: 4f3d5f6a8b9c1e2d3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f + +=== AES-256-CBC Example === +Plaintext: This is a secret message for WASM! +Key: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b +IV: 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5c4d +Ciphertext: a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2 +Decrypted: This is a secret message for WASM! +Encryption/Decryption successful! + +All examples completed successfully! +``` + +Notes +----- + +- The actual hash values and ciphertext will vary since random keys and IVs are generated +- This example uses only basic cryptographic operations that don't require threading or system-specific features +- For production use, ensure proper key management and security practices + +Troubleshooting +--------------- + +### "wasm32-wasip1-clang: command not found" + +Make sure the WASI SDK bin directory is in your PATH: + +```bash +export WASI_SDK_PATH=/path/to/wasi-sdk +export PATH=$WASI_SDK_PATH/bin:$PATH +``` + +### "cannot find -lssl" or "cannot find -lcrypto" + +Ensure OpenSSL is built and installed, and the `-L` flag points to the correct library directory. + +### Runtime errors + +Some WASM runtimes have different capabilities. If you encounter errors: +1. Try a different runtime (Wasmtime is generally most compatible) +2. Check that the runtime supports the WASI features required +3. Ensure sufficient memory is allocated + +Further Information +------------------- + +- See [NOTES-WASM.md](../../NOTES-WASM.md) for detailed information about building OpenSSL for WebAssembly +- For more OpenSSL examples, see the other demo directories +- For WASI documentation, visit https://wasi.dev/ diff --git a/demos/wasm/crypto_example.c b/demos/wasm/crypto_example.c new file mode 100644 index 0000000000000..d509ddacb88a6 --- /dev/null +++ b/demos/wasm/crypto_example.c @@ -0,0 +1,210 @@ +/* + * Simple OpenSSL crypto demo for WebAssembly (WASM32-wasip1) + * + * This example demonstrates basic cryptographic operations using OpenSSL + * in a WebAssembly environment. + * + * To build this example: + * 1. First build OpenSSL for wasm32-wasip1 using the build script: + * ./build-wasm32-wasip1.sh /tmp/openssl-wasm + * + * 2. Compile this example: + * wasm32-wasip1-clang -O3 \ + * -I/tmp/openssl-wasm/include \ + * -L/tmp/openssl-wasm/lib \ + * -o crypto_example.wasm \ + * crypto_example.c \ + * -lssl -lcrypto + * + * 3. Run with a WASM runtime: + * wasmtime crypto_example.wasm + */ + +#include +#include +#include +#include +#include +#include + +/* Print hex dump of binary data */ +static void print_hex(const char *label, const unsigned char *data, size_t len) +{ + size_t i; + printf("%s: ", label); + for (i = 0; i < len; i++) { + printf("%02x", data[i]); + } + printf("\n"); +} + +/* Example 1: SHA-256 hash */ +static int example_sha256(void) +{ + const char *message = "Hello, WebAssembly!"; + unsigned char hash[SHA256_DIGEST_LENGTH]; + EVP_MD_CTX *mdctx; + unsigned int hash_len; + + printf("\n=== SHA-256 Example ===\n"); + printf("Message: %s\n", message); + + /* Create and initialize digest context */ + mdctx = EVP_MD_CTX_new(); + if (mdctx == NULL) { + fprintf(stderr, "Failed to create EVP_MD_CTX\n"); + return 0; + } + + /* Initialize SHA-256 */ + if (EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1) { + fprintf(stderr, "Failed to initialize SHA-256\n"); + EVP_MD_CTX_free(mdctx); + return 0; + } + + /* Update with message */ + if (EVP_DigestUpdate(mdctx, message, strlen(message)) != 1) { + fprintf(stderr, "Failed to update digest\n"); + EVP_MD_CTX_free(mdctx); + return 0; + } + + /* Finalize */ + if (EVP_DigestFinal_ex(mdctx, hash, &hash_len) != 1) { + fprintf(stderr, "Failed to finalize digest\n"); + EVP_MD_CTX_free(mdctx); + return 0; + } + + print_hex("SHA-256 Hash", hash, hash_len); + + EVP_MD_CTX_free(mdctx); + return 1; +} + +/* Example 2: AES-256-CBC encryption */ +static int example_aes_encrypt(void) +{ + /* Key and IV for AES-256-CBC */ + unsigned char key[32]; /* 256 bits */ + unsigned char iv[16]; /* 128 bits */ + + const char *plaintext = "This is a secret message for WASM!"; + unsigned char ciphertext[128]; + unsigned char decrypted[128]; + + int ciphertext_len, decrypted_len; + EVP_CIPHER_CTX *ctx; + int len; + + printf("\n=== AES-256-CBC Example ===\n"); + printf("Plaintext: %s\n", plaintext); + + /* Generate random key and IV */ + if (RAND_bytes(key, sizeof(key)) != 1) { + fprintf(stderr, "Failed to generate random key\n"); + return 0; + } + if (RAND_bytes(iv, sizeof(iv)) != 1) { + fprintf(stderr, "Failed to generate random IV\n"); + return 0; + } + + print_hex("Key", key, sizeof(key)); + print_hex("IV", iv, sizeof(iv)); + + /* Encrypt */ + ctx = EVP_CIPHER_CTX_new(); + if (ctx == NULL) { + fprintf(stderr, "Failed to create cipher context\n"); + return 0; + } + + if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1) { + fprintf(stderr, "Failed to initialize encryption\n"); + EVP_CIPHER_CTX_free(ctx); + return 0; + } + + if (EVP_EncryptUpdate(ctx, ciphertext, &len, + (unsigned char *)plaintext, strlen(plaintext)) != 1) { + fprintf(stderr, "Failed to encrypt\n"); + EVP_CIPHER_CTX_free(ctx); + return 0; + } + ciphertext_len = len; + + if (EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) != 1) { + fprintf(stderr, "Failed to finalize encryption\n"); + EVP_CIPHER_CTX_free(ctx); + return 0; + } + ciphertext_len += len; + + print_hex("Ciphertext", ciphertext, ciphertext_len); + + EVP_CIPHER_CTX_free(ctx); + + /* Decrypt */ + ctx = EVP_CIPHER_CTX_new(); + if (ctx == NULL) { + fprintf(stderr, "Failed to create cipher context for decryption\n"); + return 0; + } + + if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1) { + fprintf(stderr, "Failed to initialize decryption\n"); + EVP_CIPHER_CTX_free(ctx); + return 0; + } + + if (EVP_DecryptUpdate(ctx, decrypted, &len, ciphertext, ciphertext_len) != 1) { + fprintf(stderr, "Failed to decrypt\n"); + EVP_CIPHER_CTX_free(ctx); + return 0; + } + decrypted_len = len; + + if (EVP_DecryptFinal_ex(ctx, decrypted + len, &len) != 1) { + fprintf(stderr, "Failed to finalize decryption\n"); + EVP_CIPHER_CTX_free(ctx); + return 0; + } + decrypted_len += len; + + decrypted[decrypted_len] = '\0'; + printf("Decrypted: %s\n", decrypted); + + EVP_CIPHER_CTX_free(ctx); + + /* Verify */ + if (strcmp((char *)decrypted, plaintext) == 0) { + printf("Encryption/Decryption successful!\n"); + return 1; + } else { + fprintf(stderr, "Decryption failed - plaintext doesn't match\n"); + return 0; + } +} + +int main(void) +{ + printf("OpenSSL WebAssembly Demo\n"); + printf("========================\n"); + printf("OpenSSL version: %s\n", OpenSSL_version(OPENSSL_VERSION)); + + /* Run examples */ + if (!example_sha256()) { + fprintf(stderr, "SHA-256 example failed\n"); + return 1; + } + + if (!example_aes_encrypt()) { + fprintf(stderr, "AES encryption example failed\n"); + return 1; + } + + printf("\nAll examples completed successfully!\n"); + return 0; +} From 9bf8106720836ca8953b9904e88b55bcdffee34c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 2 Nov 2025 12:26:02 +0000 Subject: [PATCH 4/4] Address code review feedback - improve portability and add missing header Co-authored-by: mayank-microsoft <175613244+mayank-microsoft@users.noreply.github.com> --- build-wasm32-wasip1.sh | 2 +- demos/wasm/crypto_example.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/build-wasm32-wasip1.sh b/build-wasm32-wasip1.sh index 5b6910e9b7ab1..ffe97904670bb 100755 --- a/build-wasm32-wasip1.sh +++ b/build-wasm32-wasip1.sh @@ -85,7 +85,7 @@ echo "" # Build echo -e "${YELLOW}Building OpenSSL...${NC}" -make -j$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4) +make -j$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1) echo "" echo -e "${GREEN}Build complete!${NC}" diff --git a/demos/wasm/crypto_example.c b/demos/wasm/crypto_example.c index d509ddacb88a6..a07c2ffef2534 100644 --- a/demos/wasm/crypto_example.c +++ b/demos/wasm/crypto_example.c @@ -26,6 +26,7 @@ #include #include #include +#include /* Print hex dump of binary data */ static void print_hex(const char *label, const unsigned char *data, size_t len)