go.crypto/openpgp: Allow config. of s2k count for symmetric encrypt.
This patch allows the user to choose the strength of the passphrase mangling during the process in which the key is produced from the passphrase. It only affects symmetric encryption. Unmodified code that calls openpgp.SymmetricallyEncrypt() will continue to get the now-default count of 65536. Otherwise, a count in the range [1024, 65011712] may be configured. Illegal values in and outside this range will silently be rounded up to legal values within the said range. The test to s2k.Serialize() has been modified to test a variety of non-default counts with all the valid hashes and ensure that the decoding component of s2k can parse and decrypt the result. Additional testing has been done with GPG to ensure that the latter can parse and decrypt files encrypted/encoded with various counts. LGTM=agl R=golang-codereviews, agl CC=golang-codereviews https://golang.org/cl/176080043
This commit is contained in:
committed by
Adam Langley
parent
5ff91abc76
commit
ca455997ca
@@ -32,6 +32,17 @@ type Config struct {
|
||||
DefaultCompressionAlgo CompressionAlgo
|
||||
// CompressionConfig configures the compression settings.
|
||||
CompressionConfig *CompressionConfig
|
||||
// S2KCount is only used for symmetric encryption. It
|
||||
// determines the strength of the passphrase stretching when
|
||||
// the said passphrase is hashed to produce a key. S2KCount
|
||||
// should be between 1024 and 65011712, inclusive. If Config
|
||||
// is nil or S2KCount is 0, the value 65536 used. Not all
|
||||
// values in the above range can be represented. S2KCount will
|
||||
// be rounded up to the next representable value if it cannot
|
||||
// be encoded exactly. When set, it is strongly encrouraged to
|
||||
// use a value that is at least 65536. See RFC 4880 Section
|
||||
// 3.7.1.3.
|
||||
S2KCount int
|
||||
}
|
||||
|
||||
func (c *Config) Random() io.Reader {
|
||||
|
||||
@@ -120,7 +120,7 @@ func SerializeSymmetricKeyEncrypted(w io.Writer, passphrase []byte, config *Conf
|
||||
keyEncryptingKey := make([]byte, keySize)
|
||||
// s2k.Serialize salts and stretches the passphrase, and writes the
|
||||
// resulting key to keyEncryptingKey and the s2k descriptor to s2kBuf.
|
||||
err = s2k.Serialize(s2kBuf, keyEncryptingKey, config.Random(), passphrase, &s2k.Config{Hash: config.Hash()})
|
||||
err = s2k.Serialize(s2kBuf, keyEncryptingKey, config.Random(), passphrase, &s2k.Config{Hash: config.Hash(), S2KCount: config.S2KCount})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user