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

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();