Skip to content

Commit 991d050

Browse files
committed
rsasign: run threads for a constant time
Fixes: openssl/project#1258 Signed-off-by: Norbert Pocs <[email protected]> Reviewed-by: Saša Nedvědický <[email protected]> Reviewed-by: Neil Horman <[email protected]> (Merged from #32)
1 parent b84edfe commit 991d050

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

source/rsasign.c

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
#include <openssl/crypto.h>
2525
#include "perflib/perflib.h"
2626

27-
#define NUM_CALLS_PER_TEST 100000
27+
#define RUN_TIME 5
2828

29-
size_t num_calls;
3029
int err = 0;
3130
EVP_PKEY *rsakey = NULL;
3231

32+
size_t *counts;
33+
OSSL_TIME max_time;
34+
3335
static const char *rsakeypem =
3436
"-----BEGIN PRIVATE KEY-----\n"
3537
"MIIBVwIBADANBgkqhkiG9w0BAQEFAASCAUEwggE9AgEAAkEAwmjwpbuKfvtBTAiQ\n"
@@ -46,41 +48,38 @@ static const char *tbs = "0123456789abcdefghij"; /* Length of SHA1 digest */
4648

4749
static int threadcount;
4850

49-
static OSSL_TIME *times = NULL;
50-
5151
void do_rsasign(size_t num)
5252
{
53-
size_t i;
5453
unsigned char sig[64];
5554
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(rsakey, NULL);
5655
size_t siglen = sizeof(sig);
57-
OSSL_TIME start, end;
56+
OSSL_TIME time;
5857

59-
start = ossl_time_now();
58+
counts[num] = 0;
6059

61-
for (i = 0; i < num_calls / threadcount; i++) {
60+
do {
6261
if (EVP_PKEY_sign_init(ctx) <= 0
63-
|| EVP_PKEY_sign(ctx, sig, &siglen, tbs, SHA_DIGEST_LENGTH) <= 0) {
62+
|| EVP_PKEY_sign(ctx, sig, &siglen, (const unsigned char*)tbs,
63+
SHA_DIGEST_LENGTH) <= 0) {
6464
err = 1;
6565
break;
6666
}
67-
}
68-
69-
end = ossl_time_now();
70-
times[num] = ossl_time_subtract(end, start);
67+
counts[num]++;
68+
time = ossl_time_now();
69+
} while(time.t < max_time.t);
7170

7271
EVP_PKEY_CTX_free(ctx);
7372
}
7473

7574
int main(int argc, char *argv[])
7675
{
7776
OSSL_TIME duration;
78-
OSSL_TIME ttime;
77+
size_t total_count = 0;
7978
double avcalltime;
8079
int terse = 0;
8180
BIO *membio = NULL;
8281
int rc = EXIT_FAILURE;
83-
size_t i;
82+
int i;
8483
int opt;
8584

8685
while ((opt = getopt(argc, argv, "t")) != -1) {
@@ -104,9 +103,6 @@ int main(int argc, char *argv[])
104103
printf("threadcount must be > 0\n");
105104
return EXIT_FAILURE;
106105
}
107-
num_calls = NUM_CALLS_PER_TEST;
108-
if (NUM_CALLS_PER_TEST % threadcount > 0) /* round up */
109-
num_calls += threadcount - NUM_CALLS_PER_TEST % threadcount;
110106

111107
assert(strlen(tbs) == SHA_DIGEST_LENGTH);
112108
membio = BIO_new_mem_buf(rsakeypem, strlen(rsakeypem));
@@ -121,12 +117,14 @@ int main(int argc, char *argv[])
121117
goto out;
122118
}
123119

124-
times = OPENSSL_malloc(sizeof(OSSL_TIME) * threadcount);
125-
if (times == NULL) {
126-
printf("Failed to create times array\n");
120+
counts = OPENSSL_malloc(sizeof(size_t) * threadcount);
121+
if (counts == NULL) {
122+
printf("Failed to create counts array\n");
127123
goto out;
128124
}
129125

126+
max_time = ossl_time_add(ossl_time_now(), ossl_seconds2time(RUN_TIME));
127+
130128
if (!perflib_run_multi_thread_test(do_rsasign, threadcount, &duration)) {
131129
printf("Failed to run the test\n");
132130
goto out;
@@ -137,11 +135,10 @@ int main(int argc, char *argv[])
137135
goto out;
138136
}
139137

140-
ttime = times[0];
141-
for (i = 1; i < threadcount; i++)
142-
ttime = ossl_time_add(ttime, times[i]);
138+
for (i = 0; i < threadcount; i++)
139+
total_count += counts[i];
143140

144-
avcalltime = ((double)ossl_time2ticks(ttime) / num_calls) / (double)OSSL_TIME_US;
141+
avcalltime = (double)RUN_TIME * 1e6 * threadcount / total_count;
145142

146143
if (terse)
147144
printf("%lf\n", avcalltime);
@@ -153,6 +150,6 @@ int main(int argc, char *argv[])
153150

154151
out:
155152
EVP_PKEY_free(rsakey);
156-
OPENSSL_free(times);
153+
OPENSSL_free(counts);
157154
return rc;
158155
}

0 commit comments

Comments
 (0)