]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/desfire_key.c
Fixed: "hf mfdes info"
[proxmark3-svn] / armsrc / desfire_key.c
1 /*-
2 * Copyright (C) 2010, Romain Tartiere.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by the
6 * Free Software Foundation, either version 3 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>
16 *
17 * $Id$
18 */
19
20 #include "desfire_key.h"
21
22 static inline void update_key_schedules (desfirekey_t key);
23
24 static inline void update_key_schedules (desfirekey_t key) {
25 // DES_set_key ((DES_cblock *)key->data, &(key->ks1));
26 // DES_set_key ((DES_cblock *)(key->data + 8), &(key->ks2));
27 // if (T_3K3DES == key->type) {
28 // DES_set_key ((DES_cblock *)(key->data + 16), &(key->ks3));
29 // }
30 }
31
32 desfirekey_t Desfire_des_key_new (const uint8_t value[8]) {
33 uint8_t data[8];
34 memcpy (data, value, 8);
35 for (int n=0; n < 8; n++)
36 data[n] &= 0xfe;
37 return Desfire_des_key_new_with_version (data);
38 }
39
40 desfirekey_t Desfire_des_key_new_with_version (const uint8_t value[8]) {
41 desfirekey_t key = NULL;
42 key->type = T_DES;
43 memcpy (key->data, value, 8);
44 memcpy (key->data+8, value, 8);
45 update_key_schedules (key);
46 return key;
47 }
48
49 desfirekey_t Desfire_3des_key_new (const uint8_t value[16]) {
50 uint8_t data[16];
51 memcpy (data, value, 16);
52 for (int n=0; n < 8; n++)
53 data[n] &= 0xfe;
54 for (int n=8; n < 16; n++)
55 data[n] |= 0x01;
56 return Desfire_3des_key_new_with_version (data);
57 }
58
59 desfirekey_t Desfire_3des_key_new_with_version (const uint8_t value[16]) {
60 desfirekey_t key = NULL;
61 key->type = T_3DES;
62 memcpy (key->data, value, 16);
63 update_key_schedules (key);
64 return key;
65 }
66
67 desfirekey_t Desfire_3k3des_key_new (const uint8_t value[24]) {
68 uint8_t data[24];
69 memcpy (data, value, 24);
70 for (int n=0; n < 8; n++)
71 data[n] &= 0xfe;
72 return Desfire_3k3des_key_new_with_version (data);
73 }
74
75 desfirekey_t Desfire_3k3des_key_new_with_version (const uint8_t value[24]) {
76 desfirekey_t key = NULL;
77 key->type = T_3K3DES;
78 memcpy (key->data, value, 24);
79 update_key_schedules (key);
80 return key;
81 }
82
83 desfirekey_t Desfire_aes_key_new (const uint8_t value[16]) {
84 return Desfire_aes_key_new_with_version (value, 0);
85 }
86
87 desfirekey_t Desfire_aes_key_new_with_version (const uint8_t value[16], uint8_t version) {
88 desfirekey_t key = NULL;
89 memcpy (key->data, value, 16);
90 key->type = T_AES;
91 key->aes_version = version;
92 return key;
93 }
94
95 uint8_t Desfire_key_get_version (desfirekey_t key) {
96 uint8_t version = 0;
97
98 for (int n = 0; n < 8; n++) {
99 version |= ((key->data[n] & 1) << (7 - n));
100 }
101
102 return version;
103 }
104
105 void Desfire_key_set_version (desfirekey_t key, uint8_t version)
106 {
107 for (int n = 0; n < 8; n++) {
108 uint8_t version_bit = ((version & (1 << (7-n))) >> (7-n));
109 key->data[n] &= 0xfe;
110 key->data[n] |= version_bit;
111 if (key->type == T_DES) {
112 key->data[n+8] = key->data[n];
113 } else {
114 // Write ~version to avoid turning a 3DES key into a DES key
115 key->data[n+8] &= 0xfe;
116 key->data[n+8] |= ~version_bit;
117 }
118 }
119 }
120
121 desfirekey_t Desfire_session_key_new (const uint8_t rnda[], const uint8_t rndb[], desfirekey_t authkey) {
122
123 desfirekey_t key = NULL;
124
125 uint8_t buffer[24];
126
127 switch (authkey->type) {
128 case T_DES:
129 memcpy (buffer, rnda, 4);
130 memcpy (buffer+4, rndb, 4);
131 key = Desfire_des_key_new_with_version (buffer);
132 break;
133 case T_3DES:
134 memcpy (buffer, rnda, 4);
135 memcpy (buffer+4, rndb, 4);
136 memcpy (buffer+8, rnda+4, 4);
137 memcpy (buffer+12, rndb+4, 4);
138 key = Desfire_3des_key_new_with_version (buffer);
139 break;
140 case T_3K3DES:
141 memcpy (buffer, rnda, 4);
142 memcpy (buffer+4, rndb, 4);
143 memcpy (buffer+8, rnda+6, 4);
144 memcpy (buffer+12, rndb+6, 4);
145 memcpy (buffer+16, rnda+12, 4);
146 memcpy (buffer+20, rndb+12, 4);
147 key = Desfire_3k3des_key_new (buffer);
148 break;
149 case T_AES:
150 memcpy (buffer, rnda, 4);
151 memcpy (buffer+4, rndb, 4);
152 memcpy (buffer+8, rnda+12, 4);
153 memcpy (buffer+12, rndb+12, 4);
154 key = Desfire_aes_key_new (buffer);
155 break;
156 }
157 return key;
158 }
Impressum, Datenschutz