SKF Wrapper

This commit is contained in:
Zhi Guan
2016-05-29 00:22:33 +02:00
parent 0cf9126a7d
commit ee4384daeb
142 changed files with 9469 additions and 6750 deletions

View File

@@ -76,3 +76,90 @@ clean:
# DO NOT DELETE THIS LINE -- make depend depends on it.
sm2_asn1.o: ../../include/openssl/asn1.h ../../include/openssl/asn1t.h
sm2_asn1.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h
sm2_asn1.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
sm2_asn1.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h
sm2_asn1.o: ../../include/openssl/ecdsa.h ../../include/openssl/err.h
sm2_asn1.o: ../../include/openssl/evp.h ../../include/openssl/kdf.h
sm2_asn1.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
sm2_asn1.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
sm2_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
sm2_asn1.o: ../../include/openssl/pkcs7.h ../../include/openssl/safestack.h
sm2_asn1.o: ../../include/openssl/sha.h ../../include/openssl/sm2.h
sm2_asn1.o: ../../include/openssl/sm3.h ../../include/openssl/stack.h
sm2_asn1.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h
sm2_asn1.o: ../../include/openssl/x509_vfy.h sm2_asn1.c
sm2_enc.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
sm2_enc.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h
sm2_enc.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
sm2_enc.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h
sm2_enc.o: ../../include/openssl/ecdsa.h ../../include/openssl/err.h
sm2_enc.o: ../../include/openssl/evp.h ../../include/openssl/kdf.h
sm2_enc.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
sm2_enc.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
sm2_enc.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
sm2_enc.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h
sm2_enc.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
sm2_enc.o: ../../include/openssl/sm2.h ../../include/openssl/sm3.h
sm2_enc.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
sm2_enc.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h sm2.h
sm2_enc.o: sm2_enc.c
sm2_err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
sm2_err.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
sm2_err.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h
sm2_err.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h
sm2_err.o: ../../include/openssl/err.h ../../include/openssl/evp.h
sm2_err.o: ../../include/openssl/kdf.h ../../include/openssl/lhash.h
sm2_err.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
sm2_err.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
sm2_err.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h
sm2_err.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
sm2_err.o: ../../include/openssl/sm2.h ../../include/openssl/sm3.h
sm2_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
sm2_err.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h
sm2_err.o: sm2_err.c
sm2_kap.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
sm2_kap.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
sm2_kap.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h
sm2_kap.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h
sm2_kap.o: ../../include/openssl/err.h ../../include/openssl/evp.h
sm2_kap.o: ../../include/openssl/kdf.h ../../include/openssl/lhash.h
sm2_kap.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
sm2_kap.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
sm2_kap.o: ../../include/openssl/ossl_typ.h ../../include/openssl/pkcs7.h
sm2_kap.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
sm2_kap.o: ../../include/openssl/sm2.h ../../include/openssl/sm3.h
sm2_kap.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
sm2_kap.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h sm2.h
sm2_kap.o: sm2_kap.c
sm2_lib.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
sm2_lib.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h
sm2_lib.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
sm2_lib.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h
sm2_lib.o: ../../include/openssl/ecdsa.h ../../include/openssl/err.h
sm2_lib.o: ../../include/openssl/evp.h ../../include/openssl/kdf.h
sm2_lib.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
sm2_lib.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
sm2_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
sm2_lib.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h
sm2_lib.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
sm2_lib.o: ../../include/openssl/sm2.h ../../include/openssl/sm3.h
sm2_lib.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
sm2_lib.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h
sm2_lib.o: sm2_lib.c
sm2_sign.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
sm2_sign.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h
sm2_sign.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
sm2_sign.o: ../../include/openssl/ec.h ../../include/openssl/ecdh.h
sm2_sign.o: ../../include/openssl/ecdsa.h ../../include/openssl/err.h
sm2_sign.o: ../../include/openssl/evp.h ../../include/openssl/kdf.h
sm2_sign.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
sm2_sign.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
sm2_sign.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
sm2_sign.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h
sm2_sign.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
sm2_sign.o: ../../include/openssl/sm2.h ../../include/openssl/sm3.h
sm2_sign.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
sm2_sign.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h
sm2_sign.o: sm2_sign.c

View File

