| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| package rsa |
|
|
| import ( |
| "crypto" |
| "crypto/internal/boring" |
| "crypto/internal/boring/bbig" |
| "crypto/internal/fips140/bigmod" |
| "crypto/internal/fips140/rsa" |
| "crypto/internal/fips140only" |
| "crypto/internal/rand" |
| cryptorand "crypto/rand" |
| "crypto/subtle" |
| "errors" |
| "fmt" |
| "internal/godebug" |
| "io" |
| "math" |
| "math/big" |
| ) |
|
|
| var bigOne = big.NewInt(1) |
|
|
| |
| |
| |
| |
| type PublicKey struct { |
| N *big.Int |
| E int |
| } |
|
|
| |
| |
|
|
| |
| |
| func (pub *PublicKey) Size() int { |
| return (pub.N.BitLen() + 7) / 8 |
| } |
|
|
| |
| func (pub *PublicKey) Equal(x crypto.PublicKey) bool { |
| xx, ok := x.(*PublicKey) |
| if !ok { |
| return false |
| } |
| return bigIntEqual(pub.N, xx.N) && pub.E == xx.E |
| } |
|
|
| |
| |
| type OAEPOptions struct { |
| |
| Hash crypto.Hash |
|
|
| |
| |
| MGFHash crypto.Hash |
|
|
| |
| |
| Label []byte |
| } |
|
|
| |
| |
| |
| |
| type PrivateKey struct { |
| PublicKey |
| D *big.Int |
| Primes []*big.Int |
|
|
| |
| |
| |
| Precomputed PrecomputedValues |
| } |
|
|
| |
| func (priv *PrivateKey) Public() crypto.PublicKey { |
| return &priv.PublicKey |
| } |
|
|
| |
| |
| func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool { |
| xx, ok := x.(*PrivateKey) |
| if !ok { |
| return false |
| } |
| if !priv.PublicKey.Equal(&xx.PublicKey) || !bigIntEqual(priv.D, xx.D) { |
| return false |
| } |
| if len(priv.Primes) != len(xx.Primes) { |
| return false |
| } |
| for i := range priv.Primes { |
| if !bigIntEqual(priv.Primes[i], xx.Primes[i]) { |
| return false |
| } |
| } |
| return true |
| } |
|
|
| |
| |
| func bigIntEqual(a, b *big.Int) bool { |
| return subtle.ConstantTimeCompare(a.Bytes(), b.Bytes()) == 1 |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func (priv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| if pssOpts, ok := opts.(*PSSOptions); ok { |
| return SignPSS(rand, priv, pssOpts.Hash, digest, pssOpts) |
| } |
|
|
| return SignPKCS1v15(rand, priv, opts.HashFunc(), digest) |
| } |
|
|
| |
| |
| |
| func (priv *PrivateKey) Decrypt(rand io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error) { |
| if opts == nil { |
| return DecryptPKCS1v15(rand, priv, ciphertext) |
| } |
|
|
| switch opts := opts.(type) { |
| case *OAEPOptions: |
| if opts.MGFHash == 0 { |
| return decryptOAEP(opts.Hash.New(), opts.Hash.New(), priv, ciphertext, opts.Label) |
| } else { |
| return decryptOAEP(opts.Hash.New(), opts.MGFHash.New(), priv, ciphertext, opts.Label) |
| } |
|
|
| case *PKCS1v15DecryptOptions: |
| if l := opts.SessionKeyLen; l > 0 { |
| plaintext = make([]byte, l) |
| if _, err := io.ReadFull(rand, plaintext); err != nil { |
| return nil, err |
| } |
| if err := DecryptPKCS1v15SessionKey(rand, priv, ciphertext, plaintext); err != nil { |
| return nil, err |
| } |
| return plaintext, nil |
| } else { |
| return DecryptPKCS1v15(rand, priv, ciphertext) |
| } |
|
|
| default: |
| return nil, errors.New("crypto/rsa: invalid options for Decrypt") |
| } |
| } |
|
|
| type PrecomputedValues struct { |
| Dp, Dq *big.Int |
| Qinv *big.Int |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| CRTValues []CRTValue |
|
|
| fips *rsa.PrivateKey |
| } |
|
|
| |
| type CRTValue struct { |
| Exp *big.Int |
| Coeff *big.Int |
| R *big.Int |
| } |
|
|
| |
| |
| |
| |
| func (priv *PrivateKey) Validate() error { |
| |
| |
| |
| if len(priv.Primes) < 2 { |
| return errors.New("crypto/rsa: missing primes") |
| } |
| |
| |
| if priv.precomputedIsConsistent() { |
| return nil |
| } |
| if priv.Precomputed.fips != nil { |
| return errors.New("crypto/rsa: precomputed values are inconsistent with the key") |
| } |
| _, err := priv.precompute() |
| return err |
| } |
|
|
| func (priv *PrivateKey) precomputedIsConsistent() bool { |
| if priv.Precomputed.fips == nil { |
| return false |
| } |
| N, e, d, P, Q, dP, dQ, qInv := priv.Precomputed.fips.Export() |
| if !bigIntEqualToBytes(priv.N, N) || priv.E != e || !bigIntEqualToBytes(priv.D, d) { |
| return false |
| } |
| if len(priv.Primes) != 2 { |
| return P == nil && Q == nil && dP == nil && dQ == nil && qInv == nil |
| } |
| return bigIntEqualToBytes(priv.Primes[0], P) && |
| bigIntEqualToBytes(priv.Primes[1], Q) && |
| bigIntEqualToBytes(priv.Precomputed.Dp, dP) && |
| bigIntEqualToBytes(priv.Precomputed.Dq, dQ) && |
| bigIntEqualToBytes(priv.Precomputed.Qinv, qInv) |
| } |
|
|
| |
| |
| func bigIntEqualToBytes(a *big.Int, b []byte) bool { |
| if a == nil || a.BitLen() > len(b)*8 { |
| return false |
| } |
| buf := a.FillBytes(make([]byte, len(b))) |
| return subtle.ConstantTimeCompare(buf, b) == 1 |
| } |
|
|
| |
| |
| var rsa1024min = godebug.New("rsa1024min") |
|
|
| func checkKeySize(size int) error { |
| if size >= 1024 { |
| return nil |
| } |
| if rsa1024min.Value() == "0" { |
| rsa1024min.IncNonDefault() |
| return nil |
| } |
| return fmt.Errorf("crypto/rsa: %d-bit keys are insecure (see https://go.dev/pkg/crypto/rsa#hdr-Minimum_key_size)", size) |
| } |
|
|
| func checkPublicKeySize(k *PublicKey) error { |
| if k.N == nil { |
| return errors.New("crypto/rsa: missing public modulus") |
| } |
| return checkKeySize(k.N.BitLen()) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func GenerateKey(random io.Reader, bits int) (*PrivateKey, error) { |
| if err := checkKeySize(bits); err != nil { |
| return nil, err |
| } |
|
|
| if boring.Enabled && rand.IsDefaultReader(random) && |
| (bits == 2048 || bits == 3072 || bits == 4096) { |
| bN, bE, bD, bP, bQ, bDp, bDq, bQinv, err := boring.GenerateKeyRSA(bits) |
| if err != nil { |
| return nil, err |
| } |
| N := bbig.Dec(bN) |
| E := bbig.Dec(bE) |
| D := bbig.Dec(bD) |
| P := bbig.Dec(bP) |
| Q := bbig.Dec(bQ) |
| Dp := bbig.Dec(bDp) |
| Dq := bbig.Dec(bDq) |
| Qinv := bbig.Dec(bQinv) |
| e64 := E.Int64() |
| if !E.IsInt64() || int64(int(e64)) != e64 { |
| return nil, errors.New("crypto/rsa: generated key exponent too large") |
| } |
|
|
| key := &PrivateKey{ |
| PublicKey: PublicKey{ |
| N: N, |
| E: int(e64), |
| }, |
| D: D, |
| Primes: []*big.Int{P, Q}, |
| Precomputed: PrecomputedValues{ |
| Dp: Dp, |
| Dq: Dq, |
| Qinv: Qinv, |
| CRTValues: make([]CRTValue, 0), |
| }, |
| } |
| return key, nil |
| } |
|
|
| random = rand.CustomReader(random) |
|
|
| if fips140only.Enforced() && bits < 2048 { |
| return nil, errors.New("crypto/rsa: use of keys smaller than 2048 bits is not allowed in FIPS 140-only mode") |
| } |
| if fips140only.Enforced() && bits%2 == 1 { |
| return nil, errors.New("crypto/rsa: use of keys with odd size is not allowed in FIPS 140-only mode") |
| } |
| if fips140only.Enforced() && !fips140only.ApprovedRandomReader(random) { |
| return nil, errors.New("crypto/rsa: only crypto/rand.Reader is allowed in FIPS 140-only mode") |
| } |
|
|
| k, err := rsa.GenerateKey(random, bits) |
| if bits < 256 && err != nil { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| for i := 1; i < 8 && err != nil; i++ { |
| k, err = rsa.GenerateKey(random, bits) |
| } |
| } |
| if err != nil { |
| return nil, err |
| } |
| N, e, d, p, q, dP, dQ, qInv := k.Export() |
| key := &PrivateKey{ |
| PublicKey: PublicKey{ |
| N: new(big.Int).SetBytes(N), |
| E: e, |
| }, |
| D: new(big.Int).SetBytes(d), |
| Primes: []*big.Int{ |
| new(big.Int).SetBytes(p), |
| new(big.Int).SetBytes(q), |
| }, |
| Precomputed: PrecomputedValues{ |
| fips: k, |
| Dp: new(big.Int).SetBytes(dP), |
| Dq: new(big.Int).SetBytes(dQ), |
| Qinv: new(big.Int).SetBytes(qInv), |
| CRTValues: make([]CRTValue, 0), |
| }, |
| } |
| return key, nil |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func GenerateMultiPrimeKey(random io.Reader, nprimes int, bits int) (*PrivateKey, error) { |
| if nprimes == 2 { |
| return GenerateKey(random, bits) |
| } |
| if fips140only.Enforced() { |
| return nil, errors.New("crypto/rsa: multi-prime RSA is not allowed in FIPS 140-only mode") |
| } |
|
|
| random = rand.CustomReader(random) |
|
|
| priv := new(PrivateKey) |
| priv.E = 65537 |
|
|
| if nprimes < 2 { |
| return nil, errors.New("crypto/rsa: GenerateMultiPrimeKey: nprimes must be >= 2") |
| } |
|
|
| if bits < 64 { |
| primeLimit := float64(uint64(1) << uint(bits/nprimes)) |
| |
| pi := primeLimit / (math.Log(primeLimit) - 1) |
| |
| |
| pi /= 4 |
| |
| |
| pi /= 2 |
| if pi <= float64(nprimes) { |
| return nil, errors.New("crypto/rsa: too few primes of given length to generate an RSA key") |
| } |
| } |
|
|
| primes := make([]*big.Int, nprimes) |
|
|
| NextSetOfPrimes: |
| for { |
| todo := bits |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if nprimes >= 7 { |
| todo += (nprimes - 2) / 5 |
| } |
| for i := 0; i < nprimes; i++ { |
| var err error |
| primes[i], err = cryptorand.Prime(random, todo/(nprimes-i)) |
| if err != nil { |
| return nil, err |
| } |
| todo -= primes[i].BitLen() |
| } |
|
|
| |
| for i, prime := range primes { |
| for j := 0; j < i; j++ { |
| if prime.Cmp(primes[j]) == 0 { |
| continue NextSetOfPrimes |
| } |
| } |
| } |
|
|
| n := new(big.Int).Set(bigOne) |
| totient := new(big.Int).Set(bigOne) |
| pminus1 := new(big.Int) |
| for _, prime := range primes { |
| n.Mul(n, prime) |
| pminus1.Sub(prime, bigOne) |
| totient.Mul(totient, pminus1) |
| } |
| if n.BitLen() != bits { |
| |
| |
| |
| continue NextSetOfPrimes |
| } |
|
|
| priv.D = new(big.Int) |
| e := big.NewInt(int64(priv.E)) |
| ok := priv.D.ModInverse(e, totient) |
|
|
| if ok != nil { |
| priv.Primes = primes |
| priv.N = n |
| break |
| } |
| } |
|
|
| priv.Precompute() |
| if err := priv.Validate(); err != nil { |
| return nil, err |
| } |
|
|
| return priv, nil |
| } |
|
|
| |
| |
| |
| var ErrMessageTooLong = errors.New("crypto/rsa: message too long for RSA key size") |
|
|
| |
| |
| var ErrDecryption = errors.New("crypto/rsa: decryption error") |
|
|
| |
| |
| var ErrVerification = errors.New("crypto/rsa: verification error") |
|
|
| |
| |
| func (priv *PrivateKey) Precompute() { |
| if priv.precomputedIsConsistent() { |
| return |
| } |
|
|
| precomputed, err := priv.precompute() |
| if err != nil { |
| |
| |
| priv.Precomputed.fips = nil |
| return |
| } |
| priv.Precomputed = precomputed |
| } |
|
|
| func (priv *PrivateKey) precompute() (PrecomputedValues, error) { |
| var precomputed PrecomputedValues |
|
|
| if priv.N == nil { |
| return precomputed, errors.New("crypto/rsa: missing public modulus") |
| } |
| if priv.D == nil { |
| return precomputed, errors.New("crypto/rsa: missing private exponent") |
| } |
| if len(priv.Primes) != 2 { |
| return priv.precomputeLegacy() |
| } |
| if priv.Primes[0] == nil { |
| return precomputed, errors.New("crypto/rsa: prime P is nil") |
| } |
| if priv.Primes[1] == nil { |
| return precomputed, errors.New("crypto/rsa: prime Q is nil") |
| } |
|
|
| |
| if priv.Precomputed.Dp != nil && priv.Precomputed.Dq != nil && priv.Precomputed.Qinv != nil { |
| k, err := rsa.NewPrivateKeyWithPrecomputation(priv.N.Bytes(), priv.E, priv.D.Bytes(), |
| priv.Primes[0].Bytes(), priv.Primes[1].Bytes(), |
| priv.Precomputed.Dp.Bytes(), priv.Precomputed.Dq.Bytes(), priv.Precomputed.Qinv.Bytes()) |
| if err != nil { |
| return precomputed, err |
| } |
| precomputed = priv.Precomputed |
| precomputed.fips = k |
| precomputed.CRTValues = make([]CRTValue, 0) |
| return precomputed, nil |
| } |
|
|
| k, err := rsa.NewPrivateKey(priv.N.Bytes(), priv.E, priv.D.Bytes(), |
| priv.Primes[0].Bytes(), priv.Primes[1].Bytes()) |
| if err != nil { |
| return precomputed, err |
| } |
|
|
| precomputed.fips = k |
| _, _, _, _, _, dP, dQ, qInv := k.Export() |
| precomputed.Dp = new(big.Int).SetBytes(dP) |
| precomputed.Dq = new(big.Int).SetBytes(dQ) |
| precomputed.Qinv = new(big.Int).SetBytes(qInv) |
| precomputed.CRTValues = make([]CRTValue, 0) |
| return precomputed, nil |
| } |
|
|
| func (priv *PrivateKey) precomputeLegacy() (PrecomputedValues, error) { |
| var precomputed PrecomputedValues |
|
|
| k, err := rsa.NewPrivateKeyWithoutCRT(priv.N.Bytes(), priv.E, priv.D.Bytes()) |
| if err != nil { |
| return precomputed, err |
| } |
| precomputed.fips = k |
|
|
| if len(priv.Primes) < 2 { |
| return precomputed, nil |
| } |
|
|
| |
| for _, prime := range priv.Primes { |
| if prime == nil { |
| return precomputed, errors.New("crypto/rsa: prime factor is nil") |
| } |
| if prime.Cmp(bigOne) <= 0 { |
| return precomputed, errors.New("crypto/rsa: prime factor is <= 1") |
| } |
| } |
|
|
| precomputed.Dp = new(big.Int).Sub(priv.Primes[0], bigOne) |
| precomputed.Dp.Mod(priv.D, precomputed.Dp) |
|
|
| precomputed.Dq = new(big.Int).Sub(priv.Primes[1], bigOne) |
| precomputed.Dq.Mod(priv.D, precomputed.Dq) |
|
|
| precomputed.Qinv = new(big.Int).ModInverse(priv.Primes[1], priv.Primes[0]) |
| if precomputed.Qinv == nil { |
| return precomputed, errors.New("crypto/rsa: prime factors are not relatively prime") |
| } |
|
|
| r := new(big.Int).Mul(priv.Primes[0], priv.Primes[1]) |
| precomputed.CRTValues = make([]CRTValue, len(priv.Primes)-2) |
| for i := 2; i < len(priv.Primes); i++ { |
| prime := priv.Primes[i] |
| values := &precomputed.CRTValues[i-2] |
|
|
| values.Exp = new(big.Int).Sub(prime, bigOne) |
| values.Exp.Mod(priv.D, values.Exp) |
|
|
| values.R = new(big.Int).Set(r) |
| values.Coeff = new(big.Int).ModInverse(r, prime) |
| if values.Coeff == nil { |
| return precomputed, errors.New("crypto/rsa: prime factors are not relatively prime") |
| } |
|
|
| r.Mul(r, prime) |
| } |
|
|
| return precomputed, nil |
| } |
|
|
| func fipsPublicKey(pub *PublicKey) (*rsa.PublicKey, error) { |
| N, err := bigmod.NewModulus(pub.N.Bytes()) |
| if err != nil { |
| return nil, err |
| } |
| return &rsa.PublicKey{N: N, E: pub.E}, nil |
| } |
|
|
| func fipsPrivateKey(priv *PrivateKey) (*rsa.PrivateKey, error) { |
| if priv.Precomputed.fips != nil { |
| return priv.Precomputed.fips, nil |
| } |
| precomputed, err := priv.precompute() |
| if err != nil { |
| return nil, err |
| } |
| return precomputed.fips, nil |
| } |
|
|