| |
| |
| |
|
|
| |
|
|
| package aes |
|
|
| import ( |
| "crypto/internal/fips140deps/cpu" |
| "crypto/internal/fips140deps/godebug" |
| "crypto/internal/impl" |
| ) |
|
|
| |
| func encryptBlockAsm(nr int, xk *uint32, dst, src *byte) |
|
|
| |
| func decryptBlockAsm(nr int, xk *uint32, dst, src *byte) |
|
|
| |
| func expandKeyAsm(nr int, key *byte, enc *uint32, dec *uint32) |
|
|
| var supportsAES = cpu.X86HasAES && cpu.X86HasSSE41 && cpu.X86HasSSSE3 || |
| cpu.ARM64HasAES || cpu.PPC64 || cpu.PPC64le |
|
|
| func init() { |
| if cpu.AMD64 { |
| impl.Register("aes", "AES-NI", &supportsAES) |
| } |
| if cpu.ARM64 { |
| impl.Register("aes", "Armv8.0", &supportsAES) |
| } |
| if cpu.PPC64 || cpu.PPC64le { |
| |
| |
| |
| |
| if godebug.Value("#ppc64aes") == "off" { |
| supportsAES = false |
| } |
| impl.Register("aes", "POWER8", &supportsAES) |
| } |
| } |
|
|
| |
| |
| |
| func checkGenericIsExpected() { |
| if supportsAES { |
| panic("crypto/aes: internal error: using generic implementation despite hardware support") |
| } |
| } |
|
|
| type block struct { |
| blockExpanded |
| } |
|
|
| func newBlock(c *Block, key []byte) *Block { |
| switch len(key) { |
| case aes128KeySize: |
| c.rounds = aes128Rounds |
| case aes192KeySize: |
| c.rounds = aes192Rounds |
| case aes256KeySize: |
| c.rounds = aes256Rounds |
| } |
| if supportsAES { |
| expandKeyAsm(c.rounds, &key[0], &c.enc[0], &c.dec[0]) |
| } else { |
| expandKeyGeneric(&c.blockExpanded, key) |
| } |
| return c |
| } |
|
|
| |
| |
| func EncryptionKeySchedule(c *Block) []uint32 { |
| return c.enc[:c.roundKeysSize()] |
| } |
|
|
| func encryptBlock(c *Block, dst, src []byte) { |
| if supportsAES { |
| encryptBlockAsm(c.rounds, &c.enc[0], &dst[0], &src[0]) |
| } else { |
| encryptBlockGeneric(&c.blockExpanded, dst, src) |
| } |
| } |
|
|
| func decryptBlock(c *Block, dst, src []byte) { |
| if supportsAES { |
| decryptBlockAsm(c.rounds, &c.dec[0], &dst[0], &src[0]) |
| } else { |
| decryptBlockGeneric(&c.blockExpanded, dst, src) |
| } |
| } |
|
|