1
2
3
4
5
6
7 package aes
8
9 import (
10 "crypto/internal/fips140deps/cpu"
11 "crypto/internal/fips140deps/godebug"
12 "crypto/internal/impl"
13 )
14
15
16 func encryptBlockAsm(nr int, xk *uint32, dst, src *byte)
17
18
19 func decryptBlockAsm(nr int, xk *uint32, dst, src *byte)
20
21
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
39
40
41
42 if godebug.Value("#ppc64aes") == "off" {
43 supportsAES = false
44 }
45 impl.Register("aes", "POWER8", &supportsAES)
46 }
47 }
48
49
50
51
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
80
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