added random number generator based on adc and xxtea

This commit is contained in:
schneider 2011-07-23 23:47:42 +02:00
parent b9a26a5698
commit cdaa8f78ca
7 changed files with 40 additions and 18 deletions

View File

@ -5,6 +5,7 @@
#include "lcd/fonts/smallfonts.h"
#include "lcd/print.h"
#include "filesystem/ff.h"
#include "basic/random.h"
/**************************************************************************/
@ -92,6 +93,7 @@ int lcdInitConfig(){
void main_default(void) {
systickInit(10);
randomInit();
if(getInputRaw()==BTN_ENTER){
ISPandReset();

View File

@ -15,6 +15,7 @@ OBJS += menu.o
OBJS += xxtea.o
OBJS += ecc.o
OBJS += byteorder.o
OBJS += random.o
LIBNAME=basic

View File

@ -13,29 +13,15 @@
#include <stdio.h>
#include <stdint.h>
#include "ecc.h"
#include "random.h"
exp_t base_order;
elem_t poly; /* the reduction polynomial */
elem_t coeff_b, base_x, base_y;
static int xrandm=100000000;
static int xrandm1=10000;
static int xrandb1=51723621;
int xmult(int p,int q)
{
int p1,p0,q1,q0;
p1=p/xrandm1; p0=p%xrandm1;
q1=q/xrandm1; q0=q%xrandm1;
return (((p0*q1+p1*q0)%xrandm1)*xrandm1+p0*q0)%xrandm;
}
unsigned char rnd1()
{
static int a=123456789;
a = (xmult(a,xrandb1)+1)%xrandm;
return a & 0xff;
return random() & 0xFF;
}

26
firmware/basic/random.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdint.h>
#include "random.h"
#include "xxtea.h"
#define STATE_SIZE 8
uint32_t state[STATE_SIZE];
uint32_t const I[4] = {12,13,14,15};
void randomInit(void)
{
uint32_t i,j,x;
for(j=0; j<STATE_SIZE; j++){
x = 0;
for(i=0; i<10240; i++){
x ^= (adcRead(1)&1)<<(i%32);
}
state[j] = x;
}
xxtea_encode_words(state, STATE_SIZE, I);
}
uint32_t random(void)
{
xxtea_encode_words(state, STATE_SIZE, I);
return state[0];
}

7
firmware/basic/random.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef _RANDOM_H_
#define _RANDOM_H_
void randomInit(void);
uint32_t random(void);
#endif

View File

@ -96,7 +96,7 @@ uint8_t openbeaconSend(void)
status = openbeaconSendPacket(oid, seq, 0xFF, strength++);
if( strength == 4 )
strength = 0;
if( seq++ & OPENBEACON_SAVE == OPENBEACON_SAVE )
if( (seq++ & OPENBEACON_SAVE) == OPENBEACON_SAVE )
openbeaconSaveBlock();
return status;
}

View File

@ -12,7 +12,7 @@ void rftransfer_send(uint16_t size, uint8_t *data)
buf[1] = size >> 8;
buf[2] = size & 0xFF;
uint16_t rand = 5; //random_rand16();
uint16_t rand = random() & 0xFFFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;