update crapto1 and fix compiling warnings
Crapto1 library from proxmark3 repo
This commit is contained in:
parent
ba072f16f6
commit
0723f53aaf
@ -35,7 +35,7 @@ AC_FUNC_REALLOC
|
|||||||
AC_CHECK_FUNCS([memset])
|
AC_CHECK_FUNCS([memset])
|
||||||
|
|
||||||
# C99
|
# C99
|
||||||
CFLAGS="$CFLAGS -std=c99"
|
CFLAGS="$CFLAGS -std=c99 -O3 -Wall"
|
||||||
|
|
||||||
AC_CONFIG_FILES([Makefile
|
AC_CONFIG_FILES([Makefile
|
||||||
src/Makefile])
|
src/Makefile])
|
||||||
|
|||||||
@ -2,9 +2,9 @@ AM_CFLAGS = @libnfc_CFLAGS@
|
|||||||
|
|
||||||
bin_PROGRAMS = mfoc
|
bin_PROGRAMS = mfoc
|
||||||
|
|
||||||
noinst_HEADERS = crapto1.h mfoc.h mifare.h nfc-utils.h
|
noinst_HEADERS = crapto1.h mfoc.h mifare.h nfc-utils.h parity.h
|
||||||
|
|
||||||
mfoc_SOURCES = crapto1.c crypto1.c mfoc.c mifare.c nfc-utils.c
|
mfoc_SOURCES = crapto1.c crypto1.c mfoc.c mifare.c nfc-utils.c parity.c
|
||||||
mfoc_LDADD = @libnfc_LIBS@
|
mfoc_LDADD = @libnfc_LIBS@
|
||||||
|
|
||||||
dist_man_MANS = mfoc.1
|
dist_man_MANS = mfoc.1
|
||||||
|
|||||||
1032
src/crapto1.c
1032
src/crapto1.c
File diff suppressed because it is too large
Load Diff
@ -15,8 +15,8 @@
|
|||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
MA 02110-1301, US$
|
MA 02110-1301, US$
|
||||||
|
|
||||||
Copyright (C) 2008-2009 bla <blapost@gmail.com>
|
Copyright (C) 2008-2014 bla <blapost@gmail.com>
|
||||||
*/
|
*/
|
||||||
#ifndef CRAPTO1_INCLUDED
|
#ifndef CRAPTO1_INCLUDED
|
||||||
#define CRAPTO1_INCLUDED
|
#define CRAPTO1_INCLUDED
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -25,66 +25,59 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct Crypto1State {uint32_t odd, even;};
|
struct Crypto1State {
|
||||||
struct Crypto1State *crypto1_create(uint64_t);
|
uint32_t odd, even;
|
||||||
void crypto1_destroy(struct Crypto1State *);
|
};
|
||||||
void crypto1_get_lfsr(struct Crypto1State *, uint64_t *);
|
#if defined(__arm__) && !defined(__linux__) && !defined(_WIN32) && !defined(__APPLE__) // bare metal ARM Proxmark lacks malloc()/free()
|
||||||
uint8_t crypto1_bit(struct Crypto1State *, uint8_t, int);
|
void crypto1_create(struct Crypto1State *s, uint64_t key);
|
||||||
uint8_t crypto1_byte(struct Crypto1State *, uint8_t, int);
|
#else
|
||||||
uint32_t crypto1_word(struct Crypto1State *, uint32_t, int);
|
struct Crypto1State *crypto1_create(uint64_t key);
|
||||||
uint32_t prng_successor(uint32_t x, uint32_t n);
|
#endif
|
||||||
|
void crypto1_destroy(struct Crypto1State*);
|
||||||
|
void crypto1_get_lfsr(struct Crypto1State*, uint64_t*);
|
||||||
|
uint8_t crypto1_bit(struct Crypto1State*, uint8_t, int);
|
||||||
|
uint8_t crypto1_byte(struct Crypto1State*, uint8_t, int);
|
||||||
|
uint32_t crypto1_word(struct Crypto1State*, uint32_t, int);
|
||||||
|
uint32_t prng_successor(uint32_t x, uint32_t n);
|
||||||
|
|
||||||
struct Crypto1State *lfsr_recovery32(uint32_t ks2, uint32_t in);
|
struct Crypto1State* lfsr_recovery32(uint32_t ks2, uint32_t in);
|
||||||
struct Crypto1State *lfsr_recovery64(uint32_t ks2, uint32_t ks3);
|
struct Crypto1State* lfsr_recovery64(uint32_t ks2, uint32_t ks3);
|
||||||
|
uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd);
|
||||||
|
struct Crypto1State*
|
||||||
|
lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8], uint32_t no_par);
|
||||||
|
|
||||||
void lfsr_rollback(struct Crypto1State *s, uint32_t in, int fb);
|
|
||||||
uint32_t lfsr_rollback_word(struct Crypto1State *s, uint32_t in, int fb);
|
uint8_t lfsr_rollback_bit(struct Crypto1State* s, uint32_t in, int fb);
|
||||||
int nonce_distance(uint32_t from, uint32_t to);
|
uint8_t lfsr_rollback_byte(struct Crypto1State* s, uint32_t in, int fb);
|
||||||
bool validate_prng_nonce(uint32_t nonce);
|
uint32_t lfsr_rollback_word(struct Crypto1State* s, uint32_t in, int fb);
|
||||||
|
int nonce_distance(uint32_t from, uint32_t to);
|
||||||
|
bool validate_prng_nonce(uint32_t nonce);
|
||||||
#define FOREACH_VALID_NONCE(N, FILTER, FSIZE)\
|
#define FOREACH_VALID_NONCE(N, FILTER, FSIZE)\
|
||||||
uint32_t __n = 0,__M = 0, N = 0;\
|
uint32_t __n = 0,__M = 0, N = 0;\
|
||||||
int __i;\
|
int __i;\
|
||||||
for(; __n < 1 << 16; N = prng_successor(__M = ++__n, 16))\
|
for(; __n < 1 << 16; N = prng_successor(__M = ++__n, 16))\
|
||||||
for(__i = FSIZE - 1; __i >= 0; __i--)\
|
for(__i = FSIZE - 1; __i >= 0; __i--)\
|
||||||
if(BIT(FILTER, __i) ^ parity(__M & 0xFF01))\
|
if(BIT(FILTER, __i) ^ evenparity32(__M & 0xFF01))\
|
||||||
break;\
|
break;\
|
||||||
else if(__i)\
|
else if(__i)\
|
||||||
__M = prng_successor(__M, (__i == 7) ? 48 : 8);\
|
__M = prng_successor(__M, (__i == 7) ? 48 : 8);\
|
||||||
else
|
else
|
||||||
|
|
||||||
#define LF_POLY_ODD (0x29CE5C)
|
#define LF_POLY_ODD (0x29CE5C)
|
||||||
#define LF_POLY_EVEN (0x870804)
|
#define LF_POLY_EVEN (0x870804)
|
||||||
#define BIT(x, n) ((x) >> (n) & 1)
|
#define BIT(x, n) ((x) >> (n) & 1)
|
||||||
#define BEBIT(x, n) BIT(x, (n) ^ 24)
|
#define BEBIT(x, n) BIT(x, (n) ^ 24)
|
||||||
static inline int parity(uint32_t x)
|
|
||||||
{
|
|
||||||
#if !defined __i386__ || !defined __GNUC__
|
|
||||||
x ^= x >> 16;
|
|
||||||
x ^= x >> 8;
|
|
||||||
x ^= x >> 4;
|
|
||||||
return BIT(0x6996, x & 0xf);
|
|
||||||
#else
|
|
||||||
__asm__("movl %1, %%eax\n"
|
|
||||||
"mov %%ax, %%cx\n"
|
|
||||||
"shrl $0x10, %%eax\n"
|
|
||||||
"xor %%ax, %%cx\n"
|
|
||||||
"xor %%ch, %%cl\n"
|
|
||||||
"setpo %%al\n"
|
|
||||||
"movzx %%al, %0\n": "=r"(x) : "r"(x): "eax", "ecx");
|
|
||||||
return x;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
static inline int filter(uint32_t const x)
|
|
||||||
{
|
|
||||||
uint32_t f;
|
|
||||||
|
|
||||||
f = 0xf22c0 >> (x & 0xf) & 16;
|
static inline int filter(uint32_t const x) {
|
||||||
f |= 0x6c9c0 >> (x >> 4 & 0xf) & 8;
|
uint32_t f;
|
||||||
f |= 0x3c8b0 >> (x >> 8 & 0xf) & 4;
|
|
||||||
f |= 0x1e458 >> (x >> 12 & 0xf) & 2;
|
f = 0xf22c0 >> (x & 0xf) & 16;
|
||||||
f |= 0x0d938 >> (x >> 16 & 0xf) & 1;
|
f |= 0x6c9c0 >> (x >> 4 & 0xf) & 8;
|
||||||
return BIT(0xEC57E80A, f);
|
f |= 0x3c8b0 >> (x >> 8 & 0xf) & 4;
|
||||||
}
|
f |= 0x1e458 >> (x >> 12 & 0xf) & 2;
|
||||||
|
f |= 0x0d938 >> (x >> 16 & 0xf) & 1;
|
||||||
|
return BIT(0xEC57E80A, f);
|
||||||
|
}
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
150
src/crypto1.c
150
src/crypto1.c
@ -1,92 +1,112 @@
|
|||||||
/* crypto1.c
|
/* crypto1.c
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or
|
This program is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU General Public License
|
modify it under the terms of the GNU General Public License
|
||||||
as published by the Free Software Foundation; either version 2
|
as published by the Free Software Foundation; either version 2
|
||||||
of the License, or (at your option) any later version.
|
of the License, or (at your option) any later version.
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
MA 02110-1301, US
|
MA 02110-1301, US
|
||||||
|
|
||||||
Copyright (C) 2008-2008 bla <blapost@gmail.com>
|
Copyright (C) 2008-2008 bla <blapost@gmail.com>
|
||||||
*/
|
*/
|
||||||
#include "crapto1.h"
|
#include "crapto1.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "parity.h"
|
||||||
|
|
||||||
#define SWAPENDIAN(x)\
|
#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)
|
||||||
|
|
||||||
struct Crypto1State *crypto1_create(uint64_t key) {
|
#if defined(__arm__) && !defined(__linux__) && !defined(_WIN32) && !defined(__APPLE__) // bare metal ARM Proxmark lacks malloc()/free()
|
||||||
struct Crypto1State *s = malloc(sizeof(*s));
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 47; s && i > 0; i -= 2) {
|
void crypto1_create(struct Crypto1State *s, uint64_t key) {
|
||||||
s->odd = s->odd << 1 | BIT(key, (i - 1) ^ 7);
|
int i;
|
||||||
s->even = s->even << 1 | BIT(key, i ^ 7);
|
|
||||||
}
|
for (i = 47; s && i > 0; i -= 2) {
|
||||||
return s;
|
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)
|
|
||||||
{
|
void crypto1_destroy(struct Crypto1State *state) {
|
||||||
free(state);
|
state->odd = 0;
|
||||||
|
state->even = 0;
|
||||||
}
|
}
|
||||||
void crypto1_get_lfsr(struct Crypto1State *state, uint64_t *lfsr)
|
#else
|
||||||
{
|
|
||||||
int i;
|
struct Crypto1State * crypto1_create(uint64_t key) {
|
||||||
for (*lfsr = 0, i = 23; i >= 0; --i) {
|
struct Crypto1State *s = malloc(sizeof (*s));
|
||||||
*lfsr = *lfsr << 1 | BIT(state->odd, i ^ 3);
|
int i;
|
||||||
*lfsr = *lfsr << 1 | BIT(state->even, i ^ 3);
|
|
||||||
}
|
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 s;
|
||||||
}
|
}
|
||||||
uint8_t crypto1_bit(struct Crypto1State *s, uint8_t in, int is_encrypted)
|
|
||||||
{
|
|
||||||
uint32_t feedin;
|
|
||||||
uint8_t ret = filter(s->odd);
|
|
||||||
|
|
||||||
feedin = ret & !!is_encrypted;
|
void crypto1_destroy(struct Crypto1State *state) {
|
||||||
feedin ^= !!in;
|
free(state);
|
||||||
feedin ^= LF_POLY_ODD & s->odd;
|
|
||||||
feedin ^= LF_POLY_EVEN & s->even;
|
|
||||||
s->even = s->even << 1 | parity(feedin);
|
|
||||||
|
|
||||||
s->odd ^= (s->odd ^= s->even, s->even ^= s->odd);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
uint8_t crypto1_byte(struct Crypto1State *s, uint8_t in, int is_encrypted)
|
#endif
|
||||||
{
|
|
||||||
uint8_t i, ret = 0;
|
|
||||||
|
|
||||||
for (i = 0; i < 8; ++i)
|
void crypto1_get_lfsr(struct Crypto1State *state, uint64_t *lfsr) {
|
||||||
ret |= crypto1_bit(s, BIT(in, i), is_encrypted) << i;
|
int i;
|
||||||
|
for (*lfsr = 0, i = 23; i >= 0; --i) {
|
||||||
return ret;
|
*lfsr = *lfsr << 1 | BIT(state->odd, i ^ 3);
|
||||||
|
*lfsr = *lfsr << 1 | BIT(state->even, i ^ 3);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted)
|
|
||||||
{
|
|
||||||
uint32_t i, ret = 0;
|
|
||||||
|
|
||||||
for (i = 0; i < 32; ++i)
|
uint8_t crypto1_bit(struct Crypto1State *s, uint8_t in, int is_encrypted) {
|
||||||
ret |= crypto1_bit(s, BEBIT(in, i), is_encrypted) << (i ^ 24);
|
uint32_t feedin, t;
|
||||||
|
uint8_t ret = filter(s->odd);
|
||||||
|
|
||||||
return ret;
|
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;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t crypto1_byte(struct Crypto1State *s, uint8_t in, int is_encrypted) {
|
||||||
|
uint8_t i, ret = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < 8; ++i)
|
||||||
|
ret |= crypto1_bit(s, BIT(in, i), is_encrypted) << i;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted) {
|
||||||
|
uint32_t i, ret = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < 32; ++i)
|
||||||
|
ret |= crypto1_bit(s, BEBIT(in, i), is_encrypted) << (i ^ 24);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* prng_successor
|
/* prng_successor
|
||||||
* helper used to obscure the keystream during authentication
|
* helper used to obscure the keystream during authentication
|
||||||
*/
|
*/
|
||||||
uint32_t prng_successor(uint32_t x, uint32_t n)
|
uint32_t prng_successor(uint32_t x, uint32_t n) {
|
||||||
{
|
SWAPENDIAN(x);
|
||||||
SWAPENDIAN(x);
|
while (n--)
|
||||||
while (n--)
|
x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
|
||||||
x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
|
|
||||||
|
|
||||||
return SWAPENDIAN(x);
|
return SWAPENDIAN(x);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,7 +37,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <inttypes.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
// NFC
|
// NFC
|
||||||
@ -131,7 +131,6 @@ int main(int argc, char *const argv[])
|
|||||||
//File pointers for the keyfile
|
//File pointers for the keyfile
|
||||||
FILE * fp;
|
FILE * fp;
|
||||||
char line[20];
|
char line[20];
|
||||||
size_t len = 0;
|
|
||||||
char * read;
|
char * read;
|
||||||
|
|
||||||
//Regexp declarations
|
//Regexp declarations
|
||||||
@ -546,7 +545,7 @@ int main(int argc, char *const argv[])
|
|||||||
nfc_close(r.pdi);
|
nfc_close(r.pdi);
|
||||||
nfc_exit(context);
|
nfc_exit(context);
|
||||||
if(pfKey) {
|
if(pfKey) {
|
||||||
fprintf(pfKey, "%012llx;%d;%c;%d;%c", knownKey, knownSector, knownKeyLetter, unknownSector, unknownKeyLetter);
|
fprintf(pfKey, "%012" PRIu64 ";%d;%c;%d;%c", knownKey, knownSector, knownKeyLetter, unknownSector, unknownKeyLetter);
|
||||||
fclose(pfKey);
|
fclose(pfKey);
|
||||||
}
|
}
|
||||||
return 9;
|
return 9;
|
||||||
@ -883,7 +882,6 @@ get_rats_is_2k(mftag t, mfreader r)
|
|||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
uint8_t abtRx[MAX_FRAME_LEN];
|
uint8_t abtRx[MAX_FRAME_LEN];
|
||||||
int szRxBits;
|
|
||||||
uint8_t abtRats[2] = { 0xe0, 0x50};
|
uint8_t abtRats[2] = { 0xe0, 0x50};
|
||||||
// Use raw send/receive methods
|
// Use raw send/receive methods
|
||||||
if (nfc_device_set_property_bool(r.pdi, NP_EASY_FRAMING, false) < 0) {
|
if (nfc_device_set_property_bool(r.pdi, NP_EASY_FRAMING, false) < 0) {
|
||||||
|
|||||||
28
src/parity.c
Normal file
28
src/parity.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||||
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||||
|
// the license.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// parity functions (all defined in parity.h)
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
const uint8_t OddByteParity[256] = {
|
||||||
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
||||||
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||||
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||||
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
||||||
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||||
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
||||||
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
||||||
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||||
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||||
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
||||||
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
||||||
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||||
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
||||||
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||||
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
||||||
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
|
||||||
|
};
|
||||||
54
src/parity.h
Normal file
54
src/parity.h
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||||
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||||
|
// the license.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Parity functions
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// all functions defined in header file by purpose. Allows compiler optimizations.
|
||||||
|
|
||||||
|
#ifndef __PARITY_H
|
||||||
|
#define __PARITY_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
extern const uint8_t OddByteParity[256];
|
||||||
|
|
||||||
|
static inline bool oddparity8(const uint8_t x) {
|
||||||
|
return OddByteParity[x];
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void oddparitybuf(const uint8_t *x, size_t len, uint8_t *parity) {
|
||||||
|
memset(parity, 0x00, (len - 1) / 8 + 1);
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
parity[i / 8] |= oddparity8(x[i]) << (7 - (i % 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool evenparity8(const uint8_t x) {
|
||||||
|
return !OddByteParity[x];
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool evenparity32(uint32_t x) {
|
||||||
|
#if !defined __GNUC__
|
||||||
|
x ^= x >> 16;
|
||||||
|
x ^= x >> 8;
|
||||||
|
return evenparity8(x);
|
||||||
|
#else
|
||||||
|
return __builtin_parity(x);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool oddparity32(uint32_t x) {
|
||||||
|
#if !defined __GNUC__
|
||||||
|
x ^= x >> 16;
|
||||||
|
x ^= x >> 8;
|
||||||
|
return oddparity8(x);
|
||||||
|
#else
|
||||||
|
return !__builtin_parity(x);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* __PARITY_H */
|
||||||
Loading…
x
Reference in New Issue
Block a user