v2 pre-release

This commit is contained in:
Zhi Guan
2017-12-07 23:58:48 +08:00
parent a569601747
commit f5a3f271a3
45 changed files with 5567 additions and 7247 deletions

View File

@@ -165,7 +165,7 @@ int base58_encode(const void *data, size_t binsz, char *b58, size_t *b58sz)
{
const uint8_t *bin = data;
int carry;
ssize_t i, j, high, zcount = 0;
size_t i, j, high, zcount = 0;
size_t size;
//uint8_t buf[size];
uint8_t *buf = NULL;

View File

@@ -6,7 +6,6 @@ SOURCE[../../libcrypto]=\
ecp_nistp224.c ecp_nistp256.c ecp_nistp521.c ecp_nistputil.c \
ecp_oct.c ec2_oct.c ec_oct.c ec_kmeth.c ecdh_ossl.c ecdh_kdf.c \
ecdsa_ossl.c ecdsa_sign.c ecdsa_vrf.c curve25519.c ecx_meth.c \
ec_ctrl.c \
{- $target{ec_asm_src} -}
GENERATE[ecp_nistz256-x86.s]=asm/ecp_nistz256-x86.pl $(PERLASM_SCHEME) $(CFLAGS) $(LIB_CFLAGS) $(PROCESSOR)

View File

@@ -1,138 +0,0 @@
/*
* Copyright (c) 2015 - 2017 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
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <openssl/ec.h>
#ifdef OPENSSL_NO_MACRO
int EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX *ctx, int nid)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN,
EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL);
}
int EVP_PKEY_CTX_set_ec_param_enc(EVP_PKEY_CTX *ctx, int param_enc)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN,
EVP_PKEY_CTRL_EC_PARAM_ENC, param_enc, NULL);
}
int EVP_PKEY_CTX_set_ecdh_cofactor_mode(EVP_PKEY_CTX *ctx, int co_mode)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_EC_ECDH_COFACTOR, co_mode, NULL);
}
int EVP_PKEY_CTX_get_ecdh_cofactor_mode(EVP_PKEY_CTX *ctx)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_EC_ECDH_COFACTOR, -2, NULL);
}
int EVP_PKEY_CTX_set_ecdh_kdf_type(EVP_PKEY_CTX *ctx, int kdf)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_EC_KDF_TYPE, kdf, NULL);
}
int EVP_PKEY_CTX_get_ecdh_kdf_type(EVP_PKEY_CTX *ctx)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_EC_KDF_TYPE, -2, NULL);
}
int EVP_PKEY_CTX_set_ecdh_kdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_EC_KDF_MD, 0, (void *)md);
}
int EVP_PKEY_CTX_get_ecdh_kdf_md(EVP_PKEY_CTX *ctx, const EVP_MD **pmd)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_GET_EC_KDF_MD, 0, (void *)pmd);
}
int EVP_PKEY_CTX_set_ecdh_kdf_outlen(EVP_PKEY_CTX *ctx, int len)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_EC_KDF_OUTLEN, len, NULL);
}
int EVP_PKEY_CTX_get_ecdh_kdf_outlen(EVP_PKEY_CTX *ctx, int *plen)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, 0, (void *)plen);
}
int EVP_PKEY_CTX_set0_ecdh_kdf_ukm(EVP_PKEY_CTX *ctx, unsigned char *der, int len)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_EC_KDF_UKM, len, (void *)der)
}
int EVP_PKEY_CTX_get0_ecdh_kdf_ukm(EVP_PKEY_CTX *ctx, unsigned char **pder)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)pder)
}
#endif

View File

@@ -15,7 +15,7 @@ SOURCE[../../libcrypto]=\
e_chacha20_poly1305.c cmeth_lib.c \
m_sm3.c \
e_sms4.c e_sms4_ccm.c e_sms4_gcm.c e_sms4_ocb.c e_sms4_wrap.c e_sms4_xts.c \
evp_ctxt.c names2.c evp_nomacro.c
evp_ctxt.c names2.c
INCLUDE[e_aes.o]=.. ../modes
INCLUDE[e_aes_cbc_hmac_sha1.o]=../modes

View File

@@ -1,306 +0,0 @@
#ifdef OPENSSL_NO_MACRO
# ifndef OPENSSL_NO_RSA
int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *rsa)
{
return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, (char *)rsa);
}
# endif
# ifndef OPENSSL_NO_DSA
int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *dsa)
{
return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, (char *)dsa);
}
# endif
# ifndef OPENSSL_NO_DH
int EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *dh)
{
return EVP_PKEY_assign(pkey, EVP_PKEY_DH, (char *)dh);
}
# endif
# ifndef OPENSSL_NO_EC
int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *ec_key)
{
return EVP_PKEY_assign(pkey, EVP_PKEY_EC, (char *)ec_key);
}
# endif
# ifndef OPENSSL_NO_PAILLIER
int EVP_PKEY_assign_PAILLIER(EVP_PKEY *pkey, PAILLIER *paillier)
{
return EVP_PKEY_assign(pkey, EVP_PKEY_PAILLIER, (char *)paillier);
}
#endif
const EVP_MD *EVP_get_digestbynid(int nid)
{
return EVP_get_digestbyname(OBJ_nid2sn(nid));
}
const EVP_MD *EVP_get_digestbyobj(ASN1_OBJECT *obj)
{
return EVP_get_digestbynid(OBJ_obj2nid(obj));
}
const EVP_CIPHER *EVP_get_cipherbynid(int nid)
{
return EVP_get_cipherbyname(OBJ_nid2sn(nid));
}
const EVP_CIPHER *EVP_get_cipherbyobj(ASN1_OBJECT *obj)
{
return EVP_get_cipherbynid(OBJ_obj2nid(obj));
}
int EVP_MD_nid(const EVP_MD *md)
{
return EVP_MD_type(md);
}
const char *EVP_MD_name(const EVP_MD *md)
{
return OBJ_nid2sn(EVP_MD_nid(md));
}
int EVP_MD_CTX_size(EVP_MD_CTX *ctx)
{
return EVP_MD_size(EVP_MD_CTX_md(ctx));
}
int EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
{
return EVP_MD_block_size(EVP_MD_CTX_md(ctx));
}
int EVP_MD_CTX_type(EVP_MD_CTX *ctx)
{
return EVP_MD_type(EVP_MD_CTX_md(ctx));
}
const char *EVP_CIPHER_name(const EVP_CIPHER *cipher)
{
return OBJ_nid2sn(EVP_CIPHER_nid(cipher));
}
int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
{
return (EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE);
}
int EVP_CIPHER_CTX_type(EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_type(EVP_CIPHER_CTX_cipher(ctx));
}
# if OPENSSL_API_COMPAT < 0x10100000L
int EVP_CIPHER_CTX_flags(EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx));
}
# endif
int EVP_CIPHER_CTX_mode(EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_mode(EVP_CIPHER_CTX_cipher(ctx));
}
long EVP_ENCODE_LENGTH(long l)
{
return (((l+2)/3*4)+(l/48+1)*2+80);
}
long EVP_DECODE_LENGTH(long l)
{
return ((l+3)/4*3+80);
}
__owur int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
ENGINE *impl)
{
return EVP_DigestInit_ex(ctx, type, impl);
}
__owur int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type)
{
return EVP_DigestInit(ctx, type);
}
__owur int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt)
{
return EVP_DigestUpdate(ctx, d, cnt);
}
__owur int EVP_VerifyInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
ENGINE *impl)
{
return EVP_DigestInit_ex(ctx, type, impl);
}
__owur int EVP_VerifyInit(EVP_MD_CTX *ctx, const EVP_MD *type)
{
return EVP_DigestInit(ctx, type);
}
__owur int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt)
{
return EVP_DigestUpdate(ctx, d, cnt);
}
/*__owur*/ int EVP_OpenUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
int *outl, const unsigned char *in, int inl)
{
return EVP_DecryptUpdate(ctx, out, outl, in, inl);
}
/*__owur*/ int EVP_SealUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
int *outl, const unsigned char *in, int inl)
{
return EVP_EncryptUpdate(ctx, out, outl, in, inl);
}
__owur int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt)
{
return EVP_DigestUpdate(ctx, d, cnt);
}
__owur int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt)
{
return EVP_DigestUpdate(ctx, d, cnt);
}
long BIO_get_md(BIO *bio, const EVP_MD **pmd)
{
return BIO_ctrl(bio,BIO_C_GET_MD,0,(char *)pmd);
}
long BIO_get_md_ctx(BIO *bio, EVP_MD_CTX **pmctx)
{
return BIO_ctrl(bio,BIO_C_GET_MD_CTX,0,(char *)pmctx);
}
long BIO_set_md_ctx(BIO *bio, EVP_MD_CTX *mctx)
{
return BIO_ctrl(bio,BIO_C_SET_MD_CTX,0,(char *)mctx);
}
long BIO_get_cipher_status(BIO *bio)
{
return BIO_ctrl(bio,BIO_C_GET_CIPHER_STATUS,0,NULL);
}
long BIO_get_cipher_ctx(BIO *bio, EVP_CIPHER_CTX *pcctx)
{
return BIO_ctrl(bio,BIO_C_GET_CIPHER_CTX,0,(char *)pcctx)
}
int EVP_add_cipher_alias(int type, const char *alias)
{
return OBJ_NAME_add(alias,
OBJ_NAME_TYPE_CIPHER_METH|OBJ_NAME_ALIAS, type);
}
int EVP_add_digest_alias(int type, const char *alias)
{
return OBJ_NAME_add(alias,
OBJ_NAME_TYPE_MD_METH|OBJ_NAME_ALIAS, type);
}
int EVP_delete_cipher_alias(const char *alias)
{
return OBJ_NAME_remove(alias,
OBJ_NAME_TYPE_CIPHER_METH|OBJ_NAME_ALIAS);
}
int EVP_delete_digest_alias(const char *alias)
{
return OBJ_NAME_remove(alias,
OBJ_NAME_TYPE_MD_METH|OBJ_NAME_ALIAS);
}
EVP_MD_CTX *EVP_MD_CTX_create(void)
{
return EVP_MD_CTX_new();
}
int EVP_MD_CTX_init(EVP_MD_CTX *ctx)
{
return EVP_MD_CTX_reset(ctx);
}
void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
{
EVP_MD_CTX_free(ctx);
}
# if OPENSSL_API_COMPAT < 0x10100000L
int EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_CTX_reset(ctx);
}
int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *ctx)
{
return EVP_CIPHER_CTX_reset(ctx);
}
# endif
int OPENSSL_add_all_algorithms_conf(void)
{
return OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
| OPENSSL_INIT_ADD_ALL_DIGESTS
| OPENSSL_INIT_LOAD_CONFIG, NULL);
}
int OPENSSL_add_all_algorithms_noconf(void)
{
return OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
| OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
}
int OpenSSL_add_all_algorithms(void)
{
# ifdef OPENSSL_LOAD_CONF
return OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
| OPENSSL_INIT_ADD_ALL_DIGESTS
| OPENSSL_INIT_LOAD_CONFIG, NULL);
# else
return OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
| OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
# endif
}
int OpenSSL_add_all_ciphers(void)
{
return OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
}
int OPENSSL_add_all_digests(void)
{
return OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
}
void OPENSSL_cleanup(void)
{
}
int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
{
return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
EVP_PKEY_CTRL_MD, 0, (void *)md);
}
int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **pmd)
{
return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
EVP_PKEY_CTRL_GET_MD, 0, (void *)pmd);
}
int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key, int keylen)
{
return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_KEYGEN,
EVP_PKEY_CTRL_SET_MAC_KEY, len, (void *)key);
}
#endif