@@ -69,8 +69,8 @@ extern "C" {
#define SM2_MAX_ID_BITS 65535
#define SM2_MAX_ID_LENGTH (SM2_MAX_ID_BITS/8)
#define SM2_DEFAULT_ID_GMT09 "1234567812345678"
#define SM2_DEFAULT_ID_GMSSL "anonym@gmssl.org"
#define SM2_DEFAULT_ID_GMT09 "1234567812345678"
#define SM2_DEFAULT_ID_GMSSL "anonym@gmssl.org"
#define SM2_DEFAULT_ID SM2_DEFAULT_ID_GMSSL
#define SM2_DEFAULT_POINT_CONVERSION_FORM POINT_CONVERSION_UNCOMPRESSED
@@ -81,7 +81,6 @@ int SM2_compute_id_digest(const EVP_MD *md, unsigned char *dgst,
unsigned int *dgstlen, EC_KEY *ec_key);
typedef struct sm2_enc_params_st {
const EVP_MD *kdf_md;
const EVP_MD *mac_md;
@@ -92,6 +91,7 @@ typedef struct sm2_enc_params_st {
#define SM2_ENC_PARAMS_mactag_size(params) \
((params)->mactag_size<0 ? EVP_MD_size((params)->mac_md) : (params->mactag_size))
int SM2_ENC_PARAMS_init_with_recommended(SM2_ENC_PARAMS *params);
typedef struct sm2_ciphertext_value_st {
EC_POINT *ephem_point;
@@ -103,6 +103,8 @@ typedef struct sm2_ciphertext_value_st {
int SM2_CIPHERTEXT_VALUE_size(const EC_GROUP *ec_group,
const SM2_ENC_PARAMS *params, size_t mlen);
SM2_CIPHERTEXT_VALUE *SM2_CIPHERTEXT_VALUE_new(const EC_GROUP *group);
void SM2_CIPHERTEXT_VALUE_free(SM2_CIPHERTEXT_VALUE *cv);
int SM2_CIPHERTEXT_VALUE_encode(const SM2_CIPHERTEXT_VALUE *cv,
const EC_GROUP *ec_group, const SM2_ENC_PARAMS *params,
@@ -130,10 +132,12 @@ int SM2_encrypt_with_recommended(unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen, EC_KEY *ec_key);
int SM2_decrypt_with_recommended(unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen, EC_KEY *ec_key);
#if 0
int SM2_encrypt_elgamal(unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen, EC_KEY *ec_key);
int SM2_decrypt_elgamal(unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen, EC_KEY *ec_key);
#endif
int SM2_compute_message_digest(const EVP_MD *id_md, const EVP_MD *msg_md,
const void *msg, size_t msglen, unsigned char *dgst,
@@ -169,14 +173,14 @@ typedef struct sm2_kap_ctx_st {
const EVP_MD *checksum_md;
point_conversion_form_t point_form;
KDF_FUNC kdf;
int is_initiator;
int do_checksum;
EC_KEY *ec_key;
unsigned char id_dgst[EVP_MAX_MD_SIZE];
unsigned int id_dgstlen;
EC_KEY *remote_pubkey;
unsigned char remote_id_dgst[EVP_MAX_MD_SIZE];
unsigned int remote_id_dgstlen;
@@ -185,7 +189,7 @@ typedef struct sm2_kap_ctx_st {
BN_CTX *bn_ctx;
BIGNUM *order;
BIGNUM *two_pow_w;
BIGNUM *t;
EC_POINT *point;
unsigned char pt_buf[1 + (OPENSSL_ECC_MAX_FIELD_BITS+7)/4];
@@ -207,54 +211,60 @@ int SM2_KAP_final_check(SM2_KAP_CTX *ctx, const unsigned char *checksum,
void SM2_KAP_CTX_cleanup(SM2_KAP_CTX *ctx);
/* BEGIN ERROR CODES */
/*
* The following lines are auto generated by the script mkerr.pl. Any changes
* made after this point may be overwritten when the script is next run.
*/
void ERR_load_SM2_strings(void);
/* Function codes. */
#define SM2_F_SM2_SET_ID 100
#define SM2_F_SM2_GET_ID 101
#define SM2_F_SM2_COMPUTE_ID_DIGEST 102
#define SM2_F_SM2_CIPHERTEXT_VALUE_SIZE 103
#define SM2_F_SM2_CIPHERTEXT_VALUE_FREE 104
#define SM2_F_SM2_CIPHERTEXT_VALUE_ENCODE 105
#define SM2_F_SM2_CIPHERTEXT_VALUE_DECODE 106
#define SM2_F_SM2_CIPHERTEXT_VALUE_PRINT 107
#define SM2_F_SM2_DO_ENCRYPT 108
#define SM2_F_SM2_DO_DECRYPT 109
#define SM2_F_SM2_ENCRYPT 110
#define SM2_F_SM2_DECRYPT 111
#define SM2_F_SM2_SIGNATURE_SIZE 112
#define SM2_F_SM2_SIGN_SETUP 113
#define SM2_F_SM2_DO_SIGN_EX 114
#define SM2_F_SM2_DO_SIGN 115
#define SM2_F_SM2_DO_VERIFY 116
#define SM2_F_SM2_SIGN_EX 117
#define SM2_F_SM2_SIGN 118
#define SM2_F_SM2_VERIFY 119
#define SM2_F_SM2_KAP_CTX_INIT 120
#define SM2_F_SM2_KAP_CTX_CLEANUP 121
#define SM2_F_SM2_KAP_PREPARE 122
#define SM2_F_SM2_KAP_COMPUTE_KEY 123
#define SM2_F_SM2_KAP_FINAL_CHECK 124
/* Error codes for the SM2 functions. */
/* Function codes. */
# define SM2_F_SM2_CIPHERTEXT_VALUE_DECODE 100
# define SM2_F_SM2_CIPHERTEXT_VALUE_ENCODE 101
# define SM2_F_SM2_CIPHERTEXT_VALUE_FREE 102
# define SM2_F_SM2_CIPHERTEXT_VALUE_PRINT 103
# define SM2_F_SM2_CIPHERTEXT_VALUE_SIZE 104
# define SM2_F_SM2_COMPUTE_ID_DIGEST 105
# define SM2_F_SM2_DECRYPT 106
# define SM2_F_SM2_DO_DECRYPT 107
# define SM2_F_SM2_DO_ENCRYPT 108
# define SM2_F_SM2_DO_SIGN 109
# define SM2_F_SM2_DO_SIGN_EX 110
# define SM2_F_SM2_DO_VERIFY 111
# define SM2_F_SM2_ENCRYPT 112
# define SM2_F_SM2_GET_ID 113
# define SM2_F_SM2_KAP_COMPUTE_KEY 114
# define SM2_F_SM2_KAP_CTX_CLEANUP 115
# define SM2_F_SM2_KAP_CTX_INIT 116
# define SM2_F_SM2_KAP_FINAL_CHECK 117
# define SM2_F_SM2_KAP_PREPARE 118
# define SM2_F_SM2_SET_ID 119
# define SM2_F_SM2_SIGN 120
# define SM2_F_SM2_SIGNATURE_SIZE 121
# define SM2_F_SM2_SIGN_EX 122
# define SM2_F_SM2_SIGN_SETUP 123
# define SM2_F_SM2_VERIFY 124
/* Reason codes. */
#define SM2_R_BAD_DATA 100
#define SM2_R_UNKNOWN_CIPHER_TYPE 101
#define SM2_R_ENCRYPT_FAILED 102
#define SM2_R_DECRYPT_FAILED 103
#define SM2_R_UNKNOWN_MAC_TYPE 104
#define SM2_R_GEN_MAC_FAILED 105
#define SM2_R_VERIFY_MAC_FAILED 106
#define SM2_R_ECDH_FAILED 107
#define SM2_R_BUFFER_TOO_SMALL 108
#define SM2_R_SM2_KAP_NOT_INITED 109
#define SM2_R_RANDOM_NUMBER_GENERATION_FAILED 110
#define SM2_R_ERROR 111
# define SM2_R_BAD_DATA 100
# define SM2_R_BAD_SIGNATURE 101
# define SM2_R_BUFFER_TOO_SMALL 102
# define SM2_R_DECRYPT_FAILED 103
# define SM2_R_ECDH_FAILED 104
# define SM2_R_ENCRYPT_FAILED 105
# define SM2_R_ERROR 106
# define SM2_R_GEN_MAC_FAILED 107
# define SM2_R_MISSING_PARAMETERS 108
# define SM2_R_NEED_NEW_SETUP_VALUES 109
# define SM2_R_RANDOM_NUMBER_GENERATION_FAILED 110
# define SM2_R_SM2_KAP_NOT_INITED 111
# define SM2_R_UNKNOWN_CIPHER_TYPE 112
# define SM2_R_UNKNOWN_MAC_TYPE 113
# define SM2_R_VERIFY_MAC_FAILED 114
#ifdef __cplusplus
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -95,6 +95,11 @@ end:
return ret;
}
SM2_CIPHERTEXT_VALUE *SM2_CIPHERTEXT_VALUE_new(const EC_GROUP *group)
{
return NULL;
}
void SM2_CIPHERTEXT_VALUE_free(SM2_CIPHERTEXT_VALUE *cv)
{
if (cv->ephem_point) EC_POINT_free(cv->ephem_point);
@@ -631,7 +636,7 @@ int SM2_do_decrypt(const SM2_ENC_PARAMS *params,
goto end;
}
if (cv->mactag_size != mactag_size ||
memcmp(mac, cv->mactag, cv->mactag_size)) {
OPENSSL_memcmp(mac, cv->mactag, cv->mactag_size)) {
SM2err(SM2_F_SM2_DO_DECRYPT, SM2_R_ERROR);
goto end;
}

View File

@@ -1,6 +1,6 @@
/* crypto/sm2/sm2_err.c */
/* ====================================================================
* Copyright (c) 2015 The GmSSL Project. All rights reserved.
* Copyright (c) 1999-2016 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -16,27 +16,27 @@
*
* 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/)"
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.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.
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@OpenSSL.org.
*
* 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.
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL 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 product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL 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
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL 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;
@@ -47,62 +47,77 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
/*
* NOTE: this file was auto generated by the mkerr.pl script: any changes
* made to it will be overwritten when the script next updates this file,
* only reason strings will be preserved.
*/
#include <stdio.h>
#include <openssl/err.h>
#include "sm2.h"
#include <openssl/sm2.h>
/* BEGIN ERROR CODES */
#ifndef OPENSSL_NO_ERR
#define ERR_FUNC(func) ERR_PACK(ERR_LIB_ECIES,func,0)
#define ERR_REASON(reason) ERR_PACK(ERR_LIB_ECIES,0,reason)
# define ERR_FUNC(func) ERR_PACK(ERR_LIB_SM2,func,0)
# define ERR_REASON(reason) ERR_PACK(ERR_LIB_SM2,0,reason)
static ERR_STRING_DATA SM2_str_functs[] = {
{ERR_FUNC(SM2_F_SM2_SET_ID), "SM2_SET_ID"},
{ERR_FUNC(SM2_F_SM2_GET_ID), "SM2_F_SM2_GET_ID"},
{ERR_FUNC(SM2_F_SM2_COMPUTE_ID_DIGEST), "SM2_compute_id_digest"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_SIZE), "SM2_CIPHERTEXT_VALUE_size"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_FREE), "SM2_CIPHERTEXT_VALUE_free"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_ENCODE), "SM2_CIPHERTEXT_VALUE_encode"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_DECODE), "SM2_CIPHERTEXT_VALUE_decode"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_PRINT), "SM2_CIPHERTEXT_VALUE_print"},
{ERR_FUNC(SM2_F_SM2_DO_ENCRYPT), "SM2_do_encrypt"},
{ERR_FUNC(SM2_F_SM2_DO_DECRYPT), "SM2_do_decrypt"},
{ERR_FUNC(SM2_F_SM2_ENCRYPT), "SM2_encrypt"},
{ERR_FUNC(SM2_F_SM2_DECRYPT), "SM2_decrypt"},
{ERR_FUNC(SM2_F_SM2_SIGNATURE_SIZE), "SM2_signature_size"},
{ERR_FUNC(SM2_F_SM2_SIGN_SETUP), "SM2_sign_setup"},
{ERR_FUNC(SM2_F_SM2_DO_SIGN_EX), "SM2_do_sign_ex"},
{ERR_FUNC(SM2_F_SM2_DO_SIGN), "SM2_do_sign"},
{ERR_FUNC(SM2_F_SM2_DO_VERIFY), "SM2_do_verify"},
{ERR_FUNC(SM2_F_SM2_SIGN_EX), "SM2_sign_ex"},
{ERR_FUNC(SM2_F_SM2_SIGN), "SM2_sign"},
{ERR_FUNC(SM2_F_SM2_VERIFY), "SM2_verify"},
{ERR_FUNC(SM2_F_SM2_KAP_CTX_INIT), "SM2_KAP_CTX_init"},
{ERR_FUNC(SM2_F_SM2_KAP_CTX_CLEANUP), "SM2_KAP_CTX_cleanup"},
{ERR_FUNC(SM2_F_SM2_KAP_PREPARE), "SM2_KAP_prepare"},
{ERR_FUNC(SM2_F_SM2_KAP_COMPUTE_KEY), "SM2_KAP_compute_key"},
{ERR_FUNC(SM2_F_SM2_KAP_FINAL_CHECK), "SM2_KAP_final_check"},
{0,NULL}
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_DECODE),
"SM2_CIPHERTEXT_VALUE_decode"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_ENCODE),
"SM2_CIPHERTEXT_VALUE_encode"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_FREE), "SM2_CIPHERTEXT_VALUE_free"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_PRINT), "SM2_CIPHERTEXT_VALUE_print"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_SIZE), "SM2_CIPHERTEXT_VALUE_size"},
{ERR_FUNC(SM2_F_SM2_COMPUTE_ID_DIGEST), "SM2_compute_id_digest"},
{ERR_FUNC(SM2_F_SM2_DECRYPT), "SM2_decrypt"},
{ERR_FUNC(SM2_F_SM2_DO_DECRYPT), "SM2_do_decrypt"},
{ERR_FUNC(SM2_F_SM2_DO_ENCRYPT), "SM2_do_encrypt"},
{ERR_FUNC(SM2_F_SM2_DO_SIGN), "SM2_do_sign"},
{ERR_FUNC(SM2_F_SM2_DO_SIGN_EX), "SM2_do_sign_ex"},
{ERR_FUNC(SM2_F_SM2_DO_VERIFY), "SM2_do_verify"},
{ERR_FUNC(SM2_F_SM2_ENCRYPT), "SM2_encrypt"},
{ERR_FUNC(SM2_F_SM2_GET_ID), "SM2_GET_ID"},
{ERR_FUNC(SM2_F_SM2_KAP_COMPUTE_KEY), "SM2_KAP_compute_key"},
{ERR_FUNC(SM2_F_SM2_KAP_CTX_CLEANUP), "SM2_KAP_CTX_cleanup"},
{ERR_FUNC(SM2_F_SM2_KAP_CTX_INIT), "SM2_KAP_CTX_init"},
{ERR_FUNC(SM2_F_SM2_KAP_FINAL_CHECK), "SM2_KAP_final_check"},
{ERR_FUNC(SM2_F_SM2_KAP_PREPARE), "SM2_KAP_prepare"},
{ERR_FUNC(SM2_F_SM2_SET_ID), "SM2_set_id"},
{ERR_FUNC(SM2_F_SM2_SIGN), "SM2_sign"},
{ERR_FUNC(SM2_F_SM2_SIGNATURE_SIZE), "SM2_SIGNATURE_SIZE"},
{ERR_FUNC(SM2_F_SM2_SIGN_EX), "SM2_sign_ex"},
{ERR_FUNC(SM2_F_SM2_SIGN_SETUP), "SM2_sign_setup"},
{ERR_FUNC(SM2_F_SM2_VERIFY), "SM2_verify"},
{0, NULL}
};
static ERR_STRING_DATA SM2_str_reasons[] = {
{ERR_REASON(SM2_R_BAD_DATA), "bad data"},
{ERR_REASON(SM2_R_UNKNOWN_CIPHER_TYPE), "unknown cipher type"},
{ERR_REASON(SM2_R_ENCRYPT_FAILED), "encrypt failed"},
{ERR_REASON(SM2_R_DECRYPT_FAILED), "decrypt failed"},
{ERR_REASON(SM2_R_UNKNOWN_MAC_TYPE), "unknown MAC type"},
{ERR_REASON(SM2_R_GEN_MAC_FAILED), "MAC generation failed"},
{ERR_REASON(SM2_R_VERIFY_MAC_FAILED), "MAC verification failed"},
{ERR_REASON(SM2_R_ECDH_FAILED), "ECDH failed"},
{ERR_REASON(SM2_R_BUFFER_TOO_SMALL), "buffer too small"},
{ERR_REASON(SM2_R_SM2_KAP_NOT_INITED), "KAP not inited"},
{ERR_REASON(SM2_R_RANDOM_NUMBER_GENERATION_FAILED), "random number generation failed"},
{ERR_REASON(SM2_R_ERROR), "Error"},
{0,NULL}
{ERR_REASON(SM2_R_BAD_DATA), "bad data"},
{ERR_REASON(SM2_R_BAD_SIGNATURE), "bad signature"},
{ERR_REASON(SM2_R_BUFFER_TOO_SMALL), "buffer too small"},
{ERR_REASON(SM2_R_DECRYPT_FAILED), "decrypt failed"},
{ERR_REASON(SM2_R_ECDH_FAILED), "ecdh failed"},
{ERR_REASON(SM2_R_ENCRYPT_FAILED), "encrypt failed"},
{ERR_REASON(SM2_R_ERROR), "error"},
{ERR_REASON(SM2_R_GEN_MAC_FAILED), "gen mac failed"},
{ERR_REASON(SM2_R_MISSING_PARAMETERS), "missing parameters"},
{ERR_REASON(SM2_R_NEED_NEW_SETUP_VALUES), "need new setup values"},
{ERR_REASON(SM2_R_RANDOM_NUMBER_GENERATION_FAILED),
"random number generation failed"},
{ERR_REASON(SM2_R_SM2_KAP_NOT_INITED), "sm2 kap not inited"},
{ERR_REASON(SM2_R_UNKNOWN_CIPHER_TYPE), "unknown cipher type"},
{ERR_REASON(SM2_R_UNKNOWN_MAC_TYPE), "unknown mac type"},
{ERR_REASON(SM2_R_VERIFY_MAC_FAILED), "verify mac failed"},
{0, NULL}
};
#endif
@@ -111,9 +126,9 @@ void ERR_load_SM2_strings(void)
{
#ifndef OPENSSL_NO_ERR
if (ERR_func_error_string(SM2_str_functs[0].error) == NULL) {
ERR_load_strings(0,SM2_str_functs);
ERR_load_strings(0,SM2_str_reasons);
}
if (ERR_func_error_string(SM2_str_functs[0].error) == NULL) {
ERR_load_strings(0, SM2_str_functs);
ERR_load_strings(0, SM2_str_reasons);
}
#endif
}

