Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
83 changes: 83 additions & 0 deletions test/tool/net/lcrypto_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
-- Helper function to print test results
local function assert_equal(actual, expected, message)
if actual ~= expected then
error(message .. ": expected " .. tostring(expected) .. ", got " .. tostring(actual))
else
print("PASS: " .. message)
end
end

-- Test RSA key pair generation
local function test_rsa_keypair_generation()
local priv_key, pub_key = crypto.generatekeypair("rsa", 2048)
assert_equal(type(priv_key), "string", "RSA private key generation")
assert_equal(type(pub_key), "string", "RSA public key generation")
end

-- Test ECDSA key pair generation
local function test_ecdsa_keypair_generation()
local priv_key, pub_key = crypto.generatekeypair("ecdsa", "secp256r1")
assert_equal(type(priv_key), "string", "ECDSA private key generation")
assert_equal(type(pub_key), "string", "ECDSA public key generation")
end

-- Test RSA encryption and decryption
local function test_rsa_encryption_decryption()
local priv_key, pub_key = crypto.generatekeypair("rsa", 2048)
local message = "Hello, RSA!"
local encrypted = crypto.encrypt("rsa", pub_key, message)
assert_equal(type(encrypted), "string", "RSA encryption")
local decrypted = crypto.decrypt("rsa", priv_key, encrypted)
assert_equal(decrypted, message, "RSA decryption")
end

-- Test RSA signing and verification
local function test_rsa_signing_verification()
local priv_key, pub_key = crypto.generatekeypair("rsa", 2048)
local message = "Sign this message"
local signature = crypto.sign("rsa", priv_key, message, "sha256")
assert_equal(type(signature), "string", "RSA signing")
local is_valid = crypto.verify("rsa", pub_key, message, signature, "sha256")
assert_equal(is_valid, true, "RSA signature verification")
end

-- Test ECDSA signing and verification
local function test_ecdsa_signing_verification()
local priv_key, pub_key = crypto.generatekeypair("ecdsa", "secp256r1")
local message = "Sign this message with ECDSA"
local signature = crypto.sign("ecdsa", priv_key, message, "sha256")
assert_equal(type(signature), "string", "ECDSA signing")
local is_valid = crypto.verify("ecdsa", pub_key, message, signature, "sha256")
assert_equal(is_valid, true, "ECDSA signature verification")
end

-- Test CSR generation
local function test_csr_generation()
local priv_key, pub_key = crypto.generatekeypair("rsa", 2048)
local subject_name = "CN=example.com,O=Example Org,C=US"
local csr = crypto.generateCsr(priv_key, subject_name)
assert_equal(type(csr), "string", "CSR generation")
end

-- Test PemToJwk conversion
local function test_pem_to_jwk()
local priv_key, pub_key = crypto.generatekeypair("rsa", 2048)
local jwk = crypto.convertPemToJwk(pub_key)
assert_equal(type(jwk), "table", "PEM to JWK conversion")
assert_equal(jwk.kty, "RSA", "JWK key type")
end

-- Run all tests
local function run_tests()
print("Running tests for lcrypto...")
test_rsa_keypair_generation()
test_ecdsa_keypair_generation()
test_rsa_encryption_decryption()
test_rsa_signing_verification()
test_ecdsa_signing_verification()
test_csr_generation()
test_pem_to_jwk()
print("All tests passed!")
end

run_tests()
2 changes: 1 addition & 1 deletion third_party/mbedtls/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
/* eliptic curves */
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
#define MBEDTLS_ECP_DP_SECP521R1_ENABLED
#define MBEDTLS_ECP_DP_CURVE25519_ENABLED
#ifndef TINY
#define MBEDTLS_ECP_DP_CURVE448_ENABLED
/*#define MBEDTLS_ECP_DP_SECP521R1_ENABLED*/
/*#define MBEDTLS_ECP_DP_BP384R1_ENABLED*/
/*#define MBEDTLS_ECP_DP_SECP192R1_ENABLED*/
/*#define MBEDTLS_ECP_DP_SECP224R1_ENABLED*/
Expand Down
1 change: 1 addition & 0 deletions tool/net/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ TOOL_NET_REDBEAN_LUA_MODULES = \
o/$(MODE)/tool/net/lmaxmind.o \
o/$(MODE)/tool/net/lsqlite3.o \
o/$(MODE)/tool/net/largon2.o \
o/$(MODE)/tool/net/lcrypto.o \
o/$(MODE)/tool/net/launch.o

o/$(MODE)/tool/net/redbean.dbg: \
Expand Down
Loading