Skip to content

Commit 33a08b3

Browse files
authored
Merge pull request #247 from ProtonMail/improve-aead
Improve AEAD handling
2 parents 9ad5572 + 531d9f5 commit 33a08b3

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

openpgp/packet/encrypted_key.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ func (e *EncryptedKey) Serialize(w io.Writer) error {
321321

322322
// SerializeEncryptedKeyAEAD serializes an encrypted key packet to w that contains
323323
// key, encrypted to pub.
324-
// If aeadSupported is set, PKESK v6 is used else v4.
324+
// If aeadSupported is set, PKESK v6 is used, otherwise v3.
325+
// Note: aeadSupported MUST match the value passed to SerializeSymmetricallyEncrypted.
325326
// If config is nil, sensible defaults will be used.
326327
func SerializeEncryptedKeyAEAD(w io.Writer, pub *PublicKey, cipherFunc CipherFunction, aeadSupported bool, key []byte, config *Config) error {
327328
return SerializeEncryptedKeyAEADwithHiddenOption(w, pub, cipherFunc, aeadSupported, key, false, config)
@@ -330,7 +331,8 @@ func SerializeEncryptedKeyAEAD(w io.Writer, pub *PublicKey, cipherFunc CipherFun
330331
// SerializeEncryptedKeyAEADwithHiddenOption serializes an encrypted key packet to w that contains
331332
// key, encrypted to pub.
332333
// Offers the hidden flag option to indicated if the PKESK packet should include a wildcard KeyID.
333-
// If aeadSupported is set, PKESK v6 is used else v4.
334+
// If aeadSupported is set, PKESK v6 is used, otherwise v3.
335+
// Note: aeadSupported MUST match the value passed to SerializeSymmetricallyEncrypted.
334336
// If config is nil, sensible defaults will be used.
335337
func SerializeEncryptedKeyAEADwithHiddenOption(w io.Writer, pub *PublicKey, cipherFunc CipherFunction, aeadSupported bool, key []byte, hidden bool, config *Config) error {
336338
var buf [36]byte // max possible header size is v6
@@ -426,6 +428,7 @@ func SerializeEncryptedKeyAEADwithHiddenOption(w io.Writer, pub *PublicKey, ciph
426428
// key, encrypted to pub.
427429
// PKESKv6 is used if config.AEAD() is not nil.
428430
// If config is nil, sensible defaults will be used.
431+
// Deprecated: Use SerializeEncryptedKeyAEAD instead.
429432
func SerializeEncryptedKey(w io.Writer, pub *PublicKey, cipherFunc CipherFunction, key []byte, config *Config) error {
430433
return SerializeEncryptedKeyAEAD(w, pub, cipherFunc, config.AEAD() != nil, key, config)
431434
}
@@ -434,6 +437,7 @@ func SerializeEncryptedKey(w io.Writer, pub *PublicKey, cipherFunc CipherFunctio
434437
// key, encrypted to pub. PKESKv6 is used if config.AEAD() is not nil.
435438
// The hidden option controls if the packet should be anonymous, i.e., omit key metadata.
436439
// If config is nil, sensible defaults will be used.
440+
// Deprecated: Use SerializeEncryptedKeyAEADwithHiddenOption instead.
437441
func SerializeEncryptedKeyWithHiddenOption(w io.Writer, pub *PublicKey, cipherFunc CipherFunction, key []byte, hidden bool, config *Config) error {
438442
return SerializeEncryptedKeyAEADwithHiddenOption(w, pub, cipherFunc, config.AEAD() != nil, key, hidden, config)
439443
}

openpgp/packet/symmetric_key_encrypted.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,21 @@ func SerializeSymmetricKeyEncrypted(w io.Writer, passphrase []byte, config *Conf
195195
// the given passphrase. The returned session key must be passed to
196196
// SerializeSymmetricallyEncrypted.
197197
// If config is nil, sensible defaults will be used.
198+
// Deprecated: Use SerializeSymmetricKeyEncryptedAEADReuseKey instead.
198199
func SerializeSymmetricKeyEncryptedReuseKey(w io.Writer, sessionKey []byte, passphrase []byte, config *Config) (err error) {
200+
return SerializeSymmetricKeyEncryptedAEADReuseKey(w, sessionKey, passphrase, config.AEAD() != nil, config)
201+
}
202+
203+
// SerializeSymmetricKeyEncryptedAEADReuseKey serializes a symmetric key packet to w.
204+
// The packet contains the given session key, encrypted by a key derived from
205+
// the given passphrase. The returned session key must be passed to
206+
// SerializeSymmetricallyEncrypted.
207+
// If aeadSupported is set, SKESK v6 is used, otherwise v4.
208+
// Note: aeadSupported MUST match the value passed to SerializeSymmetricallyEncrypted.
209+
// If config is nil, sensible defaults will be used.
210+
func SerializeSymmetricKeyEncryptedAEADReuseKey(w io.Writer, sessionKey []byte, passphrase []byte, aeadSupported bool, config *Config) (err error) {
199211
var version int
200-
if config.AEAD() != nil {
212+
if aeadSupported {
201213
version = 6
202214
} else {
203215
version = 4

openpgp/packet/symmetrically_encrypted.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func (se *SymmetricallyEncrypted) Decrypt(c CipherFunction, key []byte) (io.Read
7474
// SerializeSymmetricallyEncrypted serializes a symmetrically encrypted packet
7575
// to w and returns a WriteCloser to which the to-be-encrypted packets can be
7676
// written.
77+
// If aeadSupported is set to true, SEIPDv2 is used with the indicated CipherSuite.
78+
// Otherwise, SEIPDv1 is used with the indicated CipherFunction.
79+
// Note: aeadSupported MUST match the value passed to SerializeEncryptedKeyAEAD
80+
// and/or SerializeSymmetricKeyEncryptedAEADReuseKey.
7781
// If config is nil, sensible defaults will be used.
7882
func SerializeSymmetricallyEncrypted(w io.Writer, c CipherFunction, aeadSupported bool, cipherSuite CipherSuite, key []byte, config *Config) (Contents io.WriteCloser, err error) {
7983
writeCloser := noOpCloser{w}

openpgp/v2/write.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,8 @@ func encrypt(
594594
encryptKeys := make([]Key, len(to)+len(toHidden))
595595

596596
config := params.Config
597-
// AEAD is used only if config enables it and every key supports it
598-
aeadSupported := config.AEAD() != nil
597+
// AEAD is used if every key supports it
598+
aeadSupported := true
599599

600600
var intendedRecipients []*packet.Recipient
601601
// Intended Recipient Fingerprint subpacket SHOULD be used when creating a signed and encrypted message
@@ -691,7 +691,7 @@ func encrypt(
691691
}
692692

693693
for _, password := range params.Passwords {
694-
if err = packet.SerializeSymmetricKeyEncryptedReuseKey(params.KeyWriter, params.SessionKey, password, params.Config); err != nil {
694+
if err = packet.SerializeSymmetricKeyEncryptedAEADReuseKey(params.KeyWriter, params.SessionKey, password, aeadSupported, params.Config); err != nil {
695695
return nil, err
696696
}
697697
}

0 commit comments

Comments
 (0)