Files
xgcl/tpc/sm2/sm2m/import_enc_key.go
T
2026-05-27 23:03:00 +08:00

39 lines
961 B
Go

package sm2m
import (
"math/big"
"xdx.jelly/xgcl/gmath"
"xdx.jelly/xgcl/sm/sm2"
)
/*
导入加密密钥de,由sm2签名密钥来解密sm2加密密钥保护结构。
解密de后拆分也同签名密钥拆分
(1+de)^{-1} = de_c*de_s
为了加解密函数一致
*/
// ServerDec 服务端计算(serverKey)^(-1)*C1,使用签名密钥解密加密密钥保护结构
func ServerImportKey(serverKey *sm2.PrivateKey, in []byte) ([]byte, error) {
if len(in) < 2*sm2.ByteSize() {
return []byte{}, ErrIndataError
}
x := new(big.Int)
x.SetBytes(in[:sm2.ByteSize()])
y := new(big.Int)
y.SetBytes(in[sm2.ByteSize() : 2*sm2.ByteSize()])
sInv := new(big.Int)
sInv.Set(serverKey.D)
sInv.ModInverse(sInv, sm2.OrderN())
x, y = sm2.Curve256.ScalarMult(x, y, sInv.Bytes())
out := make([]byte, 0, 2*sm2.ByteSize())
out = append(out, gmath.BigIntToNByte(x, sm2.ByteSize())...)
out = append(out, gmath.BigIntToNByte(y, sm2.ByteSize())...)
return out, nil
}