Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
a1468575
Commit
a1468575
authored
Nov 24, 2021
by
hezhengjun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove tasshsm
parent
9ed41107
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
0 additions
and
695 deletions
+0
-695
Makefile
plugin/crypto/tasshsm/Makefile
+0
-11
adapter.go
plugin/crypto/tasshsm/adapter/adapter.go
+0
-182
main.go
plugin/crypto/tasshsm/app/main.go
+0
-157
SDF4PCIeSM.h
plugin/crypto/tasshsm/include/SDF4PCIeSM.h
+0
-0
TassAPI4PCIeSM.h
plugin/crypto/tasshsm/include/TassAPI4PCIeSM.h
+0
-0
TassCtlAPI4PCIeSM.h
plugin/crypto/tasshsm/include/TassCtlAPI4PCIeSM.h
+0
-0
TassType4PCIeSM.h
plugin/crypto/tasshsm/include/TassType4PCIeSM.h
+0
-123
secp256k1_test.go
plugin/crypto/tasshsm/secp256k1/secp256k1_test.go
+0
-97
sm2_test.go
plugin/crypto/tasshsm/sm2/sm2_test.go
+0
-125
No files found.
plugin/crypto/tasshsm/Makefile
deleted
100644 → 0
View file @
9ed41107
proj
:=
"tass"
.PHONY
:
default build sm2
default
:
build
build
:
g++
-o
tass demo/main.cpp
-I
include
-std
=
c++11
-Llinux64
-lTassSDF4PCIeSM
-lTassAPI4PCIECrypotoCard
-ltass_pcie_api
sm2
:
g++
-o
test_sm2 sm2/main.cpp
-I
include
-std
=
c++11
-Llinux64
-lTassSDF4PCIeSM
-lTassAPI4PCIECrypotoCard
-ltass_pcie_api
plugin/crypto/tasshsm/adapter/adapter.go
deleted
100644 → 0
View file @
9ed41107
package
adapter
//#cgo CFLAGS: -I../include
//#cgo LDFLAGS: -L../linux64 -lTassSDF4PCIeSM -ltass_pcie_api -lTassSDF4PCIeSM
//#cgo LDFLAGS: -ldl -lpthread -lc
//#include <stdlib.h>
//#include <stdio.h>
//#include <SDF4PCIeSM.h>
//#include <TassAPI4PCIeSM.h>
//unsigned char SM2ID[16] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38};
//unsigned char sm3Hash[32] = { 0 };
//unsigned char RS[64] = { 0 };
//void* g_hDev = NULL, *g_hSess = NULL;
//const unsigned char*skCipherByKek = NULL;
//unsigned char*RSPtr = RS;
//unsigned int signatureLen = 64;
//unsigned int *signatureLenPtr = &signatureLen;
//int setupHSM()
//{
// auto rt = SDF_OpenDevice(&g_hDev);
// if (rt) {
// printf("SDF_OpenDevice failed %#08x\n", rt);
// return rt;
// }
// rt = SDF_OpenSession(g_hDev, &g_hSess);
// if (rt) {
// printf("SDF_OpenSession failed %#08x\n", rt);
// SDF_CloseDevice(g_hDev);
// return rt;
// }
//}
//int pass_voidHandle(void *handle) {
// (void)handle;
//}
//void closeHSM() {
// SDF_CloseSession(g_hSess);
// SDF_CloseDevice(g_hDev);
//}
import
"C"
import
(
"errors"
"fmt"
"unsafe"
"github.com/33cn/chain33/common/crypto"
)
var
(
//g_hDev *C.int
//g_hSess *C.void
//g_passwd []byte
//g_passwdLen int
//SM2ID = []byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38}
)
const
(
SDF_Success
=
C
.
int
(
0
)
SM2IDSize
=
C
.
uint
(
16
)
)
//OpenHSMSession:打开TASS HSM PCIe设备并建立session
func
OpenHSMSession
()
error
{
C
.
pass_voidHandle
(
C
.
g_hDev
)
if
rt
:=
C
.
setupHSM
();
int
(
rt
)
!=
0
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Failde to setup HSM with error code:%#08x"
,
int
(
rt
)))
}
return
nil
}
//CloseHSMSession:关闭设备
func
CloseHSMSession
()
error
{
C
.
closeHSM
()
return
nil
}
//获取私钥访问权限
func
GetPrivateKeyAccessRight
(
passwordStr
string
,
keyIndex
int
)
error
{
passwd
:=
(
*
C
.
uchar
)(
unsafe
.
Pointer
(
C
.
CString
(
passwordStr
)))
defer
C
.
free
(
unsafe
.
Pointer
(
passwd
))
passwdLen
:=
len
(
passwordStr
)
rt
:=
C
.
TassGetPrivateKeyAccessRight
(
C
.
g_hSess
,
C
.
uint
(
keyIndex
),
C
.
TA_ALG_ECC_SECP_256K1
,
passwd
,
C
.
uint
(
passwdLen
))
if
SDF_Success
!=
rt
{
return
errors
.
New
(
fmt
.
Sprintf
(
"GetPrivateKeyAccessRight failed %#08x"
,
int
(
rt
)))
}
return
nil
}
//释放私钥访问权限
func
ReleaeAccessRight
(
keyIndex
int
)
error
{
rt
:=
C
.
SDF_ReleasePrivateKeyAccessRight
(
C
.
g_hSess
,
C
.
uint
(
keyIndex
))
if
SDF_Success
!=
rt
{
return
errors
.
New
(
fmt
.
Sprintf
(
"ReleaeAccessRight failed %#08x"
,
int
(
rt
)))
}
return
nil
}
//获取签名
func
SignSecp256k1
(
msg
[]
byte
,
keyIndex
int
)
(
signatureR
,
signatureS
[]
byte
,
err
error
)
{
hash
:=
crypto
.
Sha256
(
msg
)
hash2sign
:=
(
*
C
.
uchar
)(
C
.
CBytes
(
hash
))
defer
C
.
free
(
unsafe
.
Pointer
(
hash2sign
))
rt
:=
C
.
TassECCPrivateKeySign_Eth
(
C
.
g_hSess
,
C
.
TA_ALG_ECC_SECP_256K1
,
C
.
uint
(
keyIndex
),
C
.
uint
(
256
),
C
.
skCipherByKek
,
C
.
uint
(
0
),
hash2sign
,
C
.
uint
(
32
),
C
.
RSPtr
,
C
.
signatureLenPtr
)
if
SDF_Success
!=
rt
{
return
nil
,
nil
,
errors
.
New
(
fmt
.
Sprintf
(
"TassECCPrivateKeySign failed %#08x"
,
int
(
rt
)))
}
r
:=
C
.
GoBytes
(
unsafe
.
Pointer
(
&
C
.
RS
[
0
]),
C
.
int
(
32
))
s
:=
C
.
GoBytes
(
unsafe
.
Pointer
(
&
C
.
RS
[
32
]),
C
.
int
(
32
))
return
r
,
s
,
nil
}
func
SignSM2Internal
(
msg
[]
byte
,
keyIndex
int
)
(
signatureR
,
signatureS
[]
byte
,
err
error
)
{
var
signPubKey
C
.
ECCrefPublicKey
rt
:=
C
.
SDF_ExportSignPublicKey_ECC
(
C
.
g_hSess
,
C
.
uint
(
keyIndex
),
&
signPubKey
)
if
SDF_Success
!=
rt
{
return
nil
,
nil
,
errors
.
New
(
fmt
.
Sprintf
(
"SDF_ExportSignPublicKey_ECC failed:%08x"
,
int
(
rt
)))
}
//fmt.Println("SDF_ExportSignPublicKey_ECC success for chain33 verification\n")
//fmt.Printf("chain33 PubKey.bits: %d\n", int(signPubKey.bits))
//fmt.Printf("chain33 PubKey.x: %s\n", common.ToHex(C.GoBytes(unsafe.Pointer(&signPubKey.x[0]), C.int(64))))
//fmt.Printf("chain33 PubKey.y: %s\n", common.ToHex(C.GoBytes(unsafe.Pointer(&signPubKey.y[0]), C.int(64))))
rt
=
C
.
SDF_HashInit
(
C
.
g_hSess
,
C
.
SGD_SM3
,
&
signPubKey
,
&
C
.
SM2ID
[
0
],
SM2IDSize
)
if
SDF_Success
!=
rt
{
return
nil
,
nil
,
errors
.
New
(
fmt
.
Sprintf
(
"SDF_HashInit failed %#08x"
,
int
(
rt
)))
}
//fmt.Println("SDF_HashInit success\n")
msg2C
:=
(
*
C
.
uchar
)(
C
.
CBytes
(
msg
))
defer
C
.
free
(
unsafe
.
Pointer
(
msg2C
))
rt
=
C
.
SDF_HashUpdate
(
C
.
g_hSess
,
msg2C
,
C
.
uint
(
len
(
msg
)))
if
SDF_Success
!=
rt
{
return
nil
,
nil
,
errors
.
New
(
fmt
.
Sprintf
(
"SDF_HashUpdate failed %#08x"
,
int
(
rt
)))
}
//fmt.Println("SDF_HashUpdate success\n")
hashlen
:=
C
.
uint
(
32
)
rt
=
C
.
SDF_HashFinal
(
C
.
g_hSess
,
&
C
.
sm3Hash
[
0
],
&
hashlen
)
if
SDF_Success
!=
rt
{
return
nil
,
nil
,
errors
.
New
(
fmt
.
Sprintf
(
"SDF_HashFinal failed %#08x"
,
int
(
rt
)))
}
//fmt.Printf("SDF_HashFinal success! sm3Hash = %s\n", common.ToHex(C.GoBytes(unsafe.Pointer(&C.sm3Hash[0]), C.int(32))))
//0x726106a793a45af8ee1b9b6781b87391e4b49304a5f12f6dff82c7edb5f58390
var
sign
C
.
ECCSignature
rt
=
C
.
SDF_InternalSign_ECC
(
C
.
g_hSess
,
C
.
uint
(
keyIndex
),
&
C
.
sm3Hash
[
0
],
hashlen
,
&
sign
)
if
SDF_Success
!=
rt
{
return
nil
,
nil
,
errors
.
New
(
fmt
.
Sprintf
(
"SDF_InternalSign_ECC failed %#08x"
,
int
(
rt
)))
}
//fmt.Println("SDF_InternalSign_ECC success! ")
r
:=
C
.
GoBytes
(
unsafe
.
Pointer
(
&
sign
.
r
[
0
]),
C
.
int
(
64
))
s
:=
C
.
GoBytes
(
unsafe
.
Pointer
(
&
sign
.
s
[
0
]),
C
.
int
(
64
))
return
r
,
s
,
nil
}
func
MakeDERsignature
(
rb
,
sb
[]
byte
)
[]
byte
{
if
rb
[
0
]
>
0x7F
{
rb
=
append
([]
byte
{
0
},
rb
...
)
}
if
sb
[
0
]
>
0x7F
{
sb
=
append
([]
byte
{
0
},
sb
...
)
}
// total length of returned signature is 1 byte for each magic and
// length (6 total), plus lengths of r and s
length
:=
6
+
len
(
rb
)
+
len
(
sb
)
b
:=
make
([]
byte
,
length
)
b
[
0
]
=
0x30
b
[
1
]
=
byte
(
length
-
2
)
b
[
2
]
=
0x02
b
[
3
]
=
byte
(
len
(
rb
))
offset
:=
copy
(
b
[
4
:
],
rb
)
+
4
b
[
offset
]
=
0x02
b
[
offset
+
1
]
=
byte
(
len
(
sb
))
copy
(
b
[
offset
+
2
:
],
sb
)
return
b
}
plugin/crypto/tasshsm/app/main.go
deleted
100644 → 0
View file @
9ed41107
package
main
import
(
"fmt"
"math/big"
"time"
"github.com/33cn/chain33/system/crypto/secp256k1"
"github.com/33cn/chain33/common/crypto"
ethCrypto
"github.com/ethereum/go-ethereum/crypto"
"github.com/33cn/chain33/system/crypto/sm2"
gmsm_sm2
"github.com/tjfoc/gmsm/sm2"
"github.com/33cn/chain33/common"
"github.com/33cn/plugin/plugin/crypto/tasshsm/adapter"
)
func
verifySM2Signature
(
rBytes
,
sBytes
,
msg
[]
byte
)
bool
{
xBytes
,
_
:=
common
.
FromHex
(
"0000000000000000000000000000000000000000000000000000000000000000FD4241057FEC6CBEEC501F7E1763751B8F6DFCFB910FB634FBB76A16639EF172"
)
yBytes
,
_
:=
common
.
FromHex
(
"00000000000000000000000000000000000000000000000000000000000000001C6DA89F9C1A5EE9B6108E5A2A5FE336962630A34DBA1AF428451E1CE63BB3CF"
)
x
:=
new
(
big
.
Int
)
.
SetBytes
(
xBytes
)
y
:=
new
(
big
.
Int
)
.
SetBytes
(
yBytes
)
publicKey
:=
&
gmsm_sm2
.
PublicKey
{
X
:
x
,
Y
:
y
,
}
var
pubSM2
sm2
.
PubKeySM2
copy
(
pubSM2
[
:
],
gmsm_sm2
.
Compress
(
publicKey
))
r
:=
new
(
big
.
Int
)
.
SetBytes
(
rBytes
)
s
:=
new
(
big
.
Int
)
.
SetBytes
(
sBytes
)
signature
:=
sm2
.
SignatureSM2
(
sm2
.
Serialize
(
r
,
s
))
return
pubSM2
.
VerifyBytes
(
msg
,
signature
)
}
func
main
()
{
if
err
:=
adapter
.
OpenHSMSession
();
nil
!=
err
{
panic
(
"Failed to OpenHSMSession"
)
}
fmt
.
Println
(
"Succeed to OpenHSMSession"
)
fmt
.
Println
(
" "
)
fmt
.
Println
(
" "
)
fmt
.
Println
(
" "
)
keyIndex
:=
1
passwd
:=
"a1234567"
//passwd := []byte("a1234567")
if
err
:=
adapter
.
GetPrivateKeyAccessRight
(
passwd
,
keyIndex
);
nil
!=
err
{
panic
(
"Failed to GetPrivateKeyAccessRight"
)
}
verifySecp256k1
(
keyIndex
)
if
err
:=
adapter
.
ReleaeAccessRight
(
keyIndex
);
nil
!=
err
{
panic
(
"Failed to GetPrivateKeyAccessRight"
)
}
adapter
.
CloseHSMSession
()
}
func
verifySecp256k1
(
keyIndex
int
)
{
msg
,
_
:=
common
.
FromHex
(
"456789"
)
r
,
s
,
err
:=
adapter
.
SignSecp256k1
(
msg
,
keyIndex
)
if
err
!=
nil
{
panic
(
"Failed to SignSecp256k1 due to:"
+
err
.
Error
())
}
fmt
.
Println
(
"signature R="
,
common
.
ToHex
(
r
))
fmt
.
Println
(
"signature S="
,
common
.
ToHex
(
s
))
///////构建公钥////////
pub
,
_
:=
common
.
FromHex
(
"04C24FBA65F8CD81223D2935EDEA663048A1BEFB5A78BC67C80DCB5A1D601F898C35EA242D2E76CACE9EE5A61DBDA29A5076707325FE20B5A80DB0CA6D02C5D983"
)
///////构建以太坊签名并验证////////
ethSig
:=
append
(
r
,
s
...
)
hash
:=
crypto
.
Sha256
(
msg
)
VerifyResult4Eth
:=
ethCrypto
.
VerifySignature
(
pub
,
hash
[
:
],
ethSig
[
:
64
])
if
!
VerifyResult4Eth
{
panic
(
"Failed to do Signature verification for Ethereum"
)
}
fmt
.
Println
(
" ^-^ Succeed to do signature verification for Ethereum ^-^ "
)
fmt
.
Println
(
" "
)
fmt
.
Println
(
" "
)
fmt
.
Println
(
" "
)
///////构建chain33签名并验证////////
derSig
:=
adapter
.
MakeDERsignature
(
r
,
s
)
fmt
.
Println
(
" derSig "
,
common
.
ToHex
(
derSig
))
secpPubKey
,
err
:=
ethCrypto
.
UnmarshalPubkey
(
pub
)
pub33Bytes
:=
ethCrypto
.
CompressPubkey
(
secpPubKey
)
c
:=
&
secp256k1
.
Driver
{}
chain33PubKey
,
_
:=
c
.
PubKeyFromBytes
(
pub33Bytes
)
chain33Sig
,
err
:=
c
.
SignatureFromBytes
(
derSig
)
VerifyResult4Chain33
:=
chain33PubKey
.
VerifyBytes
(
msg
,
chain33Sig
)
if
!
VerifyResult4Chain33
{
panic
(
"Failed to do Signature verification for Chain33"
)
}
fmt
.
Println
(
" ^-^ Succeed to do signature verification for Chain33 ^-^ "
)
}
func
verifySM2
()
{
//msg := []byte("112233445566112233445566112233445566112233445566")
msg
,
_
:=
common
.
FromHex
(
"112233445566112233445566112233445566112233445566"
)
r
,
s
,
err
:=
adapter
.
SignSM2Internal
(
msg
,
10
)
if
err
!=
nil
{
panic
(
"Failed to SignSM2Internal due to:"
+
err
.
Error
())
}
fmt
.
Println
(
"signature R="
,
common
.
ToHex
(
r
))
fmt
.
Println
(
"signature S="
,
common
.
ToHex
(
s
))
///////构建公钥////////
xBytes
,
_
:=
common
.
FromHex
(
"0000000000000000000000000000000000000000000000000000000000000000FD4241057FEC6CBEEC501F7E1763751B8F6DFCFB910FB634FBB76A16639EF172"
)
yBytes
,
_
:=
common
.
FromHex
(
"00000000000000000000000000000000000000000000000000000000000000001C6DA89F9C1A5EE9B6108E5A2A5FE336962630A34DBA1AF428451E1CE63BB3CF"
)
x
:=
new
(
big
.
Int
)
.
SetBytes
(
xBytes
)
y
:=
new
(
big
.
Int
)
.
SetBytes
(
yBytes
)
publicKey
:=
&
gmsm_sm2
.
PublicKey
{
X
:
x
,
Y
:
y
,
}
var
pubSM2
sm2
.
PubKeySM2
copy
(
pubSM2
[
:
],
gmsm_sm2
.
Compress
(
publicKey
))
///////开始循环验证签名////////
now
:=
time
.
Now
()
now
.
Nanosecond
()
msLater
:=
now
.
Nanosecond
()
fmt
.
Printf
(
"msLater = %d
\n
"
,
msLater
)
time
.
Sleep
(
time
.
Millisecond
)
msLater
=
now
.
Nanosecond
()
fmt
.
Printf
(
"msLater = %d
\n
"
,
time
.
Now
()
.
Nanosecond
())
fmt
.
Printf
(
"msLater Sec = %d
\n
"
,
time
.
Now
()
.
Second
())
for
i
:=
0
;
i
<
10
*
1000
;
i
++
{
adapter
.
SignSM2Internal
(
msg
,
10
)
//rBytes, sBytes, _ := adapter.SignSM2Internal(msg, 10)
//fmt.Println("rBytes = ", common.ToHex(rBytes))
//fmt.Println("sBytes = ", common.ToHex(sBytes))
//r := new(big.Int).SetBytes(rBytes)
//s := new(big.Int).SetBytes(sBytes)
//signature := sm2.SignatureSM2(sm2.Serialize(r, s))
//if !pubSM2.VerifyBytes(msg, signature) {
// panic("Failed to do VerifyBytes")
//}
//fmt.Println("Succeed to do VerifyBytes for times = ", i)
}
fmt
.
Println
(
" "
)
fmt
.
Printf
(
"testLater = %d
\n
"
,
time
.
Now
()
.
Nanosecond
())
fmt
.
Printf
(
"testLater sec = %d
\n
"
,
time
.
Now
()
.
Second
())
fmt
.
Println
(
" "
)
fmt
.
Println
(
" ^-^ Successful ^-^ "
)
}
plugin/crypto/tasshsm/include/SDF4PCIeSM.h
deleted
100644 → 0
View file @
9ed41107
This diff is collapsed.
Click to expand it.
plugin/crypto/tasshsm/include/TassAPI4PCIeSM.h
deleted
100644 → 0
View file @
9ed41107
This diff is collapsed.
Click to expand it.
plugin/crypto/tasshsm/include/TassCtlAPI4PCIeSM.h
deleted
100644 → 0
View file @
9ed41107
This diff is collapsed.
Click to expand it.
plugin/crypto/tasshsm/include/TassType4PCIeSM.h
deleted
100644 → 0
View file @
9ed41107
#
pragma
once
#ifdef __cplusplus
extern
"C"
{
#endif
#define TA_DEVICE_ID_SIZE 16
typedef
enum
{
TA_DEV_STATE_INIT
=
0x0
,
//初始状态,无密钥
TA_DEV_STATE_WORK
=
0x1
,
//工作状态
TA_DEV_STATE_MNG
=
0x2
,
//管理状态
}
TassDevState
;
typedef
enum
{
TA_BOOL_FALSE
=
0
,
//私钥
TA_BOOL_TRUE
=
!
TA_BOOL_FALSE
,
//公钥
}
TassBool
;
typedef
enum
{
TA_DEV_KEY_PLATFORM
=
0
,
//管理平台密钥(公钥)
TA_DEV_KEY_SIGN
=
1
,
//设备签名密钥
TA_DEV_KEY_ENC
=
2
,
//设备加密密钥
TA_DEV_KEY_KEK
=
3
,
//设备KEK
}
TassDevKeyType
;
typedef
enum
{
TA_ALG_SM4
=
0
,
TA_ALG_SM1
=
1
,
TA_ALG_AES
=
5
,
TA_ALG_DES
=
6
,
TA_ALG_SM7
=
7
,
TA_ALG_SM2
=
2
,
TA_ALG_ECC_SECP_256R1
=
3
,
TA_ALG_ECC_SECP_256K1
=
8
,
TA_ALG_RSA
=
4
,
TA_ALG_HMAC
=
9
,
}
TassAlg
;
typedef
enum
{
TA_RSA_E_3
=
3
,
TA_RSA_E_65537
=
65537
,
}
TassRSA_E
;
typedef
enum
{
TA_SYMM_ECB_ENC
=
0x00000000
,
TA_SYMM_ECB_DEC
=
0x00000001
,
TA_SYMM_CBC_ENC
=
0x00000100
,
TA_SYMM_CBC_DEC
=
0x10000101
,
TA_SYMM_CFB_ENC
=
0x00000200
,
TA_SYMM_CFB_DEC
=
0x10000201
,
TA_SYMM_OFB_ENC
=
0x00000300
,
TA_SYMM_OFB_DEC
=
0x10000301
,
TA_SYMM_MAC
=
0x00000400
,
}
TassSymmOp
;
typedef
enum
{
TA_SM2_KEY_EXCHANGE_SPONSOR
=
0
,
TA_SM2_KEY_EXCHANGE_RESPONSE
=
1
,
}
TassSM2KeyExchangeRole
;
typedef
enum
{
TA_ASYM_SIGN
=
0
,
TA_ASYM_ENC
=
1
,
TA_ASYM_KEY_EX
=
2
,
}
TassAsymKeyUsage
;
typedef
enum
{
TA_ASYM_ENCRYPT
=
0
,
TA_ASYM_DECRYPT
=
1
,
}
TassSymmKeyUsage
;
typedef
enum
{
TA_FLASH_2K
=
0
,
TA_FLASH_32K
=
1
,
}
TassFlashFlag
;
typedef
enum
{
TA_FLASH_GET_SIZE
=
0
,
TA_FLASH_READ
=
1
,
TA_FLASH_WRITE
=
2
,
TA_FLASH_ERASE
=
3
,
}
TassFlashOp
;
typedef
struct
{
unsigned
char
sigHead
[
16
];
//签名数据头
unsigned
char
hwVer
[
4
];
//硬件版本
unsigned
char
fpgaVer
[
4
];
//FPGA版本
unsigned
char
keyMngChipVer
[
4
];
//密管芯片版本
unsigned
char
devId
[
16
];
//设备唯一标识,不可指定
unsigned
char
devSn
[
4
];
//设备序列号,可指定
unsigned
char
fpgaHwCv
[
32
];
//FPGA固件校验值
unsigned
char
devSignKeyPairInfo
[
4
];
//设备签名密钥对信息
unsigned
char
platformPkInfo
[
4
];
//管理平台公钥信息
unsigned
char
devEncKeyPairInfo
[
4
];
//设备加密密钥对信息
unsigned
char
devKEKInfo
[
4
];
//设备本地保护密钥信息
unsigned
char
kekCv
[
16
];
//设备本地保护密钥校验值
unsigned
char
adminPkAblity
[
4
];
//管理员公钥存储能力,大端格式
unsigned
char
curAdminPkNum
[
4
];
//当前管理员公钥个数,大端格式
unsigned
char
adminPkInfo
[
5
][
4
];
//管理员公钥信息5 * 4Bytes= 20Bytes
unsigned
char
devState
[
4
];
//设备状态,大端格式
unsigned
char
temp
[
8
];
//温度
unsigned
char
valtage
[
8
];
//电压
unsigned
char
current
[
8
];
//电流
unsigned
char
selfCheckCycle
[
4
];
//自检周期,大端格式
unsigned
char
lastSelfCheckInfo
[
8
];
//上次自检信息
unsigned
char
channelNum
[
4
];
//通道总数,大端格式
unsigned
char
channelAlg
[
256
];
//通道算法
unsigned
char
sig
[
64
];
//设备签名公钥对上述数据的签名
}
TassDevInfo
;
/**
* @brief 签名回调函数,用于需要私钥签名的接口调用
*
* @param data [in] 待签名数据
* @param dataLen [in] data长度
* @param sig [out] 签名值
*
* @return 成功返回0,失败返回非0
*
*/
typedef
int
(
*
TassSignCb
)(
const
unsigned
char
*
data
,
int
len
,
unsigned
char
sig
[
64
]);
#ifdef __cplusplus
}
#endif
plugin/crypto/tasshsm/secp256k1/secp256k1_test.go
deleted
100644 → 0
View file @
9ed41107
package
secp256k1
import
(
"fmt"
"testing"
"gotest.tools/assert"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/system/crypto/secp256k1"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/common"
ethCrypto
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)
//secp256k1签名DER编码格式
// 0x30 <length> 0x02
//<length r> r
//0x02 <length s>
//s
//30440220
//7C12FF568B6DA03EF2CD5681EE45EDF846172771AC6F9369B50FEBC95B1CF68F
//0220
//8B6916A3CC7423D9044E77ABECE410B69B7BD82C22ECCC76061B4BE79141D1A3
func
Test_VerifySecp256k1SigFromTass_forChain33
(
t
*
testing
.
T
)
{
require
:=
require
.
New
(
t
)
//var pubKey secp256k1.PubKeySecp256k1
pubBytes
:=
common
.
FromHex
(
"04C24FBA65F8CD81223D2935EDEA663048A1BEFB5A78BC67C80DCB5A1D601F898C35EA242D2E76CACE9EE5A61DBDA29A5076707325FE20B5A80DB0CA6D02C5D983"
)
secpPubKey
,
err
:=
ethCrypto
.
UnmarshalPubkey
(
pubBytes
)
require
.
Equal
(
nil
,
err
)
pub33Bytes
:=
ethCrypto
.
CompressPubkey
(
secpPubKey
)
c
:=
&
secp256k1
.
Driver
{}
pubKey
,
err
:=
c
.
PubKeyFromBytes
(
pub33Bytes
)
require
.
Equal
(
nil
,
err
)
//msg := []byte("12345678123456781234567812345678")
msg
:=
[]
byte
(
"456789"
)
hash
:=
crypto
.
Sha256
(
msg
)
fmt
.
Println
(
"hash = "
,
common
.
Bytes2Hex
(
hash
))
//0xfed9efbd5a8ef6820d639dbcb831daf9d6308312cc73d6188beb54a9a148e29a
sig
,
err
:=
c
.
SignatureFromBytes
(
common
.
FromHex
(
"304502207C12FF568B6DA03EF2CD5681EE45EDF846172771AC6F9369B50FEBC95B1CF68F0221008B6916A3CC7423D9044E77ABECE410B69B7BD82C22ECCC76061B4BE79141D1A3"
))
//sig, err := c.SignatureFromBytes(common.FromHex("304402207C12FF568B6DA03EF2CD5681EE45EDF846172771AC6F9369B50FEBC95B1CF68F02208B6916A3CC7423D9044E77ABECE410B69B7BD82C22ECCC76061B4BE79141D1A3"))
require
.
Equal
(
nil
,
err
)
result
:=
pubKey
.
VerifyBytes
(
msg
,
sig
)
require
.
Equal
(
true
,
result
)
}
//在以太坊上的验证签名的有效性
//注意:从加密中导出的签名信息中RS信息中的首字节必须大于0,否则签名验证失败
func
Test_Verify4Eth
(
t
*
testing
.
T
)
{
pub
:=
common
.
FromHex
(
"04C24FBA65F8CD81223D2935EDEA663048A1BEFB5A78BC67C80DCB5A1D601F898C35EA242D2E76CACE9EE5A61DBDA29A5076707325FE20B5A80DB0CA6D02C5D983"
)
sig
:=
common
.
FromHex
(
"2F2F8EF10E6C9075CAB44DE3C4F904817220537C1E7DCFADD502C03F14F5B3974C405EA9BB189B85F15B91C82CE5D6191D66238ECCCE83FA8F8FF83173F1586F00"
)
msg
:=
[]
byte
(
"456789"
)
hash
:=
crypto
.
Sha256
(
msg
)
fmt
.
Println
(
"hash = "
,
common
.
Bytes2Hex
(
hash
))
pubRecoverd
,
err
:=
ethCrypto
.
Ecrecover
(
hash
[
:
],
sig
)
require
.
Equal
(
t
,
nil
,
err
)
fmt
.
Println
(
"pubRecoverd = "
,
common
.
Bytes2Hex
(
pubRecoverd
))
VerifyResult
:=
ethCrypto
.
VerifySignature
(
pub
,
hash
[
:
],
sig
[
:
64
])
assert
.
Equal
(
t
,
true
,
VerifyResult
)
}
func
Test_secp256k1
(
t
*
testing
.
T
)
{
require
:=
require
.
New
(
t
)
c
:=
&
secp256k1
.
Driver
{}
priv
,
err
:=
c
.
PrivKeyFromBytes
(
common
.
FromHex
(
"CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944"
))
require
.
Nil
(
err
)
t
.
Logf
(
"priv:%X, len:%d"
,
priv
.
Bytes
(),
len
(
priv
.
Bytes
()))
pub
:=
priv
.
PubKey
()
require
.
NotNil
(
pub
)
t
.
Logf
(
"pub:%X, len:%d"
,
pub
.
Bytes
(),
len
(
pub
.
Bytes
()))
//msg := []byte("12345678123456781234567812345678")
//msg := []byte("hello world")
msg
:=
[]
byte
(
"456789"
)
signature
:=
priv
.
Sign
(
msg
)
t
.
Logf
(
"sign:%X, len:%d"
,
signature
.
Bytes
(),
len
(
signature
.
Bytes
()))
t
.
Logf
(
"signature in hex format:%s"
,
common
.
Bytes2Hex
(
signature
.
Bytes
()))
//0x3045022100f4009ab47dc32880b3e0bfad47885e9cfd1fd2228e804b38fb7f0f5ea6c02405022061422eb681fdd5078aa3971770cf22ce4ef12e9116995e4a3e141e23f5403014
ok
:=
pub
.
VerifyBytes
(
msg
,
signature
)
require
.
Equal
(
true
,
ok
)
}
plugin/crypto/tasshsm/sm2/sm2_test.go
deleted
100644 → 0
View file @
9ed41107
package
sm2
import
(
"fmt"
"math/big"
"testing"
"github.com/tjfoc/gmsm/sm3"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/common"
"github.com/33cn/chain33/system/crypto/sm2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gmsm_sm2
"github.com/tjfoc/gmsm/sm2"
)
//data = 112233445566112233445566112233445566112233445566
//SDF_HashFinal success! sm3Len = 32, sm3Hash = 6DB75BE80A3EE89F1F049F6F046BFF5B39CF686E502F29906DECCCF8C72E4907
//
//SDF_InternalSign_ECC success
//sign.r: 0000000000000000000000000000000000000000000000000000000000000000F5BBF85D6165F6C027EA80A2930E270DAEF152FB5F090365E2AD55BE31BF2EF4
//sign.s: 00000000000000000000000000000000000000000000000000000000000000001C68E90BFEDE1196F3CB2447EA0A6D81818627C6AA447F4CFC57BD817A42222A
//
//SDF_InternalVerify_ECC success
//SDF_ExportSignPublicKey_ECC success for chain33 verification
//chain33 PubKey.bits: 256
//chain33 PubKey.x: 0000000000000000000000000000000000000000000000000000000000000000FD4241057FEC6CBEEC501F7E1763751B8F6DFCFB910FB634FBB76A16639EF172
//chain33 PubKey.y: 00000000000000000000000000000000000000000000000000000000000000001C6DA89F9C1A5EE9B6108E5A2A5FE336962630A34DBA1AF428451E1CE63BB3CF
func
Test_Crypto
(
t
*
testing
.
T
)
{
require
:=
require
.
New
(
t
)
c
:=
&
sm2
.
Driver
{}
priv
,
err
:=
c
.
GenKey
()
require
.
Nil
(
err
)
t
.
Logf
(
"priv:%X, len:%d"
,
priv
.
Bytes
(),
len
(
priv
.
Bytes
()))
pub
:=
priv
.
PubKey
()
require
.
NotNil
(
pub
)
t
.
Logf
(
"pub:%X, len:%d"
,
pub
.
Bytes
(),
len
(
pub
.
Bytes
()))
msg
:=
[]
byte
(
"hello world"
)
signature
:=
priv
.
Sign
(
msg
)
t
.
Logf
(
"sign:%X, len:%d"
,
signature
.
Bytes
(),
len
(
signature
.
Bytes
()))
ok
:=
pub
.
VerifyBytes
(
msg
,
signature
)
require
.
Equal
(
true
,
ok
)
}
//SDF_HashFinal success! sm3Len = 32, sm3Hash = 9D6A3506E37DB26538706965689AACE220D32391812A82F6C28A06AE8E2F2434
//
//SDF_InternalSign_ECC success
//sign.r: 00000000000000000000000000000000000000000000000000000000000000003AA29337E7149047FB8AE83F30AA00125E23173C88F284ADDED2E5B59ACAA5B9
//sign.s: 0000000000000000000000000000000000000000000000000000000000000000E2E9338109D74269578216039FD4D1C764E7F6F142CBB2E3035E7E49D375D330
//
//SDF_InternalVerify_ECC success
//SDF_ExportSignPublicKey_ECC success for chain33 verification
//chain33 PubKey.bits: 256
//chain33 PubKey.x: 0000000000000000000000000000000000000000000000000000000000000000FD4241057FEC6CBEEC501F7E1763751B8F6DFCFB910FB634FBB76A16639EF172
//chain33 PubKey.y: 00000000000000000000000000000000000000000000000000000000000000001C6DA89F9C1A5EE9B6108E5A2A5FE336962630A34DBA1AF428451E1CE63BB3CF
func
Test_VerifySigFromTass
(
t
*
testing
.
T
)
{
require
:=
require
.
New
(
t
)
//c := &sm2.Driver{}
xBytes
:=
common
.
FromHex
(
"0000000000000000000000000000000000000000000000000000000000000000FD4241057FEC6CBEEC501F7E1763751B8F6DFCFB910FB634FBB76A16639EF172"
)
yBytes
:=
common
.
FromHex
(
"00000000000000000000000000000000000000000000000000000000000000001C6DA89F9C1A5EE9B6108E5A2A5FE336962630A34DBA1AF428451E1CE63BB3CF"
)
x
:=
new
(
big
.
Int
)
.
SetBytes
(
xBytes
)
y
:=
new
(
big
.
Int
)
.
SetBytes
(
yBytes
)
publicKey
:=
&
gmsm_sm2
.
PublicKey
{
X
:
x
,
Y
:
y
,
}
var
pubSM2
sm2
.
PubKeySM2
copy
(
pubSM2
[
:
],
gmsm_sm2
.
Compress
(
publicKey
))
rBytes
:=
common
.
FromHex
(
"00000000000000000000000000000000000000000000000000000000000000003AA29337E7149047FB8AE83F30AA00125E23173C88F284ADDED2E5B59ACAA5B9"
)
sBytes
:=
common
.
FromHex
(
"0000000000000000000000000000000000000000000000000000000000000000E2E9338109D74269578216039FD4D1C764E7F6F142CBB2E3035E7E49D375D330"
)
r
:=
new
(
big
.
Int
)
.
SetBytes
(
rBytes
)
s
:=
new
(
big
.
Int
)
.
SetBytes
(
sBytes
)
signature
:=
sm2
.
SignatureSM2
(
sm2
.
Serialize
(
r
,
s
))
msg
:=
[]
byte
(
"112233445566112233445566112233445566112233445566"
)
ok
:=
pubSM2
.
VerifyBytes
(
msg
,
signature
)
require
.
Equal
(
true
,
ok
)
}
func
Test_msgHash
(
t
*
testing
.
T
)
{
uid
:=
[]
byte
{
0x31
,
0x32
,
0x33
,
0x34
,
0x35
,
0x36
,
0x37
,
0x38
,
0x31
,
0x32
,
0x33
,
0x34
,
0x35
,
0x36
,
0x37
,
0x38
}
xBytes
:=
common
.
FromHex
(
"0000000000000000000000000000000000000000000000000000000000000000FD4241057FEC6CBEEC501F7E1763751B8F6DFCFB910FB634FBB76A16639EF172"
)
yBytes
:=
common
.
FromHex
(
"00000000000000000000000000000000000000000000000000000000000000001C6DA89F9C1A5EE9B6108E5A2A5FE336962630A34DBA1AF428451E1CE63BB3CF"
)
x
:=
new
(
big
.
Int
)
.
SetBytes
(
xBytes
)
y
:=
new
(
big
.
Int
)
.
SetBytes
(
yBytes
)
publicKey
:=
&
gmsm_sm2
.
PublicKey
{
X
:
x
,
Y
:
y
,
}
var
pubSM2
sm2
.
PubKeySM2
copy
(
pubSM2
[
:
],
gmsm_sm2
.
Compress
(
publicKey
))
pub
:=
gmsm_sm2
.
Decompress
(
pubSM2
[
0
:
sm2
.
SM2PublicKeyCompressed
])
za
,
err
:=
gmsm_sm2
.
ZA
(
pub
,
uid
)
assert
.
Equal
(
t
,
nil
,
err
)
//msg := []byte("112233445566112233445566112233445566112233445566")
msg
:=
common
.
FromHex
(
"112233445566112233445566112233445566112233445566"
)
fmt
.
Println
(
"msg = "
,
common
.
Bytes2Hex
(
msg
))
e
:=
sm3
.
New
()
e
.
Write
(
za
)
e
.
Write
(
msg
)
hashInt
:=
new
(
big
.
Int
)
.
SetBytes
(
e
.
Sum
(
nil
)[
:
32
])
fmt
.
Print
(
"hash = "
,
common
.
Bytes2Hex
(
hashInt
.
Bytes
()))
//hash = 0x9d6a3506e37db26538706965689aace220d32391812a82f6c28a06ae8e2f2434
//726106A793A45AF8EE1B9B6781B87391E4B49304A5F12F6DFF82C7EDB5F58390
//0x726106a793a45af8ee1b9b6781b87391e4b49304a5f12f6dff82c7edb5f58390
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment