X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/9f9b6b74695afda622c3aa05c28e576ab1fbf5c9..17505ce2a74468c89747c996a72139aa9f3ca225:/armsrc/optimized_cipher.c?ds=sidebyside
diff --git a/armsrc/optimized_cipher.c b/armsrc/optimized_cipher.c
index 23562621..ee9d5568 100644
--- a/armsrc/optimized_cipher.c
+++ b/armsrc/optimized_cipher.c
@@ -1,13 +1,13 @@
/*****************************************************************************
* WARNING
*
- * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY.
- *
- * USAGE OF THIS CODE IN OTHER WAYS MAY INFRINGE UPON THE INTELLECTUAL
- * PROPERTY OF OTHER PARTIES, SUCH AS INSIDE SECURE AND HID GLOBAL,
- * AND MAY EXPOSE YOU TO AN INFRINGEMENT ACTION FROM THOSE PARTIES.
- *
- * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS.
+ * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY.
+ *
+ * USAGE OF THIS CODE IN OTHER WAYS MAY INFRINGE UPON THE INTELLECTUAL
+ * PROPERTY OF OTHER PARTIES, SUCH AS INSIDE SECURE AND HID GLOBAL,
+ * AND MAY EXPOSE YOU TO AN INFRINGEMENT ACTION FROM THOSE PARTIES.
+ *
+ * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS.
*
*****************************************************************************
*
@@ -22,7 +22,7 @@
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
+ * by the Free Software Foundation, or, at your option, any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -31,9 +31,9 @@
*
* You should have received a copy of the GNU General Public License
* along with loclass. If not, see .
- *
- *
- *
+ *
+ *
+ *
****************************************************************************/
/**
@@ -61,11 +61,9 @@
**/
#include "optimized_cipher.h"
-#include
-#include
+#include
#include
#include
-#include
#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))
@@ -89,22 +87,20 @@ uint8_t xopt__select(bool x, bool y, uint8_t r)
// z0
// z1
-// uint8_t z0 = (r0 & r2) ^ (r1 & ~r3) ^ (r2 | r4); // <-- original
+// uint8_t z0 = (r0 & r2) ^ (r1 & ~r3) ^ (r2 | r4); // <-- original
uint8_t z0 = (r_and_ls2 >> 5) ^ ((r & ~r_ls2) >> 4) ^ ( r_or_ls2 >> 3);
-// uint8_t z1 = (r0 | r2) ^ ( r5 | r7) ^ r1 ^ r6 ^ x ^ y; // <-- original
+// uint8_t z1 = (r0 | r2) ^ ( r5 | r7) ^ r1 ^ r6 ^ x ^ y; // <-- original
uint8_t z1 = (r_or_ls2 >> 6) ^ ( r_or_ls2 >> 1) ^ (r >> 5) ^ r ^ ((x^y) << 1);
-// uint8_t z2 = (r3 & ~r5) ^ (r4 & r6 ) ^ r7 ^ x; // <-- original
+// uint8_t z2 = (r3 & ~r5) ^ (r4 & r6 ) ^ r7 ^ x; // <-- original
uint8_t z2 = ((r & ~r_ls2) >> 4) ^ (r_and_ls2 >> 3) ^ r ^ x;
return (z0 & 4) | (z1 & 2) | (z2 & 1);
}
*/
-void opt_successor(const uint8_t* k, State *s, bool y, State* successor)
-{
-
+void opt_successor(const uint8_t *k, State *s, bool y, State *successor) {
uint8_t Tt = 1 & opt_T(s);
successor->t = (s->t >> 1);
@@ -113,136 +109,124 @@ void opt_successor(const uint8_t* k, State *s, bool y, State* successor)
successor->b = s->b >> 1;
successor->b |= (opt_B(s) ^ (s->r & 0x1)) << 7;
- successor->r = (k[opt__select(Tt,y,s->r)] ^ successor->b) + s->l ;
- successor->l = successor->r+s->r;
-
+ successor->r = (k[opt__select(Tt, y, s->r)] ^ successor->b) + s->l ;
+ successor->l = successor->r + s->r;
}
-void opt_suc(const uint8_t* k,State* s, uint8_t *in, uint8_t length, bool add32Zeroes)
-{
+void opt_suc(const uint8_t *k, State *s, uint8_t *in, uint8_t length, bool add32Zeroes) {
State x2;
- int i;
- uint8_t head = 0;
- for(i =0 ; i < length ; i++)
- {
+ for (int i = 0; i < length; i++) {
+ uint8_t head;
head = 1 & (in[i] >> 7);
- opt_successor(k,s,head,&x2);
+ opt_successor(k, s, head, &x2);
head = 1 & (in[i] >> 6);
- opt_successor(k,&x2,head,s);
+ opt_successor(k, &x2, head, s);
head = 1 & (in[i] >> 5);
- opt_successor(k,s,head,&x2);
+ opt_successor(k, s, head, &x2);
head = 1 & (in[i] >> 4);
- opt_successor(k,&x2,head,s);
+ opt_successor(k, &x2, head, s);
head = 1 & (in[i] >> 3);
- opt_successor(k,s,head,&x2);
+ opt_successor(k, s, head, &x2);
head = 1 & (in[i] >> 2);
- opt_successor(k,&x2,head,s);
+ opt_successor(k, &x2, head, s);
head = 1 & (in[i] >> 1);
- opt_successor(k,s,head,&x2);
+ opt_successor(k, s, head, &x2);
head = 1 & in[i];
- opt_successor(k,&x2,head,s);
-
+ opt_successor(k, &x2, head, s);
}
//For tag MAC, an additional 32 zeroes
- if(add32Zeroes)
- for(i =0 ; i < 16 ; i++)
- {
- opt_successor(k,s,0,&x2);
- opt_successor(k,&x2,0,s);
+ if (add32Zeroes) {
+ for(int i = 0; i < 16; i++) {
+ opt_successor(k, s, 0, &x2);
+ opt_successor(k, &x2, 0, s);
}
+ }
}
-void opt_output(const uint8_t* k,State* s, uint8_t *buffer)
-{
- uint8_t times = 0;
- uint8_t bout = 0;
- State temp = {0,0,0,0};
- for( ; times < 4 ; times++)
- {
- bout =0;
+void opt_output(const uint8_t *k, State *s, uint8_t *buffer) {
+ State temp = {0, 0, 0, 0};
+ for (uint8_t times = 0; times < 4; times++) {
+ uint8_t bout = 0;
bout |= (s->r & 0x4) << 5;
- opt_successor(k,s,0,&temp);
+ opt_successor(k, s, 0, &temp);
bout |= (temp.r & 0x4) << 4;
- opt_successor(k,&temp,0,s);
+ opt_successor(k, &temp, 0, s);
bout |= (s->r & 0x4) << 3;
- opt_successor(k,s,0,&temp);
+ opt_successor(k, s, 0, &temp);
bout |= (temp.r & 0x4) << 2;
- opt_successor(k,&temp,0,s);
+ opt_successor(k, &temp, 0, s);
bout |= (s->r & 0x4) << 1;
- opt_successor(k,s,0,&temp);
+ opt_successor(k, s, 0, &temp);
bout |= (temp.r & 0x4) ;
- opt_successor(k,&temp,0,s);
+ opt_successor(k, &temp, 0, s);
bout |= (s->r & 0x4) >> 1;
- opt_successor(k,s,0,&temp);
+ opt_successor(k, s, 0, &temp);
bout |= (temp.r & 0x4) >> 2;
- opt_successor(k,&temp,0,s);
+ opt_successor(k, &temp, 0, s);
buffer[times] = bout;
}
-
}
-void opt_MAC(uint8_t* k, uint8_t* input, uint8_t* out)
-{
+void opt_MAC(uint8_t *k, uint8_t *input, uint8_t *out) {
State _init = {
- ((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
- ((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
- 0x4c, // b
- 0xE012 // t
- };
+ ((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
+ ((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
+ 0x4c, // b
+ 0xE012 // t
+ };
- opt_suc(k,&_init,input,12, false);
+ opt_suc(k, &_init, input, 12, false);
//printf("\noutp ");
- opt_output(k,&_init, out);
+ opt_output(k, &_init, out);
}
+
uint8_t rev_byte(uint8_t b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
- return b;
+ return b;
}
-void opt_reverse_arraybytecpy(uint8_t* dest, uint8_t *src, size_t len)
-{
- uint8_t i;
- for( i =0; i< len ; i++)
+
+void opt_reverse_arraybytecpy(uint8_t *dest, uint8_t *src, size_t len) {
+ for (size_t i = 0; i < len; i++) {
dest[i] = rev_byte(src[i]);
+ }
}
-void opt_doReaderMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4])
-{
+void opt_doReaderMAC(uint8_t *cc_nr_p, uint8_t *div_key_p, uint8_t mac[4]) {
static uint8_t cc_nr[12];
-
- opt_reverse_arraybytecpy(cc_nr, cc_nr_p,12);
- uint8_t dest []= {0,0,0,0,0,0,0,0};
- opt_MAC(div_key_p,cc_nr, dest);
+ opt_reverse_arraybytecpy(cc_nr, cc_nr_p, 12);
+ uint8_t dest[] = {0, 0, 0, 0, 0, 0, 0, 0};
+ opt_MAC(div_key_p, cc_nr, dest);
//The output MAC must also be reversed
- opt_reverse_arraybytecpy(mac, dest,4);
+ opt_reverse_arraybytecpy(mac, dest, 4);
return;
}
-void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4])
-{
+
+void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4]) {
static uint8_t cc_nr[8+4+4];
- opt_reverse_arraybytecpy(cc_nr, cc_p,12);
- State _init = {
- ((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
- ((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
- 0x4c, // b
- 0xE012 // t
- };
- opt_suc(div_key_p,&_init,cc_nr, 12,true);
- uint8_t dest []= {0,0,0,0};
- opt_output(div_key_p,&_init, dest);
+ opt_reverse_arraybytecpy(cc_nr, cc_p, 12);
+ State _init = {
+ ((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
+ ((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
+ 0x4c, // b
+ 0xE012 // t
+ };
+ opt_suc(div_key_p, &_init,cc_nr, 12, true);
+ uint8_t dest[] = {0, 0, 0, 0};
+ opt_output(div_key_p, &_init, dest);
//The output MAC must also be reversed
- opt_reverse_arraybytecpy(mac, dest,4);
+ opt_reverse_arraybytecpy(mac, dest, 4);
return;
-
}
+
/**
* The tag MAC can be divided (both can, but no point in dividing the reader mac) into
* two functions, since the first 8 bytes are known, we can pre-calculate the state
@@ -251,19 +235,19 @@ void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4])
* @param div_key_p
* @return the cipher state
*/
-State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p)
-{
+State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p) {
static uint8_t cc_nr[8];
- opt_reverse_arraybytecpy(cc_nr, cc_p,8);
- State _init = {
- ((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
- ((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
- 0x4c, // b
- 0xE012 // t
- };
- opt_suc(div_key_p,&_init,cc_nr, 8,false);
+ opt_reverse_arraybytecpy(cc_nr, cc_p, 8);
+ State _init = {
+ ((div_key_p[0] ^ 0x4c) + 0xEC) & 0xFF,// l
+ ((div_key_p[0] ^ 0x4c) + 0x21) & 0xFF,// r
+ 0x4c, // b
+ 0xE012 // t
+ };
+ opt_suc(div_key_p, &_init, cc_nr, 8, false);
return _init;
}
+
/**
* The second part of the tag MAC calculation, since the CC is already calculated into the state,
* this function is fed only the NR, and internally feeds the remaining 32 0-bits to generate the tag
@@ -273,15 +257,14 @@ State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p)
* @param mac - where to store the MAC
* @param div_key_p - the key to use
*/
-void opt_doTagMAC_2(State _init, uint8_t* nr, uint8_t mac[4], const uint8_t* div_key_p)
-{
- static uint8_t _nr [4];
+void opt_doTagMAC_2(State _init, uint8_t *nr, uint8_t mac[4], const uint8_t *div_key_p) {
+ static uint8_t _nr[4];
opt_reverse_arraybytecpy(_nr, nr, 4);
- opt_suc(div_key_p,&_init,_nr, 4, true);
- //opt_suc(div_key_p,&_init,nr, 4, false);
- uint8_t dest []= {0,0,0,0};
- opt_output(div_key_p,&_init, dest);
+ opt_suc(div_key_p, &_init, _nr, 4, true);
+ //opt_suc(div_key_p, &_init,nr, 4, false);
+ uint8_t dest[] = {0, 0, 0, 0};
+ opt_output(div_key_p, &_init, dest);
//The output MAC must also be reversed
- opt_reverse_arraybytecpy(mac, dest,4);
+ opt_reverse_arraybytecpy(mac, dest, 4);
return;
}