There is no way to detect the wrong key in GCM mode. What you can verify is the authentication tag authentication, which means you used the correct key. The problem is that if the authentication tag is incorrect, it can indicate each of the following (or a combination of all, up to the complete replacement of the ciphertext tag and authentication):
- Invalid key used
- encrypted data in counter mode was changed during transportation;
- Additional authenticated data has been changed.
- The authentication tag itself was changed during the migration.
What you can do is send additional data to identify the private key. It can be a readable identifier ( "encryption-key-1" ), but it can also be a KCV value, a key verification value. KCV usually consists of a null block encrypted with a key, or a cryptographically secure hash above the key (also called a fingerprint). Since encryption using zero block leak information should not be used to identify the encryption key.
In fact, you can use the AAD function for GCM mode to calculate the authentication tag from the key's identity. Please note that you cannot distinguish between a compromised fingerprint and the use of the wrong key. However, it is less likely that the fingerprint is accidentally damaged than the entire structure of the IV, AAD, encrypted text, and authentication tags.
Maarten bodewes
source share