mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2026-05-12 11:18:11 -07:00
common: fix mix of spaces & tabs
This commit is contained in:
+38
-38
@@ -1,47 +1,47 @@
|
||||
#include "bucketsort.h"
|
||||
|
||||
extern void bucket_sort_intersect(uint32_t* const estart, uint32_t* const estop,
|
||||
uint32_t* const ostart, uint32_t* const ostop,
|
||||
bucket_info_t *bucket_info, bucket_array_t bucket)
|
||||
uint32_t* const ostart, uint32_t* const ostop,
|
||||
bucket_info_t *bucket_info, bucket_array_t bucket)
|
||||
{
|
||||
uint32_t *p1, *p2;
|
||||
uint32_t *start[2];
|
||||
uint32_t *stop[2];
|
||||
uint32_t *p1, *p2;
|
||||
uint32_t *start[2];
|
||||
uint32_t *stop[2];
|
||||
|
||||
start[0] = estart;
|
||||
stop[0] = estop;
|
||||
start[1] = ostart;
|
||||
stop[1] = ostop;
|
||||
start[0] = estart;
|
||||
stop[0] = estop;
|
||||
start[1] = ostart;
|
||||
stop[1] = ostop;
|
||||
|
||||
// init buckets to be empty
|
||||
for (uint32_t i = 0; i < 2; i++) {
|
||||
for (uint32_t j = 0x00; j <= 0xff; j++) {
|
||||
bucket[i][j].bp = bucket[i][j].head;
|
||||
}
|
||||
}
|
||||
// init buckets to be empty
|
||||
for (uint32_t i = 0; i < 2; i++) {
|
||||
for (uint32_t j = 0x00; j <= 0xff; j++) {
|
||||
bucket[i][j].bp = bucket[i][j].head;
|
||||
}
|
||||
}
|
||||
|
||||
// sort the lists into the buckets based on the MSB (contribution bits)
|
||||
for (uint32_t i = 0; i < 2; i++) {
|
||||
for (p1 = start[i]; p1 <= stop[i]; p1++) {
|
||||
uint32_t bucket_index = (*p1 & 0xff000000) >> 24;
|
||||
*(bucket[i][bucket_index].bp++) = *p1;
|
||||
}
|
||||
}
|
||||
// sort the lists into the buckets based on the MSB (contribution bits)
|
||||
for (uint32_t i = 0; i < 2; i++) {
|
||||
for (p1 = start[i]; p1 <= stop[i]; p1++) {
|
||||
uint32_t bucket_index = (*p1 & 0xff000000) >> 24;
|
||||
*(bucket[i][bucket_index].bp++) = *p1;
|
||||
}
|
||||
}
|
||||
|
||||
// write back intersecting buckets as sorted list.
|
||||
// fill in bucket_info with head and tail of the bucket contents in the list and number of non-empty buckets.
|
||||
uint32_t nonempty_bucket;
|
||||
for (uint32_t i = 0; i < 2; i++) {
|
||||
p1 = start[i];
|
||||
nonempty_bucket = 0;
|
||||
for (uint32_t j = 0x00; j <= 0xff; j++) {
|
||||
if (bucket[0][j].bp != bucket[0][j].head && bucket[1][j].bp != bucket[1][j].head) { // non-empty intersecting buckets only
|
||||
bucket_info->bucket_info[i][nonempty_bucket].head = p1;
|
||||
for (p2 = bucket[i][j].head; p2 < bucket[i][j].bp; *p1++ = *p2++);
|
||||
bucket_info->bucket_info[i][nonempty_bucket].tail = p1 - 1;
|
||||
nonempty_bucket++;
|
||||
}
|
||||
}
|
||||
bucket_info->numbuckets = nonempty_bucket;
|
||||
}
|
||||
// write back intersecting buckets as sorted list.
|
||||
// fill in bucket_info with head and tail of the bucket contents in the list and number of non-empty buckets.
|
||||
uint32_t nonempty_bucket;
|
||||
for (uint32_t i = 0; i < 2; i++) {
|
||||
p1 = start[i];
|
||||
nonempty_bucket = 0;
|
||||
for (uint32_t j = 0x00; j <= 0xff; j++) {
|
||||
if (bucket[0][j].bp != bucket[0][j].head && bucket[1][j].bp != bucket[1][j].head) { // non-empty intersecting buckets only
|
||||
bucket_info->bucket_info[i][nonempty_bucket].head = p1;
|
||||
for (p2 = bucket[i][j].head; p2 < bucket[i][j].bp; *p1++ = *p2++);
|
||||
bucket_info->bucket_info[i][nonempty_bucket].tail = p1 - 1;
|
||||
nonempty_bucket++;
|
||||
}
|
||||
}
|
||||
bucket_info->numbuckets = nonempty_bucket;
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -5,20 +5,20 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct bucket {
|
||||
uint32_t *head;
|
||||
uint32_t *bp;
|
||||
uint32_t *head;
|
||||
uint32_t *bp;
|
||||
} bucket_t;
|
||||
|
||||
typedef bucket_t bucket_array_t[2][0x100];
|
||||
|
||||
typedef struct bucket_info {
|
||||
struct {
|
||||
uint32_t *head, *tail;
|
||||
} bucket_info[2][0x100];
|
||||
uint32_t numbuckets;
|
||||
struct {
|
||||
uint32_t *head, *tail;
|
||||
} bucket_info[2][0x100];
|
||||
uint32_t numbuckets;
|
||||
} bucket_info_t;
|
||||
|
||||
void bucket_sort_intersect(uint32_t* const estart, uint32_t* const estop,
|
||||
uint32_t* const ostart, uint32_t* const ostop,
|
||||
bucket_info_t *bucket_info, bucket_array_t bucket);
|
||||
uint32_t* const ostart, uint32_t* const ostop,
|
||||
bucket_info_t *bucket_info, bucket_array_t bucket);
|
||||
#endif
|
||||
+21
-21
@@ -32,33 +32,33 @@
|
||||
#include "cmd.h"
|
||||
|
||||
uint8_t cmd_send(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void* data, size_t len) {
|
||||
UsbCommand txcmd;
|
||||
UsbCommand txcmd;
|
||||
|
||||
for (size_t i=0; i < sizeof(UsbCommand); i++)
|
||||
((uint8_t*)&txcmd)[i] = 0x00;
|
||||
for (size_t i=0; i < sizeof(UsbCommand); i++)
|
||||
((uint8_t*)&txcmd)[i] = 0x00;
|
||||
|
||||
// Compose the outgoing command frame
|
||||
txcmd.cmd = cmd;
|
||||
txcmd.arg[0] = arg0;
|
||||
txcmd.arg[1] = arg1;
|
||||
txcmd.arg[2] = arg2;
|
||||
// Compose the outgoing command frame
|
||||
txcmd.cmd = cmd;
|
||||
txcmd.arg[0] = arg0;
|
||||
txcmd.arg[1] = arg1;
|
||||
txcmd.arg[2] = arg2;
|
||||
|
||||
// Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE
|
||||
if (data && len) {
|
||||
len = MIN(len, USB_CMD_DATA_SIZE);
|
||||
for (size_t i=0; i<len; i++) {
|
||||
txcmd.d.asBytes[i] = ((uint8_t*)data)[i];
|
||||
}
|
||||
}
|
||||
// Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE
|
||||
if (data && len) {
|
||||
len = MIN(len, USB_CMD_DATA_SIZE);
|
||||
for (size_t i=0; i<len; i++) {
|
||||
txcmd.d.asBytes[i] = ((uint8_t*)data)[i];
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t sendlen = 0;
|
||||
// Send frame and make sure all bytes are transmitted
|
||||
sendlen = usb_write( (uint8_t*)&txcmd, sizeof(UsbCommand) );
|
||||
uint32_t sendlen = 0;
|
||||
// Send frame and make sure all bytes are transmitted
|
||||
sendlen = usb_write( (uint8_t*)&txcmd, sizeof(UsbCommand) );
|
||||
|
||||
#ifdef WITH_FPC
|
||||
// usart_init();
|
||||
// usart_writebuffer( (uint8_t*)&txcmd, sizeof(UsbCommand) );
|
||||
// usart_init();
|
||||
// usart_writebuffer( (uint8_t*)&txcmd, sizeof(UsbCommand) );
|
||||
#endif
|
||||
|
||||
return sendlen;
|
||||
return sendlen;
|
||||
}
|
||||
+322
-322
File diff suppressed because it is too large
Load Diff
+17
-17
@@ -28,7 +28,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
struct Crypto1State {uint32_t odd, even;};
|
||||
#if defined(__arm__) && !defined(__linux__) && !defined(_WIN32) && !defined(__APPLE__) // bare metal ARM Proxmark lacks malloc()/free()
|
||||
#if defined(__arm__) && !defined(__linux__) && !defined(_WIN32) && !defined(__APPLE__) // bare metal ARM Proxmark lacks malloc()/free()
|
||||
void crypto1_create(struct Crypto1State *s, uint64_t key);
|
||||
#else
|
||||
struct Crypto1State *crypto1_create(uint64_t key);
|
||||
@@ -53,15 +53,15 @@ uint32_t lfsr_rollback_word(struct Crypto1State* s, uint32_t in, int fb);
|
||||
int nonce_distance(uint32_t from, uint32_t to);
|
||||
extern bool validate_prng_nonce(uint32_t nonce);
|
||||
#define FOREACH_VALID_NONCE(N, FILTER, FSIZE)\
|
||||
uint32_t __n = 0,__M = 0, N = 0;\
|
||||
int __i;\
|
||||
for(; __n < 1 << 16; N = prng_successor(__M = ++__n, 16))\
|
||||
for(__i = FSIZE - 1; __i >= 0; __i--)\
|
||||
if(BIT(FILTER, __i) ^ evenparity32(__M & 0xFF01))\
|
||||
break;\
|
||||
else if(__i)\
|
||||
__M = prng_successor(__M, (__i == 7) ? 48 : 8);\
|
||||
else
|
||||
uint32_t __n = 0,__M = 0, N = 0;\
|
||||
int __i;\
|
||||
for(; __n < 1 << 16; N = prng_successor(__M = ++__n, 16))\
|
||||
for(__i = FSIZE - 1; __i >= 0; __i--)\
|
||||
if(BIT(FILTER, __i) ^ evenparity32(__M & 0xFF01))\
|
||||
break;\
|
||||
else if(__i)\
|
||||
__M = prng_successor(__M, (__i == 7) ? 48 : 8);\
|
||||
else
|
||||
|
||||
#define LF_POLY_ODD (0x29CE5C)
|
||||
#define LF_POLY_EVEN (0x870804)
|
||||
@@ -69,14 +69,14 @@ extern bool validate_prng_nonce(uint32_t nonce);
|
||||
#define BEBIT(x, n) BIT(x, (n) ^ 24)
|
||||
static inline int filter(uint32_t const x)
|
||||
{
|
||||
uint32_t f;
|
||||
uint32_t f;
|
||||
|
||||
f = 0xf22c0 >> (x & 0xf) & 16;
|
||||
f |= 0x6c9c0 >> (x >> 4 & 0xf) & 8;
|
||||
f |= 0x3c8b0 >> (x >> 8 & 0xf) & 4;
|
||||
f |= 0x1e458 >> (x >> 12 & 0xf) & 2;
|
||||
f |= 0x0d938 >> (x >> 16 & 0xf) & 1;
|
||||
return BIT(0xEC57E80A, f);
|
||||
f = 0xf22c0 >> (x & 0xf) & 16;
|
||||
f |= 0x6c9c0 >> (x >> 4 & 0xf) & 8;
|
||||
f |= 0x3c8b0 >> (x >> 8 & 0xf) & 4;
|
||||
f |= 0x1e458 >> (x >> 12 & 0xf) & 2;
|
||||
f |= 0x0d938 >> (x >> 16 & 0xf) & 1;
|
||||
return BIT(0xEC57E80A, f);
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
+97
-97
@@ -1,21 +1,21 @@
|
||||
/* crypto1.c
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, US
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
MA 02110-1301, US
|
||||
|
||||
Copyright (C) 2008-2008 bla <blapost@gmail.com>
|
||||
Copyright (C) 2008-2008 bla <blapost@gmail.com>
|
||||
*/
|
||||
#include "crapto1.h"
|
||||
|
||||
@@ -23,121 +23,121 @@
|
||||
#include "parity.h"
|
||||
|
||||
#define SWAPENDIAN(x)\
|
||||
(x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
|
||||
(x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
|
||||
|
||||
#if defined(__arm__) && !defined(__linux__) && !defined(_WIN32) && !defined(__APPLE__) // bare metal ARM Proxmark lacks malloc()/free()
|
||||
#if defined(__arm__) && !defined(__linux__) && !defined(_WIN32) && !defined(__APPLE__) // bare metal ARM Proxmark lacks malloc()/free()
|
||||
void crypto1_create(struct Crypto1State *s, uint64_t key)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
for(i = 47;s && i > 0; i -= 2) {
|
||||
s->odd = s->odd << 1 | BIT(key, (i - 1) ^ 7);
|
||||
s->even = s->even << 1 | BIT(key, i ^ 7);
|
||||
}
|
||||
return;
|
||||
for(i = 47;s && i > 0; i -= 2) {
|
||||
s->odd = s->odd << 1 | BIT(key, (i - 1) ^ 7);
|
||||
s->even = s->even << 1 | BIT(key, i ^ 7);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void crypto1_destroy(struct Crypto1State *state)
|
||||
{
|
||||
state->odd = 0;
|
||||
state->even = 0;
|
||||
state->odd = 0;
|
||||
state->even = 0;
|
||||
}
|
||||
#else
|
||||
struct Crypto1State * crypto1_create(uint64_t key)
|
||||
{
|
||||
struct Crypto1State *s = malloc(sizeof(*s));
|
||||
if ( !s ) return NULL;
|
||||
struct Crypto1State *s = malloc(sizeof(*s));
|
||||
if ( !s ) return NULL;
|
||||
|
||||
s->odd = s->even = 0;
|
||||
s->odd = s->even = 0;
|
||||
|
||||
int i;
|
||||
for(i = 47; i > 0; i -= 2) {
|
||||
s->odd = s->odd << 1 | BIT(key, (i - 1) ^ 7);
|
||||
s->even = s->even << 1 | BIT(key, i ^ 7);
|
||||
}
|
||||
return s;
|
||||
int i;
|
||||
for(i = 47; i > 0; i -= 2) {
|
||||
s->odd = s->odd << 1 | BIT(key, (i - 1) ^ 7);
|
||||
s->even = s->even << 1 | BIT(key, i ^ 7);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
void crypto1_destroy(struct Crypto1State *state)
|
||||
{
|
||||
free(state);
|
||||
free(state);
|
||||
}
|
||||
#endif
|
||||
void crypto1_get_lfsr(struct Crypto1State *state, uint64_t *lfsr)
|
||||
{
|
||||
int i;
|
||||
for(*lfsr = 0, i = 23; i >= 0; --i) {
|
||||
*lfsr = *lfsr << 1 | BIT(state->odd, i ^ 3);
|
||||
*lfsr = *lfsr << 1 | BIT(state->even, i ^ 3);
|
||||
}
|
||||
int i;
|
||||
for(*lfsr = 0, i = 23; i >= 0; --i) {
|
||||
*lfsr = *lfsr << 1 | BIT(state->odd, i ^ 3);
|
||||
*lfsr = *lfsr << 1 | BIT(state->even, i ^ 3);
|
||||
}
|
||||
}
|
||||
uint8_t crypto1_bit(struct Crypto1State *s, uint8_t in, int is_encrypted)
|
||||
{
|
||||
uint32_t feedin, t;
|
||||
uint8_t ret = filter(s->odd);
|
||||
uint32_t feedin, t;
|
||||
uint8_t ret = filter(s->odd);
|
||||
|
||||
feedin = ret & !!is_encrypted;
|
||||
feedin ^= !!in;
|
||||
feedin ^= LF_POLY_ODD & s->odd;
|
||||
feedin ^= LF_POLY_EVEN & s->even;
|
||||
s->even = s->even << 1 | evenparity32(feedin);
|
||||
feedin = ret & !!is_encrypted;
|
||||
feedin ^= !!in;
|
||||
feedin ^= LF_POLY_ODD & s->odd;
|
||||
feedin ^= LF_POLY_EVEN & s->even;
|
||||
s->even = s->even << 1 | evenparity32(feedin);
|
||||
|
||||
t = s->odd;
|
||||
s->odd = s->even;
|
||||
s->even = t;
|
||||
t = s->odd;
|
||||
s->odd = s->even;
|
||||
s->even = t;
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
uint8_t crypto1_byte(struct Crypto1State *s, uint8_t in, int is_encrypted)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 0), is_encrypted) << 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 1), is_encrypted) << 1;
|
||||
ret |= crypto1_bit(s, BIT(in, 2), is_encrypted) << 2;
|
||||
ret |= crypto1_bit(s, BIT(in, 3), is_encrypted) << 3;
|
||||
ret |= crypto1_bit(s, BIT(in, 4), is_encrypted) << 4;
|
||||
ret |= crypto1_bit(s, BIT(in, 5), is_encrypted) << 5;
|
||||
ret |= crypto1_bit(s, BIT(in, 6), is_encrypted) << 6;
|
||||
ret |= crypto1_bit(s, BIT(in, 7), is_encrypted) << 7;
|
||||
return ret;
|
||||
uint8_t ret = 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 0), is_encrypted) << 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 1), is_encrypted) << 1;
|
||||
ret |= crypto1_bit(s, BIT(in, 2), is_encrypted) << 2;
|
||||
ret |= crypto1_bit(s, BIT(in, 3), is_encrypted) << 3;
|
||||
ret |= crypto1_bit(s, BIT(in, 4), is_encrypted) << 4;
|
||||
ret |= crypto1_bit(s, BIT(in, 5), is_encrypted) << 5;
|
||||
ret |= crypto1_bit(s, BIT(in, 6), is_encrypted) << 6;
|
||||
ret |= crypto1_bit(s, BIT(in, 7), is_encrypted) << 7;
|
||||
return ret;
|
||||
}
|
||||
uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted)
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
ret |= crypto1_bit(s, BEBIT(in, 0), is_encrypted) << (0 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 1), is_encrypted) << (1 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 2), is_encrypted) << (2 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 3), is_encrypted) << (3 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 4), is_encrypted) << (4 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 5), is_encrypted) << (5 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 6), is_encrypted) << (6 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 7), is_encrypted) << (7 ^ 24);
|
||||
uint32_t ret = 0;
|
||||
ret |= crypto1_bit(s, BEBIT(in, 0), is_encrypted) << (0 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 1), is_encrypted) << (1 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 2), is_encrypted) << (2 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 3), is_encrypted) << (3 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 4), is_encrypted) << (4 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 5), is_encrypted) << (5 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 6), is_encrypted) << (6 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 7), is_encrypted) << (7 ^ 24);
|
||||
|
||||
ret |= crypto1_bit(s, BEBIT(in, 8), is_encrypted) << (8 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 9), is_encrypted) << (9 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 10), is_encrypted) << (10 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 11), is_encrypted) << (11 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 12), is_encrypted) << (12 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 13), is_encrypted) << (13 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 14), is_encrypted) << (14 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 15), is_encrypted) << (15 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 8), is_encrypted) << (8 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 9), is_encrypted) << (9 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 10), is_encrypted) << (10 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 11), is_encrypted) << (11 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 12), is_encrypted) << (12 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 13), is_encrypted) << (13 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 14), is_encrypted) << (14 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 15), is_encrypted) << (15 ^ 24);
|
||||
|
||||
ret |= crypto1_bit(s, BEBIT(in, 16), is_encrypted) << (16 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 17), is_encrypted) << (17 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 18), is_encrypted) << (18 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 19), is_encrypted) << (19 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 20), is_encrypted) << (20 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 21), is_encrypted) << (21 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 22), is_encrypted) << (22 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 23), is_encrypted) << (23 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 16), is_encrypted) << (16 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 17), is_encrypted) << (17 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 18), is_encrypted) << (18 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 19), is_encrypted) << (19 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 20), is_encrypted) << (20 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 21), is_encrypted) << (21 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 22), is_encrypted) << (22 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 23), is_encrypted) << (23 ^ 24);
|
||||
|
||||
ret |= crypto1_bit(s, BEBIT(in, 24), is_encrypted) << (24 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 25), is_encrypted) << (25 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 26), is_encrypted) << (26 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 27), is_encrypted) << (27 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 28), is_encrypted) << (28 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 29), is_encrypted) << (29 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 30), is_encrypted) << (30 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 31), is_encrypted) << (31 ^ 24);
|
||||
return ret;
|
||||
ret |= crypto1_bit(s, BEBIT(in, 24), is_encrypted) << (24 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 25), is_encrypted) << (25 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 26), is_encrypted) << (26 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 27), is_encrypted) << (27 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 28), is_encrypted) << (28 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 29), is_encrypted) << (29 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 30), is_encrypted) << (30 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 31), is_encrypted) << (31 ^ 24);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* prng_successor
|
||||
@@ -145,9 +145,9 @@ uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted)
|
||||
*/
|
||||
uint32_t prng_successor(uint32_t x, uint32_t n)
|
||||
{
|
||||
SWAPENDIAN(x);
|
||||
while(n--)
|
||||
x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
|
||||
SWAPENDIAN(x);
|
||||
while(n--)
|
||||
x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
|
||||
|
||||
return SWAPENDIAN(x);
|
||||
return SWAPENDIAN(x);
|
||||
}
|
||||
|
||||
+73
-73
@@ -10,118 +10,118 @@
|
||||
#include "crc.h"
|
||||
|
||||
void crc_init_ref(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor, bool refin, bool refout) {
|
||||
crc_init(crc, order, polynom, initial_value, final_xor);
|
||||
crc->refin = refin;
|
||||
crc->refout = refout;
|
||||
crc_clear(crc);
|
||||
crc_init(crc, order, polynom, initial_value, final_xor);
|
||||
crc->refin = refin;
|
||||
crc->refout = refout;
|
||||
crc_clear(crc);
|
||||
}
|
||||
|
||||
void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor) {
|
||||
crc->order = order;
|
||||
crc->topbit = BITMASK( order-1 );
|
||||
crc->polynom = polynom;
|
||||
crc->initial_value = initial_value;
|
||||
crc->final_xor = final_xor;
|
||||
crc->mask = (1L<<order)-1;
|
||||
crc->refin = false;
|
||||
crc->refout = false;
|
||||
crc_clear(crc);
|
||||
crc->order = order;
|
||||
crc->topbit = BITMASK( order-1 );
|
||||
crc->polynom = polynom;
|
||||
crc->initial_value = initial_value;
|
||||
crc->final_xor = final_xor;
|
||||
crc->mask = (1L<<order)-1;
|
||||
crc->refin = false;
|
||||
crc->refout = false;
|
||||
crc_clear(crc);
|
||||
}
|
||||
|
||||
void crc_clear(crc_t *crc) {
|
||||
|
||||
crc->state = crc->initial_value & crc->mask;
|
||||
if (crc->refin)
|
||||
crc->state = reflect(crc->state, crc->order);
|
||||
crc->state = crc->initial_value & crc->mask;
|
||||
if (crc->refin)
|
||||
crc->state = reflect(crc->state, crc->order);
|
||||
}
|
||||
|
||||
void crc_update2(crc_t *crc, uint32_t data, int data_width){
|
||||
|
||||
if (crc->refin)
|
||||
data = reflect(data, data_width);
|
||||
if (crc->refin)
|
||||
data = reflect(data, data_width);
|
||||
|
||||
// Bring the next byte into the remainder.
|
||||
crc->state ^= data << (crc->order - data_width);
|
||||
// Bring the next byte into the remainder.
|
||||
crc->state ^= data << (crc->order - data_width);
|
||||
|
||||
for( uint8_t bit = data_width; bit > 0; --bit) {
|
||||
for( uint8_t bit = data_width; bit > 0; --bit) {
|
||||
|
||||
if (crc->state & crc->topbit)
|
||||
crc->state = (crc->state << 1) ^ crc->polynom;
|
||||
else
|
||||
crc->state = (crc->state << 1);
|
||||
}
|
||||
if (crc->state & crc->topbit)
|
||||
crc->state = (crc->state << 1) ^ crc->polynom;
|
||||
else
|
||||
crc->state = (crc->state << 1);
|
||||
}
|
||||
}
|
||||
|
||||
void crc_update(crc_t *crc, uint32_t data, int data_width)
|
||||
{
|
||||
if (crc->refin)
|
||||
data = reflect(data, data_width);
|
||||
if (crc->refin)
|
||||
data = reflect(data, data_width);
|
||||
|
||||
int i;
|
||||
for(i=0; i<data_width; i++) {
|
||||
int oldstate = crc->state;
|
||||
crc->state = crc->state >> 1;
|
||||
if( (oldstate^data) & 1 ) {
|
||||
crc->state ^= crc->polynom;
|
||||
}
|
||||
data >>= 1;
|
||||
}
|
||||
int i;
|
||||
for(i=0; i<data_width; i++) {
|
||||
int oldstate = crc->state;
|
||||
crc->state = crc->state >> 1;
|
||||
if( (oldstate^data) & 1 ) {
|
||||
crc->state ^= crc->polynom;
|
||||
}
|
||||
data >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t crc_finish(crc_t *crc) {
|
||||
uint32_t val = crc->state;
|
||||
if (crc->refout)
|
||||
val = reflect(val, crc->order);
|
||||
return ( val ^ crc->final_xor ) & crc->mask;
|
||||
uint32_t val = crc->state;
|
||||
if (crc->refout)
|
||||
val = reflect(val, crc->order);
|
||||
return ( val ^ crc->final_xor ) & crc->mask;
|
||||
}
|
||||
|
||||
/*
|
||||
static void print_crc(crc_t *crc) {
|
||||
printf(" Order %d\n Poly %x\n Init %x\n Final %x\n Mask %x\n topbit %x\n RefIn %s\n RefOut %s\n State %x\n",
|
||||
crc->order,
|
||||
crc->polynom,
|
||||
crc->initial_value,
|
||||
crc->final_xor,
|
||||
crc->mask,
|
||||
crc->topbit,
|
||||
(crc->refin) ? "TRUE":"FALSE",
|
||||
(crc->refout) ? "TRUE":"FALSE",
|
||||
crc->state
|
||||
);
|
||||
printf(" Order %d\n Poly %x\n Init %x\n Final %x\n Mask %x\n topbit %x\n RefIn %s\n RefOut %s\n State %x\n",
|
||||
crc->order,
|
||||
crc->polynom,
|
||||
crc->initial_value,
|
||||
crc->final_xor,
|
||||
crc->mask,
|
||||
crc->topbit,
|
||||
(crc->refin) ? "TRUE":"FALSE",
|
||||
(crc->refout) ? "TRUE":"FALSE",
|
||||
crc->state
|
||||
);
|
||||
}
|
||||
*/
|
||||
|
||||
// width=8 poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xA1 name="CRC-8/MAXIM"
|
||||
uint32_t CRC8Maxim(uint8_t *buff, size_t size) {
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 8, 0x31, 0, 0, true, true);
|
||||
for ( int i=0; i < size; ++i)
|
||||
crc_update2(&crc, buff[i], 8);
|
||||
return crc_finish(&crc);
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 8, 0x31, 0, 0, true, true);
|
||||
for ( int i=0; i < size; ++i)
|
||||
crc_update2(&crc, buff[i], 8);
|
||||
return crc_finish(&crc);
|
||||
}
|
||||
// width=8 poly=0x1d, init=0xc7 (0xe3 - WRONG! but it mentioned in MAD datasheet) refin=false refout=false xorout=0x00 name="CRC-8/MIFARE-MAD"
|
||||
uint32_t CRC8Mad(uint8_t *buff, size_t size) {
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 8, 0x1d, 0xc7, 0, false, false);
|
||||
for ( int i = 0; i < size; ++i)
|
||||
crc_update2(&crc, buff[i], 8);
|
||||
return crc_finish(&crc);
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 8, 0x1d, 0xc7, 0, false, false);
|
||||
for ( int i = 0; i < size; ++i)
|
||||
crc_update2(&crc, buff[i], 8);
|
||||
return crc_finish(&crc);
|
||||
}
|
||||
// width=4 poly=0xC, reversed poly=0x7 init=0x5 refin=true refout=true xorout=0x0000 check= name="CRC-4/LEGIC"
|
||||
uint32_t CRC4Legic(uint8_t *cmd, size_t size) {
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 4, 0x19 >> 1, 0x5, 0, true, true);
|
||||
crc_update2(&crc, 1, 1); /* CMD_READ */
|
||||
crc_update2(&crc, cmd[0], 8);
|
||||
crc_update2(&crc, cmd[1], 8);
|
||||
return reflect(crc_finish(&crc), 4);
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 4, 0x19 >> 1, 0x5, 0, true, true);
|
||||
crc_update2(&crc, 1, 1); /* CMD_READ */
|
||||
crc_update2(&crc, cmd[0], 8);
|
||||
crc_update2(&crc, cmd[1], 8);
|
||||
return reflect(crc_finish(&crc), 4);
|
||||
}
|
||||
// width=8 poly=0x63, reversed poly=0x8D init=0x55 refin=true refout=true xorout=0x0000 check=0xC6 name="CRC-8/LEGIC"
|
||||
// the CRC needs to be reversed before returned.
|
||||
uint32_t CRC8Legic(uint8_t *buff, size_t size) {
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 8, 0x63, 0x55, 0, true, true);
|
||||
for ( int i = 0; i < size; ++i)
|
||||
crc_update2(&crc, buff[i], 8);
|
||||
return reflect8(crc_finish(&crc));
|
||||
crc_t crc;
|
||||
crc_init_ref(&crc, 8, 0x63, 0x55, 0, true, true);
|
||||
for ( int i = 0; i < size; ++i)
|
||||
crc_update2(&crc, buff[i], 8);
|
||||
return reflect8(crc_finish(&crc));
|
||||
}
|
||||
+20
-20
@@ -9,19 +9,19 @@
|
||||
#ifndef __CRC_H
|
||||
#define __CRC_H
|
||||
|
||||
#include "common.h" //stdint, stddef, stdbool
|
||||
#include "util.h" // reflect, bswap_16
|
||||
#include "common.h" //stdint, stddef, stdbool
|
||||
#include "util.h" // reflect, bswap_16
|
||||
|
||||
typedef struct crc {
|
||||
uint32_t state;
|
||||
int order;
|
||||
uint32_t polynom;
|
||||
uint32_t initial_value;
|
||||
uint32_t final_xor;
|
||||
uint32_t mask;
|
||||
int topbit;
|
||||
bool refin; /* Parameter: Reflect input bytes? */
|
||||
bool refout; /* Parameter: Reflect output CRC? */
|
||||
uint32_t state;
|
||||
int order;
|
||||
uint32_t polynom;
|
||||
uint32_t initial_value;
|
||||
uint32_t final_xor;
|
||||
uint32_t mask;
|
||||
int topbit;
|
||||
bool refin; /* Parameter: Reflect input bytes? */
|
||||
bool refout; /* Parameter: Reflect output CRC? */
|
||||
} crc_t;
|
||||
|
||||
/* Initialize a crc structure. order is the order of the polynom, e.g. 32 for a CRC-32
|
||||
@@ -64,14 +64,14 @@ uint32_t CRC8Legic(uint8_t *buff, size_t size);
|
||||
|
||||
/* Static initialization of a crc structure */
|
||||
#define CRC_INITIALIZER(_order, _polynom, _initial_value, _final_xor) { \
|
||||
.state = ((_initial_value) & ((1L<<(_order))-1)), \
|
||||
.order = (_order), \
|
||||
.polynom = (_polynom), \
|
||||
.initial_value = (_initial_value), \
|
||||
.final_xor = (_final_xor), \
|
||||
.mask = ((1L<<(_order))-1) \
|
||||
.refin = false, \
|
||||
.refout = false \
|
||||
}
|
||||
.state = ((_initial_value) & ((1L<<(_order))-1)), \
|
||||
.order = (_order), \
|
||||
.polynom = (_polynom), \
|
||||
.initial_value = (_initial_value), \
|
||||
.final_xor = (_final_xor), \
|
||||
.mask = ((1L<<(_order))-1) \
|
||||
.refin = false, \
|
||||
.refout = false \
|
||||
}
|
||||
|
||||
#endif /* __CRC_H */
|
||||
|
||||
+126
-126
@@ -13,125 +13,125 @@ static CrcType_t crc_type = CRC_NONE;
|
||||
|
||||
void init_table(CrcType_t ct) {
|
||||
|
||||
// same crc algo, and initialised already
|
||||
if ( ct == crc_type && crc_table_init)
|
||||
return;
|
||||
// same crc algo, and initialised already
|
||||
if ( ct == crc_type && crc_table_init)
|
||||
return;
|
||||
|
||||
// not the same crc algo. reset table.
|
||||
if ( ct != crc_type)
|
||||
reset_table();
|
||||
// not the same crc algo. reset table.
|
||||
if ( ct != crc_type)
|
||||
reset_table();
|
||||
|
||||
crc_type = ct;
|
||||
crc_type = ct;
|
||||
|
||||
switch (ct) {
|
||||
case CRC_14443_A:
|
||||
case CRC_14443_B:
|
||||
case CRC_15693:
|
||||
case CRC_ICLASS: generate_table(CRC16_POLY_CCITT, true); break;
|
||||
case CRC_FELICA: generate_table(CRC16_POLY_CCITT, false); break;
|
||||
case CRC_LEGIC: generate_table(CRC16_POLY_LEGIC, true); break;
|
||||
case CRC_CCITT: generate_table(CRC16_POLY_CCITT, false); break;
|
||||
case CRC_KERMIT: generate_table(CRC16_POLY_CCITT, true); break;
|
||||
default:
|
||||
crc_table_init = false;
|
||||
crc_type = CRC_NONE;
|
||||
break;
|
||||
}
|
||||
switch (ct) {
|
||||
case CRC_14443_A:
|
||||
case CRC_14443_B:
|
||||
case CRC_15693:
|
||||
case CRC_ICLASS: generate_table(CRC16_POLY_CCITT, true); break;
|
||||
case CRC_FELICA: generate_table(CRC16_POLY_CCITT, false); break;
|
||||
case CRC_LEGIC: generate_table(CRC16_POLY_LEGIC, true); break;
|
||||
case CRC_CCITT: generate_table(CRC16_POLY_CCITT, false); break;
|
||||
case CRC_KERMIT: generate_table(CRC16_POLY_CCITT, true); break;
|
||||
default:
|
||||
crc_table_init = false;
|
||||
crc_type = CRC_NONE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void generate_table( uint16_t polynomial, bool refin) {
|
||||
|
||||
uint16_t i, j, crc, c;
|
||||
uint16_t i, j, crc, c;
|
||||
|
||||
for (i = 0; i < 256; i++) {
|
||||
crc = 0;
|
||||
if (refin)
|
||||
c = reflect8(i) << 8;
|
||||
else
|
||||
c = i << 8;
|
||||
for (i = 0; i < 256; i++) {
|
||||
crc = 0;
|
||||
if (refin)
|
||||
c = reflect8(i) << 8;
|
||||
else
|
||||
c = i << 8;
|
||||
|
||||
for (j = 0; j < 8; j++) {
|
||||
|
||||
if ( (crc ^ c) & 0x8000 )
|
||||
crc = ( crc << 1 ) ^ polynomial;
|
||||
crc = ( crc << 1 ) ^ polynomial;
|
||||
else
|
||||
crc = crc << 1;
|
||||
crc = crc << 1;
|
||||
|
||||
c = c << 1;
|
||||
}
|
||||
if (refin)
|
||||
crc = reflect16(crc);
|
||||
if (refin)
|
||||
crc = reflect16(crc);
|
||||
|
||||
crc_table[i] = crc;
|
||||
}
|
||||
crc_table[i] = crc;
|
||||
}
|
||||
crc_table_init = true;
|
||||
}
|
||||
|
||||
void reset_table(void) {
|
||||
memset(crc_table, 0, sizeof(crc_table));
|
||||
crc_table_init = false;
|
||||
crc_type = CRC_NONE;
|
||||
memset(crc_table, 0, sizeof(crc_table));
|
||||
crc_table_init = false;
|
||||
crc_type = CRC_NONE;
|
||||
}
|
||||
|
||||
// table lookup LUT solution
|
||||
uint16_t crc16_fast(uint8_t const *d, size_t n, uint16_t initval, bool refin, bool refout) {
|
||||
|
||||
// fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
|
||||
// only usable with polynom orders of 8, 16, 24 or 32.
|
||||
if (n == 0)
|
||||
// fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
|
||||
// only usable with polynom orders of 8, 16, 24 or 32.
|
||||
if (n == 0)
|
||||
return (~initval);
|
||||
|
||||
uint16_t crc = initval;
|
||||
uint16_t crc = initval;
|
||||
|
||||
if (refin)
|
||||
crc = reflect16(crc);
|
||||
if (refin)
|
||||
crc = reflect16(crc);
|
||||
|
||||
if (!refin)
|
||||
while (n--) crc = (crc << 8) ^ crc_table[ ((crc >> 8) ^ *d++) & 0xFF ];
|
||||
else
|
||||
while (n--) crc = (crc >> 8) ^ crc_table[ (crc & 0xFF) ^ *d++];
|
||||
if (!refin)
|
||||
while (n--) crc = (crc << 8) ^ crc_table[ ((crc >> 8) ^ *d++) & 0xFF ];
|
||||
else
|
||||
while (n--) crc = (crc >> 8) ^ crc_table[ (crc & 0xFF) ^ *d++];
|
||||
|
||||
if (refout^refin)
|
||||
crc = reflect16(crc);
|
||||
if (refout^refin)
|
||||
crc = reflect16(crc);
|
||||
|
||||
return crc;
|
||||
return crc;
|
||||
}
|
||||
|
||||
// bit looped solution TODO REMOVED
|
||||
uint16_t update_crc16_ex( uint16_t crc, uint8_t c, uint16_t polynomial ) {
|
||||
uint16_t i, v, tmp = 0;
|
||||
uint16_t i, v, tmp = 0;
|
||||
|
||||
v = (crc ^ c) & 0xff;
|
||||
v = (crc ^ c) & 0xff;
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
for (i = 0; i < 8; i++) {
|
||||
|
||||
if ( (tmp ^ v) & 1 )
|
||||
tmp = ( tmp >> 1 ) ^ polynomial;
|
||||
else
|
||||
tmp >>= 1;
|
||||
if ( (tmp ^ v) & 1 )
|
||||
tmp = ( tmp >> 1 ) ^ polynomial;
|
||||
else
|
||||
tmp >>= 1;
|
||||
|
||||
v >>= 1;
|
||||
}
|
||||
return ((crc >> 8) ^ tmp) & 0xffff;
|
||||
v >>= 1;
|
||||
}
|
||||
return ((crc >> 8) ^ tmp) & 0xffff;
|
||||
}
|
||||
uint16_t update_crc16( uint16_t crc, uint8_t c ) {
|
||||
return update_crc16_ex( crc, c, CRC16_POLY_CCITT);
|
||||
return update_crc16_ex( crc, c, CRC16_POLY_CCITT);
|
||||
}
|
||||
|
||||
// two ways. msb or lsb loop.
|
||||
uint16_t crc16(uint8_t const *d, size_t length, uint16_t remainder, uint16_t polynomial, bool refin, bool refout) {
|
||||
if (length == 0)
|
||||
if (length == 0)
|
||||
return (~remainder);
|
||||
|
||||
uint8_t c;
|
||||
uint8_t c;
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
c = d[i];
|
||||
if (refin) c = reflect8(c);
|
||||
c = d[i];
|
||||
if (refin) c = reflect8(c);
|
||||
|
||||
// xor in at msb
|
||||
// xor in at msb
|
||||
remainder ^= (c << 8);
|
||||
|
||||
// 8 iteration loop
|
||||
// 8 iteration loop
|
||||
for (uint8_t j = 8; j; --j) {
|
||||
if (remainder & 0x8000) {
|
||||
remainder = (remainder << 1) ^ polynomial;
|
||||
@@ -140,58 +140,58 @@ uint16_t crc16(uint8_t const *d, size_t length, uint16_t remainder, uint16_t pol
|
||||
}
|
||||
}
|
||||
}
|
||||
if (refout)
|
||||
remainder = reflect16(remainder);
|
||||
if (refout)
|
||||
remainder = reflect16(remainder);
|
||||
|
||||
return remainder;
|
||||
}
|
||||
|
||||
void compute_crc(CrcType_t ct, const uint8_t *d, size_t n, uint8_t *first, uint8_t *second) {
|
||||
|
||||
// can't calc a crc on less than 1 byte
|
||||
if ( n == 0 ) return;
|
||||
// can't calc a crc on less than 1 byte
|
||||
if ( n == 0 ) return;
|
||||
|
||||
init_table(ct);
|
||||
init_table(ct);
|
||||
|
||||
uint16_t crc = 0;
|
||||
switch (ct) {
|
||||
case CRC_14443_A: crc = crc16_a(d, n); break;
|
||||
case CRC_14443_B:
|
||||
case CRC_15693: crc = crc16_x25(d, n); break;
|
||||
case CRC_ICLASS: crc = crc16_iclass(d, n); break;
|
||||
case CRC_FELICA:crc = crc16_xmodem(d, n); break;
|
||||
//case CRC_LEGIC:
|
||||
case CRC_CCITT: crc = crc16_ccitt(d, n); break;
|
||||
case CRC_KERMIT: crc = crc16_kermit(d, n); break;
|
||||
default: break;
|
||||
}
|
||||
*first = (crc & 0xFF);
|
||||
uint16_t crc = 0;
|
||||
switch (ct) {
|
||||
case CRC_14443_A: crc = crc16_a(d, n); break;
|
||||
case CRC_14443_B:
|
||||
case CRC_15693: crc = crc16_x25(d, n); break;
|
||||
case CRC_ICLASS: crc = crc16_iclass(d, n); break;
|
||||
case CRC_FELICA:crc = crc16_xmodem(d, n); break;
|
||||
//case CRC_LEGIC:
|
||||
case CRC_CCITT: crc = crc16_ccitt(d, n); break;
|
||||
case CRC_KERMIT: crc = crc16_kermit(d, n); break;
|
||||
default: break;
|
||||
}
|
||||
*first = (crc & 0xFF);
|
||||
*second = ((crc >> 8) & 0xFF);
|
||||
}
|
||||
uint16_t crc(CrcType_t ct, const uint8_t *d, size_t n) {
|
||||
|
||||
// can't calc a crc on less than 3 byte. (1byte + 2 crc bytes)
|
||||
if ( n < 3 ) return 0;
|
||||
// can't calc a crc on less than 3 byte. (1byte + 2 crc bytes)
|
||||
if ( n < 3 ) return 0;
|
||||
|
||||
init_table(ct);
|
||||
switch (ct) {
|
||||
case CRC_14443_A: return crc16_a(d, n);
|
||||
case CRC_14443_B:
|
||||
case CRC_15693: return crc16_x25(d, n);
|
||||
case CRC_ICLASS: return crc16_iclass(d, n);
|
||||
case CRC_FELICA: return crc16_xmodem(d, n);
|
||||
//case CRC_LEGIC:
|
||||
case CRC_CCITT: return crc16_ccitt(d, n);
|
||||
case CRC_KERMIT: return crc16_kermit(d, n);
|
||||
default: break;
|
||||
}
|
||||
return 0;
|
||||
init_table(ct);
|
||||
switch (ct) {
|
||||
case CRC_14443_A: return crc16_a(d, n);
|
||||
case CRC_14443_B:
|
||||
case CRC_15693: return crc16_x25(d, n);
|
||||
case CRC_ICLASS: return crc16_iclass(d, n);
|
||||
case CRC_FELICA: return crc16_xmodem(d, n);
|
||||
//case CRC_LEGIC:
|
||||
case CRC_CCITT: return crc16_ccitt(d, n);
|
||||
case CRC_KERMIT: return crc16_kermit(d, n);
|
||||
default: break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// check CRC
|
||||
// ct crc type
|
||||
// d buffer with data
|
||||
// n length (including crc)
|
||||
// d buffer with data
|
||||
// n length (including crc)
|
||||
//
|
||||
// This function uses the message + crc bytes in order to compare the "residue" afterwards.
|
||||
// crc16 algos like CRC-A become 0x000
|
||||
@@ -199,39 +199,39 @@ uint16_t crc(CrcType_t ct, const uint8_t *d, size_t n) {
|
||||
// If calculated with crc bytes, the residue should be 0xF0B8
|
||||
bool check_crc(CrcType_t ct, const uint8_t *d, size_t n) {
|
||||
|
||||
// can't calc a crc on less than 3 byte. (1byte + 2 crc bytes)
|
||||
if ( n < 3 ) return false;
|
||||
// can't calc a crc on less than 3 byte. (1byte + 2 crc bytes)
|
||||
if ( n < 3 ) return false;
|
||||
|
||||
init_table(ct);
|
||||
init_table(ct);
|
||||
|
||||
switch (ct) {
|
||||
case CRC_14443_A: return (crc16_a(d, n) == 0);
|
||||
case CRC_14443_B: return (crc16_x25(d, n) == X25_CRC_CHECK);
|
||||
case CRC_15693: return (crc16_x25(d, n) == X25_CRC_CHECK);
|
||||
case CRC_ICLASS: return (crc16_iclass(d, n) == 0);
|
||||
case CRC_FELICA: return (crc16_xmodem(d, n) == 0);
|
||||
//case CRC_LEGIC:
|
||||
case CRC_CCITT: return (crc16_ccitt(d, n) == 0);
|
||||
default: break;
|
||||
}
|
||||
return false;
|
||||
switch (ct) {
|
||||
case CRC_14443_A: return (crc16_a(d, n) == 0);
|
||||
case CRC_14443_B: return (crc16_x25(d, n) == X25_CRC_CHECK);
|
||||
case CRC_15693: return (crc16_x25(d, n) == X25_CRC_CHECK);
|
||||
case CRC_ICLASS: return (crc16_iclass(d, n) == 0);
|
||||
case CRC_FELICA: return (crc16_xmodem(d, n) == 0);
|
||||
//case CRC_LEGIC:
|
||||
case CRC_CCITT: return (crc16_ccitt(d, n) == 0);
|
||||
default: break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1 residue=0x0000 name="CRC-16/CCITT-FALSE"
|
||||
uint16_t crc16_ccitt(uint8_t const *d, size_t n) {
|
||||
return crc16_fast(d, n, 0xffff, false, false);
|
||||
return crc16_fast(d, n, 0xffff, false, false);
|
||||
}
|
||||
|
||||
// FDX-B ISO11784/85) uses KERMIT
|
||||
// poly=0x1021 init=0x0000 refin=true refout=true xorout=0x0000 name="KERMIT"
|
||||
uint16_t crc16_kermit(uint8_t const *d, size_t n) {
|
||||
return crc16_fast(d, n, 0x0000, true, true);
|
||||
return crc16_fast(d, n, 0x0000, true, true);
|
||||
}
|
||||
|
||||
// FeliCa uses XMODEM
|
||||
// poly=0x1021 init=0x0000 refin=false refout=false xorout=0x0000 name="XMODEM"
|
||||
uint16_t crc16_xmodem(uint8_t const *d, size_t n) {
|
||||
return crc16_fast(d, n, 0x0000, false, false);
|
||||
return crc16_fast(d, n, 0x0000, false, false);
|
||||
}
|
||||
|
||||
// Following standards uses X-25
|
||||
@@ -240,14 +240,14 @@ uint16_t crc16_xmodem(uint8_t const *d, size_t n) {
|
||||
// ISO/IEC 13239 (formerly ISO/IEC 3309)
|
||||
// poly=0x1021 init=0xffff refin=true refout=true xorout=0xffff name="X-25"
|
||||
uint16_t crc16_x25(uint8_t const *d, size_t n) {
|
||||
uint16_t crc = crc16_fast(d, n, 0xffff, true, true);
|
||||
crc = ~crc;
|
||||
return crc;
|
||||
uint16_t crc = crc16_fast(d, n, 0xffff, true, true);
|
||||
crc = ~crc;
|
||||
return crc;
|
||||
}
|
||||
// CRC-A (14443-3)
|
||||
// poly=0x1021 init=0xc6c6 refin=true refout=true xorout=0x0000 name="CRC-A"
|
||||
uint16_t crc16_a(uint8_t const *d, size_t n) {
|
||||
return crc16_fast(d, n, 0xC6C6, true, true);
|
||||
return crc16_fast(d, n, 0xC6C6, true, true);
|
||||
}
|
||||
|
||||
// iClass crc
|
||||
@@ -255,12 +255,12 @@ uint16_t crc16_a(uint8_t const *d, size_t n) {
|
||||
// poly 0x1021 reflected 0x8408
|
||||
// poly=0x1021 init=0x4807 refin=true refout=true xorout=0x0BC3 check=0xF0B8 name="CRC-16/ICLASS"
|
||||
uint16_t crc16_iclass(uint8_t const *d, size_t n) {
|
||||
return crc16_fast(d, n, 0x4807, true, true);
|
||||
return crc16_fast(d, n, 0x4807, true, true);
|
||||
}
|
||||
|
||||
// This CRC-16 is used in Legic Advant systems.
|
||||
// poly=0xB400, init=depends refin=true refout=true xorout=0x0000 check= name="CRC-16/LEGIC"
|
||||
uint16_t crc16_legic(uint8_t const *d, size_t n, uint8_t uidcrc) {
|
||||
uint16_t initial = uidcrc << 8 | uidcrc;
|
||||
return crc16_fast(d, n, initial, true, true);
|
||||
uint16_t initial = uidcrc << 8 | uidcrc;
|
||||
return crc16_fast(d, n, initial, true, true);
|
||||
}
|
||||
|
||||
+11
-11
@@ -14,20 +14,20 @@
|
||||
|
||||
#define CRC16_POLY_CCITT 0x1021
|
||||
#define CRC16_POLY_LEGIC 0xc6c6 //0x6363
|
||||
#define CRC16_POLY_DNP 0x3d65
|
||||
#define CRC16_POLY_DNP 0x3d65
|
||||
|
||||
#define X25_CRC_CHECK ((uint16_t)(~0xF0B8 & 0xFFFF)) // use this for checking of a correct crc
|
||||
#define X25_CRC_CHECK ((uint16_t)(~0xF0B8 & 0xFFFF)) // use this for checking of a correct crc
|
||||
|
||||
typedef enum {
|
||||
CRC_NONE,
|
||||
CRC_14443_A,
|
||||
CRC_14443_B,
|
||||
CRC_15693,
|
||||
CRC_ICLASS,
|
||||
CRC_FELICA,
|
||||
CRC_LEGIC,
|
||||
CRC_CCITT,
|
||||
CRC_KERMIT,
|
||||
CRC_NONE,
|
||||
CRC_14443_A,
|
||||
CRC_14443_B,
|
||||
CRC_15693,
|
||||
CRC_ICLASS,
|
||||
CRC_FELICA,
|
||||
CRC_LEGIC,
|
||||
CRC_CCITT,
|
||||
CRC_KERMIT,
|
||||
} CrcType_t;
|
||||
|
||||
uint16_t update_crc16_ex( uint16_t crc, uint8_t c, uint16_t polynomial );
|
||||
|
||||
+6
-6
@@ -6,7 +6,7 @@
|
||||
#define CRC64_ECMA_PRESET 0x0000000000000000
|
||||
|
||||
const uint64_t crc64_table[] = {
|
||||
0x0000000000000000, 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26, 0xC711223CFA3E5BB5,
|
||||
0x0000000000000000, 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26, 0xC711223CFA3E5BB5,
|
||||
0x493366450E42ECDF, 0x0BC387AEA7A8DA4C, 0xCCD2A5925D9681F9, 0x8E224479F47CB76A,
|
||||
0x9266CC8A1C85D9BE, 0xD0962D61B56FEF2D, 0x17870F5D4F51B498, 0x5577EEB6E6BB820B,
|
||||
0xDB55AACF12C73561, 0x99A54B24BB2D03F2, 0x5EB4691841135847, 0x1C4488F3E8F96ED4,
|
||||
@@ -74,10 +74,10 @@ const uint64_t crc64_table[] = {
|
||||
|
||||
void crc64 (const uint8_t *data, const size_t len, uint64_t *crc) {
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
uint8_t tableIndex = (((uint8_t)(*crc >> 56)) ^ data[i]) & 0xff;
|
||||
*crc = crc64_table[tableIndex] ^ (*crc << 8);
|
||||
}
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
uint8_t tableIndex = (((uint8_t)(*crc >> 56)) ^ data[i]) & 0xff;
|
||||
*crc = crc64_table[tableIndex] ^ (*crc << 8);
|
||||
}
|
||||
}
|
||||
|
||||
//suint8_t x = (c & 0xFF00000000000000 ) >> 56;
|
||||
//suint8_t x = (c & 0xFF00000000000000 ) >> 56;
|
||||
@@ -1,9 +1,9 @@
|
||||
#include "proxmark3.h"
|
||||
/* This is the default version.c file that Makefile.common falls back to if perl is not available */
|
||||
const struct version_information __attribute__((section(".version_information"))) version_information = {
|
||||
VERSION_INFORMATION_MAGIC,
|
||||
1, /* version 1 */
|
||||
0, /* version information not present */
|
||||
2, /* cleanliness couldn't be determined */
|
||||
/* Remaining fields: zero */
|
||||
VERSION_INFORMATION_MAGIC,
|
||||
1, /* version 1 */
|
||||
0, /* version information not present */
|
||||
2, /* cleanliness couldn't be determined */
|
||||
/* Remaining fields: zero */
|
||||
};
|
||||
|
||||
+66
-66
@@ -55,15 +55,15 @@ typedef enum {
|
||||
|
||||
|
||||
enum DESFIRE_AUTH_SCHEME {
|
||||
AS_LEGACY,
|
||||
AS_NEW
|
||||
AS_LEGACY,
|
||||
AS_NEW
|
||||
};
|
||||
|
||||
enum DESFIRE_CRYPTOALGO {
|
||||
T_DES = 0x00,
|
||||
T_3DES = 0x01,
|
||||
T_3K3DES = 0x02,
|
||||
T_AES = 0x03
|
||||
T_DES = 0x00,
|
||||
T_3DES = 0x01,
|
||||
T_3K3DES = 0x02,
|
||||
T_AES = 0x03
|
||||
};
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ struct desfire_key {
|
||||
// DES_key_schedule ks1;
|
||||
// DES_key_schedule ks2;
|
||||
// DES_key_schedule ks3;
|
||||
AesCtx aes_ks;
|
||||
AesCtx aes_ks;
|
||||
uint8_t cmac_sk1[24];
|
||||
uint8_t cmac_sk2[24];
|
||||
uint8_t aes_version;
|
||||
@@ -89,10 +89,10 @@ struct desfire_tag {
|
||||
uint8_t last_internal_error;
|
||||
uint8_t last_pcd_error;
|
||||
desfirekey_t session_key;
|
||||
enum DESFIRE_AUTH_SCHEME authentication_scheme;
|
||||
enum DESFIRE_AUTH_SCHEME authentication_scheme;
|
||||
uint8_t authenticated_key_no;
|
||||
|
||||
uint8_t ivect[MAX_CRYPTO_BLOCK_SIZE];
|
||||
uint8_t ivect[MAX_CRYPTO_BLOCK_SIZE];
|
||||
uint8_t cmac[16];
|
||||
uint8_t *crypto_buffer;
|
||||
size_t crypto_buffer_size;
|
||||
@@ -111,66 +111,66 @@ enum DESFIRE_FILE_TYPES {
|
||||
};
|
||||
|
||||
enum DESFIRE_STATUS {
|
||||
OPERATION_OK = 0x00,
|
||||
NO_CHANGES = 0x0c,
|
||||
OUT_OF_EEPROM_ERROR = 0x0e,
|
||||
ILLEGAL_COMMAND_CODE = 0x1c,
|
||||
INTEGRITY_ERROR = 0x1e,
|
||||
NO_SUCH_KEY = 0x40,
|
||||
LENGTH_ERROR = 0x7e,
|
||||
PERMISSION_DENIED = 0x9d,
|
||||
PARAMETER_ERROR = 0x9e,
|
||||
APPLICATION_NOT_FOUND = 0xa0,
|
||||
APPL_INTEGRITY_ERROR = 0xa1,
|
||||
AUTHENTICATION_ERROR = 0xae,
|
||||
ADDITIONAL_FRAME = 0xaf,
|
||||
BOUNDARY_ERROR = 0xbe,
|
||||
PICC_INTEGRITY_ERROR = 0xc1,
|
||||
COMMAND_ABORTED = 0xca,
|
||||
PICC_DISABLED_ERROR = 0xcd,
|
||||
COUNT_ERROR = 0xce,
|
||||
DUPLICATE_ERROR = 0xde,
|
||||
EEPROM_ERROR = 0xee,
|
||||
FILE_NOT_FOUND = 0xf0,
|
||||
FILE_INTEGRITY_ERROR = 0xf1
|
||||
OPERATION_OK = 0x00,
|
||||
NO_CHANGES = 0x0c,
|
||||
OUT_OF_EEPROM_ERROR = 0x0e,
|
||||
ILLEGAL_COMMAND_CODE = 0x1c,
|
||||
INTEGRITY_ERROR = 0x1e,
|
||||
NO_SUCH_KEY = 0x40,
|
||||
LENGTH_ERROR = 0x7e,
|
||||
PERMISSION_DENIED = 0x9d,
|
||||
PARAMETER_ERROR = 0x9e,
|
||||
APPLICATION_NOT_FOUND = 0xa0,
|
||||
APPL_INTEGRITY_ERROR = 0xa1,
|
||||
AUTHENTICATION_ERROR = 0xae,
|
||||
ADDITIONAL_FRAME = 0xaf,
|
||||
BOUNDARY_ERROR = 0xbe,
|
||||
PICC_INTEGRITY_ERROR = 0xc1,
|
||||
COMMAND_ABORTED = 0xca,
|
||||
PICC_DISABLED_ERROR = 0xcd,
|
||||
COUNT_ERROR = 0xce,
|
||||
DUPLICATE_ERROR = 0xde,
|
||||
EEPROM_ERROR = 0xee,
|
||||
FILE_NOT_FOUND = 0xf0,
|
||||
FILE_INTEGRITY_ERROR = 0xf1
|
||||
};
|
||||
|
||||
enum DESFIRE_CMD {
|
||||
CREATE_APPLICATION = 0xca,
|
||||
DELETE_APPLICATION = 0xda,
|
||||
GET_APPLICATION_IDS = 0x6a,
|
||||
SELECT_APPLICATION = 0x5a,
|
||||
FORMAT_PICC = 0xfc,
|
||||
GET_VERSION = 0x60,
|
||||
READ_DATA = 0xbd,
|
||||
WRITE_DATA = 0x3d,
|
||||
GET_VALUE = 0x6c,
|
||||
CREDIT = 0x0c,
|
||||
DEBIT = 0xdc,
|
||||
LIMITED_CREDIT = 0x1c,
|
||||
WRITE_RECORD = 0x3b,
|
||||
READ_RECORDS = 0xbb,
|
||||
CLEAR_RECORD_FILE = 0xeb,
|
||||
COMMIT_TRANSACTION = 0xc7,
|
||||
ABORT_TRANSACTION = 0xa7,
|
||||
GET_FREE_MEMORY = 0x6e,
|
||||
GET_FILE_IDS = 0x6f,
|
||||
GET_FILE_SETTINGS = 0xf5,
|
||||
CHANGE_FILE_SETTINGS = 0x5f,
|
||||
CREATE_STD_DATA_FILE = 0xcd,
|
||||
CREATE_BACKUP_DATA_FILE = 0xcb,
|
||||
CREATE_VALUE_FILE = 0xcc,
|
||||
CREATE_LINEAR_RECORD_FILE = 0xc1,
|
||||
CREATE_CYCLIC_RECORD_FILE = 0xc0,
|
||||
DELETE_FILE = 0xdf,
|
||||
AUTHENTICATE = 0x0a, // AUTHENTICATE_NATIVE
|
||||
AUTHENTICATE_ISO = 0x1a, // AUTHENTICATE_STANDARD
|
||||
AUTHENTICATE_AES = 0xaa,
|
||||
CHANGE_KEY_SETTINGS = 0x54,
|
||||
GET_KEY_SETTINGS = 0x45,
|
||||
CHANGE_KEY = 0xc4,
|
||||
GET_KEY_VERSION = 0x64,
|
||||
AUTHENTICATION_FRAME = 0xAF
|
||||
CREATE_APPLICATION = 0xca,
|
||||
DELETE_APPLICATION = 0xda,
|
||||
GET_APPLICATION_IDS = 0x6a,
|
||||
SELECT_APPLICATION = 0x5a,
|
||||
FORMAT_PICC = 0xfc,
|
||||
GET_VERSION = 0x60,
|
||||
READ_DATA = 0xbd,
|
||||
WRITE_DATA = 0x3d,
|
||||
GET_VALUE = 0x6c,
|
||||
CREDIT = 0x0c,
|
||||
DEBIT = 0xdc,
|
||||
LIMITED_CREDIT = 0x1c,
|
||||
WRITE_RECORD = 0x3b,
|
||||
READ_RECORDS = 0xbb,
|
||||
CLEAR_RECORD_FILE = 0xeb,
|
||||
COMMIT_TRANSACTION = 0xc7,
|
||||
ABORT_TRANSACTION = 0xa7,
|
||||
GET_FREE_MEMORY = 0x6e,
|
||||
GET_FILE_IDS = 0x6f,
|
||||
GET_FILE_SETTINGS = 0xf5,
|
||||
CHANGE_FILE_SETTINGS = 0x5f,
|
||||
CREATE_STD_DATA_FILE = 0xcd,
|
||||
CREATE_BACKUP_DATA_FILE = 0xcb,
|
||||
CREATE_VALUE_FILE = 0xcc,
|
||||
CREATE_LINEAR_RECORD_FILE = 0xc1,
|
||||
CREATE_CYCLIC_RECORD_FILE = 0xc0,
|
||||
DELETE_FILE = 0xdf,
|
||||
AUTHENTICATE = 0x0a, // AUTHENTICATE_NATIVE
|
||||
AUTHENTICATE_ISO = 0x1a, // AUTHENTICATE_STANDARD
|
||||
AUTHENTICATE_AES = 0xaa,
|
||||
CHANGE_KEY_SETTINGS = 0x54,
|
||||
GET_KEY_SETTINGS = 0x45,
|
||||
CHANGE_KEY = 0xc4,
|
||||
GET_KEY_VERSION = 0x64,
|
||||
AUTHENTICATION_FRAME = 0xAF
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+528
-528
File diff suppressed because it is too large
Load Diff
+9
-9
@@ -8,16 +8,16 @@
|
||||
#include "BigBuf.h"
|
||||
#include "mifare.h"
|
||||
|
||||
#define I2C_DEVICE_ADDRESS_BOOT 0xB0
|
||||
#define I2C_DEVICE_ADDRESS_MAIN 0xC0
|
||||
#define I2C_DEVICE_ADDRESS_BOOT 0xB0
|
||||
#define I2C_DEVICE_ADDRESS_MAIN 0xC0
|
||||
|
||||
#define I2C_DEVICE_CMD_GENERATE_ATR 0x01
|
||||
#define I2C_DEVICE_CMD_SEND 0x02
|
||||
#define I2C_DEVICE_CMD_READ 0x03
|
||||
#define I2C_DEVICE_CMD_SETBAUD 0x04
|
||||
#define I2C_DEVICE_CMD_SIM_CLC 0x05
|
||||
#define I2C_DEVICE_CMD_GETVERSION 0x06
|
||||
#define I2C_DEVICE_CMD_SEND_T0 0x07
|
||||
#define I2C_DEVICE_CMD_GENERATE_ATR 0x01
|
||||
#define I2C_DEVICE_CMD_SEND 0x02
|
||||
#define I2C_DEVICE_CMD_READ 0x03
|
||||
#define I2C_DEVICE_CMD_SETBAUD 0x04
|
||||
#define I2C_DEVICE_CMD_SIM_CLC 0x05
|
||||
#define I2C_DEVICE_CMD_GETVERSION 0x06
|
||||
#define I2C_DEVICE_CMD_SEND_T0 0x07
|
||||
|
||||
|
||||
void I2C_recovery(void);
|
||||
|
||||
@@ -22,7 +22,7 @@ void ComputeCrc14443(uint16_t CrcType, const uint8_t *data, int length,
|
||||
uint8_t b;
|
||||
uint16_t crc = CrcType;
|
||||
|
||||
do {
|
||||
do {
|
||||
b = *data++;
|
||||
UpdateCrc14443(b, &crc);
|
||||
} while (--length);
|
||||
@@ -36,10 +36,10 @@ void ComputeCrc14443(uint16_t CrcType, const uint8_t *data, int length,
|
||||
}
|
||||
|
||||
bool CheckCrc14443(uint16_t CrcType, const uint8_t *data, int length) {
|
||||
if (length < 3) return false;
|
||||
uint8_t b1, b2;
|
||||
ComputeCrc14443(CrcType, data, length - 2, &b1, &b2);
|
||||
if ((b1 == data[length - 2]) && (b2 == data[length - 1]))
|
||||
return true;
|
||||
return false;
|
||||
if (length < 3) return false;
|
||||
uint8_t b1, b2;
|
||||
ComputeCrc14443(CrcType, data, length - 2, &b1, &b2);
|
||||
if ((b1 == data[length - 2]) && (b2 == data[length - 1]))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
+11
-11
@@ -9,17 +9,17 @@
|
||||
|
||||
// returns a string representation of the UID
|
||||
// UID is transmitted and stored LSB first, displayed MSB first
|
||||
// target char* buffer, where to put the UID, if NULL a static buffer is returned
|
||||
// uid[] the UID in transmission order
|
||||
// return: ptr to string
|
||||
// target char* buffer, where to put the UID, if NULL a static buffer is returned
|
||||
// uid[] the UID in transmission order
|
||||
// return: ptr to string
|
||||
char* Iso15693sprintUID(char *target, uint8_t *uid) {
|
||||
|
||||
static char tempbuf[2*8+1] = {0};
|
||||
if (target == NULL)
|
||||
target = tempbuf;
|
||||
sprintf(target, "%02X %02X %02X %02X %02X %02X %02X %02X",
|
||||
uid[7], uid[6], uid[5], uid[4],
|
||||
uid[3], uid[2], uid[1], uid[0]
|
||||
);
|
||||
return target;
|
||||
static char tempbuf[2*8+1] = {0};
|
||||
if (target == NULL)
|
||||
target = tempbuf;
|
||||
sprintf(target, "%02X %02X %02X %02X %02X %02X %02X %02X",
|
||||
uid[7], uid[6], uid[5], uid[4],
|
||||
uid[3], uid[2], uid[1], uid[0]
|
||||
);
|
||||
return target;
|
||||
}
|
||||
+83
-83
@@ -17,58 +17,58 @@
|
||||
#include "crc16.h"
|
||||
|
||||
// REQUEST FLAGS
|
||||
#define ISO15_REQ_SUBCARRIER_SINGLE 0x00 // Tag should respond using one subcarrier (ASK)
|
||||
#define ISO15_REQ_SUBCARRIER_TWO 0x01 // Tag should respond using two subcarriers (FSK)
|
||||
#define ISO15_REQ_DATARATE_LOW 0x00 // Tag should respond using low data rate
|
||||
#define ISO15_REQ_DATARATE_HIGH 0x02 // Tag should respond using high data rate
|
||||
#define ISO15_REQ_NONINVENTORY 0x00
|
||||
#define ISO15_REQ_INVENTORY 0x04 // This is an inventory request - see inventory flags
|
||||
#define ISO15_REQ_PROTOCOL_NONEXT 0x00
|
||||
#define ISO15_REQ_PROTOCOL_EXT 0x08 // RFU
|
||||
#define ISO15_REQ_SUBCARRIER_SINGLE 0x00 // Tag should respond using one subcarrier (ASK)
|
||||
#define ISO15_REQ_SUBCARRIER_TWO 0x01 // Tag should respond using two subcarriers (FSK)
|
||||
#define ISO15_REQ_DATARATE_LOW 0x00 // Tag should respond using low data rate
|
||||
#define ISO15_REQ_DATARATE_HIGH 0x02 // Tag should respond using high data rate
|
||||
#define ISO15_REQ_NONINVENTORY 0x00
|
||||
#define ISO15_REQ_INVENTORY 0x04 // This is an inventory request - see inventory flags
|
||||
#define ISO15_REQ_PROTOCOL_NONEXT 0x00
|
||||
#define ISO15_REQ_PROTOCOL_EXT 0x08 // RFU
|
||||
|
||||
// REQUEST FLAGS when INVENTORY is not set
|
||||
#define ISO15_REQ_SELECT 0x10 // only selected cards response
|
||||
#define ISO15_REQ_ADDRESS 0x20 // this req contains an address
|
||||
#define ISO15_REQ_OPTION 0x40 // Command specific option selector
|
||||
#define ISO15_REQ_SELECT 0x10 // only selected cards response
|
||||
#define ISO15_REQ_ADDRESS 0x20 // this req contains an address
|
||||
#define ISO15_REQ_OPTION 0x40 // Command specific option selector
|
||||
|
||||
//REQUEST FLAGS when INVENTORY is set
|
||||
#define ISO15_REQINV_AFI 0x10 // AFI Field is present
|
||||
#define ISO15_REQINV_SLOT1 0x20 // 1 Slot
|
||||
#define ISO15_REQINV_SLOT16 0x00 // 16 Slots
|
||||
#define ISO15_REQINV_OPTION 0x40 // Command specific option selector
|
||||
#define ISO15_REQINV_AFI 0x10 // AFI Field is present
|
||||
#define ISO15_REQINV_SLOT1 0x20 // 1 Slot
|
||||
#define ISO15_REQINV_SLOT16 0x00 // 16 Slots
|
||||
#define ISO15_REQINV_OPTION 0x40 // Command specific option selector
|
||||
|
||||
//RESPONSE FLAGS
|
||||
#define ISO15_RES_ERROR 0x01
|
||||
#define ISO15_RES_EXT 0x08 // Protocol Extention
|
||||
#define ISO15_RES_ERROR 0x01
|
||||
#define ISO15_RES_EXT 0x08 // Protocol Extention
|
||||
|
||||
// RESPONSE ERROR CODES
|
||||
#define ISO15_NOERROR 0x00
|
||||
#define ISO15_ERROR_CMD_NOT_SUP 0x01 // Command not supported
|
||||
#define ISO15_ERROR_CMD_NOT_REC 0x02 // Command not recognized (eg. parameter error)
|
||||
#define ISO15_ERROR_CMD_OPTION 0x03 // Command option not supported
|
||||
#define ISO15_ERROR_GENERIC 0x0F // No additional Info about this error
|
||||
#define ISO15_ERROR_BLOCK_UNAVAILABLE 0x10
|
||||
#define ISO15_ERROR_BLOCK_LOCKED_ALREADY 0x11 // cannot lock again
|
||||
#define ISO15_ERROR_BLOCK_LOCKED 0x12 // cannot be changed
|
||||
#define ISO15_ERROR_BLOCK_WRITE 0x13 // Writing was unsuccessful
|
||||
#define ISO15_ERROR_BLOCL_WRITELOCK 0x14 // Locking was unsuccessful
|
||||
#define ISO15_NOERROR 0x00
|
||||
#define ISO15_ERROR_CMD_NOT_SUP 0x01 // Command not supported
|
||||
#define ISO15_ERROR_CMD_NOT_REC 0x02 // Command not recognized (eg. parameter error)
|
||||
#define ISO15_ERROR_CMD_OPTION 0x03 // Command option not supported
|
||||
#define ISO15_ERROR_GENERIC 0x0F // No additional Info about this error
|
||||
#define ISO15_ERROR_BLOCK_UNAVAILABLE 0x10
|
||||
#define ISO15_ERROR_BLOCK_LOCKED_ALREADY 0x11 // cannot lock again
|
||||
#define ISO15_ERROR_BLOCK_LOCKED 0x12 // cannot be changed
|
||||
#define ISO15_ERROR_BLOCK_WRITE 0x13 // Writing was unsuccessful
|
||||
#define ISO15_ERROR_BLOCL_WRITELOCK 0x14 // Locking was unsuccessful
|
||||
|
||||
// COMMAND CODES
|
||||
#define ISO15_CMD_INVENTORY 0x01
|
||||
#define ISO15_CMD_STAYQUIET 0x02
|
||||
#define ISO15_CMD_READ 0x20
|
||||
#define ISO15_CMD_WRITE 0x21
|
||||
#define ISO15_CMD_LOCK 0x22
|
||||
#define ISO15_CMD_READMULTI 0x23
|
||||
#define ISO15_CMD_WRITEMULTI 0x24
|
||||
#define ISO15_CMD_SELECT 0x25
|
||||
#define ISO15_CMD_RESET 0x26
|
||||
#define ISO15_CMD_WRITEAFI 0x27
|
||||
#define ISO15_CMD_LOCKAFI 0x28
|
||||
#define ISO15_CMD_WRITEDSFID 0x29
|
||||
#define ISO15_CMD_LOCKDSFID 0x2A
|
||||
#define ISO15_CMD_SYSINFO 0x2B
|
||||
#define ISO15_CMD_SECSTATUS 0x2C
|
||||
#define ISO15_CMD_INVENTORY 0x01
|
||||
#define ISO15_CMD_STAYQUIET 0x02
|
||||
#define ISO15_CMD_READ 0x20
|
||||
#define ISO15_CMD_WRITE 0x21
|
||||
#define ISO15_CMD_LOCK 0x22
|
||||
#define ISO15_CMD_READMULTI 0x23
|
||||
#define ISO15_CMD_WRITEMULTI 0x24
|
||||
#define ISO15_CMD_SELECT 0x25
|
||||
#define ISO15_CMD_RESET 0x26
|
||||
#define ISO15_CMD_WRITEAFI 0x27
|
||||
#define ISO15_CMD_LOCKAFI 0x28
|
||||
#define ISO15_CMD_WRITEDSFID 0x29
|
||||
#define ISO15_CMD_LOCKDSFID 0x2A
|
||||
#define ISO15_CMD_SYSINFO 0x2B
|
||||
#define ISO15_CMD_SECSTATUS 0x2C
|
||||
|
||||
char* Iso15693sprintUID(char *target, uint8_t *uid);
|
||||
|
||||
@@ -78,49 +78,49 @@ char* Iso15693sprintUID(char *target, uint8_t *uid);
|
||||
// Mode: highspeed && one subcarrier (ASK)
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// The sampling rate is 106.353 ksps/s, for T = 18.8 us
|
||||
// The sampling rate is 106.353 ksps/s, for T = 18.8 us
|
||||
|
||||
// SOF defined as
|
||||
// 1) Unmodulated time of 56.64us
|
||||
// 2) 24 pulses of 423.75khz
|
||||
// 3) logic '1' (unmodulated for 18.88us followed by 8 pulses of 423.75khz)
|
||||
// SOF defined as
|
||||
// 1) Unmodulated time of 56.64us
|
||||
// 2) 24 pulses of 423.75khz
|
||||
// 3) logic '1' (unmodulated for 18.88us followed by 8 pulses of 423.75khz)
|
||||
|
||||
static const int Iso15693FrameSOF[] = {
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1,
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1
|
||||
};
|
||||
static const int Iso15693Logic0[] = {
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1,
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1
|
||||
};
|
||||
static const int Iso15693Logic1[] = {
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1,
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1
|
||||
};
|
||||
static const int Iso15693FrameSOF[] = {
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1,
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1
|
||||
};
|
||||
static const int Iso15693Logic0[] = {
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1,
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1
|
||||
};
|
||||
static const int Iso15693Logic1[] = {
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1,
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1
|
||||
};
|
||||
|
||||
// EOF defined as
|
||||
// 1) logic '0' (8 pulses of 423.75khz followed by unmodulated for 18.88us)
|
||||
// 2) 24 pulses of 423.75khz
|
||||
// 3) Unmodulated time of 56.64us
|
||||
static const int Iso15693FrameEOF[] = {
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1,
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
|
||||
};
|
||||
// EOF defined as
|
||||
// 1) logic '0' (8 pulses of 423.75khz followed by unmodulated for 18.88us)
|
||||
// 2) 24 pulses of 423.75khz
|
||||
// 3) Unmodulated time of 56.64us
|
||||
static const int Iso15693FrameEOF[] = {
|
||||
1, 1, 1, 1,
|
||||
1, 1, 1, 1,
|
||||
-1, -1, -1, -1,
|
||||
-1, -1, -1, -1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -11,13 +11,13 @@ ms of the GNU GPL, version 2 or,
|
||||
|
||||
/* AT91SAM7S256 has 256k Flash and 64k RAM */
|
||||
/* AT91SAM7S512 has 512k Flash and 64k RAM */
|
||||
MEMORY
|
||||
MEMORY
|
||||
{
|
||||
bootphase1 : ORIGIN = 0x00100000, LENGTH = 0x200 /* Phase 1 bootloader: Copies real bootloader to RAM */
|
||||
bootphase2 : ORIGIN = 0x00100200, LENGTH = 0x2000 - 0x200 /* Main bootloader code, stored in Flash, executed from RAM */
|
||||
osimage : ORIGIN = 0x00102000, LENGTH = 512K - 0x2000 /* Place where the main OS will end up */
|
||||
ram : ORIGIN = 0x00200000, LENGTH = 64K - 0x20 /* RAM, minus small common area */
|
||||
commonarea : ORIGIN = 0x00200000 + 64K - 0x20, LENGTH = 0x20 /* Communication between bootloader and main OS */
|
||||
bootphase1 : ORIGIN = 0x00100000, LENGTH = 0x200 /* Phase 1 bootloader: Copies real bootloader to RAM */
|
||||
bootphase2 : ORIGIN = 0x00100200, LENGTH = 0x2000 - 0x200 /* Main bootloader code, stored in Flash, executed from RAM */
|
||||
osimage : ORIGIN = 0x00102000, LENGTH = 512K - 0x2000 /* Place where the main OS will end up */
|
||||
ram : ORIGIN = 0x00200000, LENGTH = 64K - 0x20 /* RAM, minus small common area */
|
||||
commonarea : ORIGIN = 0x00200000 + 64K - 0x20, LENGTH = 0x20 /* Communication between bootloader and main OS */
|
||||
}
|
||||
|
||||
/* Export some information that can be used from within the firmware */
|
||||
|
||||
+24
-24
@@ -13,9 +13,9 @@
|
||||
// c keeps track on which step the prng is.
|
||||
// legic_prng_get_bit() = gets a bit muxed from a and b.
|
||||
struct lfsr {
|
||||
uint8_t a;
|
||||
uint8_t b;
|
||||
uint32_t c;
|
||||
uint8_t a;
|
||||
uint8_t b;
|
||||
uint32_t c;
|
||||
} lfsr;
|
||||
|
||||
// Normal init is set following variables with a random value IV
|
||||
@@ -28,38 +28,38 @@ struct lfsr {
|
||||
// which is used in the initialisation phase sending the IV
|
||||
//
|
||||
void legic_prng_init(uint8_t iv) {
|
||||
lfsr.a = iv;
|
||||
lfsr.b = 0; // hack to get a always 0 keystream
|
||||
lfsr.c = 0;
|
||||
if(iv)
|
||||
lfsr.b = (iv << 1) | 1;
|
||||
lfsr.a = iv;
|
||||
lfsr.b = 0; // hack to get a always 0 keystream
|
||||
lfsr.c = 0;
|
||||
if(iv)
|
||||
lfsr.b = (iv << 1) | 1;
|
||||
}
|
||||
|
||||
void legic_prng_forward(int count) {
|
||||
if (count == 0) return;
|
||||
if (count == 0) return;
|
||||
|
||||
lfsr.c += count;
|
||||
while(count--) {
|
||||
// According: http://www.proxmark.org/forum/viewtopic.php?pid=5437#p5437
|
||||
lfsr.a = (lfsr.a >> 1 | (lfsr.a ^ lfsr.a >> 6) << 6) & 0x7F;
|
||||
lfsr.b = lfsr.b >> 1 | (lfsr.b ^ lfsr.b >> 2 ^ lfsr.b >> 3 ^ lfsr.b >> 7) << 7;
|
||||
}
|
||||
lfsr.c += count;
|
||||
while(count--) {
|
||||
// According: http://www.proxmark.org/forum/viewtopic.php?pid=5437#p5437
|
||||
lfsr.a = (lfsr.a >> 1 | (lfsr.a ^ lfsr.a >> 6) << 6) & 0x7F;
|
||||
lfsr.b = lfsr.b >> 1 | (lfsr.b ^ lfsr.b >> 2 ^ lfsr.b >> 3 ^ lfsr.b >> 7) << 7;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t legic_prng_count() {
|
||||
return lfsr.c;
|
||||
return lfsr.c;
|
||||
}
|
||||
|
||||
uint8_t legic_prng_get_bit() {
|
||||
uint8_t idx = 7 - ( (lfsr.a & 4) | (lfsr.a >> 2 & 2) | (lfsr.a >> 4 & 1) );
|
||||
return lfsr.b >> idx & 1;
|
||||
uint8_t idx = 7 - ( (lfsr.a & 4) | (lfsr.a >> 2 & 2) | (lfsr.a >> 4 & 1) );
|
||||
return lfsr.b >> idx & 1;
|
||||
}
|
||||
|
||||
uint32_t legic_prng_get_bits(uint8_t len){
|
||||
uint32_t a = 0;
|
||||
for(uint8_t i = 0; i < len; ++i) {
|
||||
a |= legic_prng_get_bit() << i;
|
||||
legic_prng_forward(1);
|
||||
}
|
||||
return a;
|
||||
uint32_t a = 0;
|
||||
for(uint8_t i = 0; i < len; ++i) {
|
||||
a |= legic_prng_get_bit() << i;
|
||||
legic_prng_forward(1);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user