gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/sm3/sm3block_soft.go (about)

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