Is AesGcm thread safe? #71198
-
I would like to cache the AesGcm instance in a way that only one instance is created per key, but I have not found any documentation if I can use one AesGcm instance in multiple threads concurrently (imagine a web API use-case). Can I reuse one AesGcm instance across multiple threads? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You cannot. Looking at the implementation of |
Beta Was this translation helpful? Give feedback.
-
@PathogenDavid answered the question for this type specifically. But in general, the rule of thumb is that instance APIs are not thread-safe for multiple callers. If there are exceptions, they'll be called out in the API documentation. See the |
Beta Was this translation helpful? Give feedback.
You cannot.
Looking at the implementation of
EncryptCore
(specifically the OpenSSL implementation) you can see there's state being stored in the_ctxHandle
that would not appreciate differing across threads.