Update some demos

This commit is contained in:
Zhi Guan
2018-11-01 11:21:14 +08:00
parent 676076278d
commit d58931925b
18 changed files with 492 additions and 30 deletions

View File

@@ -1,15 +1,25 @@
all:
gcc keygen.c -L /usr/local/lib -lcrypto -o keygen
gcc keygen.c -DENCRYPT_KEY -DNO_PROMPT -L /usr/local/lib -lcrypto -o keygen-enc
gcc keygen.c -DENCRYPT_KEY -L /usr/local/lib -lcrypto -o keygen-enc-prompt
gcc sm2-keygen.c -L /usr/local/lib -lcrypto -o sm2-keygen
gcc sm2-keygen.c -DENCRYPT_KEY -DNO_PROMPT -L /usr/local/lib -lcrypto -o sm2-keygen-enc
gcc sm2-keygen.c -DENCRYPT_KEY -L /usr/local/lib -lcrypto -o sm2-keygen-enc-prompt
gcc sm2-sign.c -L /usr/local/lib -lcrypto -o sm2-sign
gcc sm2-encrypt.c -L /usr/local/lib -lcrypto -o sm2-encrypt
test:
./keygen
./keygen-enc
./keygen-enc-prompt
./sm2-keygen
echo
./sm2-keygen-enc
echo
#./sm2-keygen-enc-prompt
./sm2-sign
echo
./sm2-encrypt
echo
clean:
rm -fr a.out
rm -fr keygen
rm -fr keygen-enc
rm -fr keygen-enc-prompt
rm -fr sm2-keygen
rm -fr sm2-keygen-enc
rm -fr sm2-keygen-enc-prompt
rm -fr sm2-sign
rm -fr sm2-encrypt

75
demos/sm2/sm2-encrypt.c Normal file
View File

@@ -0,0 +1,75 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <openssl/ec.h>
#include <openssl/sm2.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/objects.h>
#include <openssl/is_gmssl.h>
int main(int argc, char **argv)
{
int ret = -1;
EC_KEY *ec_key = NULL;
unsigned char key[64];
unsigned char cbuf[1024];
unsigned char pbuf[1024] = {0};
size_t clen = sizeof(cbuf);
size_t plen = sizeof(pbuf);
int i;
/* generate sm2 key pair */
if (!(ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1))
|| !EC_KEY_generate_key(ec_key)) {
ERR_print_errors_fp(stderr);
goto end;
}
/* generate to be encrypted symmetric key
* Notice: sm2 encrypt should only be used to encrypt short data
*/
if (!RAND_bytes(key, sizeof(key))) {
ERR_print_errors_fp(stderr);
goto end;
}
printf("M = ");
for (i = 0; i < sizeof(key); i++) {
printf("%02X", key[i]);
}
printf("\n");
/* sm2 encrypt, hash algorithm is required for KDF */
if (!SM2_encrypt(NID_sm3, key, sizeof(key), cbuf, &clen, ec_key)) {
ERR_print_errors_fp(stderr);
goto end;
}
printf("C = ");
for (i = 0; i < clen; i++) {
printf("%02X", cbuf[i]);
}
printf("\n");
/* sm2 decrypt */
if (!SM2_decrypt(NID_sm3, cbuf, clen, pbuf, &plen, ec_key)) {
ERR_print_errors_fp(stderr);
goto end;
}
printf("M' = ");
for (i = 0; i < plen; i++) {
printf("%02X", pbuf[i]);
}
printf("\n");
ret = 0;
end:
EC_KEY_free(ec_key);
return ret;
}

65
demos/sm2/sm2-sign.c Normal file
View File

@@ -0,0 +1,65 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <openssl/ec.h>
#include <openssl/sm2.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/is_gmssl.h>
int main(int argc, char **argv)
{
int ret = -1;
EC_KEY *ec_key = NULL;
char *id = "Alice";
unsigned char msg[] = "This is the message to be signed";
unsigned char dgst[EVP_MAX_MD_SIZE];
size_t dgstlen = sizeof(dgst);
unsigned char sig[256];
unsigned int siglen = sizeof(sig);
int i;
if (!(ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1))
|| !EC_KEY_generate_key(ec_key)) {
ERR_print_errors_fp(stderr);
goto end;
}
printf("M = %s\n", (char *)msg);
printf("ID = %s\n", id);
if (!SM2_compute_message_digest(EVP_sm3(), EVP_sm3(), msg, sizeof(msg),
id, strlen(id), dgst, &dgstlen, ec_key)) {
ERR_print_errors_fp(stderr);
goto end;
}
printf("H(Z||M) = ");
for (i = 0; i < dgstlen; i++) {
printf("%02X", dgst[i]);
}
printf("\n");
if (!SM2_sign(NID_undef, dgst, dgstlen, sig, &siglen, ec_key)) {
ERR_print_errors_fp(stderr);
goto end;
}
printf("Signature = ");
for (i = 0; i < siglen; i++) {
printf("%02X", sig[i]);
}
printf("\n");
if (1 != SM2_verify(NID_undef, dgst, dgstlen, sig, siglen, ec_key)) {
ERR_print_errors_fp(stderr);
goto end;
}
printf("Verification Success!\n");
ret = 0;
end:
EC_KEY_free(ec_key);
return ret;
}