From 17a1ea2d824fbb1cecaf2ef9f7838641ec2a0c5f Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 7 Nov 2019 10:28:07 +0100 Subject: [PATCH 1/5] also build with MSVC 2019 [skip ci] --- appveyor.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 79c2ebd39..ca9e9e310 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -7,11 +7,13 @@ branches: - /^appveyor/ - /^build-ci/ image: +- Visual Studio 2019 - Visual Studio 2017 - Visual Studio 2015 build_script: - cmd: >- - if "Visual Studio 2017"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" + if "Visual Studio 2019"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" + if "Visual Studio 2017"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" if "Visual Studio 2015"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64 if "Visual Studio 2015"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64 cd.. From f3e1575f8d7a665fc5c0cbac673c8adab38004d2 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 7 Nov 2019 13:28:01 +0100 Subject: [PATCH 2/5] print PKA timing results as CSV --- demos/timing.c | 310 +++++++++++++++++++++++++++---------------------- 1 file changed, 171 insertions(+), 139 deletions(-) diff --git a/demos/timing.c b/demos/timing.c index 7dc430c88..974bd13f2 100644 --- a/demos/timing.c +++ b/demos/timing.c @@ -55,6 +55,26 @@ static void tally_results(int type) } } +#define CSV_SEP "," +#define OUTFILE stdout +static void print_csv(const char *alg, const char *op, unsigned long sz, ulong64 t) +{ + fprintf(OUTFILE, "%s" CSV_SEP "%s" CSV_SEP "%lu" CSV_SEP "%" PRI64 "u\n", alg, op, sz, t); + fflush(OUTFILE); +} +static void print_csv_dsa(const char *op, unsigned long sz1, unsigned long sz2, ulong64 t) +{ + fprintf(OUTFILE, "DSA" CSV_SEP "%s" CSV_SEP "%lu" CSV_SEP "%lu" CSV_SEP "%" PRI64 "u\n", op, sz1, sz2, t); + fflush(OUTFILE); +} +static void print_csv_header(const char *sz1, const char *sz2) +{ + fprintf(OUTFILE, "algo" CSV_SEP "operation" CSV_SEP "%s", sz1); + if (sz2) fprintf(OUTFILE, CSV_SEP "%s", sz2); + fprintf(OUTFILE, CSV_SEP "ticks\n"); + fflush(OUTFILE); +} + /* RDTSC from Scott Duplichan */ static ulong64 rdtsc (void) { @@ -640,51 +660,56 @@ static void time_prng(void) /* time various DSA operations */ static void time_dsa(void) { - dsa_key key; - ulong64 t1, t2; + dsa_key key; + ulong64 t1, t2; unsigned long x, y; - int err; -static const struct { - int group, modulus; -} groups[] = { -{ 20, 96 }, -{ 20, 128 }, -{ 24, 192 }, -{ 28, 256 }, + int err; + static const struct + { + int group, modulus; + } groups[] = + { + { 20, 96 }, + { 20, 128 }, + { 24, 192 }, + { 28, 256 }, #ifndef TFM_DESC -{ 32, 512 }, + { 32, 512 }, #endif -}; + }; if (ltc_mp.name == NULL) return; - for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) { - t2 = 0; - for (y = 0; y < 4; y++) { - t_start(); - t1 = t_read(); - if ((err = dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\ndsa_generate_pqg says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } - if ((err = dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) { - fprintf(stderr, "\n\ndsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } - t1 = t_read() - t1; - t2 += t1; + print_csv_header("group", "modulus"); + for (x = 0; x < (sizeof(groups) / sizeof(groups[0])); x++) { + t2 = 0; + for (y = 0; y < 4; y++) { + t_start(); + t1 = t_read(); + if ((err = dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) { + fprintf(stderr, "\n\ndsa_generate_pqg says %s, wait...no it should say %s...damn you!\n", + error_to_string(err), error_to_string(CRYPT_OK)); + exit(EXIT_FAILURE); + } + if ((err = dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) { + fprintf(stderr, "\n\ndsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), + error_to_string(CRYPT_OK)); + exit(EXIT_FAILURE); + } + t1 = t_read() - t1; + t2 += t1; #ifdef LTC_PROFILE - t2 <<= 2; - break; + t2 <<= 2; + break; #endif - if (y < 3) { - dsa_free(&key); - } - } - t2 >>= 2; - fprintf(stderr, "DSA-(%lu, %lu) make_key took %15"PRI64"u cycles\n", (unsigned long)groups[x].group*8, (unsigned long)groups[x].modulus*8, t2); - dsa_free(&key); + if (y < 3) { + dsa_free(&key); + } + } + t2 >>= 2; + print_csv_dsa("make_key", (unsigned long) groups[x].group * 8, (unsigned long) groups[x].modulus * 8, t2); + dsa_free(&key); } fprintf(stderr, "\n\n"); } @@ -697,123 +722,129 @@ static void time_dsa(void) { fprintf(stderr, "NO DSA\n"); } /* time various RSA operations */ static void time_rsa(void) { - rsa_key key; - ulong64 t1, t2; - unsigned char buf[2][2048] = { 0 }; + rsa_key key; + ulong64 t1, t2; + unsigned char buf[2][2048] = + { 0 }; unsigned long x, y, z, zzz; - int err, zz, stat; + int err, zz; if (ltc_mp.name == NULL) return; + print_csv_header("keysize", NULL); for (x = 2048; x <= 8192; x <<= 1) { - t2 = 0; - for (y = 0; y < 4; y++) { - t_start(); - t1 = t_read(); - if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\nrsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } - t1 = t_read() - t1; - t2 += t1; + + t2 = 0; + for (y = 0; y < 4; y++) { + t_start(); + t1 = t_read(); + if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x / 8, 65537, &key)) != CRYPT_OK) { + fprintf(stderr, "\n\nrsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), + error_to_string(CRYPT_OK)); + exit(EXIT_FAILURE); + } + t1 = t_read() - t1; + t2 += t1; #ifdef LTC_PROFILE - t2 <<= 2; - break; + t2 <<= 2; + break; #endif - if (y < 3) { - rsa_free(&key); - } - } - t2 >>= 2; - fprintf(stderr, "RSA-%lu make_key took %15"PRI64"u cycles\n", x, t2); + rsa_free(&key); + } + t2 >>= 2; + print_csv("RSA", "make_key", x, t2); - t2 = 0; - for (y = 0; y < 16; y++) { - t_start(); - t1 = t_read(); - z = sizeof(buf[1]); - if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char *)"testprog", 8, &yarrow_prng, - find_prng("yarrow"), find_hash("sha1"), - &key)) != CRYPT_OK) { - fprintf(stderr, "\n\nrsa_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } - t1 = t_read() - t1; - t2 += t1; + t2 = 0; + for (y = 0; y < 256; y++) { + t_start(); + t1 = t_read(); + z = sizeof(buf[1]); + if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char * )"testprog", 8, &yarrow_prng, + find_prng("yarrow"), find_hash("sha1"), &key)) + != CRYPT_OK) { + fprintf(stderr, "\n\nrsa_encrypt_key says %s, wait...no it should say %s...damn you!\n", + error_to_string(err), error_to_string(CRYPT_OK)); + exit(EXIT_FAILURE); + } + t1 = t_read() - t1; + t2 += t1; #ifdef LTC_PROFILE - t2 <<= 4; - break; + t2 <<= 4; + break; #endif - } - t2 >>= 4; - fprintf(stderr, "RSA-%lu encrypt_key took %15"PRI64"u cycles\n", x, t2); + } + t2 >>= 4; + print_csv("RSA", "encrypt_key", x, t2); - t2 = 0; - for (y = 0; y < 2048; y++) { - t_start(); - t1 = t_read(); - zzz = sizeof(buf[0]); - if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, (const unsigned char *)"testprog", 8, find_hash("sha1"), - &zz, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\nrsa_decrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } - t1 = t_read() - t1; - t2 += t1; + t2 = 0; + for (y = 0; y < 2048; y++) { + t_start(); + t1 = t_read(); + zzz = sizeof(buf[0]); + if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, (const unsigned char * )"testprog", 8, find_hash("sha1"), + &zz, &key)) + != CRYPT_OK) { + fprintf(stderr, "\n\nrsa_decrypt_key says %s, wait...no it should say %s...damn you!\n", + error_to_string(err), error_to_string(CRYPT_OK)); + exit(EXIT_FAILURE); + } + t1 = t_read() - t1; + t2 += t1; #ifdef LTC_PROFILE - t2 <<= 11; - break; + t2 <<= 11; + break; #endif - } - t2 >>= 11; - fprintf(stderr, "RSA-%lu decrypt_key took %15"PRI64"u cycles\n", x, t2); + } + t2 >>= 11; + print_csv("RSA", "decrypt_key", x, t2); - t2 = 0; - for (y = 0; y < 256; y++) { - t_start(); - t1 = t_read(); - z = sizeof(buf[1]); - if ((err = rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng, - find_prng("yarrow"), find_hash("sha1"), 8, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\nrsa_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } - t1 = t_read() - t1; - t2 += t1; + t2 = 0; + for (y = 0; y < 256; y++) { + t_start(); + t1 = t_read(); + z = sizeof(buf[1]); + if ((err = rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), 8, &key)) != CRYPT_OK) { + fprintf(stderr, "\n\nrsa_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), + error_to_string(CRYPT_OK)); + exit(EXIT_FAILURE); + } + t1 = t_read() - t1; + t2 += t1; #ifdef LTC_PROFILE - t2 <<= 8; - break; + t2 <<= 8; + break; #endif - } - t2 >>= 8; - fprintf(stderr, "RSA-%lu sign_hash took %15"PRI64"u cycles\n", x, t2); + } + t2 >>= 8; + print_csv("RSA", "sign_hash", x, t2); - t2 = 0; - for (y = 0; y < 2048; y++) { - t_start(); - t1 = t_read(); - if ((err = rsa_verify_hash(buf[1], z, buf[0], 20, find_hash("sha1"), 8, &stat, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\nrsa_verify_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } - if (stat == 0) { - fprintf(stderr, "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n", x, y); - exit(EXIT_FAILURE); - } - t1 = t_read() - t1; - t2 += t1; + t2 = 0; + for (y = 0; y < 2048; y++) { + int stat; + t_start(); + t1 = t_read(); + if ((err = rsa_verify_hash(buf[1], z, buf[0], 20, find_hash("sha1"), 8, &stat, &key)) != CRYPT_OK) { + fprintf(stderr, "\n\nrsa_verify_hash says %s, wait...no it should say %s...damn you!\n", + error_to_string(err), error_to_string(CRYPT_OK)); + exit(EXIT_FAILURE); + } + if (stat == 0) { + fprintf(stderr, "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n", x, y); + exit(EXIT_FAILURE); + } + t1 = t_read() - t1; + t2 += t1; #ifdef LTC_PROFILE - t2 <<= 11; - break; + t2 <<= 11; + break; #endif - } - t2 >>= 11; - fprintf(stderr, "RSA-%lu verify_hash took %15"PRI64"u cycles\n", x, t2); - fprintf(stderr, "\n\n"); - rsa_free(&key); - } + } + t2 >>= 11; + print_csv("RSA", "verify_hash", x, t2); + rsa_free(&key); + } } #else static void time_rsa(void) { fprintf(stderr, "NO RSA\n"); } @@ -836,6 +867,7 @@ static void time_dh(void) if (ltc_mp.name == NULL) return; + print_csv_header("keysize", NULL); for (x = sizes[i=0]; x < 100000; x = sizes[++i]) { t2 = 0; for (y = 0; y < 16; y++) { @@ -856,7 +888,7 @@ static void time_dh(void) dh_free(&key); } t2 >>= 4; - fprintf(stderr, "DH-%4lu make_key took %15"PRI64"u cycles\n", x*8, t2); + print_csv("DH", "make_key", x, t2); } } #else @@ -901,6 +933,7 @@ static void time_ecc(void) if (ltc_mp.name == NULL) return; + print_csv_header("keysize", NULL); for (x = sizes[i=0]; x < 100000; x = sizes[++i]) { t2 = 0; for (y = 0; y < 256; y++) { @@ -923,7 +956,7 @@ static void time_ecc(void) } } t2 >>= 8; - fprintf(stderr, "ECC-%lu make_key took %15"PRI64"u cycles\n", x*8, t2); + print_csv("ECC", "make_key", x*8, t2); t2 = 0; for (y = 0; y < 256; y++) { @@ -943,7 +976,7 @@ static void time_ecc(void) #endif } t2 >>= 8; - fprintf(stderr, "ECC-%lu encrypt_key took %15"PRI64"u cycles\n", x*8, t2); + print_csv("ECC", "encrypt_key", x*8, t2); t2 = 0; for (y = 0; y < 256; y++) { @@ -962,7 +995,7 @@ static void time_ecc(void) #endif } t2 >>= 8; - fprintf(stderr, "ECC-%lu decrypt_key took %15"PRI64"u cycles\n", x*8, t2); + print_csv("ECC", "decrypt_key", x*8, t2); t2 = 0; for (y = 0; y < 256; y++) { @@ -982,7 +1015,7 @@ static void time_ecc(void) #endif } t2 >>= 8; - fprintf(stderr, "ECC-%lu sign_hash took %15"PRI64"u cycles\n", x*8, t2); + print_csv("ECC", "sign_hash", x*8, t2); t2 = 0; for (y = 0; y < 256; y++) { @@ -1004,9 +1037,8 @@ static void time_ecc(void) #endif } t2 >>= 8; - fprintf(stderr, "ECC-%lu verify_hash took %15"PRI64"u cycles\n", x*8, t2); + print_csv("ECC", "verify_hash", x*8, t2); - fprintf(stderr, "\n\n"); ecc_free(&key); } } From f1712890d2042792d11c1b6f6d7aabc2cadfa84b Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 7 Nov 2019 16:21:05 +0100 Subject: [PATCH 3/5] generate reference-keys for timing --- .gitattributes | 1 + demos/keys/ECC-112.privkey | 2 + demos/keys/ECC-128.privkey | Bin 0 -> 66 bytes demos/keys/ECC-160.privkey | Bin 0 -> 77 bytes demos/keys/ECC-192.privkey | Bin 0 -> 88 bytes demos/keys/ECC-224.privkey | Bin 0 -> 101 bytes demos/keys/ECC-256.privkey | Bin 0 -> 113 bytes demos/keys/ECC-384.privkey | Bin 0 -> 161 bytes demos/keys/RSA-2048.privkey | Bin 0 -> 1190 bytes demos/keys/RSA-4096.privkey | Bin 0 -> 2348 bytes demos/keys/RSA-8192.privkey | Bin 0 -> 4653 bytes demos/timing.c | 176 +++++++++++++++++++++++++++++++----- 12 files changed, 154 insertions(+), 25 deletions(-) create mode 100644 demos/keys/ECC-112.privkey create mode 100644 demos/keys/ECC-128.privkey create mode 100644 demos/keys/ECC-160.privkey create mode 100644 demos/keys/ECC-192.privkey create mode 100644 demos/keys/ECC-224.privkey create mode 100644 demos/keys/ECC-256.privkey create mode 100644 demos/keys/ECC-384.privkey create mode 100644 demos/keys/RSA-2048.privkey create mode 100644 demos/keys/RSA-4096.privkey create mode 100644 demos/keys/RSA-8192.privkey diff --git a/.gitattributes b/.gitattributes index 5a182b9c3..3f86a65bb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5,3 +5,4 @@ /** export-subst /tests/test.key -text +*.privkey binary diff --git a/demos/keys/ECC-112.privkey b/demos/keys/ECC-112.privkey new file mode 100644 index 000000000..4c8e44e85 --- /dev/null +++ b/demos/keys/ECC-112.privkey @@ -0,0 +1,2 @@ +07€ ;4ÛâQ&åEQÜVÄÅjÅ3·fõ@ìñD« +«'Ôb‚0E=³@ÁG \ No newline at end of file diff --git a/demos/keys/ECC-128.privkey b/demos/keys/ECC-128.privkey new file mode 100644 index 0000000000000000000000000000000000000000..d5428c47594d5ef2e7a923f3bbe248eb082349b1 GIT binary patch literal 66 zcmXqTU}j=(U}6+t5@fh@Kr>nS;w6Eq|G#%#NbGnC6q@klv6XpQ=giUtg7Y|g&!z)~ VRt2nGFXnbmnO)TA@$&d-R{$p_8c6^E literal 0 HcmV?d00001 diff --git a/demos/keys/ECC-160.privkey b/demos/keys/ECC-160.privkey new file mode 100644 index 0000000000000000000000000000000000000000..d7374df2b3253e8419e538f955c3d3890476c609 GIT binary patch literal 77 zcmV-T0J8ruO9KK2fC2#&0u%zLPooT#%lx+)(^2|5?Ijsu^A-XX0HRZ%Dcsr1g^avS jqOW~m)0GYR@d6b9kveGV>AVjwu<0(<4us|#(9oy2fS4jS literal 0 HcmV?d00001 diff --git a/demos/keys/ECC-192.privkey b/demos/keys/ECC-192.privkey new file mode 100644 index 0000000000000000000000000000000000000000..0ff04c21777d884d3223978bec9b0814665e044b GIT binary patch literal 88 zcmV-e0H^;jRs#YDfC2#+0vI=-Y4|xYCc=_2zT_(mun=OYALsPe47>sv0LnT5)BH+< uFummV(w6oeHTfr^M8p{{P68M#9N2DO@n*4sS|qGtOv{8l2n1++7+!$Q!y`NZ literal 0 HcmV?d00001 diff --git a/demos/keys/ECC-224.privkey b/demos/keys/ECC-224.privkey new file mode 100644 index 0000000000000000000000000000000000000000..fea698de6d3b15cfbef32e997c8238f03e5807b9 GIT binary patch literal 101 zcmV-r0Gj_WV*>&QfC2#=0v!Ob1r}StNmlIy3Qk%VVyda}NX`0vZ%%K3x%FiN98;HL zC~*42^1*=+#m$QjaG7Gs$q$br6PitUBLW=&lS3q?J0%MAM+{bh>t@?yN5zh#ik0rU H_nv1Gfto4y literal 0 HcmV?d00001 diff --git a/demos/keys/ECC-256.privkey b/demos/keys/ECC-256.privkey new file mode 100644 index 0000000000000000000000000000000000000000..c84f5c9722db184adf5ed9545925b373033a099d GIT binary patch literal 113 zcmV-%0FM7KZvz4cfC2#^0wDm&en!SRjEJJXO(wd>gM$OX{2Wfb8f*)rZNQ_Cq{DFn zApnH6;Q@o|6B#FTl5?28I)Kbq%16dg=uAhZYCAlY?>GV=7AdN+sxsVnO!p6c&1_i+ TLX}WiF8_m literal 0 HcmV?d00001 diff --git a/demos/keys/ECC-384.privkey b/demos/keys/ECC-384.privkey new file mode 100644 index 0000000000000000000000000000000000000000..9053076ea727314241588c00823ccf75a9e8bd37 GIT binary patch literal 161 zcmV;S0ABwvft~{b2Y>O=QwxMVmi)?1Oz$Ea9s|9?^pC zr=rQHUC#6`JFFXojIRPPc$JVQx#I))Kw?7bBx5FMc&z>?DdX(3i}%Vf_9>-%|3fQv P(ZQy{y4QARByv4hfJ#fm literal 0 HcmV?d00001 diff --git a/demos/keys/RSA-2048.privkey b/demos/keys/RSA-2048.privkey new file mode 100644 index 0000000000000000000000000000000000000000..024d8e27d56d02459e3205e5f42de2a99f6875cd GIT binary patch literal 1190 zcmV;X1X=qqf&`)h0RRGm0RaHBb!_u0$ay|o$tmTSZ`xiO1GthXJ=tD-w2yu~-O*}2 zsI#%L3TnQlp2#zTWC`TS)+B@296I*~Jbg4@-F7wj6j{L3Or>nbcUTd-yc< z5YgX{02RNi)Ex`?F2moc6K>*uWbU#3;EEzX_SQ;?xu@f1Den}ONOMc2^QfD%IUl)! zvIsB};n?>sa=6}1VsWhk0|5X50)hbmHh%K5S1cg0VBpW8!mK#XIjts_@3VAvUVT7Q z@9fq>qHVwbwn+I1V>Pvk`p(TcWIF?n<=I_1JnmwqGfg~DCNogZ+CjvT%p^ITa{Wua z33>3zv%eH*19waQ03_)Q4iOlH2Q;FrWDS!`5NF9QEyo-9uk!A;h85&M$Vgrm$%DQ$ z*dy8&l!nj3^4&5Ay~#em(I(*U$-Ygku(_o=aLBI^iuG|3NWTsz3)9y-agU_g>=X&o`{L>Lzi3uw_r zMdu*2A>yr$KulL19a#2j-$B4NIqbn=?&5-?phIe(al&f}?J? zlNyBzN3g$}e|L!d*PS3>D=cdfOR&`+pN$jvIRAA-f{{sKw@c5yfB5Nmhk+1Z0)c@5 z*4xbZLbQ%-=ZbHT1c6o#$0IAH&L{WcxI}HFzBq#^dh6YV(}EH9Q0#2itI!r z^e^!qr$OBKH63H*7kf&!CO}0BA+Rz>g4V0-6A|8Vc<@zEU-hZ@5ZaDnG*Ut~b|d^H zhs&zD^BLtKv}cNfvysd6G+Rjxz)iJj0GFv}hkLMpw)v@e%-1h>5m8p$9N_V2pOR|= zfq)!j{pgKAIaX00EpavXHN8wZ?5V^ZV#z2c1zva$Ta^r+T?9#6mhv=vZZ9kf5f8Et z<^_kg)!Fb;TSDm0(C5A<)^nr}@3H2DJ48Qvfj-jWx+vuFisFKp@Aa0eFAY$+f}#+9 z-}K#(sF=;*=CTgeNRcbp@ZJ;AeB4Sn&D#QjfMMx)f=Wcpr1s(uw7^c{R2${@5=T70 zCJvq@ev@PTsqN19jr8@S82mO0!12^i4(B|M?l$~>{pvnyJGpQ=qZ-np`&Y4lu>>i$ zO$(Wf2F(Enm{F*(f{GAa+ar0(q|XOfK}FDxrZu~%okWOvFb=>xqP!s)^th>31`*i~ E1RDQMVE_OC literal 0 HcmV?d00001 diff --git a/demos/keys/RSA-4096.privkey b/demos/keys/RSA-4096.privkey new file mode 100644 index 0000000000000000000000000000000000000000..326e4ded9af1a6a5b52699ee511a579179f9144a GIT binary patch literal 2348 zcmV+{3Dfp4f(a-B0RRGm0s#QYVR9cR)_rABCFp zn$m=S3O}BJRcK#7$(nI)u5yFsM48!@qU;3XFpm<$FR>d#72iZr@AC!T=Afq)v% z@fT4nBNN=Yr|Pulk?_t(d?X9Dv_KbL{|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qz3( z#&vIFfzUzE&_YhaLx}={0RaHw2Mnl5%I3%|pY-5&1t&SEf-6VjQk@qW zdjNP<@RQaut&&RZTT0OLW{#3qIq$HQMd2ylmt33~GG39CsH`c)d&s_UT^EXfG@2`! zK#ZX&b=!5hnOkDO#0pBs^wb^n-(*qXG!5t<5a35wmc9@p_c>XoI4mF{GV2bNqC6O0 zLB8_S_!n|R!{PW<`=&^r0;*3A0e{iOWPV1~k-31rG2=CA5U8qhnWVygw!pWR@IXsy zsRcNw&3UEWzmgF`kwygtdUZ|pcXhd+#f;ZQVT%82%gha)sAL$b0BGZyqa@cQPkf!7Ig zyvd9=hH?$;*A;}hsVh@?X!LcV>u3|quucL*U>DYGj*{n&KR2okyrhcKY@~KH4DW{; zMb{KxFE%AU@g;nbJdQnFj|Bmp=uXH&nKm|C8M~UkwfR_H)$-tirTlX#sW=gA;BFUR zUj-Q27D#cqk3ky96mDSxf&l z75zSMpA)EeDnOVU#S2*0pyz!qxBptjs1zBAAH{g+LuxAiVo_wT969&WhOU8b_jH$K zDc&PgS0PYqc7h@9>RmzLJ2Whn?1kb4?MJQ!4~n9)BJ0r|De(-*hdO|?RDPqG S17??Non$>==^17UpoQu?G;?|Y literal 0 HcmV?d00001 diff --git a/demos/keys/RSA-8192.privkey b/demos/keys/RSA-8192.privkey new file mode 100644 index 0000000000000000000000000000000000000000..27982e10a580a4beecc9c325b012536baf8e6cb2 GIT binary patch literal 4653 zcmV+|64LE3f)XhL0RRGm1OWh}?yM!qG$eNhcBn#>JL_}<7uRjNr3RJYg3Ki#k#fEP zD)1ufTj_cE|6(F+44O$}fLJRMtJDvJU{7s|t#Aq~&tvs@(J>BU)eAx)EYqGd1iWDC zN;XX`WBx;3WO?I{Puf15_iadm5=bJW3C*n#pXf>jj)jC1|7yX?Mgh3rP$W2vR15Z} zb7e}&J*@|yvZ@9A_s55NL6wk%`zN(BdX|&EEQ!uSt)S+BBaOZH9GvSuyp&pqRUzLJ zgXdH~V)(EQ$a{|=0aF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 -4,10 +4,23 @@ #if defined(_WIN32) #define PRI64 "I64d" + #include + #ifndef PATH_MAX + #define PATH_MAX MAX_PATH + #endif #else #define PRI64 "ll" #endif + +#define DO(x) do{ \ + int err; \ + if ((err = (x)) != CRYPT_OK) { \ + fprintf(stderr, "\n\n " #x " says %s!\n", error_to_string(err)); \ + exit(EXIT_FAILURE); \ + } \ +} while(0) + static prng_state yarrow_prng; /* timing */ @@ -896,50 +909,51 @@ static void time_dh(void) { fprintf(stderr, "NO DH\n"); } #endif #if defined(LTC_MECC) -/* time various ECC operations */ -static void time_ecc(void) -{ - ecc_key key; - ulong64 t1, t2; - unsigned char buf[2][256] = { 0 }; - unsigned long i, w, x, y, z; - int err, stat; - static unsigned long sizes[] = { +static unsigned long ecc_key_sizes[] = { #ifdef LTC_ECC_SECP112R1 -112/8, +112, #endif #ifdef LTC_ECC_SECP128R1 -128/8, +128, #endif #ifdef LTC_ECC_SECP160R1 -160/8, +160, #endif #ifdef LTC_ECC_SECP192R1 -192/8, +192, #endif #ifdef LTC_ECC_SECP224R1 -224/8, +224, #endif #ifdef LTC_ECC_SECP256R1 -256/8, +256, #endif #ifdef LTC_ECC_SECP384R1 -384/8, +384, #endif #ifdef LTC_ECC_SECP512R1 -521/8, +521, #endif 100000}; +/* time various ECC operations */ +static void time_ecc(void) +{ + ecc_key key; + ulong64 t1, t2; + unsigned char buf[2][256] = { 0 }; + unsigned long i, w, x, y, z; + int err, stat; + if (ltc_mp.name == NULL) return; print_csv_header("keysize", NULL); - for (x = sizes[i=0]; x < 100000; x = sizes[++i]) { + for (x = ecc_key_sizes[i=0]; x < 100000; x = ecc_key_sizes[++i]) { t2 = 0; for (y = 0; y < 256; y++) { t_start(); t1 = t_read(); - if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) { + if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x/8, &key)) != CRYPT_OK) { fprintf(stderr, "\n\necc_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); exit(EXIT_FAILURE); } @@ -956,7 +970,7 @@ static void time_ecc(void) } } t2 >>= 8; - print_csv("ECC", "make_key", x*8, t2); + print_csv("ECC", "make_key", x, t2); t2 = 0; for (y = 0; y < 256; y++) { @@ -976,7 +990,7 @@ static void time_ecc(void) #endif } t2 >>= 8; - print_csv("ECC", "encrypt_key", x*8, t2); + print_csv("ECC", "encrypt_key", x, t2); t2 = 0; for (y = 0; y < 256; y++) { @@ -995,7 +1009,7 @@ static void time_ecc(void) #endif } t2 >>= 8; - print_csv("ECC", "decrypt_key", x*8, t2); + print_csv("ECC", "decrypt_key", x, t2); t2 = 0; for (y = 0; y < 256; y++) { @@ -1015,7 +1029,7 @@ static void time_ecc(void) #endif } t2 >>= 8; - print_csv("ECC", "sign_hash", x*8, t2); + print_csv("ECC", "sign_hash", x, t2); t2 = 0; for (y = 0; y < 256; y++) { @@ -1026,7 +1040,7 @@ static void time_ecc(void) exit(EXIT_FAILURE); } if (stat == 0) { - fprintf(stderr, "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n", x*8, y); + fprintf(stderr, "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n", x, y); exit(EXIT_FAILURE); } t1 = t_read() - t1; @@ -1037,7 +1051,7 @@ static void time_ecc(void) #endif } t2 >>= 8; - print_csv("ECC", "verify_hash", x*8, t2); + print_csv("ECC", "verify_hash", x, t2); ecc_free(&key); } @@ -1046,6 +1060,117 @@ static void time_ecc(void) static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); } #endif + +/* generate fresh PKA keys for the timing operations */ +#if defined(LTC_MRSA) || defined(LTC_MECC) + +static void write_key(const char *alg, unsigned long sz, struct list *elmnt, void *buf, unsigned long l) +{ + char name[PATH_MAX]; + FILE *f; + + snprintf(name, sizeof(name) - 1, "demos/keys/%s-%lu.privkey", alg, sz); + fprintf(stderr, "%s: Writing key %d which required %"PRI64"u ticks to %s\n", alg, elmnt->id, elmnt->avg, name); + f = fopen(name, "wb+"); + if (f == NULL) { + fprintf(stderr, "can't open %s", name); + exit(EXIT_FAILURE); + } + if (fwrite(buf, l, 1, f) != 1) { + fprintf(stderr, "can't write to %s", name); + exit(EXIT_FAILURE); + } + fclose(f); +} + +static void time_generate_keys(void) +{ + union + { +#if defined(LTC_MRSA) + rsa_key rsa; +#endif +#if defined(LTC_MECC) + ecc_key ecc; +#endif + } key[25]; + ulong64 t1 = 0; + unsigned char buf[8192] = { 0 }, op_buf[8192 / 8]; + unsigned long n, x, y, z, l; + const unsigned median = ((sizeof(key) / sizeof(key[0])) / 2); + + if (ltc_mp.name == NULL) return; + + print_csv_header("keysize", NULL); +#if defined(LTC_MRSA) + for (x = 2048; x <= 8192; x <<= 1) { + + for (y = 0; y < sizeof(key) / sizeof(key[0]); y++) { + DO(rsa_make_key(&yarrow_prng, find_prng("yarrow"), x / 8, 65537, &key[y].rsa)); + t_start(); + for (z = 0; z < 512 / (x / 1024); ++z) { + if (z == 8) { + t_start(); + t1 = t_read(); + } + l = sizeof(op_buf); + op_buf[0] = 0; + op_buf[1] = 1; + op_buf[2] = 0; + DO(rsa_exptmod(op_buf, x / 8, op_buf, &l, PK_PUBLIC, &key[y].rsa)); + } + t1 = t_read() - t1; + results[y].id = y; + results[y].avg = t1; + print_csv("RSA", "exptmod", x, t1); + } + + qsort(results, sizeof(key) / sizeof(key[0]), sizeof(struct list), &sorter); + + l = sizeof(buf); + DO(rsa_export(buf, &l, PK_PRIVATE, &key[results[median].id].rsa)); + + write_key("RSA", x, &results[median], buf, l); + + for (y = 0; y < sizeof(key) / sizeof(key[0]); y++) { + rsa_free(&key[y].rsa); + } + } +#endif +#if defined(LTC_MECC) + for (x = ecc_key_sizes[n = 0]; x < 100000; x = ecc_key_sizes[++n]) { + for (y = 0; y < sizeof(key) / sizeof(key[0]); y++) { + DO(ecc_make_key(&yarrow_prng, find_prng("yarrow"), x/8, &key[y].ecc)); + for (z = 0; z < 256; z++) { + if (z == 8) { + t_start(); + t1 = t_read(); + } + l = sizeof(op_buf); + DO(ecc_shared_secret(&key[y].ecc, &key[y].ecc, op_buf, &l)); + } + t1 = t_read() - t1; + results[y].id = y; + results[y].avg = t1; + print_csv("ECC", "shared_secret", x, t1); + } + + qsort(results, sizeof(key) / sizeof(key[0]), sizeof(struct list), &sorter); + + l = sizeof(buf); + DO(ecc_export(buf, &l, PK_PRIVATE, &key[results[median].id].ecc)); + + write_key("ECC", x, &results[median], buf, l); + + for (y = 0; y < sizeof(key) / sizeof(key[0]); y++) { + ecc_free(&key[y].ecc); + } + } +#endif +} +#endif + + static void time_macs_(unsigned long MAC_SIZE) { #if defined(LTC_OMAC) || defined(LTC_XCBC) || defined(LTC_F9_MODE) || defined(LTC_PMAC) || defined(LTC_PELICAN) || defined(LTC_HMAC) @@ -1382,6 +1507,7 @@ const struct LTC_TEST_FN(time_dsa), LTC_TEST_FN(time_ecc), LTC_TEST_FN(time_dh), + LTC_TEST_FN(time_generate_keys), }; char *single_test = NULL; unsigned int i; From 6bbb068c9280ef448179755cdbdfd3625b6a8d91 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 7 Nov 2019 16:21:41 +0100 Subject: [PATCH 4/5] use reference-keys for timing of ECC and RSA --- demos/timing.c | 122 ++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 63 deletions(-) diff --git a/demos/timing.c b/demos/timing.c index 942d3c566..9e41d1036 100644 --- a/demos/timing.c +++ b/demos/timing.c @@ -67,6 +67,9 @@ static void tally_results(int type) } } } +#if defined(LTC_MRSA) || defined(LTC_MECC) +static void read_key(const char *alg, unsigned long sz, void *buf, unsigned long *l); +#endif #define CSV_SEP "," #define OUTFILE stdout @@ -737,25 +740,22 @@ static void time_rsa(void) { rsa_key key; ulong64 t1, t2; - unsigned char buf[2][2048] = + unsigned char buf[2][4096] = { 0 }; unsigned long x, y, z, zzz; - int err, zz; + int zz; if (ltc_mp.name == NULL) return; print_csv_header("keysize", NULL); for (x = 2048; x <= 8192; x <<= 1) { +#ifndef TIMING_DONT_MAKE_KEY t2 = 0; for (y = 0; y < 4; y++) { t_start(); t1 = t_read(); - if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x / 8, 65537, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\nrsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), - error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(rsa_make_key(&yarrow_prng, find_prng("yarrow"), x / 8, 65537, &key)); t1 = t_read() - t1; t2 += t1; @@ -768,19 +768,20 @@ static void time_rsa(void) } t2 >>= 2; print_csv("RSA", "make_key", x, t2); +#endif + + zzz = sizeof(buf); + read_key("RSA", x, buf, &zzz); + + DO(rsa_import((void*)buf, zzz, &key)); t2 = 0; for (y = 0; y < 256; y++) { t_start(); t1 = t_read(); z = sizeof(buf[1]); - if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char * )"testprog", 8, &yarrow_prng, - find_prng("yarrow"), find_hash("sha1"), &key)) - != CRYPT_OK) { - fprintf(stderr, "\n\nrsa_encrypt_key says %s, wait...no it should say %s...damn you!\n", - error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char * )"testprog", 8, &yarrow_prng, + find_prng("yarrow"), find_hash("sha1"), &key)); t1 = t_read() - t1; t2 += t1; #ifdef LTC_PROFILE @@ -796,13 +797,8 @@ static void time_rsa(void) t_start(); t1 = t_read(); zzz = sizeof(buf[0]); - if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, (const unsigned char * )"testprog", 8, find_hash("sha1"), - &zz, &key)) - != CRYPT_OK) { - fprintf(stderr, "\n\nrsa_decrypt_key says %s, wait...no it should say %s...damn you!\n", - error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(rsa_decrypt_key(buf[1], z, buf[0], &zzz, (const unsigned char * )"testprog", 8, find_hash("sha1"), + &zz, &key)); t1 = t_read() - t1; t2 += t1; #ifdef LTC_PROFILE @@ -818,11 +814,7 @@ static void time_rsa(void) t_start(); t1 = t_read(); z = sizeof(buf[1]); - if ((err = rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), 8, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\nrsa_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), - error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), 8, &key)); t1 = t_read() - t1; t2 += t1; #ifdef LTC_PROFILE @@ -838,11 +830,7 @@ static void time_rsa(void) int stat; t_start(); t1 = t_read(); - if ((err = rsa_verify_hash(buf[1], z, buf[0], 20, find_hash("sha1"), 8, &stat, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\nrsa_verify_hash says %s, wait...no it should say %s...damn you!\n", - error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(rsa_verify_hash(buf[1], z, buf[0], 20, find_hash("sha1"), 8, &stat, &key)); if (stat == 0) { fprintf(stderr, "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n", x, y); exit(EXIT_FAILURE); @@ -870,7 +858,6 @@ static void time_dh(void) dh_key key; ulong64 t1, t2; unsigned long i, x, y; - int err; static unsigned long sizes[] = {768/8, 1024/8, 1536/8, 2048/8, #ifndef TFM_DESC 3072/8, 4096/8, 6144/8, 8192/8, @@ -884,17 +871,11 @@ static void time_dh(void) for (x = sizes[i=0]; x < 100000; x = sizes[++i]) { t2 = 0; for (y = 0; y < 16; y++) { - if((err = dh_set_pg_groupsize(x, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\ndh_set_pg_groupsize says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(dh_set_pg_groupsize(x, &key)); t_start(); t1 = t_read(); - if ((err = dh_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) { - fprintf(stderr, "\n\ndh_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(dh_generate_key(&yarrow_prng, find_prng("yarrow"), &key)); t1 = t_read() - t1; t2 += t1; @@ -943,20 +924,19 @@ static void time_ecc(void) ulong64 t1, t2; unsigned char buf[2][256] = { 0 }; unsigned long i, w, x, y, z; - int err, stat; + int stat; if (ltc_mp.name == NULL) return; print_csv_header("keysize", NULL); for (x = ecc_key_sizes[i=0]; x < 100000; x = ecc_key_sizes[++i]) { + +#ifndef TIMING_DONT_MAKE_KEY t2 = 0; for (y = 0; y < 256; y++) { t_start(); t1 = t_read(); - if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x/8, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\necc_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(ecc_make_key(&yarrow_prng, find_prng("yarrow"), x/8, &key)); t1 = t_read() - t1; t2 += t1; @@ -971,17 +951,20 @@ static void time_ecc(void) } t2 >>= 8; print_csv("ECC", "make_key", x, t2); +#endif + + w = sizeof(buf[0]); + read_key("ECC", x, buf[0], &w); + + DO(ecc_import(buf[0], w, &key)); t2 = 0; for (y = 0; y < 256; y++) { t_start(); t1 = t_read(); z = sizeof(buf[1]); - if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), - &key)) != CRYPT_OK) { - fprintf(stderr, "\n\necc_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), + &key)); t1 = t_read() - t1; t2 += t1; #ifdef LTC_PROFILE @@ -997,10 +980,7 @@ static void time_ecc(void) t_start(); t1 = t_read(); w = 20; - if ((err = ecc_decrypt_key(buf[1], z, buf[0], &w, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\necc_decrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(ecc_decrypt_key(buf[1], z, buf[0], &w, &key)); t1 = t_read() - t1; t2 += t1; #ifdef LTC_PROFILE @@ -1016,11 +996,8 @@ static void time_ecc(void) t_start(); t1 = t_read(); z = sizeof(buf[1]); - if ((err = ecc_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng, - find_prng("yarrow"), &key)) != CRYPT_OK) { - fprintf(stderr, "\n\necc_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(ecc_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng, + find_prng("yarrow"), &key)); t1 = t_read() - t1; t2 += t1; #ifdef LTC_PROFILE @@ -1035,10 +1012,7 @@ static void time_ecc(void) for (y = 0; y < 256; y++) { t_start(); t1 = t_read(); - if ((err = ecc_verify_hash(buf[1], z, buf[0], 20, &stat, &key)) != CRYPT_OK) { - fprintf(stderr, "\n\necc_verify_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK)); - exit(EXIT_FAILURE); - } + DO(ecc_verify_hash(buf[1], z, buf[0], 20, &stat, &key)); if (stat == 0) { fprintf(stderr, "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n", x, y); exit(EXIT_FAILURE); @@ -1064,6 +1038,28 @@ static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); } /* generate fresh PKA keys for the timing operations */ #if defined(LTC_MRSA) || defined(LTC_MECC) +static void read_key(const char *alg, unsigned long sz, void *buf, unsigned long *l) +{ + char name[PATH_MAX]; + FILE *f; + size_t n; + + snprintf(name, sizeof(name) - 1, "demos/keys/%s-%lu.privkey", alg, sz); + f = fopen(name, "rb"); + if (f == NULL) { + fprintf(stderr, "can't open %s", name); + exit(EXIT_FAILURE); + } + n = fread(buf, 1, *l, f); + if (feof(f)) { + *l = n; + } else if (ferror(f)) { + fprintf(stderr, "reading of %s errored", name); + exit(EXIT_FAILURE); + } + fclose(f); +} + static void write_key(const char *alg, unsigned long sz, struct list *elmnt, void *buf, unsigned long l) { char name[PATH_MAX]; From cebe1603f6436fc11f500f822b6c2e77dfac9b26 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 8 Nov 2019 01:57:40 +0100 Subject: [PATCH 5/5] allow building against tfm --- demos/timing.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/demos/timing.c b/demos/timing.c index 9e41d1036..691b45110 100644 --- a/demos/timing.c +++ b/demos/timing.c @@ -12,6 +12,9 @@ #define PRI64 "ll" #endif +#ifdef TFM_DESC +#include +#endif #define DO(x) do{ \ int err; \ @@ -672,6 +675,18 @@ static void time_prng(void) } } +static int check_tfm_limit(unsigned long x) +{ +#ifdef TFM_DESC + if (strcasecmp(ltc_mp.name, "tomsfastmath") == 0) { + if (x * 2 > FP_MAX_SIZE) return 1; + } +#else + LTC_UNUSED_PARAM(x); +#endif + return 0; +} + #if defined(LTC_MDSA) /* time various DSA operations */ static void time_dsa(void) @@ -689,15 +704,14 @@ static void time_dsa(void) { 20, 128 }, { 24, 192 }, { 28, 256 }, -#ifndef TFM_DESC { 32, 512 }, -#endif }; if (ltc_mp.name == NULL) return; print_csv_header("group", "modulus"); for (x = 0; x < (sizeof(groups) / sizeof(groups[0])); x++) { + if (check_tfm_limit(groups[x].modulus * 8)) break; t2 = 0; for (y = 0; y < 4; y++) { t_start(); @@ -733,7 +747,6 @@ static void time_dsa(void) static void time_dsa(void) { fprintf(stderr, "NO DSA\n"); } #endif - #if defined(LTC_MRSA) /* time various RSA operations */ static void time_rsa(void) @@ -749,6 +762,7 @@ static void time_rsa(void) print_csv_header("keysize", NULL); for (x = 2048; x <= 8192; x <<= 1) { + if (check_tfm_limit(x)) break; #ifndef TIMING_DONT_MAKE_KEY t2 = 0; @@ -859,9 +873,7 @@ static void time_dh(void) ulong64 t1, t2; unsigned long i, x, y; static unsigned long sizes[] = {768/8, 1024/8, 1536/8, 2048/8, -#ifndef TFM_DESC 3072/8, 4096/8, 6144/8, 8192/8, -#endif 100000 }; @@ -869,6 +881,7 @@ static void time_dh(void) print_csv_header("keysize", NULL); for (x = sizes[i=0]; x < 100000; x = sizes[++i]) { + if (check_tfm_limit(x)) break; t2 = 0; for (y = 0; y < 16; y++) { DO(dh_set_pg_groupsize(x, &key)); @@ -930,6 +943,7 @@ static void time_ecc(void) print_csv_header("keysize", NULL); for (x = ecc_key_sizes[i=0]; x < 100000; x = ecc_key_sizes[++i]) { + if (check_tfm_limit(x)) break; #ifndef TIMING_DONT_MAKE_KEY t2 = 0;