diff options
Diffstat (limited to 'src/crypto/rsa/boring.go')
-rw-r--r-- | src/crypto/rsa/boring.go | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/src/crypto/rsa/boring.go b/src/crypto/rsa/boring.go index 362e9307f86..fc2842fb341 100644 --- a/src/crypto/rsa/boring.go +++ b/src/crypto/rsa/boring.go @@ -10,18 +10,13 @@ import ( "crypto/internal/boring" "crypto/internal/boring/bbig" "math/big" - "sync/atomic" "unsafe" ) // Cached conversions from Go PublicKey/PrivateKey to BoringCrypto. // -// A new 'boring atomic.Value' field in both PublicKey and PrivateKey -// serves as a cache for the most recent conversion. The cache is an -// atomic.Value because code might reasonably set up a key and then -// (thinking it immutable) use it from multiple goroutines simultaneously. -// The first operation initializes the cache; if there are multiple simultaneous -// first operations, they will do redundant work but not step on each other. +// The first operation on a PublicKey or PrivateKey makes a parallel +// BoringCrypto key and saves it in pubCache or privCache. // // We could just assume that once used in a sign/verify/encrypt/decrypt operation, // a particular key is never again modified, but that has not been a @@ -36,8 +31,16 @@ type boringPub struct { orig PublicKey } +var pubCache boring.Cache +var privCache boring.Cache + +func init() { + pubCache.Register() + privCache.Register() +} + func boringPublicKey(pub *PublicKey) (*boring.PublicKeyRSA, error) { - b := (*boringPub)(atomic.LoadPointer(&pub.boring)) + b := (*boringPub)(pubCache.Get(unsafe.Pointer(pub))) if b != nil && publicKeyEqual(&b.orig, pub) { return b.key, nil } @@ -49,7 +52,7 @@ func boringPublicKey(pub *PublicKey) (*boring.PublicKeyRSA, error) { return nil, err } b.key = key - atomic.StorePointer(&pub.boring, unsafe.Pointer(b)) + pubCache.Put(unsafe.Pointer(pub), unsafe.Pointer(b)) return key, nil } @@ -59,7 +62,7 @@ type boringPriv struct { } func boringPrivateKey(priv *PrivateKey) (*boring.PrivateKeyRSA, error) { - b := (*boringPriv)(atomic.LoadPointer(&priv.boring)) + b := (*boringPriv)(privCache.Get(unsafe.Pointer(priv))) if b != nil && privateKeyEqual(&b.orig, priv) { return b.key, nil } @@ -83,7 +86,7 @@ func boringPrivateKey(priv *PrivateKey) (*boring.PrivateKeyRSA, error) { return nil, err } b.key = key - atomic.StorePointer(&priv.boring, unsafe.Pointer(b)) + privCache.Put(unsafe.Pointer(priv), unsafe.Pointer(b)) return key, nil } |