I need to generate and verify 4 byte MAC from the data which is authenticated only using AES-CCM with 32 bytes of key and a Nonce.
I tried using "CcmBlockCipher(new AesEngine())" object but it expects data to be encrypted or decrypted. However, the data I have got is not encrypted but has plain text data + MAC in it which is only mean to authenticate purpose using AES-CCM.
How to use BouncyCastle C# library in order to just generate MAC on plain text data using AES-CCM and verify the same from the received MAC?
This type of data is commonly called "additional authenticated data" (AAD). It will be included in the calculation of the authentication tag, but not encrypted (resp. decrypted).
You can use the ProcessAadBytes
(also ProcessAadByte
) method to provide this type of input to CcmBlockCipher
. Note that there is no output buffer for these methods because the cipher doesn't consider AAD part of its "stream".
So in your case, for "encryption", with no actual data to encrypt, you just add all the plaintext using ProcessAadBytes
and then call DoFinal
to get the output, which will consist only of the authentication tag. If you need to send the plaintext also, you have to arrange that yourself.
Similarly for decryption, CcmBlockCipher
does not consider the AAD as part of its usual input; it should be added using ProcessAadBytes
, followed by a DoFinal
that consists of just the tag in your case.