Skip to content

Commit d7812ab

Browse files
FiloSottilegopherbot
authored andcommitted
crypto/internal/bigmod: move nat implementation out of crypto/rsa
This will let us reuse it in crypto/ecdsa for the NIST scalar fields. The main change in API is around encoding and decoding. The SetBytes + ExpandFor sequence was hacky: SetBytes could produce a bigger size than the modulus if leading zeroes in the top byte overflowed the limb boundary, so ExpandFor had to check for and tolerate that. Also, the caller was responsible for checking that the overflow was actually all zeroes (which we weren't doing, exposing a crasher in decryption and signature verification) and then for checking that the result was less than the modulus. Instead, make SetBytes take a modulus and return an error if the value overflows. Same with Bytes: we were always allocating based on Size before FillBytes anyway, so now Bytes takes a modulus. Finally, SetBig was almost only used for moduli, so replaced NewModulusFromNat and SetBig with NewModulusFromBig. Moved the constant-time bitLen to math/big.Int.BitLen. It's slower, but BitLen is primarily used in cryptographic code, so it's safer this way. Change-Id: Ibaf7f36d80695578cb80484167d82ce1aa83832f Reviewed-on: https://go-review.googlesource.com/c/go/+/450055 Auto-Submit: Filippo Valsorda <[email protected]> Run-TryBot: Filippo Valsorda <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent 831c650 commit d7812ab

File tree

10 files changed

+616
-545
lines changed

10 files changed

+616
-545
lines changed

src/crypto/rsa/nat.go renamed to src/crypto/internal/bigmod/nat.go

Lines changed: 96 additions & 115 deletions
Large diffs are not rendered by default.

src/crypto/internal/bigmod/nat_test.go

Lines changed: 412 additions & 0 deletions
Large diffs are not rendered by default.

src/crypto/rsa/nat_test.go

Lines changed: 0 additions & 384 deletions
This file was deleted.

0 commit comments

Comments
 (0)