Tamas Ban | afc4f3c | 2024-08-13 11:46:58 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2024, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | #include "dice_protection_environment.h" |
| 12 | #include "dpe_client.h" |
| 13 | #include "dpe_context_mngr.h" |
| 14 | #include "dpe_cmd_decode.h" |
| 15 | #include "dpe_cmd_encode.h" |
| 16 | |
| 17 | #include "cmd.h" |
| 18 | #include "root_keys.h" |
| 19 | |
| 20 | #include "tfm_sp_log.h" |
| 21 | |
| 22 | #define CLIENT_ID_NS -1 |
| 23 | |
| 24 | static void print_buf(const unsigned char *buf, size_t size) |
| 25 | { |
| 26 | size_t i; |
| 27 | |
| 28 | if (buf != NULL) { |
| 29 | for (i = 0; i < size; ++i) { |
| 30 | if ((i & 0xF) == 0) { |
| 31 | LOG_DBGFMT("\r\n"); |
| 32 | } |
| 33 | if (buf[i] < 0x10) { |
| 34 | LOG_DBGFMT(" 0%x", buf[i]); |
| 35 | } else { |
| 36 | LOG_DBGFMT(" %x", buf[i]); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | LOG_DBGFMT("\r\n"); |
| 41 | LOG_DBGFMT("\r\n"); |
| 42 | } |
| 43 | |
| 44 | static dpe_error_t |
| 45 | cbor_cmd(const char *cmd_in_buf, size_t cmd_in_size, int *context_handle) |
| 46 | { |
| 47 | char cmd_out_buf[2 * 4096]; |
| 48 | size_t cmd_out_size = sizeof(cmd_out_buf); |
| 49 | dpe_error_t err; |
| 50 | |
| 51 | (void)context_handle; |
| 52 | |
| 53 | LOG_DBGFMT("DPE request (%ld):\n", cmd_in_size); |
| 54 | print_buf(cmd_in_buf, cmd_in_size); |
| 55 | |
| 56 | err = dpe_command_decode(CLIENT_ID_NS, |
| 57 | cmd_in_buf, cmd_in_size, |
| 58 | cmd_out_buf, &cmd_out_size); |
| 59 | |
| 60 | LOG_DBGFMT("DPE response (%ld):\n", cmd_out_size); |
| 61 | print_buf(cmd_out_buf, cmd_out_size); |
| 62 | |
| 63 | return err; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * DPE Library Init: |
| 68 | * - crypto_lib |
| 69 | * - platform |
| 70 | * - context manager |
| 71 | */ |
| 72 | void dpe_lib_init(int *context_handle) |
| 73 | { |
| 74 | int ret; |
| 75 | dpe_error_t err; |
| 76 | |
| 77 | ret = psa_crypto_init(); |
| 78 | if (ret != 0) { |
| 79 | printf("ERROR: Crypto init failed! (%d)\n", ret); |
| 80 | exit(1); |
| 81 | } |
| 82 | |
| 83 | ret = register_rot_cdi(); |
| 84 | if (ret != 0) { |
| 85 | printf("ERROR: RoT CDI registration failed! (%d)\n", ret); |
| 86 | exit(1); |
| 87 | } |
| 88 | |
| 89 | ret = register_root_attest_key(); |
| 90 | if (ret != 0) { |
| 91 | printf("ERROR: Root attest key registration failed! (%d)\n", ret); |
| 92 | exit(1); |
| 93 | } |
| 94 | |
| 95 | err = initialise_context_mngr(context_handle); |
| 96 | if (err != DPE_NO_ERROR) { |
| 97 | printf("ERROR: Context manager init failed (%d)\n", err); |
| 98 | exit(1); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | dpe_error_t exec_dpe_cmd(enum cmd cmd, const char *cmd_in_buf, |
| 103 | size_t cmd_in_size, int *context_handle) |
| 104 | { |
| 105 | switch(cmd) { |
| 106 | case CBOR: |
| 107 | return cbor_cmd(cmd_in_buf, cmd_in_size, context_handle); |
| 108 | default: |
| 109 | printf("ERROR: Unknown command\n"); |
| 110 | exit(1); |
| 111 | } |
| 112 | } |