Blog | G5 Cyber Security

AES-GMAC Calculation Guide

TL;DR

This guide shows you how to calculate an AES-GMAC (Galois/Counter Mode Message Authentication Code) using OpenSSL. It covers generating a key, encrypting data, and verifying the MAC.

Steps

  1. Generate an AES Key: You’ll need a 128, 192 or 256-bit AES key. For example, to generate a random 256-bit key:
    openssl rand -hex 32

    Save this output – it’s your secret key!

  2. Prepare Your Data: Decide on the data you want to authenticate. This could be any file or string of bytes.
  3. Generate an Initialization Vector (IV): The IV should be a random number, typically 128 bits long. It’s crucial that your IV is unique for each message encrypted with the same key. Use:
    openssl rand -hex 16
  4. Calculate the AES-GMAC: Use OpenSSL to calculate the MAC.
    openssl dgst -aes256gmac  -iv  -sign  | openssl base64

    Replace:

    • with the path to your AES key file.
    • with the path to your IV file.
    • with the path to your data file.

    The base64 command converts the binary MAC output into a more readable string format.

  5. Verify the AES-GMAC: To verify, recalculate the MAC using the same key and IV as before, then compare it to the original MAC.
    openssl dgst -aes256gmac  -iv  -verify  -signature 

    Replace:

    • with the path to your AES key file.
    • with the path to your IV file.
    • with the path to your data file.
    • with the path to the file containing the original MAC you calculated in step 4.

    A successful verification will output ‘Verified OK’. If it fails, the data has been tampered with or an incorrect key/IV was used.

Important Considerations

Exit mobile version