Source file src/crypto/internal/fips140/aes/aes_asm.go

     1  // Copyright 2012 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 || arm64 || loong64 || ppc64 || ppc64le) && !purego
     6  
     7  package aes
     8  
     9  import (
    10  	"crypto/internal/fips140deps/cpu"
    11  	"crypto/internal/fips140deps/godebug"
    12  	"crypto/internal/impl"
    13  )
    14  
    15  //go:noescape
    16  func encryptBlockAsm(nr int, xk *uint32, dst, src *byte)
    17  
    18  //go:noescape
    19  func decryptBlockAsm(nr int, xk *uint32, dst, src *byte)
    20  
    21  //go:noescape
    22  func expandKeyAsm(nr int, key *byte, enc *uint32, dec *uint32)
    23  
    24  var supportsAES = cpu.X86HasAES && cpu.X86HasSSE41 && cpu.X86HasSSSE3 ||
    25  	cpu.ARM64HasAES || cpu.LOONG64HasLSX || cpu.PPC64 || cpu.PPC64le
    26  
    27  func init() {
    28  	if cpu.AMD64 {
    29  		impl.Register("aes", "AES-NI", &supportsAES)
    30  	}
    31  	if cpu.ARM64 {
    32  		impl.Register("aes", "Armv8.0", &supportsAES)
    33  	}
    34  	if cpu.LOONG64 {
    35  		impl.Register("aes", "LSX", &supportsAES)
    36  	}
    37  	if cpu.PPC64 || cpu.PPC64le {
    38  		// The POWER architecture doesn't have a way to turn off AES support
    39  		// at runtime with GODEBUG=cpu.something=off, so introduce a new GODEBUG
    40  		// knob for that. It's intentionally only checked at init() time, to
    41  		// avoid the performance overhead of checking it every time.
    42  		if godebug.Value("#ppc64aes") == "off" {
    43  			supportsAES = false
    44  		}
    45  		impl.Register("aes", "POWER8", &supportsAES)
    46  	}
    47  }
    48  
    49  // checkGenericIsExpected is called by the variable-time implementation to make
    50  // sure it is not used when hardware support is available. It shouldn't happen,
    51  // but this way it's more evidently correct.
    52  func checkGenericIsExpected() {
    53  	if supportsAES {
    54  		panic("crypto/aes: internal error: using generic implementation despite hardware support")
    55  	}
    56  }
    57  
    58  type block struct {
    59  	blockExpanded
    60  }
    61  
    62  func newBlock(c *Block, key []byte) *Block {
    63  	switch len(key) {
    64  	case aes128KeySize:
    65  		c.rounds = aes128Rounds
    66  	case aes192KeySize:
    67  		c.rounds = aes192Rounds
    68  	case aes256KeySize:
    69  		c.rounds = aes256Rounds
    70  	}
    71  	if supportsAES {
    72  		expandKeyAsm(c.rounds, &key[0], &c.enc[0], &c.dec[0])
    73  	} else {
    74  		expandKeyGeneric(&c.blockExpanded, key)
    75  	}
    76  	return c
    77  }
    78  
    79  // EncryptionKeySchedule is used from the GCM implementation to access the
    80  // precomputed AES key schedule, to pass to the assembly implementation.
    81  func EncryptionKeySchedule(c *Block) []uint32 {
    82  	return c.enc[:c.roundKeysSize()]
    83  }
    84  
    85  func encryptBlock(c *Block, dst, src []byte) {
    86  	if supportsAES {
    87  		encryptBlockAsm(c.rounds, &c.enc[0], &dst[0], &src[0])
    88  	} else {
    89  		encryptBlockGeneric(&c.blockExpanded, dst, src)
    90  	}
    91  }
    92  
    93  func decryptBlock(c *Block, dst, src []byte) {
    94  	if supportsAES {
    95  		decryptBlockAsm(c.rounds, &c.dec[0], &dst[0], &src[0])
    96  	} else {
    97  		decryptBlockGeneric(&c.blockExpanded, dst, src)
    98  	}
    99  }
   100  

View as plain text