1/* BEGIN_HEADER */ 2#include "block_cipher_internal.h" 3 4#define BLOCK_SIZE 16 5 6#if defined(MBEDTLS_AES_C) 7#define VALID_CIPHER_ID MBEDTLS_CIPHER_ID_AES 8#define BADKEY_ERROR MBEDTLS_ERR_AES_INVALID_KEY_LENGTH 9#elif defined(MBEDTLS_ARIA_C) 10#define VALID_CIPHER_ID MBEDTLS_CIPHER_ID_ARIA 11#define BADKEY_ERROR MBEDTLS_ERR_ARIA_BAD_INPUT_DATA 12#elif defined(MBEDTLS_CAMELLIA_C) 13#define VALID_CIPHER_ID MBEDTLS_CIPHER_ID_CAMELLIA 14#define BADKEY_ERROR MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA 15#else 16#undef VALID_CIPHER_ID 17#endif 18/* END_HEADER */ 19 20/* BEGIN_DEPENDENCIES 21 * depends_on:MBEDTLS_BLOCK_CIPHER_C 22 * END_DEPENDENCIES 23 */ 24 25/* BEGIN_CASE depends_on:VALID_CIPHER_ID */ 26void invalid() 27{ 28 /* That size is valid for a key or an input/output block. */ 29 unsigned char buf[16] = { 0 }; 30 31 mbedtls_block_cipher_context_t ctx; 32 33 mbedtls_block_cipher_init(&ctx); 34 35 /* Bad parameters to setup */ 36 TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, 37 mbedtls_block_cipher_setup(&ctx, MBEDTLS_CIPHER_ID_NONE)); 38 TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, 39 mbedtls_block_cipher_setup(&ctx, MBEDTLS_CIPHER_ID_DES)); 40 41 /* setkey() before successful setup() */ 42 TEST_EQUAL(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT, 43 mbedtls_block_cipher_setkey(&ctx, buf, 128)); 44 45 /* encrypt() before successful setup() */ 46 TEST_EQUAL(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT, 47 mbedtls_block_cipher_encrypt(&ctx, buf, buf)); 48 49 /* free() before successful setup() 50 * No return value to check, but shouldn't cause memory errors. */ 51 mbedtls_block_cipher_free(&ctx); 52 53 /* Now properly setup the context */ 54 mbedtls_block_cipher_init(&ctx); 55 TEST_EQUAL(0, mbedtls_block_cipher_setup(&ctx, VALID_CIPHER_ID)); 56 57 /* Bad parameters to setkey() */ 58 TEST_EQUAL(BADKEY_ERROR, 59 mbedtls_block_cipher_setkey(&ctx, buf, 42)); 60 61exit: 62 mbedtls_block_cipher_free(&ctx); 63} 64/* END_CASE */ 65 66/* BEGIN_CASE */ 67void test_vec(int cipher_id_arg, data_t *key, data_t *input, data_t *outref) 68{ 69 mbedtls_block_cipher_context_t ctx; 70 mbedtls_cipher_id_t cipher_id = cipher_id_arg; 71 unsigned char output[BLOCK_SIZE]; 72 73 mbedtls_block_cipher_init(&ctx); 74 75 memset(output, 0x00, sizeof(output)); 76 77 TEST_EQUAL(0, mbedtls_block_cipher_setup(&ctx, cipher_id)); 78 TEST_EQUAL(0, mbedtls_block_cipher_setkey(&ctx, key->x, 8 * key->len)); 79 80 /* Encrypt with input != output */ 81 TEST_EQUAL(0, mbedtls_block_cipher_encrypt(&ctx, input->x, output)); 82 ASSERT_COMPARE(output, BLOCK_SIZE, outref->x, outref->len); 83 84 /* Encrypt with input == output. 85 * (Also, encrypting again ensures the previous call to encrypt() 86 * did not change the state of the context.) */ 87 memcpy(output, input->x, BLOCK_SIZE); 88 TEST_EQUAL(0, mbedtls_block_cipher_encrypt(&ctx, output, output)); 89 ASSERT_COMPARE(output, BLOCK_SIZE, outref->x, outref->len); 90 91exit: 92 mbedtls_block_cipher_free(&ctx); 93} 94/* END_CASE */ 95 96/* BEGIN_CASE */ 97void block_cipher_psa_dynamic_dispatch(int cipher_type, int pre_psa_ret, int post_psa_engine) 98{ 99 mbedtls_block_cipher_context_t ctx; 100 (void) post_psa_engine; 101 102 /* Intentionally no PSA init here! (Will be done later.) */ 103 104 mbedtls_block_cipher_init(&ctx); 105 106 /* Before PSA crypto init */ 107 TEST_EQUAL(pre_psa_ret, mbedtls_block_cipher_setup(&ctx, cipher_type)); 108 109#if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA) 110 TEST_EQUAL(ctx.engine, MBEDTLS_BLOCK_CIPHER_ENGINE_LEGACY); 111#endif 112 113 mbedtls_block_cipher_free(&ctx); 114 115 /* Now initilize PSA Crypto */ 116 BLOCK_CIPHER_PSA_INIT(); 117 118 mbedtls_block_cipher_init(&ctx); 119 /* After PSA Crypto init */ 120 TEST_EQUAL(0, mbedtls_block_cipher_setup(&ctx, cipher_type)); 121#if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA) 122 TEST_EQUAL(ctx.engine, post_psa_engine); 123#endif 124 125exit: 126 mbedtls_block_cipher_free(&ctx); 127 BLOCK_CIPHER_PSA_DONE(); 128} 129/* END_CASE */ 130