Ronald Cron | fa036c8 | 2021-03-23 09:33:25 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Test driver for hash entry points. |
| 3 | */ |
| 4 | /* Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 21 | #include "mbedtls/config.h" |
| 22 | #else |
| 23 | #include MBEDTLS_CONFIG_FILE |
| 24 | #endif |
| 25 | |
| 26 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) |
| 27 | #include "psa_crypto_hash.h" |
| 28 | |
| 29 | #include "test/drivers/hash.h" |
| 30 | |
| 31 | test_driver_hash_hooks_t test_driver_hash_hooks = TEST_DRIVER_HASH_INIT; |
| 32 | |
| 33 | psa_status_t test_transparent_hash_compute( |
| 34 | psa_algorithm_t alg, |
| 35 | const uint8_t *input, size_t input_length, |
| 36 | uint8_t *hash, size_t hash_size, size_t *hash_length ) |
| 37 | { |
| 38 | test_driver_hash_hooks.hits++; |
| 39 | |
| 40 | if( test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
| 41 | { |
| 42 | test_driver_hash_hooks.driver_status = |
| 43 | test_driver_hash_hooks.forced_status; |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | test_driver_hash_hooks.driver_status = |
| 48 | mbedtls_transparent_test_driver_hash_compute( |
| 49 | alg, input, input_length, |
| 50 | hash, hash_size, hash_length ); |
| 51 | } |
| 52 | |
| 53 | return( test_driver_hash_hooks.driver_status ); |
| 54 | } |
| 55 | |
| 56 | psa_status_t test_transparent_hash_setup( |
| 57 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 58 | psa_algorithm_t alg ) |
| 59 | { |
| 60 | test_driver_hash_hooks.hits++; |
| 61 | |
| 62 | if( test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
| 63 | { |
| 64 | test_driver_hash_hooks.driver_status = |
| 65 | test_driver_hash_hooks.forced_status; |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | test_driver_hash_hooks.driver_status = |
| 70 | mbedtls_transparent_test_driver_hash_setup( operation, alg ); |
| 71 | } |
| 72 | |
| 73 | return( test_driver_hash_hooks.driver_status ); |
| 74 | } |
| 75 | |
| 76 | psa_status_t test_transparent_hash_clone( |
| 77 | const mbedtls_transparent_test_driver_hash_operation_t *source_operation, |
| 78 | mbedtls_transparent_test_driver_hash_operation_t *target_operation ) |
| 79 | { |
| 80 | test_driver_hash_hooks.hits++; |
| 81 | |
| 82 | if( test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
| 83 | { |
| 84 | test_driver_hash_hooks.driver_status = |
| 85 | test_driver_hash_hooks.forced_status; |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | test_driver_hash_hooks.driver_status = |
| 90 | mbedtls_transparent_test_driver_hash_clone( source_operation, |
| 91 | target_operation ); |
| 92 | } |
| 93 | |
| 94 | return( test_driver_hash_hooks.driver_status ); |
| 95 | } |
| 96 | |
| 97 | psa_status_t test_transparent_hash_update( |
| 98 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 99 | const uint8_t *input, |
| 100 | size_t input_length ) |
| 101 | { |
| 102 | test_driver_hash_hooks.hits++; |
| 103 | |
| 104 | if( test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
| 105 | { |
| 106 | test_driver_hash_hooks.driver_status = |
| 107 | test_driver_hash_hooks.forced_status; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | test_driver_hash_hooks.driver_status = |
| 112 | mbedtls_transparent_test_driver_hash_update( |
| 113 | operation, input, input_length ); |
| 114 | } |
| 115 | |
| 116 | return( test_driver_hash_hooks.driver_status ); |
| 117 | } |
| 118 | |
| 119 | psa_status_t test_transparent_hash_finish( |
| 120 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 121 | uint8_t *hash, |
| 122 | size_t hash_size, |
| 123 | size_t *hash_length ) |
| 124 | { |
| 125 | test_driver_hash_hooks.hits++; |
| 126 | |
| 127 | if( test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
| 128 | { |
| 129 | test_driver_hash_hooks.driver_status = |
| 130 | test_driver_hash_hooks.forced_status; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | test_driver_hash_hooks.driver_status = |
| 135 | mbedtls_transparent_test_driver_hash_finish( |
| 136 | operation, hash, hash_size, hash_length ); |
| 137 | } |
| 138 | |
| 139 | return( test_driver_hash_hooks.driver_status ); |
| 140 | } |
| 141 | |
| 142 | psa_status_t test_transparent_hash_abort( |
| 143 | mbedtls_transparent_test_driver_hash_operation_t *operation ) |
| 144 | { |
| 145 | test_driver_hash_hooks.hits++; |
| 146 | |
| 147 | if( test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
| 148 | { |
| 149 | test_driver_hash_hooks.driver_status = |
| 150 | test_driver_hash_hooks.forced_status; |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | test_driver_hash_hooks.driver_status = |
| 155 | mbedtls_transparent_test_driver_hash_abort( operation ); |
| 156 | } |
| 157 | |
| 158 | return( test_driver_hash_hooks.driver_status ); |
| 159 | } |
| 160 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |