]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/desfire_key.c
CHG: enhanced the debug output for some LF demod/decode
[proxmark3-svn] / armsrc / desfire_key.c
CommitLineData
f38a1528 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 */
f6c18637 19#include <stdlib.h>
20#include "desfire_key.h"
f38a1528 21
22static inline void update_key_schedules (desfirekey_t key);
23
24static 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
f6c18637 32void Desfire_des_key_new (const uint8_t value[8], desfirekey_t key) {
f38a1528 33 uint8_t data[8];
34 memcpy (data, value, 8);
35 for (int n=0; n < 8; n++)
36 data[n] &= 0xfe;
f6c18637 37 Desfire_des_key_new_with_version (data, key);
f38a1528 38}
39
f6c18637 40void Desfire_des_key_new_with_version (const uint8_t value[8], desfirekey_t key) {
41 if ( 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 }
f38a1528 47}
48
f6c18637 49void Desfire_3des_key_new (const uint8_t value[16], desfirekey_t key) {
f38a1528 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;
f6c18637 56 Desfire_3des_key_new_with_version (data, key);
f38a1528 57}
58
f6c18637 59void Desfire_3des_key_new_with_version (const uint8_t value[16], desfirekey_t key) {
60 if ( key != NULL ){
61 key->type = T_3DES;
62 memcpy (key->data, value, 16);
857bc2ff 63 memcpy (key->data + 16, value, 8);
f6c18637 64 update_key_schedules (key);
65 }
f38a1528 66}
67
f6c18637 68void Desfire_3k3des_key_new (const uint8_t value[24], desfirekey_t key) {
f38a1528 69 uint8_t data[24];
70 memcpy (data, value, 24);
71 for (int n=0; n < 8; n++)
72 data[n] &= 0xfe;
f6c18637 73 Desfire_3k3des_key_new_with_version (data, key);
f38a1528 74}
75
f6c18637 76void Desfire_3k3des_key_new_with_version (const uint8_t value[24], desfirekey_t key) {
77 if ( key != NULL){
78 key->type = T_3K3DES;
79 memcpy (key->data, value, 24);
80 update_key_schedules (key);
81 }
f38a1528 82}
83
f6c18637 84 void Desfire_aes_key_new (const uint8_t value[16], desfirekey_t key) {
85 Desfire_aes_key_new_with_version (value, 0, key);
f38a1528 86}
87
f6c18637 88 void Desfire_aes_key_new_with_version (const uint8_t value[16], uint8_t version, desfirekey_t key) {
89
90 if (key != NULL) {
91 memcpy (key->data, value, 16);
92 key->type = T_AES;
93 key->aes_version = version;
94 }
f38a1528 95}
96
97uint8_t Desfire_key_get_version (desfirekey_t key) {
98 uint8_t version = 0;
99
100 for (int n = 0; n < 8; n++) {
101 version |= ((key->data[n] & 1) << (7 - n));
102 }
f38a1528 103 return version;
104}
105
106void Desfire_key_set_version (desfirekey_t key, uint8_t version)
107{
108 for (int n = 0; n < 8; n++) {
109 uint8_t version_bit = ((version & (1 << (7-n))) >> (7-n));
110 key->data[n] &= 0xfe;
111 key->data[n] |= version_bit;
112 if (key->type == T_DES) {
113 key->data[n+8] = key->data[n];
114 } else {
115 // Write ~version to avoid turning a 3DES key into a DES key
116 key->data[n+8] &= 0xfe;
117 key->data[n+8] |= ~version_bit;
118 }
119 }
120}
121
f6c18637 122void Desfire_session_key_new (const uint8_t rnda[], const uint8_t rndb[], desfirekey_t authkey, desfirekey_t key) {
f38a1528 123
124 uint8_t buffer[24];
125
126 switch (authkey->type) {
127 case T_DES:
128 memcpy (buffer, rnda, 4);
129 memcpy (buffer+4, rndb, 4);
f6c18637 130 Desfire_des_key_new_with_version (buffer, key);
f38a1528 131 break;
132 case T_3DES:
133 memcpy (buffer, rnda, 4);
134 memcpy (buffer+4, rndb, 4);
135 memcpy (buffer+8, rnda+4, 4);
136 memcpy (buffer+12, rndb+4, 4);
f6c18637 137 Desfire_3des_key_new_with_version (buffer, key);
f38a1528 138 break;
139 case T_3K3DES:
140 memcpy (buffer, rnda, 4);
141 memcpy (buffer+4, rndb, 4);
142 memcpy (buffer+8, rnda+6, 4);
143 memcpy (buffer+12, rndb+6, 4);
144 memcpy (buffer+16, rnda+12, 4);
145 memcpy (buffer+20, rndb+12, 4);
f6c18637 146 Desfire_3k3des_key_new (buffer, key);
f38a1528 147 break;
148 case T_AES:
149 memcpy (buffer, rnda, 4);
150 memcpy (buffer+4, rndb, 4);
151 memcpy (buffer+8, rnda+12, 4);
152 memcpy (buffer+12, rndb+12, 4);
f6c18637 153 Desfire_aes_key_new (buffer, key);
f38a1528 154 break;
155 }
f38a1528 156}
Impressum, Datenschutz