node-fe1-fpe icon indicating copy to clipboard operation
node-fe1-fpe copied to clipboard

Decryption wrong if format() is less than firstFactor

Open sparecycles opened this issue 4 years ago • 0 comments

The calculation of left in decryption can result in a negative number when modulu is less than 0,

https://github.com/eCollect/node-fe1-fpe/blob/ceae15baf310e65c89504ce785f568b4c204f433/index.js#L63

this leads to a failure to recover the original value.

The value produced should be based on - modulu in all cases (to ensure a positive result):

 left = (modulu > 0) ? firstFactor - modulu : - modulu; 

You can confirm this change is an improvement by

  • first removing all expectedEV assertions from the unit tests (tests should still pass with less assertions)
  • modifying the format() function to return ... % 100n to force the modulu <= 0 case to occur, (tests now fail).
  • then updating the code as above. (tests pass again, because the code was fixed).

Then remember to

  • restore the current format() function.
  • restore the current test code (all original tests will still pass with all assertions).

sparecycles avatar Nov 02 '20 21:11 sparecycles