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

33 lines
847 B
Go

package sm2a
import (
"fmt"
"io"
"math/big"
"xdx.jelly/xgcl/gmath"
"xdx.jelly/xgcl/sm/sm2"
)
// ServerEncKeyGen 服务端加密密钥生成
func ServerEncKeyGen(in []byte, rand io.Reader) (*sm2.PrivateKey, *sm2.PublicKey, []byte, error) {
buf := make([]byte, sm2.ByteSize())
if n, err := rand.Read(buf); n != len(buf) || err != nil {
return nil, nil, nil, fmt.Errorf("Generate random number error")
}
ds, _ := sm2.GenPrivateKey(buf)
pk := sm2.GenPublicKey(ds)
out := make([]byte, 0, 2*sm2.ByteSize())
out = append(out, gmath.BigIntToNByte(pk.X, sm2.ByteSize())...)
out = append(out, gmath.BigIntToNByte(pk.Y, sm2.ByteSize())...)
x := new(big.Int)
y := new(big.Int)
x.SetBytes(in[:sm2.ByteSize()])
y.SetBytes(in[sm2.ByteSize() : 2*sm2.ByteSize()])
pk.X, pk.Y = sm2.Curve().Add(pk.X, pk.Y, x, y)
return ds, pk, out, nil
}