Skip to content

Commit ceac3bc

Browse files
committed
Make more arguments optional
one-shot and SHA1 by default
1 parent b1c7702 commit ceac3bc

File tree

1 file changed

+47
-56
lines changed

1 file changed

+47
-56
lines changed

source/evp_hash.c

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
#define RUN_TIME 5
3131
#define DATA_SIZE 1500
3232

33+
static int threadcount;
34+
static OSSL_TIME max_time;
35+
3336
size_t *counts = NULL;
34-
OSSL_TIME max_time;
3537
int err = 0;
36-
static int threadcount;
37-
size_t num_calls;
3838

3939
typedef enum {
4040
SHA1_ALG = 0,
@@ -57,13 +57,12 @@ int hash_sha1_deprecated()
5757

5858
if (!SHA1_Init(&sha_ctx))
5959
return 0;
60+
6061
for (i = 0; i < update_times; i++)
6162
if (!SHA1_Update(&sha_ctx, data, sizeof(data)))
6263
return 0;
63-
if (!SHA1_Final(md, &sha_ctx))
64-
return 0;
6564

66-
return 1;
65+
return SHA1_Final(md, &sha_ctx);
6766
}
6867

6968
int hash_sha224_deprecated()
@@ -74,13 +73,12 @@ int hash_sha224_deprecated()
7473

7574
if (!SHA224_Init(&sha256_ctx))
7675
return 0;
76+
7777
for (i = 0; i < update_times; i++)
7878
if (!SHA224_Update(&sha256_ctx, data, sizeof(data)))
7979
return 0;
80-
if (!SHA224_Final(md, &sha256_ctx))
81-
return 0;
8280

83-
return 1;
81+
return SHA224_Final(md, &sha256_ctx);
8482
}
8583

8684
int hash_sha256_deprecated()
@@ -91,13 +89,12 @@ int hash_sha256_deprecated()
9189

9290
if (!SHA256_Init(&sha256_ctx))
9391
return 0;
92+
9493
for (i = 0; i < update_times; i++)
9594
if (!SHA256_Update(&sha256_ctx, data, sizeof(data)))
9695
return 0;
97-
if (!SHA256_Final(md, &sha256_ctx))
98-
return 0;
9996

100-
return 1;
97+
return SHA256_Final(md, &sha256_ctx);
10198
}
10299

103100
int hash_sha384_deprecated()
@@ -108,13 +105,12 @@ int hash_sha384_deprecated()
108105

109106
if (!SHA384_Init(&sha512_ctx))
110107
return 0;
108+
111109
for (i = 0; i < update_times; i++)
112110
if (!SHA384_Update(&sha512_ctx, data, sizeof(data)))
113111
return 0;
114-
if (!SHA384_Final(md, &sha512_ctx))
115-
return 0;
116112

117-
return 1;
113+
return SHA384_Final(md, &sha512_ctx);
118114
}
119115

120116
int hash_sha512_deprecated()
@@ -125,13 +121,12 @@ int hash_sha512_deprecated()
125121

126122
if (!SHA512_Init(&sha512_ctx))
127123
return 0;
124+
128125
for (i = 0; i < update_times; i++)
129126
if (!SHA512_Update(&sha512_ctx, data, sizeof(data)))
130127
return 0;
131-
if (!SHA512_Final(md, &sha512_ctx))
132-
return 0;
133128

134-
return 1;
129+
return SHA512_Final(md, &sha512_ctx);
135130
}
136131

137132

@@ -147,10 +142,7 @@ int hash_evp(EVP_MD_CTX *mctx, const EVP_MD *evp_md)
147142
if (!EVP_DigestUpdate(mctx, data, sizeof(data)))
148143
return 0;
149144

150-
if (!EVP_DigestFinal(mctx, md, NULL))
151-
return 0;
152-
153-
return 1;
145+
return EVP_DigestFinal(mctx, md, NULL);
154146
}
155147

156148
void do_hash_deprecated(size_t num)
@@ -192,12 +184,12 @@ void do_hash_evp(size_t num)
192184

193185
void print_help()
194186
{
195-
printf("Usage: evp_hash [-h] [-x] [-t] update-times algorithm thread-count\n");
187+
printf("Usage: evp_hash [-h] [-x] [-t] [-u update-times] [-a algorithm] thread-count\n");
196188
printf("-h - print this help output\n");
197189
printf("-x - use deprecated API instead of EVP API\n");
198190
printf("-t - terse output\n");
199-
printf("update-times - times to update digest. 1 for one-shot\n");
200-
printf("algorithm - one of: [SHA1, SHA224, SHA256, SHA384, SHA512]\n");
191+
printf("-u update-times - times to update digest. 1 for one-shot. By default, do one-shot\n");
192+
printf("-a algorithm - One of: [SHA1, SHA224, SHA256, SHA384, SHA512]. By default, use SHA1\n");
201193
printf("thread-count - number of threads\n");
202194
}
203195

