Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openssl hmac using aes-256-cbc

Tags:

openssl

aes

hmac

I am trying to take an AES HMAC of a file using the openssl command line program on Linux. I have been looking at the man pages but can't quite figure out how successfully make a HMAC. I can encrypt a file using the enc command with openssl however I can't seem to create a HMAC. The encryption looks like the following:

openssl enc -aes-256-cbc -in plaintext -out ciphertext

Any advice or tutorials would be wonderful

like image 689
Ryan Avatar asked Apr 09 '10 22:04

Ryan


2 Answers

You may be asking about CBC-MAC. For that, I think you just encrypt your message or file with an IV of 0 and then take the last block (16 bytes for AES256-cbc). I found a blog post that describes how to do this with OpenSSL:

openssl enc -e -aes-256-cbc -K 0123456789ABCDEF -iv 0000000000000000 < file | tail -c 16 | od -A n
  • -K is where you provide your key, which the wiki page says should be different than what you're using to encrypt the file, if you're encrypting it at all.
  • -iv obviously provides an all-zero IV, which is the key for CBC-MAC.
  • The tail -c 16 is to get the last AES256-cbc block, which is 16-bytes long.
  • od is to convert it to hex, which that web site says is common. Otherwise instead of od -A n you could do base64 if that's more applicable, or leave it off completely to have just the raw bytes.

You can't take an AES HMAC of a file because AES256-cbc is a block cipher, not a hashing algorithm. AES256-cbc is for encrypting and decrypting a file. HMAC is for verifying a file's integrity and requires a hash algorithm at its core such as SHA-1 or MD5.

Are you trying to sign or verify a file, or encrypt it? To sign, check out the OpenSSL dgst command and use simple HMACs like MD5 or SHA-1, or go all out and digitally sign it with DSS/DSA.

Also, I believe using a block cipher as a MAC is called an EMAC, but OpenSSL doesn't do EMAC as far as I know. EMAC just takes the last block of an encrypted file and encrypts it to create a MAC.

like image 164
indiv Avatar answered Nov 08 '22 07:11

indiv


You can perform an HMAC of a file using an AES key as input to the HMAC.

Encrypt the file using any AES algorithm you want (in this example -aes-256-cbc); generate an AES key based on a password (change "password" to your password) and use the -p switch to dump the salt, key, and iv used to encrypt.

openssl enc -e -k password -p -aes-256-cbc -in plaintext -out ciphertext
salt=A2402067B9BFD4A1
key=EB3A88115C30F26C3987F1AB2577DF5B58C80EBEEA623506517FAD843C64E1FC
iv =B382453BCBF579CE14C0726D343F40E2

Create an HMAC of any object/file using a hash algorithm of your choice and the AES key that was used to encrypt the file:

openssl dgst -hmac EB3A88115C30F26C3987F1AB2577DF5B58C80EBEEA623506517FAD843C64E1FC -sha256 ciphertext
HMAC-SHA256(ciphertext)= fa3fb9c9c743f35ba81793e2704c3fc9737cd2675011110cb1655ea7ceed2914

Remember the AES key for use later if you need to verify the HMAC. Note that I do not know what you are trying to accomplish and I cannot claim the above meets your cryptographic requirements, but it does show how to encrypt a file, obtain the encryption key, and use that encryption key in an HMAC computation.

like image 23
Daniel Avatar answered Nov 08 '22 06:11

Daniel