View File

@@ -14,7 +14,6 @@
#include <openssl/x509.h>
#include "internal/evp_int.h"
// 不同的cipher属性不同似乎不能像MD那样简单处理
const EVP_CIPHER *EVP_get_default_cipher(void)
{
return NULL;

View File

@@ -74,6 +74,7 @@ static ERR_STRING_DATA SDF_str_functs[] = {
{ERR_FUNC(SDF_F_SDF_IMPORTKEYWITHISK_ECC), "SDF_ImportKeyWithISK_ECC"},
{ERR_FUNC(SDF_F_SDF_IMPORTKEYWITHISK_RSA), "SDF_ImportKeyWithISK_RSA"},
{ERR_FUNC(SDF_F_SDF_IMPORTKEYWITHKEK), "SDF_ImportKeyWithKEK"},
{ERR_FUNC(SDF_F_SDF_INTERNALDECRYPT_ECC), "SDF_InternalDecrypt_ECC"},
{ERR_FUNC(SDF_F_SDF_INTERNALPRIVATEKEYOPERATION_RSA),
"SDF_InternalPrivateKeyOperation_RSA"},
{ERR_FUNC(SDF_F_SDF_INTERNALPUBLICKEYOPERATION_RSA),

View File

@@ -879,7 +879,6 @@ int SDF_GenerateKeyWithKEK(
return SDR_OK;
}
int SDF_ImportKeyWithKEK(
void *hSessionHandle,
unsigned int uiAlgID,
@@ -917,7 +916,6 @@ int SDF_ImportKeyWithKEK(
return SDR_OK;
}
int SDF_DestroyKey(
void *hSessionHandle,
void *hKeyHandle)
@@ -939,7 +937,6 @@ int SDF_DestroyKey(
return SDR_OK;
}
int SDF_ExternalPublicKeyOperation_RSA(
void *hSessionHandle,
RSArefPublicKey *pucPublicKey,
@@ -971,7 +968,6 @@ int SDF_ExternalPublicKeyOperation_RSA(
return SDR_OK;
}
int SDF_InternalPublicKeyOperation_RSA(
void *hSessionHandle,
unsigned int uiKeyIndex,
@@ -1003,7 +999,6 @@ int SDF_InternalPublicKeyOperation_RSA(
return SDR_OK;
}
int SDF_InternalPrivateKeyOperation_RSA(
void *hSessionHandle,
unsigned int uiKeyIndex,
@@ -1035,7 +1030,6 @@ int SDF_InternalPrivateKeyOperation_RSA(
return SDR_OK;
}
int SDF_ExternalVerify_ECC(
void *hSessionHandle,
unsigned int uiAlgID,
@@ -1074,7 +1068,6 @@ int SDF_ExternalVerify_ECC(
return SDR_OK;
}
int SDF_InternalSign_ECC(
void *hSessionHandle,
unsigned int uiISKIndex,
@@ -1103,7 +1096,6 @@ int SDF_InternalSign_ECC(
return SDR_OK;
}
int SDF_InternalVerify_ECC(
void *hSessionHandle,
unsigned int uiIPKIndex,
@@ -1132,7 +1124,6 @@ int SDF_InternalVerify_ECC(
return SDR_OK;
}
int SDF_ExternalEncrypt_ECC(
void *hSessionHandle,
unsigned int uiAlgID,
@@ -1171,6 +1162,35 @@ int SDF_ExternalEncrypt_ECC(
return SDR_OK;
}
int SDF_InternalDecrypt_ECC(
void *hSessionHandle,
unsigned int uiISKIndex,
unsigned int uiAlgID,
ECCCipher *pucEncData,
unsigned char *pucData,
unsigned int *uiDataLength)
{
int ret = SDR_UNKNOWERR;
if (!sdf_method || !sdf_method->InternalDecrypt_ECC) {
SDFerr(SDF_F_SDF_INTERNALDECRYPT_ECC, SDF_R_NOT_INITIALIZED);
return SDR_NOTSUPPORT;
}
if ((ret = sdf_method->InternalDecrypt_ECC(
hSessionHandle,
uiISKIndex,
uiAlgID,
pucEncData,
pucData,
uiDataLength)) != SDR_OK) {
SDFerr(SDF_F_SDF_INTERNALDECRYPT_ECC,
sdf_get_error_reason(ret));
return ret;
}
return SDR_OK;
}
int SDF_Encrypt(
void *hSessionHandle,
@@ -1213,7 +1233,6 @@ int SDF_Encrypt(
return SDR_OK;
}
int SDF_Decrypt(
void *hSessionHandle,
void *hKeyHandle,
@@ -1254,7 +1273,6 @@ int SDF_Decrypt(
return SDR_OK;
}
int SDF_CalculateMAC(
void *hSessionHandle,
void *hKeyHandle,
@@ -1330,7 +1348,6 @@ int SDF_HashInit(
return SDR_OK;
}
int SDF_HashUpdate(
void *hSessionHandle,
unsigned char *pucData,
@@ -1354,7 +1371,6 @@ int SDF_HashUpdate(
return SDR_OK;
}
int SDF_HashFinal(
void *hSessionHandle,
unsigned char *pucHash,
@@ -1378,7 +1394,6 @@ int SDF_HashFinal(
return SDR_OK;
}
int SDF_CreateFile(
void *hSessionHandle,
unsigned char *pucFileName,

View File

@@ -108,11 +108,9 @@ SDF_METHOD *SDF_METHOD_load_library(const char *so_path)
SDF_METHOD_BIND_FUNCTION(InternalSign_ECC);
SDF_METHOD_BIND_FUNCTION(InternalVerify_ECC);
SDF_METHOD_BIND_FUNCTION(ExternalEncrypt_ECC);
#if 0
SDF_METHOD_BIND_FUNCTION(ExternalDecrypt_ECC);
SDF_METHOD_BIND_FUNCTION(InternalEncrypt_ECC);
// SDF_METHOD_BIND_FUNCTION(ExternalDecrypt_ECC);
// SDF_METHOD_BIND_FUNCTION(InternalEncrypt_ECC);
SDF_METHOD_BIND_FUNCTION(InternalDecrypt_ECC);
#endif
SDF_METHOD_BIND_FUNCTION(Encrypt);
SDF_METHOD_BIND_FUNCTION(Decrypt);
SDF_METHOD_BIND_FUNCTION(CalculateMAC);

View File

@@ -73,6 +73,7 @@ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. */
#include <stdio.h>
#include <string.h>
#include <openssl/crypto.h>
#include <openssl/serpent.h>
@@ -267,8 +268,8 @@ void serpent_set_decrypt_key(serpent_key_t *key, const unsigned char *user_key)
void serpent_encrypt(const void *in, void *out, serpent_key_t *key)
{
int8_t i;
memcpy(out, in, SERPENT_BLOCK_SIZE);
serpent_blk *_out = out;
memcpy(out, in, SERPENT_BLOCK_SIZE);
i = 0;
for (;;) {
@@ -287,9 +288,9 @@ void serpent_encrypt(const void *in, void *out, serpent_key_t *key)
void serpent_decrypt(const void *in, void *out, serpent_key_t *key)
{
int8_t i;
serpent_blk *_out = out;
memcpy(out, in, SERPENT_BLOCK_SIZE);
serpent_blk *_out = out;
i = SERPENT_ROUNDS;
serpent_whiten(_out, key, i);

View File

@@ -1,3 +1,3 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=sm2_err.c sm2_asn1.c sm2_id.c sm2_sign.c sm2_enc.c \
sm2_oct.c sm2_exch.c sm2_kmeth.c sm2_ctrl.c
sm2_oct.c sm2_exch.c sm2_kmeth.c

View File

@@ -1,114 +0,0 @@
/*
* Copyright (c) 2015 - 2017 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
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <openssl/sm2.h>
#ifdef OPENSSL_NO_MACRO
int EVP_PKEY_CTX_set_ec_scheme(EVP_PKEY_CTX *ctx, int scheme)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_SIGN|EVP_PKEY_OP_SIGNCTX|
EVP_PKEY_OP_VERIFY|EVP_PKEY_OP_VERIFYCTX|
EVP_PKEY_OP_ENCRYPT|EVP_PKEY_OP_DECRYPT|
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_EC_SCHEME, scheme, NULL);
}
int EVP_PKEY_CTX_get_ec_scheme(EVP_PKEY_CTX *ctx, int scheme)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_SIGN|EVP_PKEY_OP_SIGNCTX|
EVP_PKEY_OP_VERIFY|EVP_PKEY_OP_VERIFYCTX|
EVP_PKEY_OP_ENCRYPT|EVP_PKEY_OP_DECRYPT|
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_EC_SCHEME, -2, NULL);
}
int EVP_PKEY_CTX_set_signer_id(EVP_PKEY_CTX *ctx, const char *id)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_SIGN|EVP_PKEY_OP_SIGNCTX|
EVP_PKEY_OP_VERIFY|EVP_PKEY_OP_VERIFYCTX|
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_SIGNER_ID, 0, (void *)id);
}
int EVP_PKEY_CTX_get_signer_id(EVP_PKEY_CTX *ctx, const char *id)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_SIGN|EVP_PKEY_OP_SIGNCTX|
EVP_PKEY_OP_VERIFY|EVP_PKEY_OP_VERIFYCTX|
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_GET_SIGNER_ID, 0, (void *)pid)
}
int EVP_PKEY_CTX_get_signer_zid(EVP_PKEY_CTX *ctx, unsigned char **pzid)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_SIGN|EVP_PKEY_OP_SIGNCTX|
EVP_PKEY_OP_VERIFY|EVP_PKEY_OP_VERIFYCTX|
EVP_PKEY_OP_DERIVE,
EVP_PKEY_CTRL_GET_SIGNER_ZID, 0, (void *)pzid);
}
int EVP_PKEY_CTX_set_ec_encrypt_param(EVP_PKEY_CTX *ctx, int param)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_ENCRYPT|EVP_PKEY_OP_DECRYPT,
EVP_PKEY_CTRL_EC_ENCRYPT_PARAM, param, NULL);
}
int EVP_PKEY_CTX_get_ec_encrypt_param(EVP_PKEY_CTX *ctx)
{
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
EVP_PKEY_OP_ENCRYPT|EVP_PKEY_OP_DECRYPT,
EVP_PKEY_CTRL_EC_ENCRYPT_PARAM, -2, NULL);
}
#endif

View File

@@ -1,585 +0,0 @@
/* ====================================================================
* Copyright (c) 2015 - 2016 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
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
*/
#include <string.h>
#include <openssl/ec.h>
#include <openssl/sm2.h>
#include <openssl/kdf.h>
#include "sm2_lcl.h"
int SM2_KAP_CTX_init(SM2_KAP_CTX *ctx,
EC_KEY *ec_key, const char *id, size_t idlen,
EC_KEY *remote_pubkey, const char *rid, size_t ridlen,
int is_initiator, int do_checksum)
{
int ret = 0;
int w;
size_t len;
if (!ctx || !ec_key || !remote_pubkey) {
ECerr(EC_F_SM2_KAP_CTX_INIT, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
memset(ctx, 0, sizeof(*ctx));
ctx->id_dgst_md = EVP_sm3();
ctx->kdf_md = EVP_sm3();
ctx->checksum_md = EVP_sm3();
ctx->point_form = SM2_DEFAULT_POINT_CONVERSION_FORM;
if (!(ctx->kdf = KDF_get_x9_63(ctx->kdf_md))) {
ECerr(EC_F_SM2_KAP_CTX_INIT, EC_R_INVALID_KDF_MD);
goto end;
}
ctx->is_initiator = is_initiator;
ctx->do_checksum = do_checksum;
if (EC_GROUP_cmp(EC_KEY_get0_group(ec_key),
EC_KEY_get0_group(remote_pubkey), NULL) != 0) {
ECerr(EC_F_SM2_KAP_CTX_INIT, 0);
goto end;
}
len = ctx->id_dgstlen;
if (!SM2_compute_id_digest(ctx->id_dgst_md, id, idlen,
ctx->id_dgst, &len, ec_key)) {
ECerr(EC_F_SM2_KAP_CTX_INIT, 0);
goto end;
}
ctx->id_dgstlen = len;
if (!(ctx->ec_key = EC_KEY_dup(ec_key))) {
ECerr(EC_F_SM2_KAP_CTX_INIT, ERR_R_EC_LIB);
goto end;
}
len = ctx->remote_id_dgstlen;
if (!SM2_compute_id_digest(ctx->id_dgst_md, rid, ridlen,
ctx->remote_id_dgst, &len, remote_pubkey)) {
ECerr(EC_F_SM2_KAP_CTX_INIT, 0);
goto end;
}
ctx->remote_id_dgstlen = len;
if (!(ctx->remote_pubkey = EC_KEY_dup(remote_pubkey))) {
ECerr(EC_F_SM2_KAP_CTX_INIT, 0);
goto end;
}
ctx->group = EC_KEY_get0_group(ec_key);
ctx->bn_ctx = BN_CTX_new();
ctx->order = BN_new();
ctx->two_pow_w = BN_new();
ctx->t = BN_new();
if (!ctx->bn_ctx || !ctx->order || !ctx->two_pow_w || !ctx->t) {
ECerr(EC_F_SM2_KAP_CTX_INIT, ERR_R_BN_LIB);
goto end;
}
if (!EC_GROUP_get_order(EC_KEY_get0_group(ec_key), ctx->order, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_CTX_INIT, ERR_R_EC_LIB);
goto end;
}
w = (BN_num_bits(ctx->order) + 1)/2 - 1;
if (!BN_one(ctx->two_pow_w)) {
ECerr(EC_F_SM2_KAP_CTX_INIT, ERR_R_BN_LIB);
goto end;
}
if (!BN_lshift(ctx->two_pow_w, ctx->two_pow_w, w)) {
ECerr(EC_F_SM2_KAP_CTX_INIT, ERR_R_BN_LIB);
goto end;
}
if (!(ctx->point = EC_POINT_new(ctx->group))) {
ECerr(EC_F_SM2_KAP_CTX_INIT, ERR_R_EC_LIB);
goto end;
}
ret = 1;
end:
if (!ret) SM2_KAP_CTX_cleanup(ctx);
return ret;
}
void SM2_KAP_CTX_cleanup(SM2_KAP_CTX *ctx)
{
if (ctx) {
EC_KEY_free(ctx->ec_key);
EC_KEY_free(ctx->remote_pubkey);
BN_CTX_free(ctx->bn_ctx);
BN_free(ctx->two_pow_w);
BN_free(ctx->order);
EC_POINT_free(ctx->point);
BN_free(ctx->t);
memset(ctx, 0, sizeof(*ctx));
}
}
/* FIXME: ephem_point_len should be both input and output */
int SM2_KAP_prepare(SM2_KAP_CTX *ctx, unsigned char *ephem_point,
size_t *ephem_point_len)
{
int ret = 0;
const BIGNUM *prikey;
BIGNUM *h = NULL;
BIGNUM *r = NULL;
BIGNUM *x = NULL;
if (!(prikey = EC_KEY_get0_private_key(ctx->ec_key))) {
ECerr(EC_F_SM2_KAP_PREPARE, EC_R_SM2_KAP_NOT_INITED);
return 0;
}
h = BN_new();
r = BN_new();
x = BN_new();
if (!h || !r || !x) {
ECerr(EC_F_SM2_KAP_PREPARE, 0);
goto end;
}
/*
* r = rand(1, n)
* R = rG = (x, y)
*/
do {
if (!BN_rand_range(r, ctx->order)) {
ECerr(EC_F_SM2_KAP_PREPARE, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
goto end;
}
} while (BN_is_zero(r));
if (!EC_POINT_mul(ctx->group, ctx->point, r, NULL, NULL, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_PREPARE, ERR_R_EC_LIB);
goto end;
}
if (EC_METHOD_get_field_type(EC_GROUP_method_of(ctx->group)) == NID_X9_62_prime_field) {
if (!EC_POINT_get_affine_coordinates_GFp(ctx->group, ctx->point, x, NULL, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_PREPARE, ERR_R_EC_LIB);
goto end;
}
} else {
if (!EC_POINT_get_affine_coordinates_GF2m(ctx->group, ctx->point, x, NULL, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_PREPARE, ERR_R_EC_LIB);
goto end;
}
}
/*
* w = ceil(keybits / 2) - 1
* x = 2^w + (x and (2^w - 1)) = 2^w + (x mod 2^w)
* t = (d + x * r) mod n
* t = (h * t) mod n
*/
if (!ctx->t) {
ECerr(EC_F_SM2_KAP_PREPARE, EC_R_SM2_KAP_NOT_INITED);
goto end;
}
if (!BN_nnmod(x, x, ctx->two_pow_w, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_PREPARE, ERR_R_BN_LIB);
goto end;
}
if (!BN_add(x, x, ctx->two_pow_w)) {
ECerr(EC_F_SM2_KAP_PREPARE, ERR_R_BN_LIB);
goto end;
}
if (!BN_mod_mul(ctx->t, x, r, ctx->order, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_PREPARE, ERR_R_BN_LIB);
goto end;
}
if (!BN_mod_add(ctx->t, ctx->t, prikey, ctx->order, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_PREPARE, ERR_R_BN_LIB);
goto end;
}
if (!EC_GROUP_get_cofactor(ctx->group, h, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_PREPARE, ERR_R_EC_LIB);
goto end;
}
if (!BN_mul(ctx->t, ctx->t, h, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_PREPARE, ERR_R_BN_LIB);
goto end;
}
/* encode R = (x, y) for output and local buffer */
// FIXME: ret is size_t and ret is the output length
ret = EC_POINT_point2oct(ctx->group, ctx->point, ctx->point_form,
ephem_point, *ephem_point_len, ctx->bn_ctx);
memcpy(ctx->pt_buf, ephem_point, ret);
*ephem_point_len = ret;
ret = 1;
end:
if (h) BN_free(h);
if (r) BN_free(r);
if (x) BN_free(x);
return ret;
}
int SM2_KAP_compute_key(SM2_KAP_CTX *ctx, const unsigned char *remote_point,
size_t remote_point_len, unsigned char *key, size_t keylen,
unsigned char *checksum, size_t *checksumlen)
{
int ret = 0;
EVP_MD_CTX *md_ctx = NULL;
BIGNUM *x = NULL;
unsigned char share_pt_buf[1 + (OPENSSL_ECC_MAX_FIELD_BITS+7)/4 + EVP_MAX_MD_SIZE * 2 + 100];
unsigned char remote_pt_buf[1 + (OPENSSL_ECC_MAX_FIELD_BITS+7)/4 + 111];
unsigned char dgst[EVP_MAX_MD_SIZE];
unsigned int dgstlen;
unsigned int len, bnlen;
size_t klen = keylen;
md_ctx = EVP_MD_CTX_new();
x = BN_new();
if (!md_ctx || !x) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, 0);
goto end;
}
/*
* decode point R = (x, y), encode (x, y)
* x = 2^w + (x and (2^w - 1)) = 2^w + (x mod 2^w), w = ceil(keybits / 2) - 1
* U = ht * (P + x * R)
* check U != O
*/
if (!EC_POINT_oct2point(ctx->group, ctx->point,
remote_point, remote_point_len, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, 0);
goto end;
}
if (!(len = EC_POINT_point2oct(ctx->group, ctx->point, POINT_CONVERSION_UNCOMPRESSED,
remote_pt_buf, sizeof(remote_pt_buf), ctx->bn_ctx))) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, 0);
goto end;
}
if (EC_METHOD_get_field_type(EC_GROUP_method_of(ctx->group)) == NID_X9_62_prime_field) {
if (!EC_POINT_get_affine_coordinates_GFp(ctx->group, ctx->point, x, NULL, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EC_LIB);
goto end;
}
} else {
if (!EC_POINT_get_affine_coordinates_GF2m(ctx->group, ctx->point, x, NULL, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EC_LIB);
goto end;
}
}
/* x = 2^w + (x and (2^w - 1)) = 2^w + (x mod 2^w) */
if (!BN_nnmod(x, x, ctx->two_pow_w, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_BN_LIB);
goto end;
}
if (!BN_add(x, x, ctx->two_pow_w)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_BN_LIB);
goto end;
}
/*
if (!BN_mod_mul(x, x, ctx->t, ctx->order, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_BN_LIB);
goto end;
}
*/
/* U = ht * (P + x * R), check U != O */
if (!EC_POINT_mul(ctx->group, ctx->point, NULL, ctx->point, x, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EC_LIB);
goto end;
}
if (!EC_POINT_add(ctx->group, ctx->point, ctx->point,
EC_KEY_get0_public_key(ctx->remote_pubkey), ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EC_LIB);
goto end;
}
if (!EC_POINT_mul(ctx->group, ctx->point, NULL, ctx->point, ctx->t, ctx->bn_ctx)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EC_LIB);
goto end;
}
if (EC_POINT_is_at_infinity(ctx->group, ctx->point)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, 0);
goto end;
}
/* encode U, append with ZA, ZB */
if (!(len = EC_POINT_point2oct(ctx->group, ctx->point, POINT_CONVERSION_UNCOMPRESSED,
share_pt_buf, sizeof(share_pt_buf), ctx->bn_ctx))) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, 0);
goto end;
}
if (ctx->is_initiator) {
memcpy(share_pt_buf + len, ctx->id_dgst, ctx->id_dgstlen);
len += ctx->id_dgstlen;
memcpy(share_pt_buf + len, ctx->remote_id_dgst, ctx->remote_id_dgstlen);
len += ctx->remote_id_dgstlen;
} else {
memcpy(share_pt_buf + len, ctx->remote_id_dgst, ctx->remote_id_dgstlen);
len += ctx->remote_id_dgstlen;
memcpy(share_pt_buf + len, ctx->id_dgst, ctx->id_dgstlen);
len += ctx->id_dgstlen;
}
/* key = KDF(xu, yu, ZA, ZB) */
if (!ctx->kdf(share_pt_buf + 1, len - 1, key, &klen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, 0);
goto end;
}
if (ctx->do_checksum) {
/* generate checksum S1 or SB start with 0x02
* S1 = SB = Hash(0x02, yu, Hash(xu, ZA, ZB, x1, y1, x2, y2))
*/
if (!EVP_DigestInit_ex(md_ctx, ctx->checksum_md, NULL)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
bnlen = BN_num_bytes(ctx->order);
if (!EVP_DigestUpdate(md_ctx, share_pt_buf + 1, bnlen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (ctx->is_initiator) {
/* update ZA,ZB,x1,y1,x2,y2 */
if (!EVP_DigestUpdate(md_ctx, ctx->id_dgst, ctx->id_dgstlen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, ctx->remote_id_dgst, ctx->remote_id_dgstlen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, ctx->pt_buf + 1, bnlen * 2)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, remote_pt_buf + 1, bnlen * 2)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
} else {
if (!EVP_DigestUpdate(md_ctx, ctx->remote_id_dgst, ctx->remote_id_dgstlen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, ctx->id_dgst, ctx->id_dgstlen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, remote_pt_buf + 1, bnlen * 2)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, ctx->pt_buf + 1, bnlen * 2)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
}
if (!EVP_DigestFinal_ex(md_ctx, dgst, &dgstlen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
/* now dgst == H(xu,ZA,ZB,x1,y1,x2,y2)
*/
/* S1 = SB = Hash(0x02, yu, dgst) */
if (!EVP_DigestInit_ex(md_ctx, ctx->checksum_md, NULL)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, "\x02", 1)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, share_pt_buf + 1 + bnlen, bnlen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, dgst, dgstlen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
/* output S1 to local buffer or SB to output */
if (ctx->is_initiator) {
if (!EVP_DigestFinal_ex(md_ctx, ctx->checksum, &len)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
} else {
if (!EVP_DigestFinal_ex(md_ctx, checksum, &len)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
*checksumlen = len;
}
/* generate checksum SA or S2 start with 0x03
* SA = S2 = Hash(0x03, yu, dgst)
*/
if (!EVP_DigestInit_ex(md_ctx, ctx->checksum_md, NULL)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, "\x03", 1)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, share_pt_buf + 1 + bnlen, bnlen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (!EVP_DigestUpdate(md_ctx, dgst, dgstlen)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
if (ctx->is_initiator) {
if (!EVP_DigestFinal_ex(md_ctx, checksum, &len)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
*checksumlen = len;
} else {
if (!EVP_DigestFinal_ex(md_ctx, ctx->checksum, &len)) {
ECerr(EC_F_SM2_KAP_COMPUTE_KEY, ERR_R_EVP_LIB);
goto end;
}
}
}
ret = 1;
end:
EVP_MD_CTX_free(md_ctx);
BN_free(x);
return ret;
}
int SM2_KAP_final_check(SM2_KAP_CTX *ctx, const unsigned char *checksum,
size_t checksumlen)
{
if (ctx->do_checksum) {
if (checksumlen != EVP_MD_size(ctx->checksum_md)) {
ECerr(EC_F_SM2_KAP_FINAL_CHECK, EC_R_INVALID_SM2_KAP_CHECKSUM_LENGTH);
return 0;
}
if (memcmp(ctx->checksum, checksum, checksumlen)) {
ECerr(EC_F_SM2_KAP_FINAL_CHECK, EC_R_INVALID_SM2_KAP_CHECKSUM_VALUE);
return 0;
}
}
return 1;
}
int SM2_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
const EC_KEY *ec_key, void *(*KDF) (const void *in, size_t inlen, void *out, size_t *outlen))
{
return 0;
}

View File

@@ -181,19 +181,7 @@ end:
return NULL;
}
SM9PublicKey *SM9_extract_sign_public_key(SM9PublicParameters *mpk,
const char *id, size_t idlen)
{
return NULL;
}
SM9PublicKey *SM9_extract_exch_public_key(SM9PublicParameters *mpk,
const char *id, size_t idlen)
{
return NULL;
}
SM9PublicKey *SM9_extract_enc_public_key(SM9PublicParameters *mpk,
SM9PublicKey *SM9_extract_public_key(SM9PublicParameters *mpk,
const char *id, size_t idlen)
{
return NULL;

View File

@@ -352,6 +352,7 @@ static void x509v3_cache_extensions(X509 *x)
EXTENDED_KEY_USAGE *extusage;
X509_EXTENSION *ex;
const EVP_MD *md;
int i;
#ifndef OPENSSL_NO_SHA
md = EVP_sha1();
#elif !defined(OPENSSL_NO_SM3)
@@ -360,7 +361,6 @@ static void x509v3_cache_extensions(X509 *x)
return;
#endif
int i;
if (x->ex_flags & EXFLAG_SET)
return;
X509_digest(x, md, x->sha1_hash, NULL);