mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-12 17:03:37 +08:00
First SM9 release
SM9 setup/keygen/sign/verify has been tested. See example codes `demos/sm9/`.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2016 The GmSSL Project. All rights reserved.
|
||||
* Copyright (c) 2016 - 2018 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -52,21 +52,133 @@
|
||||
#include "sm9_lcl.h"
|
||||
|
||||
/*
|
||||
* h = H1(ID_B || hid, N)
|
||||
* Q_B = [h]P1 + P_pub
|
||||
* r_A = rand(1, N-1)
|
||||
* R_A = [r_A]Q_B
|
||||
* compute Q = H1(peer_id) * P1 + Ppube
|
||||
* r = rand(1, n-1)
|
||||
* R = r * Q
|
||||
* g = e(Ppube, P2)
|
||||
* g1' = g2 = g^r
|
||||
* send R to peer
|
||||
*/
|
||||
SM9PublicKey *SM9_generate_key_exchange(SM9PublicParameters *mpk,
|
||||
const char *peer_id, size_t peer_idlen, BIGNUM **r)
|
||||
int SM9_generate_key_exchange(unsigned char *R, size_t *Rlen,
|
||||
BIGNUM *r, unsigned char *gr, size_t *grlen,
|
||||
const char *peer_id, size_t peer_idlen,
|
||||
SM9PrivateKey *sk, int initiator)
|
||||
{
|
||||
return NULL;
|
||||
int ret = 0;
|
||||
EC_GROUP *group = NULL;
|
||||
EC_POINT *Ppube = NULL;
|
||||
EC_POINT *Q = NULL;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
BIGNUM *h = NULL;
|
||||
const EVP_MD *md;
|
||||
int point_form = POINT_CONVERSION_COMPRESSED;
|
||||
const BIGNUM *p = SM9_get0_prime();
|
||||
const BIGNUM *n = SM9_get0_order();
|
||||
fp12_t g;
|
||||
int len;
|
||||
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm9bn256v1))
|
||||
|| !(Ppube = EC_POINT_new(group))
|
||||
|| !(Q = EC_POINT_new(group))
|
||||
|| !(bn_ctx = BN_CTX_new())) {
|
||||
SM9err(SM9_F_SM9_GENERATE_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
BN_CTX_start(bn_ctx);
|
||||
if (!fp12_init(g, bn_ctx)) {
|
||||
SM9err(SM9_F_SM9_GENERATE_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* r = rand(1, n-1) */
|
||||
do {
|
||||
if (!BN_rand_range(r, n)) {
|
||||
SM9err(SM9_F_SM9_GENERATE_KEY_EXCHANGE, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
} while (BN_is_zero(r));
|
||||
|
||||
switch (OBJ_obj2nid(sk->hash1)) {
|
||||
case NID_sm9hash1_with_sm3:
|
||||
md = EVP_sm3();
|
||||
break;
|
||||
case NID_sm9hash1_with_sha256:
|
||||
md = EVP_sha256();
|
||||
break;
|
||||
default:
|
||||
SM9err(SM9_F_SM9_GENERATE_KEY_EXCHANGE, ERR_R_SM9_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* h = H1(peer_id) */
|
||||
if (!SM9_hash1(md, &h, peer_id, peer_idlen, SM9_HID_EXCH, n, bn_ctx)) {
|
||||
SM9err(SM9_F_SM9_GENERATE_KEY_EXCHANGE, ERR_R_SM9_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* get Ppube from sk */
|
||||
if (!EC_POINT_oct2point(group, Ppube, ASN1_STRING_get0_data(sk->pointPpub),
|
||||
ASN1_STRING_length(sk->pointPpub), bn_ctx)) {
|
||||
SM9err(SM9_F_SM9_GENERATE_KEY_EXCHANGE, ERR_R_SM9_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EC_POINT_mul(group, Q, h, NULL, NULL, bn_ctx)
|
||||
/* Q = H1(peer_id) * P1 + Ppube */
|
||||
|| !EC_POINT_add(group, Q, Q, Ppube, bn_ctx)
|
||||
/* R = r * Q */
|
||||
|| !EC_POINT_mul(group, Q, NULL, Q, r, bn_ctx)
|
||||
|| (len = EC_POINT_point2oct(group, Q, point_form, R, *Rlen, bn_ctx)) <= 0) {
|
||||
}
|
||||
*Rlen = len;
|
||||
|
||||
/* g = e(Ppube, P2) */
|
||||
if (!rate_pairing(g, NULL, Ppube, bn_ctx)) {
|
||||
SM9err(SM9_F_SM9_GENERATE_KEY_EXCHANGE, ERR_R_SM9_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* g1' = g2 = g^r */
|
||||
if (!fp12_pow(g, g, r, p, bn_ctx) || !fp12_to_bin(g, gr)) {
|
||||
SM9err(SM9_F_SM9_GENERATE_KEY_EXCHANGE, ERR_R_SM9_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
end:
|
||||
EC_GROUP_free(group);
|
||||
EC_POINT_free(Ppube);
|
||||
EC_POINT_free(Q);
|
||||
if (bn_ctx) {
|
||||
BN_CTX_end(bn_ctx);
|
||||
}
|
||||
BN_CTX_free(bn_ctx);
|
||||
BN_free(h);
|
||||
fp12_cleanup(g);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SM9_compute_share_key(SM9PublicParameters *mpk,
|
||||
unsigned char *out, size_t *outlen,
|
||||
const char *peer_id, size_t peer_idlen, SM9PublicKey *peer_exch,
|
||||
const char *id, size_t idlen, SM9PublicKey *exch,
|
||||
/*
|
||||
* check peer's R in E(F_p)
|
||||
* e = g2' = g1 = e(peer_R, sk)
|
||||
* g3' = g3 = g2'^r = g1^r = er
|
||||
* S' = H(0x82 || g1' || H(g2' || g3' || ID_A || ID_B || R_A || R_B))
|
||||
* SA = H(0x83 || g1' || H(g2' || g3' || ID_A || ID_B || R_A || R_B))
|
||||
* KA = KDF(ID_A || ID_B || R_A || R_B || g1' || g2' || g3')
|
||||
*
|
||||
* KB = KDF(ID_A || ID_B || R_A || R_B || g1 || g2 || g3)
|
||||
* SB = H(0x82 || g1 || Hash(g2 || g3 || ID_A || ID_B || R_A || R_B))
|
||||
* SA'= H(0x83 || g1 || Hash(g2 || g3 || ID_A || ID_B || R_A || R_B))
|
||||
*
|
||||
*/
|
||||
int SM9_compute_share_key(unsigned char *key, size_t keylen,
|
||||
unsigned char *peer_mac, size_t *peer_maclen, /* compure with received peer's mac */
|
||||
unsigned char *mac, size_t *maclen, int compute_mac, /* send to peer */
|
||||
const unsigned char *peer_R, size_t peer_Rlen, /* recv from peer */
|
||||
const unsigned char *R, size_t Rlen, /* cached from SM9_generate_key_exchange */
|
||||
const BIGNUM *r, /* cached from SM9_generate_key_exchange */
|
||||
const char *peer_id, size_t peer_idlen,
|
||||
SM9PrivateKey *sk, int initiator)
|
||||
{
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user