-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathccmac_bf.cpp
More file actions
111 lines (99 loc) · 2.8 KB
/
ccmac_bf.cpp
File metadata and controls
111 lines (99 loc) · 2.8 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*------------------------------ Information ---------------------------*//**
*
* $HeadURL$
*
* @file ccmac_bf.cpp
*
* @author Jo2003
*
* @date 21.11.2016
*
* $Id$
*
*///------------------------- (c) 2016 by Jo2003 --------------------------
#include "ccmac_bf.h"
//------------------------------------------------------------------------------
//! @brief Constructs the object.
//!
//! @param[in] k initial key as hex string
//! @param[in] k1 k1 key as hrx string
//! @param[in] k2 k2 key as hex string
//------------------------------------------------------------------------------
CCMAC_Bf::CCMAC_Bf(const QString &k, const QString &k1, const QString &k2)
: mKey(QByteArray::fromHex(k.toUtf8())),
mK1(QByteArray::fromHex(k1.toUtf8())),
mK2(QByteArray::fromHex(k2.toUtf8())),
mBs(8)
{
pBubbles = new QBlowfish(mKey);
}
//------------------------------------------------------------------------------
//! @brief Destroys the object.
//------------------------------------------------------------------------------
CCMAC_Bf::~CCMAC_Bf()
{
if (pBubbles)
{
delete pBubbles;
pBubbles = NULL;
}
}
//------------------------------------------------------------------------------
//! @brief get cmac-bf sign to message
//!
//! @param[in] message message to sign
//!
//! @return sign for message as string
//------------------------------------------------------------------------------
QString CCMAC_Bf::sign(const QString &message)
{
QByteArray msg = message.toUtf8();
QByteArray prev = QByteArray(mBs, '\0');
QByteArray tok;
QByteArray key;
int i;
int pad;
int loop = msg.size() / mBs;
if ((pad = (msg.size() % mBs)) == 0)
{
loop --;
}
for (i = 0; i < loop; i ++)
{
tok = msg.mid(i * mBs, mBs);
prev = pBubbles->encrypted(xor64(tok, prev));
}
if (pad == 0)
{
key = mK1;
tok = msg.mid(loop * mBs, mBs);
}
else
{
key = mK2;
tok = msg.mid(loop * mBs, pad);
tok.append(0x80);
for (i = (pad + 1); i < mBs; i++)
{
tok.append((char)0);
}
}
return QString(pBubbles->encrypted(xor64(xor64(tok, prev), key)).toHex());
}
//------------------------------------------------------------------------------
//! @brief xor for 64 bit using byte arrays
//!
//! @param[in] val1 The value 1
//! @param[in] val2 The value 2
//!
//! @return xored value
//------------------------------------------------------------------------------
QByteArray CCMAC_Bf::xor64(const QByteArray& val1, const QByteArray& val2)
{
QByteArray ba;
for (int i = 0; i < mBs; i++)
{
ba.append(val1.at(i) ^ val2.at(i));
}
return ba;
}