github.com/emmansun/gmsm@v0.29.1/zuc/eia_asm_ppc64x.s (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  #include "textflag.h"
     8  
     9  DATA ·rcon+0x00(SB)/8, $0x0706050403020100 // Permute for vector doubleword endian swap
    10  DATA ·rcon+0x08(SB)/8, $0x0f0e0d0c0b0a0908
    11  DATA ·rcon+0x10(SB)/8, $0x0f0f0f0f0f0f0f0f // bit_reverse_and_table
    12  DATA ·rcon+0x18(SB)/8, $0x0f0f0f0f0f0f0f0f
    13  DATA ·rcon+0x20(SB)/8, $0x0008040c020a060e // bit_reverse_table_l
    14  DATA ·rcon+0x28(SB)/8, $0x0109050d030b070f // bit_reverse_table_l
    15  DATA ·rcon+0x30(SB)/8, $0x0000000010111213 // data mask
    16  DATA ·rcon+0x38(SB)/8, $0x0000000014151617 // data mask
    17  DATA ·rcon+0x40(SB)/8, $0x0000000018191a1b // data mask
    18  DATA ·rcon+0x48(SB)/8, $0x000000001c1d1e1f // data mask
    19  DATA ·rcon+0x50(SB)/8, $0x0405060708090a0b // ks mask
    20  DATA ·rcon+0x58(SB)/8, $0x0001020304050607 // ks mask
    21  GLOBL ·rcon(SB), RODATA, $96
    22  
    23  #define XTMP1 V0
    24  #define XTMP2 V1
    25  #define XTMP3 V2
    26  #define XTMP4 V3
    27  #define XDATA V6
    28  #define XDIGEST V7
    29  #define KS_L V8
    30  #define KS_M1 V9
    31  #define BIT_REV_TAB_L V12
    32  #define BIT_REV_TAB_H V13
    33  #define BIT_REV_AND_TAB V14
    34  
    35  #define PTR R7
    36  
    37  // func eia3Round16B(t *uint32, keyStream *uint32, p *byte, tagSize int)
    38  TEXT ·eia3Round16B(SB),NOSPLIT,$0
    39  	MOVD t+0(FP), R3
    40  	MOVD ks+8(FP), R4
    41  	MOVD p+16(FP), R5
    42  
    43  #ifndef GOARCH_ppc64le
    44  	MOVD	$·rcon(SB), PTR // PTR points to rcon addr
    45  	LVX	(PTR), XTMP1
    46  	ADD	$0x10, PTR
    47  #else
    48  	MOVD	$·rcon+0x10(SB), PTR // PTR points to rcon addr (skipping permute vector)
    49  #endif
    50  
    51  	LXVD2X (R5)(R0), XDATA
    52  #ifndef GOARCH_ppc64le
    53  	VPERM XDATA, XDATA, XTMP1, XDATA
    54  #endif
    55  
    56  	LXVD2X (PTR)(R0), BIT_REV_AND_TAB
    57  	VAND	BIT_REV_AND_TAB, XDATA, XTMP3
    58  	VSPLTISB $4, XTMP2;
    59  	VSRW	XDATA, XTMP2, XTMP1
    60  	VAND	BIT_REV_AND_TAB, XTMP1, XTMP1
    61  
    62  	MOVD	$0x10, R8
    63  	LXVD2X (PTR)(R8), BIT_REV_TAB_L
    64  	VSLB  BIT_REV_TAB_L, XTMP2, BIT_REV_TAB_H
    65  	VPERM BIT_REV_TAB_L, BIT_REV_TAB_L, XTMP1, XTMP1
    66  	VPERM BIT_REV_TAB_H, BIT_REV_TAB_H, XTMP3, XTMP3
    67  	VXOR XTMP1, XTMP3, XTMP3 // XTMP3 - bit reverse data bytes
    68  
    69  	// ZUC authentication part, 4x32 data bits
    70  	// setup data
    71  	VSPLTISB $0, XTMP2
    72  	MOVD $0x20, R8
    73  	LXVD2X (PTR)(R8), XTMP4
    74  	VPERM XTMP2, XTMP3, XTMP4, XTMP1
    75  	MOVD $0x30, R8
    76  	LXVD2X (PTR)(R8), XTMP4
    77  	VPERM XTMP2, XTMP3, XTMP4, XTMP2
    78  
    79  	// setup KS
    80  	LXVW4X (R4), KS_L
    81  	MOVD $8, R8
    82  	LXVW4X (R8)(R4), KS_M1
    83  	MOVD $0x40, R8
    84  	LXVD2X (PTR)(R8), XTMP4
    85  	VPERM KS_L, KS_L, XTMP4, KS_L
    86  	VPERM KS_M1, KS_M1, XTMP4, KS_M1
    87  
    88  	// clmul
    89  	// xor the results from 4 32-bit words together
    90  	// Calculate lower 32 bits of tag
    91  	VPMSUMD XTMP1, KS_L, XTMP3
    92  	VPMSUMD XTMP2, KS_M1, XTMP4
    93  	VXOR XTMP3, XTMP4, XTMP3
    94  	VSPLTW $2, XTMP3, XDIGEST
    95  
    96  	// Update tag
    97  	MFVSRWZ XDIGEST, R8
    98  	MOVWZ (R3), R6
    99  	XOR R6, R8, R6 
   100  	MOVW R6, (R3)
   101  
   102  	// Copy last 16 bytes of KS to the front
   103  	MOVD $16, R8
   104  	LXVD2X (R8)(R4), XTMP1
   105  	STXVD2X XTMP1, (R4)(R0)
   106  
   107  	RET