Current section

Files

Jump to
gossamer src gossamer subtle_crypto.ffi.mjs
Raw

src/gossamer/subtle_crypto.ffi.mjs

// src/gossamer/crypto_key_pair.ts
import * as $pair from "./crypto_key_pair.mjs";
function toCryptoKeyPair(pair) {
return $pair.CryptoKeyPair$CryptoKeyPair(pair.publicKey, pair.privateKey);
}
// src/gossamer/key_format.ts
import * as $keyFormat from "./key_format.mjs";
function toKeyFormat(value) {
if ($keyFormat.KeyFormat$isPkcs8(value)) return "pkcs8";
if ($keyFormat.KeyFormat$isRaw(value)) return "raw";
return "spki";
}
// src/gossamer/key_usage.ts
import * as $keyUsage from "./key_usage.mjs";
// src/utils/list.ts
import {
List$Empty,
List$isNonEmpty,
List$NonEmpty,
List$NonEmpty$first,
List$NonEmpty$rest
} from "../../prelude.mjs";
function toArray(list) {
const array = [];
let current = list;
while (List$isNonEmpty(current)) {
array.push(List$NonEmpty$first(current));
current = List$NonEmpty$rest(current);
}
return array;
}
// src/gossamer/key_usage.ts
function toKeyUsage(value) {
if ($keyUsage.KeyUsage$isDecrypt(value)) return "decrypt";
if ($keyUsage.KeyUsage$isDeriveBits(value)) return "deriveBits";
if ($keyUsage.KeyUsage$isDeriveKey(value)) return "deriveKey";
if ($keyUsage.KeyUsage$isEncrypt(value)) return "encrypt";
if ($keyUsage.KeyUsage$isSign(value)) return "sign";
if ($keyUsage.KeyUsage$isUnwrapKey(value)) return "unwrapKey";
if ($keyUsage.KeyUsage$isVerify(value)) return "verify";
return "wrapKey";
}
function toKeyUsageArray(usages) {
return toArray(usages).map(toKeyUsage);
}
// src/gossamer/subtle_crypto/derive_algorithm.ts
import * as $alg from "./subtle_crypto/derive_algorithm.mjs";
function toDeriveAlgorithm(algorithm) {
if ($alg.DeriveAlgorithm$isName(algorithm)) {
return $alg.DeriveAlgorithm$Name$0(algorithm);
}
if ($alg.DeriveAlgorithm$isHkdf(algorithm)) {
return {
name: "HKDF",
hash: $alg.DeriveAlgorithm$Hkdf$hash(algorithm),
info: $alg.DeriveAlgorithm$Hkdf$info(
algorithm
),
salt: $alg.DeriveAlgorithm$Hkdf$salt(
algorithm
)
};
}
if ($alg.DeriveAlgorithm$isPbkdf2(algorithm)) {
return {
name: "PBKDF2",
hash: $alg.DeriveAlgorithm$Pbkdf2$hash(algorithm),
iterations: $alg.DeriveAlgorithm$Pbkdf2$iterations(algorithm),
salt: $alg.DeriveAlgorithm$Pbkdf2$salt(
algorithm
)
};
}
return {
name: "ECDH",
public: $alg.DeriveAlgorithm$Ecdh$public(algorithm)
};
}
// src/gossamer/subtle_crypto/derived_key_type.ts
import * as $type from "./subtle_crypto/derived_key_type.mjs";
function toDerivedKeyType(derivedKeyType) {
if ($type.DerivedKeyType$isName(derivedKeyType)) {
return $type.DerivedKeyType$Name$0(derivedKeyType);
}
if ($type.DerivedKeyType$isAesDerived(derivedKeyType)) {
return {
name: $type.DerivedKeyType$AesDerived$name(derivedKeyType),
length: $type.DerivedKeyType$AesDerived$length(derivedKeyType)
};
}
return {
name: "HMAC",
hash: $type.DerivedKeyType$HmacDerived$hash(derivedKeyType)
};
}
// src/gossamer/subtle_crypto/encrypt_algorithm.ts
import * as $alg2 from "./subtle_crypto/encrypt_algorithm.mjs";
function toEncryptAlgorithm(algorithm) {
if ($alg2.EncryptAlgorithm$isName(algorithm)) {
return $alg2.EncryptAlgorithm$Name$0(algorithm);
}
if ($alg2.EncryptAlgorithm$isAesCbc(algorithm)) {
return {
name: "AES-CBC",
iv: $alg2.EncryptAlgorithm$AesCbc$iv(algorithm)
};
}
if ($alg2.EncryptAlgorithm$isAesGcm(algorithm)) {
return {
name: "AES-GCM",
iv: $alg2.EncryptAlgorithm$AesGcm$iv(algorithm)
};
}
if ($alg2.EncryptAlgorithm$isAesGcmWith(algorithm)) {
return {
name: "AES-GCM",
iv: $alg2.EncryptAlgorithm$AesGcmWith$iv(
algorithm
),
additionalData: $alg2.EncryptAlgorithm$AesGcmWith$additional_data(
algorithm
),
tagLength: $alg2.EncryptAlgorithm$AesGcmWith$tag_length(algorithm)
};
}
if ($alg2.EncryptAlgorithm$isAesCtr(algorithm)) {
return {
name: "AES-CTR",
counter: $alg2.EncryptAlgorithm$AesCtr$counter(
algorithm
),
length: $alg2.EncryptAlgorithm$AesCtr$length(algorithm)
};
}
if ($alg2.EncryptAlgorithm$isRsaOaepWith(algorithm)) {
return {
name: "RSA-OAEP",
label: $alg2.EncryptAlgorithm$RsaOaepWith$label(
algorithm
)
};
}
return { name: "RSA-OAEP" };
}
// src/gossamer/subtle_crypto/import_algorithm.ts
import * as $alg3 from "./subtle_crypto/import_algorithm.mjs";
function toImportAlgorithm(algorithm) {
if ($alg3.ImportAlgorithm$isName(algorithm)) {
return $alg3.ImportAlgorithm$Name$0(algorithm);
}
if ($alg3.ImportAlgorithm$isHmacImport(algorithm)) {
return {
name: "HMAC",
hash: $alg3.ImportAlgorithm$HmacImport$hash(algorithm)
};
}
if ($alg3.ImportAlgorithm$isRsaHashedImport(algorithm)) {
return {
name: $alg3.ImportAlgorithm$RsaHashedImport$name(algorithm),
hash: $alg3.ImportAlgorithm$RsaHashedImport$hash(algorithm)
};
}
return {
name: $alg3.ImportAlgorithm$EcImport$name(algorithm),
namedCurve: $alg3.ImportAlgorithm$EcImport$named_curve(algorithm)
};
}
// src/gossamer/subtle_crypto/wrap_algorithm.ts
import * as $alg4 from "./subtle_crypto/wrap_algorithm.mjs";
function toWrapAlgorithm(algorithm) {
if ($alg4.WrapAlgorithm$isName(algorithm)) {
return $alg4.WrapAlgorithm$Name$0(algorithm);
}
if ($alg4.WrapAlgorithm$isAesCbc(algorithm)) {
return {
name: "AES-CBC",
iv: $alg4.WrapAlgorithm$AesCbc$iv(algorithm)
};
}
if ($alg4.WrapAlgorithm$isAesCtr(algorithm)) {
return {
name: "AES-CTR",
counter: $alg4.WrapAlgorithm$AesCtr$counter(
algorithm
),
length: $alg4.WrapAlgorithm$AesCtr$length(algorithm)
};
}
if ($alg4.WrapAlgorithm$isRsaOaepWith(algorithm)) {
return {
name: "RSA-OAEP",
label: $alg4.WrapAlgorithm$RsaOaepWith$label(
algorithm
)
};
}
return { name: "RSA-OAEP" };
}
// src/gossamer/subtle_crypto/key_gen_algorithm.ts
import * as $alg5 from "./subtle_crypto/key_gen_algorithm.mjs";
function toKeyGenAlgorithm(algorithm) {
if ($alg5.KeyGenAlgorithm$isAes(algorithm)) {
return {
name: $alg5.KeyGenAlgorithm$Aes$name(algorithm),
length: $alg5.KeyGenAlgorithm$Aes$length(algorithm)
};
}
return {
name: "HMAC",
hash: $alg5.KeyGenAlgorithm$HmacGen$hash(algorithm)
};
}
// src/gossamer/subtle_crypto/key_pair_gen_algorithm.ts
import * as $alg6 from "./subtle_crypto/key_pair_gen_algorithm.mjs";
function toKeyPairGenAlgorithm(algorithm) {
if ($alg6.KeyPairGenAlgorithm$isRsa(algorithm)) {
return {
name: $alg6.KeyPairGenAlgorithm$Rsa$name(algorithm),
modulusLength: $alg6.KeyPairGenAlgorithm$Rsa$modulus_length(algorithm),
publicExponent: $alg6.KeyPairGenAlgorithm$Rsa$public_exponent(
algorithm
),
hash: $alg6.KeyPairGenAlgorithm$Rsa$hash(algorithm)
};
}
return {
name: $alg6.KeyPairGenAlgorithm$Ec$name(algorithm),
namedCurve: $alg6.KeyPairGenAlgorithm$Ec$named_curve(algorithm)
};
}
// src/gossamer/subtle_crypto/sign_algorithm.ts
import * as $alg7 from "./subtle_crypto/sign_algorithm.mjs";
function toSignAlgorithm(algorithm) {
if ($alg7.SignAlgorithm$isName(algorithm)) {
return $alg7.SignAlgorithm$Name$0(algorithm);
}
if ($alg7.SignAlgorithm$isHmac(algorithm)) return "HMAC";
if ($alg7.SignAlgorithm$isRsassaPkcs1V15(algorithm)) {
return "RSASSA-PKCS1-v1_5";
}
if ($alg7.SignAlgorithm$isRsaPss(algorithm)) {
return {
name: "RSA-PSS",
saltLength: $alg7.SignAlgorithm$RsaPss$salt_length(algorithm)
};
}
return {
name: "ECDSA",
hash: $alg7.SignAlgorithm$Ecdsa$hash(algorithm)
};
}
// src/utils/result.ts
import { Result$Error, Result$Ok } from "../../prelude.mjs";
function toResult(value) {
return value === null || value === void 0 ? Result$Error(void 0) : Result$Ok(value);
}
toResult.fromThrows = function(throws) {
try {
return Result$Ok(throws());
} catch (error) {
return Result$Error(
error instanceof Error ? error.message : String(error)
);
}
};
toResult.fromPromise = function(promise) {
return promise.then(
(value) => Result$Ok(value),
(error) => Result$Error(error instanceof Error ? error.message : String(error))
);
};
// src/gossamer/subtle_crypto.ffi.ts
var subtle = globalThis.crypto.subtle;
var digest = (algorithm, data) => {
return toResult.fromPromise(
subtle.digest(algorithm, data)
);
};
var encrypt = (algorithm, key, data) => {
return toResult.fromPromise(
subtle.encrypt(
toEncryptAlgorithm(algorithm),
key,
data
)
);
};
var decrypt = (algorithm, key, data) => {
return toResult.fromPromise(
subtle.decrypt(
toEncryptAlgorithm(algorithm),
key,
data
)
);
};
var sign = (algorithm, key, data) => {
return toResult.fromPromise(
subtle.sign(
toSignAlgorithm(algorithm),
key,
data
)
);
};
var verify = (algorithm, key, signature, data) => {
return toResult.fromPromise(
subtle.verify(
toSignAlgorithm(algorithm),
key,
signature,
data
)
);
};
var generate_key = (algorithm, extractable, usages) => {
return toResult.fromPromise(
subtle.generateKey(
toKeyGenAlgorithm(algorithm),
extractable,
toKeyUsageArray(usages)
)
);
};
var generate_key_pair = (algorithm, extractable, usages) => {
return toResult.fromPromise(
subtle.generateKey(
toKeyPairGenAlgorithm(algorithm),
extractable,
toKeyUsageArray(usages)
).then(toCryptoKeyPair)
);
};
var import_key = (format, keyData, algorithm, extractable, usages) => {
return toResult.fromPromise(
subtle.importKey(
toKeyFormat(format),
keyData,
toImportAlgorithm(algorithm),
extractable,
toKeyUsageArray(usages)
)
);
};
var import_key_jwk = (keyData, algorithm, extractable, usages) => {
return toResult.fromPromise(
subtle.importKey(
"jwk",
keyData,
toImportAlgorithm(algorithm),
extractable,
toKeyUsageArray(usages)
)
);
};
var export_key = (format, key) => {
return toResult.fromPromise(
subtle.exportKey(toKeyFormat(format), key)
);
};
var export_key_jwk = (key) => {
return toResult.fromPromise(
subtle.exportKey("jwk", key)
);
};
var derive_bits = (algorithm, baseKey, length) => {
return toResult.fromPromise(
subtle.deriveBits(toDeriveAlgorithm(algorithm), baseKey, length)
);
};
var derive_key = (algorithm, baseKey, derivedKeyType, extractable, usages) => {
return toResult.fromPromise(
subtle.deriveKey(
toDeriveAlgorithm(algorithm),
baseKey,
toDerivedKeyType(derivedKeyType),
extractable,
toKeyUsageArray(usages)
)
);
};
var wrap_key = (format, key, wrappingKey, algorithm) => {
return toResult.fromPromise(
subtle.wrapKey(
toKeyFormat(format),
key,
wrappingKey,
toWrapAlgorithm(algorithm)
)
);
};
var wrap_key_jwk = (key, wrappingKey, algorithm) => {
return toResult.fromPromise(
subtle.wrapKey("jwk", key, wrappingKey, toWrapAlgorithm(algorithm))
);
};
var unwrap_key = (format, wrappedKey, unwrappingKey, unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, usages) => {
return toResult.fromPromise(
subtle.unwrapKey(
toKeyFormat(format),
wrappedKey,
unwrappingKey,
toWrapAlgorithm(unwrapAlgorithm),
toImportAlgorithm(unwrappedKeyAlgorithm),
extractable,
toKeyUsageArray(usages)
)
);
};
var unwrap_key_jwk = (wrappedKey, unwrappingKey, unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, usages) => {
return toResult.fromPromise(
subtle.unwrapKey(
"jwk",
wrappedKey,
unwrappingKey,
toWrapAlgorithm(unwrapAlgorithm),
toImportAlgorithm(unwrappedKeyAlgorithm),
extractable,
toKeyUsageArray(usages)
)
);
};
export {
decrypt,
derive_bits,
derive_key,
digest,
encrypt,
export_key,
export_key_jwk,
generate_key,
generate_key_pair,
import_key,
import_key_jwk,
sign,
unwrap_key,
unwrap_key_jwk,
verify,
wrap_key,
wrap_key_jwk
};