Advertisement
Guest User

Untitled

a guest
Sep 11th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.35 KB | None | 0 0
  1. /* sha funzona hmac no */
  2.  
  3.  
  4. #define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - c) ++b; a += c;
  5.  
  6. #include <math.h>
  7. #include <string.h>
  8.  
  9. class sha1 {
  10. private :
  11.   unsigned long H[5] = {
  12.     0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0              };
  13.   unsigned long k[4] = {
  14.     0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6              };
  15.   unsigned char buffer[64] = {
  16.     0              };
  17.  
  18.   unsigned long words[80] = {
  19.     0              };
  20.  
  21.   void input(char *);
  22.   void process();
  23.   void transform();
  24.   void final();
  25.  
  26.   unsigned long shift(unsigned long, int);
  27.   unsigned long hash[5];
  28.  
  29.  
  30. public :
  31.   sha1(char *);
  32.   unsigned long * get_hash();
  33.   void get_string_hash(unsigned char *);
  34. };
  35.  
  36. class hmacSha1 {
  37. private :
  38.   unsigned long * hash;
  39.  
  40. public :
  41.   hmacSha1(char *, char *);
  42.   unsigned long * get_hash();
  43.  
  44. };
  45.  
  46. hmacSha1::hmacSha1(char * key, char * message) {
  47.   unsigned char * opad, * ipad, * message_sha;
  48.  
  49.   opad = (unsigned char *)malloc(20 * sizeof(unsigned char));
  50.  
  51.   ipad = (unsigned char *)malloc((64+strlen(message)) * sizeof(unsigned char));
  52.   message_sha = (unsigned char *)malloc((strlen(message)) * sizeof(unsigned char));
  53.  
  54.   char hmac_key[64] = {
  55.     0  };
  56.    
  57.   unsigned long * temp_hash;
  58.  
  59.   char temp_sha[20];
  60.  
  61.   int i;
  62.  
  63.   if(strlen(key)>64) {
  64.     sha1 sha_obj(key);
  65.     temp_hash = sha_obj.get_hash();
  66.     memcpy(hmac_key, temp_hash, 20);
  67.   }
  68.  
  69.   if(strlen(key)<64) {
  70.     for(i=0;i<strlen(key);i++) {  
  71.       hmac_key[i] = key[i];
  72.     }
  73.   }
  74.  
  75.  
  76.    
  77.  
  78.  
  79.   for(i=0;i<strlen(message);i++) {
  80.     message_sha[i] = message[i];
  81.   }
  82.  
  83.   for(i=0;i<64;i++) {
  84.     ipad[i] = hmac_key[i] ^ 0x5c;
  85.     opad[i] = hmac_key[i] ^ 0x36;
  86.   }
  87.  
  88.   memcpy(ipad+64, message_sha, strlen(message));
  89.  
  90.   sha1 iash((char *)ipad);
  91.  
  92.   for(i=0;i<64+strlen(message);i++) {
  93.   Serial.print(ipad[i], HEX);
  94.   }
  95.  
  96.   Serial.println();
  97.  
  98.  
  99.  
  100.   temp_hash = iash.get_hash();
  101.  
  102.  
  103.  
  104.   /*memcpy(opad+64, temp_hash, 20);
  105.  
  106.  
  107.  
  108.   sha1 oash((char *)opad);*/
  109.   hash = iash.get_hash();
  110.  
  111. }
  112.  
  113. unsigned long * hmacSha1::get_hash() {
  114.   return hash;
  115. }
  116.  
  117. void setup() {
  118.   unsigned long * hash;
  119.   unsigned char string[20];
  120.   int i;
  121.   Serial.begin(9600);
  122.  
  123.   hmacSha1 test("ciao", "ciao");
  124.   hash = test.get_hash();
  125.  
  126.   for(i=0;i<5;i++) {
  127.     Serial.print(hash[i], HEX);
  128.   }
  129.  
  130.  
  131. }
  132.  
  133.  
  134. void loop() {
  135.  
  136.  
  137. }
  138.  
  139.  
  140.  
  141. sha1::sha1(char * message) {
  142.  
  143.  
  144.   input(message);
  145.  
  146.  
  147. }
  148.  
  149. unsigned long * sha1::get_hash() {
  150.   return H;
  151. }
  152.  
  153. void sha1::get_string_hash(unsigned char * string) {
  154.   memcpy(string, H, 20);
  155. }
  156.  
  157. void sha1::input(char * message) {
  158.   int length = strlen(message);
  159.  
  160.   int i,j;
  161.   unsigned long size64[2]={
  162.     0              };
  163.  
  164.   j=0;
  165.   for(i=0; i<length;i++) {    
  166.     buffer[j] = message[i];
  167.  
  168.     /*Serial.print(j);
  169.      Serial.print(" - ");
  170.      Serial.println((char)buffer[j]);*/
  171.  
  172.     if (j == 63) {
  173.  
  174.       process();
  175.       memset(buffer,0,64);
  176.       j=0;
  177.     }
  178.     else {
  179.       j++;
  180.     }
  181.   }
  182.  
  183.  
  184.  
  185.  
  186.  
  187.   if(j<56) {
  188.     buffer[j] = 0x80;
  189.     j++;
  190.     while(j<56) {
  191.       buffer[j]=0x00;
  192.       j++;
  193.     }
  194.  
  195.   }
  196.   else {
  197.     buffer[j] = 0x80;
  198.     j++;
  199.     while(j<64) {
  200.       buffer[j]=0x00;
  201.       j++;
  202.     }
  203.  
  204.     /*for(i=0; i<64;i++) {    
  205.      Serial.println(buffer[i],BIN);
  206.      }*/
  207.  
  208.     process();
  209.     memset(buffer,0,56);
  210.   }
  211.  
  212.   DBL_INT_ADD(size64[0],size64[1],8 * length);
  213.   buffer[63] = size64[0];
  214.   buffer[62] = size64[0] >> 8;
  215.   buffer[61] = size64[0] >> 16;
  216.   buffer[60] = size64[0] >> 24;
  217.   buffer[59] = size64[1];
  218.   buffer[58] = size64[1] >> 8;
  219.   buffer[57] = size64[1] >> 16;  
  220.   buffer[56] = size64[1] >> 24;
  221.  
  222.   /*for(i=0;i<64;i++) {
  223.    Serial.println(buffer[i], BIN);
  224.    }*/
  225.  
  226.   process();
  227.  
  228.  
  229.  
  230. }
  231.  
  232. void sha1::process() {
  233.   int i;
  234.  
  235.   unsigned long a,b,c,d;
  236.  
  237.  
  238.   for(i=0;i<16;i++) {  
  239.     a = buffer[i*4];
  240.     b = buffer[i*4+1];
  241.     c = buffer[i*4+2];
  242.     d = buffer[i*4+3];
  243.  
  244.     words[i] = (a << 24) + (b << 16) + (c << 8) + d;
  245.   }
  246.  
  247.   /*for(i=0;i<16;i++) {
  248.    Serial.println(words[i],BIN);
  249.    }*/
  250.  
  251.   for(i=16;i<80;i++) {
  252.     words[i] = shift(words[i-3] ^ words[i-8] ^ words[i-14] ^ words[i-16], 1);
  253.   }
  254.  
  255.   //Serial.println(words[35], BIN);
  256.  
  257.   transform();
  258. }
  259.  
  260. void sha1::transform() {
  261.   int i;
  262.   unsigned long a, b, c, d, e, t;
  263.   a = H[0];
  264.   b = H[1];
  265.   c = H[2];
  266.   d = H[3];
  267.   e = H[4];
  268.  
  269.  
  270.  
  271.   for (i=0; i < 20; i++) {
  272.     t = shift(a,5) + ((b & c) ^ (~b & d)) + e + k[0] + words[i];
  273.     e = d;
  274.     d = c;
  275.     c = shift(b,30);
  276.     b = a;
  277.     a = t;
  278.   }  
  279.   for ( ; i < 40; i++) {
  280.     t = shift(a,5) + (b ^ c ^ d) + e + k[1] + words[i];
  281.     e = d;
  282.     d = c;
  283.     c = shift(b,30);
  284.     b = a;
  285.     a = t;
  286.   }  
  287.   for ( ; i < 60; i++) {
  288.     t = shift(a,5) + ((b & c) ^ (b & d) ^ (c & d))  + e + k[2] + words[i];
  289.     e = d;
  290.     d = c;
  291.     c = shift(b,30);
  292.     b = a;
  293.     a = t;
  294.   }  
  295.   for ( ; i < 80; i++) {
  296.     t = shift(a,5) + (b ^ c ^ d) + e + k[3] + words[i];
  297.     e = d;
  298.     d = c;
  299.     c = shift(b,30);
  300.     b = a;
  301.     a = t;
  302.   }
  303.  
  304.   H[0] += a;
  305.   H[1] += b;
  306.   H[2] += c;
  307.   H[3] += d;
  308.   H[4] += e;
  309. }
  310.  
  311. void sha1::final() {
  312.   /*int i;
  313.    for(i=0;i<5;i++) {
  314.    Serial.println(H[i], HEX);
  315.    }*/
  316. }
  317.  
  318. unsigned long sha1::shift(unsigned long word_input, int bits) {
  319.   return (word_input << bits) | ((word_input) >> (32-bits));
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement