mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-07 12:33:39 +08:00
fast SMS4 with Intel AVX2/KNC-NI
This commit is contained in:
36
demos/ameth.c
Normal file
36
demos/ameth.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/asn1.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
int i;
|
||||
|
||||
/*
|
||||
int EVP_PKEY_asn1_get_count(void);
|
||||
const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx);
|
||||
const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type);
|
||||
const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
|
||||
const char *str, int len);
|
||||
|
||||
*/
|
||||
|
||||
int count = EVP_PKEY_asn1_get_count();
|
||||
printf("EVP_PKEY_asn1_get_count() = %d\n", count);
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
const EVP_PKEY_ASN1_METHOD *ameth;
|
||||
ameth = EVP_PKEY_asn1_get0(i);
|
||||
|
||||
int j;
|
||||
const unsigned char *p = (const unsigned char *)ameth;
|
||||
for (j = 0; j < 64; j++) {
|
||||
printf("%02x", p[j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
}
|
||||
8
demos/gmssl/ec_key.pem
Normal file
8
demos/gmssl/ec_key.pem
Normal file
@@ -0,0 +1,8 @@
|
||||
-----BEGIN EC PARAMETERS-----
|
||||
BggqhkjOPQMBAQ==
|
||||
-----END EC PARAMETERS-----
|
||||
-----BEGIN EC PRIVATE KEY-----
|
||||
MF8CAQEEGFKLikRIH3/XDuvS4Ih6SFr+bWVAnAAv1aAKBggqhkjOPQMBAaE0AzIA
|
||||
BP2x9MuZ06z72j1BMoUe6zYbmenKt3RgZZufuTMbAoz2XIeFwlmDk2pX6XS2+uiU
|
||||
gA==
|
||||
-----END EC PRIVATE KEY-----
|
||||
10
demos/gmssl/eckey.pem
Normal file
10
demos/gmssl/eckey.pem
Normal file
@@ -0,0 +1,10 @@
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIBYQIBADCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEA/////v//////
|
||||
//////////////8AAAAA//////////8wRAQg/////v////////////////////8A
|
||||
AAAA//////////wEICjp+p6dn140TVqeS89lCafzl4n1FauPkt28vUFNlA6TBEEE
|
||||
MsSuLB8ZgRlfmQRGajnJlI/jC7/yZgvhcVpFiTNMdMe8Nzai9PZ3nFm9zuNraSFT
|
||||
0KmHfMYqR0AC3zLlITnwoAIhAP////7///////////////9yA99rIcYFK1O79Ak5
|
||||
1UEjAgEBBG0wawIBAQQgPiMJOFBUJIqDZgYNyIei38Yknx9O9PpMAcmLGVx4PQqh
|
||||
RANCAARZKqeiImjJ27a/49Cquf0Zz8U0429NlCFxY6YmS1Lu9i9ApqUH7UfY7tb0
|
||||
9w8CpoqgJk4TjDz9ZQxNJPA2kZlq
|
||||
-----END PRIVATE KEY-----
|
||||
7
demos/gmssl/ecparam.pem
Normal file
7
demos/gmssl/ecparam.pem
Normal file
@@ -0,0 +1,7 @@
|
||||
-----BEGIN EC PARAMETERS-----
|
||||
MIHgAgEBMCwGByqGSM49AQECIQD////+/////////////////////wAAAAD/////
|
||||
/////zBEBCD////+/////////////////////wAAAAD//////////AQgKOn6np2f
|
||||
XjRNWp5Lz2UJp/OXifUVq4+S3by9QU2UDpMEQQQyxK4sHxmBGV+ZBEZqOcmUj+ML
|
||||
v/JmC+FxWkWJM0x0x7w3NqL09necWb3O42tpIVPQqYd8xipHQALfMuUhOfCgAiEA
|
||||
/////v///////////////3ID32shxgUrU7v0CTnVQSMCAQE=
|
||||
-----END EC PARAMETERS-----
|
||||
9
demos/gmssl/ecpubkey.pem
Normal file
9
demos/gmssl/ecpubkey.pem
Normal file
@@ -0,0 +1,9 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEA/////v//////////
|
||||
//////////8AAAAA//////////8wRAQg/////v////////////////////8AAAAA
|
||||
//////////wEICjp+p6dn140TVqeS89lCafzl4n1FauPkt28vUFNlA6TBEEEMsSu
|
||||
LB8ZgRlfmQRGajnJlI/jC7/yZgvhcVpFiTNMdMe8Nzai9PZ3nFm9zuNraSFT0KmH
|
||||
fMYqR0AC3zLlITnwoAIhAP////7///////////////9yA99rIcYFK1O79Ak51UEj
|
||||
AgEBA0IABFkqp6IiaMnbtr/j0Kq5/RnPxTTjb02UIXFjpiZLUu72L0CmpQftR9ju
|
||||
1vT3DwKmiqAmThOMPP1lDE0k8DaRmWo=
|
||||
-----END PUBLIC KEY-----
|
||||
11
demos/gmssl/gencert.sh
Executable file
11
demos/gmssl/gencert.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash -x
|
||||
|
||||
KEY_FILE=user.key
|
||||
REQ_FILE=user.req
|
||||
CERT_FILE=user.pem
|
||||
|
||||
gmssl ecparam -genkey -name sm2p256v1 -text -out $KEY_FILE
|
||||
gmssl req -new -key $KEY_FILE -out $REQ_FILE
|
||||
gmssl ca -out $CERT_FILE -outdir . -infiles $REQ_FILE
|
||||
gmssl pkcs12 -export -out user.pfx -in $CERT_FILE -inkey $KEY_FILE -certfile ./demoCA/cacert.pem
|
||||
|
||||
@@ -1,23 +1,27 @@
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
gmssl=/usr/local/bin/gmssl
|
||||
#gmssl=/usr/local/bin/gmssl
|
||||
|
||||
gmssl=../../apps/gmssl
|
||||
paramfile=ecparam.pem
|
||||
keyfile=eckey.pem
|
||||
pubkeyfile=ecpubkey.pem
|
||||
pkeyopt="-pkeyopt ec_paramgen_curve:sm2p256v1"
|
||||
|
||||
|
||||
echo -n abc | $gmssl dgst -sm3
|
||||
echo -n abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd | gmssl dgst -sm3
|
||||
#echo -n abc | $gmssl dgst -sm3
|
||||
#echo -n abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd | gmssl dgst -sm3
|
||||
|
||||
#$gmssl version
|
||||
#$gmssl ecparam -list_curves | grep sm2
|
||||
#$gmssl ecparam -text -noout -name sm2p256v1 -param_enc explicit
|
||||
$gmssl version
|
||||
$gmssl ecparam -list_curves | grep sm2
|
||||
$gmssl ecparam -text -noout -name sm2p256v1 -param_enc explicit
|
||||
|
||||
$gmssl genpkey -genparam -algorithm SM2 $ecpkeyopt -out $paramfile
|
||||
#$gmssl genpkey -algorithm EC $pkeyopt -out $keyfile
|
||||
#$gmssl pkey -text -noout -in $keyfile
|
||||
#$gmssl pkey -in $keyfile -pubout -out $pubkeyfile
|
||||
#$gmssl pkey -text -noout -pubin -in $pubkeyfile
|
||||
$gmssl genpkey -algorithm SM2 $ecpkeyopt -out $keyfile
|
||||
$gmssl pkey -text -noout -in $keyfile
|
||||
|
||||
$gmssl pkey -in $keyfile -pubout -out $pubkeyfile
|
||||
$gmssl pkey -text -noout -pubin -in $pubkeyfile
|
||||
|
||||
echo hello | $gmssl pkeyutl -sign -inkey $keyfile -hexdump
|
||||
|
||||
19
demos/gmssl/initca.sh
Executable file
19
demos/gmssl/initca.sh
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
DIR=demoCA
|
||||
|
||||
rm -fr $DIR
|
||||
mkdir $DIR
|
||||
mkdir $DIR/certs
|
||||
mkdir $DIR/crl
|
||||
mkdir $DIR/newcerts
|
||||
mkdir $DIR/private/
|
||||
touch $DIR/index.txt
|
||||
touch $DIR/crlnumber
|
||||
touch $DIR/private/.rand
|
||||
echo 01 > $DIR/serial
|
||||
|
||||
gmssl ecparam -genkey -name sm2p256v1 -text -out $DIR/private/cakey.pem
|
||||
gmssl req -new -x509 -days 3650 -key $DIR/private/cakey.pem -out $DIR/cacert.pem
|
||||
gmssl x509 -text -noout -in $DIR/cacert.pem
|
||||
|
||||
32
demos/gmssl/pem.c
Normal file
32
demos/gmssl/pem.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/ec.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
BIO *in = BIO_new_fp(stdin, BIO_NOCLOSE);
|
||||
EC_GROUP *group = NULL;
|
||||
EC_KEY *ec_key = NULL;
|
||||
ERR_load_crypto_strings();
|
||||
|
||||
|
||||
|
||||
group = PEM_read_bio_SM2PKParameters(in, NULL, NULL, NULL);
|
||||
if (!group) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!EC_GROUP_check(group, NULL)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
ec_key = EC_KEY_new();
|
||||
EC_KEY_set_group(ec_key, group);
|
||||
|
||||
return 0;
|
||||
}
|
||||
111
demos/gmssl/seal.c
Normal file
111
demos/gmssl/seal.c
Normal file
@@ -0,0 +1,111 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/ecies.h>
|
||||
#include <openssl/objects.h>
|
||||
|
||||
|
||||
|
||||
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, ¶m);
|
||||
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_pkey_enc(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() */
|
||||
|
||||
printf("%s() success!\n", __FUNCTION__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
test_pkey_enc();
|
||||
return 0;
|
||||
}
|
||||
|
||||
11
demos/gmssl/sm2-gencert.sh
Executable file
11
demos/gmssl/sm2-gencert.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash -x
|
||||
|
||||
KEY_FILE=user.key
|
||||
REQ_FILE=user.req
|
||||
CERT_FILE=user.pem
|
||||
|
||||
gmssl ecparam -genkey -name sm2p256v1 -text -out $KEY_FILE
|
||||
gmssl req -new -key $KEY_FILE -out $REQ_FILE
|
||||
gmssl ca -out $CERT_FILE -outdir . -infiles $REQ_FILE
|
||||
gmssl pkcs12 -export -out user.pfx -in $CERT_FILE -inkey $KEY_FILE -certfile ./demoCA/cacert.pem
|
||||
|
||||
19
demos/gmssl/sm2-initca.sh
Executable file
19
demos/gmssl/sm2-initca.sh
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
DIR=demoCA
|
||||
|
||||
rm -fr $DIR
|
||||
mkdir $DIR
|
||||
mkdir $DIR/certs
|
||||
mkdir $DIR/crl
|
||||
mkdir $DIR/newcerts
|
||||
mkdir $DIR/private/
|
||||
touch $DIR/index.txt
|
||||
touch $DIR/crlnumber
|
||||
touch $DIR/private/.rand
|
||||
echo 01 > $DIR/serial
|
||||
|
||||
gmssl ecparam -genkey -name sm2p256v1 -text -out $DIR/private/cakey.pem
|
||||
gmssl req -new -x509 -days 3650 -key $DIR/private/cakey.pem -out $DIR/cacert.pem
|
||||
gmssl x509 -text -noout -in $DIR/cacert.pem
|
||||
|
||||
@@ -59,14 +59,297 @@
|
||||
#include <openssl/engine.h>
|
||||
#include <openssl/sm2.h>
|
||||
|
||||
#define NUM_PKEYS 4
|
||||
|
||||
int main()
|
||||
{
|
||||
int ret = -1;
|
||||
int verbose = 0;
|
||||
BIO *out = NULL;
|
||||
|
||||
int id = EVP_PKEY_SM2;
|
||||
const EVP_MD *md = EVP_sm3();
|
||||
ENGINE *engine = NULL;
|
||||
EVP_PKEY_CTX *pkctx;
|
||||
|
||||
EVP_PKEY_CTX *pkctx = NULL;
|
||||
EVP_PKEY *pkey = NULL;
|
||||
EVP_MD_CTX *mdctx = NULL;
|
||||
EVP_CIPHER_CTX *cpctx = NULL;
|
||||
|
||||
unsigned char dgst[EVP_MAX_MD_SIZE] = "hello world";
|
||||
size_t dgstlen = 32;
|
||||
unsigned char sig[256];
|
||||
size_t siglen = sizeof(sig);
|
||||
|
||||
unsigned char msg[] = "hello world this is the message";
|
||||
size_t msglen = sizeof(msg);
|
||||
unsigned char cbuf[512];
|
||||
size_t cbuflen = sizeof(cbuf);
|
||||
unsigned char mbuf[512];
|
||||
size_t mbuflen = sizeof(mbuf);
|
||||
|
||||
int len;
|
||||
unsigned int ulen;
|
||||
|
||||
ERR_load_crypto_strings();
|
||||
|
||||
out = BIO_new_fp(stdout, BIO_NOCLOSE);
|
||||
|
||||
if (!(pkctx = EVP_PKEY_CTX_new_id(id, engine))) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_PKEY_keygen_init(pkctx)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_PKEY_keygen(pkctx, &pkey)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
EVP_PKEY_CTX_free(pkctx);
|
||||
|
||||
if (0) {
|
||||
EVP_PKEY_print_public(out, pkey, 4, NULL);
|
||||
BIO_printf(out, "\n");
|
||||
EVP_PKEY_print_private(out, pkey, 4, NULL);
|
||||
BIO_printf(out, "\n");
|
||||
}
|
||||
|
||||
if (!(pkctx = EVP_PKEY_CTX_new(pkey, engine))) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* EVP_PKEY_sign() */
|
||||
|
||||
if (!EVP_PKEY_sign_init(pkctx)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
bzero(sig, sizeof(sig));
|
||||
siglen = sizeof(sig);
|
||||
dgstlen = 32;
|
||||
|
||||
if (!EVP_PKEY_sign(pkctx, sig, &siglen, dgst, dgstlen)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
size_t i;
|
||||
printf("signature (%zu bytes) = ", siglen);
|
||||
for (i = 0; i < siglen; i++) {
|
||||
printf("%02X", sig[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (!EVP_PKEY_verify_init(pkctx)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (EVP_PKEY_verify(pkctx, sig, siglen, dgst, dgstlen) != SM2_VERIFY_SUCCESS) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
printf("signature verification success!\n");
|
||||
}
|
||||
|
||||
/* EVP_PKEY_encrypt() */
|
||||
|
||||
if (!EVP_PKEY_encrypt_init(pkctx)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
cbuflen = sizeof(cbuf);
|
||||
if (!EVP_PKEY_encrypt(pkctx, cbuf, &cbuflen, msg, msglen)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
size_t i;
|
||||
printf("ciphertext (%zu bytes) = ", cbuflen);
|
||||
for (i = 0; i < cbuflen; i++) {
|
||||
printf("%02X", cbuf[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (!EVP_PKEY_decrypt_init(pkctx)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
bzero(mbuf, sizeof(mbuf));
|
||||
mbuflen = sizeof(mbuf);
|
||||
if (!EVP_PKEY_decrypt(pkctx, mbuf, &mbuflen, cbuf, cbuflen)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
printf("original message = %s\n", msg);
|
||||
printf("decrypted message = %s\n", mbuf);
|
||||
}
|
||||
|
||||
|
||||
pkctx = EVP_PKEY_CTX_new_id(id, engine);
|
||||
/* EVP_PKEY_encrypt_old */
|
||||
|
||||
|
||||
if ((len = EVP_PKEY_encrypt_old(cbuf, msg, (int)msglen, pkey)) <= 0) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
int i;
|
||||
printf("ciphertext (%d bytes) = ", len);
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%02X", cbuf[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
bzero(mbuf, sizeof(mbuf));
|
||||
if ((len = EVP_PKEY_decrypt_old(mbuf, cbuf, len, pkey)) <= 0) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
printf("original message = %s\n", msg);
|
||||
printf("decrypted message = %s\n", mbuf);
|
||||
}
|
||||
|
||||
if (!(mdctx = EVP_MD_CTX_create())) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
/* EVP_SignInit_ex/Update/Final_ex */
|
||||
|
||||
if (!EVP_SignInit_ex(mdctx, EVP_sm3(), engine)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_SignUpdate(mdctx, msg, msglen)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_SignFinal(mdctx, sig, &ulen, pkey)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
siglen = ulen;
|
||||
|
||||
if (verbose) {
|
||||
size_t i;
|
||||
printf("signature (%zu bytes) = ", siglen);
|
||||
for (i = 0; i < siglen; i++) {
|
||||
printf("%02X", sig[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (!EVP_VerifyInit_ex(mdctx, EVP_sm3(), engine)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_VerifyUpdate(mdctx, msg, msglen)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (EVP_VerifyFinal(mdctx, sig, ulen, pkey) != SM2_VERIFY_SUCCESS) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
/* EVP_DigestSignInit/Update/Final() */
|
||||
// FIXME: return values might be different, not just 1 or 0
|
||||
if (!EVP_DigestSignInit(mdctx, &pkctx, md, engine, pkey)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_DigestSignUpdate(mdctx, msg, msglen)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
siglen = sizeof(sig);
|
||||
if (!EVP_DigestSignFinal(mdctx, sig, &siglen)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
pkctx = NULL;
|
||||
if (!EVP_DigestVerifyInit(mdctx, &pkctx, md, engine, pkey)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_DigestVerifyUpdate(mdctx, msg, msglen)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_DigestVerifyFinal(mdctx, sig, siglen)) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
goto end;
|
||||
}
|
||||
|
||||
|
||||
/* EVP_SealInit/Update/Final() EVP_OpenInit/Update/Final() */
|
||||
/*
|
||||
EVP_PKEY *pk[NUM_PKEYS] = {0};
|
||||
unsigned char iv[16];
|
||||
unsigned char ek[NUM_PKEYS][256];
|
||||
int eklen[NUM_PKEYS];
|
||||
|
||||
RAND_pseudo_bytes(iv, sizeof(iv));
|
||||
|
||||
int i;
|
||||
for (i = 0; i < NUM_PKEYS; i++) {
|
||||
}
|
||||
|
||||
if (!(cpctx = EVP_CIPHER_CTX_new())) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_SealInit(cpctx, cipher, ek, &ekl, iv, pubk, npubk)) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_SealUpdate(cpctx, msg, msglen)) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_SealFinal(cpctx, cbuf, (int *)&cbuflen)) {
|
||||
goto end;
|
||||
}
|
||||
*/
|
||||
|
||||
printf("test success!\n");
|
||||
ret = 1;
|
||||
end:
|
||||
ERR_print_errors_fp(stderr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,111 +0,0 @@
|
||||
/* demo/gmssl/sms4enc.c */
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2016 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 <stdlib.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/rand.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int ret = -1;
|
||||
FILE *fp = stdin;
|
||||
unsigned char key[32];
|
||||
unsigned char buf[1024];
|
||||
int len;
|
||||
const EVP_MD *md;
|
||||
HMAC_CTX hmctx;
|
||||
unsigned char mac[EVP_MAX_MD_SIZE];
|
||||
unsigned int maclen, i;
|
||||
|
||||
if (argc == 2) {
|
||||
if (!(fp = fopen(argv[1], "r"))) {
|
||||
fprintf(stderr, "open file %s failed\n", argv[1]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
HMAC_CTX_init(&hmctx);
|
||||
|
||||
RAND_bytes(key, sizeof(key));
|
||||
|
||||
OpenSSL_add_all_digests();
|
||||
if (!(md = EVP_get_digestbyname("sm3"))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EVP_DigestSignInit()) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
while ((len = fread(buf, 1, sizeof(buf), fp))) {
|
||||
EVP_DigestSignUpdate(&hmctx, buf, len);
|
||||
}
|
||||
|
||||
if (!EVP_DigestSignFinal()) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
for (i = 0; i < maclen; i++) {
|
||||
printf("%02x", mac[i]);
|
||||
}
|
||||
printf("\n");
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
fclose(fp);
|
||||
EVP_cleanup();
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user