chg: syntax sugar

This commit is contained in:
iceman1001 2018-01-02 11:15:23 +01:00
commit 036d050384

View file

@ -96,8 +96,7 @@ uint8_t xopt__select(bool x, bool y, uint8_t r)
} }
*/ */
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); uint8_t Tt = 1 & opt_T(s);
successor->t = (s->t >> 1); successor->t = (s->t >> 1);
@ -108,81 +107,74 @@ void opt_successor(const uint8_t* k, State *s, bool y, State* successor)
successor->r = (k[opt__select(Tt,y,s->r)] ^ successor->b) + s->l ; successor->r = (k[opt__select(Tt,y,s->r)] ^ successor->b) + s->l ;
successor->l = successor->r+s->r; 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; State x2;
int i; int i;
uint8_t head = 0; uint8_t head = 0;
for(i =0 ; i < length ; i++) for (i = 0; i < length; i++) {
{
head = 1 & (in[i] >> 7); head = 1 & (in[i] >> 7);
opt_successor(k,s,head,&x2); opt_successor(k, s, head, &x2);
head = 1 & (in[i] >> 6); head = 1 & (in[i] >> 6);
opt_successor(k,&x2,head,s); opt_successor(k, &x2, head, s);
head = 1 & (in[i] >> 5); head = 1 & (in[i] >> 5);
opt_successor(k,s,head,&x2); opt_successor(k, s, head, &x2);
head = 1 & (in[i] >> 4); head = 1 & (in[i] >> 4);
opt_successor(k,&x2,head,s); opt_successor(k, &x2, head, s);
head = 1 & (in[i] >> 3); head = 1 & (in[i] >> 3);
opt_successor(k,s,head,&x2); opt_successor(k, s, head, &x2);
head = 1 & (in[i] >> 2); head = 1 & (in[i] >> 2);
opt_successor(k,&x2,head,s); opt_successor(k, &x2, head, s);
head = 1 & (in[i] >> 1); head = 1 & (in[i] >> 1);
opt_successor(k,s,head,&x2); opt_successor(k, s, head, &x2);
head = 1 & in[i]; head = 1 & in[i];
opt_successor(k,&x2,head,s); opt_successor(k, &x2, head, s);
} }
//For tag MAC, an additional 32 zeroes //For tag MAC, an additional 32 zeroes
if(add32Zeroes) if (add32Zeroes) {
for(i =0 ; i < 16 ; i++) for (i = 0; i < 16; i++) {
{ opt_successor(k, s, 0, &x2);
opt_successor(k,s,0,&x2); opt_successor(k, &x2, 0, s);
opt_successor(k,&x2,0,s); }
} }
} }
void opt_output(const uint8_t* k,State* s, uint8_t *buffer) void opt_output(const uint8_t* k,State* s, uint8_t *buffer) {
{
uint8_t times = 0; uint8_t times = 0;
uint8_t bout = 0; uint8_t bout = 0;
State temp = {0,0,0,0}; State temp = {0,0,0,0};
for( ; times < 4 ; times++) for ( ; times < 4; times++) {
{
bout =0; bout =0;
bout |= (s->r & 0x4) << 5; bout |= (s->r & 0x4) << 5;
opt_successor(k,s,0,&temp); opt_successor(k, s, 0, &temp);
bout |= (temp.r & 0x4) << 4; bout |= (temp.r & 0x4) << 4;
opt_successor(k,&temp,0,s); opt_successor(k, &temp, 0, s);
bout |= (s->r & 0x4) << 3; bout |= (s->r & 0x4) << 3;
opt_successor(k,s,0,&temp); opt_successor(k, s, 0, &temp);
bout |= (temp.r & 0x4) << 2; bout |= (temp.r & 0x4) << 2;
opt_successor(k,&temp,0,s); opt_successor(k, &temp, 0, s);
bout |= (s->r & 0x4) << 1; bout |= (s->r & 0x4) << 1;
opt_successor(k,s,0,&temp); opt_successor(k, s, 0, &temp);
bout |= (temp.r & 0x4) ; bout |= (temp.r & 0x4) ;
opt_successor(k,&temp,0,s); opt_successor(k, &temp, 0, s);
bout |= (s->r & 0x4) >> 1; bout |= (s->r & 0x4) >> 1;
opt_successor(k,s,0,&temp); opt_successor(k, s, 0, &temp);
bout |= (temp.r & 0x4) >> 2; bout |= (temp.r & 0x4) >> 2;
opt_successor(k,&temp,0,s); opt_successor(k, &temp, 0, s);
buffer[times] = bout; 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 = { State _init = {
((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l ((k[0] ^ 0x4c) + 0xEC) & 0xFF,// l
((k[0] ^ 0x4c) + 0x21) & 0xFF,// r ((k[0] ^ 0x4c) + 0x21) & 0xFF,// r
@ -191,35 +183,32 @@ void opt_MAC(uint8_t* k, uint8_t* input, uint8_t* out)
}; };
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) { uint8_t rev_byte(uint8_t b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2; b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1; b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b; return b;
} }
void opt_reverse_arraybytecpy(uint8_t* dest, uint8_t *src, size_t len)
{ void opt_reverse_arraybytecpy(uint8_t* dest, uint8_t *src, size_t len) {
uint8_t i; uint8_t i;
for( i =0; i< len ; i++) for ( i =0; i< len ; i++)
dest[i] = rev_byte(src[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]; static uint8_t cc_nr[12];
opt_reverse_arraybytecpy(cc_nr, cc_nr_p, 12); opt_reverse_arraybytecpy(cc_nr, cc_nr_p, 12);
uint8_t dest []= {0,0,0,0,0,0,0,0}; uint8_t dest [] = {0,0,0,0,0,0,0,0};
opt_MAC(div_key_p, cc_nr, dest); opt_MAC(div_key_p, cc_nr, dest);
//The output MAC must also be reversed //The output MAC must also be reversed
opt_reverse_arraybytecpy(mac, dest, 4); opt_reverse_arraybytecpy(mac, dest, 4);
return; 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]; static uint8_t cc_nr[8+4+4];
opt_reverse_arraybytecpy(cc_nr, cc_p, 12); opt_reverse_arraybytecpy(cc_nr, cc_p, 12);
State _init = { State _init = {
@ -229,7 +218,7 @@ void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4])
0xE012 // t 0xE012 // t
}; };
opt_suc(div_key_p, &_init, cc_nr, 12, true); opt_suc(div_key_p, &_init, cc_nr, 12, true);
uint8_t dest []= {0,0,0,0}; uint8_t dest [] = {0,0,0,0};
opt_output(div_key_p, &_init, dest); opt_output(div_key_p, &_init, dest);
//The output MAC must also be reversed //The output MAC must also be reversed
opt_reverse_arraybytecpy(mac, dest,4); opt_reverse_arraybytecpy(mac, dest,4);
@ -244,8 +233,7 @@ void opt_doTagMAC(uint8_t *cc_p, const uint8_t *div_key_p, uint8_t mac[4])
* @param div_key_p * @param div_key_p
* @return the cipher state * @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]; static uint8_t cc_nr[8];
opt_reverse_arraybytecpy(cc_nr, cc_p, 8); opt_reverse_arraybytecpy(cc_nr, cc_p, 8);
State _init = { State _init = {
@ -266,13 +254,12 @@ State opt_doTagMAC_1(uint8_t *cc_p, const uint8_t *div_key_p)
* @param mac - where to store the MAC * @param mac - where to store the MAC
* @param div_key_p - the key to use * @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) void opt_doTagMAC_2(State _init, uint8_t* nr, uint8_t mac[4], const uint8_t* div_key_p) {
{ static uint8_t _nr[4];
static uint8_t _nr [4];
opt_reverse_arraybytecpy(_nr, 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, true);
//opt_suc(div_key_p, &_init,nr, 4, false);
uint8_t dest []= {0,0,0,0}; uint8_t dest [] = {0,0,0,0};
opt_output(div_key_p, &_init, dest); opt_output(div_key_p, &_init, dest);
//The output MAC must also be reversed //The output MAC must also be reversed
opt_reverse_arraybytecpy(mac, dest,4); opt_reverse_arraybytecpy(mac, dest,4);