Skip to content

Commit 9ad5572

Browse files
authored
Adapt aead preferences on key generation (#248)
Advertise SEIPDv2 and AEAD modes during key generation only if AEAD configuration is enabled.
1 parent d7733dc commit 9ad5572

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

openpgp/key_generation.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ func (t *Entity) AddUserId(name, comment, email string, config *packet.Config) e
9191
}
9292

9393
func writeKeyProperties(selfSignature *packet.Signature, creationTime time.Time, keyLifetimeSecs uint32, config *packet.Config) error {
94+
advertiseAead := config.AEAD() != nil
95+
9496
selfSignature.CreationTime = creationTime
9597
selfSignature.KeyLifetimeSecs = &keyLifetimeSecs
9698
selfSignature.FlagsValid = true
9799
selfSignature.FlagSign = true
98100
selfSignature.FlagCertify = true
99101
selfSignature.SEIPDv1 = true // true by default, see 5.8 vs. 5.14
100-
selfSignature.SEIPDv2 = config.AEAD() != nil
102+
selfSignature.SEIPDv2 = advertiseAead
101103

102104
// Set the PreferredHash for the SelfSignature from the packet.Config.
103105
// If it is not the must-implement algorithm from rfc4880bis, append that.
@@ -126,16 +128,19 @@ func writeKeyProperties(selfSignature *packet.Signature, creationTime time.Time,
126128
selfSignature.PreferredCompression = append(selfSignature.PreferredCompression, uint8(config.Compression()))
127129
}
128130

129-
// And for DefaultMode.
130-
modes := []uint8{uint8(config.AEAD().Mode())}
131-
if config.AEAD().Mode() != packet.AEADModeOCB {
132-
modes = append(modes, uint8(packet.AEADModeOCB))
133-
}
131+
if advertiseAead {
132+
// Get the preferred AEAD mode from the packet.Config.
133+
// If it is not the must-implement algorithm from rfc9580, append that.
134+
modes := []uint8{uint8(config.AEAD().Mode())}
135+
if config.AEAD().Mode() != packet.AEADModeOCB {
136+
modes = append(modes, uint8(packet.AEADModeOCB))
137+
}
134138

135-
// For preferred (AES256, GCM), we'll generate (AES256, GCM), (AES256, OCB), (AES128, GCM), (AES128, OCB)
136-
for _, cipher := range selfSignature.PreferredSymmetric {
137-
for _, mode := range modes {
138-
selfSignature.PreferredCipherSuites = append(selfSignature.PreferredCipherSuites, [2]uint8{cipher, mode})
139+
// For preferred (AES256, GCM), we'll generate (AES256, GCM), (AES256, OCB), (AES128, GCM), (AES128, OCB)
140+
for _, cipher := range selfSignature.PreferredSymmetric {
141+
for _, mode := range modes {
142+
selfSignature.PreferredCipherSuites = append(selfSignature.PreferredCipherSuites, [2]uint8{cipher, mode})
143+
}
139144
}
140145
}
141146
return nil

openpgp/v2/key_generation.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,15 @@ func (t *Entity) AddDirectKeySignature(selectedKeyProperties *keyProperties, con
147147
}
148148

149149
func writeKeyProperties(selfSignature *packet.Signature, selectedKeyProperties *keyProperties) error {
150+
advertiseAead := selectedKeyProperties.aead != nil
151+
150152
selfSignature.CreationTime = selectedKeyProperties.creationTime
151153
selfSignature.KeyLifetimeSecs = &selectedKeyProperties.keyLifetimeSecs
152154
selfSignature.FlagsValid = true
153155
selfSignature.FlagSign = true
154156
selfSignature.FlagCertify = true
155157
selfSignature.SEIPDv1 = true // true by default, see 5.8 vs. 5.14
156-
selfSignature.SEIPDv2 = selectedKeyProperties.aead != nil
158+
selfSignature.SEIPDv2 = advertiseAead
157159

158160
// Set the PreferredHash for the SelfSignature from the packet.Config.
159161
// If it is not the must-implement algorithm from rfc4880bis, append that.
@@ -197,18 +199,22 @@ func writeKeyProperties(selfSignature *packet.Signature, selectedKeyProperties *
197199
selfSignature.PreferredCompression = append(selfSignature.PreferredCompression, uint8(selectedKeyProperties.compression))
198200
}
199201

200-
// And for DefaultMode.
201-
modes := []uint8{uint8(selectedKeyProperties.aead.Mode())}
202-
if selectedKeyProperties.aead.Mode() != packet.AEADModeOCB {
203-
modes = append(modes, uint8(packet.AEADModeOCB))
204-
}
202+
if advertiseAead {
203+
// Get the preferred AEAD mode from the packet.Config.
204+
// If it is not the must-implement algorithm from rfc9580, append that.
205+
modes := []uint8{uint8(selectedKeyProperties.aead.Mode())}
206+
if selectedKeyProperties.aead.Mode() != packet.AEADModeOCB {
207+
modes = append(modes, uint8(packet.AEADModeOCB))
208+
}
205209

206-
// For preferred (AES256, GCM), we'll generate (AES256, GCM), (AES256, OCB), (AES128, GCM), (AES128, OCB)
207-
for _, cipher := range selfSignature.PreferredSymmetric {
208-
for _, mode := range modes {
209-
selfSignature.PreferredCipherSuites = append(selfSignature.PreferredCipherSuites, [2]uint8{cipher, mode})
210+
// For preferred (AES256, GCM), we'll generate (AES256, GCM), (AES256, OCB), (AES128, GCM), (AES128, OCB)
211+
for _, cipher := range selfSignature.PreferredSymmetric {
212+
for _, mode := range modes {
213+
selfSignature.PreferredCipherSuites = append(selfSignature.PreferredCipherSuites, [2]uint8{cipher, mode})
214+
}
210215
}
211216
}
217+
212218
return nil
213219
}
214220

openpgp/v2/keys.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ func (e *Entity) PrimaryIdentity(date time.Time, config *packet.Config) (*packet
6161
var primaryIdentityCandidatesSelfSigs []*packet.Signature
6262
for _, identity := range e.Identities {
6363
selfSig, err := identity.Verify(date, config) // identity must be valid at date
64-
if err == nil { // verification is successful
64+
if err == nil {
65+
// verification is successful
6566
primaryIdentityCandidates = append(primaryIdentityCandidates, identity)
6667
primaryIdentityCandidatesSelfSigs = append(primaryIdentityCandidatesSelfSigs, selfSig)
6768
}

0 commit comments

Comments
 (0)