diff options
author | Florian Dold <florian@dold.me> | 2022-12-21 18:31:46 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2022-12-21 18:31:54 +0100 |
commit | 0b6002bbbc7d1d1e70bd798cd809884de6b9a471 (patch) | |
tree | d2f499cfbe26349cd441ff1054b23b5fd8b74212 /packages/taler-util/src/taler-crypto.ts | |
parent | 3557e307a8e3894819c908e3a0eae5ef93411924 (diff) | |
download | wallet-core-0b6002bbbc7d1d1e70bd798cd809884de6b9a471.tar.xz |
crypto rename
Diffstat (limited to 'packages/taler-util/src/taler-crypto.ts')
-rw-r--r-- | packages/taler-util/src/taler-crypto.ts | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/packages/taler-util/src/taler-crypto.ts b/packages/taler-util/src/taler-crypto.ts index 113e4194b..3e55f68f1 100644 --- a/packages/taler-util/src/taler-crypto.ts +++ b/packages/taler-util/src/taler-crypto.ts @@ -55,7 +55,7 @@ export function getRandomBytesF<T extends number, N extends string>( return nacl.randomBytes(n); } -const useNative = true; +export const useNative = true; const encTable = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; @@ -182,29 +182,41 @@ export function eddsaGetPublic(eddsaPriv: Uint8Array): Uint8Array { return pair.publicKey; } -export function ecdheGetPublic(ecdhePriv: Uint8Array): Uint8Array { +export function ecdhGetPublic(ecdhePriv: Uint8Array): Uint8Array { + if (useNative && "_ecdheGetPublic" in globalThis) { + // @ts-ignore + return globalThis._ecdheGetPublic(ecdhePriv); + } return nacl.scalarMult_base(ecdhePriv); } -export function keyExchangeEddsaEcdhe( +export function keyExchangeEddsaEcdh( eddsaPriv: Uint8Array, - ecdhePub: Uint8Array, + ecdhPub: Uint8Array, ): Uint8Array { + if (useNative && "_keyExchangeEddsaEcdh" in globalThis) { + // @ts-ignore + return globalThis._keyExchangeEddsaEcdh(eddsaPriv, ecdhPub); + } const ph = hash(eddsaPriv); const a = new Uint8Array(32); for (let i = 0; i < 32; i++) { a[i] = ph[i]; } - const x = nacl.scalarMult(a, ecdhePub); + const x = nacl.scalarMult(a, ecdhPub); return hash(x); } -export function keyExchangeEcdheEddsa( - ecdhePriv: Uint8Array & MaterialEcdhePriv, +export function keyExchangeEcdhEddsa( + ecdhPriv: Uint8Array & MaterialEcdhePriv, eddsaPub: Uint8Array & MaterialEddsaPub, ): Uint8Array { + if (useNative && "_keyExchangeEcdhEddsa" in globalThis) { + // @ts-ignore + return globalThis._keyExchangeEcdhEddsa(ecdhPriv, eddsaPub); + } const curve25519Pub = nacl.sign_ed25519_pk_to_curve25519(eddsaPub); - const x = nacl.scalarMult(ecdhePriv, curve25519Pub); + const x = nacl.scalarMult(ecdhPriv, curve25519Pub); return hash(x); } @@ -365,6 +377,10 @@ export function rsaBlind( bks: Uint8Array, rsaPubEnc: Uint8Array, ): Uint8Array { + if (useNative && "_rsaBlind" in globalThis) { + // @ts-ignore + return globalThis._rsaBlind(hm, bks, rsaPubEnc); + } const rsaPub = rsaPubDecode(rsaPubEnc); const data = rsaFullDomainHash(hm, rsaPub); const r = rsaBlindingKeyDerive(rsaPub, bks); @@ -378,6 +394,10 @@ export function rsaUnblind( rsaPubEnc: Uint8Array, bks: Uint8Array, ): Uint8Array { + if (useNative && "_rsaUnblind" in globalThis) { + // @ts-ignore + return globalThis._rsaUnblind(sig, rsaPubEnc, bks); + } const rsaPub = rsaPubDecode(rsaPubEnc); const blinded_s = loadBigInt(sig); const r = rsaBlindingKeyDerive(rsaPub, bks); @@ -391,6 +411,10 @@ export function rsaVerify( rsaSig: Uint8Array, rsaPubEnc: Uint8Array, ): boolean { + if (useNative && "_rsaVerify" in globalThis) { + // @ts-ignore + return globalThis._rsaVerify(hm, rsaSig, rsaPubEnc); + } const rsaPub = rsaPubDecode(rsaPubEnc); const d = rsaFullDomainHash(hm, rsaPub); const sig = loadBigInt(rsaSig); @@ -629,7 +653,7 @@ export function createEddsaKeyPair(): EddsaKeyPair { export function createEcdheKeyPair(): EcdheKeyPair { const ecdhePriv = nacl.randomBytes(32); - const ecdhePub = ecdheGetPublic(ecdhePriv); + const ecdhePub = ecdhGetPublic(ecdhePriv); return { ecdhePriv, ecdhePub }; } @@ -1353,7 +1377,7 @@ export function encryptContractForMerge( mergePriv, contractTermsCompressed, ]); - const key = keyExchangeEcdheEddsa(contractPriv, pursePub); + const key = keyExchangeEcdhEddsa(contractPriv, pursePub); return encryptWithDerivedKey(getRandomBytesF(24), key, data, mergeSalt); } @@ -1370,7 +1394,7 @@ export function encryptContractForDeposit( bufferForUint32(contractTermsBytes.length), contractTermsCompressed, ]); - const key = keyExchangeEcdheEddsa(contractPriv, pursePub); + const key = keyExchangeEcdhEddsa(contractPriv, pursePub); return encryptWithDerivedKey(getRandomBytesF(24), key, data, depositSalt); } @@ -1388,7 +1412,7 @@ export async function decryptContractForMerge( pursePub: PursePublicKey, contractPriv: ContractPrivateKey, ): Promise<DecryptForMergeResult> { - const key = keyExchangeEcdheEddsa(contractPriv, pursePub); + const key = keyExchangeEcdhEddsa(contractPriv, pursePub); const dec = await decryptWithDerivedKey(enc, key, mergeSalt); const mergePriv = dec.slice(8, 8 + 32); const contractTermsCompressed = dec.slice(8 + 32); @@ -1408,7 +1432,7 @@ export async function decryptContractForDeposit( pursePub: PursePublicKey, contractPriv: ContractPrivateKey, ): Promise<DecryptForDepositResult> { - const key = keyExchangeEcdheEddsa(contractPriv, pursePub); + const key = keyExchangeEcdhEddsa(contractPriv, pursePub); const dec = await decryptWithDerivedKey(enc, key, depositSalt); const contractTermsCompressed = dec.slice(8); const contractTermsBuf = fflate.unzlibSync(contractTermsCompressed); |