-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKey.cpp
More file actions
48 lines (37 loc) · 1.05 KB
/
Key.cpp
File metadata and controls
48 lines (37 loc) · 1.05 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
#include "Key.h"
#include <string.h>
Key::Key() :
type(KeyType::Invalid),
size(0),
value()
{
}
Key::Key(KeyType type, size_t size, const void *data):
type(type),
size(size),
value()
{
memcpy(value, data, size);
}
bool Key::isValid() const
{
return type != KeyType::Invalid;
}
bool Key::operator==(const Key &rhs) const
{
if(type == KeyType::Invalid)
return type == rhs.type;
return type == rhs.type && size == rhs.size && isBufferEqual(value, rhs.value, size);
}
bool Key::isBufferEqual(const unsigned char *lhs, const unsigned char *rhs, size_t size) const
{
unsigned char maskOfDifferences = 0;
/** This comparison by design checks whole buffers to harden timing attacks */
for(size_t i = 0; i < size; ++i)
maskOfDifferences |= lhs[i] ^ rhs[i];
return (maskOfDifferences == 0);
}
DallasIButtonKey::DallasIButtonKey(const uint8_t uid[]):
Key(KeyType::iButton, UID_SIZE, uid) {}
MifareClassicKey::MifareClassicKey(const uint8_t nuid[]):
Key(KeyType::MifareClassic, NUID_SIZE, nuid) {}