This commit is contained in:
Zhi Guan
2016-04-18 13:41:56 +02:00
parent 37ac6cd45a
commit da6bb109b4
27 changed files with 1494 additions and 255 deletions

82
crypto/sm2/sm2_locl.h Normal file
View File

@@ -0,0 +1,82 @@
/* crypto/sm2/sm2_locl.h */
/* ====================================================================
* 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.
* ====================================================================
*
*/
#ifndef HEADER_SM2_LOCL_H
#define HEADER_SM2_LOCL_H
#ifdef __cplusplus
extern "C" {
#endif
struct sm2sign_method {
const char *name;
SM2_SIG *(*sm2_do_sign)(const unsigned char *dgst, int dgstlen);
int (*sm2_sign_setup)(void);
int (*sm2_do_verify)(void);
int flag;
void *app_data;
};
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,7 +1,3 @@
/*
* cc -Wall -I ../../include/ sm2test.c ../../libcrypto.a
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
@@ -9,7 +5,7 @@
#include <openssl/evp.h>
#include <openssl/rand.h>
static int test_sm2_sign(void)
static void test_sm2_sign(void)
{
int rv;
EC_KEY *ec_key = NULL;
@@ -39,10 +35,9 @@ static int test_sm2_sign(void)
ECDSA_SIG_free(sig);
printf("%s() success\n", __FUNCTION__);
return 0;
}
static int test_sm2_enc(void)
static void test_sm2_enc(void)
{
int rv;
EC_KEY *ec_key = NULL;
@@ -85,10 +80,9 @@ static int test_sm2_enc(void)
printf("decrypted plaintext: %s\n", ptbuf);
*/
printf("%s() success\n", __FUNCTION__);
return 0;
}
static int test_sm2_kap(void)
static void test_sm2_kap(void)
{
int rv = 0;
@@ -137,7 +131,248 @@ static int test_sm2_kap(void)
rv = SM2_KAP_final_check(&ctx2);
OPENSSL_assert(rv == 1);
}
int main(int argc, char **argv)
{
int rv;
EC_KEY *ec_key = NULL;
EVP_PKEY *pkey = NULL;
const EVP_PKEY_METHOD *pmeth;
const EVP_PKEY_ASN1_METHOD *ameth;
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);
pkey = EVP_PKEY_new();
OPENSSL_assert(pkey);
pmeth = EVP_PKEY_meth_find(EVP_PKEY_SM2);
OPENSSL_assert(pmeth);
ameth = EVP_PKEY_asn1_find(NULL, EVP_PKEY_SM2);
OPENSSL_assert(ameth);
rv = EVP_PKEY_set1_SM2(pkey, ec_key);
OPENSSL_assert(rv == 1);
printf("pkey type : %d\n", EVP_PKEY_type(pkey->type));
printf("pkey id : %d\n", EVP_PKEY_id(pkey));
printf("pkey base id : %d\n", EVP_PKEY_base_id(pkey));
printf("pkey bits : %d\n", EVP_PKEY_bits(pkey));
rv = EVP_PKEY_print_public(bio, pkey, 0, NULL);
OPENSSL_assert(rv == 1);
rv = EVP_PKEY_print_private(bio, pkey, 0, NULL);
OPENSSL_assert(rv == 1);
rv = EVP_PKEY_print_params(bio, pkey, 0, NULL);
OPENSSL_assert(rv == 1);
printf("%s() success!\n", __FUNCTION__);
return 0;
}
int test_sm2_evp_digestsign(void)
{
int rv;
EC_KEY *ec_key = NULL;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *pk_ctx = NULL;
EVP_MD_CTX *md_ctx = NULL;
const *EVP_MD *md = EVP_sm3();
char *msg1 = "Hello ";
char *msg2 = "World!";
unsigned char sig[512];
size_t siglen = sizeof(sig);
/* init EVP_PKEY */
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);
pkey = EVP_PKEY_new();
OPENSSL_assert(pkey != NULL);
rv = EVP_PKEY_set1_SM2(pkey, ec_key);
OPENSSL_assert(rv == 1);
/* test EVP_DigestSignInit/Update/Final */
md_ctx = EVP_MD_CTX_create();
OPENSSL_assert(md_ctx != NULL);
rv = EVP_DigestSignInit(md_ctx, &pk_ctx, md, NULL, pkey);
if (rv != 1)
ERR_print_errors_fp(stderr);
OPENSSL_assert(rv == 1);
OPENSSL_assert(pkctx != NULL);
rv = EVP_DigestSignUpdate(md_ctx, msg1, strlen(msg1));
OPENSSL_assert(rv == 1);
rv = EVP_DigestSignUpdate(md_ctx, msg2, strlen(msg2));
OPENSSL_assert(rv == 1);
rv = EVP_DigestSignFinal(md_ctx, sig, &siglen);
OPENSSL_assert(rv == 1);
EC_KEY_free(ec_key);
EVP_PKEY_free(pkey);
EVP_PKEY_CTX_free(pk_ctx);
EVP_MD_CTX_destroy(md_ctx);
printf("%s() success!\n", __FUNCTION__);
return 0;
}
int sm2_test_evp_pkey_encrypt(void)
{
EC_KEY *ec_key = NULL;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
char *msg = "Hello world!";
unsigned char ptbuf[256];
unsigned char ctbuf[256];
size_t ptlen, ctlen, i;
/* Generate SM2 EVP_PKEY */
ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1);
pkey = EVP_PKEY_new();
EC_KEY_generate_key(ec_key);
EVP_PKEY_set1_SM2(pkey, ec_key);
ctx = EVP_PKEY_CTX_new(pkey, NULL);
/* Encrypt */
EVP_PKEY_encrypt_init(ctx);
ctlen = sizeof(ctbuf);
bzero(ctbuf, ctlen);
EVP_PKEY_encrypt(ctx, ctbuf, &ctlen, (unsigned char *)msg, strlen(msg) + 1);
printf("encrypted message (%zu bytes) : ", ctlen);
for (i = 0; i < ctlen; i++) {
printf("%02x", ctbuf[i]);
}
printf("\n");
/* Decrypt */
EVP_PKEY_decrypt_init(ctx);
ptlen = sizeof(ptbuf);
bzero(ptbuf, ptlen);
if (!EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen)) {
fprintf(stderr, "sm2 decrypt failed.\n");
}
printf("decrypted message : %s\n", ptbuf);
EVP_PKEY_free(pkey);
EC_KEY_free(ec_key);
EVP_PKEY_CTX_free(ctx);
return 0;
}
EVP_PKEY *pkey_new_ec()
{
int rv;
ECIES_PARAMS param;
EC_KEY *ec_key = NULL;
EVP_PKEY *pkey = NULL;
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);
param.mac_nid = NID_hmac_full_ecies;
param.kdf_md = EVP_sha1();
param.sym_cipher = EVP_aes_128_cbc();
param.mac_md = EVP_sha1();
rv = ECIES_set_parameters(ec_key, &param);
ERR_print_errors_fp(stderr);
OPENSSL_assert(rv == 1);
OPENSSL_assert(ECIES_get_parameters(ec_key) != NULL);
pkey = EVP_PKEY_new();
OPENSSL_assert(pkey);
const EVP_PKEY_ASN1_METHOD *ameth = EVP_PKEY_asn1_find(NULL, EVP_PKEY_SM2);
OPENSSL_assert(ameth);
rv = EVP_PKEY_set1_SM2(pkey, ec_key);
ERR_print_errors_fp(stderr);
OPENSSL_assert(rv == 1);
return pkey;
}
int test_sm2_pkey_seal(void)
{
int rv;
EVP_PKEY *pkey[2];
int num_pkeys = sizeof(pkey)/sizeof(pkey[0]);
EVP_CIPHER_CTX ctx;
const EVP_CIPHER *cipher = EVP_sms4_cbc();
unsigned char iv[16];
unsigned char ek[2][256];
int eklen[sizeof(pkey)/sizeof(pkey[0])];
char *msg1 = "Hello ";
char *msg2 = "World!";
unsigned char ctbuf[256];
unsigned char ptbuf[256];
unsigned char *p;
int len, ctlen;
int i;
for (i = 0; i < num_pkeys; i++) {
pkey[i] = pkey_new_ec();
}
EVP_CIPHER_CTX_init(&ctx);
RAND_bytes(iv, sizeof(iv));
/* EVP_SealInit/Update/Final() */
rv = EVP_SealInit(&ctx, cipher, ek, eklen, iv, pkey, num_pkeys);
OPENSSL_assert(rv == num_pkeys);
p = ctbuf;
rv = EVP_SealUpdate(&ctx, p, &len, (unsigned char *)msg1, strlen(msg1));
OPENSSL_assert(rv == 1);
p += len;
rv = EVP_SealUpdate(&ctx, p, &len, (unsigned char *)msg2, strlen(msg2));
OPENSSL_assert(rv == 1);
p += len;
rv = EVP_SealFinal(&ctx, p, &len);
OPENSSL_assert(rv == 1);
p += len;
ctlen = p - ctbuf;
/* EVP_OpenInit/Update/Final() */
// TODO
printf("%s() success!\n", __FUNCTION__);
return 0;
}
@@ -148,3 +383,4 @@ int main(int argc, char **argv)
return 0;
}