gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/sm3/sm3block_soft.go (about)

     1  // Copyright (c) 2022 zhaochun
     2  // core-gm is licensed under Mulan PSL v2.
     3  // You can use this software according to the terms and conditions of the Mulan PSL v2.
     4  // You may obtain a copy of Mulan PSL v2 at:
     5  //          http://license.coscl.org.cn/MulanPSL2
     6  // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
     7  // See the Mulan PSL v2 for more details.
     8  
     9  //go:build !amd64 && !arm64
    10  // +build !amd64,!arm64
    11  
    12  package sm3
    13  
    14  /*
    15  sm3/sm3block_soft.go sm3的block相关处理的纯软实现,仅在非amd64、非arm64架构平台上使用。
    16  */
    17  
    18  import (
    19  	"math/bits"
    20  	"runtime"
    21  )
    22  
    23  func init() {
    24  	cpuType = runtime.GOARCH
    25  	// fmt.Printf("该平台CPU架构: %s , SM3的块处理采用纯软实现。", cpuType)
    26  }
    27  
    28  func block(dig *digest, p []byte) {
    29  	blockSoft(dig, p)
    30  }
    31  
    32  var _T = []uint32{
    33  	0x79cc4519,
    34  	0x7a879d8a,
    35  }
    36  
    37  func p0(x uint32) uint32 {
    38  	return x ^ bits.RotateLeft32(x, 9) ^ bits.RotateLeft32(x, 17)
    39  }
    40  
    41  func p1(x uint32) uint32 {
    42  	return x ^ bits.RotateLeft32(x, 15) ^ bits.RotateLeft32(x, 23)
    43  }
    44  
    45  func ff(x, y, z uint32) uint32 {
    46  	return (x & y) | (x & z) | (y & z)
    47  }
    48  
    49  func gg(x, y, z uint32) uint32 {
    50  	return (x & y) | (^x & z)
    51  }
    52  
    53  func blockSoft(dig *digest, p []byte) {
    54  	//fmt.Println("SM3散列纯软实现...")
    55  	var w [68]uint32
    56  	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]
    57  	for len(p) >= chunk {
    58  		for i := 0; i < 4; i++ {
    59  			j := i * 4
    60  			w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
    61  		}
    62  		a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7
    63  		for i := 0; i < 12; i++ {
    64  			j := (i + 4) * 4
    65  			w[i+4] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[j+2])<<8 | uint32(p[j+3])
    66  			ss1 := bits.RotateLeft32(bits.RotateLeft32(a, 12)+e+bits.RotateLeft32(_T[0], i), 7)
    67  			ss2 := ss1 ^ bits.RotateLeft32(a, 12)
    68  			tt1 := a ^ b ^ c + d + ss2 + (w[i] ^ w[i+4])
    69  			tt2 := e ^ f ^ g + h + ss1 + w[i]
    70  			d = c
    71  			c = bits.RotateLeft32(b, 9)
    72  			b = a
    73  			a = tt1
    74  			h = g
    75  			g = bits.RotateLeft32(f, 19)
    76  			f = e
    77  			e = p0(tt2)
    78  		}
    79  
    80  		for i := 12; i < 16; i++ {
    81  			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]
    82  			ss1 := bits.RotateLeft32(bits.RotateLeft32(a, 12)+e+bits.RotateLeft32(_T[0], i), 7)
    83  			ss2 := ss1 ^ bits.RotateLeft32(a, 12)
    84  			tt1 := a ^ b ^ c + d + ss2 + (w[i] ^ w[i+4])
    85  			tt2 := e ^ f ^ g + h + ss1 + w[i]
    86  			d = c
    87  			c = bits.RotateLeft32(b, 9)
    88  			b = a
    89  			a = tt1
    90  			h = g
    91  			g = bits.RotateLeft32(f, 19)
    92  			f = e
    93  			e = p0(tt2)
    94  		}
    95  
    96  		for i := 16; i < 64; i++ {
    97  			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]
    98  			ss1 := bits.RotateLeft32(bits.RotateLeft32(a, 12)+e+bits.RotateLeft32(_T[1], i), 7)
    99  			ss2 := ss1 ^ bits.RotateLeft32(a, 12)
   100  			tt1 := ff(a, b, c) + d + ss2 + (w[i] ^ w[i+4])
   101  			tt2 := gg(e, f, g) + h + ss1 + w[i]
   102  
   103  			d = c
   104  			c = bits.RotateLeft32(b, 9)
   105  			b = a
   106  			a = tt1
   107  			h = g
   108  			g = bits.RotateLeft32(f, 19)
   109  			f = e
   110  			e = p0(tt2)
   111  		}
   112  		h0 ^= a
   113  		h1 ^= b
   114  		h2 ^= c
   115  		h3 ^= d
   116  		h4 ^= e
   117  		h5 ^= f
   118  		h6 ^= g
   119  		h7 ^= h
   120  		p = p[chunk:]
   121  	}
   122  	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
   123  }