Advertisement
tari

tari

Dec 7th, 2010
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1.  
  2. __attribute__((vec_type_hint(uint4)))
  3. void sha1_chunk(__local uint *h,       //Hash state (160-bit = 5*32)
  4.            __constant uint *chunk      //512-bit chunk to process
  5.            ) {
  6.     //Loop counter
  7.     uchar i;
  8.     //Working buffer
  9.     __local uint w[80];
  10.     __private uint4 *wv = (uint4 *)w;   //Vector alias for w
  11.    
  12.     //Copy chunk into w
  13.     for (i = 0; i < 16; i++)
  14.         wv[i/4] = vload4(i, chunk);
  15.  
  16.     //Extend w to 80 words
  17.     for (i = 16; i < 32; i++)
  18.         w[i] = (w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]) << 1;
  19.     //Explicit vector optimization for rounds 32-79
  20.     for (i = 32; i < 80; i++)
  21.         wv[i/4] = (vload4(i-6, wv) ^ vload4(i-16, wv) ^
  22.                    vload4(i-28, wv) ^ vload4(i-32, wv)) << 2;
  23.  
  24.     //Proper hashing
  25.     uint a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
  26.     uint f, k;
  27. #define SHUFFLE() sha1_chunk_shuffle(&a, &b, &c, &d, &e, &w[i])
  28.     for (i = 0; i < 20; i++) {
  29.         f = (b & c) | ((~b) & d);
  30.         k = 0x5A827999;
  31.         SHUFFLE();
  32.     }
  33.     for (i = 20; i < 40; i++) {
  34.         f = b ^ c ^ d;
  35.         k = 0x6ED9EBA1;
  36.         SHUFFLE();
  37.     }
  38.     for (i = 40; i < 60; i++) {
  39.         f = (b & c) | (b & d) | (c & d)
  40.         k = 0x8F1BBCDC
  41.         SHUFFLE();
  42.     }
  43.     for (i = 60; i < 80; i++) {
  44.         f = b ^ c ^ d;
  45.         k = 0xCA62C1D6;
  46.         SHUFFLE();
  47.     }
  48. #undef SHUFFLE()
  49.  
  50.     //Update old hash value
  51.     h[0] += a;
  52.     h[1] += b;
  53.     h[2] += c;
  54.     h[3] += d;
  55.     h[4] += e;
  56. }
  57.  
  58. void sha1_chunk_shuffle(uint *a, uint *b, uint *c, uint *d,
  59.                         uint *e, uint *f, uint *k, uint *wi) {
  60.     uint t = (*a << 5) + *f + *e + *k + *wi;
  61.     *e = *d;
  62.     *d = *c;
  63.     *c = *b << 30;
  64.     *b = *a;
  65.     *a = t;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement