Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __attribute__((vec_type_hint(uint4)))
- void sha1_chunk(__local uint *h, //Hash state (160-bit = 5*32)
- __constant uint *chunk //512-bit chunk to process
- ) {
- //Loop counter
- uchar i;
- //Working buffer
- __local uint w[80];
- __private uint4 *wv = (uint4 *)w; //Vector alias for w
- //Copy chunk into w
- for (i = 0; i < 16; i++)
- wv[i/4] = vload4(i, chunk);
- //Extend w to 80 words
- for (i = 16; i < 32; i++)
- w[i] = (w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]) << 1;
- //Explicit vector optimization for rounds 32-79
- for (i = 32; i < 80; i++)
- wv[i/4] = (vload4(i-6, wv) ^ vload4(i-16, wv) ^
- vload4(i-28, wv) ^ vload4(i-32, wv)) << 2;
- //Proper hashing
- uint a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
- uint f, k;
- #define SHUFFLE() sha1_chunk_shuffle(&a, &b, &c, &d, &e, &w[i])
- for (i = 0; i < 20; i++) {
- f = (b & c) | ((~b) & d);
- k = 0x5A827999;
- SHUFFLE();
- }
- for (i = 20; i < 40; i++) {
- f = b ^ c ^ d;
- k = 0x6ED9EBA1;
- SHUFFLE();
- }
- for (i = 40; i < 60; i++) {
- f = (b & c) | (b & d) | (c & d)
- k = 0x8F1BBCDC
- SHUFFLE();
- }
- for (i = 60; i < 80; i++) {
- f = b ^ c ^ d;
- k = 0xCA62C1D6;
- SHUFFLE();
- }
- #undef SHUFFLE()
- //Update old hash value
- h[0] += a;
- h[1] += b;
- h[2] += c;
- h[3] += d;
- h[4] += e;
- }
- void sha1_chunk_shuffle(uint *a, uint *b, uint *c, uint *d,
- uint *e, uint *f, uint *k, uint *wi) {
- uint t = (*a << 5) + *f + *e + *k + *wi;
- *e = *d;
- *d = *c;
- *c = *b << 30;
- *b = *a;
- *a = t;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement