This commit is contained in:
Zhi Guan
2015-11-02 11:15:27 +08:00
parent ade9fc1a2e
commit 37541150e5
13 changed files with 790 additions and 30 deletions

View File

@@ -499,6 +499,9 @@ static int pkey_ec_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen
{
EC_KEY *ec_key = ctx->pkey->pkey.ec;
ECIES_PARAMS *param = ECIES_get_parameters(ec_key);
fprintf(stderr, "%s %s %d\n", __FUNCTION__, __FILE__, __LINE__);
return ECIES_encrypt(out, outlen, param, in, inlen, ec_key);
}
@@ -507,6 +510,7 @@ static int pkey_ec_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen
{
EC_KEY *ec_key = ctx->pkey->pkey.ec;
ECIES_PARAMS *param = ECIES_get_parameters(ec_key);
fprintf(stderr, "%s %s %d\n", __FUNCTION__, __FILE__, __LINE__);
return ECIES_decrypt(out, outlen, param, in, inlen, ec_key);
}
#endif

Binary file not shown.

View File

@@ -62,17 +62,41 @@
static void *ecies_data_dup(void *data) {
return data;
ECIES_PARAMS *ret = NULL;
ECIES_PARAMS *param = (ECIES_PARAMS *)data;
OPENSSL_assert(data);
if (!(ret = OPENSSL_malloc(sizeof(ECIES_PARAMS)))) {
return NULL;
}
ret->kdf_md = param->kdf_md;
ret->sym_cipher = param->sym_cipher;
ret->mac_md = param->mac_md;
return ret;
}
static void ecies_data_free(void *data) {
OPENSSL_free(data);
return;
}
int ECIES_set_parameters(EC_KEY *ec_key, const ECIES_PARAMS *param)
{
if (!EC_KEY_insert_key_method_data(ec_key, param,
ECIES_PARAMS *data = NULL;
OPENSSL_assert(ec_key);
OPENSSL_assert(param);
data = ecies_data_dup(param);
if (!EC_KEY_insert_key_method_data(ec_key, data,
ecies_data_dup, ecies_data_free, ecies_data_free)) {
printf("EC_KEY_insert_key_method_data() error\n");
return 0;
}
return 1;

View File

@@ -89,7 +89,7 @@ static const EVP_PKEY_METHOD *standard_methods[] = {
#endif
#ifndef OPENSSL_NO_EC
&ec_pkey_meth,
&sm2_pkey_meth,
//&sm2_pkey_meth,
#endif
&hmac_pkey_meth,
&cmac_pkey_meth,

BIN
crypto/sm2/.sm2test.c.swp Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -103,11 +103,13 @@ int SM2_do_decrypt(const EVP_MD *kdf_md, const EVP_MD *mac_md,
const SM2_CIPHERTEXT_VALUE *cv, unsigned char *out, size_t *outlen,
EC_KEY *ec_key);
int SM2_encrypt(const EVP_MD *kdf_md, const EVP_MD *mac_md,
point_conversion_form_t point_form, unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen, EC_KEY *ec_key);
point_conversion_form_t point_form,
const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key);
int SM2_decrypt(const EVP_MD *kdf_md, const EVP_MD *mac_md,
point_conversion_form_t point_form, const unsigned char *in,
size_t inlen, unsigned char *out, size_t *outlen, EC_KEY *ec_key);
point_conversion_form_t point_form,
const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key);
#define SM2_signature_size(ec_key) ECDSA_size(ec_key)

View File

@@ -73,11 +73,15 @@ int SM2_CIPHERTEXT_VALUE_size(const EC_GROUP *ec_group,
if (!point || !bn_ctx) {
goto end;
}
#if 0
//FIXME: len will be 1 !!!
if (!(len = EC_POINT_point2oct(ec_group, point, point_form,
NULL, 0, bn_ctx))) {
goto end;
}
#endif
len = 1 + 2 * ((EC_GROUP_get_degree(ec_group) + 7)/8);
len += mlen + EVP_MD_size(mac_md);
ret = len;
@@ -144,33 +148,44 @@ SM2_CIPHERTEXT_VALUE *SM2_CIPHERTEXT_VALUE_decode(const EC_GROUP *ec_group,
{
int ok = 0;
SM2_CIPHERTEXT_VALUE *ret = NULL;
BN_CTX *bn_ctx = NULL;
int len = SM2_CIPHERTEXT_VALUE_size(ec_group, point_form, 0, mac_md);
int ptlen = len - EVP_MD_size(mac_md);
BN_CTX *bn_ctx = BN_CTX_new();
int ptlen;
int fixlen;
if (!(len = SM2_CIPHERTEXT_VALUE_size(ec_group, point_form, 0, mac_md))) {
if (!bn_ctx) {
return NULL;
}
if (!(fixlen = SM2_CIPHERTEXT_VALUE_size(ec_group, point_form, 0, mac_md))) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
goto end;
}
if (buflen <= len) {
if (buflen <= fixlen) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
goto end;
}
if (!(ret = OPENSSL_malloc(sizeof(SM2_CIPHERTEXT_VALUE)))) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
goto end;
}
ret->ephem_point = EC_POINT_new(ec_group);
ret->ciphertext_size = buflen - len;
ret->ciphertext_size = buflen - fixlen;
ret->ciphertext = OPENSSL_malloc(ret->ciphertext_size);
if (!ret->ephem_point || !ret->ciphertext) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
goto end;
}
if (!(bn_ctx = BN_CTX_new())) {
goto end;
}
if (!EC_POINT_oct2point(ec_group, ret->ephem_point, buf, len, bn_ctx)) {
ptlen = fixlen - EVP_MD_size(mac_md);
if (!EC_POINT_oct2point(ec_group, ret->ephem_point, buf, ptlen, bn_ctx)) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
ERR_print_errors_fp(stdout);
goto end;
}
memcpy(ret->ciphertext, buf + ptlen, ret->ciphertext_size);
ret->mactag_size = EVP_MD_size(mac_md);
memcpy(ret->mactag, buf + buflen - ret->mactag_size, ret->mactag_size);
@@ -225,8 +240,9 @@ end:
}
int SM2_encrypt(const EVP_MD *kdf_md, const EVP_MD *mac_md,
point_conversion_form_t point_form, unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen, EC_KEY *ec_key)
point_conversion_form_t point_form,
const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key)
{
int ret = 0;
const EC_GROUP *ec_group = EC_KEY_get0_group(ec_key);
@@ -411,8 +427,9 @@ end:
}
int SM2_decrypt(const EVP_MD *kdf_md, const EVP_MD *mac_md,
point_conversion_form_t point_form, const unsigned char *in,
size_t inlen, unsigned char *out, size_t *outlen, EC_KEY *ec_key)
point_conversion_form_t point_form,
const unsigned char *in, size_t inlen,
unsigned char *out, size_t *outlen, EC_KEY *ec_key)
{
int ret = 0;
const EC_GROUP *ec_group = EC_KEY_get0_group(ec_key);
@@ -420,9 +437,11 @@ int SM2_decrypt(const EVP_MD *kdf_md, const EVP_MD *mac_md,
int len;
if (!(len = SM2_CIPHERTEXT_VALUE_size(ec_group, point_form, 0, mac_md))) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
goto end;
}
if (inlen <= len) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
goto end;
}
@@ -430,13 +449,16 @@ int SM2_decrypt(const EVP_MD *kdf_md, const EVP_MD *mac_md,
*outlen = inlen - len;
return 1;
} else if (*outlen < inlen - len) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
return 0;
}
if (!(cv = SM2_CIPHERTEXT_VALUE_decode(ec_group, point_form, mac_md, in, inlen))) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
goto end;
}
if (!SM2_do_decrypt(kdf_md, mac_md, cv, out, outlen, ec_key)) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
goto end;
}

View File

@@ -1,5 +1,10 @@
/*
* cc -Wall -I ../../include/ sm2test.c ../../libcrypto.a
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <openssl/sm2.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
@@ -45,30 +50,45 @@ static int test_sm2_enc(void)
SM2_CIPHERTEXT_VALUE *cv = NULL;
unsigned char ctbuf[512];
unsigned char ptbuf[512];
size_t len;
size_t len, len2;
BIO *bio = BIO_new_fp(stdout, BIO_NOCLOSE);
ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1);
OPENSSL_assert(ec_key);
rv = EC_KEY_generate_key(ec_key);
OPENSSL_assert(rv == 1);
cv = SM2_do_encrypt(EVP_sm3(), EVP_sm3(), msg, (size_t)strlen(msg), ec_key);
cv = SM2_do_encrypt(EVP_sm3(), EVP_sm3(), (unsigned char *)msg, (size_t)strlen(msg), ec_key);
OPENSSL_assert(cv);
SM2_CIPHERTEXT_VALUE_print(bio, EC_KEY_get0_group(ec_key), cv, 0, 0);
{
BIO *bio = BIO_new_fp(stdout, BIO_NOCLOSE);
SM2_CIPHERTEXT_VALUE_print(bio, EC_KEY_get0_group(ec_key), cv, 0, 0);
}
bzero(ptbuf, sizeof(ptbuf));
len = sizeof(ptbuf);
rv = SM2_do_decrypt(EVP_sm3(), EVP_sm3(), cv, ptbuf, &len, ec_key);
OPENSSL_assert(rv == 1);
len = sizeof(ctbuf);
rv = SM2_encrypt(EVP_sm3(), EVP_sm3(),
SM2_DEFAULT_POINT_CONVERSION_FORM,
(unsigned char *)msg, (size_t)strlen(msg), ctbuf, &len, ec_key);
OPENSSL_assert(rv == 1);
bzero(ptbuf, sizeof(ptbuf));
len2 = sizeof(ptbuf);
rv = SM2_decrypt(EVP_sm3(), EVP_sm3(),
SM2_DEFAULT_POINT_CONVERSION_FORM,
ctbuf, len, ptbuf, &len2, ec_key);
OPENSSL_assert(rv == 1);
/*
printf("original plaintext: %s\n", msg);
printf("decrypted plaintext: %s\n", ptbuf);
*/
printf("%s() success\n", __FUNCTION__);
return 0;
}
int main(int argc, char **argv)
{
test_sm2_sign();