Created
June 12, 2018 00:31
-
-
Save romen/c51662cf39421e3aef6f47b831bdcd06 to your computer and use it in GitHub Desktop.
What is the proper way to efficiently use digital signatures?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <openssl/dsa.h> | |
#include <openssl/engine.h> | |
#include <stdio.h> | |
#include <openssl/evp.h> | |
int N=3000; | |
int main() | |
{ | |
DSA *set=DSA_new(); | |
int a; | |
a=DSA_generate_parameters_ex(set,1024,NULL,1,NULL,NULL,NULL); | |
printf("%d\n",a); | |
a=DSA_generate_key(set); | |
printf("%d\n",a); | |
unsigned char msg[]="I am watching you!I am watching you!"; | |
unsigned char sign[256]; | |
unsigned int size; | |
for(int i=0;i<N;i++) { | |
size=sizeof(sign); /* reset to the size of the buffer */ | |
a=DSA_sign(1,msg,32,sign,&size,set); | |
printf("%d %d\n",a,size); | |
} | |
printf("DONE\n"); | |
DSA_free(set); /* free the memory allocated for the key */ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <openssl/pem.h> | |
#include <openssl/err.h> | |
#include <openssl/evp.h> | |
#define KEYFILE "private_key.pem" | |
#define N 3000 | |
#define BUFFSIZE 80 | |
EVP_PKEY *read_secret_key_from_file(const char * fname) | |
{ | |
EVP_PKEY *key = NULL; | |
FILE *fp = fopen(fname, "r"); | |
if(!fp) { | |
perror(fname); return NULL; | |
} | |
key = PEM_read_PrivateKey(fp, NULL, NULL, NULL); | |
fclose(fp); | |
return key; | |
} | |
int do_sign(EVP_PKEY *key, const unsigned char *msg, const size_t mlen, | |
unsigned char **sig, size_t *slen) | |
{ | |
EVP_MD_CTX *mdctx = NULL; | |
int ret = 0; | |
/* Create the Message Digest Context */ | |
if(!(mdctx = EVP_MD_CTX_create())) goto err; | |
/* Initialise the DigestSign operation - SHA-256 has been selected | |
* as the message digest function in this example */ | |
if(1 != EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), NULL, key)) | |
goto err; | |
/* Call update with the message */ | |
if(1 != EVP_DigestSignUpdate(mdctx, msg, mlen)) goto err; | |
/* Finalise the DigestSign operation */ | |
/* First call EVP_DigestSignFinal with a NULL sig parameter to | |
* obtain the length of the signature. Length is returned in slen */ | |
if(1 != EVP_DigestSignFinal(mdctx, NULL, slen)) goto err; | |
/* Allocate memory for the signature based on size in slen */ | |
if(!(*sig = OPENSSL_malloc(*slen))) goto err; | |
/* Obtain the signature */ | |
if(1 != EVP_DigestSignFinal(mdctx, *sig, slen)) goto err; | |
/* Success */ | |
ret = 1; | |
err: | |
if(ret != 1) | |
{ | |
/* Do some error handling */ | |
} | |
/* Clean up */ | |
if(*sig && !ret) OPENSSL_free(*sig); | |
if(mdctx) EVP_MD_CTX_destroy(mdctx); | |
return ret; | |
} | |
int main() | |
{ | |
int ret = EXIT_FAILURE; | |
const char *str = "I am watching you!I am watching you!"; | |
unsigned char *sig = NULL; | |
size_t slen = 0; | |
unsigned char msg[BUFFSIZE]; | |
size_t mlen = 0; | |
EVP_PKEY *key = read_secret_key_from_file(KEYFILE); | |
if(!key) goto err; | |
for(int i=0;i<N;i++) { | |
if ( snprintf((char *)msg, BUFFSIZE, "%s %d", str, i+1) < 0 ) | |
goto err; | |
mlen = strlen((const char*)msg); | |
if (!do_sign(key, msg, mlen, &sig, &slen)) goto err; | |
OPENSSL_free(sig); sig = NULL; | |
printf("\"%s\" -> siglen=%lu\n", msg, slen); | |
} | |
printf("DONE\n"); | |
ret = EXIT_SUCCESS; | |
err: | |
if (ret != EXIT_SUCCESS) { | |
ERR_print_errors_fp(stderr); | |
fprintf(stderr, "Something broke!\n"); | |
} | |
if (key) | |
EVP_PKEY_free(key); | |
exit(ret); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lscpu | |
Architecture: x86_64 | |
CPU op-mode(s): 32-bit, 64-bit | |
Byte Order: Little Endian | |
CPU(s): 4 | |
On-line CPU(s) list: 0-3 | |
Thread(s) per core: 1 | |
Core(s) per socket: 4 | |
Socket(s): 1 | |
NUMA node(s): 1 | |
Vendor ID: GenuineIntel | |
CPU family: 6 | |
Model: 94 | |
Model name: Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz | |
Stepping: 3 | |
CPU MHz: 3189.400 | |
BogoMIPS: 6384.00 | |
Virtualization: VT-x | |
L1d cache: 32K | |
L1i cache: 32K | |
L2 cache: 256K | |
L3 cache: 6144K | |
NUMA node0 CPU(s): 0-3 | |
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti retpoline intel_pt rsb_ctxsw spec_ctrl tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves dtherm arat pln pts hwp hwp_notify hwp_act_window hwp_epp | |
/opt/openssl-110h/bin/openssl version | |
OpenSSL 1.1.0h 27 Mar 2018 | |
Generating NIST P-256 private key | |
/opt/openssl-110h/bin/openssl ecparam -genkey -name prime256v1 -noout -out private_key.pem | |
cc op_example.c -O2 -g -I/opt/openssl-110h/include -L/opt/openssl-110h/lib -lcrypto -Wl,--enable-new-dtags,-rpath,/opt/openssl-110h/lib -o op_example | |
cc dsa_example.c -O2 -g -I/opt/openssl-110h/include -L/opt/openssl-110h/lib -lcrypto -Wl,--enable-new-dtags,-rpath,/opt/openssl-110h/lib -o dsa_example | |
cc evp_example.c -O2 -g -I/opt/openssl-110h/include -L/opt/openssl-110h/lib -lcrypto -Wl,--enable-new-dtags,-rpath,/opt/openssl-110h/lib -o evp_example | |
Running ./op_example | |
time ./op_example >/dev/null | |
0.32user 0.00system 0:00.32elapsed 100%CPU (0avgtext+0avgdata 3452maxresident)k | |
0inputs+0outputs (0major+153minor)pagefaults 0swaps | |
Running ./dsa_example | |
time ./dsa_example >/dev/null | |
0.42user 0.00system 0:00.42elapsed 100%CPU (0avgtext+0avgdata 3404maxresident)k | |
0inputs+0outputs (0major+153minor)pagefaults 0swaps | |
Running ./evp_example | |
time ./evp_example >/dev/null | |
0.12user 0.00system 0:00.12elapsed 99%CPU (0avgtext+0avgdata 3764maxresident)k | |
0inputs+0outputs (0major+157minor)pagefaults 0swaps | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KEYFILE=private_key.pem | |
ifeq ($(OPENSSL_PREFIX),) | |
OPENSSL_PREFIX := /usr | |
else | |
RPATH := -Wl,--enable-new-dtags,-rpath,$(OPENSSL_PREFIX)/lib | |
endif | |
CFLAGS := -O2 -g -I$(OPENSSL_PREFIX)/include | |
LDFLAGS := -L$(OPENSSL_PREFIX)/lib -lcrypto $(RPATH) | |
TARGETS=op_example dsa_example evp_example | |
all: $(TARGETS) | |
.PHONY: all | |
%: %.c | |
$(CC) $< $(CFLAGS) $(LDFLAGS) -o $@ | |
$(KEYFILE): | |
@echo | |
@echo Generating NIST P-256 private key | |
$(OPENSSL_PREFIX)/bin/openssl ecparam -genkey -name prime256v1 -noout -out $@ | |
@echo | |
.DEFAULT_GOAL := | |
benchmark: info $(KEYFILE) $(TARGETS) | |
@echo | |
@echo Running ./op_example | |
time ./op_example >/dev/null | |
@echo | |
@echo Running ./dsa_example | |
time ./dsa_example >/dev/null | |
@echo | |
@echo Running ./evp_example | |
time ./evp_example >/dev/null | |
@echo | |
.PHONY: benchmark | |
info: lscpu ossl_version | |
.PHONY: info | |
lscpu: | |
lscpu | |
.PHONY: lscpu | |
ossl_version: | |
@echo | |
$(OPENSSL_PREFIX)/bin/openssl version | |
@echo | |
.PHONY: ossl_version | |
clean: | |
@rm -rf $(TARGETS) $(KEYFILE) | |
.PHONY: clean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <openssl/dsa.h> | |
#include <openssl/engine.h> | |
#include <stdio.h> | |
#include <openssl/evp.h> | |
int N=3000; | |
int main() | |
{ | |
DSA *set=DSA_new(); | |
int a; | |
a=DSA_generate_parameters_ex(set,1024,NULL,1,NULL,NULL,NULL); | |
printf("%d\n",a); | |
a=DSA_generate_key(set); | |
printf("%d\n",a); | |
unsigned char msg[]="I am watching you!I am watching you!"; | |
unsigned char sign[256]; | |
unsigned int size; | |
for(int i=0;i<N;i++) | |
a=DSA_sign(1,msg,32,sign,&size,set); | |
printf("%d %d\n",a,size); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment