1
2
3
4
5
6
7 package aes
8
9 import (
10 "crypto/internal/fips140deps/byteorder"
11 "crypto/internal/fips140deps/cpu"
12 "crypto/internal/impl"
13 )
14
15 func rotwLE(t uint32) uint32 {
16 return (t >> 8) | (t << 24)
17 }
18
19 var supportsAES = cpu.RISCV64HasV && cpu.RISCV64HasZvkned &&
20 cpu.RISCV64HasZbb && cpu.RISCV64VLENB >= 16
21
22 func init() {
23 impl.Register("aes", "Zvkned", &supportsAES)
24 }
25
26 type block struct {
27 blockExpanded
28 }
29
30
31 func expandKeyAsm(nr int, key *byte, enc *uint32, dec *uint32)
32
33
34 func encryptBlockAsm(nr int, xk *uint32, dst, src *byte)
35
36
37 func decryptBlockAsm(nr int, xk *uint32, dst, src *byte)
38
39 func checkGenericIsExpected() {
40 if supportsAES {
41 panic("crypto/aes: internal error: using generic implementation despite hardware support")
42 }
43 }
44
45
46
47
48
49 func expandKeyAES192LE(c *blockExpanded, key []byte) {
50 const nk = 6
51 const n = 52
52
53 for i := 0; i < nk; i++ {
54 c.enc[i] = byteorder.LEUint32(key[4*i:])
55 }
56 for i := nk; i < n; i++ {
57 t := c.enc[i-1]
58 if i%nk == 0 {
59 t = subw(rotwLE(t)) ^ uint32(powx[i/nk-1])
60 }
61 c.enc[i] = c.enc[i-nk] ^ t
62 }
63 for i := 0; i < n; i += 4 {
64 ei := n - i - 4
65 for j := 0; j < 4; j++ {
66 c.dec[i+j] = c.enc[ei+j]
67 }
68 }
69 }
70
71 func newBlock(c *Block, key []byte) *Block {
72 switch len(key) {
73 case aes128KeySize:
74 c.rounds = aes128Rounds
75 case aes192KeySize:
76 c.rounds = aes192Rounds
77 case aes256KeySize:
78 c.rounds = aes256Rounds
79 default:
80 panic("invalid AES key size")
81 }
82
83 if !supportsAES {
84 expandKeyGeneric(&c.blockExpanded, key)
85 } else if c.rounds == aes192Rounds {
86 expandKeyAES192LE(&c.blockExpanded, key)
87 } else {
88 expandKeyAsm(c.rounds, &key[0], &c.enc[0], &c.dec[0])
89 }
90 return c
91 }
92
93 func EncryptionKeySchedule(c *Block) []uint32 {
94 return c.enc[:c.roundKeysSize()]
95 }
96
97 func encryptBlock(c *Block, dst, src []byte) {
98 if supportsAES {
99 encryptBlockAsm(c.rounds, &c.enc[0], &dst[0], &src[0])
100 } else {
101 encryptBlockGeneric(&c.blockExpanded, dst, src)
102 }
103 }
104
105 func decryptBlock(c *Block, dst, src []byte) {
106 if supportsAES {
107 decryptBlockAsm(c.rounds, &c.dec[0], &dst[0], &src[0])
108 } else {
109 decryptBlockGeneric(&c.blockExpanded, dst, src)
110 }
111 }
112
View as plain text