github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/crypto/sha1/sha1block.go (about) 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 import ( 8 "math/bits" 9 ) 10 11 const ( 12 _K0 = 0x5A827999 13 _K1 = 0x6ED9EBA1 14 _K2 = 0x8F1BBCDC 15 _K3 = 0xCA62C1D6 16 ) 17 18 // blockGeneric is a portable, pure Go version of the SHA-1 block step. 19 // It's used by sha1block_generic.go and tests. 20 func blockGeneric(dig *digest, p []byte) { 21 var w [16]uint32 22 23 h0, h1, h2, h3, h4 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] 24 for len(p) >= chunk { 25 // Can interlace the computation of w with the 26 // rounds below if needed for speed. 27 for i := 0; i < 16; i++ { 28 j := i * 4 29 w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3]) 30 } 31 32 a, b, c, d, e := h0, h1, h2, h3, h4 33 34 // Each of the four 20-iteration rounds 35 // differs only in the computation of f and 36 // the choice of K (_K0, _K1, etc). 37 i := 0 38 for ; i < 16; i++ { 39 f := b&c | (^b)&d 40 t := bits.RotateLeft32(a, 5) + f + e + w[i&0xf] + _K0 41 a, b, c, d, e = t, a, bits.RotateLeft32(b, 30), c, d 42 } 43 for ; i < 20; i++ { 44 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf] 45 w[i&0xf] = tmp<<1 | tmp>>(32-1) 46 47 f := b&c | (^b)&d 48 t := bits.RotateLeft32(a, 5) + f + e + w[i&0xf] + _K0 49 a, b, c, d, e = t, a, bits.RotateLeft32(b, 30), 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 t := bits.RotateLeft32(a, 5) + f + e + w[i&0xf] + _K1 56 a, b, c, d, e = t, a, bits.RotateLeft32(b, 30), c, d 57 } 58 for ; i < 60; i++ { 59 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf] 60 w[i&0xf] = tmp<<1 | tmp>>(32-1) 61 f := ((b | c) & d) | (b & c) 62 t := bits.RotateLeft32(a, 5) + f + e + w[i&0xf] + _K2 63 a, b, c, d, e = t, a, bits.RotateLeft32(b, 30), c, d 64 } 65 for ; i < 80; i++ { 66 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf] 67 w[i&0xf] = tmp<<1 | tmp>>(32-1) 68 f := b ^ c ^ d 69 t := bits.RotateLeft32(a, 5) + f + e + w[i&0xf] + _K3 70 a, b, c, d, e = t, a, bits.RotateLeft32(b, 30), c, d 71 } 72 73 h0 += a 74 h1 += b 75 h2 += c 76 h3 += d 77 h4 += e 78 79 p = p[chunk:] 80 } 81 82 dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] = h0, h1, h2, h3, h4 83 }