]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/optimized_cipher.c
cleaning up iclass.c and optimized_cipher.c
[proxmark3-svn] / armsrc / optimized_cipher.c
CommitLineData
c99dc845
MHS
1/*****************************************************************************
2 * WARNING
3 *
17505ce2 4 * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY.
5 *
6 * USAGE OF THIS CODE IN OTHER WAYS MAY INFRINGE UPON THE INTELLECTUAL
7 * PROPERTY OF OTHER PARTIES, SUCH AS INSIDE SECURE AND HID GLOBAL,
8 * AND MAY EXPOSE YOU TO AN INFRINGEMENT ACTION FROM THOSE PARTIES.
9 *
10 * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS.
c99dc845
MHS
11 *
12 *****************************************************************************
13 *
14 * This file is part of loclass. It is a reconstructon of the cipher engine
15 * used in iClass, and RFID techology.
16 *
17 * The implementation is based on the work performed by
18 * Flavio D. Garcia, Gerhard de Koning Gans, Roel Verdult and
19 * Milosch Meriac in the paper "Dismantling IClass".
20 *
21 * Copyright (C) 2014 Martin Holst Swende
22 *
23 * This is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License version 2 as published
298e1a2d 25 * by the Free Software Foundation, or, at your option, any later version.
c99dc845
MHS
26 *
27 * This file is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with loclass. If not, see <http://www.gnu.org/licenses/>.
17505ce2 34 *
35 *
36 *
c99dc845
MHS
37 ****************************************************************************/
38
39/**
40
41 This file contains an optimized version of the MAC-calculation algorithm. Some measurements on
42 a std laptop showed it runs in about 1/3 of the time:
43
44 Std: 0.428962
45 Opt: 0.151609
46
47 Additionally, it is self-reliant, not requiring e.g. bitstreams from the cipherutils, thus can
48 be easily dropped into a code base.
49
50 The optimizations have been performed in the following steps:
51 * Parameters passed by reference instead of by value.
52 * Iteration instead of recursion, un-nesting recursive loops into for-loops.
53 * Handling of bytes instead of individual bits, for less shuffling and masking
54 * Less creation of "objects", structs, and instead reuse of alloc:ed memory
55 * Inlining some functions via #define:s
56
57 As a consequence, this implementation is less generic. Also, I haven't bothered documenting this.
58 For a thorough documentation, check out the MAC-calculation within cipher.c instead.
59
60 -- MHS 2015
61**/
62
63#include "optimized_cipher.h"
b8e461ff 64#include <stddef.h>
c99dc845
MHS
65#include <stdbool.h>
66#include <stdint.h>
c99dc845 67
c99dc845
MHS
68
69#define opt_T(s) (0x1 & ((s->t >> 15) ^ (s->t >> 14)^ (s->t >> 10)^ (s->t >> 8)^ (s->t >> 5)^ (s->t >> 4)^ (s->t >> 1)^ s->t))
70
71#define opt_B(s) (((s->b >> 6) ^ (s->b >> 5) ^ (s->b >> 4) ^ (s->b)) & 0x1)
72
73#define opt__select(x,y,r) (4 & (((r & (r << 2)) >> 5) ^ ((r & ~(r << 2)) >> 4) ^ ( (r | r << 2) >> 3)))\
74 |(2 & (((r | r << 2) >> 6) ^ ( (r | r << 2) >> 1) ^ (r >> 5) ^ r ^ ((x^y) << 1)))\
75 |(1 & (((r & ~(r << 2)) >> 4) ^ ((r & (r << 2)) >> 3) ^ r ^ x))
76
77/*
78 * Some background on the expression above can be found here...
79uint8_t xopt__select(bool x, bool y, uint8_t r)
80{
81 uint8_t r_ls2 = r << 2;
82 uint8_t r_and_ls2 = r & r_ls2;
83 uint8_t r_or_ls2 = r | r_ls2;
84
85 //r: r0 r1 r2 r3 r4 r5 r6 r7
86 //r_ls2: r2 r3 r4 r5 r6 r7 0 0
87 // z0
88 // z1
89
17505ce2 90// uint8_t z0 = (r0 & r2) ^ (r1 & ~r3) ^ (r2 | r4); // <-- original
c99dc845
MHS
91 uint8_t z0 = (r_and_ls2 >> 5) ^ ((r & ~r_ls2) >> 4) ^ ( r_or_ls2 >> 3);
92
17505ce2 93// uint8_t z1 = (r0 | r2) ^ ( r5 | r7) ^ r1 ^ r6 ^ x ^ y; // <-- original
c99dc845
MHS
94 uint8_t z1 = (r_or_ls2 >> 6) ^ ( r_or_ls2 >> 1) ^ (r >> 5) ^ r ^ ((x^y) << 1);
95
17505ce2 96// uint8_t z2 = (r3 & ~r5) ^ (r4 & r6 ) ^ r7 ^ x; // <-- original
c99dc845
MHS
97 uint8_t z2 = ((r & ~r_ls2) >> 4) ^ (r_and_ls2 >> 3) ^ r ^ x;
98
99 return (z0 & 4) | (z1 & 2) | (z2 & 1);
100}
101*/
102
17505ce2 103void opt_successor(const uint8_t *k, State *s, bool y, State *successor) {
c99dc845
MHS
104 uint8_t Tt = 1 & opt_T(s);
105
106 successor->t = (s->t >> 1);
107 successor->t |= (Tt ^ (s->r >> 7 & 0x1) ^ (s->r >> 3 & 0x1)) << 15;
108
109 successor->b = s->b >> 1;
110 successor->b |= (opt_B(s) ^ (s->r & 0x1)) << 7;
111
17505ce2 112 successor->r = (k[opt__select(Tt, y, s->r)] ^ successor->b) + s->l ;
113 successor->l = successor->r + s->r;
c99dc845
MHS
114}
115
17505ce2 116void opt_suc(const uint8_t *k, State *s, uint8_t *in, uint8_t length, bool add32Zeroes) {
c99dc845 117 State x2;
17505ce2 118 for (int i = 0; i < length; i++) {
119 uint8_t head;
c99dc845 120 head = 1 & (in[i] >> 7);
17505ce2 121 opt_successor(k, s, head, &x2);
c99dc845
MHS
122
123 head = 1 & (in[i] >> 6);
17505ce2 124 opt_successor(k, &x2, head, s);
c99dc845
MHS
125
126 head = 1 & (in[i] >> 5);
17505ce2 127 opt_successor(k, s, head, &x2);
c99dc845
MHS
128
129 head = 1 & (in[i] >> 4);
17505ce2 130 opt_successor(k, &x2, head, s);
c99dc845
MHS
131
132 head = 1 & (in[i] >> 3);
17505ce2 133 opt_successor(k, s, head, &x2);
c99dc845
MHS
134
135 head = 1 & (in[i] >> 2);
17505ce2 136 opt_successor(k, &x2, head, s);
c99dc845
MHS
137
138 head = 1 & (in[i] >> 1);
17505ce2 139 opt_successor(k, s, head, &x2);
c99dc845
MHS
140
141 head = 1 & in[i];
17505ce2 142 opt_successor(k, &x2, head, s);
c99dc845 143 }
61fe9073 144 //For tag MAC, an additional 32 zeroes
17505ce2 145 if (add32Zeroes) {
146 for(int i = 0; i < 16; i++) {
147 opt_successor(k, s, 0, &x2);
148 opt_successor(k, &x2, 0, s);
61fe9073 149 }
17505ce2 150 }
c99dc845
MHS
151}
152
17505ce2 153void opt_output(const uint8_t *k, State *s, uint8_t *buffer) {
154 State temp = {0, 0, 0, 0};
155 for (uint8_t times = 0; times < 4; times++) {
156 uint8_t bout = 0;
c99dc845 157 bout |= (s->r & 0x4) << 5;
17505ce2 158 opt_successor(k, s, 0, &temp);
c99dc845 159 bout |= (temp.r & 0x4) << 4;
17505ce2 160 opt_successor(k, &temp, 0, s);
c99dc845 161 bout |= (s->r & 0x4) << 3;
17505ce2 162 opt_successor(k, s, 0, &temp);
c99dc845 163 bout |= (temp.r & 0x4) << 2;
17505ce2 164 opt_successor(k, &temp, 0, s);
c99dc845 165 bout |= (s->r & 0x4) << 1;
17505ce2 166 opt_successor(k, s, 0, &temp);
c99dc845 167 bout |= (temp.r & 0x4) ;
17505ce2 168 opt_successor(k, &temp, 0, s);
c99dc845 169 bout |= (s->r & 0x4) >> 1;
17505ce2 170 opt_successor(k, s, 0, &temp);
c99dc845 171 bout |= (temp.r & 0x4) >> 2;
17505ce2 172 opt_successor(k, &temp, 0, s);
c99dc845
MHS
173 buffer[times] = bout;
174 }
c99dc845
MHS
175}
176
17505ce2 177void opt_MAC(uint8_t *k, uint8_t *input, uint8_t *out) {
c99dc845 178 State _init = {
17505ce2 179 ((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
180 ((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
181 0x4c, // b
182 0xE012 // t
183 };
c99dc845 184
17505ce2 185 opt_suc(k, &_init, input, 12, false);
c99dc845 186 //printf("\noutp ");
17505ce2 187 opt_output(k, &_init, out);
c99dc845 188}
17505ce2 189
c99dc845
MHS
190uint8_t rev_byte(uint8_t b) {
191 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
192 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
193 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
17505ce2 194 return b;
c99dc845 195}
17505ce2 196
197void opt_reverse_arraybytecpy(uint8_t *dest, uint8_t *src, size_t len) {
198 for (size_t i = 0; i < len; i++) {
c99dc845 199 dest[i] = rev_byte(src[i]);
17505ce2 200 }
c99dc845
MHS
201}
202
17505ce2 203void opt_doReaderMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4]) {
61fe9073 204 static uint8_t cc_nr[12];
17505ce2 205 opt_reverse_arraybytecpy(cc_nr, cc_nr_p, 12);
206 uint8_t dest[] = {0, 0, 0, 0, 0, 0, 0, 0};
207 opt_MAC(div_key_p, cc_nr, dest);
61fe9073 208 //The output MAC must also be reversed
17505ce2 209 opt_reverse_arraybytecpy(mac, dest, 4);
61fe9073
MHS
210 return;
211}
17505ce2 212
213void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4]) {
61fe9073 214 static uint8_t cc_nr[8+4+4];
17505ce2 215 opt_reverse_arraybytecpy(cc_nr, cc_p, 12);
216 State _init = {
217 ((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
218 ((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
219 0x4c, // b
220 0xE012 // t
221 };
222 opt_suc(div_key_p, &_init,cc_nr, 12, true);
223 uint8_t dest[] = {0, 0, 0, 0};
224 opt_output(div_key_p, &_init, dest);
61fe9073 225 //The output MAC must also be reversed
17505ce2 226 opt_reverse_arraybytecpy(mac, dest, 4);
61fe9073 227 return;
61fe9073 228}
17505ce2 229
61fe9073
MHS
230/**
231 * The tag MAC can be divided (both can, but no point in dividing the reader mac) into
232 * two functions, since the first 8 bytes are known, we can pre-calculate the state
233 * reached after feeding CC to the cipher.
234 * @param cc_p
235 * @param div_key_p
236 * @return the cipher state
237 */
17505ce2 238State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p) {
61fe9073 239 static uint8_t cc_nr[8];
17505ce2 240 opt_reverse_arraybytecpy(cc_nr, cc_p, 8);
241 State _init = {
242 ((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
243 ((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
244 0x4c, // b
245 0xE012 // t
246 };
247 opt_suc(div_key_p, &_init, cc_nr, 8, false);
61fe9073
MHS
248 return _init;
249}
17505ce2 250
61fe9073
MHS
251/**
252 * The second part of the tag MAC calculation, since the CC is already calculated into the state,
253 * this function is fed only the NR, and internally feeds the remaining 32 0-bits to generate the tag
254 * MAC response.
255 * @param _init - precalculated cipher state
256 * @param nr - the reader challenge
257 * @param mac - where to store the MAC
258 * @param div_key_p - the key to use
259 */
17505ce2 260void opt_doTagMAC_2(State _init, uint8_t *nr, uint8_t mac[4], const uint8_t *div_key_p) {
261 static uint8_t _nr[4];
61fe9073 262 opt_reverse_arraybytecpy(_nr, nr, 4);
17505ce2 263 opt_suc(div_key_p, &_init, _nr, 4, true);
264 //opt_suc(div_key_p, &_init,nr, 4, false);
265 uint8_t dest[] = {0, 0, 0, 0};
266 opt_output(div_key_p, &_init, dest);
c99dc845 267 //The output MAC must also be reversed
17505ce2 268 opt_reverse_arraybytecpy(mac, dest, 4);
c99dc845
MHS
269 return;
270}
Impressum, Datenschutz