Skip to content

Commit 4e25a67

Browse files
author
Michelle Clayton
committed
Add comment and wrap AddCleanup
1 parent 7b3699e commit 4e25a67

File tree

11 files changed

+32
-25
lines changed

11 files changed

+32
-25
lines changed

cng/aes.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewAESCipher(key []byte) (cipher.Block, error) {
3030
return nil, err
3131
}
3232
c := &aesCipher{kh: kh, key: bytes.Clone(key)}
33-
runtime.AddCleanup(c, destroyKey, kh)
33+
addCleanupKey(c, kh)
3434
return c, nil
3535
}
3636

@@ -161,7 +161,7 @@ func newCBC(encrypt bool, alg string, key, iv []byte) *cbcCipher {
161161
panic(err)
162162
}
163163
x := &cbcCipher{kh: kh, encrypt: encrypt, blockSize: blockSize}
164-
runtime.AddCleanup(x, destroyKey, kh)
164+
addCleanupKey(x, kh)
165165
x.SetIV(iv)
166166
return x
167167
}
@@ -245,7 +245,7 @@ func newGCM(key []byte, tls cipherGCMTLS) (*aesGCM, error) {
245245
return nil, err
246246
}
247247
g := &aesGCM{kh: kh, tls: tls}
248-
runtime.AddCleanup(g, destroyKey, kh)
248+
addCleanupKey(g, kh)
249249
return g, nil
250250
}
251251

cng/chacha20poly1305.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewChaCha20Poly1305(key []byte) (cipher.AEAD, error) {
4040
return nil, err
4141
}
4242
c := &chacha20poly1305{kh: kh}
43-
runtime.AddCleanup(c, destroyKey, kh)
43+
addCleanupKey(c, kh)
4444
return c, nil
4545
}
4646

cng/des.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func NewDESCipher(key []byte) (cipher.Block, error) {
2929
return nil, err
3030
}
3131
c := &desCipher{kh: kh, alg: bcrypt.DES_ALGORITHM, key: bytes.Clone(key)}
32-
runtime.AddCleanup(c, destroyKey, kh)
32+
addCleanupKey(c, kh)
3333
return c, nil
3434
}
3535

@@ -39,7 +39,7 @@ func NewTripleDESCipher(key []byte) (cipher.Block, error) {
3939
return nil, err
4040
}
4141
c := &desCipher{kh: kh, alg: bcrypt.DES3_ALGORITHM, key: bytes.Clone(key)}
42-
runtime.AddCleanup(c, destroyKey, kh)
42+
addCleanupKey(c, kh)
4343
return c, nil
4444
}
4545

cng/dsa.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func NewPrivateKeyDSA(params DSAParameters, X, Y BigInt) (*PrivateKeyDSA, error)
147147
return nil, err
148148
}
149149
k := &PrivateKeyDSA{params, X, Y, hkey}
150-
runtime.AddCleanup(k, destroyKey, hkey)
150+
addCleanupKey(k, hkey)
151151
return k, nil
152152
}
153153

@@ -166,7 +166,7 @@ func NewPublicKeyDSA(params DSAParameters, Y BigInt) (*PublicKeyDSA, error) {
166166
return nil, err
167167
}
168168
k := &PublicKeyDSA{params, Y, hkey}
169-
runtime.AddCleanup(k, destroyKey, hkey)
169+
addCleanupKey(k, hkey)
170170
return k, nil
171171
}
172172

cng/ecdh.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func GenerateKeyECDH(curve string) (*PrivateKeyECDH, []byte, error) {
128128
bytes = bytes[hdr.KeySize*2:]
129129

130130
k := &PrivateKeyECDH{hkey, isNIST(curve)}
131-
runtime.AddCleanup(k, destroyKey, hkey)
131+
addCleanupKey(k, hkey)
132132
return k, bytes, nil
133133
}
134134

@@ -163,7 +163,7 @@ func NewPublicKeyECDH(curve string, bytes []byte) (*PublicKeyECDH, error) {
163163
return nil, err
164164
}
165165
k := &PublicKeyECDH{hkey, append([]byte(nil), bytes...), nil}
166-
runtime.AddCleanup(k, destroyKey, hkey)
166+
addCleanupKey(k, hkey)
167167
return k, nil
168168
}
169169

@@ -192,7 +192,7 @@ func NewPrivateKeyECDH(curve string, key []byte) (*PrivateKeyECDH, error) {
192192
return nil, err
193193
}
194194
k := &PrivateKeyECDH{hkey, nist}
195-
runtime.AddCleanup(k, destroyKey, hkey)
195+
addCleanupKey(k, hkey)
196196
return k, nil
197197
}
198198

