summaryrefslogtreecommitdiffstats
path: root/common/RijndaelCBCMAC.h
blob: 97e1f47159b9660a70bafcfed78011f9af081d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * File:	RijndaelCBCMAC.h
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 * See included license file for license details.
 */
#if !defined(_RijndaelCBCMAC_h_)
#define _RijndaelCBCMAC_h_

#include "AESKey.h"
#include <string.h>

/*!
 * \brief Class to compute CBC-MAC using the AES/Rijndael cipher.
 *
 * Currently only supports 128-bit keys and block sizes.
 */
class RijndaelCBCMAC
{
public:
	enum
	{
		BLOCK_SIZE = 16	//!< Number of bytes in one cipher block.
	};
	
	//! The cipher block data type.
	typedef uint8_t block_t[BLOCK_SIZE];
	
public:
	//! \brief Default constructor.
	//!
	//! The key and IV are both set to zero.
	RijndaelCBCMAC() {}
	
	//! \brief Constructor.
	RijndaelCBCMAC(const AESKey<128> & key, const uint8_t * iv=0);
	
	//! \brief Process data.
	void update(const uint8_t * data, unsigned length);
	
	//! \brief Signal that all data has been processed.
	void finalize();
	
	//! \brief Returns a reference to the current MAC value.
	const block_t & getMAC() const { return m_mac; }
	
	//! \brief Assignment operator.
	RijndaelCBCMAC & operator = (const RijndaelCBCMAC & other)
	{
		m_key = other.m_key;
		memcpy(m_mac, other.m_mac, sizeof(m_mac));
		return *this;
	}
	
protected:
	AESKey<128> m_key;	//!< 128-bit key to use for the CBC-MAC.
	block_t m_mac;	//!< Current message authentication code value.
	
	void updateOneBlock(const uint8_t * data);
};

#endif // _RijndaelCBCMAC_h_