11import { secp256k1 } from "@noble/curves/secp256k1.js" ;
2- import { concatBytes } from "@noble/curves/utils.js" ;
2+ import { concatBytes , equalBytes } from "@noble/curves/utils.js" ;
33
44// eslint-disable-next-line @typescript-eslint/no-explicit-any, n/no-unsupported-features/node-builtins
55const browserCrypto = globalThis . crypto || ( globalThis as any ) . msCrypto || { } ;
@@ -21,7 +21,7 @@ function assert(condition: boolean, message: string) {
2121 }
2222}
2323
24- export function uint8ArrayToBigInt ( arr : Uint8Array ) : bigint {
24+ function uint8ArrayToBigInt ( arr : Uint8Array ) : bigint {
2525 let result = 0n ;
2626 for ( let i = 0 ; i < arr . length ; i ++ ) {
2727 result = ( result << 8n ) | BigInt ( arr [ i ] ) ;
@@ -41,19 +41,6 @@ function isValidPrivateKey(privateKey: Uint8Array): boolean {
4141 ) ; // < G
4242}
4343
44- // Compare two buffers in constant time to prevent timing attacks.
45- function equalConstTime ( b1 : Uint8Array , b2 : Uint8Array ) : boolean {
46- if ( b1 . length !== b2 . length ) {
47- return false ;
48- }
49- let res = 0 ;
50- for ( let i = 0 ; i < b1 . length ; i ++ ) {
51- res |= b1 [ i ] ^ b2 [ i ] ;
52- }
53-
54- return res === 0 ;
55- }
56-
5744/* This must check if we're in the browser or
5845not, since the functions are different and does
5946not convert using browserify */
@@ -133,7 +120,7 @@ async function hmacSha256Sign(key: Uint8Array, msg: Uint8Array): Promise<Uint8Ar
133120
134121async function hmacSha256Verify ( key : Uint8Array , msg : Uint8Array , sig : Uint8Array ) : Promise < boolean > {
135122 const expectedSig = await hmacSha256Sign ( key , msg ) ;
136- return equalConstTime ( expectedSig , sig ) ;
123+ return equalBytes ( expectedSig , sig ) ;
137124}
138125
139126function assertValidPrivateKey ( privateKey : Uint8Array ) : void {
@@ -184,7 +171,10 @@ export const getPublicCompressed = function (privateKey: Uint8Array): Uint8Array
184171export const sign = async function ( privateKey : Uint8Array , msg : Uint8Array ) : Promise < Uint8Array > {
185172 assertValidPrivateKey ( privateKey ) ;
186173 assertValidMessage ( msg ) ;
187- const sig = secp256k1 . sign ( msg , privateKey , { prehash : false , format : "der" } ) ;
174+ const sig = secp256k1 . sign ( msg , privateKey , {
175+ prehash : false ,
176+ format : "der" ,
177+ } ) ;
188178 return sig ;
189179} ;
190180
@@ -237,8 +227,7 @@ export const encrypt = async function (
237227 const iv = opts . iv || randomBytes ( 16 ) ;
238228 const encryptionKey = hash . slice ( 0 , 32 ) ;
239229 const macKey = hash . slice ( 32 ) ;
240- const data = await aesCbcEncrypt ( iv , encryptionKey , msg ) ;
241- const ciphertext = data ;
230+ const ciphertext = await aesCbcEncrypt ( iv , encryptionKey , msg ) ;
242231 const dataToMac = concatBytes ( iv , ephemPublicKey , ciphertext ) ;
243232 const mac = await hmacSha256Sign ( macKey , dataToMac ) ;
244233 return {
0 commit comments