diff options
author | Florian Dold <florian@dold.me> | 2022-12-21 18:50:15 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2022-12-21 18:50:15 +0100 |
commit | 8a98a5f880b9559312779232b6086016c008969b (patch) | |
tree | ee66b16ab5529aef7ad4ecda7a08385cf3e23a9c | |
parent | 0b6002bbbc7d1d1e70bd798cd809884de6b9a471 (diff) |
wallet: use taler runtime (tart) if available
-rw-r--r-- | packages/taler-util/src/taler-crypto.ts | 90 | ||||
-rw-r--r-- | packages/taler-wallet-embedded/src/wallet-qjs.ts | 2 |
2 files changed, 55 insertions, 37 deletions
diff --git a/packages/taler-util/src/taler-crypto.ts b/packages/taler-util/src/taler-crypto.ts index 3e55f68f1..0f8d2d950 100644 --- a/packages/taler-util/src/taler-crypto.ts +++ b/packages/taler-util/src/taler-crypto.ts @@ -57,6 +57,36 @@ export function getRandomBytesF<T extends number, N extends string>( export const useNative = true; +/** + * Interface of the native Taler runtime library. + */ +interface NativeTartLib { + decodeUtf8(buf: Uint8Array): string; + decodeUtf8(str: string): Uint8Array; + randomBytes(n: number): Uint8Array; + encodeCrock(buf: Uint8Array | ArrayBuffer): string; + decodeCrock(str: string): Uint8Array; + hash(buf: Uint8Array): Uint8Array; + eddsaGetPublic(buf: Uint8Array): Uint8Array; + ecdheGetPublic(buf: Uint8Array): Uint8Array; + eddsaSign(msg: Uint8Array, priv: Uint8Array): Uint8Array; + eddsaVerify(msg: Uint8Array, sig: Uint8Array, pub: Uint8Array): boolean; + kdf(outLen: number, ikm: Uint8Array, salt?: Uint8Array, info?: Uint8Array): Uint8Array; + keyExchangeEcdhEddsa(ecdhPriv: Uint8Array, eddsaPub: Uint8Array): Uint8Array; + keyExchangeEddsaEcdh(eddsaPriv: Uint8Array, ecdhPub: Uint8Array): Uint8Array; + rsaBlind(hmsg: Uint8Array, bks: Uint8Array, rsaPub: Uint8Array): Uint8Array; + rsaUnblind(blindSig: Uint8Array, rsaPub: Uint8Array, bks: Uint8Array): Uint8Array; + rsaVerify(hmsg: Uint8Array, rsaSig: Uint8Array, rsaPub: Uint8Array): boolean +} + +// @ts-ignore +let tart: NativeTartLib | undefined; + +if (useNative) { + // @ts-ignore + tart = globalThis._tart; +} + const encTable = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; class EncodingError extends Error { @@ -101,9 +131,8 @@ function getValue(chr: string): number { } export function encodeCrock(data: ArrayBuffer): string { - if (useNative && "_encodeCrock" in globalThis) { - // @ts-ignore - return globalThis._encodeCrock(data); + if (tart) { + return tart.encodeCrock(data); } const dataBytes = new Uint8Array(data); let sb = ""; @@ -142,9 +171,8 @@ export function kdfKw(args: { } export function decodeCrock(encoded: string): Uint8Array { - if (useNative && "_decodeCrock" in globalThis) { - // @ts-ignore - return globalThis._decodeCrock(encoded); + if (tart) { + return tart.decodeCrock(encoded); } const size = encoded.length; let bitpos = 0; @@ -174,18 +202,16 @@ export function decodeCrock(encoded: string): Uint8Array { } export function eddsaGetPublic(eddsaPriv: Uint8Array): Uint8Array { - if (useNative && "_eddsaGetPublic" in globalThis) { - // @ts-ignore - return globalThis._eddsaGetPublic(eddsaPriv); + if (tart) { + return tart.eddsaGetPublic(eddsaPriv); } const pair = nacl.crypto_sign_keyPair_fromSeed(eddsaPriv); return pair.publicKey; } export function ecdhGetPublic(ecdhePriv: Uint8Array): Uint8Array { - if (useNative && "_ecdheGetPublic" in globalThis) { - // @ts-ignore - return globalThis._ecdheGetPublic(ecdhePriv); + if (tart) { + return tart.ecdheGetPublic(ecdhePriv); } return nacl.scalarMult_base(ecdhePriv); } @@ -194,9 +220,8 @@ export function keyExchangeEddsaEcdh( eddsaPriv: Uint8Array, ecdhPub: Uint8Array, ): Uint8Array { - if (useNative && "_keyExchangeEddsaEcdh" in globalThis) { - // @ts-ignore - return globalThis._keyExchangeEddsaEcdh(eddsaPriv, ecdhPub); + if (tart) { + return tart.keyExchangeEddsaEcdh(eddsaPriv, ecdhPub); } const ph = hash(eddsaPriv); const a = new Uint8Array(32); @@ -211,9 +236,8 @@ export function keyExchangeEcdhEddsa( ecdhPriv: Uint8Array & MaterialEcdhePriv, eddsaPub: Uint8Array & MaterialEddsaPub, ): Uint8Array { - if (useNative && "_keyExchangeEcdhEddsa" in globalThis) { - // @ts-ignore - return globalThis._keyExchangeEcdhEddsa(ecdhPriv, eddsaPub); + if (tart) { + return tart.keyExchangeEcdhEddsa(ecdhPriv, eddsaPub); } const curve25519Pub = nacl.sign_ed25519_pk_to_curve25519(eddsaPub); const x = nacl.scalarMult(ecdhPriv, curve25519Pub); @@ -377,9 +401,8 @@ export function rsaBlind( bks: Uint8Array, rsaPubEnc: Uint8Array, ): Uint8Array { - if (useNative && "_rsaBlind" in globalThis) { - // @ts-ignore - return globalThis._rsaBlind(hm, bks, rsaPubEnc); + if (tart) { + return tart.rsaBlind(hm, bks, rsaPubEnc); } const rsaPub = rsaPubDecode(rsaPubEnc); const data = rsaFullDomainHash(hm, rsaPub); @@ -394,9 +417,8 @@ export function rsaUnblind( rsaPubEnc: Uint8Array, bks: Uint8Array, ): Uint8Array { - if (useNative && "_rsaUnblind" in globalThis) { - // @ts-ignore - return globalThis._rsaUnblind(sig, rsaPubEnc, bks); + if (tart) { + return tart.rsaUnblind(sig, rsaPubEnc, bks) } const rsaPub = rsaPubDecode(rsaPubEnc); const blinded_s = loadBigInt(sig); @@ -411,9 +433,8 @@ export function rsaVerify( rsaSig: Uint8Array, rsaPubEnc: Uint8Array, ): boolean { - if (useNative && "_rsaVerify" in globalThis) { - // @ts-ignore - return globalThis._rsaVerify(hm, rsaSig, rsaPubEnc); + if (tart) { + return tart.rsaVerify(hm, rsaSig, rsaPubEnc); } const rsaPub = rsaPubDecode(rsaPubEnc); const d = rsaFullDomainHash(hm, rsaPub); @@ -658,9 +679,8 @@ export function createEcdheKeyPair(): EcdheKeyPair { } export function hash(d: Uint8Array): Uint8Array { - if (useNative && "_hash" in globalThis) { - // @ts-ignore - return globalThis._hash(d); + if (tart) { + return tart.hash(d); } return nacl.hash(d); } @@ -747,9 +767,8 @@ export function hashDenomPub(pub: DenominationPubKey): Uint8Array { } export function eddsaSign(msg: Uint8Array, eddsaPriv: Uint8Array): Uint8Array { - if (useNative && "_eddsaSign" in globalThis) { - // @ts-ignore - return globalThis._eddsaSign(msg, eddsaPriv); + if (tart) { + return tart.eddsaSign(msg, eddsaPriv); } const pair = nacl.crypto_sign_keyPair_fromSeed(eddsaPriv); return nacl.sign_detached(msg, pair.secretKey); @@ -760,9 +779,8 @@ export function eddsaVerify( sig: Uint8Array, eddsaPub: Uint8Array, ): boolean { - if (useNative && "_eddsaVerify" in globalThis) { - // @ts-ignore - return globalThis._eddsaVerify(msg, sig, eddsaPub); + if (tart) { + return tart.eddsaVerify(msg, sig, eddsaPub); } return nacl.sign_detached_verify(msg, sig, eddsaPub); } diff --git a/packages/taler-wallet-embedded/src/wallet-qjs.ts b/packages/taler-wallet-embedded/src/wallet-qjs.ts index 889cbd83a..cdd5400cf 100644 --- a/packages/taler-wallet-embedded/src/wallet-qjs.ts +++ b/packages/taler-wallet-embedded/src/wallet-qjs.ts @@ -59,7 +59,7 @@ setGlobalLogLevelFromString("trace"); setPRNG(function (x: Uint8Array, n: number) { // @ts-ignore - const va = globalThis._randomBytes(n); + const va = globalThis._tart.randomBytes(n); const v = new Uint8Array(va); for (let i = 0; i < n; i++) x[i] = v[i]; for (let i = 0; i < v.length; i++) v[i] = 0; |