| Remote Access Control |

# cmac.h File Reference

## Detailed Description

Header file for CMAC public functions.

This file contains the prototypes for the CMAC generation functions. The implementation is found in the cmac.c file. The CMAC generation function relies on a precalculated subkey. Use the calcCMACSubkey() function to precalculate the subkey. The calcCMAC function is used to generate a 16-byte CMAC from a message.

- File: cmac.h
- Compiler: IAR EWAVR 4.11B
- Supported devices: ATtiny45/85
- AppNote: AVR411 - Secure Rolling Code Algorithm for Wireless Link

Author:
  Atmel Corporation: http://www.atmel.com
  Support email: avr@atmel.com

Name

Revision
  1193

Date
  2006-10-31 14:21:08 +0100 (ti, 31 okt 2006)

Copyright (c) 2006, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file cmac.h.

`#include "common.h"`
`#include "config.h"`

Include dependency graph for cmac.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Functions
void | calcCMAC (const byte *message, const byte *CMACSubkey, byte *scheduleBuffer, const byte *key, byte *MACStorage)
void | calcCMACSubkey (byte *subKeyStorage, byte *scheduleBuffer, const byte *key)

## Function Documentation

void calcCMAC | ( | const byte * | message,
|  | const byte * | CMACSubkey,
|  | byte * | scheduleBuffer,
|  | const byte * | key,
|  | byte * | MACStorage |
| ) |  |  |

Use precalculated subkey and generate an AES-CMAC from the message.

Definition at line 105 of file cmac.c.

References addConstant(), BLOCK_SIZE, cipher(), copyBytes(), and MESSAGE_SIZE_WO_MAC.

Referenced by main().

    00110 {
    00111         // Copy message data to MACStorage, which should be BLOCK_SIZE.
    00112         copyBytes( MACStorage, message, MESSAGE_SIZE_WO_MAC );
    00113
    00114 #if MESSAGE_SIZE_WO_MAC < BLOCK_SIZE
    00115         // Pad message before calculating CMAC.
    00116   #if MESSAGE_SIZE_WO_MAC + 1 < BLOCK_SIZE
    00117         byte pos = MESSAGE_SIZE_WO_MAC + 1;
    00118         do {
    00119                 MACStorage[ pos ] = 0x00;
    00120         } while( ++pos < BLOCK_SIZE );
    00121   #endif
    00122         MACStorage[ MESSAGE_SIZE_WO_MAC ] = 0x80;
    00123 #endif
    00124
    00125         // Add (XOR) subkey.
    00126         addConstant( MACStorage, CMACSubkey, BLOCK_SIZE );
    00127
    00128         // Encrypt.
    00129         cipher( MACStorage, scheduleBuffer, key );
    00130 }

Here is the call graph for this function:

void calcCMACSubkey | ( | byte * | subKeyStorage,
|  | byte * | scheduleBuffer,
|  | const byte * | key |
| ) |  |  |

Precalculate the subkey required for AES-CMAC calculation.

Definition at line 56 of file cmac.c.

References BLOCK_SIZE, and cipher().

Referenced by main().

    00059 {
    00060         byte blockSize;
    00061         byte * blockPtr;
    00062         bool overflow;
    00063
    00064         // Fill crypto block with all zeros.
    00065         blockSize = BLOCK_SIZE;
    00066         blockPtr = subKeyStorage;
    00067         do {
    00068                 *blockPtr++ = 0;
    00069         } while( --blockSize );
    00070
    00071         // Encrypt zeros with secret key.
    00072         cipher( subKeyStorage, scheduleBuffer, key );
    00073
    00074         // Multiply by 2 modulo (0b1^120 cat 0b10000111).
    00075         blockSize = BLOCK_SIZE - 1;
    00076         blockPtr = subKeyStorage;
    00077         overflow = blockPtr[0] & 0x80;
    00078         do {
    00079                 blockPtr[0] = (blockPtr[0] << 1) | (blockPtr[1] >> 7);
    00080                 ++blockPtr;
    00081         } while( --blockSize );
    00082         blockPtr[0] <<= 1;
    00083         if( overflow ) {
    00084                 blockPtr[0] ^= 0x87;
    00085         }
    00086
    00087 #if MESSAGE_SIZE_WO_MAC < BLOCK_SIZE
    00088         // Multiply by 2 modulo (0b1^120 cat 0b10000111) again.
    00089         blockSize = BLOCK_SIZE - 1;
    00090         blockPtr = subKeyStorage;
    00091         overflow = blockPtr[0] & 0x80;
    00092         do {
    00093                 blockPtr[0] = (blockPtr[0] << 1) | (blockPtr[1] >> 7);
    00094                 ++blockPtr;
    00095         } while( --blockSize );
    00096         blockPtr[0] <<= 1;
    00097         if( overflow ) {
    00098                 blockPtr[0] ^= 0x87;
    00099         }
    00100 #endif
    00101 }

Here is the call graph for this function:

@DOC_TITLE@