From 47dd094f6d9dcee2a351250eb915060051f16444 Mon Sep 17 00:00:00 2001 From: Leon Lynch Date: Wed, 1 Nov 2023 21:36:13 +0100 Subject: [PATCH] Add import flags parameter to tr31_import() This matches a similar change made for tr31_export() and is in anticipation of future import flags. The import_flags option variable has also been added to tr31-tool in anticipation of future import flags. --- src/tr31-tool.c | 9 ++++----- src/tr31.c | 4 +++- src/tr31.h | 2 ++ test/tr31_decode_test.c | 8 ++++---- test/tr31_decrypt_test.c | 34 +++++++++++++++++----------------- test/tr31_export_test.c | 2 +- 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/tr31-tool.c b/src/tr31-tool.c index 2528746..1dc608a 100644 --- a/src/tr31-tool.c +++ b/src/tr31-tool.c @@ -51,6 +51,7 @@ struct tr31_tool_options_t { // valid if import is true size_t key_block_len; char* key_block; + uint32_t import_flags; // export parameters // valid if export is true @@ -81,8 +82,6 @@ struct tr31_tool_options_t { const char* export_opt_block_TS_str; bool export_opt_block_WP; uint8_t export_opt_block_WP_value; - - // export flags uint32_t export_flags; // kbpk parameters @@ -750,7 +749,7 @@ static int tr31_init_from_header(const char* header, struct tr31_ctx_t* tr31_ctx memcpy(tmp_keyblock + 1, tmp, 4); // misuse TR-31 import function to parse header into TR-31 context object - r = tr31_import(tmp_keyblock, sizeof(tmp_keyblock), NULL, tr31_ctx); + r = tr31_import(tmp_keyblock, sizeof(tmp_keyblock), NULL, 0, tr31_ctx); if (r) { return r; } @@ -829,10 +828,10 @@ static int do_tr31_import(const struct tr31_tool_options_t* options) if (options->kbpk) { // if key block protection key was provided // parse and decrypt TR-31 key block - r = tr31_import(options->key_block, options->key_block_len, &kbpk, &tr31_ctx); + r = tr31_import(options->key_block, options->key_block_len, &kbpk, options->import_flags, &tr31_ctx); } else { // else if no key block protection key was provided // parse TR-31 key block - r = tr31_import(options->key_block, options->key_block_len, NULL, &tr31_ctx); + r = tr31_import(options->key_block, options->key_block_len, NULL, options->import_flags, &tr31_ctx); } // check for errors if (r) { diff --git a/src/tr31.c b/src/tr31.c index baa78b5..7996d42 100644 --- a/src/tr31.c +++ b/src/tr31.c @@ -1827,6 +1827,7 @@ int tr31_import( const char* key_block, size_t key_block_len, const struct tr31_key_t* kbpk, + uint32_t flags, struct tr31_ctx_t* ctx ) { @@ -1853,10 +1854,11 @@ int tr31_import( // initialise processing state object // this will populate: + // - state.flags // - state.enc_block_size // - state.authenticator_length header = (const struct tr31_header_t*)key_block; - r = tr31_state_init(0, header->version_id, &state); + r = tr31_state_init(flags, header->version_id, &state); if (r) { // return error value as-is return r; diff --git a/src/tr31.h b/src/tr31.h index 1655c3d..a923496 100644 --- a/src/tr31.h +++ b/src/tr31.h @@ -867,6 +867,7 @@ int tr31_opt_block_decode_WP( * @param key_block TR-31 key block. Must contain printable ASCII characters. Null-termination not required. * @param key_block_len Length of TR-31 key block in bytes, excluding null-termination. * @param kbpk TR-31 key block protection key. NULL if not available or decryption is not required. + * @param flags TR-31 import flags. * @param ctx TR-31 context object output * @return Zero for success. Less than zero for internal error. Greater than zero for data error. See @ref tr31_error_t */ @@ -874,6 +875,7 @@ int tr31_import( const char* key_block, size_t key_block_len, const struct tr31_key_t* kbpk, + uint32_t flags, struct tr31_ctx_t* ctx ); diff --git a/test/tr31_decode_test.c b/test/tr31_decode_test.c index 4d93976..e07679f 100644 --- a/test/tr31_decode_test.c +++ b/test/tr31_decode_test.c @@ -46,7 +46,7 @@ int main(void) // test key block decoding for format version B with optional block KS printf("Test 1 (Format version B with optional block KS)...\n"); - r = tr31_import(test1_tr31_ascii, strlen(test1_tr31_ascii), NULL, &test_tr31); + r = tr31_import(test1_tr31_ascii, strlen(test1_tr31_ascii), NULL, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -94,7 +94,7 @@ int main(void) // test key block decoding for format version D containing TDES key printf("Test 2 (Format version D containing TDES key)...\n"); - r = tr31_import(test2_tr31_ascii, strlen(test2_tr31_ascii), NULL, &test_tr31); + r = tr31_import(test2_tr31_ascii, strlen(test2_tr31_ascii), NULL, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -118,7 +118,7 @@ int main(void) // test key block decoding for format version D containing AES key printf("Test 3 (Format version D containing AES key)...\n"); - r = tr31_import(test3_tr31_ascii, strlen(test3_tr31_ascii), NULL, &test_tr31); + r = tr31_import(test3_tr31_ascii, strlen(test3_tr31_ascii), NULL, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -142,7 +142,7 @@ int main(void) // test key block decoding for format version B with optional blocks KS, KC, and KP printf("Test 4 (Format version B with optional block KS, KC, KP)...\n"); - r = tr31_import(test4_tr31_ascii, strlen(test4_tr31_ascii), NULL, &test_tr31); + r = tr31_import(test4_tr31_ascii, strlen(test4_tr31_ascii), NULL, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; diff --git a/test/tr31_decrypt_test.c b/test/tr31_decrypt_test.c index fe311b6..2684100 100644 --- a/test/tr31_decrypt_test.c +++ b/test/tr31_decrypt_test.c @@ -225,7 +225,7 @@ int main(void) // test key block decryption for format version A printf("Test 1 (Basic format version A)...\n"); - r = tr31_import(test1_tr31_format_a, strlen(test1_tr31_format_a), &test_kbpk, &test_tr31); + r = tr31_import(test1_tr31_format_a, strlen(test1_tr31_format_a), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -261,7 +261,7 @@ int main(void) // test key block decryption for format version B printf("Test 1 (Basic format version B)...\n"); - r = tr31_import(test1_tr31_format_b, strlen(test1_tr31_format_b), &test_kbpk, &test_tr31); + r = tr31_import(test1_tr31_format_b, strlen(test1_tr31_format_b), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -297,7 +297,7 @@ int main(void) // test key block decryption for format version C printf("Test 1 (Basic format version C)...\n"); - r = tr31_import(test1_tr31_format_c, strlen(test1_tr31_format_c), &test_kbpk, &test_tr31); + r = tr31_import(test1_tr31_format_c, strlen(test1_tr31_format_c), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -339,7 +339,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test2_kbpk); test_kbpk.data = (void*)test2_kbpk; - r = tr31_import(test2_tr31_ascii, strlen(test2_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test2_tr31_ascii, strlen(test2_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -381,7 +381,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test3_kbpk); test_kbpk.data = (void*)test3_kbpk; - r = tr31_import(test3_tr31_ascii, strlen(test3_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test3_tr31_ascii, strlen(test3_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -423,7 +423,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test4_kbpk); test_kbpk.data = (void*)test4_kbpk; - r = tr31_import(test4_tr31_ascii, strlen(test4_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test4_tr31_ascii, strlen(test4_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -489,7 +489,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test5_kbpk); test_kbpk.data = (void*)test5_kbpk; - r = tr31_import(test5_tr31_ascii, strlen(test5_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test5_tr31_ascii, strlen(test5_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -555,7 +555,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test6_kbpk); test_kbpk.data = (void*)test6_kbpk; - r = tr31_import(test6_tr31_ascii, strlen(test6_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test6_tr31_ascii, strlen(test6_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -597,7 +597,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test7_kbpk); test_kbpk.data = (void*)test7_kbpk; - r = tr31_import(test7_tr31_ascii, strlen(test7_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test7_tr31_ascii, strlen(test7_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -639,7 +639,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test8_kbpk); test_kbpk.data = (void*)test8_kbpk; - r = tr31_import(test8_tr31_ascii, strlen(test8_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test8_tr31_ascii, strlen(test8_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -681,7 +681,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test9_kbpk); test_kbpk.data = (void*)test9_kbpk; - r = tr31_import(test9_tr31_ascii, strlen(test9_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test9_tr31_ascii, strlen(test9_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -723,7 +723,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test10_kbpk); test_kbpk.data = (void*)test10_kbpk; - r = tr31_import(test10_tr31_ascii, strlen(test10_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test10_tr31_ascii, strlen(test10_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -765,7 +765,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test11_kbpk); test_kbpk.data = (void*)test11_kbpk; - r = tr31_import(test11_tr31_ascii, strlen(test11_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test11_tr31_ascii, strlen(test11_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -807,7 +807,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test15_kbpk); test_kbpk.data = (void*)test15_kbpk; - r = tr31_import(test15_tr31_ascii, strlen(test15_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test15_tr31_ascii, strlen(test15_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -873,7 +873,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test16_kbpk); test_kbpk.data = (void*)test16_kbpk; - r = tr31_import(test16_tr31_ascii, strlen(test16_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test16_tr31_ascii, strlen(test16_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -939,7 +939,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test17_kbpk); test_kbpk.data = (void*)test17_kbpk; - r = tr31_import(test17_tr31_ascii, strlen(test17_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test17_tr31_ascii, strlen(test17_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; @@ -1018,7 +1018,7 @@ int main(void) test_kbpk.mode_of_use = TR31_KEY_MODE_OF_USE_ENC_DEC; test_kbpk.length = sizeof(test18_kbpk); test_kbpk.data = (void*)test18_kbpk; - r = tr31_import(test18_tr31_ascii, strlen(test18_tr31_ascii), &test_kbpk, &test_tr31); + r = tr31_import(test18_tr31_ascii, strlen(test18_tr31_ascii), &test_kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit; diff --git a/test/tr31_export_test.c b/test/tr31_export_test.c index 3b933da..e0ce17e 100644 --- a/test/tr31_export_test.c +++ b/test/tr31_export_test.c @@ -906,7 +906,7 @@ int main(void) tr31_release(&test_tr31); // Import and decrypt key block - r = tr31_import(key_block, strlen(key_block), &test[i].kbpk, &test_tr31); + r = tr31_import(key_block, strlen(key_block), &test[i].kbpk, 0, &test_tr31); if (r) { fprintf(stderr, "tr31_import() error %d: %s\n", r, tr31_get_error_string(r)); goto exit;