119
crypto/sm2/sm2_err2.c Normal file
View File

@@ -0,0 +1,119 @@
/* crypto/sm2/sm2_err.c */
/* ====================================================================
* Copyright (c) 2015 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 <openssl/err.h>
#include "sm2.h"
#ifndef OPENSSL_NO_ERR
#define ERR_FUNC(func) ERR_PACK(ERR_LIB_ECIES,func,0)
#define ERR_REASON(reason) ERR_PACK(ERR_LIB_ECIES,0,reason)
static ERR_STRING_DATA SM2_str_functs[] = {
{ERR_FUNC(SM2_F_SM2_SET_ID), "SM2_SET_ID"},
{ERR_FUNC(SM2_F_SM2_GET_ID), "SM2_F_SM2_GET_ID"},
{ERR_FUNC(SM2_F_SM2_COMPUTE_ID_DIGEST), "SM2_compute_id_digest"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_SIZE), "SM2_CIPHERTEXT_VALUE_size"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_FREE), "SM2_CIPHERTEXT_VALUE_free"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_ENCODE), "SM2_CIPHERTEXT_VALUE_encode"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_DECODE), "SM2_CIPHERTEXT_VALUE_decode"},
{ERR_FUNC(SM2_F_SM2_CIPHERTEXT_VALUE_PRINT), "SM2_CIPHERTEXT_VALUE_print"},
{ERR_FUNC(SM2_F_SM2_DO_ENCRYPT), "SM2_do_encrypt"},
{ERR_FUNC(SM2_F_SM2_DO_DECRYPT), "SM2_do_decrypt"},
{ERR_FUNC(SM2_F_SM2_ENCRYPT), "SM2_encrypt"},
{ERR_FUNC(SM2_F_SM2_DECRYPT), "SM2_decrypt"},
{ERR_FUNC(SM2_F_SM2_SIGNATURE_SIZE), "SM2_signature_size"},
{ERR_FUNC(SM2_F_SM2_SIGN_SETUP), "SM2_sign_setup"},
{ERR_FUNC(SM2_F_SM2_DO_SIGN_EX), "SM2_do_sign_ex"},
{ERR_FUNC(SM2_F_SM2_DO_SIGN), "SM2_do_sign"},
{ERR_FUNC(SM2_F_SM2_DO_VERIFY), "SM2_do_verify"},
{ERR_FUNC(SM2_F_SM2_SIGN_EX), "SM2_sign_ex"},
{ERR_FUNC(SM2_F_SM2_SIGN), "SM2_sign"},
{ERR_FUNC(SM2_F_SM2_VERIFY), "SM2_verify"},
{ERR_FUNC(SM2_F_SM2_KAP_CTX_INIT), "SM2_KAP_CTX_init"},
{ERR_FUNC(SM2_F_SM2_KAP_CTX_CLEANUP), "SM2_KAP_CTX_cleanup"},
{ERR_FUNC(SM2_F_SM2_KAP_PREPARE), "SM2_KAP_prepare"},
{ERR_FUNC(SM2_F_SM2_KAP_COMPUTE_KEY), "SM2_KAP_compute_key"},
{ERR_FUNC(SM2_F_SM2_KAP_FINAL_CHECK), "SM2_KAP_final_check"},
{0,NULL}
};
static ERR_STRING_DATA SM2_str_reasons[] = {
{ERR_REASON(SM2_R_BAD_DATA), "bad data"},
{ERR_REASON(SM2_R_UNKNOWN_CIPHER_TYPE), "unknown cipher type"},
{ERR_REASON(SM2_R_ENCRYPT_FAILED), "encrypt failed"},
{ERR_REASON(SM2_R_DECRYPT_FAILED), "decrypt failed"},
{ERR_REASON(SM2_R_UNKNOWN_MAC_TYPE), "unknown MAC type"},
{ERR_REASON(SM2_R_GEN_MAC_FAILED), "MAC generation failed"},
{ERR_REASON(SM2_R_VERIFY_MAC_FAILED), "MAC verification failed"},
{ERR_REASON(SM2_R_ECDH_FAILED), "ECDH failed"},
{ERR_REASON(SM2_R_BUFFER_TOO_SMALL), "buffer too small"},
{ERR_REASON(SM2_R_SM2_KAP_NOT_INITED), "KAP not inited"},
{ERR_REASON(SM2_R_RANDOM_NUMBER_GENERATION_FAILED), "random number generation failed"},
{ERR_REASON(SM2_R_ERROR), "Error"},
{0,NULL}
};
#endif
void ERR_load_SM2_strings(void)
{
#ifndef OPENSSL_NO_ERR
if (ERR_func_error_string(SM2_str_functs[0].error) == NULL) {
ERR_load_strings(0,SM2_str_functs);
ERR_load_strings(0,SM2_str_reasons);
}
#endif
}

View File

@@ -202,33 +202,27 @@ int SM2_update_public_key(EC_KEY *ec_key, const EC_POINT *pub_key)
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
if (!EC_POINT_get_affine_coordinates_GFp(group, pub_key, x, NULL, bn_ctx)) {
SM2err(SM2_F_SM2_KAP_COMPUTE_KEY, ERR_R_EC_LIB);
goto end;
}
} else {
if (!EC_POINT_get_affine_coordinates_GF2m(group, pub_key, x, NULL, bn_ctx)) {
SM2err(SM2_F_SM2_KAP_COMPUTE_KEY, ERR_R_EC_LIB);
goto end;
}
}
if (!BN_nnmod(x, x, ctx->two_pow_w, bn_ctx)) {
SM2err(SM2_F_SM2_KAP_PREPARE, ERR_R_BN_LIB);
goto end;
}
if (!BN_add(x, x, ctx->two_pow_w)) {
SM2err(SM2_F_SM2_KAP_PREPARE, ERR_R_BN_LIB);
goto end;
}
if (!BN_mod_mul(ctx->t, x, r, ctx->order, ctx->bn_ctx)) {
SM2err(SM2_F_SM2_KAP_PREPARE, ERR_R_BN_LIB);
goto end;
}
if (!EC_POINT_mul(group, point, NULL, point, x, ctx->bn_ctx)) {
SM2err(SM2_F_SM2_KAP_COMPUTE_KEY, ERR_R_EC_LIB);
goto end;
}
@@ -648,12 +642,10 @@ int SM2_KAP_final_check(SM2_KAP_CTX *ctx, const unsigned char *checksum,
if (ctx->do_checksum) {
if (checksumlen != EVP_MD_size(ctx->checksum_md)) {
SM2err(SM2_F_SM2_KAP_FINAL_CHECK, 0);
return 0;
}
if (memcmp(ctx->checksum, checksum, checksumlen)) {
SM2err(SM2_F_SM2_KAP_COMPUTE_KEY, ERR_R_EC_LIB);
return 0;
}

View File

@@ -146,12 +146,10 @@ int sm2_get_public_key_data(unsigned char *buf, EC_KEY *ec_key)
if (EC_METHOD_get_field_type(EC_GROUP_method_of(ec_group)) == NID_X9_62_prime_field) {
if (!EC_GROUP_get_curve_GFp(ec_group, p, x, y, bn_ctx)) {
SM2err(SM2_F_SM2_KAP_PREPARE, ERR_R_EC_LIB);
goto err;
}
} else {
if (!EC_GROUP_get_curve_GF2m(ec_group, p, x, y, bn_ctx)) {
SM2err(SM2_F_SM2_KAP_PREPARE, ERR_R_EC_LIB);
goto err;
}
}

View File

@@ -58,7 +58,6 @@
#include <openssl/rand.h>
#include <openssl/sm2.h>
/* k in [1, n-1], (x, y) = kG */
static int sm2_sign_setup(EC_KEY *ec_key, BN_CTX *ctx_in, BIGNUM **kp, BIGNUM **xp)
{
@@ -71,13 +70,13 @@ static int sm2_sign_setup(EC_KEY *ec_key, BN_CTX *ctx_in, BIGNUM **kp, BIGNUM **
EC_POINT *point = NULL;
if (ec_key == NULL || (ec_group = EC_KEY_get0_group(ec_key)) == NULL) {
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
SM2err(SM2_F_SM2_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if (ctx_in == NULL) {
if ((ctx = BN_CTX_new()) == NULL) {
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_MALLOC_FAILURE);
SM2err(SM2_F_SM2_SIGN_SETUP,ERR_R_MALLOC_FAILURE);
return 0;
}
}
@@ -89,17 +88,17 @@ static int sm2_sign_setup(EC_KEY *ec_key, BN_CTX *ctx_in, BIGNUM **kp, BIGNUM **
x = BN_new();
order = BN_new();
if (!k || !x || !order) {
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
SM2err(SM2_F_SM2_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!EC_GROUP_get_order(ec_group, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
SM2err(SM2_F_SM2_SIGN_SETUP, ERR_R_EC_LIB);
goto err;
}
if ((point = EC_POINT_new(ec_group)) == NULL) {
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
SM2err(SM2_F_SM2_SIGN_SETUP, ERR_R_EC_LIB);
goto err;
}
@@ -107,8 +106,8 @@ static int sm2_sign_setup(EC_KEY *ec_key, BN_CTX *ctx_in, BIGNUM **kp, BIGNUM **
/* get random k */
do {
if (!BN_rand_range(k, order)) {
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
SM2err(SM2_F_SM2_SIGN_SETUP,
SM2_R_RANDOM_NUMBER_GENERATION_FAILED);
goto err;
}
@@ -116,25 +115,25 @@ static int sm2_sign_setup(EC_KEY *ec_key, BN_CTX *ctx_in, BIGNUM **kp, BIGNUM **
/* compute r the x-coordinate of generator * k */
if (!EC_POINT_mul(ec_group, point, k, NULL, NULL, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
SM2err(SM2_F_SM2_SIGN_SETUP, ERR_R_EC_LIB);
goto err;
}
if (EC_METHOD_get_field_type(EC_GROUP_method_of(ec_group)) == NID_X9_62_prime_field) {
if (!EC_POINT_get_affine_coordinates_GFp(ec_group, point, x, NULL, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
SM2err(SM2_F_SM2_SIGN_SETUP,ERR_R_EC_LIB);
goto err;
}
} else /* NID_X9_62_characteristic_two_field */ {
if (!EC_POINT_get_affine_coordinates_GF2m(ec_group, point, x, NULL, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
SM2err(SM2_F_SM2_SIGN_SETUP,ERR_R_EC_LIB);
goto err;
}
}
//FIXME: do we need this?
if (!BN_nnmod(x, x, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_SIGN_SETUP, ERR_R_BN_LIB);
goto err;
}
@@ -181,12 +180,12 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgst_len,
ec_group = EC_KEY_get0_group(ec_key);
priv_key = EC_KEY_get0_private_key(ec_key);
if (!ec_group || !priv_key) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
return NULL;
}
if (!(ret = ECDSA_SIG_new())) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_MALLOC_FAILURE);
return NULL;
}
@@ -195,11 +194,11 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgst_len,
e = BN_new();
bn = BN_new();
if (!ctx || !order || !e || !bn) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!EC_GROUP_get_order(ec_group, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_EC_LIB);
goto err;
}
@@ -211,13 +210,13 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgst_len,
}
#endif
if (!BN_bin2bn(dgst, dgst_len, e)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto err;
}
#if 0
if ((8 * dgst_len > i) && !BN_rshift(e, e, 8 - (i & 0x7))) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto err;
}
#endif
@@ -226,14 +225,14 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgst_len,
/* use or compute k and (kG).x */
if (!in_k || !in_x) {
if (!sm2_sign_setup(ec_key, ctx, &k, &ret->r)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,ERR_R_ECDSA_LIB);
SM2err(SM2_F_SM2_DO_SIGN,ERR_R_ECDSA_LIB);
goto err;
}
ck = k;
} else {
ck = in_k;
if (!BN_copy(ret->r, in_x)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_MALLOC_FAILURE);
goto err;
}
}
@@ -241,20 +240,20 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgst_len,
/* r = e + x (mod n) */
if (!BN_mod_add(ret->r, ret->r, e, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto err;
}
if (!BN_mod_add(bn, ret->r, ck, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto err;
}
/* check r != 0 && r + k != n */
if (BN_is_zero(ret->r) || BN_is_zero(bn)) {
if (in_k && in_x) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ECDSA_R_NEED_NEW_SETUP_VALUES);
SM2err(SM2_F_SM2_DO_SIGN, SM2_R_NEED_NEW_SETUP_VALUES);
goto err;
} else
continue;
@@ -262,35 +261,35 @@ static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgst_len,
/* s = ((1 + d)^-1 * (k - rd)) mod n */
if (!BN_one(bn)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto err;
}
if (!BN_mod_add(ret->s, priv_key, bn, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto err;
}
if (!BN_mod_inverse(ret->s, ret->s, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto err;
}
if (!BN_mod_mul(bn, ret->r, priv_key, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto err;
}
if (!BN_mod_sub(bn, ck, bn, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto err;
}
if (!BN_mod_mul(ret->s, ret->s, bn, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_SIGN, ERR_R_BN_LIB);
goto err;
}
/* check s != 0 */
if (BN_is_zero(ret->s)) {
if (in_k && in_x) {
ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ECDSA_R_NEED_NEW_SETUP_VALUES);
SM2err(SM2_F_SM2_DO_SIGN, SM2_R_NEED_NEW_SETUP_VALUES);
goto err;
}
} else
@@ -331,7 +330,7 @@ int sm2_do_verify(const unsigned char *dgst, int dgstlen,
!(ec_group = EC_KEY_get0_group(ec_key)) ||
!(pub_key = EC_KEY_get0_public_key(ec_key))) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
SM2err(SM2_F_SM2_DO_VERIFY, SM2_R_MISSING_PARAMETERS);
return -1;
}
@@ -341,11 +340,11 @@ int sm2_do_verify(const unsigned char *dgst, int dgstlen,
t = BN_new();
if (!ctx || !order || !e || !t) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!EC_GROUP_get_order(ec_group, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_EC_LIB);
goto err;
}
@@ -357,14 +356,14 @@ int sm2_do_verify(const unsigned char *dgst, int dgstlen,
BN_is_negative(sig->s) ||
BN_ucmp(sig->s, order) >= 0) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
SM2err(SM2_F_SM2_DO_VERIFY, SM2_R_BAD_SIGNATURE);
ret = 0;
goto err;
}
/* check t = r + s != 0 */
if (!BN_mod_add(t, sig->r, sig->s, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_BN_LIB);
goto err;
}
if (BN_is_zero(t)) {
@@ -380,44 +379,44 @@ int sm2_do_verify(const unsigned char *dgst, int dgstlen,
}
#endif
if (!BN_bin2bn(dgst, dgstlen, e)) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_BN_LIB);
goto err;
}
#if 0
if ((8 * dgstlen > i) && !BN_rshift(e, e, 8 - (i & 0x7))) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_BN_LIB);
goto err;
}
#endif
/* compute (x, y) = sG + tP, P is pub_key */
if (!(point = EC_POINT_new(ec_group))) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!EC_POINT_mul(ec_group, point, sig->s, pub_key, t, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_EC_LIB);
goto err;
}
if (EC_METHOD_get_field_type(EC_GROUP_method_of(ec_group)) == NID_X9_62_prime_field) {
if (!EC_POINT_get_affine_coordinates_GFp(ec_group, point, t, NULL, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_EC_LIB);
goto err;
}
} else /* NID_X9_62_characteristic_two_field */ {
if (!EC_POINT_get_affine_coordinates_GF2m(ec_group, point, t, NULL, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_EC_LIB);
goto err;
}
}
if (!BN_nnmod(t, t, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_BN_LIB);
goto err;
}
/* check (sG + tP).x + e == sig.r */
if (!BN_mod_add(t, t, e, order, ctx)) {
ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_DO_VERIFY, ERR_R_BN_LIB);
goto err;
}
if (BN_ucmp(t, sig->r) == 0) {
@@ -492,8 +491,6 @@ int SM2_verify(int type, const unsigned char *dgst, int dgstlen,
int derlen = -1;
int ret = -1;
fprintf(stderr, "%s %d %s() executed\n", __FILE__, __LINE__, __FUNCTION__);
if (!(s = ECDSA_SIG_new())) {
return ret;
}
@@ -516,4 +513,3 @@ err:
ECDSA_SIG_free(s);
return ret;
}

View File

@@ -48,14 +48,14 @@ int change_rand(const char *hex)
fake_rand.status = old_rand->status;
fake_rand.bytes = fbytes;
fake_rand.pseudorand = old_rand->bytes;
if (!RAND_set_rand_method(&fake_rand)) {
return 0;
}
}
rnd_number = hex;
return 1;
}
}
int restore_rand(void)
{
@@ -83,7 +83,7 @@ int hexequbin(const char *hex, const unsigned char *bin, size_t binlen)
sprintf(buf + i*2, "%02X", bin[i]);
}
buf[buflen - 1] = 0;
if (memcmp(hex, buf, binlen * 2) == 0) {
ret = 1;
}
@@ -114,7 +114,7 @@ EC_GROUP *new_ec_group(int is_prime_field,
goto err;
}
if (!BN_hex2bn(&p, p_hex) ||
if (!BN_hex2bn(&p, p_hex) ||
!BN_hex2bn(&a, a_hex) ||
!BN_hex2bn(&b, b_hex) ||
!BN_hex2bn(&x, x_hex) ||
@@ -150,7 +150,7 @@ EC_GROUP *new_ec_group(int is_prime_field,
goto err;
}
EC_GROUP_set_asn1_flag(group, flag);
EC_GROUP_set_asn1_flag(group, flag);
EC_GROUP_set_point_conversion_form(group, form);
ok = 1;
@@ -217,7 +217,7 @@ EC_KEY *new_ec_key(const EC_GROUP *group,
}
if (!EC_KEY_set_public_key_affine_coordinates(ec_key, x, y)) {
goto end;
}
}
ok = 1;
end:
@@ -229,7 +229,7 @@ end:
EC_KEY_free(ec_key);
ec_key = NULL;
}
return ec_key;
return ec_key;
}
int test_sm2_sign(const EC_GROUP *group,
@@ -293,7 +293,7 @@ int test_sm2_sign(const EC_GROUP *group,
fprintf(stderr, "error: %s %d\n", __FUNCTION__, __LINE__);
goto err;
}
/* verify */
if (!(pubkey = new_ec_key(group, NULL, xP, yP, id))) {
fprintf(stderr, "error: %s %d\n", __FUNCTION__, __LINE__);
@@ -337,8 +337,8 @@ int test_sm2_enc(const EC_GROUP *group,
}
buflen = sizeof(buf);
if (!SM2_encrypt_with_recommended((const unsigned char *)M, strlen(M),
buf, &buflen, ec_key)) {
if (!SM2_encrypt_with_recommended(buf, &buflen,
(const unsigned char *)M, strlen(M), ec_key)) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
@@ -350,12 +350,12 @@ int test_sm2_enc(const EC_GROUP *group,
goto end;
}
EC_KEY_free(ec_key);
if (!(ec_key = new_ec_key(group, d, xP, yP, NULL))) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
if (!SM2_decrypt_with_recommended(buf, buflen, msg, &msglen, ec_key)) {
if (!SM2_decrypt_with_recommended(msg, &msglen, buf, buflen, ec_key)) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
@@ -554,7 +554,7 @@ int test_sm2_test_vector()
"01686522130D590FB8DE635D8FCA715CC6BF3D05BEF3F75DA5D543454448166612",
"ALICE123@YAHOO.COM",
"26352AF82EC19F207BBC6F9474E11E90CE0F7DDACE03B27F801817E897A81FD5",
"message digest",
"message digest",
"AD673CBDA311417129A9EAA5F9AB1AA1633AD47718A84DFD46C17C6FA0AA3B12",
"36CD79FC8E24B7357A8A7B4A46D454C397703D6498158C605399B341ADA186D6",
"6D3FBA26EAB2A1054F5D198332E335817C8AC453ED26D3391CD4439D825BF25B",
@@ -574,7 +574,7 @@ int test_sm2_test_vector()
"4C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F",
"04"
"245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F6252E7"
"76CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84400F01B8"
"76CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84400F01B8"
"650053A89B41C418B0C3AAD00D886C00286467"
"9C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A285E07480653426D")) {
printf("sm2 enc p256 failed\n");
@@ -624,7 +624,7 @@ int test_sm2_test_vector()
printf("sm2 kap p256 passed\n");
}
#if 1
#if 0
/* ZA will not pass! */
if (!test_sm2_kap(
sm2b257test,
@@ -702,7 +702,7 @@ end:
EVP_PKEY_CTX_free(pkctx);
return ret;
}
int test_evp_pkey_sign(EVP_PKEY *pkey, int do_sm2, int verbose)
{
int ret = 0;
@@ -770,7 +770,7 @@ int test_evp_pkey_sign(EVP_PKEY *pkey, int do_sm2, int verbose)
ret = 1;
end:
EVP_PKEY_CTX_free(pkctx);
return ret;
return ret;
}
int test_evp_pkey_encrypt(EVP_PKEY *pkey, int do_sm2, int verbose)
@@ -791,7 +791,7 @@ int test_evp_pkey_encrypt(EVP_PKEY *pkey, int do_sm2, int verbose)
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
/* EVP_PKEY_encrypt() */
if (!EVP_PKEY_encrypt_init(pkctx)) {
@@ -802,7 +802,7 @@ int test_evp_pkey_encrypt(EVP_PKEY *pkey, int do_sm2, int verbose)
if (!EVP_PKEY_CTX_set_ec_enc_type(pkctx, type)) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
}
cbuflen = sizeof(cbuf);
if (!EVP_PKEY_encrypt(pkctx, cbuf, &cbuflen, msg, msglen)) {
@@ -827,7 +827,7 @@ int test_evp_pkey_encrypt(EVP_PKEY *pkey, int do_sm2, int verbose)
if (!EVP_PKEY_CTX_set_ec_enc_type(pkctx, type)) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
}
bzero(mbuf, sizeof(mbuf));
mbuflen = sizeof(mbuf);
@@ -1000,7 +1000,7 @@ int test_evp_digestsign(EVP_PKEY *pkey, int do_sm2, const EVP_MD *md, int verbos
goto end;
}
pkctx = NULL;
pkctx = NULL;
if (!EVP_DigestVerifyInit(mdctx, &pkctx, md, NULL, pkey)) {
ERR_print_errors_fp(stderr);
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
@@ -1063,14 +1063,15 @@ int test_evp_seal(int curve_id, const EVP_CIPHER *cipher, BIO *out, int verbose)
}
if (NUM_PKEYS != EVP_SealInit(cctx, cipher, ek, ekl, iv, pkey, NUM_PKEYS)) {
ERR_print_errors_fp(stderr);
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
if (verbose > 1) {
for (i = 0; i < NUM_PKEYS; i++) {
int j;
BIO_printf(out, "ek[i] (%d-byte) = ", ekl[i]);
BIO_printf(out, "ek[%d] (%d-byte) = ", i, ekl[i]);
for (j = 0; j < ekl[i]; j++) {
BIO_printf(out, "%02X", ek[i][j]);
}
@@ -1110,7 +1111,7 @@ int test_evp_seal(int curve_id, const EVP_CIPHER *cipher, BIO *out, int verbose)
BIO_printf(out, "\n");
}
if (!EVP_OpenInit(cctx, cipher, ek[0], ekl[0], iv, pkey[0])) {
if (!EVP_OpenInit(cctx, cipher, ek[1], ekl[1], iv, pkey[1])) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
@@ -1205,13 +1206,13 @@ int test_sm2_evp(int verbose)
if (!test_evp_sign(pkey, md, verbose)) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
}
/* test seal/open */
if (!test_evp_seal(curve_id, cipher, out, verbose)) {
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
goto end;
}
}
ret = 1;
@@ -1224,10 +1225,10 @@ end:
}
int main(int argc, char **argv)
{
{
int ret = -1;
BIO *out = NULL;
BIO *out = NULL;
out = BIO_new_fp(stdout, BIO_NOCLOSE);
/*
@@ -1238,20 +1239,20 @@ int main(int argc, char **argv)
} else {
CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
}
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
*/
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
*/
ERR_load_crypto_strings();
RAND_seed(rnd_seed, sizeof(rnd_seed));
if (!test_sm2_test_vector()) {
goto err;
}
if (!test_sm2_evp(2)) {
goto err;
}
}
ret =0;
err:
@@ -1261,7 +1262,7 @@ err:
if (ret)
ERR_print_errors(out);
//CRYPTO_cleanup_all_ex_data();
//ERR_remove_thread_state(NULL);
//ERR_free_strings();