github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/sm3/sm3block_soft.go (about)

     1  // Copyright 2022 s1ren@github.com/hxx258456.
     2  
     3  //go:build !amd64 && !arm64
     4  // +build !amd64,!arm64
     5  
     6  package sm3
     7  
     8  /*
     9  sm3/sm3block_soft.go sm3的block相关处理的纯软实现,仅在非amd64、非arm64架构平台上使用。
    10  */
    11  
    12  import (
    13  	"fmt"
    14  	"math/bits"
    15  	"runtime"
    16  )
    17  
    18  func init() {
    19  	cpuType = runtime.GOARCH
    20  	// fmt.Printf("该平台CPU架构: %s , SM3的块处理采用纯软实现。", cpuType)
    21  }
    22  
    23  func block(dig *digest, p []byte) {
    24  	blockSoft(dig, p)
    25  }
    26  
    27  var _T = []uint32{
    28  	0x79cc4519,
    29  	0x7a879d8a,
    30  }
    31  
    32  func p0(x uint32) uint32 {
    33  	return x ^ bits.RotateLeft32(x, 9) ^ bits.RotateLeft32(x, 17)
    34  }
    35  
    36  func p1(x uint32) uint32 {
    37  	return x ^ bits.RotateLeft32(x, 15) ^ bits.RotateLeft32(x, 23)
    38  }
    39  
    40  func ff(x, y, z uint32) uint32 {
    41  	return (x & y) | (x & z) | (y & z)
    42  }
    43  
    44  func gg(x, y, z uint32) uint32 {
    45  	return (x & y) | (^x & z)
    46  }
    47  
    48  func blockSoft(dig *digest, p []byte) {
    49  	fmt.Println("SM3散列纯软实现...")
    50  	var w [68]uint32
    51  	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]
    52  	for len(p) >= chunk {
    53  		for i := 0; i < 4; i++ {
    54  			j := i * 4
    55  			w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
    56  		}
    57  		a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7
    58  		for i := 0; i < 12; i++ {
    59  			j := (i + 4) * 4
    60  			w[i+4] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
    61  			ss1 := bits.RotateLeft32(bits.RotateLeft32(a, 12)+e+bits.RotateLeft32(_T[0], i), 7)
    62  			ss2 := ss1 ^ bits.RotateLeft32(a, 12)
    63  			tt1 := a ^ b ^ c + d + ss2 + (w[i] ^ w[i+4])
    64  			tt2 := e ^ f ^ g + h + ss1 + w[i]
    65  			d = c
    66  			c = bits.RotateLeft32(b, 9)
    67  			b = a
    68  			a = tt1
    69  			h = g
    70  			g = bits.RotateLeft32(f, 19)
    71  			f = e
    72  			e = p0(tt2)
    73  		}
    74  
    75  		for i := 12; i < 16; i++ {
    76  			w[i+4] = p1(w[i-12]^w[i-5]^bits.RotateLeft32(w[i+1], 15)) ^ bits.RotateLeft32(w[i-9], 7) ^ w[i-2]
    77  			ss1 := bits.RotateLeft32(bits.RotateLeft32(a, 12)+e+bits.RotateLeft32(_T[0], i), 7)
    78  			ss2 := ss1 ^ bits.RotateLeft32(a, 12)
    79  			tt1 := a ^ b ^ c + d + ss2 + (w[i] ^ w[i+4])
    80  			tt2 := e ^ f ^ g + h + ss1 + w[i]
    81  			d = c
    82  			c = bits.RotateLeft32(b, 9)
    83  			b = a
    84  			a = tt1
    85  			h = g
    86  			g = bits.RotateLeft32(f, 19)
    87  			f = e
    88  			e = p0(tt2)
    89  		}
    90  
    91  		for i := 16; i < 64; i++ {
    92  			w[i+4] = p1(w[i-12]^w[i-5]^bits.RotateLeft32(w[i+1], 15)) ^ bits.RotateLeft32(w[i-9], 7) ^ w[i-2]
    93  			ss1 := bits.RotateLeft32(bits.RotateLeft32(a, 12)+e+bits.RotateLeft32(_T[1], i), 7)
    94  			ss2 := ss1 ^ bits.RotateLeft32(a, 12)
    95  			tt1 := ff(a, b, c) + d + ss2 + (w[i] ^ w[i+4])
    96  			tt2 := gg(e, f, g) + h + ss1 + w[i]
    97  
    98  			d = c
    99  			c = bits.RotateLeft32(b, 9)
   100  			b = a
   101  			a = tt1
   102  			h = g
   103  			g = bits.RotateLeft32(f, 19)
   104  			f = e
   105  			e = p0(tt2)
   106  		}
   107  		h0 ^= a
   108  		h1 ^= b
   109  		h2 ^= c
   110  		h3 ^= d
   111  		h4 ^= e
   112  		h5 ^= f
   113  		h6 ^= g
   114  		h7 ^= h
   115  		p = p[chunk:]
   116  	}
   117  	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
   118  }