-
Notifications
You must be signed in to change notification settings - Fork 3
/
msg_ecdhesecretkeyexchange.go
169 lines (145 loc) · 4.77 KB
/
msg_ecdhesecretkeyexchange.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
package gtp
import (
"bytes"
"git.golaxy.org/framework/util/binaryutil"
)
// ECDHESecretKeyExchange消息标志位
const (
Flag_Signature Flag = 1 << (iota + Flag_Customize) // 有签名数据,在双方的ECDHE秘钥交换消息中携带,表示可以验证对方签名
)
// SignatureAlgorithm 签名算法
type SignatureAlgorithm struct {
AsymmetricEncryption AsymmetricEncryption // 非对称加密算法
PaddingMode PaddingMode // 填充方案
Hash Hash // 摘要函数
}
// Read implements io.Reader
func (sa SignatureAlgorithm) Read(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
if err := bs.WriteUint8(uint8(sa.AsymmetricEncryption)); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteUint8(uint8(sa.PaddingMode)); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteUint8(uint8(sa.Hash)); err != nil {
return bs.BytesWritten(), err
}
return bs.BytesWritten(), nil
}
// Write implements io.Writer
func (sa *SignatureAlgorithm) Write(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
asymmetricEncryption, err := bs.ReadUint8()
if err != nil {
return bs.BytesRead(), err
}
sa.AsymmetricEncryption = AsymmetricEncryption(asymmetricEncryption)
paddingMode, err := bs.ReadUint8()
if err != nil {
return bs.BytesRead(), err
}
sa.PaddingMode = PaddingMode(paddingMode)
hash, err := bs.ReadUint8()
if err != nil {
return bs.BytesRead(), err
}
sa.Hash = Hash(hash)
return bs.BytesRead(), nil
}
// Size 大小
func (SignatureAlgorithm) Size() int {
return binaryutil.SizeofUint8() + binaryutil.SizeofUint8() + binaryutil.SizeofUint8()
}
// MsgECDHESecretKeyExchange ECDHE秘钥交换消息,利用(g^a mod p)^b mod p == (g^b mod p)^a mod p等式,交换秘钥
// (注意:为了提高解码性能,减少内存碎片,解码string与bytes字段时均使用引用类型,引用字节池中的bytes,GC时会被归还字节池,不要直接持有此类型字段)
type MsgECDHESecretKeyExchange struct {
NamedCurve NamedCurve // 曲线类型
PublicKey []byte // 公钥
IV []byte // iv
Nonce []byte // nonce
NonceStep []byte // nonce step
SignatureAlgorithm SignatureAlgorithm // 签名算法
Signature []byte // 签名
}
// Read implements io.Reader
func (m MsgECDHESecretKeyExchange) Read(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
if err := bs.WriteUint8(uint8(m.NamedCurve)); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteBytes(m.PublicKey); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteBytes(m.IV); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteBytes(m.Nonce); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteBytes(m.NonceStep); err != nil {
return bs.BytesWritten(), err
}
if _, err := binaryutil.ReadFrom(&bs, m.SignatureAlgorithm); err != nil {
return bs.BytesWritten(), err
}
if err := bs.WriteBytes(m.Signature); err != nil {
return bs.BytesWritten(), err
}
return bs.BytesWritten(), nil
}
// Write implements io.Writer
func (m *MsgECDHESecretKeyExchange) Write(p []byte) (int, error) {
bs := binaryutil.NewBigEndianStream(p)
namedCurve, err := bs.ReadUint8()
if err != nil {
return bs.BytesRead(), err
}
m.NamedCurve = NamedCurve(namedCurve)
m.PublicKey, err = bs.ReadBytesRef()
if err != nil {
return bs.BytesRead(), err
}
m.IV, err = bs.ReadBytesRef()
if err != nil {
return bs.BytesRead(), err
}
m.Nonce, err = bs.ReadBytesRef()
if err != nil {
return bs.BytesRead(), err
}
m.NonceStep, err = bs.ReadBytesRef()
if err != nil {
return bs.BytesRead(), err
}
if _, err := bs.WriteTo(&m.SignatureAlgorithm); err != nil {
return bs.BytesRead(), err
}
m.Signature, err = bs.ReadBytesRef()
if err != nil {
return bs.BytesRead(), err
}
return bs.BytesRead(), nil
}
// Size 大小
func (m MsgECDHESecretKeyExchange) Size() int {
return binaryutil.SizeofUint8() + binaryutil.SizeofBytes(m.PublicKey) + binaryutil.SizeofBytes(m.IV) +
binaryutil.SizeofBytes(m.Nonce) + binaryutil.SizeofBytes(m.NonceStep) + m.SignatureAlgorithm.Size() +
binaryutil.SizeofBytes(m.Signature)
}
// MsgId 消息Id
func (MsgECDHESecretKeyExchange) MsgId() MsgId {
return MsgId_ECDHESecretKeyExchange
}
// Clone 克隆消息对象
func (m *MsgECDHESecretKeyExchange) Clone() Msg {
return &MsgECDHESecretKeyExchange{
NamedCurve: m.NamedCurve,
PublicKey: bytes.Clone(m.PublicKey),
IV: bytes.Clone(m.IV),
Nonce: bytes.Clone(m.Nonce),
NonceStep: bytes.Clone(m.NonceStep),
SignatureAlgorithm: m.SignatureAlgorithm,
Signature: bytes.Clone(m.Signature),
}
}