rsa
7 分钟阅读
rsa
https://pkg.go.dev/crypto/rsa@go1.20.1
Package rsa implements RSA encryption as specified in PKCS #1 and RFC 8017.
RSA is a single, fundamental operation that is used in this package to implement either public-key encryption or public-key signatures.
The original specification for encryption and signatures with RSA is PKCS #1 and the terms “RSA encryption” and “RSA signatures” by default refer to PKCS #1 version 1.5. However, that specification has flaws and new designs should use version 2, usually called by just OAEP and PSS, where possible.
Two sets of interfaces are included in this package. When a more abstract interface isn’t necessary, there are functions for encrypting/decrypting with v1.5/OAEP and signing/verifying with v1.5/PSS. If one needs to abstract over the public key primitive, the PrivateKey type implements the Decrypter and Signer interfaces from the crypto package.
Operations in this package are implemented using constant-time algorithms, except for GenerateKey, PrivateKey.Precompute, and PrivateKey.Validate. Every other operation only leaks the bit size of the involved values, which all depend on the selected key size.
常量
|
|
变量
var ErrDecryption = errors.New("crypto/rsa: decryption error")
ErrDecryption represents a failure to decrypt a message. It is deliberately vague to avoid adaptive attacks.
var ErrMessageTooLong = errors.New("crypto/rsa: message too long for RSA key size")
ErrMessageTooLong is returned when attempting to encrypt or sign a message which is too large for the size of the key. When using SignPSS, this can also be returned if the size of the salt is too large.
var ErrVerification = errors.New("crypto/rsa: verification error")
ErrVerification represents a failure to verify a signature. It is deliberately vague to avoid adaptive attacks.
函数
func DecryptOAEP
|
|
DecryptOAEP decrypts ciphertext using RSA-OAEP.
OAEP is parameterised by a hash function that is used as a random oracle. Encryption and decryption of a given message must use the same hash function and sha256.New() is a reasonable choice.
The random parameter is legacy and ignored, and it can be as nil.
The label parameter must match the value given when encrypting. See EncryptOAEP for details.
DecryptOAEP Example
|
|
func DecryptPKCS1v15
|
|
DecryptPKCS1v15 decrypts a plaintext using RSA and the padding scheme from PKCS #1 v1.5. The random parameter is legacy and ignored, and it can be as nil.
Note that whether this function returns an error or not discloses secret information. If an attacker can cause this function to run repeatedly and learn whether each instance returned an error then they can decrypt and forge signatures as if they had the private key. See DecryptPKCS1v15SessionKey for a way of solving this problem.
func DecryptPKCS1v15SessionKey
|
|
DecryptPKCS1v15SessionKey decrypts a session key using RSA and the padding scheme from PKCS #1 v1.5. The random parameter is legacy and ignored, and it can be as nil. It returns an error if the ciphertext is the wrong length or if the ciphertext is greater than the public modulus. Otherwise, no error is returned. If the padding is valid, the resulting plaintext message is copied into key. Otherwise, key is unchanged. These alternatives occur in constant time. It is intended that the user of this function generate a random session key beforehand and continue the protocol with the resulting value. This will remove any possibility that an attacker can learn any information about the plaintext. See “Chosen Ciphertext Attacks Against Protocols Based on the RSA Encryption Standard PKCS #1”, Daniel Bleichenbacher, Advances in Cryptology (Crypto ‘98).
Note that if the session key is too small then it may be possible for an attacker to brute-force it. If they can do that then they can learn whether a random value was used (because it’ll be different for the same ciphertext) and thus whether the padding was correct. This defeats the point of this function. Using at least a 16-byte key will protect against this attack.
DecryptPKCS1v15SessionKey Example
RSA is able to encrypt only a very limited amount of data. In order to encrypt reasonable amounts of data a hybrid scheme is commonly used: RSA is used to encrypt a key for a symmetric primitive like AES-GCM.
Before encrypting, data is “padded” by embedding it in a known structure. This is done for a number of reasons, but the most obvious is to ensure that the value is large enough that the exponentiation is larger than the modulus. (Otherwise it could be decrypted with a square-root.)
In these designs, when using PKCS #1 v1.5, it’s vitally important to avoid disclosing whether the received RSA message was well-formed (that is, whether the result of decrypting is a correctly padded message) because this leaks secret information. DecryptPKCS1v15SessionKey is designed for this situation and copies the decrypted, symmetric key (if well-formed) in constant-time over a buffer that contains a random key. Thus, if the RSA result isn’t well-formed, the implementation uses a random key in constant time.
|
|
func EncryptOAEP
|
|
EncryptOAEP encrypts the given message with RSA-OAEP.
OAEP is parameterised by a hash function that is used as a random oracle. Encryption and decryption of a given message must use the same hash function and sha256.New() is a reasonable choice.
The random parameter is used as a source of entropy to ensure that encrypting the same message twice doesn’t result in the same ciphertext.
The label parameter may contain arbitrary data that will not be encrypted, but which gives important context to the message. For example, if a given public key is used to encrypt two types of messages then distinct label values could be used to ensure that a ciphertext for one purpose cannot be used for another by an attacker. If not required it can be empty.
The message must be no longer than the length of the public modulus minus twice the hash length, minus a further 2.
EncryptOAEP Example
|
|
func EncryptPKCS1v15
|
|
EncryptPKCS1v15 encrypts the given message with RSA and the padding scheme from PKCS #1 v1.5. The message must be no longer than the length of the public modulus minus 11 bytes.
The random parameter is used as a source of entropy to ensure that encrypting the same message twice doesn’t result in the same ciphertext.
WARNING: use of this function to encrypt plaintexts other than session keys is dangerous. Use RSA OAEP in new protocols.
func SignPKCS1v15
|
|
SignPKCS1v15 calculates the signature of hashed using RSASSA-PKCS1-V1_5-SIGN from RSA PKCS #1 v1.5. Note that hashed must be the result of hashing the input message using the given hash function. If hash is zero, hashed is signed directly. This isn’t advisable except for interoperability.
The random parameter is legacy and ignored, and it can be as nil.
This function is deterministic. Thus, if the set of possible messages is small, an attacker may be able to build a map from messages to signatures and identify the signed messages. As ever, signatures provide authenticity, not confidentiality.
SignPKCS1v15 Example
|
|
func SignPSS <- go1.2
|
|
SignPSS calculates the signature of digest using PSS.
digest must be the result of hashing the input message using the given hash function. The opts argument may be nil, in which case sensible defaults are used. If opts.Hash is set, it overrides hash.
func VerifyPKCS1v15
|
|
VerifyPKCS1v15 verifies an RSA PKCS #1 v1.5 signature. hashed is the result of hashing the input message using the given hash function and sig is the signature. A valid signature is indicated by returning a nil error. If hash is zero then hashed is used directly. This isn’t advisable except for interoperability.
VerifyPKCS1v15 Example
|
|
func VerifyPSS <- go1.2
|
|
VerifyPSS verifies a PSS signature.
A valid signature is indicated by returning a nil error. digest must be the result of hashing the input message using the given hash function. The opts argument may be nil, in which case sensible defaults are used. opts.Hash is ignored.
类型
type CRTValue
|
|
CRTValue contains the precomputed Chinese remainder theorem values.
type OAEPOptions <- go1.5
|
|
OAEPOptions is an interface for passing options to OAEP decryption using the crypto.Decrypter interface.
type PKCS1v15DecryptOptions <- go1.5
|
|
PKCS1v15DecryptOptions is for passing options to PKCS #1 v1.5 decryption using the crypto.Decrypter interface.
type PSSOptions <- go1.2
|
|
PSSOptions contains options for creating and verifying PSS signatures.
(*PSSOptions) HashFunc <- go1.4
|
|
HashFunc returns opts.Hash so that PSSOptions implements crypto.SignerOpts.
type PrecomputedValues
|
|
type PrivateKey
|
|
A PrivateKey represents an RSA key
func GenerateKey
|
|
GenerateKey generates an RSA keypair of the given bit size using the random source random (for example, crypto/rand.Reader).
func GenerateMultiPrimeKey
|
|
GenerateMultiPrimeKey generates a multi-prime RSA keypair of the given bit size and the given random source.
Table 1 in “On the Security of Multi-prime RSA” suggests maximum numbers of primes for a given bit size.
Although the public keys are compatible (actually, indistinguishable) from the 2-prime case, the private keys are not. Thus it may not be possible to export multi-prime private keys in certain formats or to subsequently import them into other code.
This package does not implement CRT optimizations for multi-prime RSA, so the keys with more than two primes will have worse performance.
Note: The use of this function with a number of primes different from two is not recommended for the above security, compatibility, and performance reasons. Use GenerateKey instead.
(*PrivateKey) Decrypt <- go1.5
|
|
Decrypt decrypts ciphertext with priv. If opts is nil or of type *PKCS1v15DecryptOptions then PKCS #1 v1.5 decryption is performed. Otherwise opts must have type *OAEPOptions and OAEP decryption is done.
(*PrivateKey) Equal <- go1.15
|
|
Equal reports whether priv and x have equivalent values. It ignores Precomputed values.
(*PrivateKey) Precompute
|
|
Precompute performs some calculations that speed up private key operations in the future.
(*PrivateKey) Public <- go1.4
|
|
Public returns the public key corresponding to priv.
(*PrivateKey) Sign <- go1.4
|
|
Sign signs digest with priv, reading randomness from rand. If opts is a *PSSOptions then the PSS algorithm will be used, otherwise PKCS #1 v1.5 will be used. digest must be the result of hashing the input message using opts.HashFunc().
This method implements crypto.Signer, which is an interface to support keys where the private part is kept in, for example, a hardware module. Common uses should use the Sign* functions in this package directly.
(*PrivateKey) Validate
|
|
Validate performs basic sanity checks on the key. It returns nil if the key is valid, or else an error describing a problem.
type PublicKey
|
|
A PublicKey represents the public part of an RSA key.
(*PublicKey) Equal <- go1.15
|
|
Equal reports whether pub and x have the same value.
(*PublicKey) Size <- go1.11
|
|
Size returns the modulus size in bytes. Raw signatures and ciphertexts for or by this public key will have the same size.