summaryrefslogtreecommitdiffstats
path: root/common/RijndaelCBCMAC.cpp
blob: fad03399fac001656c2f0053ffab2841cceb5baf (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * File:	RijndaelCBCMAC.cpp
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 * See included license file for license details.
 */

#include "RijndaelCBCMAC.h"
#include "rijndael.h"
#include <assert.h>
#include "Logging.h"

void logHexArray(Logger::log_level_t level, const uint8_t * bytes, unsigned count);

//! \param key The key to use as the CBC-MAC secret.
//! \param iv Initialization vector. Defaults to zero if not provided.
RijndaelCBCMAC::RijndaelCBCMAC(const AESKey<128> & key, const uint8_t * iv)
:	m_key(key)
{
	if (iv)
	{
		memcpy(m_mac, iv, sizeof(m_mac));
	}
	else
	{
		memset(m_mac, 0, sizeof(m_mac));
	}
}

//! \param data Pointer to data to process.
//! \param length Number of bytes to process. Must be evenly divisible by #BLOCK_SIZE.
void RijndaelCBCMAC::update(const uint8_t * data, unsigned length)
{
	assert(length % BLOCK_SIZE == 0);
	unsigned blocks = length / BLOCK_SIZE;
	while (blocks--)
	{
		updateOneBlock(data);
		data += BLOCK_SIZE;
	}
}

//! It appears that some forms of CBC-MAC encrypt the final output block again in
//! order to protect against a plaintext attack. This method is a placeholder for
//! such an operation, but it currently does nothing.
void RijndaelCBCMAC::finalize()
{
}

//! On entry the current value of m_mac becomes the initialization vector
//! for the CBC encryption of this block. The output of the encryption then
//! becomes the new MAC, which is stored in m_mac.
void RijndaelCBCMAC::updateOneBlock(const uint8_t * data)
{
	Rijndael cipher;
	cipher.init(Rijndael::CBC, Rijndael::Encrypt, m_key, Rijndael::Key16Bytes, m_mac);
	cipher.blockEncrypt(data, BLOCK_SIZE * 8, m_mac);	// size is in bits
	
//	Log::log(Logger::DEBUG2, "CBC-MAC output block:\n");
//	logHexArray(Logger::DEBUG2, (const uint8_t *)&m_mac, sizeof(m_mac));
}

/*!
 * \brief Log an array of bytes as hex.
 */
void logHexArray(Logger::log_level_t level, const uint8_t * bytes, unsigned count)
{
	Log::SetOutputLevel leveler(level);
//		Log::log("    ");
	unsigned i;
	for (i = 0; i < count; ++i, ++bytes)
	{
		if ((i % 16 == 0) && (i < count - 1))
		{
			if (i != 0)
			{
				Log::log("\n");
			}
			Log::log("    0x%04x: ", i);
		}
		Log::log("%02x ", *bytes & 0xff);
	}
	
	Log::log("\n");
}