init: v1.0.0

This commit is contained in:
yaole
2026-05-27 23:03:00 +08:00
commit 8d97f750eb
466 changed files with 80067 additions and 0 deletions
+112
View File
@@ -0,0 +1,112 @@
package common
import (
"fmt"
)
type SDRError uint32
const (
SDR_OK SDRError = 0x0 // 操作成功
SDR_BASE SDRError = 0x01000000 // 错误码基础值
SDR_UNKNOWERR SDRError = SDR_BASE + 0x00000001 // 未知错误
SDR_NOTSUPPORT SDRError = SDR_BASE + 0x00000002 // 不支持的接口调用
SDR_COMMFAIL SDRError = SDR_BASE + 0x00000003 // 与设备通信失败
SDR_HARDFAIL SDRError = SDR_BASE + 0x00000004 // 运算模块无响应
SDR_OPENDEVICE SDRError = SDR_BASE + 0x00000005 // 打开设备失败
SDR_OPENSESSION SDRError = SDR_BASE + 0x00000006 // 创建会话失败
SDR_PARDENY SDRError = SDR_BASE + 0x00000007 // 无私钥使用权限
SDR_KEYNOTEXIST SDRError = SDR_BASE + 0x00000008 // 不存在的密钥调用
SDR_ALGNOTSUPPORT SDRError = SDR_BASE + 0x00000009 // 不支持的算法调用
SDR_ALGMODNOTSUPPORT SDRError = SDR_BASE + 0x0000000A // 不支持的算法模式调用
SDR_PKOPERR SDRError = SDR_BASE + 0x0000000B // 公钥运算失败
SDR_SKOPERR SDRError = SDR_BASE + 0x0000000C // 私钥运算失败
SDR_SIGNERR SDRError = SDR_BASE + 0x0000000D // 签名运算失败
SDR_VERIFYERR SDRError = SDR_BASE + 0x0000000E // 验证签名失败
SDR_SYMOPERR SDRError = SDR_BASE + 0x0000000F // 对称算法运算失败
SDR_STEPERR SDRError = SDR_BASE + 0x00000010 // 多步运算步骤错误
SDR_FILESIZEERR SDRError = SDR_BASE + 0x00000011 // 文件长度超出限制
SDR_FILENOEXIST SDRError = SDR_BASE + 0x00000012 // 指定的文件不存在
SDR_FILEOFSERR SDRError = SDR_BASE + 0x00000013 // 文件起始位置错误
SDR_KEYTYPEERR SDRError = SDR_BASE + 0x00000014 // 密钥类型错误
SDR_KEYERR SDRError = SDR_BASE + 0x00000015 // 密钥错误
SDR_ENCDATAERR SDRError = SDR_BASE + 0x00000016 // ECC加密数据错误
SDR_RANDERR SDRError = SDR_BASE + 0x00000017 // 随机数产生失败
SDR_PRKRERR SDRError = SDR_BASE + 0x00000018 // 私钥使用权限获取失败
SDR_MACERR SDRError = SDR_BASE + 0x00000019 // MAC运算失败
SDR_FILEEXISTS SDRError = SDR_BASE + 0x0000001A // 指定文件已存在
SDR_FILEWERR SDRError = SDR_BASE + 0x0000001B // 文件写入失败
SDR_NOBUFFER SDRError = SDR_BASE + 0x0000001C // 存储空间不足
SDR_INARGERR SDRError = SDR_BASE + 0x0000001D // 输入参数错误
SDR_OUTARGERR SDRError = SDR_BASE + 0x0000001E // 输出参数错误
)
func (s SDRError) Error() string {
return s.String()
}
func (s SDRError) String() string {
switch s {
case SDR_UNKNOWERR:
return fmt.Sprintf("0x%08x: 未知错误", uint32(SDR_UNKNOWERR))
case SDR_NOTSUPPORT:
return fmt.Sprintf("0x%08x: 不支持的接口调用", uint32(SDR_NOTSUPPORT))
case SDR_COMMFAIL:
return fmt.Sprintf("0x%08x: 与设备通信失败", uint32(SDR_COMMFAIL))
case SDR_HARDFAIL:
return fmt.Sprintf("0x%08x: 运算模块无响应", uint32(SDR_HARDFAIL))
case SDR_OPENDEVICE:
return fmt.Sprintf("0x%08x: 打开设备失败", uint32(SDR_OPENDEVICE))
case SDR_OPENSESSION:
return fmt.Sprintf("0x%08x: 创建会话失败", uint32(SDR_OPENSESSION))
case SDR_PARDENY:
return fmt.Sprintf("0x%08x: 无私钥使用权限", uint32(SDR_PARDENY))
case SDR_KEYNOTEXIST:
return fmt.Sprintf("0x%08x: 不存在的密钥调用", uint32(SDR_KEYNOTEXIST))
case SDR_ALGNOTSUPPORT:
return fmt.Sprintf("0x%08x: 不支持的算法调用", uint32(SDR_ALGNOTSUPPORT))
case SDR_ALGMODNOTSUPPORT:
return fmt.Sprintf("0x%08x: 不支持的算法模式调用", uint32(SDR_ALGMODNOTSUPPORT))
case SDR_PKOPERR:
return fmt.Sprintf("0x%08x: 公钥运算失败", uint32(SDR_PKOPERR))
case SDR_SKOPERR:
return fmt.Sprintf("0x%08x: 私钥运算失败", uint32(SDR_SKOPERR))
case SDR_SIGNERR:
return fmt.Sprintf("0x%08x: 签名运算失败", uint32(SDR_SIGNERR))
case SDR_VERIFYERR:
return fmt.Sprintf("0x%08x: 验证签名失败", uint32(SDR_VERIFYERR))
case SDR_SYMOPERR:
return fmt.Sprintf("0x%08x: 对称算法运算失败", uint32(SDR_SYMOPERR))
case SDR_STEPERR:
return fmt.Sprintf("0x%08x: 多步运算步骤错误", uint32(SDR_STEPERR))
case SDR_FILESIZEERR:
return fmt.Sprintf("0x%08x: 文件长度超出限制", uint32(SDR_FILESIZEERR))
case SDR_FILENOEXIST:
return fmt.Sprintf("0x%08x: 指定的文件不存在", uint32(SDR_FILENOEXIST))
case SDR_FILEOFSERR:
return fmt.Sprintf("0x%08x: 文件起始位置错误", uint32(SDR_FILEOFSERR))
case SDR_KEYTYPEERR:
return fmt.Sprintf("0x%08x: 密钥类型错误", uint32(SDR_KEYTYPEERR))
case SDR_KEYERR:
return fmt.Sprintf("0x%08x: 密钥错误", uint32(SDR_KEYERR))
case SDR_ENCDATAERR:
return fmt.Sprintf("0x%08x: ECC加密数据错误", uint32(SDR_ENCDATAERR))
case SDR_RANDERR:
return fmt.Sprintf("0x%08x: 随机数产生失败", uint32(SDR_RANDERR))
case SDR_PRKRERR:
return fmt.Sprintf("0x%08x: 私钥使用权限获取失败", uint32(SDR_PRKRERR))
case SDR_MACERR:
return fmt.Sprintf("0x%08x: MAC运算失败", uint32(SDR_MACERR))
case SDR_FILEEXISTS:
return fmt.Sprintf("0x%08x: 指定文件已存在", uint32(SDR_FILEEXISTS))
case SDR_FILEWERR:
return fmt.Sprintf("0x%08x: 文件写入失败", uint32(SDR_FILEWERR))
case SDR_NOBUFFER:
return fmt.Sprintf("0x%08x: 存储空间不足", uint32(SDR_NOBUFFER))
case SDR_INARGERR:
return fmt.Sprintf("0x%08x: 输入参数错误", uint32(SDR_INARGERR))
case SDR_OUTARGERR:
return fmt.Sprintf("0x%08x: 输出参数错误", uint32(SDR_OUTARGERR))
default:
return "未知错误"
}
}
+10
View File
@@ -0,0 +1,10 @@
package common
import (
"fmt"
"testing"
)
func TestSDRError(t *testing.T) {
fmt.Println(SDR_ENCDATAERR)
}
+181
View File
@@ -0,0 +1,181 @@
package common
/**
sdf数据结构和sm2的数据结构方法绑定在sm2.Publickey.MarshalSDF上
*/
const (
RSAref_MAX_BITS = 2048 //nolint
RSAref_MAX_LEN = ((RSAref_MAX_BITS + 7) / 8) //nolint
RSAref_MAX_PBITS = ((RSAref_MAX_BITS + 1) / 2) //nolint
RSAref_MAX_PLEN = ((RSAref_MAX_PBITS + 7) / 8) //nolint
ECCref_MAX_BITS = 512 //nolint
ECCref_MAX_LEN = ((ECCref_MAX_BITS + 7) / 8) //nolint
)
//RSArefPublicKey RSA公钥
type RSArefPublicKey struct {
Bits uint32
M [RSAref_MAX_LEN]byte
E [RSAref_MAX_LEN]byte
}
//RSArefPrivateKey RSA私钥
type RSArefPrivateKey struct {
Bits uint32
M [RSAref_MAX_LEN]byte
E [RSAref_MAX_LEN]byte
D [RSAref_MAX_LEN]byte
Prime [2][RSAref_MAX_PLEN]byte
Pexp [2][RSAref_MAX_PLEN]byte // Dp and Dq, Dp =
Coef [RSAref_MAX_PLEN]byte // q^-1 mod p
}
type EnvelopedRSAKey struct {
SymmAlgID uint32 // 对称算法标识,限定ECB模式
Bits uint32 // RSA密钥对的模长
EncryptedPriKey [11][RSAref_MAX_PLEN]byte // RSA密钥对私钥的密文
PubKey RSArefPublicKey // RSA密钥对的公钥
L uint32 // 用RSA保护公钥加密的对称密钥密文长度
C [RSAref_MAX_LEN]byte // 用RSA保护公钥加密的对称密钥密文
}
//ECCrefPublicKey sm2公钥
type ECCrefPublicKey struct {
Bits uint32
X [ECCref_MAX_LEN]byte
Y [ECCref_MAX_LEN]byte
}
//ECCrefPrivateKey sm2私钥
type ECCrefPrivateKey struct {
Bits uint32
K [ECCref_MAX_LEN]byte
}
//ECCCipher sm2加密结构
type ECCCipher struct {
X [ECCref_MAX_LEN]byte
Y [ECCref_MAX_LEN]byte
M [32]byte
L uint32
C []byte
}
//ECCSignature sm2签名结果
type ECCSignature struct {
R [ECCref_MAX_LEN]byte
S [ECCref_MAX_LEN]byte
}
//EnvelopedECCKey sm2密钥保护结构
type EnvelopedECCKey struct {
AsymmAlgID uint32
SymmAlgID uint32
Bits uint32
EncryptedPriKey [ECCref_MAX_LEN]byte
PubKey ECCrefPublicKey
ECCCipherBlob ECCCipher
}
// //SM9MastPrivateKey SM9主私钥
// type SM9MastPrivateKey struct {
// Bits uint32
// S [32]byte
// }
// //SM9SignMastPublicKey 签名主公钥
// type SM9SignMastPublicKey struct {
// CompressType uint32
// Bits uint32
// Xa [32]byte
// Xb [32]byte
// Ya [32]byte
// Yb [32]byte
// }
// //SM9EncMastPublicKey 加密主公钥
// type SM9EncMastPublicKey struct {
// Bits uint32
// X [32]byte
// Y [32]byte
// }
// //SM9UserSignPrivateKey 用户签名私钥
// type SM9UserSignPrivateKey struct {
// Bits uint32
// X [32]byte
// Y [32]byte
// }
// //SM9UserEncPrivateKey 用户加密私钥
// type SM9UserEncPrivateKey struct {
// CompressType uint32
// Bits uint32
// Xa [32]byte
// Xb [32]byte
// Ya [32]byte
// Yb [32]byte
// }
// //SM9Cipher SM9加密数据结构
// type SM9Cipher struct {
// EnType uint32
// X [32]byte
// Y [32]byte
// M [32]byte
// L uint32
// C [1024]byte
// }
// //SM9Signature SM9签名数据结构
// type SM9Signature struct {
// H [32]byte
// X [32]byte
// Y [32]byte
// }
// //SM9KeyPackage SM9密钥封装数据结构
// type SM9KeyPackage struct {
// K [32]byte
// X [32]byte
// Y [32]byte
// }
// //SM9PairEncEnvelopedKey SM9用户加密密钥对保护结构
// type SM9PairEncEnvelopedKey struct {
// Version uint32
// SymAlgID uint32
// ASymmAlgID uint32
// Bits uint32
// EncryptedPriKey [128]byte
// EncMastPubKey SM9EncMastPublicKey
// UserIDLen uint32
// UserID [1024]byte
// PairCipher SM9Cipher
// }
// //SM9PairSignEnvelopedKey SM9用户签名密钥对保护结构
// type SM9PairSignEnvelopedKey struct {
// Version uint32
// SymAlgID uint32
// ASymmAlgID uint32
// Bits uint32
// EncryptedPriKey [64]byte
// SignMastPubKey SM9SignMastPublicKey
// UserIDLen uint32
// UserID [1024]byte
// PairCipher SM9Cipher
// }
// func (e *ECCrefPrivateKey) MarshalGcl() *sm2.PrivateKey {
// sk := &sm2.PrivateKey{
// D: new(big.Int).SetBytes(e.K[:]),
// }
// pk := sm2.GenPublicKey(sk)
// sk.PublicKey = *pk
// return sk
// }
// func (e *ECCrefPrivateKey) UnmarshalGcl(sk *sm2.PrivateKey) error {
// return gmath.FillBytes(sk.D, e.K[:])
// }