-
Notifications
You must be signed in to change notification settings - Fork 21
/
marshal.go
201 lines (157 loc) · 5.35 KB
/
marshal.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package sm2
import (
"errors"
"math/big"
"encoding/asn1"
"crypto/elliptic"
)
type sm2Signature struct {
R, S *big.Int
}
func MarshalSignatureASN1(r, s *big.Int) ([]byte, error) {
return asn1.Marshal(sm2Signature{r, s})
}
func UnmarshalSignatureASN1(sign []byte) (r, s *big.Int, err error) {
var sm2Sign sm2Signature
_, err = asn1.Unmarshal(sign, &sm2Sign)
if err != nil {
return
}
return sm2Sign.R, sm2Sign.S, nil
}
// 拼接编码
func marshalCipherBytes(curve elliptic.Curve, c []byte, mode Mode, h hashFunc) []byte {
byteLen := (curve.Params().BitSize + 7) / 8
hashSize := h().Size()
// C1C3C2 密文结构: x + y + hash + CipherText
// C1C2C3 密文结构: x + y + CipherText + hash
switch mode {
case C1C2C3:
c1 := make([]byte, 2*byteLen)
c2 := make([]byte, len(c) - 2*byteLen - hashSize)
c3 := make([]byte, hashSize)
copy(c1, c[0:]) // x1, y1
copy(c3, c[2*byteLen:]) // hash
copy(c2, c[2*byteLen+hashSize:]) // 密文
ct := make([]byte, 0)
ct = append(ct, c1...)
ct = append(ct, c2...)
ct = append(ct, c3...)
return append([]byte{0x04}, ct...)
case C1C3C2:
fallthrough
default:
return append([]byte{0x04}, c...)
}
}
func unmarshalCipherBytes(curve elliptic.Curve, data []byte, mode Mode, h hashFunc) ([]byte, error) {
typ := data[0]
if typ != byte(0x04) {
return nil, errors.New("cryptobin/sm2: encrypted data is error and miss prefix '4'.")
}
hashSize := h().Size()
byteLen := (curve.Params().BitSize + 7) / 8
if len(data) < 2*byteLen + hashSize {
return nil, errors.New("cryptobin/sm2: encrypt data is too short.")
}
switch mode {
case C1C2C3:
data = data[1:]
c1 := make([]byte, 2*byteLen)
c2 := make([]byte, len(data) - 2*byteLen - hashSize)
c3 := make([]byte, hashSize)
copy(c1, data[0:]) // x1, y1
copy(c2, data[2*byteLen:]) // 密文
copy(c3, data[len(data) - hashSize:]) // hash
c := make([]byte, 0)
c = append(c, c1...)
c = append(c, c3...)
c = append(c, c2...)
data = c
case C1C3C2:
fallthrough
default:
data = data[1:]
}
return data, nil
}
// asn.1 编码
func marshalCipherASN1(curve elliptic.Curve, data []byte, mode Mode, h hashFunc) ([]byte, error) {
hashSize := h().Size()
if mode == C1C2C3 {
return marshalCipherASN1Old(curve, data, hashSize)
}
return marshalCipherASN1New(curve, data, hashSize)
}
func unmarshalCipherASN1(curve elliptic.Curve, data []byte, mode Mode) ([]byte, error) {
if mode == C1C2C3 {
return unmarshalCipherASN1Old(curve, data)
}
return unmarshalCipherASN1New(curve, data)
}
// c1c3c2 格式
type cipherASN1New struct {
XCoordinate *big.Int
YCoordinate *big.Int
HASH []byte
CipherText []byte
}
// sm2 密文转 asn.1 编码格式
// sm2 密文结构: x + y + hash + CipherText
func marshalCipherASN1New(curve elliptic.Curve, data []byte, hashSize int) ([]byte, error) {
byteLen := (curve.Params().BitSize + 7) / 8
x := new(big.Int).SetBytes(data[:byteLen])
y := new(big.Int).SetBytes(data[byteLen:2*byteLen])
hash := data[2*byteLen:2*byteLen+hashSize]
cipherText := data[2*byteLen+hashSize:]
return asn1.Marshal(cipherASN1New{x, y, hash, cipherText})
}
// sm2 密文 asn.1 编码格式转 C1|C3|C2 拼接格式
func unmarshalCipherASN1New(curve elliptic.Curve, b []byte) ([]byte, error) {
var data cipherASN1New
_, err := asn1.Unmarshal(b, &data)
if err != nil {
return nil, err
}
xBuf := bigIntToBytes(curve, data.XCoordinate)
yBuf := bigIntToBytes(curve, data.YCoordinate)
c := []byte{}
c = append(c, xBuf...) // x分量
c = append(c, yBuf...) // y分量
c = append(c, data.HASH...) // hash
c = append(c, data.CipherText...) // cipherText
return c, nil
}
// c1c2c3 格式
type cipherASN1Old struct {
XCoordinate *big.Int
YCoordinate *big.Int
CipherText []byte
HASH []byte
}
// sm2 密文转 asn.1 编码格式
// sm2 密文结构: x + y + CipherText + hash
func marshalCipherASN1Old(curve elliptic.Curve, data []byte, hashSize int) ([]byte, error) {
byteLen := (curve.Params().BitSize + 7) / 8
x := new(big.Int).SetBytes(data[:byteLen])
y := new(big.Int).SetBytes(data[byteLen:2*byteLen])
hash := data[2*byteLen:2*byteLen+hashSize]
cipherText := data[2*byteLen+hashSize:]
return asn1.Marshal(cipherASN1Old{x, y, cipherText, hash})
}
// sm2 密文 asn.1 编码格式转 C1|C3|C2 拼接格式
func unmarshalCipherASN1Old(curve elliptic.Curve, b []byte) ([]byte, error) {
var data cipherASN1Old
_, err := asn1.Unmarshal(b, &data)
if err != nil {
return nil, err
}
xBuf := bigIntToBytes(curve, data.XCoordinate)
yBuf := bigIntToBytes(curve, data.YCoordinate)
c := []byte{}
c = append(c, xBuf...) // x分量
c = append(c, yBuf...) // y分量
c = append(c, data.HASH...) // hash
c = append(c, data.CipherText...) // cipherText
return c, nil
}