mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-14 18:48:13 -07:00
CHG: some textual change to README.txt
ADD: a prng.c to collect some different PRNG's i've ran into ADD: some changes the tea implementation ADD: a enhanced version - SwapEndian64ex
This commit is contained in:
parent
a7474bb30a
commit
f4d0ffd1b9
8 changed files with 86 additions and 37 deletions
39
common/prng.c
Normal file
39
common/prng.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------------
|
||||
// Burtle Prng - Modified. 42iterations instead of 20.
|
||||
// ref: http://burtleburtle.net/bob/rand/smallprng.html
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "prng.h"
|
||||
|
||||
#define rot(x,k) (((x)<<(k))|((x)>>(32-(k))))
|
||||
uint32_t burtle_get_mod( prng_ctx *x ) {
|
||||
uint32_t e = x->a - rot(x->b, 21);
|
||||
x->a = x->b ^ rot(x->c, 19);
|
||||
x->b = x->c + rot(x->d, 6);
|
||||
x->c = x->d + e;
|
||||
x->d = e + x->a;
|
||||
return x->d;
|
||||
}
|
||||
|
||||
void burtle_init_mod(prng_ctx *x, uint32_t seed ) {
|
||||
x->a = 0xf1ea5eed;
|
||||
x->b = x->c = x->d = seed;
|
||||
for (uint8_t i=0; i < 42; ++i) {
|
||||
(void)burtle_get_mod(x);
|
||||
}
|
||||
}
|
||||
|
||||
void burtle_init(prng_ctx *x, uint32_t seed ) {
|
||||
uint32_t i;
|
||||
x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
|
||||
for (i=0; i < 20; ++i) {
|
||||
(void)burtle_get_mod(x);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint32_t GetSimplePrng( uint32_t seed ){
|
||||
seed *= 0x19660D;
|
||||
seed += 0x3C6EF35F;
|
||||
return seed;
|
||||
}
|
24
common/prng.h
Normal file
24
common/prng.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------------
|
||||
// Burtle Prng - Modified. 42iterations instead of 20.
|
||||
// ref: http://burtleburtle.net/bob/rand/smallprng.html
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef __PRNG_H
|
||||
#define __PRNG_H
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
typedef struct prng_ctx {
|
||||
uint32_t a;
|
||||
uint32_t b;
|
||||
uint32_t c;
|
||||
uint32_t d;
|
||||
} prng_ctx;
|
||||
|
||||
//uint32_t burtle_get( prng_ctx *x );
|
||||
uint32_t burtle_get_mod( prng_ctx *x );
|
||||
void burtle_init_mod(prng_ctx *x, uint32_t seed );
|
||||
void burtle_init(prng_ctx *x, uint32_t seed );
|
||||
|
||||
uint32_t GetSimplePrng( uint32_t seed );
|
||||
#endif /* __PRNG_H */
|
22
common/tea.c
22
common/tea.c
|
@ -10,8 +10,6 @@
|
|||
#define ROUNDS 32
|
||||
#define DELTA 0x9E3779B9
|
||||
#define SUM 0xC6EF3720
|
||||
#define SWAPENDIAN(x)\
|
||||
(x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
|
||||
|
||||
void tea_encrypt(uint8_t *v, uint8_t *key) {
|
||||
|
||||
|
@ -28,13 +26,6 @@ void tea_encrypt(uint8_t *v, uint8_t *key) {
|
|||
//input
|
||||
y = bytes_to_num(v, 4);
|
||||
z = bytes_to_num(v+4, 4);
|
||||
|
||||
// SWAPENDIAN(a);
|
||||
// SWAPENDIAN(b);
|
||||
// SWAPENDIAN(c);
|
||||
// SWAPENDIAN(d);
|
||||
// SWAPENDIAN(y);
|
||||
// SWAPENDIAN(z);
|
||||
|
||||
while ( n-- > 0 ) {
|
||||
sum += DELTA;
|
||||
|
@ -42,9 +33,6 @@ void tea_encrypt(uint8_t *v, uint8_t *key) {
|
|||
z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
|
||||
}
|
||||
|
||||
// SWAPENDIAN(y);
|
||||
// SWAPENDIAN(z);
|
||||
|
||||
num_to_bytes(y, 4, v);
|
||||
num_to_bytes(z, 4, v+4);
|
||||
}
|
||||
|
@ -65,22 +53,12 @@ void tea_decrypt(uint8_t *v, uint8_t *key) {
|
|||
y = bytes_to_num(v, 4);
|
||||
z = bytes_to_num(v+4, 4);
|
||||
|
||||
// SWAPENDIAN(a);
|
||||
// SWAPENDIAN(b);
|
||||
// SWAPENDIAN(c);
|
||||
// SWAPENDIAN(d);
|
||||
// SWAPENDIAN(y);
|
||||
// SWAPENDIAN(z);
|
||||
|
||||
/* sum = delta<<5, in general sum = delta * n */
|
||||
while ( n-- > 0 ) {
|
||||
z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
|
||||
y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
|
||||
sum -= DELTA;
|
||||
}
|
||||
|
||||
// SWAPENDIAN(y);
|
||||
// SWAPENDIAN(z);
|
||||
num_to_bytes(y, 4, v);
|
||||
num_to_bytes(z, 4, v+4);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue