Home | History | Annotate | Download | only in sha1
      1 // Copyright 2009 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package sha1
      6 
      7 const (
      8 	_K0 = 0x5A827999
      9 	_K1 = 0x6ED9EBA1
     10 	_K2 = 0x8F1BBCDC
     11 	_K3 = 0xCA62C1D6
     12 )
     13 
     14 // blockGeneric is a portable, pure Go version of the SHA1 block step.
     15 // It's used by sha1block_generic.go and tests.
     16 func blockGeneric(dig *digest, p []byte) {
     17 	var w [16]uint32
     18 
     19 	h0, h1, h2, h3, h4 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4]
     20 	for len(p) >= chunk {
     21 		// Can interlace the computation of w with the
     22 		// rounds below if needed for speed.
     23 		for i := 0; i < 16; i++ {
     24 			j := i * 4
     25 			w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
     26 		}
     27 
     28 		a, b, c, d, e := h0, h1, h2, h3, h4
     29 
     30 		// Each of the four 20-iteration rounds
     31 		// differs only in the computation of f and
     32 		// the choice of K (_K0, _K1, etc).
     33 		i := 0
     34 		for ; i < 16; i++ {
     35 			f := b&c | (^b)&d
     36 			a5 := a<<5 | a>>(32-5)
     37 			b30 := b<<30 | b>>(32-30)
     38 			t := a5 + f + e + w[i&0xf] + _K0
     39 			a, b, c, d, e = t, a, b30, c, d
     40 		}
     41 		for ; i < 20; i++ {
     42 			tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
     43 			w[i&0xf] = tmp<<1 | tmp>>(32-1)
     44 
     45 			f := b&c | (^b)&d
     46 			a5 := a<<5 | a>>(32-5)
     47 			b30 := b<<30 | b>>(32-30)
     48 			t := a5 + f + e + w[i&0xf] + _K0
     49 			a, b, c, d, e = t, a, b30, c, d
     50 		}
     51 		for ; i < 40; i++ {
     52 			tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
     53 			w[i&0xf] = tmp<<1 | tmp>>(32-1)
     54 			f := b ^ c ^ d
     55 			a5 := a<<5 | a>>(32-5)
     56 			b30 := b<<30 | b>>(32-30)
     57 			t := a5 + f + e + w[i&0xf] + _K1
     58 			a, b, c, d, e = t, a, b30, c, d
     59 		}
     60 		for ; i < 60; i++ {
     61 			tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
     62 			w[i&0xf] = tmp<<1 | tmp>>(32-1)
     63 			f := ((b | c) & d) | (b & c)
     64 
     65 			a5 := a<<5 | a>>(32-5)
     66 			b30 := b<<30 | b>>(32-30)
     67 			t := a5 + f + e + w[i&0xf] + _K2
     68 			a, b, c, d, e = t, a, b30, c, d
     69 		}
     70 		for ; i < 80; i++ {
     71 			tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
     72 			w[i&0xf] = tmp<<1 | tmp>>(32-1)
     73 			f := b ^ c ^ d
     74 			a5 := a<<5 | a>>(32-5)
     75 			b30 := b<<30 | b>>(32-30)
     76 			t := a5 + f + e + w[i&0xf] + _K3
     77 			a, b, c, d, e = t, a, b30, c, d
     78 		}
     79 
     80 		h0 += a
     81 		h1 += b
     82 		h2 += c
     83 		h3 += d
     84 		h4 += e
     85 
     86 		p = p[chunk:]
     87 	}
     88 
     89 	dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] = h0, h1, h2, h3, h4
     90 }
     91