Path: blob/master/Authenticated-Encryption/AE-with-MACs/MAC-then-Encrypt/README.md
1402 views
MAC-then-Encrypt
Prerequisites:
In this article we will:
Discuss Authenticated Encryption using MAC-then-Encrypt technique
Implement a simple Authenticated Encryption service using MAC-then-Encrypt technique
This method of authenticated encryption using MACs is slightly different from Encrypt-and-MAC technique. In this technique, only one entity is sent over the communication channel and that is the ciphertext. The ciphertext itself is generated from the plaintext and the authentication tag, we will see the internals in coming sections.
As mentioned before, keys used for encryption and for generating MAC must be different.
To understand MAC-then-Encrypt clearly, have a look at this illustration from Wikipedia:
Sending messages securely using MAC-then-Encrypt
Consider Alice as the sender and Bob as the receiver. Bob will not only decrypt the message and read it, but also check if the authenticated tag of the message received is equal to the authentication tag received. The message is accepted by Bob only if this condition holds true. To send a message through MAC-then-Encrypt technique:
Assuming the message to be sent is
M
, Alice computes it's corresponding authenticated tagT
as:, where
k2
is the key used to generate the authentication tag. Note that key is not known to the attacker.Also, the mechanism of this technique is not affected by the type of the algorithm used to generate the authentication tag. Alice can used either a Hash based MAC or block cipher modes such as CBC mode to compute the authentication tag.
Alice must make sure that the algorithm is secure enough and authentication tag does not reveal any information about the plaintext.
Next, Alice concatenates message
M
and it's authentication tagT
, pads it to make it a multiple of the block size and encrypts the resultant string using a secure block cipher algorithm (AES etc.) to generate ciphertextC
, where
k1
is the key used to generate the ciphertext.
Alice then sends this ciphertext through the communication channel to Bob.
Implementation of the steps described above:
Authentication through MAC-then-Encrypt
After receiving Alice's message, Bob does the following:
Decrypts the ciphertext
C
using keyk1
:Bob now has the decrypted ciphertext. As we know that the plaintext obtained is a concatenation of message
M
and authenticationT
. Bob now separatesM
andT
and also checks if MAC of the message obtained is equal to the authentication tagT
:If the tags match, then
M
will be accepted and an acknowledge will be sent to Alice. Otherwise, a VerificationError will be generated.
Implementation of the above verification steps:
You can check out the entire example script for MAC-then-Encrypt technique here