@@ -36,6 +36,12 @@ static OSSL_TIME max_time;
3636size_t * counts = NULL ;
3737int err = 0 ;
3838
39+ typedef enum {
40+ DEPRECATED = 0 ,
41+ EVP_ISOLATED ,
42+ EVP_SHARED ,
43+ } operation_type ;
44+
3945typedef enum {
4046 SHA1_ALG = 0 ,
4147 SHA224_ALG ,
@@ -47,9 +53,11 @@ typedef enum {
4753static unsigned char data [DATA_SIZE ];
4854static int update_times = 1 ;
4955static const EVP_MD * evp_md = NULL ;
50- static int (* hash_func_deprecated )(void );
56+ static int (* hash_func_isolated )(void );
57+
58+ /* These functions are not allowed to take any parameters */
5159
52- int hash_sha1_deprecated ()
60+ static int hash_deprecated_sha1 ()
5361{
5462 int i ;
5563 SHA_CTX sha_ctx ;
@@ -65,7 +73,7 @@ int hash_sha1_deprecated()
6573 return SHA1_Final (md , & sha_ctx );
6674}
6775
68- int hash_sha224_deprecated ()
76+ static int hash_deprecated_sha224 ()
6977{
7078 int i ;
7179 SHA256_CTX sha256_ctx ;
@@ -81,7 +89,7 @@ int hash_sha224_deprecated()
8189 return SHA224_Final (md , & sha256_ctx );
8290}
8391
84- int hash_sha256_deprecated ()
92+ static int hash_deprecated_sha256 ()
8593{
8694 int i ;
8795 SHA256_CTX sha256_ctx ;
@@ -97,7 +105,7 @@ int hash_sha256_deprecated()
97105 return SHA256_Final (md , & sha256_ctx );
98106}
99107
100- int hash_sha384_deprecated ()
108+ static int hash_deprecated_sha384 ()
101109{
102110 int i ;
103111 SHA512_CTX sha512_ctx ;
@@ -113,7 +121,7 @@ int hash_sha384_deprecated()
113121 return SHA384_Final (md , & sha512_ctx );
114122}
115123
116- int hash_sha512_deprecated ()
124+ static int hash_deprecated_sha512 ()
117125{
118126 int i ;
119127 SHA512_CTX sha512_ctx ;
@@ -129,28 +137,59 @@ int hash_sha512_deprecated()
129137 return SHA512_Final (md , & sha512_ctx );
130138}
131139
132-
133- int hash_evp (EVP_MD_CTX * mctx )
140+ static int hash_evp (const EVP_MD * evp_md )
134141{
135- int i ;
142+ int i , ret = 0 ;
136143 unsigned char md [EVP_MAX_MD_SIZE ];
144+ EVP_MD_CTX * mctx = EVP_MD_CTX_new ();
137145
138- if (!EVP_DigestInit_ex (mctx , NULL , NULL ))
139- return 0 ;
146+ if (mctx == NULL || !EVP_DigestInit_ex (mctx , evp_md , NULL ))
147+ goto err ;
140148
141149 for (i = 0 ; i < update_times ; i ++ )
142150 if (!EVP_DigestUpdate (mctx , data , sizeof (data )))
143- return 0 ;
151+ goto err ;
144152
145- return EVP_DigestFinal_ex (mctx , md , NULL );
153+ if (!EVP_DigestFinal_ex (mctx , md , NULL ))
154+ goto err ;
155+
156+ ret = 1 ;
157+ err :
158+ EVP_MD_CTX_free (mctx );
159+ return ret ;
160+ }
161+
162+ static int hash_evp_sha1 ()
163+ {
164+ return hash_evp (EVP_sha1 ());
146165}
147166
148- void do_hash_deprecated (size_t num )
167+ static int hash_evp_sha224 ()
168+ {
169+ return hash_evp (EVP_sha224 ());
170+ }
171+
172+ static int hash_evp_sha256 ()
173+ {
174+ return hash_evp (EVP_sha256 ());
175+ }
176+
177+ static int hash_evp_sha384 ()
178+ {
179+ return hash_evp (EVP_sha384 ());
180+ }
181+
182+ static int hash_evp_sha512 ()
183+ {
184+ return hash_evp (EVP_sha512 ());
185+ }
186+
187+ static void do_hash_isolated (size_t num )
149188{
150189 OSSL_TIME time ;
151190
152191 do {
153- if (!hash_func_deprecated ()) {
192+ if (!hash_func_isolated ()) {
154193 err = 1 ;
155194 return ;
156195 }
@@ -160,7 +199,26 @@ void do_hash_deprecated(size_t num)
160199 } while (time .t < max_time .t );
161200}
162201
163- void do_hash_evp (size_t num )
202+ /*
203+ * These functions can access global state, so they can do some initial setup.
204+ */
205+
206+ static int hash_evp_shared (EVP_MD_CTX * mctx )
207+ {
208+ int i ;
209+ unsigned char md [EVP_MAX_MD_SIZE ];
210+
211+ if (!EVP_DigestInit_ex (mctx , NULL , NULL ))
212+ return 0 ;
213+
214+ for (i = 0 ; i < update_times ; i ++ )
215+ if (!EVP_DigestUpdate (mctx , data , sizeof (data )))
216+ return 0 ;
217+
218+ return EVP_DigestFinal_ex (mctx , md , NULL );
219+ }
220+
221+ static void do_hash_evp_shared (size_t num )
164222{
165223 OSSL_TIME time ;
166224 EVP_MD_CTX * mctx = EVP_MD_CTX_new ();
@@ -171,7 +229,7 @@ void do_hash_evp(size_t num)
171229 }
172230
173231 do {
174- if (!hash_evp (mctx )) {
232+ if (!hash_evp_shared (mctx )) {
175233 err = 1 ;
176234 goto err ;
177235 }
@@ -184,12 +242,14 @@ void do_hash_evp(size_t num)
184242 EVP_MD_CTX_free (mctx );
185243}
186244
187- void print_help ()
245+ /* Main */
246+
247+ static void print_help ()
188248{
189- printf ("Usage: evp_hash [-h] [-x ] [-t ] [-u update-times] [-a algorithm] thread-count\n" );
249+ printf ("Usage: evp_hash [-h] [-t ] [-o operation ] [-u update-times] [-a algorithm] thread-count\n" );
190250 printf ("-h - print this help output\n" );
191- printf ("-x - use deprecated API instead of EVP API\n" );
192251 printf ("-t - terse output\n" );
252+ printf ("-o operation - mode of operation. One of [deprecated, evp_isolated, evp_shared] (default: evp_shared)\n" );
193253 printf ("-u update-times - times to update digest. 1 for one-shot (default: 1)\n" );
194254 printf ("-a algorithm - One of: [SHA1, SHA224, SHA256, SHA384, SHA512] (default: SHA1)\n" );
195255 printf ("thread-count - number of threads\n" );
@@ -200,16 +260,26 @@ int main(int argc, char *argv[])
200260 OSSL_TIME duration ;
201261 size_t total_count = 0 ;
202262 double av ;
203- int terse = 0 , deprecated_api = 0 , hash_algorithm = SHA1_ALG ;
263+ int terse = 0 , operation = EVP_SHARED , hash_algorithm = SHA1_ALG ;
204264 int j , opt , rc = EXIT_FAILURE ;
205265
206- while ((opt = getopt (argc , argv , "htxu :a:" )) != -1 ) {
266+ while ((opt = getopt (argc , argv , "hto:u :a:" )) != -1 ) {
207267 switch (opt ) {
208268 case 't' :
209269 terse = 1 ;
210270 break ;
211- case 'x' :
212- deprecated_api = 1 ;
271+ case 'o' :
272+ if (strcmp (optarg , "deprecated" ) == 0 ) {
273+ operation = DEPRECATED ;
274+ } else if (strcmp (optarg , "evp_isolated" ) == 0 ) {
275+ operation = EVP_ISOLATED ;
276+ } else if (strcmp (optarg , "evp_shared" ) == 0 ) {
277+ operation = EVP_SHARED ;
278+ } else {
279+ fprintf (stderr , "operation is one of [deprecated, evp_isolated, evp_shared]\n" );
280+ print_help ();
281+ goto out ;
282+ }
213283 break ;
214284 case 'u' :
215285 update_times = atoi (optarg );
@@ -266,29 +336,54 @@ int main(int argc, char *argv[])
266336
267337 max_time = ossl_time_add (ossl_time_now (), ossl_seconds2time (RUN_TIME ));
268338
269- if (deprecated_api ) {
339+ switch (operation ) {
340+ case DEPRECATED :
341+ switch (hash_algorithm ) {
342+ case SHA1_ALG :
343+ hash_func_isolated = hash_deprecated_sha1 ;
344+ break ;
345+ case SHA224_ALG :
346+ hash_func_isolated = hash_deprecated_sha224 ;
347+ break ;
348+ case SHA256_ALG :
349+ hash_func_isolated = hash_deprecated_sha256 ;
350+ break ;
351+ case SHA384_ALG :
352+ hash_func_isolated = hash_deprecated_sha384 ;
353+ break ;
354+ case SHA512_ALG :
355+ hash_func_isolated = hash_deprecated_sha512 ;
356+ break ;
357+ default :
358+ err = 1 ;
359+ goto out ;
360+ }
361+ err = !perflib_run_multi_thread_test (do_hash_isolated , threadcount , & duration ) || err ;
362+ break ;
363+ case EVP_ISOLATED :
270364 switch (hash_algorithm ) {
271365 case SHA1_ALG :
272- hash_func_deprecated = hash_sha1_deprecated ;
366+ hash_func_isolated = hash_evp_sha1 ;
273367 break ;
274368 case SHA224_ALG :
275- hash_func_deprecated = hash_sha224_deprecated ;
369+ hash_func_isolated = hash_evp_sha224 ;
276370 break ;
277371 case SHA256_ALG :
278- hash_func_deprecated = hash_sha256_deprecated ;
372+ hash_func_isolated = hash_evp_sha256 ;
279373 break ;
280374 case SHA384_ALG :
281- hash_func_deprecated = hash_sha384_deprecated ;
375+ hash_func_isolated = hash_evp_sha384 ;
282376 break ;
283377 case SHA512_ALG :
284- hash_func_deprecated = hash_sha512_deprecated ;
378+ hash_func_isolated = hash_evp_sha512 ;
285379 break ;
286380 default :
287381 err = 1 ;
288382 goto out ;
289383 }
290- err = !perflib_run_multi_thread_test (do_hash_deprecated , threadcount , & duration ) || err ;
291- } else {
384+ err = !perflib_run_multi_thread_test (do_hash_isolated , threadcount , & duration ) || err ;
385+ break ;
386+ case EVP_SHARED :
292387 switch (hash_algorithm ) {
293388 case SHA1_ALG :
294389 evp_md = EVP_sha1 ();
@@ -310,7 +405,8 @@ int main(int argc, char *argv[])
310405 goto out ;
311406 }
312407
313- err = !perflib_run_multi_thread_test (do_hash_evp , threadcount , & duration ) || err ;
408+ err = !perflib_run_multi_thread_test (do_hash_evp_shared , threadcount , & duration ) || err ;
409+ break ;
314410 }
315411
316412 if (err ) {
0 commit comments