...
1
2
3
4
5 package aes
6
7 import (
8 "crypto/cipher"
9 "crypto/internal/alias"
10 "crypto/internal/boring"
11 "strconv"
12 )
13
14
15 const BlockSize = 16
16
17
18 type aesCipher struct {
19 enc []uint32
20 dec []uint32
21 }
22
23 type KeySizeError int
24
25 func (k KeySizeError) Error() string {
26 return "crypto/aes: invalid key size " + strconv.Itoa(int(k))
27 }
28
29
30
31
32
33 func NewCipher(key []byte) (cipher.Block, error) {
34 k := len(key)
35 switch k {
36 default:
37 return nil, KeySizeError(k)
38 case 16, 24, 32:
39 break
40 }
41 if boring.Enabled {
42 return boring.NewAESCipher(key)
43 }
44 return newCipher(key)
45 }
46
47
48
49 func newCipherGeneric(key []byte) (cipher.Block, error) {
50 n := len(key) + 28
51 c := aesCipher{make([]uint32, n), make([]uint32, n)}
52 expandKeyGo(key, c.enc, c.dec)
53 return &c, nil
54 }
55
56 func (c *aesCipher) BlockSize() int { return BlockSize }
57
58 func (c *aesCipher) Encrypt(dst, src []byte) {
59 if len(src) < BlockSize {
60 panic("crypto/aes: input not full block")
61 }
62 if len(dst) < BlockSize {
63 panic("crypto/aes: output not full block")
64 }
65 if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
66 panic("crypto/aes: invalid buffer overlap")
67 }
68 encryptBlockGo(c.enc, dst, src)
69 }
70
71 func (c *aesCipher) Decrypt(dst, src []byte) {
72 if len(src) < BlockSize {
73 panic("crypto/aes: input not full block")
74 }
75 if len(dst) < BlockSize {
76 panic("crypto/aes: output not full block")
77 }
78 if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
79 panic("crypto/aes: invalid buffer overlap")
80 }
81 decryptBlockGo(c.dec, dst, src)
82 }
83
View as plain text