github.com/insolar/x-crypto@v0.0.0-20191031140942-75fab8a325f6/sha256/sha256block.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  // 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 blockGeneric(dig *digest, p []byte) {
    79  	var w [64]uint32
    80  	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]
    81  	for len(p) >= chunk {
    82  		// Can interlace the computation of w with the
    83  		// rounds below if needed for speed.
    84  		for i := 0; i < 16; i++ {
    85  			j := i * 4
    86  			w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
    87  		}
    88  		for i := 16; i < 64; i++ {
    89  			v1 := w[i-2]
    90  			t1 := (v1>>17 | v1<<(32-17)) ^ (v1>>19 | v1<<(32-19)) ^ (v1 >> 10)
    91  			v2 := w[i-15]
    92  			t2 := (v2>>7 | v2<<(32-7)) ^ (v2>>18 | v2<<(32-18)) ^ (v2 >> 3)
    93  			w[i] = t1 + w[i-7] + t2 + w[i-16]
    94  		}
    95  
    96  		a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7
    97  
    98  		for i := 0; i < 64; i++ {
    99  			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]
   100  
   101  			t2 := ((a>>2 | a<<(32-2)) ^ (a>>13 | a<<(32-13)) ^ (a>>22 | a<<(32-22))) + ((a & b) ^ (a & c) ^ (b & c))
   102  
   103  			h = g
   104  			g = f
   105  			f = e
   106  			e = d + t1
   107  			d = c
   108  			c = b
   109  			b = a
   110  			a = t1 + t2
   111  		}
   112  
   113  		h0 += a
   114  		h1 += b
   115  		h2 += c
   116  		h3 += d
   117  		h4 += e
   118  		h5 += f
   119  		h6 += g
   120  		h7 += h
   121  
   122  		p = p[chunk:]
   123  	}
   124  
   125  	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
   126  }