Add bounded structured crypto operations

This commit is contained in:
udo
2026-07-22 13:43:21 +00:00
parent 68b73343a2
commit 3d155203bd
15 changed files with 564 additions and 0 deletions
+1
View File
@@ -14,6 +14,7 @@ hmac_sha256
hmac_sha256_hex
random_bytes
crypto_equal
crypto_operation
password_hash
password_verify
password_needs_rehash
+28
View File
@@ -0,0 +1,28 @@
:sig
DValue crypto_operation(DValue request)
:params
request : structured operation, algorithm, and operation-specific fields
return value : map with ok, bounded error code, and operation-specific output
:content
Runs one explicitly supported structured asymmetric cryptographic operation. The initial allowlist is `key_generate` with `ES256` and `jwt_sign` with `ES256`. Unknown operations and algorithms fail closed.
ES256 signing validates that `x`, `y`, and `d` form one P-256 key, forces the protected `alg` to `ES256`, and emits a compact JWT with a 64-byte JOSE signature. Requests, nesting, values, and output are bounded. Header and claims roots must be JSON objects containing valid UTF-8 without raw control bytes.
This function does not replace typed digest, HMAC, password, randomness, or constant-time comparison APIs. It exposes no raw signing, arbitrary curve/digest selection, encryption, or generic OpenSSL access. Keep returned private JWKs secret.
:example
DValue key_request;
key_request["operation"] = "key_generate";
key_request["algorithm"] = "ES256";
DValue key = crypto_operation(key_request);
DValue sign_request;
sign_request["operation"] = "jwt_sign";
sign_request["algorithm"] = "ES256";
sign_request["private_jwk"] = key["private_jwk"];
sign_request["protected_header"]["typ"] = "JWT";
sign_request["claims"]["iss"] = "https://client.example";
DValue signed_jwt = crypto_operation(sign_request);
print(signed_jwt["ok"].to_bool() ? "signed" : "failed", "\n");