Skip to content

Commit 350d4c3

Browse files
authored
Merge pull request #327 from gilles-peskine-arm/psa-hash_compute
Implement psa_hash_compute and psa_hash_compare
2 parents f712e16 + 13faa2d commit 350d4c3

File tree

5 files changed

+520
-282
lines changed

5 files changed

+520
-282
lines changed

include/psa/crypto.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ psa_status_t psa_hash_compare(psa_algorithm_t alg,
932932
const uint8_t *input,
933933
size_t input_length,
934934
const uint8_t *hash,
935-
const size_t hash_length);
935+
size_t hash_length);
936936

937937
/** The type of the state data structure for multipart hash operations.
938938
*
@@ -1300,7 +1300,7 @@ psa_status_t psa_mac_verify(psa_key_handle_t handle,
13001300
const uint8_t *input,
13011301
size_t input_length,
13021302
const uint8_t *mac,
1303-
const size_t mac_length);
1303+
size_t mac_length);
13041304

13051305
/** The type of the state data structure for multipart MAC operations.
13061306
*

library/psa_crypto.c

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,6 +2351,58 @@ psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
23512351
return( PSA_SUCCESS );
23522352
}
23532353

2354+
psa_status_t psa_hash_compute( psa_algorithm_t alg,
2355+
const uint8_t *input, size_t input_length,
2356+
uint8_t *hash, size_t hash_size,
2357+
size_t *hash_length )
2358+
{
2359+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2360+
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2361+
2362+
*hash_length = hash_size;
2363+
status = psa_hash_setup( &operation, alg );
2364+
if( status != PSA_SUCCESS )
2365+
goto exit;
2366+
status = psa_hash_update( &operation, input, input_length );
2367+
if( status != PSA_SUCCESS )
2368+
goto exit;
2369+
status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2370+
if( status != PSA_SUCCESS )
2371+
goto exit;
2372+
2373+
exit:
2374+
if( status == PSA_SUCCESS )
2375+
status = psa_hash_abort( &operation );
2376+
else
2377+
psa_hash_abort( &operation );
2378+
return( status );
2379+
}
2380+
2381+
psa_status_t psa_hash_compare( psa_algorithm_t alg,
2382+
const uint8_t *input, size_t input_length,
2383+
const uint8_t *hash, size_t hash_length )
2384+
{
2385+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2386+
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2387+
2388+
status = psa_hash_setup( &operation, alg );
2389+
if( status != PSA_SUCCESS )
2390+
goto exit;
2391+
status = psa_hash_update( &operation, input, input_length );
2392+
if( status != PSA_SUCCESS )
2393+
goto exit;
2394+
status = psa_hash_verify( &operation, hash, hash_length );
2395+
if( status != PSA_SUCCESS )
2396+
goto exit;
2397+
2398+
exit:
2399+
if( status == PSA_SUCCESS )
2400+
status = psa_hash_abort( &operation );
2401+
else
2402+
psa_hash_abort( &operation );
2403+
return( status );
2404+
}
2405+
23542406
psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
23552407
psa_hash_operation_t *target_operation )
23562408
{
@@ -2685,14 +2737,8 @@ static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
26852737

26862738
if( key_length > block_size )
26872739
{
2688-
status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2689-
if( status != PSA_SUCCESS )
2690-
goto cleanup;
2691-
status = psa_hash_update( &hmac->hash_ctx, key, key_length );
2692-
if( status != PSA_SUCCESS )
2693-
goto cleanup;
2694-
status = psa_hash_finish( &hmac->hash_ctx,
2695-
ipad, sizeof( ipad ), &key_length );
2740+
status = psa_hash_compute( hash_alg, key, key_length,
2741+
ipad, sizeof( ipad ), &key_length );
26962742
if( status != PSA_SUCCESS )
26972743
goto cleanup;
26982744
}

tests/scripts/check-test-cases.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,30 @@
2020
#
2121
# This file is part of Mbed TLS (https://tls.mbed.org)
2222

23+
import argparse
2324
import glob
2425
import os
2526
import re
2627
import sys
2728

2829
class Results:
2930
"""Store file and line information about errors or warnings in test suites."""
30-
def __init__(self):
31+
32+
def __init__(self, options):
3133
self.errors = 0
3234
self.warnings = 0
35+
self.ignore_warnings = options.quiet
3336

3437
def error(self, file_name, line_number, fmt, *args):
3538
sys.stderr.write(('{}:{}:ERROR:' + fmt + '\n').
3639
format(file_name, line_number, *args))
3740
self.errors += 1
3841

3942
def warning(self, file_name, line_number, fmt, *args):
40-
sys.stderr.write(('{}:{}:Warning:' + fmt + '\n')
41-
.format(file_name, line_number, *args))
42-
self.warnings += 1
43+
if not self.ignore_warnings:
44+
sys.stderr.write(('{}:{}:Warning:' + fmt + '\n')
45+
.format(file_name, line_number, *args))
46+
self.warnings += 1
4347

4448
def collect_test_directories():
4549
"""Get the relative path for the TLS and Crypto test directories."""
@@ -108,16 +112,24 @@ def check_ssl_opt_sh(results, file_name):
108112
file_name, line_number, description)
109113

110114
def main():
115+
parser = argparse.ArgumentParser(description=__doc__)
116+
parser.add_argument('--quiet', '-q',
117+
action='store_true',
118+
help='Hide warnings')
119+
parser.add_argument('--verbose', '-v',
120+
action='store_false', dest='quiet',
121+
help='Show warnings (default: on; undoes --quiet)')
122+
options = parser.parse_args()
111123
test_directories = collect_test_directories()
112-
results = Results()
124+
results = Results(options)
113125
for directory in test_directories:
114126
for data_file_name in glob.glob(os.path.join(directory, 'suites',
115127
'*.data')):
116128
check_test_suite(results, data_file_name)
117129
ssl_opt_sh = os.path.join(directory, 'ssl-opt.sh')
118130
if os.path.exists(ssl_opt_sh):
119131
check_ssl_opt_sh(results, ssl_opt_sh)
120-
if results.warnings or results.errors:
132+
if (results.warnings or results.errors) and not options.quiet:
121133
sys.stderr.write('{}: {} errors, {} warnings\n'
122134
.format(sys.argv[0], results.errors, results.warnings))
123135
sys.exit(1 if results.errors else 0)

0 commit comments

Comments
 (0)