test_suite_psa_its.function 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* BEGIN_HEADER */
  2. /* This test file is specific to the ITS implementation in PSA Crypto
  3. * on top of stdio. It expects to know what the stdio name of a file is
  4. * based on its keystore name.
  5. */
  6. #include "../library/psa_crypto_its.h"
  7. #include "test/psa_helpers.h"
  8. /* Internal definitions of the implementation, copied for the sake of
  9. * some of the tests and of the cleanup code. */
  10. #define PSA_ITS_STORAGE_PREFIX ""
  11. #define PSA_ITS_STORAGE_FILENAME_PATTERN "%08lx%08lx"
  12. #define PSA_ITS_STORAGE_SUFFIX ".psa_its"
  13. #define PSA_ITS_STORAGE_FILENAME_LENGTH \
  14. ( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \
  15. 16 + /*UID (64-bit number in hex)*/ \
  16. 16 + /*UID (64-bit number in hex)*/ \
  17. sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \
  18. 1 /*terminating null byte*/ )
  19. #define PSA_ITS_STORAGE_TEMP \
  20. PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
  21. static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename )
  22. {
  23. /* Break up the UID into two 32-bit pieces so as not to rely on
  24. * long long support in snprintf. */
  25. mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
  26. "%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
  27. PSA_ITS_STORAGE_PREFIX,
  28. (unsigned long) ( uid >> 32 ),
  29. (unsigned long) ( uid & 0xffffffff ),
  30. PSA_ITS_STORAGE_SUFFIX );
  31. }
  32. /* Maximum uid used by the test, recorded so that cleanup() can delete
  33. * all files. 0xffffffffffffffff is always cleaned up, so it does not
  34. * need to and should not be taken into account for uid_max. */
  35. static psa_storage_uid_t uid_max = 0;
  36. static void cleanup( void )
  37. {
  38. /* Call remove() on all the files that a test might have created.
  39. * We ignore the error if the file exists but remove() fails because
  40. * it can't be checked portably (except by attempting to open the file
  41. * first, which is needlessly slow and complicated here). A failure of
  42. * remove() on an existing file is very unlikely anyway and would not
  43. * have significant consequences other than perhaps failing the next
  44. * test case. */
  45. char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
  46. psa_storage_uid_t uid;
  47. for( uid = 0; uid < uid_max; uid++ )
  48. {
  49. psa_its_fill_filename( uid, filename );
  50. (void) remove( filename );
  51. }
  52. psa_its_fill_filename( (psa_storage_uid_t)( -1 ), filename );
  53. (void) remove( filename );
  54. (void) remove( PSA_ITS_STORAGE_TEMP );
  55. uid_max = 0;
  56. }
  57. static psa_status_t psa_its_set_wrap( psa_storage_uid_t uid,
  58. uint32_t data_length,
  59. const void *p_data,
  60. psa_storage_create_flags_t create_flags )
  61. {
  62. if( uid_max != (psa_storage_uid_t)( -1 ) && uid_max < uid )
  63. uid_max = uid;
  64. return( psa_its_set( uid, data_length, p_data, create_flags ) );
  65. }
  66. /* END_HEADER */
  67. /* BEGIN_DEPENDENCIES
  68. * depends_on:MBEDTLS_PSA_ITS_FILE_C
  69. * END_DEPENDENCIES
  70. */
  71. /* BEGIN_CASE */
  72. void set_get_remove( int uid_arg, int flags_arg, data_t *data )
  73. {
  74. psa_storage_uid_t uid = uid_arg;
  75. uint32_t flags = flags_arg;
  76. struct psa_storage_info_t info;
  77. unsigned char *buffer = NULL;
  78. size_t ret_len = 0;
  79. ASSERT_ALLOC( buffer, data->len );
  80. PSA_ASSERT( psa_its_set_wrap( uid, data->len, data->x, flags ) );
  81. PSA_ASSERT( psa_its_get_info( uid, &info ) );
  82. TEST_ASSERT( info.size == data->len );
  83. TEST_ASSERT( info.flags == flags );
  84. PSA_ASSERT( psa_its_get( uid, 0, data->len, buffer, &ret_len ) );
  85. ASSERT_COMPARE( data->x, data->len, buffer, ret_len );
  86. PSA_ASSERT( psa_its_remove( uid ) );
  87. exit:
  88. mbedtls_free( buffer );
  89. cleanup( );
  90. }
  91. /* END_CASE */
  92. /* BEGIN_CASE */
  93. void set_overwrite( int uid_arg,
  94. int flags1_arg, data_t *data1,
  95. int flags2_arg, data_t *data2 )
  96. {
  97. psa_storage_uid_t uid = uid_arg;
  98. uint32_t flags1 = flags1_arg;
  99. uint32_t flags2 = flags2_arg;
  100. struct psa_storage_info_t info;
  101. unsigned char *buffer = NULL;
  102. size_t ret_len = 0;
  103. ASSERT_ALLOC( buffer, MAX( data1->len, data2->len ) );
  104. PSA_ASSERT( psa_its_set_wrap( uid, data1->len, data1->x, flags1 ) );
  105. PSA_ASSERT( psa_its_get_info( uid, &info ) );
  106. TEST_ASSERT( info.size == data1->len );
  107. TEST_ASSERT( info.flags == flags1 );
  108. PSA_ASSERT( psa_its_get( uid, 0, data1->len, buffer, &ret_len ) );
  109. ASSERT_COMPARE( data1->x, data1->len, buffer, ret_len );
  110. PSA_ASSERT( psa_its_set_wrap( uid, data2->len, data2->x, flags2 ) );
  111. PSA_ASSERT( psa_its_get_info( uid, &info ) );
  112. TEST_ASSERT( info.size == data2->len );
  113. TEST_ASSERT( info.flags == flags2 );
  114. ret_len = 0;
  115. PSA_ASSERT( psa_its_get( uid, 0, data2->len, buffer, &ret_len ) );
  116. ASSERT_COMPARE( data2->x, data2->len, buffer, ret_len );
  117. PSA_ASSERT( psa_its_remove( uid ) );
  118. exit:
  119. mbedtls_free( buffer );
  120. cleanup( );
  121. }
  122. /* END_CASE */
  123. /* BEGIN_CASE */
  124. void set_multiple( int first_id, int count )
  125. {
  126. psa_storage_uid_t uid0 = first_id;
  127. psa_storage_uid_t uid;
  128. char stored[40];
  129. char retrieved[40];
  130. size_t ret_len = 0;
  131. memset( stored, '.', sizeof( stored ) );
  132. for( uid = uid0; uid < uid0 + count; uid++ )
  133. {
  134. mbedtls_snprintf( stored, sizeof( stored ),
  135. "Content of file 0x%08lx", (unsigned long) uid );
  136. PSA_ASSERT( psa_its_set_wrap( uid, sizeof( stored ), stored, 0 ) );
  137. }
  138. for( uid = uid0; uid < uid0 + count; uid++ )
  139. {
  140. mbedtls_snprintf( stored, sizeof( stored ),
  141. "Content of file 0x%08lx", (unsigned long) uid );
  142. PSA_ASSERT( psa_its_get( uid, 0, sizeof( stored ), retrieved, &ret_len ) );
  143. ASSERT_COMPARE( retrieved, ret_len,
  144. stored, sizeof( stored ) );
  145. PSA_ASSERT( psa_its_remove( uid ) );
  146. TEST_ASSERT( psa_its_get( uid, 0, 0, NULL, NULL ) ==
  147. PSA_ERROR_DOES_NOT_EXIST );
  148. }
  149. exit:
  150. cleanup( );
  151. }
  152. /* END_CASE */
  153. /* BEGIN_CASE */
  154. void nonexistent( int uid_arg, int create_and_remove )
  155. {
  156. psa_storage_uid_t uid = uid_arg;
  157. struct psa_storage_info_t info;
  158. if( create_and_remove )
  159. {
  160. PSA_ASSERT( psa_its_set_wrap( uid, 0, NULL, 0 ) );
  161. PSA_ASSERT( psa_its_remove( uid ) );
  162. }
  163. TEST_ASSERT( psa_its_remove( uid ) == PSA_ERROR_DOES_NOT_EXIST );
  164. TEST_ASSERT( psa_its_get_info( uid, &info ) ==
  165. PSA_ERROR_DOES_NOT_EXIST );
  166. TEST_ASSERT( psa_its_get( uid, 0, 0, NULL, NULL ) ==
  167. PSA_ERROR_DOES_NOT_EXIST );
  168. exit:
  169. cleanup( );
  170. }
  171. /* END_CASE */
  172. /* BEGIN_CASE */
  173. void get_at( int uid_arg, data_t *data,
  174. int offset, int length_arg,
  175. int expected_status )
  176. {
  177. psa_storage_uid_t uid = uid_arg;
  178. unsigned char *buffer = NULL;
  179. psa_status_t status;
  180. size_t length = length_arg >= 0 ? length_arg : 0;
  181. unsigned char *trailer;
  182. size_t i;
  183. size_t ret_len = 0;
  184. ASSERT_ALLOC( buffer, length + 16 );
  185. trailer = buffer + length;
  186. memset( trailer, '-', 16 );
  187. PSA_ASSERT( psa_its_set_wrap( uid, data->len, data->x, 0 ) );
  188. status = psa_its_get( uid, offset, length_arg, buffer, &ret_len );
  189. TEST_ASSERT( status == (psa_status_t) expected_status );
  190. if( status == PSA_SUCCESS )
  191. ASSERT_COMPARE( data->x + offset, (size_t) length_arg,
  192. buffer, ret_len );
  193. for( i = 0; i < 16; i++ )
  194. TEST_ASSERT( trailer[i] == '-' );
  195. PSA_ASSERT( psa_its_remove( uid ) );
  196. exit:
  197. mbedtls_free( buffer );
  198. cleanup( );
  199. }
  200. /* END_CASE */
  201. /* BEGIN_CASE */
  202. void get_fail( int uid_arg, data_t *data,
  203. int overwrite_magic, int cut_header,
  204. int expected_status )
  205. {
  206. psa_storage_uid_t uid = uid_arg;
  207. unsigned char *buffer = NULL;
  208. psa_status_t status;
  209. size_t n;
  210. size_t ret_len = 0;
  211. char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
  212. FILE *stream = NULL;
  213. char bad_char = 'X';
  214. PSA_ASSERT( psa_its_set_wrap( uid, data->len, data->x, 0 ) );
  215. psa_its_fill_filename( uid, filename );
  216. stream = fopen( filename, "rb+" );
  217. TEST_ASSERT( NULL != stream );
  218. if( 0 != overwrite_magic )
  219. {
  220. /* Overwrite the 1st byte of the file, the ITS magic number */
  221. TEST_ASSERT( fseek( stream, 0, SEEK_SET ) == 0 );
  222. n = fwrite( &bad_char, 1, 1, stream );
  223. TEST_ASSERT( 1 == n );
  224. }
  225. if( 0 != cut_header )
  226. {
  227. /* Reopen file and truncate it to 0 byte by specifying the 'w' flag */
  228. stream = freopen( filename, "wb", stream );
  229. TEST_ASSERT( NULL != stream );
  230. }
  231. fclose( stream );
  232. stream = NULL;
  233. status = psa_its_get( uid, 0, 0, buffer, &ret_len );
  234. TEST_ASSERT( status == (psa_status_t) expected_status );
  235. TEST_ASSERT( 0 == ret_len );
  236. PSA_ASSERT( psa_its_remove( uid ) );
  237. /* Check if the file is really deleted. */
  238. stream = fopen( filename, "rb" );
  239. TEST_ASSERT( NULL == stream );
  240. exit:
  241. if( stream != NULL )
  242. fclose( stream );
  243. mbedtls_free( buffer );
  244. cleanup( );
  245. }
  246. /* END_CASE */