Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: implement RSA-OAEP (generateKey, import, encrypt, decrypt)

orezraey9ac8a376373a
diff --git a/src/browser/tests/crypto.html b/src/browser/tests/crypto.html
index b9bbe4aba..3880793e7 100644
--- a/src/browser/tests/crypto.html
+++ b/src/browser/tests/crypto.html
@@ -125,6 +125,54 @@
});
</script>
+<script id=rsa-oaep-roundtrip type=module>
+ const state = await testing.async();
+ const { privateKey, publicKey } = await crypto.subtle.generateKey(
+ {
+ name: "RSA-OAEP",
+ modulusLength: 2048,
+ publicExponent: new Uint8Array([1, 0, 1]),
+ hash: "SHA-256",
+ },
+ true,
+ ["encrypt", "decrypt"],
+ );
+
+ const plaintext = new TextEncoder().encode("lightpanda rsa-oaep");
+ const ciphertext = await crypto.subtle.encrypt({ name: "RSA-OAEP" }, publicKey, plaintext);
+ const decrypted = await crypto.subtle.decrypt({ name: "RSA-OAEP" }, privateKey, ciphertext);
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual(true, privateKey instanceof CryptoKey);
+ testing.expectEqual(true, publicKey instanceof CryptoKey);
+ testing.expectEqual(2048, publicKey.algorithm.modulusLength);
+ testing.expectEqual(256, ciphertext.byteLength);
+ testing.expectEqual("lightpanda rsa-oaep", new TextDecoder().decode(decrypted));
+ });
+</script>
+
+<script id=rsa-oaep-import type=module>
+ const state = await testing.async();
+ const fromB64 = (b64) => Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
+ // A throwaway 2048-bit RSA key pair (spki public + pkcs8 private) for the
+ // import path: encrypt with the imported public key, decrypt with the private.
+ const spki = fromB64("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2J6+FUn6SF2o4+6vj6jbsGfumBrlZIS7OTRmw3sJOzvinrDRQ3hf+TmCgBSHcGbohHTItgB8mfM1MMbsIggYjbNSs+bRhVmbtMFS3GxBsrNt+YvaEMF0C3sdYi6+gWZqJYrfEF/a8HT2+IZQJ5B7PsIOoVPWlDkaLQBPlQDgK0JJQJ+1mOI8dXHz9PaPXB1v9/w3nUpRV6bdy9Iui453XnoY8ajIKy0bzk12160PxZM4vAvrvdmTY12BwkGsim+7iqNlLjU6fJKb4D0766YfroGDPhxd3sDqxLteQeA64oIcqeQzDq89843DS1VeGwv+nrwNHCy3cAmLVaytk4vECQIDAQAB");
+ const pkcs8 = fromB64("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDYnr4VSfpIXajj7q+PqNuwZ+6YGuVkhLs5NGbDewk7O+KesNFDeF/5OYKAFIdwZuiEdMi2AHyZ8zUwxuwiCBiNs1Kz5tGFWZu0wVLcbEGys235i9oQwXQLex1iLr6BZmolit8QX9rwdPb4hlAnkHs+wg6hU9aUORotAE+VAOArQklAn7WY4jx1cfP09o9cHW/3/DedSlFXpt3L0i6LjndeehjxqMgrLRvOTXbXrQ/Fkzi8C+u92ZNjXYHCQayKb7uKo2UuNTp8kpvgPTvrph+ugYM+HF3ewOrEu15B4Drighyp5DMOrz3zjcNLVV4bC/6evA0cLLdwCYtVrK2Ti8QJAgMBAAECggEAHW4z1SCf6IURFSawo4wFjQoR2IiO8gE2jqcX7ycgMfQI7haH1+ra3zl5CX3AiPzySkyzmEaopyHr8ByWqGkBLP9YFSke/kcWaJB7eYcbxfOq1WSy0+P0qDqp21UOfPY1JbSgadEcJd8+TrWw3i87D2O+KR0C4lB/xe1s4omj6zxuSMOL3igRtu+pSem3e5Wb/lid2OIAskxgt+RiyRSPsajBunhah4WxhY0NQIt5WK0kiNfpd8vz3qVTnF8t5oPPWLvdHIApDHLOkbeX+0XA5XeJojdPgN/vdInuXYoFp/VF6Jc+nAgCOEDI/+JPQ+Jpoy1FJoB5fZimivPvGbXrIQKBgQDup6Y/+zRYIRCN8VpcUBuvQHLbE17UDTrVrSh8fqPJpAWvNPrYZb+tG23cuPec4FIGoadjwiuFgsXs9lXv0qPxcgTyR6l0RzrDWNmt8HvPRrgEIJEwv+BT+heRUecaXHSvG1vCam06MkjgFLvnCJ7hEVCNf4+irhLCNV+J9TnZ4wKBgQDoXR6WdVCDEHz+PBsfzcINbRxyWLPIJmULcngqpeQ3UyrxWXxXR2BKuVrOZq5Dn9XcmP/zZ9u4eCI2Nst4eLlzQIxXDxsj62Y/5yAc9tw4nzTvxPaKf7orBMrRjYSmj9vOrpYIlVjV+cPRBfjxfRonSDf3kWlUS62GFweeDQo+IwKBgQDiMJ8PEOVhiaUJAvEjgQFcLnctT8SWshtLHZy7scNGf2Cr2EjPBFCQnuiXQ+/bDXhwtJHTqNpa5cD1f9ufO0MKuYgaoPToMgJt1y4o/z45C8uojb1OOO1Cakwf/UJN58bnl8R2FSyE9dfAGaalp7P9mM2B7p6SDu3Uh4WPWY6FBQKBgHINgv4HB0XNygxgJFAwBpfgfp4Bu/z6G47K2QDV/HbLc8CTU9zjQ/hpkpDNBaxbv/VPhBJNYCPjk6dYrfasCxrvC/OPjprK6J1lLPYKlX2VZ75oxzqkBxUmfQbul7XJ1vJT74C3Dau5tMvcSpz98uBlKlNyJGkj3EB8/VguvmBRAoGBAJFSOHauuxGtvw64JnMEkUL9vnjuryyH6CcXgSP6SMY58O2KL/nMEGeAYguwrkl5CdoN2zrWr8/I8cr1UrkKHtzsQHrCYlA6LaZaHDUdxCEIyarO7z18ZXzi32HRol1UFjpQTVg2N9t9rGqC2Hg+hg2D0kGJCwf/t8Uydc2tiTh/");
+ const publicKey = await crypto.subtle.importKey("spki", spki, { name: "RSA-OAEP", hash: "SHA-256" }, false, ["encrypt"]);
+ const privateKey = await crypto.subtle.importKey("pkcs8", pkcs8, { name: "RSA-OAEP", hash: "SHA-256" }, false, ["decrypt"]);
+
+ const message = new TextEncoder().encode("imported key path");
+ const ciphertext = await crypto.subtle.encrypt({ name: "RSA-OAEP" }, publicKey, message);
+ const decrypted = await crypto.subtle.decrypt({ name: "RSA-OAEP" }, privateKey, ciphertext);
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual(2048, publicKey.algorithm.modulusLength);
+ testing.expectEqual("imported key path", new TextDecoder().decode(decrypted));
+ });
+</script>
+
<script id=aes-ctr-counter-wrap type=module>
const state = await testing.async();
// AES-CTR must increment only the rightmost `length` bits of the counter
diff --git a/src/browser/webapi/CryptoKey.zig b/src/browser/webapi/CryptoKey.zig
index 73b30c0b6..7a60b689e 100644
--- a/src/browser/webapi/CryptoKey.zig
+++ b/src/browser/webapi/CryptoKey.zig
@@ -61,6 +61,7 @@ pub const Algorithm = struct {
name: []const u8,
hash: ?[]const u8 = null,
named_curve: ?[]const u8 = null,
+ modulus_length: ?u32 = null,
};
/// https://developer.mozilla.org/en-US/docs/Web/API/CryptoKeyPair
@@ -184,6 +185,7 @@ const AlgorithmReport = union(enum) {
keyed: struct { name: []const u8, length: u32 },
hmac: struct { name: []const u8, length: u32, hash: struct { name: []const u8 } },
ec: struct { name: []const u8, namedCurve: []const u8 },
+ rsa: struct { name: []const u8, modulusLength: u32, hash: struct { name: []const u8 } },
named: struct { name: []const u8 },
};
@@ -200,6 +202,11 @@ pub fn getAlgorithm(self: *const CryptoKey) AlgorithmReport {
.name = self._algorithm.name,
.namedCurve = self._algorithm.named_curve orelse "",
} },
+ .rsa => .{ .rsa = .{
+ .name = self._algorithm.name,
+ .modulusLength = self._algorithm.modulus_length orelse 0,
+ .hash = .{ .name = self._algorithm.hash orelse "" },
+ } },
else => .{ .named = .{ .name = self._algorithm.name } },
};
}
diff --git a/src/browser/webapi/SubtleCrypto.zig b/src/browser/webapi/SubtleCrypto.zig
index 413209b3a..cee3e9886 100644
--- a/src/browser/webapi/SubtleCrypto.zig
+++ b/src/browser/webapi/SubtleCrypto.zig
@@ -58,16 +58,11 @@ pub fn generateKey(
.hmac_key_gen => |params| return HMAC.init(params, extractable, key_usages, exec),
.aes_key_gen => |params| return AES.generate(params, extractable, key_usages, exec),
.ec_key_gen => |params| return EC.generate(params, extractable, key_usages, exec),
- .rsa_hashed_key_gen => |params| {
- try RSA.validate(params, key_usages);
- log.debug(.not_implemented, "generateKey", .{ .name = params.name });
- },
+ .rsa_hashed_key_gen => |params| return RSA.generate(params, extractable, key_usages, exec),
.name => |js_name| return generateKeyFromName(try js_name.toSSO(false), extractable, key_usages, exec),
.object => |object| return generateKeyFromName(try object.name.toSSO(false), extractable, key_usages, exec),
.invalid => return local.typeError("invalid algorithm"),
}
-
- return error.NotSupported;
}
fn generateKeyFromName(
@@ -145,6 +140,22 @@ pub fn importKey(
};
return EC.import(name, algo.namedCurve(), format, der, is_private, extractable, mask, exec);
}
+ if (RSA.canonicalName(name) != null) {
+ const der = switch (key_data) {
+ .bytes => |b| b.values,
+ .jwk => return error.NotSupported,
+ };
+ // RSA-OAEP import params {name, hash} match the `.hmac` variant of the
+ // Import union (same shape: a name plus a hash), so the hash is read there.
+ const hash_name = switch (algo) {
+ .hmac => |h| switch (h.hash) {
+ .string => |s| s,
+ .object => |o| o.name,
+ },
+ else => return local.typeError("RSA-OAEP import requires a hash"),
+ };
+ return RSA.import(format, der, hash_name, is_private, extractable, mask, exec);
+ }
log.debug(.not_implemented, "SubtleCrypto.importKey", .{ .name = name });
return error.NotSupported;
}
@@ -235,6 +246,9 @@ fn asymmetricAllowedUsages(name: []const u8, is_private: bool) ?[]const []const
if (eqlIgnoreCase(name, "ECDH") or eqlIgnoreCase(name, "X25519") or eqlIgnoreCase(name, "X448")) {
return if (is_private) &.{ "deriveKey", "deriveBits" } else &.{};
}
+ if (eqlIgnoreCase(name, "RSA-OAEP")) {
+ return if (is_private) &.{ "decrypt", "unwrapKey" } else &.{ "encrypt", "wrapKey" };
+ }
return null;
}
@@ -425,6 +439,21 @@ fn cryptOp(
exec: *const Execution,
) !js.Promise {
const local = exec.js.local.?;
+ // RSA-OAEP is handled before the switch: the algorithm may arrive as a bare
+ // string ("RSA-OAEP"), which would hit `.name => NotSupported` below. The key
+ // type, not the algorithm's shape, decides. No iv/counter is involved.
+ if (key._type == .rsa) {
+ const rsa_out = blk: {
+ if (encrypting) {
+ if (!key.canEncrypt()) return error.InvalidAccessError;
+ break :blk try RSA.encrypt(key, data, exec);
+ }
+ if (!key.canDecrypt()) return error.InvalidAccessError;
+ break :blk try RSA.decrypt(key, data, exec);
+ };
+ return local.resolvePromise(js.ArrayBuffer{ .values = rsa_out });
+ }
+
const params = switch (algo) {
.params => |p| p,
// A bare string identifier carries no iv/counter, so it can't drive AES.
diff --git a/src/browser/webapi/crypto/RSA.zig b/src/browser/webapi/crypto/RSA.zig
index 27dd49d00..29613cb70 100644
--- a/src/browser/webapi/crypto/RSA.zig
+++ b/src/browser/webapi/crypto/RSA.zig
@@ -16,11 +16,18 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-//! RSA generateKey parameter validation. See AES.zig for the rationale.
+//! RSA-OAEP: key generation, import (spki/pkcs8 via DER), and the
+//! encrypt/decrypt operations. The key material lives as a libcrypto EVP_PKEY
+//! in `CryptoKey._vary.pkey`. See EC.zig for the asymmetric-key rationale.
const std = @import("std");
const algorithm = @import("algorithm.zig");
+const common = @import("common.zig");
+const crypto = @import("../../../sys/libcrypto.zig");
+const js = @import("../../js/js.zig");
+const CryptoKey = @import("../CryptoKey.zig");
+const Execution = js.Execution;
pub fn validate(params: algorithm.Init.RsaHashedKeyGen, key_usages: []const []const u8) !void {
const allowed: []const []const u8 = blk: {
@@ -80,6 +87,197 @@ fn isValidPublicExponent(bytes: []const u8) bool {
return false;
}
+/// The registered name (case-insensitive), or null if not an RSA-OAEP algorithm.
+pub fn canonicalName(name: []const u8) ?[]const u8 {
+ if (eql(name, "RSA-OAEP")) return "RSA-OAEP";
+ return null;
+}
+
+/// The (private, public) usages legal for an RSA-OAEP key pair.
+fn usageSets() struct { private: []const []const u8, public: []const []const u8 } {
+ return .{ .private = &.{ "decrypt", "unwrapKey" }, .public = &.{ "encrypt", "wrapKey" } };
+}
+
+/// Generates an RSA-OAEP key pair of `modulusLength` bits with the given public
+/// exponent. Mirrors EC.generate: each key keeps the EVP_PKEY of its half, and
+/// the requested usages are split across the pair (validate() rejected illegal
+/// ones).
+pub fn generate(
+ params: algorithm.Init.RsaHashedKeyGen,
+ extractable: bool,
+ key_usages: []const []const u8,
+ exec: *const Execution,
+) !js.Promise {
+ const local = exec.js.local.?;
+ try validate(params, key_usages);
+
+ // validate() also accepts RSASSA-PKCS1-v1_5 and RSA-PSS (so their parameter
+ // errors are reported correctly), but only RSA-OAEP generation is implemented.
+ if (!eql(params.name, "RSA-OAEP")) return error.NotSupported;
+
+ // modulusLength must fit the c_int BoringSSL's generator takes; reject an
+ // absurd size here instead of trapping on the @intCast below.
+ if (params.modulusLength > std.math.maxInt(c_int)) return error.OperationError;
+
+ const hash_name = switch (params.hash) {
+ .string => |s| s,
+ .object => |o| o.name,
+ };
+ const sets = usageSets();
+ const private_mask = maskOf(sets.private, key_usages);
+ const public_mask = maskOf(sets.public, key_usages);
+
+ // Public exponent as a big-endian BIGNUM (e.g. 65537 == {0x01, 0x00, 0x01}).
+ const e = crypto.BN_new() orelse return error.OutOfMemory;
+ defer crypto.BN_free(e);
+ const exp = params.publicExponent.values;
+ if (crypto.BN_bin2bn(exp.ptr, exp.len, e) == null) return error.OperationError;
+
+ const rsa = crypto.RSA_new() orelse return error.OutOfMemory;
+ defer crypto.RSA_free(rsa);
+ if (crypto.RSA_generate_key_ex(rsa, @intCast(params.modulusLength), e, null) != 1) {
+ return error.OperationError;
+ }
+
+ const private_pkey = crypto.EVP_PKEY_new() orelse return error.OutOfMemory;
+ errdefer crypto.EVP_PKEY_free(private_pkey);
+ if (crypto.EVP_PKEY_set1_RSA(private_pkey, rsa) != 1) return error.OutOfMemory;
+
+ // An independent public-only key for the other half of the pair, so freeing
+ // one CryptoKey never touches the other's EVP_PKEY.
+ const rsa_pub = crypto.RSAPublicKey_dup(rsa) orelse return error.OutOfMemory;
+ defer crypto.RSA_free(rsa_pub);
+ const public_pkey = crypto.EVP_PKEY_new() orelse return error.OutOfMemory;
+ errdefer crypto.EVP_PKEY_free(public_pkey);
+ if (crypto.EVP_PKEY_set1_RSA(public_pkey, rsa_pub) != 1) return error.OutOfMemory;
+
+ const modulus_length: u32 = @intCast(crypto.EVP_PKEY_bits(private_pkey));
+
+ const private = try CryptoKey.init(exec, .{
+ ._type = .rsa,
+ ._kind = .private,
+ ._extractable = extractable,
+ ._usages = private_mask,
+ ._key = &.{},
+ ._algorithm = .{ .name = "RSA-OAEP", .hash = hash_name, .modulus_length = modulus_length },
+ ._vary = .{ .pkey = private_pkey },
+ });
+
+ const public = try CryptoKey.init(exec, .{
+ ._type = .rsa,
+ ._kind = .public,
+ // Public keys are always extractable.
+ ._extractable = true,
+ ._usages = public_mask,
+ ._key = &.{},
+ ._algorithm = .{ .name = "RSA-OAEP", .hash = hash_name, .modulus_length = modulus_length },
+ ._vary = .{ .pkey = public_pkey },
+ });
+
+ return local.resolvePromise(CryptoKey.Pair{ .privateKey = private, .publicKey = public });
+}
+
+/// Imports an RSA-OAEP key from DER: `spki` (public) or `pkcs8` (private).
+/// jwk/raw aren't handled yet. libcrypto parses the DER, so a malformed
+/// structure is DataError. `hash_name` is the OAEP digest, kept for encrypt.
+pub fn import(
+ format: []const u8,
+ der: []const u8,
+ hash_name: []const u8,
+ is_private: bool,
+ extractable: bool,
+ usages_mask: u8,
+ exec: *const Execution,
+) !js.Promise {
+ const local = exec.js.local.?;
+
+ // Reject an unsupported OAEP digest at import time (findDigest knows the set),
+ // rather than letting it surface only on the first encrypt/decrypt.
+ _ = crypto.findDigest(hash_name) catch return error.NotSupported;
+
+ var ptr: [*c]const u8 = der.ptr;
+ const pkey: *crypto.EVP_PKEY = blk: {
+ if (std.mem.eql(u8, format, "spki")) {
+ break :blk crypto.d2i_PUBKEY(null, &ptr, @intCast(der.len)) orelse return error.DataError;
+ }
+ if (std.mem.eql(u8, format, "pkcs8")) {
+ break :blk crypto.d2i_AutoPrivateKey(null, &ptr, @intCast(der.len)) orelse return error.DataError;
+ }
+ // jwk / raw not implemented yet.
+ return error.NotSupported;
+ };
+ errdefer crypto.EVP_PKEY_free(pkey);
+
+ if (crypto.EVP_PKEY_id(pkey) != crypto.EVP_PKEY_RSA) return error.DataError;
+
+ const crypto_key = try CryptoKey.init(exec, .{
+ ._type = .rsa,
+ ._kind = if (is_private) .private else .public,
+ ._extractable = extractable,
+ ._usages = usages_mask,
+ ._key = &.{},
+ ._algorithm = .{ .name = "RSA-OAEP", .hash = hash_name, .modulus_length = @intCast(crypto.EVP_PKEY_bits(pkey)) },
+ ._vary = .{ .pkey = pkey },
+ });
+ return local.resolvePromise(crypto_key);
+}
+
+/// Encrypts `data` with the public key (RSA-OAEP).
+pub fn encrypt(key: *const CryptoKey, data: []const u8, exec: *const Execution) ![]const u8 {
+ return crypt(key, data, true, exec);
+}
+
+/// Decrypts `data` with the private key (RSA-OAEP).
+pub fn decrypt(key: *const CryptoKey, data: []const u8, exec: *const Execution) ![]const u8 {
+ return crypt(key, data, false, exec);
+}
+
+/// Shared RSA-OAEP transform. `encrypting` selects the init/op pair; the
+/// padding, OAEP digest (the hash stored on the key), MGF1 digest and the
+/// two-call output sizing are identical for both directions.
+fn crypt(key: *const CryptoKey, data: []const u8, encrypting: bool, exec: *const Execution) ![]const u8 {
+ const hash_name = key._algorithm.hash orelse return error.OperationError;
+ const md = crypto.findDigest(hash_name) catch return error.OperationError;
+
+ const ctx = crypto.EVP_PKEY_CTX_new(key.getKeyObject(), null) orelse return error.OperationError;
+ defer crypto.EVP_PKEY_CTX_free(ctx);
+
+ const init_ok = if (encrypting) crypto.EVP_PKEY_encrypt_init(ctx) else crypto.EVP_PKEY_decrypt_init(ctx);
+ if (init_ok != 1) return error.OperationError;
+ if (crypto.EVP_PKEY_CTX_set_rsa_padding(ctx, crypto.RSA_PKCS1_OAEP_PADDING) != 1) return error.OperationError;
+ if (crypto.EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md) != 1) return error.OperationError;
+ if (crypto.EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md) != 1) return error.OperationError;
+
+ // First call (null output) reports the size; the second writes the buffer.
+ var out_len: usize = 0;
+ if (encrypting) {
+ if (crypto.EVP_PKEY_encrypt(ctx, null, &out_len, data.ptr, data.len) != 1) return error.OperationError;
+ } else {
+ if (crypto.EVP_PKEY_decrypt(ctx, null, &out_len, data.ptr, data.len) != 1) return error.OperationError;
+ }
+ const out = try exec.local_arena.alloc(u8, out_len);
+ if (encrypting) {
+ if (crypto.EVP_PKEY_encrypt(ctx, out.ptr, &out_len, data.ptr, data.len) != 1) return error.OperationError;
+ } else {
+ if (crypto.EVP_PKEY_decrypt(ctx, out.ptr, &out_len, data.ptr, data.len) != 1) return error.OperationError;
+ }
+ return out[0..out_len];
+}
+
+/// The usage bitmask of those `usages` that belong to `set`.
+fn maskOf(set: []const []const u8, usages: []const []const u8) u8 {
+ var mask: u8 = 0;
+ for (usages) |u| {
+ for (set) |s| {
+ if (std.mem.eql(u8, s, u)) {
+ mask |= common.usageBit(u).?;
+ break;
+ }
+ }
+ }
+ return mask;
+}
+
fn eql(a: []const u8, b: []const u8) bool {
return std.ascii.eqlIgnoreCase(a, b);
}
diff --git a/src/sys/libcrypto.zig b/src/sys/libcrypto.zig
index 37c3c9cf2..b255091ba 100644
--- a/src/sys/libcrypto.zig
+++ b/src/sys/libcrypto.zig
@@ -125,6 +125,7 @@ pub const NID_secp521r1 = 716; // P-521
pub extern fn EVP_PKEY_new() ?*EVP_PKEY;
pub extern fn EVP_PKEY_id(pkey: *const EVP_PKEY) c_int;
+pub extern fn EVP_PKEY_bits(pkey: *const EVP_PKEY) c_int;
pub extern fn EVP_PKEY_set1_EC_KEY(pkey: *EVP_PKEY, key: *EC_KEY) c_int;
pub extern fn EC_KEY_new_by_curve_name(nid: c_int) ?*EC_KEY;
@@ -137,6 +138,33 @@ pub extern fn EC_KEY_set_public_key(key: *EC_KEY, point: *const EC_POINT) c_int;
pub extern fn d2i_PUBKEY(out: ?*?*EVP_PKEY, inp: *[*c]const u8, len: c_long) ?*EVP_PKEY;
pub extern fn d2i_AutoPrivateKey(out: ?*?*EVP_PKEY, inp: *[*c]const u8, len: c_long) ?*EVP_PKEY;
+// RSA-OAEP: key generation, import, and the encrypt/decrypt operations.
+pub const EVP_PKEY_RSA = 6; // NID_rsaEncryption
+pub const RSA_PKCS1_OAEP_PADDING = 4;
+
+const struct_rsa_st = opaque {};
+pub const RSA = struct_rsa_st;
+const struct_bignum_st = opaque {};
+pub const BIGNUM = struct_bignum_st;
+
+pub extern fn RSA_new() ?*RSA;
+pub extern fn RSA_free(rsa: ?*RSA) void;
+pub extern fn RSA_generate_key_ex(rsa: *RSA, bits: c_int, e: *const BIGNUM, cb: ?*anyopaque) c_int;
+pub extern fn RSAPublicKey_dup(rsa: *const RSA) ?*RSA;
+pub extern fn EVP_PKEY_set1_RSA(pkey: *EVP_PKEY, key: *RSA) c_int;
+
+pub extern fn BN_new() ?*BIGNUM;
+pub extern fn BN_free(bn: ?*BIGNUM) void;
+pub extern fn BN_bin2bn(in: [*c]const u8, len: usize, ret: ?*BIGNUM) ?*BIGNUM;
+
+pub extern fn EVP_PKEY_encrypt_init(ctx: ?*EVP_PKEY_CTX) c_int;
+pub extern fn EVP_PKEY_encrypt(ctx: ?*EVP_PKEY_CTX, out: [*c]u8, out_len: *usize, in: [*c]const u8, in_len: usize) c_int;
+pub extern fn EVP_PKEY_decrypt_init(ctx: ?*EVP_PKEY_CTX) c_int;
+pub extern fn EVP_PKEY_decrypt(ctx: ?*EVP_PKEY_CTX, out: [*c]u8, out_len: *usize, in: [*c]const u8, in_len: usize) c_int;
+pub extern fn EVP_PKEY_CTX_set_rsa_padding(ctx: ?*EVP_PKEY_CTX, padding: c_int) c_int;
+pub extern fn EVP_PKEY_CTX_set_rsa_oaep_md(ctx: ?*EVP_PKEY_CTX, md: *const EVP_MD) c_int;
+pub extern fn EVP_PKEY_CTX_set_rsa_mgf1_md(ctx: ?*EVP_PKEY_CTX, md: *const EVP_MD) c_int;
+
const NID_X25519 = @as(c_int, 948);
pub const EVP_PKEY_X25519 = NID_X25519;
const NID_ED25519 = 949;