bitchan / eccrypto

JavaScript Elliptic curve cryptography library
Creative Commons Zero v1.0 Universal
302 stars 98 forks source link

toArray() returns unpadded Buffer arrays, resulting in Bad MAC error #90

Open tetratorus opened 1 year ago

tetratorus commented 1 year ago

https://github.com/bitchan/eccrypto/blob/a4f4a5f85ef5aa1776dfa1b7801cad808264a19c/browser.js#L205

Px is a BN, and the toArray() method on BN doesn't have padding. So if it starts with two leading zeros (eg. '00928cfa...', happens with 1/256 probability), the length of the resulting Buffer will only be length 31 instead of length 32.

Reproducible example

var encryption = {
  ciphertext: Buffer.from('e614aff7db97b01d4b0d5cfb1387b4763cb369f74d743bed95020330d57e3ae91a574bd7ae89da0885eb5f6e332a296f', 'hex'),
  ephemPublicKey: Buffer.from('04fb0a7c19defeaeeb34defbc47be3c9a4c1de500895c1e1e8ce6d0991595217f8e76c4594968e8c77d83c26f4f1ee496c40c7ac48816a4ee2edf38c550d8916a0', 'hex'),
  iv: Buffer.from('456f0c039cb2224849082c3d0feebec1', 'hex'),
  mac: Buffer.from('df7352dcdf2ee10c939276791515340479b526920a155b8ac932a5a26ea4c924', 'hex')
}

var decryptionKey = Buffer.from('78bb3f8efcd59ebc8c4f0dee865ba10e375869921c62caa5b3b46699504bb280', 'hex')
mburger81 commented 1 year ago

Where you able to resolve this?