On Tue, Jul 12, 2011 at 2:03 AM, Andy Bennett <[email protected]> wrote:
> Hi,
>
>> Probably not the normal use for this channel, but *shrug*, can't hurt.
>>
>> I was attempting to code a sha256 hash function, for hashing a
>> password before sending it over the open net.  (Sounds like it's
>> better then md5 for this)
>>
>> getting started, I found some psedocode at
>> http://en.wikipedia.org/wiki/SHA2 and went to work.
>>
>> decided a blank string would be the best,
>>
>> It gives me
>> 74525b2e06b6cfebaa347250d2a6c6c9a5438fbbd4b44ffefe68dcdd7b1d1206
>>
>> but according to wikipedia above, it should be
>> e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
>
> As you've not included the code I can't really help but I thought you
> might like to have a read of these in relation to passwords and hashing:
>
> Hashing secrets, salting and MAC
> http://benlog.com/articles/2008/06/19/dont-hash-secrets/
>
> bcrypt
> http://www.usenix.org/events/usenix99/provos/provos_html/node1.html
>
> http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html
>
>
>
>
> Regards,
> @ndy

could have sworn I attached it (also, finally fixed it this morning,
found a javascript version, so I compared our variables each
iteration).

had the h[0]+=a; h[1]+=b; in the loop, when it was suppose to be after
the loop, as well as some endian issues with the data.  At least it
works

Thanks for the links,  new territory for me at the moment.  (and while
I hate to say it, my first solution's probably not going to be the
best).

 (originally planning on md5,  but sha2 sounded like a more secure solution.  )

I wanted to use it for client/server login's between 2 C programs.
Looks like doing some research on hmac-sha256 would have some benefit.

Fingerprints on files, sha256 should serve this fine.

also tossing around the idea of doing a hash on each packet (something
quick & simple).  A way to identify it was most likely something I
should process.  [or put some pattern into the packet...].  That way,
if some random program like firefox connected to the server, then It
is aware of it.  Not sure if a hash is good, or if I should just toss
in a pattern like 0xa8e2 in the packet as a fingerprint.



hmac, hmm...
server has the sha256 hash'd password
client has the sha256 hash'd password

server generates 64bit of random data as a key, send to client.

on client&server
2 64bit array's, o_key_pad, and i_key_pad, filled with 0x5c and 0x36
xor w/ key
return sha256(o_key_pad || hash(i_key_pad || sha256_password));


wonder if there is a way around sending the key from the server to the
client...  (This key would be unique for each client, discarded when
logged out).  My main concern is that the password cannot be generated
from the data sent from the client to the server.

-- 
Nathan Coulson (conathan)
------
Location: British Columbia, Canada
Timezone: PST (-8)
Webpage: http://www.nathancoulson.com
#include <stdio.h>
#include <string.h>
#include <stdint.h>
//created from http://en.wikipedia.org/wiki/SHA2
unsigned int k[64]={
   0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
   0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
   0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
   0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
   0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
   0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
   0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
   0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};


unsigned int rRt(unsigned int i, unsigned char j) {
  return (i>>j) | (i << (32-j));
}

void endianFlip(unsigned int *w, int j) {
  unsigned char c[4];
  int i;
  for(i=0; i<j; i++) {
    *(int*)c=w[i];
    w[i]= c[0]<<24 | c[1]<<16 | c[2]<<8 | c[3];
  }
}
void sha256S(unsigned int hsh[8], char *str, int length) {
  int i;
  unsigned int a,b,c,d,e,f,g,h;
  unsigned int s0, s1, maj, t1,t2,ch;
  unsigned int w[64];


  uint64_t tl=length*8;
  hsh[0]=0x6a09e667; hsh[1]=0xbb67ae85; hsh[2]=0x3c6ef372; hsh[3]=0xa54ff53a;
  hsh[4]=0x510e527f; hsh[5]=0x9b05688c; hsh[6]=0x1f83d9ab; hsh[7]=0x5be0cd19;

  while(length>=-1) {
    if(length>=64) {
      memcpy(w, str, 64);
      length-=64; str+=64;
    } else if (length>56) {
      *(length+(char*)w)=0x80;
      memset(length+1+(char*)w, 0, 64-length-1);
      memcpy(w, str, length);
      length=-1;
    } else {
      if(length>=0)
        *(length+(char*)w)=0x80;
      memset(length+1+(char*)w, 0, 56-length-1);
      if(length>0)
        memcpy(w, str, length);
        for(i=0; i<8; i++) //add length
          ((char*)w)[63-i]=((char*)&tl)[i];
      length=-2;

    }
    endianFlip(w,64);
    //extend to 64 32bit words
    for(i=16; i<64; i++) {
      s0= rRt(w[i-15], 7) ^ rRt(w[i-15],18) ^ (w[i-15]>>3);
      s1= rRt(w[i-2], 17) ^ rRt(w[i-2],19) ^ (w[i-2]>>10);
      w[i]=w[i-16]+s0+w[i-7]+s1;
    } 
    a=hsh[0]; b=hsh[1]; c=hsh[2]; d=hsh[3];
    e=hsh[4]; f=hsh[5]; g=hsh[6]; h=hsh[7];
    //main loop
    for(i=0; i<64; i++) {
      s0=rRt(a,2) ^ rRt(a,13) ^ rRt(a,22);
      maj=(a&b) ^ (a&c) ^ (b&c);
      t2=s0+maj;
      s1=rRt(e,6) ^ rRt(e,11) ^ rRt(e, 25);
      ch=(e&f) ^ ((~e)&g);
      t1=h+s1+ch+k[i]+w[i];

      h=g;
      g=f;
      f=e;
      e=d+t1;
      d=c;
      c=b;
      b=a;
      a=t1+t2;
    }
    hsh[0]+=a;
    hsh[1]+=b;
    hsh[2]+=c;
    hsh[3]+=d;
    hsh[4]+=e;
    hsh[5]+=f;
    hsh[6]+=g;
    hsh[7]+=h;
  }
}

int main() {
  int i;
  char *str="d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e532243";
  unsigned int z=0x98765432;
  unsigned int h[8];

  sha256S(h, str, strlen(str));

  fprintf(stderr, "%s\n", str);
  for(i=0; i<8; i++) {
    fprintf(stderr, "%08x", h[i]);
  }
  fprintf(stderr, "\n");
}
-- 
http://linuxfromscratch.org/mailman/listinfo/lfs-chat
FAQ: http://www.linuxfromscratch.org/faq/
Unsubscribe: See the above information page

Reply via email to