fpe icon indicating copy to clipboard operation
fpe copied to clipboard

Unused `R` in decryption

Open TomMD opened this issue 3 years ago • 5 comments

Looking here:

https://github.com/capitalone/fpe/blob/e6bfa0dae121111c2e93483777f0f2cde562f341/ff1/ff1.go#L484

The comment suggests R should be used but it is entirely unused. (As reported by staticcheck here). Is this dead/delete-able or does it indicate a decryption error?

TomMD avatar Oct 15 '20 19:10 TomMD

It's strange that staticcheck considers it unused, because it is consumed later on in the for loop with xored[offset+x] = R[x] ^ xored[offset+x].

Strictly speaking, I suppose it doesn't actually need to be a standalone variable, but I felt that for readability it makes sense to follow the algorithm definition/spec, Step 6.ii - Let R = PRF(P || Q). . It's then used in the xor step in 6.iii.

In the code, R is a "sub-slice" of Y and overlaps part of PQ. These all could have been separate, but as part of performance improvements in v1.1.0, they were changed to share a backing array.

anitgandhi avatar Oct 15 '20 20:10 anitgandhi

I see, a false positive (a rather odd one too). I'm all in favor of the readability aspect of intermediate variables. Thanks for setting the record straight and for such a detailed response!

TomMD avatar Oct 15 '20 20:10 TomMD

It's strange that staticcheck considers it unused, because it is consumed later on

Technically, it isn't. xored[offset+x] = R[x] ^ xored[offset+x] on line 554 makes use of the value of R set on line 534: R, err = c.prf(PQ). The definition of R on line 484 is never being read.

dominikh avatar Oct 16 '20 04:10 dominikh

Reopening in light of @dominikh point - it appears not to be a false positive after all.

TomMD avatar Oct 16 '20 15:10 TomMD

Yeah indeed, thanks for pointing it out folks. Unfortunately I no longer have write access to this repo so TBD on when this will get fixed...

anitgandhi avatar Oct 16 '20 16:10 anitgandhi