Skip to content

Commit eeed4b4

Browse files
committed
stupidgcm: implement key wipe
Not bulletproof due to possible GC copies, but still raises to bar for extracting the key. #211
1 parent 7e0fefe commit eeed4b4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

internal/stupidgcm/stupidgcm.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func (g *stupidGCM) Seal(dst, iv, in, authData []byte) []byte {
5656
if len(in) == 0 {
5757
log.Panic("Zero-length input data is not supported")
5858
}
59+
if len(g.key) != keyLen {
60+
log.Panicf("Wrong key length: %d. Key has been wiped?", len(g.key))
61+
}
5962

6063
// If the "dst" slice is large enough we can use it as our output buffer
6164
outLen := len(in) + tagLen
@@ -140,6 +143,9 @@ func (g *stupidGCM) Open(dst, iv, in, authData []byte) ([]byte, error) {
140143
if len(in) <= tagLen {
141144
log.Panic("Input data too short")
142145
}
146+
if len(g.key) != keyLen {
147+
log.Panicf("Wrong key length: %d. Key has been wiped?", len(g.key))
148+
}
143149

144150
// If the "dst" slice is large enough we can use it as our output buffer
145151
outLen := len(in) - tagLen
@@ -224,3 +230,15 @@ func (g *stupidGCM) Open(dst, iv, in, authData []byte) ([]byte, error) {
224230
}
225231
return append(dst, buf...), nil
226232
}
233+
234+
// Wipe wipes the AES key from memory by overwriting it with zeros and
235+
// setting the reference to nil.
236+
//
237+
// This is not bulletproof due to possible GC copies, but
238+
// still raises to bar for extracting the key.
239+
func (g *stupidGCM) Wipe() {
240+
for i := range g.key {
241+
g.key[i] = 0
242+
}
243+
g.key = nil
244+
}

0 commit comments

Comments
 (0)