Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 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.
- // you are calling sendkey twice, is this intended?
- sendkey((uint32_t)sharedindex);
- if (Serial1.available()){
- union data d;
- int i;
- for (i = 0; i < 4; i++) {
- d.components[i] = Serial1.read();
- }
- othersharedIndex = d.sharedindex;
- Serial.print (othersharedIndex, DEC);
- B = othersharedIndex;
- }
- Serial.print (othersharedIndex, DEC);
- B = othersharedIndex;
- othersharedIndex = (pow_mod(B, a, p));
- }
- void loop(){
- }
Add Comment
Please, Sign In to add comment