| 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. Note that this function in turn relies on the AES key schedule being precalculated. 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, byte *MACStorage)
void | calcCMACSubkey (byte *cryptoBlock)

## Function Documentation

void calcCMAC | ( | const byte * | message,
|  | byte * | MACStorage |
| ) |  |  |

Use precalculated subkey and AES key schedule for generating an AES-CMAC.

Definition at line 111 of file cmac.c.

References addConstantFromEEPROM(), BLOCK_SIZE, cipherLookup(), CMACSubkey, copyBytes(), and MESSAGE_SIZE_WO_MAC.

Referenced by transmitCommandMessage().

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

Here is the call graph for this function:

void calcCMACSubkey | ( | byte * | cryptoBlock | ) |

Precalculate the subkey required for AES-CMAC calculation.

Definition at line 61 of file cmac.c.

References BLOCK_SIZE, cipherLookup(), CMACSubkey, and copyBytesToEEPROM().

Referenced by main().

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

Here is the call graph for this function:

@DOC_TITLE@