@@ -122,14 +122,21 @@ func (key *PrivateKey) Sign(r io.Reader, msg []byte, opts crypto.SignerOpts) ([]
122
122
123
123
// Decrypt implements the crypto.Decrypter operation for the given key.
124
124
func (key * PrivateKey ) Decrypt (rand io.Reader , msg []byte , opts crypto.DecrypterOpts ) ([]byte , error ) {
125
- switch opts := opts .(type ) {
126
- case * rsa.PKCS1v15DecryptOptions :
127
- ptxt , decyptErr := key .execute (gokeyless .OpRSADecrypt , msg )
125
+ opts1v15 , ok := opts .(* rsa.PKCS1v15DecryptOptions )
126
+ if opts != nil && ! ok {
127
+ return nil , errors .New ("invalid options for Decrypt" )
128
+ }
129
+
130
+ ptxt , err := key .execute (gokeyless .OpRSADecrypt , msg )
131
+ if err != nil {
132
+ return nil , err
133
+ }
128
134
135
+ if ok {
129
136
// If opts.SessionKeyLen is set, we must perform a variation of
130
137
// rsa.DecryptPKCS1v15SessionKey to ensure the entire operation
131
138
// is performed in constant time regardless of padding errors.
132
- if l := opts .SessionKeyLen ; l > 0 {
139
+ if l := opts1v15 .SessionKeyLen ; l > 0 {
133
140
plaintext := make ([]byte , l )
134
141
if _ , err := io .ReadFull (rand , plaintext ); err != nil {
135
142
return nil , err
@@ -140,9 +147,6 @@ func (key *PrivateKey) Decrypt(rand io.Reader, msg []byte, opts crypto.Decrypter
140
147
subtle .ConstantTimeCopy (valid , plaintext [:l2 ], ptxt [:l2 ])
141
148
return plaintext , nil
142
149
}
143
- // Otherwise, we can just return the error like rsa.DecryptPKCS1v15.
144
- return ptxt , decyptErr
145
- default :
146
- return nil , errors .New ("invalid options for Decrypt" )
147
150
}
151
+ return ptxt , nil
148
152
}
0 commit comments