]> Cypherpunks.ru repositories - gostls13.git/commit
crypto/internal/bigmod: move nat implementation out of crypto/rsa
authorFilippo Valsorda <filippo@golang.org>
Sat, 12 Nov 2022 13:01:15 +0000 (14:01 +0100)
committerGopher Robot <gobot@golang.org>
Mon, 21 Nov 2022 16:19:15 +0000 (16:19 +0000)
commitd7812ab38031ae524a731b4d2f19adcecd22c2f4
tree09047be60d7c7af0abf8697d1312e11002be3c86
parent831c6509ccddfc55c7f2121c5158f5f40916e6ad
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 <filippo@golang.org>
Run-TryBot: Filippo Valsorda <filippo@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
src/crypto/internal/bigmod/nat.go [moved from src/crypto/rsa/nat.go with 77% similarity]
src/crypto/internal/bigmod/nat_test.go [new file with mode: 0644]
src/crypto/rsa/nat_test.go [deleted file]
src/crypto/rsa/pkcs1v15.go
src/crypto/rsa/pss.go
src/crypto/rsa/rsa.go
src/crypto/rsa/rsa_test.go
src/go/build/deps_test.go
src/math/big/int.go
src/math/big/nat.go