diff --git a/cng/aes.go b/cng/aes.go index 3427115..ed226ec 100644 --- a/cng/aes.go +++ b/cng/aes.go @@ -10,7 +10,6 @@ import ( "crypto/cipher" "errors" "runtime" - "sync" "unsafe" "github.com/microsoft/go-crypto-winnative/internal/bcrypt" @@ -19,20 +18,17 @@ import ( const aesBlockSize = 16 -var aesCache sync.Map - type aesAlgorithm struct { h bcrypt.ALG_HANDLE allowedKeySized []int } -type aesCacheEntry struct { - id string - mode string -} - func loadAes(id string, mode string) (h aesAlgorithm, err error) { - if v, ok := aesCache.Load(aesCacheEntry{id, mode}); ok { + type aesCacheEntry struct { + id string + mode string + } + if v, ok := algCache.Load(aesCacheEntry{id, mode}); ok { return v.(aesAlgorithm), nil } err = bcrypt.OpenAlgorithmProvider(&h.h, utf16PtrFromString(id), nil, bcrypt.ALG_NONE_FLAG) @@ -54,7 +50,7 @@ func loadAes(id string, mode string) (h aesAlgorithm, err error) { for size := info.MinLength; size <= info.MaxLength; size += info.Increment { h.allowedKeySized = append(h.allowedKeySized, int(size)) } - aesCache.Store(aesCacheEntry{id, mode}, h) + algCache.Store(aesCacheEntry{id, mode}, h) return } diff --git a/cng/cng.go b/cng/cng.go index 9c1c8a3..524d3a1 100644 --- a/cng/cng.go +++ b/cng/cng.go @@ -7,16 +7,14 @@ package cng import ( + "sync" "syscall" "unsafe" "github.com/microsoft/go-crypto-winnative/internal/bcrypt" ) -type algCacheEntry struct { - id string - flags uint32 -} +var algCache sync.Map func utf16PtrFromString(s string) *uint16 { str, err := syscall.UTF16PtrFromString(s) diff --git a/cng/rsa.go b/cng/rsa.go index d40ca0d..c05246b 100644 --- a/cng/rsa.go +++ b/cng/rsa.go @@ -12,27 +12,24 @@ import ( "hash" "math/big" "runtime" - "sync" "unsafe" "github.com/microsoft/go-crypto-winnative/internal/bcrypt" ) -var rsaCache sync.Map - type rsaAlgorithm struct { h bcrypt.ALG_HANDLE } -func loadRsa(id string, flags bcrypt.AlgorithmProviderFlags) (h rsaAlgorithm, err error) { - if v, ok := rsaCache.Load(algCacheEntry{id, uint32(flags)}); ok { +func loadRsa(id string) (h rsaAlgorithm, err error) { + if v, ok := algCache.Load(id); ok { return v.(rsaAlgorithm), nil } - err = bcrypt.OpenAlgorithmProvider(&h.h, utf16PtrFromString(id), nil, flags) + err = bcrypt.OpenAlgorithmProvider(&h.h, utf16PtrFromString(id), nil, bcrypt.ALG_NONE_FLAG) if err != nil { return } - rsaCache.Store(algCacheEntry{id, uint32(flags)}, h) + algCache.Store(id, h) return } @@ -42,7 +39,7 @@ func GenerateKeyRSA(bits int) (N, E, D, P, Q, Dp, Dq, Qinv *big.Int, err error) bad := func(e error) (N, E, D, P, Q, Dp, Dq, Qinv *big.Int, err error) { return nil, nil, nil, nil, nil, nil, nil, nil, e } - h, err := loadRsa(bcrypt.RSA_ALGORITHM, bcrypt.ALG_NONE_FLAG) + h, err := loadRsa(bcrypt.RSA_ALGORITHM) if err != nil { return bad(err) } @@ -95,7 +92,7 @@ type PublicKeyRSA struct { } func NewPublicKeyRSA(N, E *big.Int) (*PublicKeyRSA, error) { - h, err := loadRsa(bcrypt.RSA_ALGORITHM, bcrypt.ALG_NONE_FLAG) + h, err := loadRsa(bcrypt.RSA_ALGORITHM) if err != nil { return nil, err } @@ -122,7 +119,7 @@ func (k *PrivateKeyRSA) finalize() { } func NewPrivateKeyRSA(N, E, D, P, Q, Dp, Dq, Qinv *big.Int) (*PrivateKeyRSA, error) { - h, err := loadRsa(bcrypt.RSA_ALGORITHM, bcrypt.ALG_NONE_FLAG) + h, err := loadRsa(bcrypt.RSA_ALGORITHM) if err != nil { return nil, err } diff --git a/cng/sha.go b/cng/sha.go index 3bdbdea..918e04f 100644 --- a/cng/sha.go +++ b/cng/sha.go @@ -9,7 +9,6 @@ package cng import ( "hash" "runtime" - "sync" "github.com/microsoft/go-crypto-winnative/internal/bcrypt" ) @@ -34,8 +33,6 @@ func NewSHA512() hash.Hash { return newSHAX(bcrypt.SHA512_ALGORITHM, nil) } -var shaCache sync.Map - type shaAlgorithm struct { h bcrypt.ALG_HANDLE size uint32 @@ -43,7 +40,11 @@ type shaAlgorithm struct { } func loadSha(id string, flags bcrypt.AlgorithmProviderFlags) (h shaAlgorithm, err error) { - if v, ok := shaCache.Load(algCacheEntry{id, uint32(flags)}); ok { + type entry struct { + id string + flags uint32 + } + if v, ok := algCache.Load(entry{id, uint32(flags)}); ok { return v.(shaAlgorithm), nil } err = bcrypt.OpenAlgorithmProvider(&h.h, utf16PtrFromString(id), nil, flags) @@ -58,7 +59,7 @@ func loadSha(id string, flags bcrypt.AlgorithmProviderFlags) (h shaAlgorithm, er if err != nil { return } - shaCache.Store(algCacheEntry{id, uint32(flags)}, h) + algCache.Store(entry{id, uint32(flags)}, h) return }