mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-07 21:53:41 +08:00
first step of v2 final release
This commit is contained in:
@@ -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
|
||||
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
|
||||
|
||||
@@ -66,3 +66,8 @@ ASN1_SEQUENCE(SM2CiphertextValue) = {
|
||||
} ASN1_SEQUENCE_END(SM2CiphertextValue)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(SM2CiphertextValue)
|
||||
IMPLEMENT_ASN1_DUP_FUNCTION(SM2CiphertextValue)
|
||||
|
||||
int SM2CiphertextValue_size(const EC_GROUP *group, int inlen)
|
||||
{
|
||||
return 1024;
|
||||
}
|
||||
|
||||
114
crypto/sm2/sm2_ctrl.c
Normal file
114
crypto/sm2/sm2_ctrl.c
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* 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
|
||||
@@ -246,9 +246,9 @@ int SM2_encrypt(int type, const unsigned char *in, size_t inlen,
|
||||
}
|
||||
|
||||
if (!out) {
|
||||
*outlen = i2d_SM2CiphertextValue(cv, NULL);
|
||||
*outlen = i2d_SM2CiphertextValue(cv, NULL) + 96;
|
||||
ret = 1;
|
||||
} else if (*outlen < i2d_SM2CiphertextValue(cv, NULL)) {
|
||||
} else if (*outlen < i2d_SM2CiphertextValue(cv, NULL) + 64) {
|
||||
SM2err(SM2_F_SM2_ENCRYPT, SM2_R_BUFFER_TOO_SMALL);
|
||||
ret = 0;
|
||||
} else {
|
||||
@@ -266,48 +266,54 @@ int SM2_decrypt(int type, const unsigned char *in, size_t inlen,
|
||||
unsigned char *out, size_t *outlen, EC_KEY *ec_key)
|
||||
{
|
||||
int ret = 0;
|
||||
SM2CiphertextValue *cv = NULL;
|
||||
const EVP_MD *md;
|
||||
const unsigned char *p;
|
||||
SM2CiphertextValue *cv = NULL;
|
||||
|
||||
/* check arguments */
|
||||
if (!(md = EVP_get_digestbynid(type))) {
|
||||
SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST_ALGOR);
|
||||
return 0;
|
||||
}
|
||||
if (!in) {
|
||||
SM2err(SM2_F_SM2_DECRYPT, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (inlen <= 0 || inlen > INT_MAX) {
|
||||
SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_INPUT_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!out) {
|
||||
*outlen = inlen;
|
||||
return 1;
|
||||
} else if (*outlen < inlen) {
|
||||
SM2err(SM2_F_SM2_DECRYPT, SM2_R_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(md = EVP_get_digestbynid(type))) {
|
||||
SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST_ALGOR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(cv = d2i_SM2CiphertextValue(NULL, &in, (long)inlen))) {
|
||||
/* decode asn.1 and check no data remaining */
|
||||
p = in;
|
||||
if (!(cv = d2i_SM2CiphertextValue(NULL, &p, (long)inlen))) {
|
||||
SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_CIPHERTEXT);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (inlen != i2d_SM2CiphertextValue(cv, NULL)) {
|
||||
if (p != in + inlen) {
|
||||
SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_CIPHERTEXT);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* return or check output length */
|
||||
if (!out) {
|
||||
*outlen = ASN1_STRING_length(cv->ciphertext);
|
||||
ret = 1;
|
||||
goto end;
|
||||
} else if (*outlen < ASN1_STRING_length(cv->ciphertext)) {
|
||||
SM2err(SM2_F_SM2_DECRYPT, SM2_R_BUFFER_TOO_SMALL);
|
||||
ret = 0;
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* do decrypt */
|
||||
if (!SM2_do_decrypt(md, cv, out, outlen, ec_key)) {
|
||||
SM2err(SM2_F_SM2_DECRYPT, SM2_R_DECRYPT_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
end:
|
||||
SM2CiphertextValue_free(cv);
|
||||
return ret;
|
||||
|
||||
@@ -45,7 +45,6 @@
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
@@ -577,10 +576,11 @@ int SM2_KAP_final_check(SM2_KAP_CTX *ctx, const unsigned char *checksum,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SM2_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
|
||||
const EC_KEY *ec_key, KDF_FUNC kdf_f)
|
||||
int SM2_compute_share_key(unsigned char *out, size_t *outlen,
|
||||
const EC_POINT *peer_ephem, EC_KEY *ephem,
|
||||
const EC_POINT *peer_pk, const unsigned char *peer_z, size_t peer_zlen,
|
||||
const unsigned char *z, size_t zlen, EC_KEY *sk, int initiator)
|
||||
{
|
||||
|
||||
|
||||
return 0;
|
||||
memset(out, 1, *outlen);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -50,8 +50,8 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <openssl/e_os2.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/sm2.h>
|
||||
|
||||
Reference in New Issue
Block a user