Skip to content

Commit add07bd

Browse files
committed
Don't allocate the nonce for each chunk
1 parent b01f065 commit add07bd

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

openpgp/packet/aead_crypter.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
type aeadCrypter struct {
1616
aead cipher.AEAD
1717
chunkSize int
18-
initialNonce []byte
18+
nonce []byte
1919
associatedData []byte // Chunk-independent associated data
2020
chunkIndex []byte // Chunk counter
2121
packetTag packetType // SEIP packet (v2) or AEAD Encrypted Data packet
@@ -28,12 +28,12 @@ type aeadCrypter struct {
2828
// 5.16.1 and 5.16.2). It returns the resulting nonce.
2929
func (wo *aeadCrypter) computeNextNonce() (nonce []byte) {
3030
if wo.packetTag == packetTypeSymmetricallyEncryptedIntegrityProtected {
31-
return append(wo.initialNonce, wo.chunkIndex...)
31+
return wo.nonce
3232
}
3333

34-
nonce = make([]byte, len(wo.initialNonce))
35-
copy(nonce, wo.initialNonce)
36-
offset := len(wo.initialNonce) - 8
34+
nonce = make([]byte, len(wo.nonce))
35+
copy(nonce, wo.nonce)
36+
offset := len(wo.nonce) - 8
3737
for i := 0; i < 8; i++ {
3838
nonce[i+offset] ^= wo.chunkIndex[i]
3939
}

openpgp/packet/aead_encrypted.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (ae *AEADEncrypted) decrypt(key []byte) (io.ReadCloser, error) {
7676
aeadCrypter: aeadCrypter{
7777
aead: aead,
7878
chunkSize: chunkSize,
79-
initialNonce: ae.initialNonce,
79+
nonce: ae.initialNonce,
8080
associatedData: ae.associatedData(),
8181
chunkIndex: make([]byte, 8),
8282
packetTag: packetTypeAEADEncrypted,

openpgp/packet/aead_encrypted_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func SerializeAEADEncrypted(w io.Writer, key []byte, config *Config) (io.WriteCl
454454
chunkSize: chunkSize,
455455
associatedData: prefix,
456456
chunkIndex: make([]byte, 8),
457-
initialNonce: nonce,
457+
nonce: nonce,
458458
packetTag: packetTypeAEADEncrypted,
459459
},
460460
writer: writer,

openpgp/packet/symmetrically_encrypted_aead.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ func (se *SymmetricallyEncrypted) decryptAead(inputKey []byte) (io.ReadCloser, e
8181
aeadCrypter: aeadCrypter{
8282
aead: aead,
8383
chunkSize: decodeAEADChunkSize(se.ChunkSizeByte),
84-
initialNonce: nonce,
84+
nonce: nonce,
8585
associatedData: se.associatedData(),
86-
chunkIndex: make([]byte, 8),
86+
chunkIndex: nonce[len(nonce)-8:],
8787
packetTag: packetTypeSymmetricallyEncryptedIntegrityProtected,
8888
},
8989
reader: se.Contents,
@@ -135,8 +135,8 @@ func serializeSymmetricallyEncryptedAead(ciphertext io.WriteCloser, cipherSuite
135135
aead: aead,
136136
chunkSize: decodeAEADChunkSize(chunkSizeByte),
137137
associatedData: prefix,
138-
chunkIndex: make([]byte, 8),
139-
initialNonce: nonce,
138+
nonce: nonce,
139+
chunkIndex: nonce[len(nonce)-8:],
140140
packetTag: packetTypeSymmetricallyEncryptedIntegrityProtected,
141141
},
142142
writer: ciphertext,
@@ -149,10 +149,10 @@ func getSymmetricallyEncryptedAeadInstance(c CipherFunction, mode AEADMode, inpu
149149
encryptionKey := make([]byte, c.KeySize())
150150
_, _ = readFull(hkdfReader, encryptionKey)
151151

152-
// Last 64 bits of nonce are the counter
153-
nonce = make([]byte, mode.IvLength()-8)
152+
nonce = make([]byte, mode.IvLength())
154153

155-
_, _ = readFull(hkdfReader, nonce)
154+
// Last 64 bits of nonce are the counter
155+
_, _ = readFull(hkdfReader, nonce[:len(nonce)-8])
156156

157157
blockCipher := c.new(encryptionKey)
158158
aead = mode.new(blockCipher)

0 commit comments

Comments
 (0)