github.com/emmansun/gmsm@v0.29.1/sm4/gcm_ppc64x_test.go (about)

     1  // Copyright 2024 Sun Yimin. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build (ppc64 || ppc64le) && !purego
     6  
     7  package sm4
     8  
     9  import (
    10  	"encoding/binary"
    11  	"fmt"
    12  	"runtime"
    13  	"testing"
    14  )
    15  
    16  func TestCmul(t *testing.T) {
    17  	key := make([]byte, 16)
    18  	c1 := &sm4CipherAsm{sm4Cipher{}, 4, 4 * BlockSize}
    19  	expandKeyAsm(&key[0], &ck[0], &c1.enc[0], &c1.dec[0], INST_AES)
    20  	hle := make([]byte, gcmBlockSize)
    21  	c1.Encrypt(hle, hle)
    22  	if fmt.Sprintf("%x", hle) != "9f1f7bff6f5511384d9430531e538fd3" {
    23  		t.Errorf("1 got %x", hle)
    24  	}
    25  	var h1, h2 uint64
    26  	// Reverse the bytes in each 8 byte chunk
    27  	// Load little endian, store big endian
    28  	if runtime.GOARCH == "ppc64le" {
    29  		h1 = binary.LittleEndian.Uint64(hle[:8])
    30  		h2 = binary.LittleEndian.Uint64(hle[8:])
    31  	} else {
    32  		h1 = binary.BigEndian.Uint64(hle[:8])
    33  		h2 = binary.BigEndian.Uint64(hle[8:])
    34  	}
    35  	binary.BigEndian.PutUint64(hle[:8], h1)
    36  	binary.BigEndian.PutUint64(hle[8:], h2)
    37  
    38  	if fmt.Sprintf("%x", hle) != "3811556fff7b1f9fd38f531e5330944d" {
    39  		t.Errorf("2 got %x", hle)
    40  	}
    41  	aead, _ := c1.NewGCM(12, 16)
    42  	if runtime.GOARCH == "ppc64le" {
    43  		for i := 0; i < 16; i++ {
    44  			if fmt.Sprintf("%x", aead.(*gcmAsm).productTable[i*16:(i+1)*16]) != table[i] {
    45  				t.Errorf("productTable %v got %x", i, aead.(*gcmAsm).productTable[i*16:(i+1)*16])
    46  			}
    47  		}
    48  	}
    49  }
    50  
    51  var table = [16]string{
    52  	"000000000000000000000000000000c2",
    53  	"0000000000000000a71fa73ca660289b",
    54  	"a71fa73ca660289b7022aadefef73efc",
    55  	"7022aadefef73efc0000000000000000",
    56  	"00000000000000009208acefd693f27f",
    57  	"9208acefd693f27fc7223dce2c483080",
    58  	"c7223dce2c4830800000000000000000",
    59  	"000000000000000095c5b74db0d6c213",
    60  	"95c5b74db0d6c213c8984b421897287c",
    61  	"c8984b421897287c0000000000000000",
    62  	"000000000000000050a174a4b5189613",
    63  	"50a174a4b518961329a304696d059054",
    64  	"29a304696d0590540000000000000000",
    65  	"00000000000000000000000000000000",
    66  	"00000000000000000000000000000000",
    67  	"00000000000000000000000000000000",
    68  }