Source File
rsa.go
Belonging Package
crypto/rsa
Label []byte
}
var (
errPublicModulus = errors.New("crypto/rsa: missing public modulus")
errPublicExponentSmall = errors.New("crypto/rsa: public exponent too small")
errPublicExponentLarge = errors.New("crypto/rsa: public exponent too large")
)
func ( *PublicKey) error {
if .N == nil {
return errPublicModulus
}
if .E < 2 {
return errPublicExponentSmall
}
if .E > 1<<31-1 {
return errPublicExponentLarge
}
return nil
}
func ( *PrivateKey) () crypto.PublicKey {
return &.PublicKey
}
func ( *PrivateKey) ( io.Reader, []byte, crypto.SignerOpts) ([]byte, error) {
if , := .(*PSSOptions); {
return SignPSS(, , .Hash, , )
}
return SignPKCS1v15(, , .HashFunc(), )
}
func ( *PrivateKey) ( io.Reader, []byte, crypto.DecrypterOpts) ( []byte, error) {
if == nil {
return DecryptPKCS1v15(, , )
}
switch opts := .(type) {
case *OAEPOptions:
return DecryptOAEP(.Hash.New(), , , , .Label)
case *PKCS1v15DecryptOptions:
if := .SessionKeyLen; > 0 {
= make([]byte, )
if , := io.ReadFull(, ); != nil {
return nil,
}
if := DecryptPKCS1v15SessionKey(, , , ); != nil {
return nil,
}
return , nil
} else {
return DecryptPKCS1v15(, , )
}
default:
return nil, errors.New("crypto/rsa: invalid options for Decrypt")
}
}
type PrecomputedValues struct {
Dp, Dq *big.Int // D mod (P-1) (or mod Q-1)
Qinv *big.Int // Q^-1 mod P
func ( *PrivateKey) () error {
if := checkPub(&.PublicKey); != nil {
return
}
func ( io.Reader, int) (*PrivateKey, error) {
return GenerateMultiPrimeKey(, 2, )
}
func ( io.Reader, int, int) (*PrivateKey, error) {
randutil.MaybeReadByte()
:= new(PrivateKey)
.E = 65537
if < 2 {
return nil, errors.New("crypto/rsa: GenerateMultiPrimeKey: nprimes must be >= 2")
}
if < 64 {
func ( *[4]byte) {
if [3]++; [3] != 0 {
return
}
if [2]++; [2] != 0 {
return
}
if [1]++; [1] != 0 {
return
}
[0]++
}
func ( hash.Hash, io.Reader, *PublicKey, []byte, []byte) ([]byte, error) {
if := checkPub(); != nil {
return nil,
}
.Reset()
:= .Size()
if len() > -2*.Size()-2 {
return nil, ErrMessageTooLong
}
.Write()
:= .Sum(nil)
.Reset()
:= make([]byte, )
:= [1 : 1+.Size()]
:= [1+.Size():]
copy([0:.Size()], )
[len()-len()-1] = 1
copy([len()-len():], )
, := io.ReadFull(, )
if != nil {
return nil,
}
mgf1XOR(, , )
mgf1XOR(, , )
:= new(big.Int)
.SetBytes()
:= encrypt(new(big.Int), , )
:= make([]byte, )
return .FillBytes(), nil
}
var ErrDecryption = errors.New("crypto/rsa: decryption error")
var ErrVerification = errors.New("crypto/rsa: verification error")
func ( *PrivateKey) () {
if .Precomputed.Dp != nil {
return
}
.Precomputed.Dp = new(big.Int).Sub(.Primes[0], bigOne)
.Precomputed.Dp.Mod(.D, .Precomputed.Dp)
.Precomputed.Dq = new(big.Int).Sub(.Primes[1], bigOne)
.Precomputed.Dq.Mod(.D, .Precomputed.Dq)
.Precomputed.Qinv = new(big.Int).ModInverse(.Primes[1], .Primes[0])
:= new(big.Int).Mul(.Primes[0], .Primes[1])
.Precomputed.CRTValues = make([]CRTValue, len(.Primes)-2)
for := 2; < len(.Primes); ++ {
:= .Primes[]
:= &.Precomputed.CRTValues[-2]
.Exp = new(big.Int).Sub(, bigOne)
.Exp.Mod(.D, .Exp)
.R = new(big.Int).Set()
.Coeff = new(big.Int).ModInverse(, )
.Mul(, )
}
}
if .Cmp(.N) > 0 {
= ErrDecryption
return
}
if .N.Sign() == 0 {
return nil, ErrDecryption
}
var *big.Int
if != nil {
randutil.MaybeReadByte()
var *big.Int
= new(big.Int)
for {
, = rand.Int(, .N)
if != nil {
return
}
if .Cmp(bigZero) == 0 {
= bigOne
}
:= .ModInverse(, .N)
if != nil {
break
}
}
:= big.NewInt(int64(.E))
:= new(big.Int).Exp(, , .N) // N != 0
:= new(big.Int).Set()
.Mul(, )
.Mod(, .N)
=
}
if .Precomputed.Dp == nil {
= new(big.Int).Exp(, .D, .N)
= new(big.Int).Exp(, .Precomputed.Dp, .Primes[0])
:= new(big.Int).Exp(, .Precomputed.Dq, .Primes[1])
.Sub(, )
if .Sign() < 0 {
.Add(, .Primes[0])
}
.Mul(, .Precomputed.Qinv)
.Mod(, .Primes[0])
.Mul(, .Primes[1])
.Add(, )
for , := range .Precomputed.CRTValues {
:= .Primes[2+]
.Exp(, .Exp, )
.Sub(, )
.Mul(, .Coeff)
.Mod(, )
if .Sign() < 0 {
.Add(, )
}
.Mul(, .R)
.Add(, )
}
}
:= subtle.ConstantTimeCompare(, )
var , , int
= 1
:= [.Size():]
for := 0; < len(); ++ {
:= subtle.ConstantTimeByteEq([], 0)
:= subtle.ConstantTimeByteEq([], 1)
= subtle.ConstantTimeSelect(&, , )
= subtle.ConstantTimeSelect(, 0, )
= subtle.ConstantTimeSelect(&^, 1, )
}
if &&^&^ != 1 {
return nil, ErrDecryption
}
return [+1:], nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |