update ec

ec_kmeth and strict signature
This commit is contained in:
Zhi Guan
2017-02-19 23:42:33 +08:00
parent 90a001b539
commit 046a5b2104
5 changed files with 112 additions and 15 deletions

View File

@@ -24,7 +24,13 @@ static const EC_KEY_METHOD openssl_ec_key_method = {
ossl_ecdsa_sign_setup,
ossl_ecdsa_sign_sig,
ossl_ecdsa_verify,
ossl_ecdsa_verify_sig
ossl_ecdsa_verify_sig,
#ifndef OPENSSL_NO_SM2
ossl_ecies_encrypt,
ossl_ecies_do_encrypt,
ossl_ecies_decrypt,
ossl_ecies_do_decrypt,
#endif
};
static const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
@@ -315,3 +321,18 @@ void EC_KEY_METHOD_get_verify(EC_KEY_METHOD *meth,
if (pverify_sig != NULL)
*pverify_sig = meth->verify_sig;
}
#ifndef OPENSSL_NO_SM2
void EC_KEY_METHOD_get_encrypt(EC_KEY_METHOD *meth,
int (**pencrypt)(int a),
int (**pdo_encrypt)(int a))
{
if (pencrypt != NULL)
*pencrypt = meth->encrypt;
if (pdo_encrypt != NULL)
*pdo_encrypt = meth->do_encrypt;
}
#endif

View File

@@ -624,3 +624,14 @@ int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
const uint8_t peer_public_value[32]);
void X25519_public_from_private(uint8_t out_public_value[32],
const uint8_t private_key[32]);
#ifndef OPENSSL_NO_SM2
int ossl_ecies_encrypt(int type, const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key);
ECIES_CIPHERTEXT_VALUE *ossl_ecies_do_encrypt(int type, const unsigned char *in,
size_t inlen, EC_KEY *ec_key);
int ossl_ecies_decrypt(int type, const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key);
int ossl_ecies_do_decrypt(int type, const ECIES_CIPHERTEXT_VALUE *in,
unsigned char *out, size_t *outlen, EC_KEY *ec_key);
#endif

View File

@@ -297,6 +297,19 @@ ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
}
while (1);
#if 0
if (!BN_rshift1(tmp, order)) {
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
goto err;
}
if (BN_cmp(ret->s, tmp) <= 0) {
if (!BN_sub(ret->s, order, ret->s)) {
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
goto err;
}
}
#endif
ok = 1;
err:
if (!ok) {
@@ -392,6 +405,18 @@ int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
ret = 0; /* signature is invalid */
goto err;
}
#if 0
if (!BN_rshift1(m, order)) {
ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
goto err;
}
if (BN_cmp(sig->r, m) <= 0) {
ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
ret = 0;
goto err;
}
#endif
/* calculate tmp1 = inv(S) mod order */
if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);

28
crypto/ecies/ecies_ossl.c Normal file
View File

@@ -0,0 +1,28 @@
int ossl_ecies_encrypt(int type, const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key)
{
return 0;
}
ECIES_CIPHERTEXT_VALUE *ossl_ecies_do_encrypt(int type, const unsigned char *in,
size_t inlen, EC_KEY *ec_key)
{
return NULL;
}
int ossl_ecies_decrypt(int type, const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key)
{
return 0;
}
int ossl_ecies_do_decrypt(int type, const ECIES_CIPHERTEXT_VALUE *in,
unsigned char *out, size_t *outlen, EC_KEY *ec_key)
{
return NULL;
}

View File

@@ -220,8 +220,6 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgstlen,
}
#endif
fprintf(stderr, " --- %s %d\n", __func__, __LINE__);
do {
/* use or compute k and (kG).x */
if (!in_k || !in_x) {
@@ -237,8 +235,6 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgstlen,
goto end;
}
}
fprintf(stderr, " --- %s %d\n", __func__, __LINE__);
/* r = e + x (mod n) */
if (!BN_mod_add(ret->r, ret->r, e, order, ctx)) {
@@ -246,13 +242,10 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgstlen,
goto end;
}
fprintf(stderr, " --- %s %d\n", __func__, __LINE__);
if (!BN_mod_add(bn, ret->r, ck, order, ctx)) {
ECerr(EC_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto end;
}
fprintf(stderr, " --- %s %d\n", __func__, __LINE__);
/* check r != 0 && r + k != n */
if (BN_is_zero(ret->r) || BN_is_zero(bn)) {
@@ -263,28 +256,21 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgstlen,
continue;
}
fprintf(stderr, " --- %s %d\n", __func__, __LINE__);
/* s = ((1 + d)^-1 * (k - rd)) mod n */
if (!BN_one(bn)) {
ECerr(EC_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto end;
}
if (ret->s == NULL) {
fprintf(stderr, " --- %s %d\n", __func__, __LINE__);
}
fprintf(stderr, " --- %s %d\n", __func__, __LINE__);
if (!BN_mod_add(ret->s, priv_key, bn, order, ctx)) {
ECerr(EC_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto end;
}
fprintf(stderr, " --- %s %d\n", __func__, __LINE__);
if (!BN_mod_inverse(ret->s, ret->s, order, ctx)) {
ECerr(EC_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto end;
}
fprintf(stderr, " --- %s %d\n", __func__, __LINE__);
if (!BN_mod_mul(bn, ret->r, priv_key, order, ctx)) {
ECerr(EC_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto end;
@@ -310,6 +296,21 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgstlen,
} while (1);
#if 0
if (!BN_rshift1(bn, order)) {
ECerr(EC_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto end;
}
if (BN_cmp(ret->r, bn) <= 0) {
if (!BN_sub(ret->r, order, ret->r)
|| !BN_sub(ret->s, order, ret->s)) {
ECerr(EC_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto end;
}
}
#endif
ok = 1;
end:
@@ -360,6 +361,17 @@ int sm2_do_verify(const unsigned char *dgst, int dgstlen,
goto end;
}
#if 0
if (!BN_rshift1(t, order)) {
ECerr(EC_F_SM2_DO_VERIFY, ERR_R_BN_LIB);
goto end;
}
if (BN_cmp(sig->r, t) <= 0) {
ECerr(EC_F_SM2_DO_VERIFY, ERR_R_BN_LIB); //FIXME: error code
goto end;
}
#endif
/* check r, s in [1, n-1] and r + s != 0 (mod n) */
if (BN_is_zero(sig->r) ||
BN_is_negative(sig->r) ||