@@ -32,7 +32,9 @@ func New(key []byte) cipher.AEAD {
32
32
}
33
33
34
34
// Same as "New" without the 64-byte restriction.
35
- func new2 (key []byte ) cipher.AEAD {
35
+ func new2 (keyIn []byte ) cipher.AEAD {
36
+ // Create a private copy so the caller can zero the one he owns
37
+ key := append ([]byte {}, keyIn ... )
36
38
return & sivAead {
37
39
key : key ,
38
40
}
@@ -53,6 +55,9 @@ func (s *sivAead) Seal(dst, nonce, plaintext, authData []byte) []byte {
53
55
// SIV supports any nonce size, but in gocryptfs we exclusively use 16.
54
56
log .Panic ("nonce must be 16 bytes long" )
55
57
}
58
+ if len (s .key ) == 0 {
59
+ log .Panic ("Key has been wiped?" )
60
+ }
56
61
// https://github.com/jacobsa/crypto/blob/master/siv/encrypt.go#L48:
57
62
// As per RFC 5297 section 3, you may use this function for nonce-based
58
63
// authenticated encryption by passing a nonce as the last associated
@@ -71,7 +76,22 @@ func (s *sivAead) Open(dst, nonce, ciphertext, authData []byte) ([]byte, error)
71
76
// SIV supports any nonce size, but in gocryptfs we exclusively use 16.
72
77
log .Panic ("nonce must be 16 bytes long" )
73
78
}
79
+ if len (s .key ) == 0 {
80
+ log .Panic ("Key has been wiped?" )
81
+ }
74
82
associated := [][]byte {authData , nonce }
75
83
dec , err := siv .Decrypt (s .key , ciphertext , associated )
76
84
return append (dst , dec ... ), err
77
85
}
86
+
87
+ // Wipe tries to wipe the AES key from memory by overwriting it with zeros
88
+ // and setting the reference to nil.
89
+ //
90
+ // This is not bulletproof due to possible GC copies, but
91
+ // still raises to bar for extracting the key.
92
+ func (s * sivAead ) Wipe () {
93
+ for i := range s .key {
94
+ s .key [i ] = 0
95
+ }
96
+ s .key = nil
97
+ }
0 commit comments