-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes.go
More file actions
119 lines (92 loc) · 3.01 KB
/
Copy pathaes.go
File metadata and controls
119 lines (92 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Package aes implements convenience helpers for AES block cipher modes.
package aes
import (
stdaes "crypto/aes"
"crypto/rand"
"errors"
"fmt"
"io"
)
// Mode identifies an AES mode of operation.
type Mode uint8
const (
// ModeCBC selects cipher block chaining mode.
ModeCBC Mode = iota + 1
// ModeCFB selects cipher feedback mode.
ModeCFB
// ModeCTR selects counter mode.
ModeCTR
// ModeECB selects electronic codebook mode.
ModeECB
// ModeIGE selects infinite garble extension mode.
ModeIGE
// ModeOFB selects output feedback mode.
ModeOFB
)
const (
// IGEIVSize is the size, in bytes, of an IGE initialization vector.
IGEIVSize = stdaes.BlockSize << 1
blockMask = stdaes.BlockSize - 1
// KeySize128 is the size, in bytes, of an AES-128 key.
KeySize128 = 16
// KeySize192 is the size, in bytes, of an AES-192 key.
KeySize192 = 24
// KeySize256 is the size, in bytes, of an AES-256 key.
KeySize256 = 32
)
// ErrUnknownMode is returned when New is called with an unknown mode.
var ErrUnknownMode = errors.New("aes: unknown cipher mode")
// KeySizeError is returned when an AES key has the wrong length.
type KeySizeError int
func (e KeySizeError) Error() string {
return fmt.Sprintf("aes: invalid key size %d: must be 16, 24, or 32 bytes", int(e))
}
// IvSizeError is returned when an initialization vector has the wrong length.
type IvSizeError int
func (e IvSizeError) Error() string {
return fmt.Sprintf("aes: invalid IV size %d: must be %d bytes", int(e), stdaes.BlockSize)
}
// IgeIvSizeError is returned when an IGE initialization vector has the wrong length.
type IgeIvSizeError int
func (e IgeIvSizeError) Error() string {
return fmt.Sprintf("aes: invalid IGE IV size %d: must be %d bytes", int(e), IGEIVSize)
}
// InvalidDataError is returned when plaintext is empty or not block aligned.
type InvalidDataError int
func (e InvalidDataError) Error() string {
if e == 0 {
return "aes: plaintext is empty"
}
return fmt.Sprintf("aes: plaintext length %d is not a multiple of the block size", int(e))
}
// InvalidCiphertextError is returned when ciphertext is empty or not block aligned.
type InvalidCiphertextError int
func (e InvalidCiphertextError) Error() string {
if e == 0 {
return "aes: ciphertext is empty"
}
return fmt.Sprintf("aes: ciphertext length %d is not a multiple of the block size", int(e))
}
// GenerateRandomBytes returns n cryptographically secure random bytes.
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return nil, err
}
return b, nil
}
// GenerateIV returns a new random AES initialization vector.
func GenerateIV() ([]byte, error) {
return GenerateRandomBytes(stdaes.BlockSize)
}
// GenerateKey returns a new random AES key of the given size.
//
// The size must be [KeySize128], [KeySize192], or [KeySize256].
func GenerateKey(size int) ([]byte, error) {
switch size {
case KeySize128, KeySize192, KeySize256:
default:
return nil, KeySizeError(size)
}
return GenerateRandomBytes(size)
}