-
Notifications
You must be signed in to change notification settings - Fork 4
/
padding.go
186 lines (169 loc) · 5.54 KB
/
padding.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package crypt
import (
"bytes"
"fmt"
)
type PaddingScheme uint8
const (
PAD_PKCS7 PaddingScheme = iota
PAD_ISO97971
PAD_ANSIX923
PAD_ISO10126
PAD_ZEROPADDING
PAD_NOPADDING
)
func (scheme PaddingScheme) String() string {
switch scheme {
case PAD_PKCS7:
return "PKCS7"
case PAD_ISO97971:
return "ISO/IEC 9797-1"
case PAD_ANSIX923:
return "ANSI X.923"
case PAD_ISO10126:
return "ISO10126"
case PAD_ZEROPADDING:
return "ZeroPadding"
case PAD_NOPADDING:
return "NoPadding"
}
return ""
}
func padSize(dataSize, blockSize int) (padding int) {
padding = blockSize - dataSize%blockSize
return
}
// PKCS7
func PKCS7Padding(plaintext []byte, blockSize int) ([]byte, error) {
if blockSize < 1 || blockSize > 255 {
return nil, fmt.Errorf("crypt.PKCS7Padding blockSize is out of bounds: %d", blockSize)
}
padding := padSize(len(plaintext), blockSize)
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(plaintext, padtext...), nil
}
func PKCS7UnPadding(ciphertext []byte, blockSize int) ([]byte, error) {
length := len(ciphertext)
if length%blockSize != 0 {
return nil, fmt.Errorf("crypt.PKCS7UnPadding ciphertext's length isn't a multiple of blockSize")
}
unpadding := int(ciphertext[length-1])
if unpadding > blockSize || unpadding <= 0 {
return nil, fmt.Errorf("crypt.PKCS7UnPadding invalid padding found: %v", unpadding)
}
var pad = ciphertext[length-unpadding : length-1]
for _, v := range pad {
if int(v) != unpadding {
return nil, fmt.Errorf("crypt.PKCS7UnPadding invalid padding found")
}
}
return ciphertext[:length-unpadding], nil
}
// Zero padding
func ZeroPadding(plaintext []byte, blockSize int) ([]byte, error) {
if blockSize < 1 || blockSize > 255 {
return nil, fmt.Errorf("crypt.ZeroPadding blockSize is out of bounds: %d", blockSize)
}
padding := padSize(len(plaintext), blockSize)
padtext := bytes.Repeat([]byte{0}, padding)
return append(plaintext, padtext...), nil
}
func ZeroUnPadding(ciphertext []byte, _ int) ([]byte, error) {
return bytes.TrimRightFunc(ciphertext, func(r rune) bool { return r == rune(0) }), nil
}
// ISO/IEC 9797-1 Padding Method 2
func ISO97971Padding(plaintext []byte, blockSize int) ([]byte, error) {
return ZeroPadding(append(plaintext, 0x80), blockSize)
}
func ISO97971UnPadding(ciphertext []byte, blockSize int) ([]byte, error) {
data, err := ZeroUnPadding(ciphertext, blockSize)
if err != nil {
return nil, err
}
return data[:len(data)-1], nil
}
// ANSI X.923 padding
func AnsiX923Padding(plaintext []byte, blockSize int) ([]byte, error) {
if blockSize < 1 || blockSize > 255 {
return nil, fmt.Errorf("crypt.AnsiX923Padding blockSize is out of bounds: %d", blockSize)
}
padding := padSize(len(plaintext), blockSize)
padtext := append(bytes.Repeat([]byte{byte(0)}, padding-1), byte(padding))
return append(plaintext, padtext...), nil
}
func AnsiX923UnPadding(ciphertext []byte, blockSize int) ([]byte, error) {
length := len(ciphertext)
if length%blockSize != 0 {
return nil, fmt.Errorf("crypt.AnsiX923UnPadding ciphertext's length isn't a multiple of blockSize")
}
unpadding := int(ciphertext[length-1])
if unpadding > blockSize || unpadding < 1 {
return nil, fmt.Errorf("crypt.AnsiX923UnPadding invalid padding found: %d", unpadding)
}
if length-unpadding < length-2 {
pad := ciphertext[length-unpadding : length-2]
for _, v := range pad {
if int(v) != 0 {
return nil, fmt.Errorf("crypt.AnsiX923UnPadding invalid padding found")
}
}
}
return ciphertext[0 : length-unpadding], nil
}
// ISO10126 implements ISO 10126 byte padding. This has been withdrawn in 2007.
func ISO10126Padding(plaintext []byte, blockSize int) ([]byte, error) {
if blockSize < 1 || blockSize > 256 {
return nil, fmt.Errorf("crypt.ISO10126Padding blockSize is out of bounds: %d", blockSize)
}
padding := padSize(len(plaintext), blockSize)
padtext := append(randBytes(padding-1), byte(padding))
return append(plaintext, padtext...), nil
}
func ISO10126UnPadding(ciphertext []byte, blockSize int) ([]byte, error) {
length := len(ciphertext)
if length%blockSize != 0 {
return nil, fmt.Errorf("crypt.ISO10126UnPadding ciphertext's length isn't a multiple of blockSize")
}
unpadding := int(ciphertext[length-1])
if unpadding > blockSize || unpadding < 1 {
return nil, fmt.Errorf("crypt.ISO10126UnPadding invalid padding found: %v", unpadding)
}
return ciphertext[:length-unpadding], nil
}
func Padding(scheme PaddingScheme, plaintext []byte, blockSize int) (padded []byte, err error) {
switch scheme {
case PAD_PKCS7:
padded, err = PKCS7Padding(plaintext, blockSize)
case PAD_ISO97971:
padded, err = ISO97971Padding(plaintext, blockSize)
case PAD_ANSIX923:
padded, err = AnsiX923Padding(plaintext, blockSize)
case PAD_ISO10126:
padded, err = ISO10126Padding(plaintext, blockSize)
case PAD_ZEROPADDING:
padded, err = ZeroPadding(plaintext, blockSize)
case PAD_NOPADDING:
if len(plaintext)%blockSize != 0 {
return nil, fmt.Errorf("crypt.NoPadding plaintext is not a multiple of the block size")
}
return plaintext, nil
}
return
}
func UnPadding(scheme PaddingScheme, ciphertext []byte, blockSize int) (data []byte, err error) {
switch scheme {
case PAD_PKCS7:
data, err = PKCS7UnPadding(ciphertext, blockSize)
case PAD_ISO97971:
data, err = ISO97971UnPadding(ciphertext, blockSize)
case PAD_ANSIX923:
data, err = AnsiX923UnPadding(ciphertext, blockSize)
case PAD_ISO10126:
data, err = ISO10126UnPadding(ciphertext, blockSize)
case PAD_ZEROPADDING:
data, err = ZeroUnPadding(ciphertext, blockSize)
case PAD_NOPADDING:
return ciphertext, nil
}
return
}