xor_unaligned.go raw

   1  // Copyright 2015 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  //go:build (amd64 || 386 || ppc64le) && !appengine
   6  // +build amd64 386 ppc64le
   7  // +build !appengine
   8  
   9  package sha3
  10  
  11  import "unsafe"
  12  
  13  // A storageBuf is an aligned array of maxRate bytes.
  14  type storageBuf [maxRate / 8]uint64
  15  
  16  func (b *storageBuf) asBytes() *[maxRate]byte {
  17  	return (*[maxRate]byte)(unsafe.Pointer(b)) //nolint:gosec
  18  }
  19  
  20  // xorInuses unaligned reads and writes to update d.a to contain d.a
  21  // XOR buf.
  22  func xorIn(d *State, buf []byte) {
  23  	n := len(buf)
  24  	bw := (*[maxRate / 8]uint64)(unsafe.Pointer(&buf[0]))[: n/8 : n/8] //nolint:gosec
  25  	if n >= 72 {
  26  		d.a[0] ^= bw[0]
  27  		d.a[1] ^= bw[1]
  28  		d.a[2] ^= bw[2]
  29  		d.a[3] ^= bw[3]
  30  		d.a[4] ^= bw[4]
  31  		d.a[5] ^= bw[5]
  32  		d.a[6] ^= bw[6]
  33  		d.a[7] ^= bw[7]
  34  		d.a[8] ^= bw[8]
  35  	}
  36  	if n >= 104 {
  37  		d.a[9] ^= bw[9]
  38  		d.a[10] ^= bw[10]
  39  		d.a[11] ^= bw[11]
  40  		d.a[12] ^= bw[12]
  41  	}
  42  	if n >= 136 {
  43  		d.a[13] ^= bw[13]
  44  		d.a[14] ^= bw[14]
  45  		d.a[15] ^= bw[15]
  46  		d.a[16] ^= bw[16]
  47  	}
  48  	if n >= 144 {
  49  		d.a[17] ^= bw[17]
  50  	}
  51  	if n >= 168 {
  52  		d.a[18] ^= bw[18]
  53  		d.a[19] ^= bw[19]
  54  		d.a[20] ^= bw[20]
  55  	}
  56  }
  57  
  58  func copyOut(d *State, buf []byte) {
  59  	ab := (*[maxRate]uint8)(unsafe.Pointer(&d.a[0])) //nolint:gosec
  60  	copy(buf, ab[:])
  61  }
  62