LibLogicalAccess  2.5.0
An Open Source RFID Library
Loading...
Searching...
No Matches
samkeyentry.hpp
Go to the documentation of this file.
1
7#ifndef LOGICALACCESS_SAMKEYENTRY_HPP
8#define LOGICALACCESS_SAMKEYENTRY_HPP
9
10#include <cstring>
11#include <logicalaccess/key.hpp>
13
14namespace logicalaccess
15{
16typedef struct s_SETAV1
17{
18 unsigned char dumpsessionkey;
19 unsigned char allowcrypto;
20 unsigned char keepIV;
21 unsigned char keytype[3];
22 unsigned char rfu[2];
23 unsigned char hightcommandsecurity;
24 unsigned char disablekeyentry;
26 unsigned char disablewritekeytopicc;
27 unsigned char disabledecryption;
28 unsigned char disableencryption;
29 unsigned char disableverifymac;
30 unsigned char disablegeneratemac;
32
33typedef struct s_SETAV2
34{
35 unsigned char dumpsessionkey;
36 unsigned char allowcrypto;
37 unsigned char keepIV;
38 unsigned char keytype[3];
39 unsigned char rfu[2];
40 unsigned char authkey;
41 unsigned char disablekeyentry;
42 unsigned char lockkey;
43 unsigned char disablewritekeytopicc;
44 unsigned char disabledecryption;
45 unsigned char disableencryption;
46 unsigned char disableverifymac;
47 unsigned char disablegeneratemac;
49
50typedef struct s_EXTSET
51{
52 unsigned char keyclass[3];
53 unsigned char dumpsessionkey;
54 unsigned char diversifieduse;
55 unsigned char rfu[2];
57
59{
60 unsigned char desfireAid[3];
61 unsigned char desfirekeyno;
62 unsigned char cekno;
63 unsigned char cekv;
64 unsigned char kuc;
65 unsigned char set[2];
66 unsigned char vera;
67 unsigned char verb;
68 unsigned char verc;
70
72{
73 unsigned char desfireAid[3];
74 unsigned char desfirekeyno;
75 unsigned char cekno;
76 unsigned char cekv;
77 unsigned char kuc;
78 unsigned char set[2];
79 unsigned char vera;
80 unsigned char verb;
81 unsigned char verc;
82 unsigned char ExtSET;
84
88template <typename T, typename S>
89class LLA_CARDS_SAMAV_API SAMKeyEntry : public SAMBasicKeyEntry
90{
91 public:
94 {
95 memset(&d_keyentryinformation, 0x00, sizeof(d_keyentryinformation));
96 }
97
98 explicit SAMKeyEntry(const std::string &str, const std::string &str1 = "",
99 const std::string &str2 = "")
100 : SAMBasicKeyEntry(str, str1, str2)
101 {
102 memset(&d_keyentryinformation, 0x00, sizeof(d_keyentryinformation));
103 }
104
105 SAMKeyEntry(const void **buf, size_t buflen, char numberkey)
106 : SAMBasicKeyEntry(buf, buflen, numberkey)
107 {
108 memset(&d_keyentryinformation, 0x00, sizeof(d_keyentryinformation));
109 }
110
115 {
116 }
117
123 bool operator!=(const SAMKeyEntry &key) const
124 {
125 return !operator==(key);
126 }
127
128 void setSET(unsigned char *t)
129 {
130 memcpy(d_keyentryinformation.set, t, sizeof(*t));
131 }
132
134 {
135 S set;
136 char *x = (char *)&set;
137
138 unsigned char set_save[2];
139 unsigned char j = 0;
140
141 memcpy(set_save, d_keyentryinformation.set, 2);
142
143 for (char i = 7; i >= 0; --i)
144 {
145 if ((set_save[j] & 0x80) == 0x80)
146 x[i + j * 8] = 1;
147 else
148 x[i + j * 8] = 0;
149 set_save[j] = set_save[j] << 1;
150 if (i == 0 && j == 0)
151 {
152 i = 8;
153 j++;
154 }
155 }
156
157 return set;
158 }
159
160 void setSET(const S &t)
161 {
162 char *x = (char *)&t;
163 memset(d_keyentryinformation.set, 0, 2);
164 unsigned char j = 0;
165 for (char i = 7; i >= 0; --i)
166 {
167 d_keyentryinformation.set[j] += (char)x[i + j * 8];
168 if (i == 0 && j == 0)
169 {
170 i = 8;
171 j++;
172 }
173 else if (i != 0)
174 d_keyentryinformation.set[j] = d_keyentryinformation.set[j] << 1;
175 }
176 }
177
179 {
180 return d_keyentryinformation;
181 }
182 void setKeyEntryInformation(const T &t)
183 {
184 d_keyentryinformation = t;
185 }
186
188 {
189 unsigned char keytype = 0x38 & d_keyentryinformation.set[0];
190 size_t oldsize = getLength();
191
192 switch (keytype)
193 {
194 case SAM_KEY_DES: d_keyType = SAM_KEY_DES; break;
195
196 case SAM_KEY_3K3DES: d_keyType = SAM_KEY_3K3DES; break;
197
198 case SAM_KEY_AES: d_keyType = SAM_KEY_AES; break;
199 default:;
200 }
201
202 unsigned char *tmp = new unsigned char[getLength()];
203 if (getLength() < oldsize)
204 oldsize = getLength();
205 memcpy(tmp, &*d_key, oldsize);
206 delete d_key;
207 d_key = tmp;
208 }
209
211 {
212 d_keyentryinformation.set[0] =
213 d_keyentryinformation.set[0] - (0x38 & d_keyentryinformation.set[0]);
214 switch (d_keyType)
215 {
216 case SAM_KEY_DES: break;
217
218 case SAM_KEY_3K3DES: d_keyentryinformation.set[0] |= 0x18; break;
219
220 case SAM_KEY_AES: d_keyentryinformation.set[0] |= 0x20; break;
221 }
222 }
223
224 void setKeysData(std::vector<ByteVector> keys, SAMKeyType type) override
225 {
226 SAMBasicKeyEntry::setKeysData(keys, type);
227 setSETKeyTypeFromKeyType();
228 }
229
230 private:
232};
233}
234
235#endif /* LOGICALACCESS_SAMKEYENTRY_HPP */
A SAMBasicKeyEntry Key class.
Definition: sambasickeyentry.hpp:73
A SAMKeyEntry class.
Definition: samkeyentry.hpp:90
SAMKeyEntry(const void **buf, size_t buflen, char numberkey)
Definition: samkeyentry.hpp:105
void setKeyEntryInformation(const T &t)
Definition: samkeyentry.hpp:182
void setSET(unsigned char *t)
Definition: samkeyentry.hpp:128
SAMKeyEntry()
Definition: samkeyentry.hpp:92
T d_keyentryinformation
Definition: samkeyentry.hpp:231
S getSETStruct()
Definition: samkeyentry.hpp:133
bool operator!=(const SAMKeyEntry &key) const
Inequality operator.
Definition: samkeyentry.hpp:123
void setSETKeyTypeFromKeyType()
Definition: samkeyentry.hpp:210
void setKeyTypeFromSET()
Definition: samkeyentry.hpp:187
void setSET(const S &t)
Definition: samkeyentry.hpp:160
SAMKeyEntry(const std::string &str, const std::string &str1="", const std::string &str2="")
Definition: samkeyentry.hpp:98
T & getKeyEntryInformation()
Definition: samkeyentry.hpp:178
void setKeysData(std::vector< ByteVector > keys, SAMKeyType type) override
Set the keys data.
Definition: samkeyentry.hpp:224
~SAMKeyEntry()
Destructor.
Definition: samkeyentry.hpp:114
Definition: asn1.hpp:9
SAMKeyType
The SAM key type.
Definition: sambasickeyentry.hpp:33
@ SAM_KEY_AES
Definition: sambasickeyentry.hpp:33
@ SAM_KEY_DES
Definition: sambasickeyentry.hpp:33
@ SAM_KEY_3K3DES
Definition: sambasickeyentry.hpp:33
struct logicalaccess::s_KeyEntryAV1Information KeyEntryAV1Information
struct logicalaccess::s_KeyEntryAV2Information KeyEntryAV2Information
struct logicalaccess::s_EXTSET ExtSETStruct
struct logicalaccess::s_SETAV2 SETAV2
struct logicalaccess::s_SETAV1 SETAV1
SAMBasicKeyEntry source.
Definition: samkeyentry.hpp:51
unsigned char keyclass[3]
Definition: samkeyentry.hpp:52
unsigned char dumpsessionkey
Definition: samkeyentry.hpp:53
unsigned char rfu[2]
Definition: samkeyentry.hpp:55
unsigned char diversifieduse
Definition: samkeyentry.hpp:54
Definition: samkeyentry.hpp:59
unsigned char verc
Definition: samkeyentry.hpp:68
unsigned char desfireAid[3]
Definition: samkeyentry.hpp:60
unsigned char cekno
Definition: samkeyentry.hpp:62
unsigned char vera
Definition: samkeyentry.hpp:66
unsigned char desfirekeyno
Definition: samkeyentry.hpp:61
unsigned char kuc
Definition: samkeyentry.hpp:64
unsigned char set[2]
Definition: samkeyentry.hpp:65
unsigned char verb
Definition: samkeyentry.hpp:67
unsigned char cekv
Definition: samkeyentry.hpp:63
Definition: samkeyentry.hpp:72
unsigned char desfirekeyno
Definition: samkeyentry.hpp:74
unsigned char ExtSET
Definition: samkeyentry.hpp:82
unsigned char cekno
Definition: samkeyentry.hpp:75
unsigned char desfireAid[3]
Definition: samkeyentry.hpp:73
unsigned char verc
Definition: samkeyentry.hpp:81
unsigned char kuc
Definition: samkeyentry.hpp:77
unsigned char set[2]
Definition: samkeyentry.hpp:78
unsigned char verb
Definition: samkeyentry.hpp:80
unsigned char vera
Definition: samkeyentry.hpp:79
unsigned char cekv
Definition: samkeyentry.hpp:76
Definition: samkeyentry.hpp:17
unsigned char keepIV
Definition: samkeyentry.hpp:20
unsigned char rfu[2]
Definition: samkeyentry.hpp:22
unsigned char disablewritekeytopicc
Definition: samkeyentry.hpp:26
unsigned char disablegeneratemac
Definition: samkeyentry.hpp:30
unsigned char disableencryption
Definition: samkeyentry.hpp:28
unsigned char dumpsessionkey
Definition: samkeyentry.hpp:18
unsigned char hostauthenticationafterreset
Definition: samkeyentry.hpp:25
unsigned char disablekeyentry
Definition: samkeyentry.hpp:24
unsigned char keytype[3]
Definition: samkeyentry.hpp:21
unsigned char allowcrypto
Definition: samkeyentry.hpp:19
unsigned char hightcommandsecurity
Definition: samkeyentry.hpp:23
unsigned char disabledecryption
Definition: samkeyentry.hpp:27
unsigned char disableverifymac
Definition: samkeyentry.hpp:29
Definition: samkeyentry.hpp:34
unsigned char authkey
Definition: samkeyentry.hpp:40
unsigned char lockkey
Definition: samkeyentry.hpp:42
unsigned char disabledecryption
Definition: samkeyentry.hpp:44
unsigned char disablegeneratemac
Definition: samkeyentry.hpp:47
unsigned char keytype[3]
Definition: samkeyentry.hpp:38
unsigned char disablewritekeytopicc
Definition: samkeyentry.hpp:43
unsigned char dumpsessionkey
Definition: samkeyentry.hpp:35
unsigned char disableverifymac
Definition: samkeyentry.hpp:46
unsigned char allowcrypto
Definition: samkeyentry.hpp:36
unsigned char keepIV
Definition: samkeyentry.hpp:37
unsigned char disableencryption
Definition: samkeyentry.hpp:45
unsigned char rfu[2]
Definition: samkeyentry.hpp:39
unsigned char disablekeyentry
Definition: samkeyentry.hpp:41