From f59690be3f9a31481d227f885bfe2466be0c5d27 Mon Sep 17 00:00:00 2001 From: Adam Langley Date: Fri, 21 Nov 2014 14:11:46 -0800 Subject: [PATCH] go.crypto/openpgp: fix nil Config case. This is a followup to https://golang.org/cl/176080043/. That change didn't handle the case of a *Config being nil. CC=golang-codereviews https://golang.org/cl/179200043 --- openpgp/packet/config.go | 7 +++++++ openpgp/packet/symmetric_key_encrypted.go | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/openpgp/packet/config.go b/openpgp/packet/config.go index 3f17096..d977cde 100644 --- a/openpgp/packet/config.go +++ b/openpgp/packet/config.go @@ -79,3 +79,10 @@ func (c *Config) Compression() CompressionAlgo { } return c.DefaultCompressionAlgo } + +func (c *Config) PasswordHashIterations() int { + if c == nil || c.S2KCount == 0 { + return 0 + } + return c.S2KCount +} diff --git a/openpgp/packet/symmetric_key_encrypted.go b/openpgp/packet/symmetric_key_encrypted.go index 21739a1..7dff655 100644 --- a/openpgp/packet/symmetric_key_encrypted.go +++ b/openpgp/packet/symmetric_key_encrypted.go @@ -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(), S2KCount: config.S2KCount}) + err = s2k.Serialize(s2kBuf, keyEncryptingKey, config.Random(), passphrase, &s2k.Config{Hash: config.Hash(), S2KCount: config.PasswordHashIterations()}) if err != nil { return }