This commit is contained in:
Zhi Guan
2017-05-15 17:23:26 +08:00
parent bba5011e7c
commit 79e2936664
24 changed files with 531 additions and 76 deletions

View File

@@ -1,3 +1,51 @@
/* ====================================================================
* Copyright (c) 2014 - 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.
* ====================================================================
*/
/*
* Copyright 2012-2014 Luke Dashjr
*
@@ -11,7 +59,7 @@
#include <string.h>
#include <sys/types.h>
#include "base58.h"
#include <openssl/base58.h>
static const int8_t b58digits_map[] = {
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
@@ -147,4 +195,3 @@ bool base58_encode(const void *data, size_t binsz, char *b58, size_t *b58sz)
return true;
}

2
crypto/base58/build.info Normal file
View File

@@ -0,0 +1,2 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=base58.c

View File

@@ -61,7 +61,7 @@ static int pkey_ec_init(EVP_PKEY_CTX *ctx)
dctx->sign_type = NID_secg_scheme;
dctx->exch_type = NID_secg_scheme;
dctx->enc_type = NID_secg_scheme;
dctx->enc_param = NID_undef;
dctx->enc_param = NID_sm3;
#endif
ctx->data = dctx;
return 1;

View File

@@ -1,3 +1,51 @@
/* ====================================================================
* Copyright (c) 2014 - 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.
* ====================================================================
*/
/**
Copyright © 2015 Odzhan
Copyright © 2008 Daniel Otte

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_exch.c sm2_kmeth.c
sm2_oct.c sm2_exch.c sm2_kmeth.c

View File

@@ -321,6 +321,7 @@ int SM2_do_decrypt(const EVP_MD *md, const SM2CiphertextValue *cv,
const BIGNUM *pri_key;
KDF_FUNC kdf;
EC_POINT *point = NULL;
EC_POINT *tmp_point = NULL;
BIGNUM *n = NULL;
BIGNUM *h = NULL;
BN_CTX *bn_ctx = NULL;
@@ -374,6 +375,7 @@ int SM2_do_decrypt(const EVP_MD *md, const SM2CiphertextValue *cv,
/* malloc */
point = EC_POINT_new(group);
tmp_point = EC_POINT_new(group);
n = BN_new();
h = BN_new();
bn_ctx = BN_CTX_new();
@@ -412,12 +414,12 @@ int SM2_do_decrypt(const EVP_MD *md, const SM2CiphertextValue *cv,
}
/* check [h]C1 != O */
if (!EC_POINT_mul(group, point, NULL, point, h, bn_ctx)) {
if (!EC_POINT_mul(group, tmp_point, NULL, point, h, bn_ctx)) {
SM2err(SM2_F_SM2_DO_DECRYPT, ERR_R_EC_LIB);
goto end;
}
if (EC_POINT_is_at_infinity(group, point)) {
if (EC_POINT_is_at_infinity(group, tmp_point)) {
SM2err(SM2_F_SM2_DO_DECRYPT, SM2_R_INVALID_CIPHERTEXT);
goto end;
}
@@ -462,6 +464,7 @@ int SM2_do_decrypt(const EVP_MD *md, const SM2CiphertextValue *cv,
ret = 1;
end:
EC_POINT_free(point);
EC_POINT_free(tmp_point);
BN_free(n);
BN_free(h);
BN_CTX_free(bn_ctx);

View File

@@ -69,6 +69,8 @@ int SM2_KAP_CTX_init(SM2_KAP_CTX *ctx,
}
memset(ctx, 0, sizeof(*ctx));
ctx->id_dgstlen = sizeof(ctx->id_dgst);
ctx->remote_id_dgstlen = sizeof(ctx->remote_id_dgst);
ctx->id_dgst_md = EVP_sm3();
ctx->kdf_md = EVP_sm3();

View File

@@ -99,7 +99,7 @@ struct sm2_kap_ctx_st {
int i2o_SM2CiphertextValue(const EC_GROUP *group, const SM2CiphertextValue *cv,
unsigned char **pout);
SM2CiphertextValue *o2i_SM2CiphertextValue(const EC_GROUP *group,
SM2CiphertextValue *o2i_SM2CiphertextValue(const EC_GROUP *group, const EVP_MD *md,
SM2CiphertextValue **cv, const unsigned char **pin, long len);

View File

@@ -66,6 +66,7 @@ int i2o_SM2CiphertextValue(const EC_GROUP *group, const SM2CiphertextValue *cv,
BN_CTX *bn_ctx = NULL;
unsigned char *buf;
unsigned char *p;
size_t siz;
if (!group || !cv || !pout) {
SM2err(SM2_F_I2O_SM2CIPHERTEXTVALUE,
@@ -76,7 +77,7 @@ int i2o_SM2CiphertextValue(const EC_GROUP *group, const SM2CiphertextValue *cv,
nbytes = (EC_GROUP_get_degree(group) + 7)/8;
if (!cv->xCoordinate || BN_num_bytes(cv->xCoordinate) > nbytes
|| !cv->yCoordinate || BN_num_bytes(cv->BN_num_bytes) > nbytes
|| !cv->yCoordinate || BN_num_bytes(cv->yCoordinate) > nbytes
|| ASN1_STRING_length(cv->hash) <= 0
|| ASN1_STRING_length(cv->hash) > EVP_MAX_MD_SIZE
|| ASN1_STRING_length(cv->ciphertext) <= 0) {
@@ -133,13 +134,13 @@ int i2o_SM2CiphertextValue(const EC_GROUP *group, const SM2CiphertextValue *cv,
memcpy(p, ASN1_STRING_get0_data(cv->ciphertext),
ASN1_STRING_length(cv->ciphertext));
p += ASN1_STRING_length(cv->ciphertext);
outlen += siz;
outlen += ASN1_STRING_length(cv->ciphertext);
/* encode hash */
memcpy(out, ASN1_STRING_get0_data(cv->hash),
memcpy(p, ASN1_STRING_get0_data(cv->hash),
ASN1_STRING_length(cv->hash));
p += ASN1_STRING_length(cv->hash);
outlen += siz;
outlen += ASN1_STRING_length(cv->hash);
/* output */
if (*pout) {
@@ -163,7 +164,9 @@ SM2CiphertextValue *o2i_SM2CiphertextValue(const EC_GROUP *group,
SM2CiphertextValue *ret = NULL;
SM2CiphertextValue *cv = NULL;
BN_CTX *bn_ctx = NULL;
unsigned char *p;
EC_POINT *point = NULL;
const unsigned char *p;
int nbytes;
if (!group || !pin) {
SM2err(SM2_F_O2I_SM2CIPHERTEXTVALUE,
@@ -190,7 +193,7 @@ SM2CiphertextValue *o2i_SM2CiphertextValue(const EC_GROUP *group,
}
if (!(point = EC_POINT_new(group))
|| !(bn_ctx = BN_CTX_new(bn_ctx))) {
|| !(bn_ctx = BN_CTX_new())) {
SM2err(SM2_F_O2I_SM2CIPHERTEXTVALUE,
ERR_R_MALLOC_FAILURE);
goto end;
@@ -276,36 +279,3 @@ end:
BN_CTX_free(bn_ctx);
return ret;
}
int SM2_encrypt(const EVP_MD *md, const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key)
{
int ret = 0;
SM2CiphertextValue *cv = NULL;
if (!(cv = SM2_do_encrypt(md, in, inlen, ec_key))) {
goto end;
}
if (!out) {
len = i2o_SM2CiphertextValue(cv, NULL);
*outlen = len;
return 1;
}
if (!(i2o_SM2CiphertextValue(cv, &out))) {
goto end;
}
return 0;
}
int SM2_decrypt(int type, const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key)
{
SM2CiphertextValue *cv = NULL;
}

View File

@@ -1,3 +1,52 @@
/* ====================================================================
* Copyright (c) 2014 - 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 <openssl/speck.h>
#define ROR(x, r) ((x >> r) | (x << ((sizeof(SPECK_TYPE) * 8) - r)))//循环右移