Name: Anonymous 2012-10-12 9:04
Full code (its also full of nonsense, please forgive
Posting this for reference for http://boards.4chan.org/g/res/28230027
What this should do, is do (g ^ random) mod p, exchange the resulting value with itself, and xor unxor lines with the resulting shared index. its a communication program for arduino.
Right now, it doesnt communicate.
Help please.
uint8_t key = 0;
uint32_t othersharedIndex = 0;
uint32_t sharedindex = 0;
uint32_t g = 16807;
uint32_t p = 2147483647;
uint32_t B = 0;
union data
{
uint32_t sharedindex;
unsigned char components[4];
};
void sendkey(uint32_t sharedindex) {
if(Serial1.available()){
union data d;
d.sharedindex = sharedindex;
int i;
for (i = 0; i < 4; i++) {
Serial1.write(d.components[i]);
}
}
}
uint8_t get_seed()
{
uint8_t seed = 0;
for (int i=0; i<16; i++) {
seed = seed << 1;
seed = seed ^ analogRead(i);
}
return seed;
}
//This is Dr. Bowlings random seed function, which I think should be good enough, it was understood, not just blindly copied.
uint32_t mul_mod(uint32_t b, uint32_t e, uint32_t m) {
uint32_t answer = 0; //What we want to find out, 42
uint32_t r = b; //local variable so we don't modify b outside of this thing.
uint32_t a = e;
while ( r ) {
if ( r & 1 ) {
answer = ( answer + a ) % m;
}
/*mul_mod adds the base and itself, then modding, sneakily staying under the 31 bit treshold as mod will always be smaller or equal t
an mod and we avoid nasty multiplications. */
r = r >> 1;
a = (a << 1) % m ;
}
return answer;
}
/*This is mul_mod a modified pow_mod that doesn't overflow, and which pow_mod calls for its calculations now. Developed from class ha
out and TA input, and based on Pow_mod */
uint32_t pow_mod(uint32_t b, uint32_t e, uint32_t m) {
// Modified pow_mod which will not overflow when using large exponents or moduli, based upon Dr. Hoover's original Pow_mod
// if b = 0 or m = 0 then result is always 0
if ( b == 0 || m == 0 ) {
return 0;
}
// if e = 0 then result is 1
if ( e == 0 ) {
return 1;
}
// Special cases ^
// reduce b mod m
uint32_t r = 1; // Our result.
uint32_t redb = b % m;
// stop the moment no bits left in e to be processed
while (e){
if ( e & 1 ) {
r = mul_mod(r, redb, m);
}
// and we need to ensure that r = (b ** e_[i..0] ) % m
// is the current bit of e set?
r = r;
redb = mul_mod(redb, redb, m);
//Repeats on the next power of b
e = e >>1;
// We shift to move things forward, when we have 0 left we are done.
}
// at this point r = (b ** e) % m, we don't overflow anymore!
return r;
}
void readline(char *s, int bufsize){
uint8_t i = 0;
while( i < bufsize-1 ) {
// wait for a character
while (Serial.available() == 0) {
} /* Do nothing */
// grab the character and save in the buffer
s[i] = Serial.read();
// if end of line or somehow a \0 got sent, we are done
if (s[i] == '\n' || s[i] == '\0') break;
i += 1;
}
// \0 teminate the string
s[i] = '\0';
}
/* readlong:
Read a sequence characters from the serial monitor and interpret
them as a decimal integer.
The characters can be leading and tailing blanks, a leading '-',
and the digits 0-9.
Return that number as a 32 bit int.
*/
int32_t readlong()
{
char s[128]; /* 128 characters should be more than enough. */
readline(s, 128);
return atol(s);
}
/* Read a line (up to maxlen characters) off of the serial port. */
void readPublicKey(char *s, int maxlen)
{
int i = 0;
while(1) {
while (Serial.available() == 0) {
} /* Do nothing */
s[i] = Serial.read();
// Need to search for '\r' b/c of minicom
if (s[i] == '\0' || s[i] == '\r' || s[i] == '\n' || i == maxlen-1 || s[i] == ':') break;
i += 1;
}
}
/*void sendkey(uint32_t sharedindex) {
if(Serial1.available()){
// reads in a byte and shifts it over for the next byte
Serial1.write( (sharedindex >> 24) & 0xFF );
Serial1.write( (sharedindex >> 16) & 0xFF );
Serial1.write( (sharedindex >> 8) & 0xFF );
Serial1.write( sharedindex & 0xFF );
}
}
//This breaks the key into bytes and sends it over
*/
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
randomSeed(get_seed());
uint32_t a = random(256); //& 0xff ;
// the & 0xff masks this from potential eavesdroppers by making our 8 bit key appear as a 32 bit one.
Serial.println("This is my shared index a: ");
//Serial.println(pow_mod(g, a, p)); //This displays shared index on my screen.
sharedindex = (pow_mod(g, a, p));
Serial.println(sharedindex);
delay(3000); //No need to rush, setup time for exchange.
//uint32_t go;
//go = readlong();
sendkey((uint32_t)sharedindex);
//This sends the local Shared index, as bytes, to my partner.
/*
uint32_t keylisten = 1;
if (Serial1.available() > 0){
while (keylisten = 1){
sendkey((uint32_t)sharedindex);
if (Serial1.available() >= 4){
othersharedIndex = othersharedIndex | Serial1.read() << 24;
othersharedIndex = othersharedIndex | Serial1.read() << 16;
othersharedIndex = othersharedIndex | Serial1.read() << 8;
othersharedIndex = othersharedIndex | Serial1.read();
Serial.println("My partner's Shared Index is:");
keylisten = 0;
}
}
}
*/
sendkey((uint32_t)sharedindex);
if (Serial1.available()){
union data d;
int i;
for (i = 0; i < 4; i++) {
d.components[i] = Serial1.read();
}
uint32_t retrieved_value = d.sharedindex;
Serial.print (othersharedIndex, DEC);
B = othersharedIndex;
}
Serial.print (othersharedIndex, DEC);
B = othersharedIndex;
//Serial.print (othersharedIndex, DEC);
// B = othersharedIndex;
//Serial.println(B);
othersharedIndex = (pow_mod(B, a, p));
//Serial.println(othersharedIndex);
//int16_t inByte = Serial1.read();
//Serial.write(inByte ^ othersharedIndex) ;
//uint8_t incomingByte = Serial.read();
//uint32_t othersharedIndex = 0;
//Serial.print(othersharedIndex);
}
void loop(){
//unsigned char *bytes = (unsigned char*)& sharedindex;
//Serial.write(bytes, 4);
//uint32_t keylisten = 1;
/*if (Serial1.available() > 0){
while (keylisten == 1){
sendkey((uint32_t)sharedindex);
if (Serial1.available() >= 4){
othersharedIndex = othersharedIndex | Serial1.read() << 24;
othersharedIndex = othersharedIndex | Serial1.read() << 16;
othersharedIndex = othersharedIndex | Serial1.read() << 8;
othersharedIndex = othersharedIndex | Serial1.read();
Serial.println("My partner's Shared Index is:");
keylisten = 0;
}
}
}
*/
/*
uint32_t e = 40;
while (e){
uint32_t e = 50;
sendkey((uint32_t)sharedindex);
e--;
}
*/
/*
if (Serial1.available()){
int32_t inByte = Serial1.read();
Serial.write(inByte);
}
*/
//uint32_t go;
//go = readlong();
//Debugging, allows me to pause.Program wont advance till I hit enter
/*
Serial.println ("This is a:");
Serial.println(a);
Debugging code
*/
/*
uint32_t b, e, m;
b = readlong();
Serial.print("b=");
Serial.print(b);
Serial.print(" ");
e = readlong();
Serial.print("e=");
Serial.print(e);
Serial.print(" ");
m = readlong();
Serial.print("m=");
Serial.print(m);
Serial.print("\n");
Serial.print("answer = ");
Serial.println(pow_mod(b, e, m));
Debugging code to test pow mod and mul mod functions (plug in number, power, mod, will display answer in serial.
*/
}Posting this for reference for http://boards.4chan.org/g/res/28230027
What this should do, is do (g ^ random) mod p, exchange the resulting value with itself, and xor unxor lines with the resulting shared index. its a communication program for arduino.
Right now, it doesnt communicate.
Help please.