cipher.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Test driver for cipher functions
  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. #ifndef PSA_CRYPTO_TEST_DRIVERS_CIPHER_H
  20. #define PSA_CRYPTO_TEST_DRIVERS_CIPHER_H
  21. #include "mbedtls/build_info.h"
  22. #if defined(PSA_CRYPTO_DRIVER_TEST)
  23. #include <psa/crypto_driver_common.h>
  24. #include <psa/crypto.h>
  25. #include "mbedtls/cipher.h"
  26. typedef struct {
  27. /* If non-null, on success, copy this to the output. */
  28. void *forced_output;
  29. size_t forced_output_length;
  30. /* If not PSA_SUCCESS, return this error code instead of processing the
  31. * function call. */
  32. psa_status_t forced_status;
  33. /* Count the amount of times one of the cipher driver functions is called. */
  34. unsigned long hits;
  35. } mbedtls_test_driver_cipher_hooks_t;
  36. #define MBEDTLS_TEST_DRIVER_CIPHER_INIT { NULL, 0, PSA_SUCCESS, 0 }
  37. static inline mbedtls_test_driver_cipher_hooks_t
  38. mbedtls_test_driver_cipher_hooks_init(void)
  39. {
  40. const mbedtls_test_driver_cipher_hooks_t v = MBEDTLS_TEST_DRIVER_CIPHER_INIT;
  41. return v;
  42. }
  43. extern mbedtls_test_driver_cipher_hooks_t mbedtls_test_driver_cipher_hooks;
  44. psa_status_t mbedtls_test_transparent_cipher_encrypt(
  45. const psa_key_attributes_t *attributes,
  46. const uint8_t *key, size_t key_length,
  47. psa_algorithm_t alg,
  48. const uint8_t *iv, size_t iv_length,
  49. const uint8_t *input, size_t input_length,
  50. uint8_t *output, size_t output_size, size_t *output_length);
  51. psa_status_t mbedtls_test_transparent_cipher_decrypt(
  52. const psa_key_attributes_t *attributes,
  53. const uint8_t *key, size_t key_length,
  54. psa_algorithm_t alg,
  55. const uint8_t *input, size_t input_length,
  56. uint8_t *output, size_t output_size, size_t *output_length);
  57. psa_status_t mbedtls_test_transparent_cipher_encrypt_setup(
  58. mbedtls_transparent_test_driver_cipher_operation_t *operation,
  59. const psa_key_attributes_t *attributes,
  60. const uint8_t *key, size_t key_length,
  61. psa_algorithm_t alg);
  62. psa_status_t mbedtls_test_transparent_cipher_decrypt_setup(
  63. mbedtls_transparent_test_driver_cipher_operation_t *operation,
  64. const psa_key_attributes_t *attributes,
  65. const uint8_t *key, size_t key_length,
  66. psa_algorithm_t alg);
  67. psa_status_t mbedtls_test_transparent_cipher_abort(
  68. mbedtls_transparent_test_driver_cipher_operation_t *operation);
  69. psa_status_t mbedtls_test_transparent_cipher_set_iv(
  70. mbedtls_transparent_test_driver_cipher_operation_t *operation,
  71. const uint8_t *iv, size_t iv_length);
  72. psa_status_t mbedtls_test_transparent_cipher_update(
  73. mbedtls_transparent_test_driver_cipher_operation_t *operation,
  74. const uint8_t *input, size_t input_length,
  75. uint8_t *output, size_t output_size, size_t *output_length);
  76. psa_status_t mbedtls_test_transparent_cipher_finish(
  77. mbedtls_transparent_test_driver_cipher_operation_t *operation,
  78. uint8_t *output, size_t output_size, size_t *output_length);
  79. /*
  80. * opaque versions
  81. */
  82. psa_status_t mbedtls_test_opaque_cipher_encrypt(
  83. const psa_key_attributes_t *attributes,
  84. const uint8_t *key, size_t key_length,
  85. psa_algorithm_t alg,
  86. const uint8_t *iv, size_t iv_length,
  87. const uint8_t *input, size_t input_length,
  88. uint8_t *output, size_t output_size, size_t *output_length);
  89. psa_status_t mbedtls_test_opaque_cipher_decrypt(
  90. const psa_key_attributes_t *attributes,
  91. const uint8_t *key, size_t key_length,
  92. psa_algorithm_t alg,
  93. const uint8_t *input, size_t input_length,
  94. uint8_t *output, size_t output_size, size_t *output_length);
  95. psa_status_t mbedtls_test_opaque_cipher_encrypt_setup(
  96. mbedtls_opaque_test_driver_cipher_operation_t *operation,
  97. const psa_key_attributes_t *attributes,
  98. const uint8_t *key, size_t key_length,
  99. psa_algorithm_t alg);
  100. psa_status_t mbedtls_test_opaque_cipher_decrypt_setup(
  101. mbedtls_opaque_test_driver_cipher_operation_t *operation,
  102. const psa_key_attributes_t *attributes,
  103. const uint8_t *key, size_t key_length,
  104. psa_algorithm_t alg);
  105. psa_status_t mbedtls_test_opaque_cipher_abort(
  106. mbedtls_opaque_test_driver_cipher_operation_t *operation);
  107. psa_status_t mbedtls_test_opaque_cipher_set_iv(
  108. mbedtls_opaque_test_driver_cipher_operation_t *operation,
  109. const uint8_t *iv, size_t iv_length);
  110. psa_status_t mbedtls_test_opaque_cipher_update(
  111. mbedtls_opaque_test_driver_cipher_operation_t *operation,
  112. const uint8_t *input, size_t input_length,
  113. uint8_t *output, size_t output_size, size_t *output_length);
  114. psa_status_t mbedtls_test_opaque_cipher_finish(
  115. mbedtls_opaque_test_driver_cipher_operation_t *operation,
  116. uint8_t *output, size_t output_size, size_t *output_length);
  117. #endif /* PSA_CRYPTO_DRIVER_TEST */
  118. #endif /* PSA_CRYPTO_TEST_DRIVERS_CIPHER_H */