src/pkg/crypto/sha1/sha1block.go - The Go Programming Language

Golang

Source file src/pkg/crypto/sha1/sha1block.go

     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	// SHA1 block step.
     6	// In its own file so that a faster assembly or C version
     7	// can be substituted easily.
     8	
     9	package sha1
    10	
    11	const (
    12		_K0 = 0x5A827999
    13		_K1 = 0x6ED9EBA1
    14		_K2 = 0x8F1BBCDC
    15		_K3 = 0xCA62C1D6
    16	)
    17	
    18	func _Block(dig *digest, p []byte) int {
    19		var w [80]uint32
    20	
    21		n := 0
    22		h0, h1, h2, h3, h4 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4]
    23		for len(p) >= _Chunk {
    24			// Can interlace the computation of w with the
    25			// rounds below if needed for speed.
    26			for i := 0; i < 16; i++ {
    27				j := i * 4
    28				w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
    29			}
    30			for i := 16; i < 80; i++ {
    31				tmp := w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]
    32				w[i] = tmp<<1 | tmp>>(32-1)
    33			}
    34	
    35			a, b, c, d, e := h0, h1, h2, h3, h4
    36	
    37			// Each of the four 20-iteration rounds
    38			// differs only in the computation of f and
    39			// the choice of K (_K0, _K1, etc).
    40			for i := 0; i < 20; i++ {
    41				f := b&c | (^b)&d
    42				a5 := a<<5 | a>>(32-5)
    43				b30 := b<<30 | b>>(32-30)
    44				t := a5 + f + e + w[i] + _K0
    45				a, b, c, d, e = t, a, b30, c, d
    46			}
    47			for i := 20; i < 40; i++ {
    48				f := b ^ c ^ d
    49				a5 := a<<5 | a>>(32-5)
    50				b30 := b<<30 | b>>(32-30)
    51				t := a5 + f + e + w[i] + _K1
    52				a, b, c, d, e = t, a, b30, c, d
    53			}
    54			for i := 40; i < 60; i++ {
    55				f := b&c | b&d | c&d
    56				a5 := a<<5 | a>>(32-5)
    57				b30 := b<<30 | b>>(32-30)
    58				t := a5 + f + e + w[i] + _K2
    59				a, b, c, d, e = t, a, b30, c, d
    60			}
    61			for i := 60; i < 80; i++ {
    62				f := b ^ c ^ d
    63				a5 := a<<5 | a>>(32-5)
    64				b30 := b<<30 | b>>(32-30)
    65				t := a5 + f + e + w[i] + _K3
    66				a, b, c, d, e = t, a, b30, c, d
    67			}
    68	
    69			h0 += a
    70			h1 += b
    71			h2 += c
    72			h3 += d
    73			h4 += e
    74	
    75			p = p[_Chunk:]
    76			n += _Chunk
    77		}
    78	
    79		dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] = h0, h1, h2, h3, h4
    80		return n
    81	}