Guest User

Untitled

a guest
Jan 15th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.73 KB | None | 0 0
  1. uint8_t key = 0;
  2. uint32_t othersharedIndex = 0;
  3. uint32_t sharedindex = 0;
  4. uint32_t g = 16807;
  5. uint32_t p = 2147483647;
  6. uint32_t B = 0;
  7.  
  8.  
  9. union data
  10. {
  11.     uint32_t sharedindex;
  12.     unsigned char components[4];
  13. };
  14. void sendkey(uint32_t sharedindex) {
  15.  
  16.   if(Serial1.available()){
  17.  
  18.       union data d;
  19.       d.sharedindex = sharedindex;
  20.       int i;
  21.       for (i = 0; i < 4; i++) {
  22.           Serial1.write(d.components[i]);
  23.       }
  24.   }
  25. }
  26. uint8_t get_seed()
  27. {
  28.   uint8_t seed = 0;
  29.   for (int i=0; i<16; i++) {
  30.     seed = seed << 1;
  31.     seed = seed ^ analogRead(i);
  32.   }
  33.   return seed;
  34. }
  35. //This is Dr. Bowlings random seed function, which I think should be good enough, it was understood, not just blindly copied.
  36.  
  37. uint32_t mul_mod(uint32_t b, uint32_t e, uint32_t m) {
  38.  
  39.   uint32_t answer = 0;  //What we want to find out, 42
  40.   uint32_t r = b; //local variable so we don't modify b outside of this thing.
  41.   uint32_t a = e;
  42.   while ( r ) {
  43.  
  44.     if ( r & 1 ) {
  45.       answer = ( answer + a ) % m;
  46.     }
  47.     /*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
  48.     an mod and we avoid nasty multiplications. */
  49.       r = r >> 1;
  50.     a = (a << 1) % m ;
  51.   }
  52.   return answer;
  53. }
  54.  
  55. /*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
  56. out and TA input, and based on Pow_mod */
  57.  
  58.  
  59. uint32_t pow_mod(uint32_t b, uint32_t e, uint32_t m) {
  60.  
  61.   // Modified pow_mod which will not overflow when using large exponents or moduli, based upon Dr. Hoover's original Pow_mod
  62.  
  63.   // if b = 0 or m = 0 then result is always 0
  64.   if ( b == 0 || m == 0 ) {
  65.     return 0;
  66.   }
  67.  
  68.   // if e = 0 then result is 1
  69.   if ( e == 0 ) {
  70.     return 1;
  71.   }
  72.   // Special cases ^
  73.   // reduce b mod m
  74.   uint32_t r = 1; // Our result.
  75.   uint32_t redb = b % m;
  76.  
  77.   // stop the moment no bits left in e to be processed
  78.   while (e){
  79.     if ( e & 1 ) {
  80.       r = mul_mod(r, redb, m);
  81.     }
  82.  
  83.     // and we need to ensure that  r = (b ** e_[i..0] ) % m
  84.     // is the current bit of e set?
  85.     r = r;
  86.     redb = mul_mod(redb, redb, m);
  87.     //Repeats on the next power of b
  88.     e = e >>1;
  89.     // We shift to move things forward, when we have 0 left we are done.
  90.  
  91.  
  92.   }
  93.  
  94.   // at this point r = (b ** e) % m, we don't overflow anymore!
  95.   return r;
  96. }
  97.  
  98. void readline(char *s, int bufsize){
  99.   uint8_t i = 0;
  100.  
  101.   while( i < bufsize-1 ) {
  102.     // wait for a character
  103.     while (Serial.available() == 0) {
  104.     } /* Do nothing */
  105.  
  106.     // grab the character and save in the buffer
  107.     s[i] = Serial.read();
  108.  
  109.     // if end of line or somehow a \0 got sent, we are done
  110.     if (s[i] == '\n' || s[i] == '\0') break;
  111.     i += 1;
  112.   }
  113.   // \0 teminate the string
  114.   s[i] = '\0';
  115. }
  116.  
  117. /* readlong:
  118.  
  119.  Read a sequence characters from the serial monitor and interpret
  120.  them as a decimal integer.
  121.  
  122.  The characters can be leading and tailing blanks, a leading '-',
  123.  and the digits 0-9.
  124.  
  125.  Return that number as a 32 bit int.
  126.  */
  127. int32_t readlong()
  128. {
  129.   char s[128]; /* 128 characters should be more than enough. */
  130.   readline(s, 128);
  131.   return atol(s);
  132. }
  133.  
  134. /* Read a line (up to maxlen characters) off of the serial port. */
  135. void readPublicKey(char *s, int maxlen)
  136. {
  137.   int i = 0;
  138.  
  139.   while(1) {
  140.     while (Serial.available() == 0) {
  141.     } /* Do nothing */
  142.     s[i] = Serial.read();
  143.     // Need to search for '\r' b/c of minicom
  144.     if (s[i] == '\0' || s[i] == '\r' || s[i] == '\n' || i == maxlen-1 || s[i] == ':') break;
  145.     i += 1;
  146.   }
  147. }
  148.  
  149.  
  150. void setup()
  151. {
  152.   Serial.begin(9600);
  153.   Serial1.begin(9600);
  154.   randomSeed(get_seed());
  155.   uint32_t a = random(256); //& 0xff ;
  156.   // the & 0xff masks this from potential eavesdroppers by making our 8 bit key appear as a 32 bit one.
  157.  
  158.   Serial.println("This is my shared index a: ");
  159.   //Serial.println(pow_mod(g, a, p)); //This displays shared index on my screen.
  160.   sharedindex = (pow_mod(g, a, p));
  161.  
  162.   Serial.println(sharedindex);
  163.   delay(3000); //No need to rush, setup time for exchange.
  164.  //uint32_t go;
  165.   //go = readlong();
  166.   sendkey((uint32_t)sharedindex);
  167.   //This sends the local Shared index, as bytes, to my partner.
  168.  
  169.   // you are calling sendkey twice, is this intended?
  170.   sendkey((uint32_t)sharedindex);
  171.  
  172.     if (Serial1.available()){
  173.         union data d;
  174.         int i;
  175.         for (i = 0; i < 4; i++) {
  176.             d.components[i] = Serial1.read();
  177.         }
  178.        othersharedIndex = d.sharedindex;
  179.         Serial.print (othersharedIndex, DEC);
  180.         B = othersharedIndex;
  181.  
  182.     }
  183.    
  184.     Serial.print (othersharedIndex, DEC);
  185.         B = othersharedIndex;
  186.  
  187.  
  188.   othersharedIndex = (pow_mod(B, a, p));
  189.  
  190. }
  191.  
  192. void loop(){
  193.  
  194.  
  195.  
  196. }
Add Comment
Please, Sign In to add comment