blob: 43a435e63a93fda732a937fba08ba6f42cb3f5fa [file] [log] [blame]
Tamas Banafc4f3c2024-08-13 11:46:58 +02001/*
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +00002 * SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
Tamas Banafc4f3c2024-08-13 11:46:58 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Tamas Ban628192c2024-08-13 13:35:58 +02008#include <argp.h>
Tamas Banafc4f3c2024-08-13 11:46:58 +02009#include <stdio.h>
10#include <stdlib.h>
11
12#include "dice_protection_environment.h"
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +000013#include "tfm_log_unpriv.h"
Tamas Banafc4f3c2024-08-13 11:46:58 +020014
Tamas Ban545bb5c2024-08-13 12:00:52 +020015#include "extra_s_tests.h"
16#include "test_framework.h"
17
Tamas Banafc4f3c2024-08-13 11:46:58 +020018#include "cmd.h"
19
Tamas Ban545bb5c2024-08-13 12:00:52 +020020static struct test_suite_t test_suites[] = {
21 {&register_testsuite_extra_s_interface, 0, 0, 0},
22 /* End of test suites */
23 {0, 0, 0, 0}
24};
25
Tamas Ban628192c2024-08-13 13:35:58 +020026static char doc[] = "\nDICE Protection Environment (DPE): \n" \
27 " - Without any argument it executes the regression test suite\n" \
28 " - With 2 arguments it executes a DPE command as below";
29static char args_doc[] = "<ARG1> <ARG2>";
30static struct argp_option options[] = {
31 { "cbor", 'c', "CBOR_INPUT", 0, "Execute an already CBOR encoded command."},
32 { "derive-context", 'd', "RAW_INPUT", 0, "Execute a DeriveContext command."},
33 { "certify-key", 'k', "RAW_INPUT", 0, "Execute a CertifyKey command."},
34 { "get-cert-chain", 'g', "RAW_INPUT", 0, "Execute a GetCertificateChain command."},
35 { "rnd-dpe-cmd", 'r', "RAW_INPUT", 0, "Execute a random supported command."},
36 { 0 }
37};
38
39struct arguments {
40 enum cmd cmd;
41};
42
43static error_t parse_opt(int key, char *arg, struct argp_state *state) {
44 struct arguments *arguments = state->input;
45
46 switch (key) {
47 case 'c': arguments->cmd = CBOR; break;
48 case 'd': arguments->cmd = DC; break;
49 case 'k': arguments->cmd = CK; break;
50 case 'g': arguments->cmd = GCC; break;
51 case 'r': arguments->cmd = RND; break;
52 case ARGP_KEY_ARG:
53 argp_usage(state);
54 break;
55 default: return ARGP_ERR_UNKNOWN;
56 }
57
58 return 0;
59}
Tamas Ban545bb5c2024-08-13 12:00:52 +020060
Tamas Banafc4f3c2024-08-13 11:46:58 +020061static int read_cmd(const char path[], char *cmd_buf, size_t *cmd_buf_size)
62{
63 FILE *fd;
64 size_t cmd_size;
65
66 if ((fd = fopen(path, "r")) == NULL) {
67 printf("ERROR: File (%s) cannot be opened.\n", path);
68 return -1;
69 }
70
71 fseek(fd, 0, SEEK_END);
72 cmd_size = ftell(fd);
73 rewind(fd);
74
75 if (*cmd_buf_size < cmd_size) {
76 printf("ERROR: cmd_buf is too small\n");
77 return -1;
78 }
79
80 for (size_t i = 0; i < cmd_size; ++i) {
81 cmd_buf[i] = fgetc(fd);
82 }
83 *cmd_buf_size = cmd_size;
84
85 fclose(fd);
86
87 return 0;
88}
89
90int main(int argc, char **argv)
91{
92 int context_handle;
93 int ret;
94 char cmd_in_buf[4096] = {0};
95 size_t cmd_in_size = sizeof(cmd_in_buf);
96 dpe_error_t err;
Tamas Ban628192c2024-08-13 13:35:58 +020097 struct arguments arguments;
98 struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };
Tamas Banafc4f3c2024-08-13 11:46:58 +020099
100 dpe_lib_init(&context_handle);
Tamas Ban628192c2024-08-13 13:35:58 +0200101 argp_parse(&argp, argc, argv, 0, 0, &arguments);
Tamas Banafc4f3c2024-08-13 11:46:58 +0200102
Tamas Ban545bb5c2024-08-13 12:00:52 +0200103 if (argc == 1) {
104 /*************** Invoked without any command line parameter ************/
105 printf("Execute DPE regression test\n");
106 /* Regression test prints the result to the console */
107 (void)run_test("DPE Regression", test_suites);
108
Tamas Ban628192c2024-08-13 13:35:58 +0200109 } else if (argc == 3) {
110 /****************** Input params are provided *************************/
Jackson Cooper-Driver90d89a02025-03-03 16:41:37 +0000111 INFO_UNPRIV_RAW("Execute DPE API test (%s %s)\n", argv[1], argv[2]);
Tamas Ban628192c2024-08-13 13:35:58 +0200112 ret = read_cmd(argv[2], cmd_in_buf, &cmd_in_size);
Tamas Banafc4f3c2024-08-13 11:46:58 +0200113 if (ret < 0) {
114 exit(1);
115 }
116
Tamas Ban628192c2024-08-13 13:35:58 +0200117 err = exec_dpe_cmd(arguments.cmd, cmd_in_buf, cmd_in_size, &context_handle);
Tamas Banafc4f3c2024-08-13 11:46:58 +0200118 if (err != DPE_NO_ERROR) {
119 printf("DPE command decode/execution failed (%d)\n", ret);
120 exit(1);
121 }
Tamas Banafc4f3c2024-08-13 11:46:58 +0200122 }
123
124 exit(0);
125}