Skip to content

Commit aaeb239

Browse files
authored
Support optional authenticated data in local encrypt/decrypt (#424)
1 parent 8b3f0f7 commit aaeb239

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

pkg/vault/cryptography.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import (
99
)
1010

1111
type EncryptOpts struct {
12-
Data string
13-
KeyContext KeyContext
12+
Data string
13+
KeyContext KeyContext
14+
AssociatedData string
1415
}
1516

1617
type DecryptOpts struct {
17-
Data string
18+
Data string
19+
AssociatedData string
1820
}
1921

2022
type Decoded struct {
@@ -28,6 +30,7 @@ type Decoded struct {
2830
func LocalEncrypt(
2931
data string,
3032
keyPair DataKeyPair,
33+
associatedData string,
3134
) (string, error) {
3235
// Decode the plaintext data key
3336
dataKey, err := base64.StdEncoding.DecodeString(keyPair.DataKey)
@@ -56,7 +59,7 @@ func LocalEncrypt(
5659
iv := make([]byte, 32)
5760
rand.Read(iv)
5861

59-
ciphertext := aesgcm.Seal(nil, iv, []byte(data), nil)
62+
ciphertext := aesgcm.Seal(nil, iv, []byte(data), []byte(associatedData))
6063
ciphertextLen := len(ciphertext) - 16
6164
authTag := ciphertext[ciphertextLen:]
6265
payload := append(iv, authTag[:]...)
@@ -71,6 +74,7 @@ func LocalEncrypt(
7174
func LocalDecrypt(
7275
decoded Decoded,
7376
dataKey DataKey,
77+
associatedData string,
7478
) (string, error) {
7579
key, err := base64.StdEncoding.DecodeString(dataKey.Key)
7680
if err != nil {
@@ -87,7 +91,7 @@ func LocalDecrypt(
8791
return "", err
8892
}
8993

90-
plaintext, err := aesgcm.Open(nil, decoded.Iv, append(decoded.Ciphertext, decoded.Tag[:]...), nil)
94+
plaintext, err := aesgcm.Open(nil, decoded.Iv, append(decoded.Ciphertext, decoded.Tag[:]...), []byte(associatedData))
9195
if err != nil {
9296
return "", err
9397
}

pkg/vault/cryptography_test.go

+27-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestLocalDecrypt(t *testing.T) {
5555
require.Equal(t, iv, decoded.Iv)
5656
require.Equal(t, authTag, decoded.Tag)
5757

58-
plaintext, err := LocalDecrypt(decoded, dataKey)
58+
plaintext, err := LocalDecrypt(decoded, dataKey, "")
5959
require.NoError(t, err)
6060
require.Equal(t, expected, plaintext)
6161
}
@@ -68,13 +68,37 @@ func TestLocalEncryptAndDecrypt(t *testing.T) {
6868
}
6969
data := "super secret access codes"
7070

71-
ciphertext, err := LocalEncrypt(data, keyPair)
71+
ciphertext, err := LocalEncrypt(data, keyPair, "")
7272
require.NoError(t, err)
7373

7474
decoded, err := Decode(ciphertext)
7575
require.NoError(t, err)
7676

77-
plaintext, err := LocalDecrypt(decoded, DataKey{Id: keyPair.Id, Key: keyPair.DataKey})
77+
plaintext, err := LocalDecrypt(decoded, DataKey{Id: keyPair.Id, Key: keyPair.DataKey}, "")
78+
require.NoError(t, err)
79+
require.Equal(t, data, plaintext)
80+
}
81+
82+
func TestLocalEncryptWithAssociatedData(t *testing.T) {
83+
keyPair := DataKeyPair{
84+
DataKey: "hNjAWl++MJjDZ64dUeYlgJZDEbemRmdKvNHUnnRFUNg=",
85+
Id: "0205e0ec-828e-5594-96ac-a68fc8257fb7",
86+
EncryptedKeys: "V09TLkVLTS52MQAwMjA1ZTBlYy04MjhlLTU1OTQtOTZhYy1hNjhmYzgyNTdmYjcBATEBJGNmMjllNjhhLWYzMmQtNDI4YS05NDg2LTY5YjAyM2JmNjUyNAF0Y2YyOWU2OGEtZjMyZC00MjhhLTk0ODYtNjliMDIzYmY2NTI0uRXneWi4j8iJN4vYJQfGWJVDhk3ogkZmUda857GKGPgneo0xw+M7O5Tg/Z1WbfPPc+C5ncUpj1sHz5LUaU6uSyAO48f4CdpK3dn6UjErRUM=",
87+
}
88+
data := "super secret access codes"
89+
aad := "seq1"
90+
91+
ciphertext, err := LocalEncrypt(data, keyPair, aad)
92+
require.NoError(t, err)
93+
94+
decoded, err := Decode(ciphertext)
95+
require.NoError(t, err)
96+
97+
plaintext, err := LocalDecrypt(decoded, DataKey{Id: keyPair.Id, Key: keyPair.DataKey}, "seq2")
98+
require.EqualError(t, err, "cipher: message authentication failed")
99+
require.Equal(t, "", plaintext)
100+
101+
plaintext, err = LocalDecrypt(decoded, DataKey{Id: keyPair.Id, Key: keyPair.DataKey}, aad)
78102
require.NoError(t, err)
79103
require.Equal(t, data, plaintext)
80104
}

pkg/vault/vault.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func Encrypt(
9797
return "", err
9898
}
9999

100-
return LocalEncrypt(opts.Data, dataKeyPair)
100+
return LocalEncrypt(opts.Data, dataKeyPair, opts.AssociatedData)
101101
}
102102

103103
// Decrypt perfroms a local decryption of data that was previously encrypted with Vault.
@@ -116,5 +116,5 @@ func Decrypt(
116116
return "", err
117117
}
118118

119-
return LocalDecrypt(decoded, dataKey)
119+
return LocalDecrypt(decoded, dataKey, opts.AssociatedData)
120120
}

0 commit comments

Comments
 (0)