src/pkg/crypto/sha256/sha256block.go - The Go Programming Language

Golang

Source file src/pkg/crypto/sha256/sha256block.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	// SHA256 block step.
     6	// In its own file so that a faster assembly or C version
     7	// can be substituted easily.
     8	
     9	package sha256
    10	
    11	var _K = []uint32{
    12		0x428a2f98,
    13		0x71374491,
    14		0xb5c0fbcf,
    15		0xe9b5dba5,
    16		0x3956c25b,
    17		0x59f111f1,
    18		0x923f82a4,
    19		0xab1c5ed5,
    20		0xd807aa98,
    21		0x12835b01,
    22		0x243185be,
    23		0x550c7dc3,
    24		0x72be5d74,
    25		0x80deb1fe,
    26		0x9bdc06a7,
    27		0xc19bf174,
    28		0xe49b69c1,
    29		0xefbe4786,
    30		0x0fc19dc6,
    31		0x240ca1cc,
    32		0x2de92c6f,
    33		0x4a7484aa,
    34		0x5cb0a9dc,
    35		0x76f988da,
    36		0x983e5152,
    37		0xa831c66d,
    38		0xb00327c8,
    39		0xbf597fc7,
    40		0xc6e00bf3,
    41		0xd5a79147,
    42		0x06ca6351,
    43		0x14292967,
    44		0x27b70a85,
    45		0x2e1b2138,
    46		0x4d2c6dfc,
    47		0x53380d13,
    48		0x650a7354,
    49		0x766a0abb,
    50		0x81c2c92e,
    51		0x92722c85,
    52		0xa2bfe8a1,
    53		0xa81a664b,
    54		0xc24b8b70,
    55		0xc76c51a3,
    56		0xd192e819,
    57		0xd6990624,
    58		0xf40e3585,
    59		0x106aa070,
    60		0x19a4c116,
    61		0x1e376c08,
    62		0x2748774c,
    63		0x34b0bcb5,
    64		0x391c0cb3,
    65		0x4ed8aa4a,
    66		0x5b9cca4f,
    67		0x682e6ff3,
    68		0x748f82ee,
    69		0x78a5636f,
    70		0x84c87814,
    71		0x8cc70208,
    72		0x90befffa,
    73		0xa4506ceb,
    74		0xbef9a3f7,
    75		0xc67178f2,
    76	}
    77	
    78	func _Block(dig *digest, p []byte) int {
    79		var w [64]uint32
    80		n := 0
    81		h0, h1, h2, h3, h4, h5, h6, h7 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4], dig.h[5], dig.h[6], dig.h[7]
    82		for len(p) >= _Chunk {
    83			// Can interlace the computation of w with the
    84			// rounds below if needed for speed.
    85			for i := 0; i < 16; i++ {
    86				j := i * 4
    87				w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
    88			}
    89			for i := 16; i < 64; i++ {
    90				t1 := (w[i-2]>>17 | w[i-2]<<(32-17)) ^ (w[i-2]>>19 | w[i-2]<<(32-19)) ^ (w[i-2] >> 10)
    91	
    92				t2 := (w[i-15]>>7 | w[i-15]<<(32-7)) ^ (w[i-15]>>18 | w[i-15]<<(32-18)) ^ (w[i-15] >> 3)
    93	
    94				w[i] = t1 + w[i-7] + t2 + w[i-16]
    95			}
    96	
    97			a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7
    98	
    99			for i := 0; i < 64; i++ {
   100				t1 := h + ((e>>6 | e<<(32-6)) ^ (e>>11 | e<<(32-11)) ^ (e>>25 | e<<(32-25))) + ((e & f) ^ (^e & g)) + _K[i] + w[i]
   101	
   102				t2 := ((a>>2 | a<<(32-2)) ^ (a>>13 | a<<(32-13)) ^ (a>>22 | a<<(32-22))) + ((a & b) ^ (a & c) ^ (b & c))
   103	
   104				h = g
   105				g = f
   106				f = e
   107				e = d + t1
   108				d = c
   109				c = b
   110				b = a
   111				a = t1 + t2
   112			}
   113	
   114			h0 += a
   115			h1 += b
   116			h2 += c
   117			h3 += d
   118			h4 += e
   119			h5 += f
   120			h6 += g
   121			h7 += h
   122	
   123			p = p[_Chunk:]
   124			n += _Chunk
   125		}
   126	
   127		dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4], dig.h[5], dig.h[6], dig.h[7] = h0, h1, h2, h3, h4, h5, h6, h7
   128		return n
   129	}