@@ -210,6 +210,8 @@ func (k *PrivateKeyECDH) PublicKey() (*PublicKeyECDH, error) {
210210
// Only include X.
211211
bytes = data[:hdr.KeySize]
212212
}
213+
// No cleanup needed: pub.priv prevents k from being garbage collected,
214+
// so k's cleanup will destroy the shared hkey when both are unreachable.
213215
pub := &PublicKeyECDH{k.hkey, bytes, k}
214216
return pub, nil
215217
}

cng/ecdsa.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func NewPublicKeyECDSA(curve string, X, Y BigInt) (*PublicKeyECDSA, error) {
9090
return nil, err
9191
}
9292
k := &PublicKeyECDSA{hkey}
93-
runtime.AddCleanup(k, destroyKey, hkey)
93+
addCleanupKey(k, hkey)
9494
return k, nil
9595
}
9696

@@ -108,7 +108,7 @@ func NewPrivateKeyECDSA(curve string, X, Y, D BigInt) (*PrivateKeyECDSA, error)
108108
return nil, err
109109
}
110110
k := &PrivateKeyECDSA{hkey}
111-
runtime.AddCleanup(k, destroyKey, hkey)
111+
addCleanupKey(k, hkey)
112112
return k, nil
113113
}
114114

cng/hash.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
// maxHashSize is the size of SHA512 and SHA3_512, the largest hashes we support.
2121
const maxHashSize = 64
2222

23-
// destroyHash is a cleanup function for releasing bcrypt hash handles.
24-
func destroyHash(ctx bcrypt.HASH_HANDLE) {
25-
bcrypt.DestroyHash(ctx)
23+
// addCleanupHash attaches a cleanup function to ptr that will destroy ctx.
24+
func addCleanupHash[T any](ptr *T, ctx bcrypt.HASH_HANDLE) {
25+
runtime.AddCleanup(ptr, func(ctx bcrypt.HASH_HANDLE) {
26+
bcrypt.DestroyHash(ctx)
27+
}, ctx)
2628
}
2729

2830
// SupportsHash returns true if a hash.Hash implementation is supported for h.
@@ -202,15 +204,15 @@ func (h *Hash) init() {
202204
if err != nil {
203205
panic(err)
204206
}
205-
runtime.AddCleanup(h, destroyHash, h.ctx)
207+
addCleanupHash(h, h.ctx)
206208
}
207209

208210
func (h *Hash) Clone() (HashCloner, error) {
209211
defer runtime.KeepAlive(h)
210212
h2 := &Hash{alg: h.alg, key: bytes.Clone(h.key)}
211213
if h.ctx != 0 {
212214
hashClone(h.ctx, &h2.ctx)
213-
runtime.AddCleanup(h2, destroyHash, h2.ctx)
215+
addCleanupHash(h2, h2.ctx)
214216
}
215217
return h2, nil
216218
}

cng/keys.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ package cng
88

99
import (
1010
"errors"
11+
"runtime"
1112
"unsafe"
1213

1314
"github.com/microsoft/go-crypto-winnative/internal/bcrypt"
1415
)
1516

16-
// destroyKey is a cleanup function for releasing bcrypt key handles.
17-
func destroyKey(kh bcrypt.KEY_HANDLE) {
18-
bcrypt.DestroyKey(kh)
17+
// addCleanupKey attaches a cleanup function to ptr that will destroy kh.
18+
func addCleanupKey[T any](ptr *T, kh bcrypt.KEY_HANDLE) {
19+
runtime.AddCleanup(ptr, func(kh bcrypt.KEY_HANDLE) {
20+
bcrypt.DestroyKey(kh)
21+
}, kh)
1922
}
2023

2124
const (

cng/rc4.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func NewRC4Cipher(key []byte) (*RC4Cipher, error) {
2525
return nil, err
2626
}
2727
c := &RC4Cipher{kh: kh}
28-
runtime.AddCleanup(c, destroyKey, kh)
28+
addCleanupKey(c, kh)
2929
return c, nil
3030
}
3131

cng/rsa.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func NewPublicKeyRSA(N, E BigInt) (*PublicKeyRSA, error) {
9595
return nil, err
9696
}
9797
k := &PublicKeyRSA{hkey, uint32(N.bitLen())}
98-
runtime.AddCleanup(k, destroyKey, hkey)
98+
addCleanupKey(k, hkey)
9999
return k, nil
100100
}
101101

@@ -117,7 +117,7 @@ func NewPrivateKeyRSA(N, E, D, P, Q, Dp, Dq, Qinv BigInt) (*PrivateKeyRSA, error
117117
return nil, err
118118
}
119119
k := &PrivateKeyRSA{hkey, uint32(N.bitLen())}
120-
runtime.AddCleanup(k, destroyKey, hkey)
120+
addCleanupKey(k, hkey)
121121
return k, nil
122122
}
123123

0 commit comments

Comments
 (0)