@@ -206,56 +198,55 @@ int main(int argc, char *argv[])
206198
OSSL_TIME duration;
207199
size_t total_count = 0;
208200
double av;
209-
int terse = 0, deprecated_api = 0, hash_algorithm = -1;
201+
int terse = 0, deprecated_api = 0, hash_algorithm = SHA1_ALG;
210202
int j, opt, rc = EXIT_FAILURE;
211203

212-
while ((opt = getopt(argc, argv, "htx")) != -1) {
204+
while ((opt = getopt(argc, argv, "htxu:a:")) != -1) {
213205
switch (opt) {
214206
case 't':
215207
terse = 1;
216208
break;
217209
case 'x':
218210
deprecated_api = 1;
219211
break;
212+
case 'u':
213+
update_times = atoi(optarg);
214+
if (update_times <= 0) {
215+
fprintf(stderr, "update-times must be a positive integer\n");
216+
goto out;
217+
}
218+
break;
219+
case 'a':
220+
if (strcmp(optarg, "SHA1") == 0) {
221+
hash_algorithm = SHA1_ALG;
222+
} else if (strcmp(optarg, "SHA224") == 0) {
223+
hash_algorithm = SHA224_ALG;
224+
} else if (strcmp(optarg, "SHA256") == 0) {
225+
hash_algorithm = SHA256_ALG;
226+
} else if (strcmp(optarg, "SHA384") == 0) {
227+
hash_algorithm = SHA384_ALG;
228+
} else if (strcmp(optarg, "SHA512") == 0) {
229+
hash_algorithm = SHA512_ALG;
230+
} else {
231+
fprintf(stderr, "algorithm is one of: [SHA1, SHA224, SHA256, SHA384, SHA512]\n");
232+
print_help();
233+
goto out;
234+
}
235+
break;
220236
case 'h':
221237
default:
222238
print_help();
223239
goto out;
224240
}
225241
}
226242

227-
if (argv[optind] == NULL
228-
|| argv[optind+1] == NULL
229-
|| argv[optind+2] == NULL
230-
|| argv[optind+3] != NULL) {
243+
if (argc - optind != 1) {
231244
fprintf(stderr, "Incorrect number of arguments\n");
232245
print_help();
233246
goto out;
234247
}
235248

236-
update_times = atoi(argv[optind]);
237-
if (update_times <= 0) {
238-
fprintf(stderr, "update-times must be a positive integer\n");
239-
goto out;
240-
}
241-
242-
if (strcmp(argv[optind+1], "SHA1") == 0) {
243-
hash_algorithm = SHA1_ALG;
244-
} else if (strcmp(argv[optind+1], "SHA224") == 0) {
245-
hash_algorithm = SHA224_ALG;
246-
} else if (strcmp(argv[optind+1], "SHA256") == 0) {
247-
hash_algorithm = SHA256_ALG;
248-
} else if (strcmp(argv[optind+1], "SHA384") == 0) {
249-
hash_algorithm = SHA384_ALG;
250-
} else if (strcmp(argv[optind+1], "SHA512") == 0) {
251-
hash_algorithm = SHA512_ALG;
252-
} else {
253-
fprintf(stderr, "algorithm is one of: [SHA1, SHA224, SHA256, SHA384, SHA512]\n");
254-
print_help();
255-
goto out;
256-
}
257-
258-
threadcount = atoi(argv[optind+2]);
249+
threadcount = atoi(argv[optind]);
259250
if (threadcount < 1) {
260251
fprintf(stderr, "thread-count must be a positive integer\n");
261252
print_help();
@@ -265,14 +256,14 @@ int main(int argc, char *argv[])
265256
if (!RAND_bytes((unsigned char *)data, sizeof(data)))
266257
goto out;
267258

268-
max_time = ossl_time_add(ossl_time_now(), ossl_seconds2time(RUN_TIME));
269-
270259
counts = OPENSSL_zalloc(sizeof(size_t) * threadcount);
271260
if (counts == NULL) {
272261
fprintf(stderr, "Failed to create counts array\n");
273262
goto out;
274263
}
275264

265+
max_time = ossl_time_add(ossl_time_now(), ossl_seconds2time(RUN_TIME));
266+
276267
if (deprecated_api) {
277268
switch (hash_algorithm) {
278269
case SHA1_ALG:

0 commit comments

Comments
 (0)