ISO 9797-1
CBC-MAC
C#
Cryptography
Algorithm 1

ISO 9797-1 Algorithm 1 CBC-MAC in C

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

The International Organization for Standardization (ISO) has established various standards for data encryption and cryptographic techniques, among which the ISO 9797-1 standard defines mechanisms for computing Message Authentication Codes (MACs) over data using block cipher algorithms. This article delves into Algorithm 1 from the ISO 9797-1 standard, commonly known as the Cipher Block Chaining Message Authentication Code (CBC-MAC) method, and its implementation in C#.

Understanding CBC-MAC

What is CBC-MAC?

CBC-MAC is a cryptographic technique used to ensure data integrity and authenticity. It is a symmetric key block cipher algorithm that generates a fixed-size string of bits known as a MAC, using a block cipher in Cipher Block Chaining (CBC) mode.

In CBC-MAC, the input data is divided into blocks of fixed size, and each block is encrypted in a sequence where each block's encryption depends on the encryption of the previous block. The final block's encrypted value serves as the MAC for the entire data set.

How Does CBC-MAC Work?

  1. Initialization: Select an initial vector (IV) and a symmetric key.
  2. Block Division: Divide the data into blocks. If the data size is not a multiple of the block size, padding may be required.
  3. Encryption Process:
    • Encrypt the first block using the block cipher and the IV.
    • For each subsequent block, XOR it with the ciphertext of the previous block before encryption.
  4. Final Output: The output of the last block encryption serves as the MAC.

Key Properties of CBC-MAC

  • Integrity Checking: Ensures that data has not been altered.
  • Authenticity Verification: Confirms that data originates from a verified sender.
  • Key Dependency: Security relies heavily on the confidentiality of the symmetric key.

Implementing CBC-MAC in C#

Below is a basic C# implementation of CBC-MAC using the `System.Security.Cryptography` namespace. For the sake of simplicity, let's use the AES algorithm as the underlying block cipher.

  • Block Size and Key: Here we use AES which generally uses a block size of 128 bits (16 bytes). The key is also specified as 16 bytes to be compatible with AES-128. Ensure that key and block sizes match the algorithm used.
  • IV Initialization: The IV is set to zero. This is typical in a standard CBC-MAC implementation.
  • Data Padding: The implementation assumes no-padding; in real scenarios, padding schemes like PKCS7 might be necessary if data is not block-size aligned.
  • Efficiency: Fast operation due to its basis in block cipher encryption, a common hardware-optimized operation.
  • Compact Output: Fixed-length MAC irrespective of input data size.
  • Key Security: Compromise of the key compromises the MAC's security.
  • Padding: Care must be taken to pad input data correctly.
  • Fixed Block Size: Data must be processed in block-size multiples.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.