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/json_web_key.ts
import * as $jsonWebKey from "./json_web_key.mjs";
import * as $option from "../../gleam_stdlib/gleam/option.mjs";
// 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 fromArrayMapped(array, map) {
let list = List$Empty();
for (let i = array.length - 1; i >= 0; --i) {
list = List$NonEmpty(map(array[i], i), list);
}
return list;
}
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";
if ($keyUsage.KeyUsage$isOther(value)) {
return $keyUsage.KeyUsage$Other$0(value);
}
return "wrapKey";
}
function fromKeyUsage(value) {
switch (value) {
case "decrypt":
return $keyUsage.KeyUsage$Decrypt();
case "deriveBits":
return $keyUsage.KeyUsage$DeriveBits();
case "deriveKey":
return $keyUsage.KeyUsage$DeriveKey();
case "encrypt":
return $keyUsage.KeyUsage$Encrypt();
case "sign":
return $keyUsage.KeyUsage$Sign();
case "unwrapKey":
return $keyUsage.KeyUsage$UnwrapKey();
case "verify":
return $keyUsage.KeyUsage$Verify();
case "wrapKey":
return $keyUsage.KeyUsage$WrapKey();
default:
return $keyUsage.KeyUsage$Other(value);
}
}
function toKeyUsageArray(usages) {
return toArray(usages).map(toKeyUsage);
}
// src/utils/option.ts
import {
Option$None,
Option$Some
} from "../../gleam_stdlib/gleam/option.mjs";
function toOption(value) {
return value === null || value === void 0 ? Option$None() : Option$Some(value);
}
// src/gossamer/json_web_key.ts
function unwrap(option) {
return $option.Option$isSome(option) ? $option.Option$Some$0(option) : void 0;
}
function toJsonWebKey(jwk) {
const result = {};
const kty = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$kty(jwk));
if (kty !== void 0) result.kty = kty;
const use_ = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$use_(jwk));
if (use_ !== void 0) result.use = use_;
const keyOps = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$key_ops(jwk));
if (keyOps !== void 0) {
result.key_ops = toKeyUsageArray(keyOps);
}
const alg = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$alg(jwk));
if (alg !== void 0) result.alg = alg;
const ext = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$ext(jwk));
if (ext !== void 0) result.ext = ext;
const crv = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$crv(jwk));
if (crv !== void 0) result.crv = crv;
const x = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$x(jwk));
if (x !== void 0) result.x = x;
const y = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$y(jwk));
if (y !== void 0) result.y = y;
const d = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$d(jwk));
if (d !== void 0) result.d = d;
const n = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$n(jwk));
if (n !== void 0) result.n = n;
const e = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$e(jwk));
if (e !== void 0) result.e = e;
const p = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$p(jwk));
if (p !== void 0) result.p = p;
const q = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$q(jwk));
if (q !== void 0) result.q = q;
const dp = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$dp(jwk));
if (dp !== void 0) result.dp = dp;
const dq = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$dq(jwk));
if (dq !== void 0) result.dq = dq;
const qi = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$qi(jwk));
if (qi !== void 0) result.qi = qi;
const k = unwrap($jsonWebKey.JsonWebKey$JsonWebKey$k(jwk));
if (k !== void 0) result.k = k;
return result;
}
function fromJsonWebKey(jwk) {
return $jsonWebKey.JsonWebKey$JsonWebKey(
toOption(jwk.kty),
toOption(jwk.use),
jwk.key_ops !== void 0 ? $option.Option$Some(
fromArrayMapped(jwk.key_ops, fromKeyUsage)
) : $option.Option$None(),
toOption(jwk.alg),
toOption(jwk.ext),
toOption(jwk.crv),
toOption(jwk.x),
toOption(jwk.y),
toOption(jwk.d),
toOption(jwk.n),
toOption(jwk.e),
toOption(jwk.p),
toOption(jwk.q),
toOption(jwk.dp),
toOption(jwk.dq),
toOption(jwk.qi),
toOption(jwk.k)
);
}
// 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";
if ($keyFormat.KeyFormat$isOther(value)) {
return $keyFormat.KeyFormat$Other$0(value);
}
return "spki";
}
// src/gossamer/hash_algorithm.ts
import * as $ha from "./hash_algorithm.mjs";
function toHashAlgorithm(value) {
if ($ha.HashAlgorithm$isSha1(value)) return "SHA-1";
if ($ha.HashAlgorithm$isSha256(value)) return "SHA-256";
if ($ha.HashAlgorithm$isSha384(value)) return "SHA-384";
if ($ha.HashAlgorithm$isOther(value)) return $ha.HashAlgorithm$Other$0(value);
return "SHA-512";
}
// src/gossamer/subtle_crypto/derive_algorithm.ts
import * as $alg from "./subtle_crypto/derive_algorithm.mjs";
function toDeriveAlgorithm(algorithm) {
if ($alg.DeriveAlgorithm$isOther(algorithm)) {
return $alg.DeriveAlgorithm$Other$0(algorithm);
}
if ($alg.DeriveAlgorithm$isHkdf(algorithm)) {
return {
name: "HKDF",
hash: toHashAlgorithm($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: toHashAlgorithm($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";
// src/gossamer/aes_algorithm.ts
import * as $aa from "./aes_algorithm.mjs";
function toAesAlgorithm(value) {
if ($aa.AesAlgorithm$isAesCbc(value)) return "AES-CBC";
if ($aa.AesAlgorithm$isAesCtr(value)) return "AES-CTR";
if ($aa.AesAlgorithm$isAesKw(value)) return "AES-KW";
if ($aa.AesAlgorithm$isOther(value)) return $aa.AesAlgorithm$Other$0(value);
return "AES-GCM";
}
// src/gossamer/subtle_crypto/derived_key_type.ts
function toDerivedKeyType(derivedKeyType) {
if ($type.DerivedKeyType$isOther(derivedKeyType)) {
return $type.DerivedKeyType$Other$0(derivedKeyType);
}
if ($type.DerivedKeyType$isAesDerived(derivedKeyType)) {
return {
name: toAesAlgorithm(
$type.DerivedKeyType$AesDerived$name(derivedKeyType)
),
length: $type.DerivedKeyType$AesDerived$length(derivedKeyType)
};
}
return {
name: "HMAC",
hash: toHashAlgorithm(
$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$isOther(algorithm)) {
return $alg2.EncryptAlgorithm$Other$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";
// src/gossamer/ec_algorithm.ts
import * as $ea from "./ec_algorithm.mjs";
function toEcAlgorithm(value) {
if ($ea.EcAlgorithm$isEcdh(value)) return "ECDH";
if ($ea.EcAlgorithm$isOther(value)) return $ea.EcAlgorithm$Other$0(value);
return "ECDSA";
}
// src/gossamer/named_curve.ts
import * as $nc from "./named_curve.mjs";
function toNamedCurve(value) {
if ($nc.NamedCurve$isP256(value)) return "P-256";
if ($nc.NamedCurve$isP384(value)) return "P-384";
if ($nc.NamedCurve$isOther(value)) return $nc.NamedCurve$Other$0(value);
return "P-521";
}
// src/gossamer/rsa_algorithm.ts
import * as $ra from "./rsa_algorithm.mjs";
function toRsaAlgorithm(value) {
if ($ra.RsaAlgorithm$isRsaOaep(value)) return "RSA-OAEP";
if ($ra.RsaAlgorithm$isRsaPss(value)) return "RSA-PSS";
if ($ra.RsaAlgorithm$isOther(value)) {
return $ra.RsaAlgorithm$Other$0(value);
}
return "RSASSA-PKCS1-v1_5";
}
// src/gossamer/subtle_crypto/import_algorithm.ts
function toImportAlgorithm(algorithm) {
if ($alg3.ImportAlgorithm$isOther(algorithm)) {
return $alg3.ImportAlgorithm$Other$0(algorithm);
}
if ($alg3.ImportAlgorithm$isHmacImport(algorithm)) {
return {
name: "HMAC",
hash: toHashAlgorithm($alg3.ImportAlgorithm$HmacImport$hash(algorithm))
};
}
if ($alg3.ImportAlgorithm$isRsaHashedImport(algorithm)) {
return {
name: toRsaAlgorithm(
$alg3.ImportAlgorithm$RsaHashedImport$name(algorithm)
),
hash: toHashAlgorithm(
$alg3.ImportAlgorithm$RsaHashedImport$hash(algorithm)
)
};
}
return {
name: toEcAlgorithm($alg3.ImportAlgorithm$EcImport$name(algorithm)),
namedCurve: toNamedCurve(
$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$isOther(algorithm)) {
return $alg4.WrapAlgorithm$Other$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$isOther(algorithm)) {
return $alg5.KeyGenAlgorithm$Other$0(algorithm);
}
if ($alg5.KeyGenAlgorithm$isAes(algorithm)) {
return {
name: toAesAlgorithm($alg5.KeyGenAlgorithm$Aes$name(algorithm)),
length: $alg5.KeyGenAlgorithm$Aes$length(algorithm)
};
}
return {
name: "HMAC",
hash: toHashAlgorithm($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$isOther(algorithm)) {
return $alg6.KeyPairGenAlgorithm$Other$0(algorithm);
}
if ($alg6.KeyPairGenAlgorithm$isRsa(algorithm)) {
return {
name: toRsaAlgorithm($alg6.KeyPairGenAlgorithm$Rsa$name(algorithm)),
modulusLength: $alg6.KeyPairGenAlgorithm$Rsa$modulus_length(algorithm),
publicExponent: $alg6.KeyPairGenAlgorithm$Rsa$public_exponent(
algorithm
),
hash: toHashAlgorithm($alg6.KeyPairGenAlgorithm$Rsa$hash(algorithm))
};
}
if ($alg6.KeyPairGenAlgorithm$isEd25519(algorithm)) return "Ed25519";
if ($alg6.KeyPairGenAlgorithm$isX25519(algorithm)) return "X25519";
return {
name: toEcAlgorithm($alg6.KeyPairGenAlgorithm$Ec$name(algorithm)),
namedCurve: toNamedCurve(
$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$isOther(algorithm)) {
return $alg7.SignAlgorithm$Other$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: toHashAlgorithm($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(
toHashAlgorithm(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",
toJsonWebKey(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).then(fromJsonWebKey)
);
};
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
};