Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Test driver for cipher functions. |
| 3 | * Currently only supports multi-part operations using AES-CTR. |
| 4 | */ |
| 5 | /* Copyright (C) 2020, ARM Limited, All Rights Reserved |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * |
| 20 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 21 | */ |
| 22 | |
| 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 24 | #include "mbedtls/config.h" |
| 25 | #else |
| 26 | #include MBEDTLS_CONFIG_FILE |
| 27 | #endif |
| 28 | |
| 29 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) |
| 30 | #include "psa/crypto.h" |
| 31 | #include "mbedtls/cipher.h" |
| 32 | |
| 33 | #include "drivers/cipher.h" |
| 34 | |
| 35 | #include "test/random.h" |
| 36 | |
| 37 | #include <string.h> |
| 38 | |
| 39 | /* If non-null, on success, copy this to the output. */ |
| 40 | void *test_driver_cipher_forced_output = NULL; |
| 41 | size_t test_driver_cipher_forced_output_length = 0; |
| 42 | |
| 43 | psa_status_t test_transparent_cipher_status = PSA_ERROR_NOT_SUPPORTED; |
| 44 | unsigned long test_transparent_cipher_hit = 0; |
| 45 | |
| 46 | psa_status_t test_transparent_cipher_encrypt( |
| 47 | const psa_key_attributes_t *attributes, |
| 48 | const uint8_t *key, size_t key_length, |
| 49 | psa_algorithm_t alg, |
| 50 | const uint8_t *input, size_t input_length, |
| 51 | uint8_t *output, size_t output_size, size_t *output_length) |
| 52 | { |
| 53 | (void) attributes; |
| 54 | (void) key; |
| 55 | (void) key_length; |
| 56 | (void) alg; |
| 57 | (void) input; |
| 58 | (void) input_length; |
| 59 | test_transparent_cipher_hit++; |
| 60 | |
| 61 | if( test_transparent_cipher_status != PSA_SUCCESS ) |
| 62 | return test_transparent_cipher_status; |
| 63 | if( output_size < test_driver_cipher_forced_output_length ) |
| 64 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 65 | |
| 66 | memcpy(output, test_driver_cipher_forced_output, test_driver_cipher_forced_output_length); |
| 67 | *output_length = test_driver_cipher_forced_output_length; |
| 68 | |
| 69 | return test_transparent_cipher_status; |
| 70 | } |
| 71 | |
| 72 | psa_status_t test_transparent_cipher_decrypt( |
| 73 | const psa_key_attributes_t *attributes, |
| 74 | const uint8_t *key, size_t key_length, |
| 75 | psa_algorithm_t alg, |
| 76 | const uint8_t *input, size_t input_length, |
| 77 | uint8_t *output, size_t output_size, size_t *output_length) |
| 78 | { |
| 79 | (void) attributes; |
| 80 | (void) key; |
| 81 | (void) key_length; |
| 82 | (void) alg; |
| 83 | (void) input; |
| 84 | (void) input_length; |
| 85 | test_transparent_cipher_hit++; |
| 86 | |
| 87 | if( test_transparent_cipher_status != PSA_SUCCESS ) |
| 88 | return test_transparent_cipher_status; |
| 89 | if( output_size < test_driver_cipher_forced_output_length ) |
| 90 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 91 | |
| 92 | memcpy(output, test_driver_cipher_forced_output, test_driver_cipher_forced_output_length); |
| 93 | *output_length = test_driver_cipher_forced_output_length; |
| 94 | |
| 95 | return test_transparent_cipher_status; |
| 96 | } |
| 97 | |
| 98 | psa_status_t test_transparent_cipher_encrypt_setup( |
| 99 | test_transparent_cipher_operation_t *operation, |
| 100 | const psa_key_attributes_t *attributes, |
| 101 | const uint8_t *key, size_t key_length, |
| 102 | psa_algorithm_t alg) |
| 103 | { |
| 104 | (void) attributes; |
| 105 | (void) key; |
| 106 | (void) key_length; |
| 107 | (void) alg; |
| 108 | |
| 109 | /* write our struct, this will trigger memory corruption failures |
| 110 | * in test when we go outside of bounds. */ |
| 111 | memset(operation, 0, sizeof(test_transparent_cipher_operation_t)); |
| 112 | |
| 113 | test_transparent_cipher_hit++; |
| 114 | return test_transparent_cipher_status; |
| 115 | } |
| 116 | |
| 117 | psa_status_t test_transparent_cipher_decrypt_setup( |
| 118 | test_transparent_cipher_operation_t *operation, |
| 119 | const psa_key_attributes_t *attributes, |
| 120 | const uint8_t *key, size_t key_length, |
| 121 | psa_algorithm_t alg) |
| 122 | { |
| 123 | (void) attributes; |
| 124 | (void) key; |
| 125 | (void) key_length; |
| 126 | (void) alg; |
| 127 | |
| 128 | /* write our struct, this will trigger memory corruption failures |
| 129 | * in test when we go outside of bounds. */ |
| 130 | memset(operation, 0, sizeof(test_transparent_cipher_operation_t)); |
| 131 | |
| 132 | test_transparent_cipher_hit++; |
| 133 | return test_transparent_cipher_status; |
| 134 | } |
| 135 | |
| 136 | psa_status_t test_transparent_cipher_abort( |
| 137 | test_transparent_cipher_operation_t *operation) |
| 138 | { |
| 139 | /* write our struct, this will trigger memory corruption failures |
| 140 | * in test when we go outside of bounds. */ |
| 141 | memset(operation, 0, sizeof(test_transparent_cipher_operation_t)); |
| 142 | |
| 143 | test_transparent_cipher_hit++; |
| 144 | return test_transparent_cipher_status; |
| 145 | } |
| 146 | |
| 147 | psa_status_t test_transparent_cipher_generate_iv( |
| 148 | test_transparent_cipher_operation_t *operation, |
| 149 | uint8_t *iv, |
| 150 | size_t iv_size, |
| 151 | size_t *iv_length) |
| 152 | { |
| 153 | (void) operation; |
| 154 | (void) iv; |
| 155 | (void) iv_size; |
| 156 | (void) iv_length; |
| 157 | |
| 158 | test_transparent_cipher_hit++; |
| 159 | return test_transparent_cipher_status; |
| 160 | } |
| 161 | |
| 162 | psa_status_t test_transparent_cipher_set_iv( |
| 163 | test_transparent_cipher_operation_t *operation, |
| 164 | const uint8_t *iv, |
| 165 | size_t iv_length) |
| 166 | { |
| 167 | (void) operation; |
| 168 | (void) iv; |
| 169 | (void) iv_length; |
| 170 | |
| 171 | test_transparent_cipher_hit++; |
| 172 | return test_transparent_cipher_status; |
| 173 | } |
| 174 | |
| 175 | psa_status_t test_transparent_cipher_update( |
| 176 | test_transparent_cipher_operation_t *operation, |
| 177 | const uint8_t *input, |
| 178 | size_t input_length, |
| 179 | uint8_t *output, |
| 180 | size_t output_size, |
| 181 | size_t *output_length) |
| 182 | { |
| 183 | (void) operation; |
| 184 | (void) input; |
| 185 | (void) input_length; |
| 186 | test_transparent_cipher_hit++; |
| 187 | |
| 188 | if( test_transparent_cipher_status != PSA_SUCCESS ) |
| 189 | return test_transparent_cipher_status; |
| 190 | if( output_size < test_driver_cipher_forced_output_length ) |
| 191 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 192 | |
| 193 | memcpy(output, test_driver_cipher_forced_output, test_driver_cipher_forced_output_length); |
| 194 | *output_length = test_driver_cipher_forced_output_length; |
| 195 | |
| 196 | return test_transparent_cipher_status; |
| 197 | } |
| 198 | |
| 199 | psa_status_t test_transparent_cipher_finish( |
| 200 | test_transparent_cipher_operation_t *operation, |
| 201 | uint8_t *output, |
| 202 | size_t output_size, |
| 203 | size_t *output_length) |
| 204 | { |
| 205 | (void) operation; |
| 206 | test_transparent_cipher_hit++; |
| 207 | |
| 208 | if( test_transparent_cipher_status != PSA_SUCCESS ) |
| 209 | return test_transparent_cipher_status; |
| 210 | if( output_size < test_driver_cipher_forced_output_length ) |
| 211 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 212 | |
| 213 | memcpy(output, test_driver_cipher_forced_output, test_driver_cipher_forced_output_length); |
| 214 | *output_length = test_driver_cipher_forced_output_length; |
| 215 | |
| 216 | return test_transparent_cipher_status; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * opaque versions, to do |
| 221 | */ |
| 222 | psa_status_t test_opaque_cipher_encrypt( |
| 223 | const psa_key_attributes_t *attributes, |
| 224 | const uint8_t *key, size_t key_length, |
| 225 | psa_algorithm_t alg, |
| 226 | const uint8_t *input, size_t input_length, |
| 227 | uint8_t *output, size_t output_size, size_t *output_length) |
| 228 | { |
| 229 | (void) attributes; |
| 230 | (void) key; |
| 231 | (void) key_length; |
| 232 | (void) alg; |
| 233 | (void) input; |
| 234 | (void) input_length; |
| 235 | (void) output; |
| 236 | (void) output_size; |
| 237 | (void) output_length; |
| 238 | return PSA_ERROR_NOT_SUPPORTED; |
| 239 | } |
| 240 | |
| 241 | psa_status_t test_opaque_cipher_decrypt( |
| 242 | const psa_key_attributes_t *attributes, |
| 243 | const uint8_t *key, size_t key_length, |
| 244 | psa_algorithm_t alg, |
| 245 | const uint8_t *input, size_t input_length, |
| 246 | uint8_t *output, size_t output_size, size_t *output_length) |
| 247 | { |
| 248 | (void) attributes; |
| 249 | (void) key; |
| 250 | (void) key_length; |
| 251 | (void) alg; |
| 252 | (void) input; |
| 253 | (void) input_length; |
| 254 | (void) output; |
| 255 | (void) output_size; |
| 256 | (void) output_length; |
| 257 | return PSA_ERROR_NOT_SUPPORTED; |
| 258 | } |
| 259 | |
| 260 | psa_status_t test_opaque_cipher_encrypt_setup( |
| 261 | test_opaque_cipher_operation_t *operation, |
| 262 | const psa_key_attributes_t *attributes, |
| 263 | const uint8_t *key, size_t key_length, |
| 264 | psa_algorithm_t alg) |
| 265 | { |
| 266 | (void) operation; |
| 267 | (void) attributes; |
| 268 | (void) key; |
| 269 | (void) key_length; |
| 270 | (void) alg; |
| 271 | return PSA_ERROR_NOT_SUPPORTED; |
| 272 | } |
| 273 | |
| 274 | psa_status_t test_opaque_cipher_decrypt_setup( |
| 275 | test_opaque_cipher_operation_t *operation, |
| 276 | const psa_key_attributes_t *attributes, |
| 277 | const uint8_t *key, size_t key_length, |
| 278 | psa_algorithm_t alg) |
| 279 | { |
| 280 | (void) operation; |
| 281 | (void) attributes; |
| 282 | (void) key; |
| 283 | (void) key_length; |
| 284 | (void) alg; |
| 285 | return PSA_ERROR_NOT_SUPPORTED; |
| 286 | } |
| 287 | |
| 288 | psa_status_t test_opaque_cipher_abort( |
| 289 | test_opaque_cipher_operation_t *operation) |
| 290 | { |
| 291 | (void) operation; |
| 292 | return PSA_ERROR_NOT_SUPPORTED; |
| 293 | } |
| 294 | |
| 295 | psa_status_t test_opaque_cipher_generate_iv( |
| 296 | test_opaque_cipher_operation_t *operation, |
| 297 | uint8_t *iv, |
| 298 | size_t iv_size, |
| 299 | size_t *iv_length) |
| 300 | { |
| 301 | (void) operation; |
| 302 | (void) iv; |
| 303 | (void) iv_size; |
| 304 | (void) iv_length; |
| 305 | return PSA_ERROR_NOT_SUPPORTED; |
| 306 | } |
| 307 | |
| 308 | psa_status_t test_opaque_cipher_set_iv( |
| 309 | test_opaque_cipher_operation_t *operation, |
| 310 | const uint8_t *iv, |
| 311 | size_t iv_length) |
| 312 | { |
| 313 | (void) operation; |
| 314 | (void) iv; |
| 315 | (void) iv_length; |
| 316 | return PSA_ERROR_NOT_SUPPORTED; |
| 317 | } |
| 318 | |
| 319 | psa_status_t test_opaque_cipher_update( |
| 320 | test_opaque_cipher_operation_t *operation, |
| 321 | const uint8_t *input, |
| 322 | size_t input_length, |
| 323 | uint8_t *output, |
| 324 | size_t output_size, |
| 325 | size_t *output_length) |
| 326 | { |
| 327 | (void) operation; |
| 328 | (void) input; |
| 329 | (void) input_length; |
| 330 | (void) output; |
| 331 | (void) output_size; |
| 332 | (void) output_length; |
| 333 | return PSA_ERROR_NOT_SUPPORTED; |
| 334 | } |
| 335 | |
| 336 | psa_status_t test_opaque_cipher_finish( |
| 337 | test_opaque_cipher_operation_t *operation, |
| 338 | uint8_t *output, |
| 339 | size_t output_size, |
| 340 | size_t *output_length) |
| 341 | { |
| 342 | (void) operation; |
| 343 | (void) output; |
| 344 | (void) output_size; |
| 345 | (void) output_length; |
| 346 | return PSA_ERROR_NOT_SUPPORTED; |
| 347 | } |
| 348 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |