/
auth.go
705 lines (583 loc) · 18.9 KB
/
auth.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
* Copyright 2018 The openwallet Authors
* This file is part of the openwallet library.
*
* The openwallet library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The openwallet library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package owtp
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/blocktree/go-owcrypt"
"github.com/blocktree/openwallet/common"
"github.com/blocktree/openwallet/crypto"
"github.com/blocktree/openwallet/log"
"github.com/mr-tron/base58/base58"
"math/big"
)
//KeyAgreement 协商密码
type KeyAgreement struct {
EncryptType string //协商密码类型
PublicKeyInitiator string //发送方:本地公钥
PrivateKeyInitiator string //发送方:本地私钥
TmpPublicKeyInitiator string //发送方:临时公钥
TmpPrivateKeyInitiator string //发送方:临时私钥
PublicKeyResponder string //响应方:本地公钥
PrivateKeyResponder string //响应方:本地私钥
TmpPublicKeyResponder string //响应方:临时公钥
TmpPrivateKeyResponder string //响应方:临时私钥
S2 string //响应方:本地验证码,RequestKeyAgreement生成
SB string //响应方:生成协商密码的必要验证码,RequestKeyAgreement生成
SA string //发送方:本地验证码,ResponseKeyAgreement生成
Key string //协商的密钥
}
//Authorization 授权
type Authorization interface {
//EnableAuth 开启授权
EnableAuth() bool
//GenerateSignature 生成签名,并把签名加入到DataPacket中
GenerateSignature(data *DataPacket) bool
//VerifySignature 校验签名,若验证错误,可更新错误信息到DataPacket中
VerifySignature(data *DataPacket) bool
//EnableKeyAgreement 开启密码协商
EnableKeyAgreement() bool
//InitKeyAgreement 发起协商
InitKeyAgreement(keyAgreement *KeyAgreement) error
//RequestKeyAgreement 请求协商,计算密钥
RequestKeyAgreement(keyAgreement *KeyAgreement) error
//ResponseKeyAgreement 响应协商,计算密钥
ResponseKeyAgreement(keyAgreement *KeyAgreement) error
//VerifyKeyAgreement 验证协商结果
VerifyKeyAgreement(keyAgreement *KeyAgreement) bool
//EncryptData 加密数据
EncryptData(data []byte, key []byte) ([]byte, error)
//DecryptData 解密数据
DecryptData(data []byte, key []byte) ([]byte, error)
//EncryptDataPacket 加密数据
EncryptDataPacket(data *DataPacket, key []byte) error
//DecryptDataPacket 解密数据
DecryptDataPacket(data *DataPacket, key []byte) error
}
type AuthorizationBase struct{}
//EnableAuth 开启授权
func (base *AuthorizationBase) EnableAuth() bool {
return false
}
//GenerateSignature 生成签名,并把签名加入到DataPacket中
func (base *AuthorizationBase) GenerateSignature(data *DataPacket) bool {
return true
}
//VerifySignature 校验签名,若验证错误,可更新错误信息到DataPacket中
func (base *AuthorizationBase) VerifySignature(data *DataPacket) bool {
return true
}
//EnableKeyAgreement 开启密码协商
func (base *AuthorizationBase) EnableKeyAgreement() bool {
return false
}
//InitKeyAgreement 发起协商
func (base *AuthorizationBase) InitKeyAgreement(keyAgreement *KeyAgreement) error {
return fmt.Errorf("InitKeyAgreement is not implemented")
}
//RequestKeyAgreement 请求协商,计算密钥
func (base *AuthorizationBase) RequestKeyAgreement(keyAgreement *KeyAgreement) error {
return fmt.Errorf("RequestKeyAgreement is not implemented")
}
//ResponseKeyAgreement 响应协商,计算密钥
func (base *AuthorizationBase) ResponseKeyAgreement(keyAgreement *KeyAgreement) error {
return fmt.Errorf("ResponseKeyAgreement is not implemented")
}
//VerifyKeyAgreement 是否完成密码协商,验证协商结果
func (base *AuthorizationBase) VerifyKeyAgreement(keyAgreement *KeyAgreement) bool {
return false
}
//EncryptData 加密数据
func (base *AuthorizationBase) EncryptData(data []byte, key []byte) ([]byte, error) {
return nil, fmt.Errorf("EncryptData is not implemented")
}
//DecryptData 解密数据
func (base *AuthorizationBase) DecryptData(data []byte, key []byte) ([]byte, error) {
return nil, fmt.Errorf("DecryptData is not implemented")
}
//EncryptDataPacket 加密数据
func (base *AuthorizationBase) EncryptDataPacket(data *DataPacket, key []byte) error {
return fmt.Errorf("EncryptDataPacket is not implemented")
}
//DecryptDataPacket 解密数据
func (base *AuthorizationBase) DecryptDataPacket(data *DataPacket, key []byte) error {
return fmt.Errorf("DecryptDataPacket is not implemented")
}
type Certificate struct {
privateKeyBytes []byte
publicKeyBytes []byte
//consultType string //协商类型
}
//RandomPrivateKey 生成随机私钥
func NewRandomCertificate() Certificate {
priKey := make([]byte, 32)
_, err := rand.Read(priKey)
if err != nil {
return Certificate{}
}
pubkey, ret := owcrypt.GenPubkey(priKey, owcrypt.ECC_CURVE_SM2_STANDARD)
if ret != owcrypt.SUCCESS {
return Certificate{}
}
cert := Certificate{
privateKeyBytes: priKey,
publicKeyBytes: pubkey,
}
return cert
}
func NewCertificate(privateKey string, consultType ...string) (Certificate, error) {
if len(privateKey) == 0 {
return Certificate{}, fmt.Errorf("private key is empty")
}
priKey, err := base58.Decode(privateKey)
if err != nil {
return Certificate{}, err
}
pubkey, ret := owcrypt.GenPubkey(priKey, owcrypt.ECC_CURVE_SM2_STANDARD)
if ret != owcrypt.SUCCESS {
return Certificate{}, err
}
//log.Debug("SME PUB:", hex.EncodeToString(pubkey))
return Certificate{
privateKeyBytes: priKey,
publicKeyBytes: pubkey,
}, nil
}
func (cert *Certificate) KeyPair() (priv string, pub string) {
return base58.Encode(cert.privateKeyBytes), base58.Encode(cert.publicKeyBytes)
}
func (cert *Certificate) ID() string {
if len(cert.publicKeyBytes) == 0 {
return ""
}
nodeID := owcrypt.Hash(cert.publicKeyBytes, 0, owcrypt.HASH_ALG_SHA256)
return base58.Encode(nodeID)
}
func (cert *Certificate) PublicKeyBytes() []byte {
return cert.publicKeyBytes
}
func (cert *Certificate) PrivateKeyBytes() []byte {
return cert.privateKeyBytes
}
//Authorization 授权
type OWTPAuth struct {
AuthorizationBase
//本地公钥用于验证数据包合法性
localPublicKey []byte
//本地私钥用户签名数据包,生成协商密钥
localPrivateKey []byte
//远程节点的公钥
remotePublicKey []byte
//是否开启
enable bool
//是否协商
isConsult bool
}
func NewOWTPAuthWithCertificate(cert Certificate, enable bool) (*OWTPAuth, error) {
//var (
// isConsult bool
//)
//
//if len(cert.consultType) > 0 {
// isConsult = true
//} else {
// isConsult = false
//}
auth := &OWTPAuth{
localPrivateKey: cert.PrivateKeyBytes(),
localPublicKey: cert.PublicKeyBytes(),
//consultType: cert.consultType,
//isConsult: isConsult,
enable: enable,
}
return auth, nil
}
// RemotePID 远程节点ID
func (auth *OWTPAuth) RemotePID() string {
//nodeID := crypto.SHA256(auth.remotePublicKey)
nodeID := owcrypt.Hash(auth.remotePublicKey, 0, owcrypt.HASH_ALG_SHA256)
return base58.Encode(nodeID)
}
// LocalPID 远程节点ID
func (auth *OWTPAuth) LocalPID() string {
//log.Debug("input bytes:", auth.localPublicKey)
//nodeID1 := crypto.SHA256(auth.localPublicKey)
nodeID := owcrypt.Hash(auth.localPublicKey, 0, owcrypt.HASH_ALG_SHA256)
//log.Debug("output1 bytes:", nodeID1)
//log.Debug("output2 bytes:", nodeID)
return base58.Encode(nodeID)
}
//GenerateSignature 生成签名,并把签名加入到DataPacket中
func (auth *OWTPAuth) GenerateSignature(data *DataPacket) bool {
if auth.EnableAuth() {
pub := base58.Encode(auth.localPublicKey)
//给数据包生成签名
dataString := common.NewString(data.Data)
plainText := fmt.Sprintf("%d%s%d%d%s", data.Req, data.Method, data.Nonce, data.Timestamp, dataString)
hash := owcrypt.Hash([]byte(plainText), 0, owcrypt.HASH_ALG_DOUBLE_SHA256)
nodeID := owcrypt.Hash(auth.localPublicKey, 0, owcrypt.HASH_ALG_SHA256)
signature, _, ret := owcrypt.Signature(auth.localPrivateKey, nodeID, hash, owcrypt.ECC_CURVE_SM2_STANDARD)
if ret != owcrypt.SUCCESS {
return false
}
data.SecretData.PublicKeyInitiator = pub
data.Signature = base58.Encode(signature)
//log.Debug("GenerateSignature packet.Signature: ", data.Signature)
}
return true
}
//VerifySignature 校验签名,若验证错误,可更新错误信息到DataPacket中
func (auth *OWTPAuth) VerifySignature(data *DataPacket) bool {
//验证数据包签名是否合法
if auth.EnableAuth() {
var publickey []byte
if len(data.SecretData.PublicKeyInitiator) > 0 {
publickey, _ = base58.Decode(data.SecretData.PublicKeyInitiator)
} else {
publickey = auth.remotePublicKey
}
if len(publickey) == 0 {
return false
}
//log.Debug("VerifySignature packet.Req: ", data.Req)
//log.Debug("VerifySignature packet.Signature: ", data.Signature)
dataString := data.Data.(string)
plainText := fmt.Sprintf("%d%s%d%d%s", data.Req, data.Method, data.Nonce, data.Timestamp, dataString)
//log.Debug("VerifySignature plainText: ", plainText)
hash := owcrypt.Hash([]byte(plainText), 0, owcrypt.HASH_ALG_DOUBLE_SHA256)
//log.Debug("VerifySignature hash: ", hex.EncodeToString(hash))
nodeID := owcrypt.Hash(publickey, 0, owcrypt.HASH_ALG_SHA256)
//log.Debug("VerifySignature remotePublicKey: ", hex.EncodeToString(auth.remotePublicKey))
//log.Debug("VerifySignature nodeID: ", base58.Encode(nodeID))
//log.Debug("VerifySignature remotePID: ", auth.RemotePID())
signature, err := base58.Decode(data.Signature)
if err != nil {
return false
}
if base58.Encode(nodeID) != auth.RemotePID() {
return false
}
ret := owcrypt.Verify(publickey, nodeID, hash, signature, owcrypt.ECC_CURVE_SM2_STANDARD)
if ret != owcrypt.SUCCESS {
return false
}
}
return true
}
//EnableAuth 开启授权
func (auth *OWTPAuth) EnableAuth() bool {
return auth.enable
}
//EncryptData 加密数据
func (auth *OWTPAuth) EncryptData(data []byte, key []byte) ([]byte, error) {
//使用协商密钥加密数据
if auth.EnableKeyAgreement() && len(key) > 0 && len(data) > 0 {
encD, err := crypto.AESEncrypt(data, key)
if err != nil {
return data, err
}
encS := base64.StdEncoding.EncodeToString(encD)
return []byte(encS), nil
}
return data, nil
}
//DecryptData 解密数据
func (auth *OWTPAuth) DecryptData(data []byte, key []byte) ([]byte, error) {
//使用协商密钥解密数据
if auth.EnableKeyAgreement() && len(key) > 0 && len(data) > 0 {
encD, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return data, err
}
decD, err := crypto.AESDecrypt(encD, key)
if err != nil {
return data, err
}
return decD, nil
}
return data, nil
}
//EncryptDataPacket 加密数据
func (auth *OWTPAuth) EncryptDataPacket(packet *DataPacket, key []byte) error {
var dataByte []byte
if encStr, ok := packet.Data.(string); ok {
//无需转换为json
dataByte = []byte(encStr)
} else {
enc, encErr := json.Marshal(packet.Data)
if encErr != nil {
return fmt.Errorf("json.Marshal data failed")
}
dataByte = enc
}
//使用协商密钥加密数据
if auth.EnableKeyAgreement() && len(key) > 0 && len(dataByte) > 0 {
//DataPacket = 1时,使用新的加密方案
if packet.Version == DataPacketVersionV1 {
//把nonce作为salt
nonceBit := big.NewInt(int64(packet.Nonce)).Bytes()
h := hmac.New(sha256.New, nonceBit)
h.Write(key)
md := h.Sum(nil)
key = md
}
encD, err := crypto.AESEncrypt(dataByte, key)
if err != nil {
return err
}
chipText := base64.StdEncoding.EncodeToString(encD)
packet.Data = chipText
} else {
packet.Data = string(dataByte)
}
return nil
}
//DecryptDataPacket 解密数据
func (auth *OWTPAuth) DecryptDataPacket(packet *DataPacket, key []byte) error {
rawData, ok := packet.Data.(string)
if !ok {
return fmt.Errorf("data parse failed")
}
//使用协商密钥解密数据
if auth.EnableKeyAgreement() && len(key) > 0 && len(rawData) > 0 {
//DataPacket = 1时,使用新的加密方案
if packet.Version == DataPacketVersionV1 {
//把nonce作为salt
nonceBit := big.NewInt(int64(packet.Nonce)).Bytes()
h := hmac.New(sha256.New, nonceBit)
h.Write(key)
md := h.Sum(nil)
key = md
}
encD, err := base64.StdEncoding.DecodeString(rawData)
if err != nil {
return err
}
decD, err := crypto.AESDecrypt(encD, key)
if err != nil {
return err
}
packet.Data = decD
} else {
packet.Data = []byte(rawData)
}
return nil
}
//AuthHeader 返回授权头
func (auth *OWTPAuth) HTTPAuthHeader() map[string]string {
if len(auth.localPublicKey) == 0 {
return nil
}
a := base58.Encode(auth.localPublicKey)
return map[string]string{
"a": a,
}
}
//EnableKeyAgreement 开启密码协商
func (auth *OWTPAuth) EnableKeyAgreement() bool {
return auth.isConsult
}
//RandomPrivateKey 生成随机私钥
func RandomPrivateKey() string {
buf := make([]byte, 32)
_, err := rand.Read(buf)
if err != nil {
return ""
}
key := base58.Encode(buf)
return key
}
func (auth *OWTPAuth) VerifyKeyAgreement(keyAgreement *KeyAgreement) bool {
//没有完成密码协商
if len(keyAgreement.SA) == 0 {
//请求协商密码参数存在
if len(keyAgreement.PublicKeyInitiator) > 0 &&
len(keyAgreement.TmpPublicKeyInitiator) > 0 {
auth.isConsult = false
return false
} else {
//没有请求协商密码参数
auth.isConsult = false
return true
}
}
//auth.isConsult = true
if len(keyAgreement.S2) == 0 {
return false
}
s2, err := base58.Decode(keyAgreement.S2)
if err != nil {
return false
}
sa, err := base58.Decode(keyAgreement.SA)
if err != nil {
return false
}
ret := owcrypt.KeyAgreement_responder_step2(sa, s2, owcrypt.ECC_CURVE_SM2_STANDARD)
if ret != owcrypt.SUCCESS {
return false
}
if Debug {
log.Debug("VerifyKeyAgreement passed")
}
auth.isConsult = true
return true
}
//InitKeyAgreement 发起协商
func (auth *OWTPAuth) InitKeyAgreement(keyAgreement *KeyAgreement) error {
tmpPrikeyInitiator, tmpPubkeyInitiator := owcrypt.KeyAgreement_initiator_step1(owcrypt.ECC_CURVE_SM2_STANDARD)
//auth.tmpPrivateKey = tmpPrikeyInitiator
//auth.tmpPublicKey = tmpPubkeyInitiator
//auth.consultType = keyAgreement.EncryptType
keyAgreement.TmpPrivateKeyInitiator = base58.Encode(tmpPrikeyInitiator)
keyAgreement.TmpPublicKeyInitiator = base58.Encode(tmpPubkeyInitiator)
keyAgreement.PublicKeyInitiator = base58.Encode(auth.localPublicKey)
auth.isConsult = true
return nil
}
//RequestKeyAgreement 请求协商
func (auth *OWTPAuth) RequestKeyAgreement(keyAgreement *KeyAgreement) error {
pubkeyBytes, err := base58.Decode(keyAgreement.PublicKeyInitiator)
if err != nil {
return err
}
tmpPubkeyBytes, err := base58.Decode(keyAgreement.TmpPublicKeyInitiator)
if err != nil {
return err
}
localPubkey, err := base58.Decode(keyAgreement.PublicKeyResponder)
if err != nil {
return err
}
localPrivkey, err := base58.Decode(keyAgreement.PrivateKeyResponder)
if err != nil {
return err
}
auth.localPublicKey = localPubkey
auth.localPrivateKey = localPrivkey
key, tmpPubkeyResponder, s2, sb, err := auth.requestKeyAgreement(pubkeyBytes, tmpPubkeyBytes, localPubkey, localPrivkey)
if err != nil {
return err
}
//auth.secretKey = key
//auth.localChecksum = s2
auth.isConsult = true
keyAgreement.SB = base58.Encode(sb)
keyAgreement.Key = base58.Encode(key)
keyAgreement.S2 = base58.Encode(s2)
keyAgreement.TmpPublicKeyResponder = base58.Encode(tmpPubkeyResponder)
//result := map[string]interface{}{
// "pubkeyOther": base58.Encode(auth.localPublicKey),
// "tmpPubkeyOther": base58.Encode(tmpPubkeyResponder),
// "sb": base58.Encode(sb),
// "secretKey": base58.Encode(key),
// "localChecksum": base58.Encode(s2),
//}
return nil
}
//ResponseKeyAgreement 响应协商
func (auth *OWTPAuth) ResponseKeyAgreement(keyAgreement *KeyAgreement) error {
remotePublicKeyBytes, err := base58.Decode(keyAgreement.PublicKeyResponder)
if err != nil {
return err
}
remoteTmpPublicKeyBytes, err := base58.Decode(keyAgreement.TmpPublicKeyResponder)
if err != nil {
return err
}
sbBytes, err := base58.Decode(keyAgreement.SB)
if err != nil {
return err
}
tmpPublicKeyBytes, err := base58.Decode(keyAgreement.TmpPublicKeyInitiator)
if err != nil {
return err
}
tmpPrivateKeyBytes, err := base58.Decode(keyAgreement.TmpPrivateKeyInitiator)
if err != nil {
return err
}
key, sa, err := auth.responseKeyAgreement(remotePublicKeyBytes, remoteTmpPublicKeyBytes, sbBytes, tmpPublicKeyBytes, tmpPrivateKeyBytes)
if err != nil {
return err
}
//auth.secretKey = key
//auth.localChecksum = sa
auth.isConsult = true
//result := map[string]interface{}{
// "secretKey": base58.Encode(key),
// "localChecksum": base58.Encode(sa),
//}
keyAgreement.Key = base58.Encode(key)
keyAgreement.SA = base58.Encode(sa)
return nil
}
//requestKeyAgreement 请求协商
func (auth *OWTPAuth) requestKeyAgreement(
pubkeyInitiatorBytes, tmpPubkeyInitiatorBytes []byte,
pubkeyResponderBytes, privkeyResponderBytes []byte) ([]byte, []byte, []byte, []byte, error) {
IDinitiator := owcrypt.Hash(pubkeyInitiatorBytes, 0, owcrypt.HASH_ALG_SHA256)
IDresponder := owcrypt.Hash(pubkeyResponderBytes, 0, owcrypt.HASH_ALG_SHA256)
//key, tmpPubkeyResponder, S2, SB, ret := owcrypt.KeyAgreement_responder_step1(
// IDinitiator,
// 32,
// IDresponder,
// 32,
// auth.localPrivateKey,
// auth.localPublicKey,
// pubkeyBytes,
// tmpPubkeyBytes,
// 32,
// owcrypt.ECC_CURVE_SM2_STANDARD)
key, tmpPubkeyResponder, S2, SB, ret := owcrypt.KeyAgreement_responder_ElGamal_step1(
IDinitiator,
IDresponder,
privkeyResponderBytes,
pubkeyResponderBytes,
pubkeyInitiatorBytes,
tmpPubkeyInitiatorBytes,
32,
privkeyResponderBytes,
owcrypt.ECC_CURVE_SM2_STANDARD)
if ret != owcrypt.SUCCESS {
return nil, nil, nil, nil, fmt.Errorf("KeyAgreement_responder_ElGamal_step1 failed")
}
return key, tmpPubkeyResponder, S2, SB, nil
}
//responseKeyAgreement 响应协商
func (auth *OWTPAuth) responseKeyAgreement(pubkeyResponder, tmpPubkeyResponder, sb, tmpPublicKey, tmpPrivateKey []byte) ([]byte, []byte, error) {
IDinitiator := owcrypt.Hash(auth.localPublicKey, 0, owcrypt.HASH_ALG_SHA256)
IDresponder := owcrypt.Hash(pubkeyResponder, 0, owcrypt.HASH_ALG_SHA256)
retA, SA, ret := owcrypt.KeyAgreement_initiator_step2(
IDinitiator,
IDresponder,
auth.localPrivateKey,
auth.localPublicKey,
pubkeyResponder,
tmpPrivateKey,
tmpPublicKey,
tmpPubkeyResponder,
sb,
32,
owcrypt.ECC_CURVE_SM2_STANDARD)
if ret != owcrypt.SUCCESS {
return nil, nil, fmt.Errorf("KeyAgreement_initiator_step2 failed")
}
return retA, SA, nil
}