git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/chacha/chacha_386.go (about)

     1  // Copyright (c) 2016 Andreas Auernhammer. All rights reserved.
     2  // Use of this source code is governed by a license that can be
     3  // found in the LICENSE file.
     4  
     5  // +build 386,!gccgo,!appengine,!nacl
     6  
     7  package chacha
     8  
     9  import (
    10  	"encoding/binary"
    11  
    12  	"golang.org/x/sys/cpu"
    13  )
    14  
    15  func init() {
    16  	useSSE2 = cpu.X86.HasSSE2
    17  	useSSSE3 = cpu.X86.HasSSSE3
    18  	useAVX = false
    19  	useAVX2 = false
    20  }
    21  
    22  func initialize(state *[64]byte, key []byte, nonce *[16]byte) {
    23  	binary.LittleEndian.PutUint32(state[0:], sigma[0])
    24  	binary.LittleEndian.PutUint32(state[4:], sigma[1])
    25  	binary.LittleEndian.PutUint32(state[8:], sigma[2])
    26  	binary.LittleEndian.PutUint32(state[12:], sigma[3])
    27  	copy(state[16:], key[:])
    28  	copy(state[48:], nonce[:])
    29  }
    30  
    31  // This function is implemented in chacha_386.s
    32  //go:noescape
    33  func hChaCha20SSE2(out *[32]byte, nonce *[16]byte, key *[32]byte)
    34  
    35  // This function is implemented in chacha_386.s
    36  //go:noescape
    37  func hChaCha20SSSE3(out *[32]byte, nonce *[16]byte, key *[32]byte)
    38  
    39  // This function is implemented in chacha_386.s
    40  //go:noescape
    41  func xorKeyStreamSSE2(dst, src []byte, block, state *[64]byte, rounds int) int
    42  
    43  func hChaCha20(out *[32]byte, nonce *[16]byte, key *[32]byte) {
    44  	switch {
    45  	case useSSSE3:
    46  		hChaCha20SSSE3(out, nonce, key)
    47  	case useSSE2:
    48  		hChaCha20SSE2(out, nonce, key)
    49  	default:
    50  		hChaCha20Generic(out, nonce, key)
    51  	}
    52  }
    53  
    54  func xorKeyStream(dst, src []byte, block, state *[64]byte, rounds int) int {
    55  	if useSSE2 {
    56  		return xorKeyStreamSSE2(dst, src, block, state, rounds)
    57  	} else {
    58  		return xorKeyStreamGeneric(dst, src, block, state, rounds)
    59  	}
    60  }