Add sm9 and paillier pem support

This commit is contained in:
Zhi Guan
2018-11-16 15:26:30 +08:00
parent 5baad7e043
commit 9a999ae907
38 changed files with 7956 additions and 6992 deletions

View File

@@ -50,9 +50,60 @@
#include <string.h>
#include <openssl/err.h>
#include <openssl/sm9.h>
#include <openssl/crypto.h>
#include <openssl/bn_hash.h>
#include "sm9_lcl.h"
SM9_MASTER_KEY *SM9_MASTER_KEY_new(void)
{
SM9_MASTER_KEY *ret = NULL;
if (!(ret = OPENSSL_zalloc(sizeof(*ret)))) {
SM9err(SM9_F_SM9_MASTER_KEY_NEW, ERR_R_MALLOC_FAILURE);
return NULL;
}
return ret;
}
void SM9_MASTER_KEY_free(SM9_MASTER_KEY *key)
{
if (key) {
ASN1_OBJECT_free(key->pairing);
ASN1_OBJECT_free(key->scheme);
ASN1_OBJECT_free(key->hash1);
ASN1_OCTET_STRING_free(key->pointPpub);
BN_clear_free(key->masterSecret);
}
OPENSSL_clear_free(key, sizeof(*key));
}
SM9_KEY *SM9_KEY_new(void)
{
SM9_KEY *ret = NULL;
if (!(ret = OPENSSL_zalloc(sizeof(*ret)))) {
SM9err(SM9_F_SM9_KEY_NEW, ERR_R_MALLOC_FAILURE);
return NULL;
}
return ret;
}
void SM9_KEY_free(SM9_KEY *key)
{
if (key) {
ASN1_OBJECT_free(key->pairing);
ASN1_OBJECT_free(key->scheme);
ASN1_OBJECT_free(key->hash1);
ASN1_OCTET_STRING_free(key->pointPpub);
ASN1_OCTET_STRING_free(key->identity);
ASN1_OCTET_STRING_free(key->publicPoint);
}
OPENSSL_clear_free(key, sizeof(*key));
}
int SM9PrivateKey_get_gmtls_public_key(SM9PublicParameters *mpk,
SM9PrivateKey *sk, unsigned char pub_key[1024])
{
@@ -99,32 +150,26 @@ int SM9_DigestInit(EVP_MD_CTX *ctx, unsigned char prefix,
return 1;
}
#if 0
int SM9_DigestFinal(EVP_MD_CTX *ctx1, BIGNUM *h, const BIGNUM *n_1)
int SM9_MASTER_KEY_up_ref(SM9_MASTER_KEY *msk)
{
int ret = 0;
EVP_MD_CTX *ctx2 = NULL;
const unsigned char ct1[4] = {0x00, 0x00, 0x00, 0x01};
const unsigned char ct2[4] = {0x00, 0x00, 0x00, 0x02};
unsigned char Ha[EVP_MAX_MD_SIZE * 2];
unsigned int len = 0;
int i;
if (!(ctx2 = EVP_MD_CTX_new())
|| !EVP_MD_CTX_copy(ctx2, ctx1)
|| !EVP_DigestUpdate(ctx1, ct1, sizeof(ct))
|| !EVP_DigestUpdate(ctx2, ct2, sizeof(ct2))
|| !EVP_DigestFinal_ex(ctx1, Ha, &len)
|| !EVP_DigestFinal_ex(ctx2, Ha + len, &len)
|| !BN_bin2bn(Ha, 40, h)
|| !BN_mod(h, h, n_1, bn_ctx)
|| !BN_add_word(h, 1)) {
ERR_print_errors_fp(stderr);
goto end;
}
if (CRYPTO_atomic_add(&msk->references, 1, &i, msk->lock) <= 0)
return 0;
ret = 1;
end:
EVP_MD_CTX_free(ctx2);
return ret;
REF_PRINT_COUNT("SM9_MASTER_KEY", msk);
REF_ASSERT_ISNT(i < 2);
return ((i > 1) ? 1 : 0);
}
int SM9_KEY_up_ref(SM9_KEY *sk)
{
int i;
if (CRYPTO_atomic_add(&sk->references, 1, &i, sk->lock) <= 0)
return 0;
REF_PRINT_COUNT("SM9_KEY", sk);
REF_ASSERT_ISNT(i < 2);
return ((i > 1) ? 1 : 0);
}
#endif