Skip to content

Commit

Permalink
stupidgcm: implement key wipe
Browse files Browse the repository at this point in the history
Not bulletproof due to possible GC copies, but
still raises to bar for extracting the key.

#211
  • Loading branch information
rfjakob committed Feb 17, 2018
1 parent 7e0fefe commit eeed4b4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/stupidgcm/stupidgcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func (g *stupidGCM) Seal(dst, iv, in, authData []byte) []byte {
if len(in) == 0 {
log.Panic("Zero-length input data is not supported")
}
if len(g.key) != keyLen {
log.Panicf("Wrong key length: %d. Key has been wiped?", len(g.key))
}

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

// If the "dst" slice is large enough we can use it as our output buffer
outLen := len(in) - tagLen
Expand Down Expand Up @@ -224,3 +230,15 @@ func (g *stupidGCM) Open(dst, iv, in, authData []byte) ([]byte, error) {
}
return append(dst, buf...), nil
}

// Wipe wipes the AES key from memory by overwriting it with zeros and
// setting the reference to nil.
//
// This is not bulletproof due to possible GC copies, but
// still raises to bar for extracting the key.
func (g *stupidGCM) Wipe() {
for i := range g.key {
g.key[i] = 0
}
g.key = nil
}

0 comments on commit eeed4b4

Please sign in to comment.