Skip to content

Commit

Permalink
Add import flags parameter to tr31_import()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leonlynch committed Nov 1, 2023
1 parent 97c590f commit 47dd094
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
9 changes: 4 additions & 5 deletions src/tr31-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/tr31.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
{
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/tr31.h
Original file line number Diff line number Diff line change
Expand Up @@ -867,13 +867,15 @@ 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
*/
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
);

Expand Down
8 changes: 4 additions & 4 deletions test/tr31_decode_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
34 changes: 17 additions & 17 deletions test/tr31_decrypt_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/tr31_export_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 47dd094

Please sign in to comment.