Add SM2 ZID support

This commit is contained in:
Zhi Guan
2018-08-07 20:25:04 +08:00
parent 028d209985
commit 838f4effbe
2 changed files with 32 additions and 0 deletions

View File

@@ -936,3 +936,33 @@ func (sk *PrivateKey) DeriveKey(alg string, peer PublicKey, eng *Engine) ([]byte
}
return C.GoBytes(unsafe.Pointer(key), C.int(keylen)), nil
}
func (pk *PublicKey) ComputeSM2IDDigest(id string) ([]byte, error) {
if C.EVP_PKEY_EC != C.EVP_PKEY_id(pk.pkey) {
return nil, errors.New("Invalid public key type")
}
cid := C.CString(id)
defer C.free(unsafe.Pointer(cid))
outbuf := make([]byte, 64)
outlen := C.size_t(len(outbuf))
if 1 != C.SM2_compute_id_digest(C.EVP_sm3(), cid, C.size_t(len(id)),
(*C.uchar)(&outbuf[0]), &outlen, C.EVP_PKEY_get0_EC_KEY(pk.pkey)) {
return nil, GetErrors()
}
return outbuf[:32], nil
}
func (sk *PrivateKey) ComputeSM2IDDigest(id string) ([]byte, error) {
if C.EVP_PKEY_EC != C.EVP_PKEY_id(sk.pkey) {
return nil, errors.New("Invalid public key type")
}
cid := C.CString(id)
defer C.free(unsafe.Pointer(cid))
outbuf := make([]byte, 64)
outlen := C.size_t(len(outbuf))
if 1 != C.SM2_compute_id_digest(C.EVP_sm3(), cid, C.size_t(len(id)),
(*C.uchar)(&outbuf[0]), &outlen, C.EVP_PKEY_get0_EC_KEY(sk.pkey)) {
return nil, GetErrors()
}
return outbuf[:32], nil
}

View File

@@ -196,7 +196,9 @@ func main() {
fmt.Println(sm2pkpem_)
/* SM2 sign/verification */
sm2zid, _ := sm2pk.ComputeSM2IDDigest("1234567812345678")
sm3ctx.Reset()
sm3ctx.Update(sm2zid)
sm3ctx.Update([]byte("message"))
tbs, _ := sm3ctx.Final()