|
| 1 | +// SPDX-License-Identifier: BSD-2-Clause |
| 2 | +/* |
| 3 | + * Copyright (c) 2025, Advanced Micro Devices, Inc. All rights reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <err.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <string.h> |
| 10 | +#include <tee_client_api.h> |
| 11 | + |
| 12 | +#include <ecdh_ta.h> |
| 13 | + |
| 14 | +static void hexdump(const void *p, size_t len) |
| 15 | +{ |
| 16 | + const unsigned char *b = (const unsigned char *)p; |
| 17 | + |
| 18 | + for (size_t i = 0; i < len; i++) { |
| 19 | + printf("%02x", b[i]); |
| 20 | + if ((i + 1) % 32 == 0) |
| 21 | + printf("\n"); |
| 22 | + } |
| 23 | + if (len % 32) |
| 24 | + printf("\n"); |
| 25 | +} |
| 26 | + |
| 27 | +int main(void) |
| 28 | +{ |
| 29 | + TEEC_Result res; |
| 30 | + TEEC_Context ctx; |
| 31 | + TEEC_Session sess; |
| 32 | + TEEC_Operation op; |
| 33 | + TEEC_UUID uuid = TA_ECDH_UUID; |
| 34 | + uint32_t err_origin; |
| 35 | + |
| 36 | + res = TEEC_InitializeContext(NULL, &ctx); |
| 37 | + if (res != TEEC_SUCCESS) |
| 38 | + errx(1, "TEEC_InitializeContext failed with code 0x%x", res); |
| 39 | + |
| 40 | + res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, |
| 41 | + NULL, &err_origin); |
| 42 | + if (res != TEEC_SUCCESS) |
| 43 | + errx(1, "TEEC_OpenSession failed 0x%x origin 0x%x", |
| 44 | + res, err_origin); |
| 45 | + |
| 46 | + uint32_t curve = TA_ECC_CURVE_NIST_P384; |
| 47 | + |
| 48 | + uint8_t secret[ECDH_BUF_BYTES]; |
| 49 | + |
| 50 | + memset(&op, 0, sizeof(op)); |
| 51 | + op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, |
| 52 | + TEEC_NONE, |
| 53 | + TEEC_NONE, |
| 54 | + TEEC_MEMREF_TEMP_OUTPUT); |
| 55 | + |
| 56 | + op.params[0].value.a = curve; /* IN: curve id */ |
| 57 | + op.params[0].value.b = 0; /* OUT: secret len */ |
| 58 | + op.params[3].tmpref.buffer = secret; /* OUT buffer for secret */ |
| 59 | + op.params[3].tmpref.size = sizeof(secret); |
| 60 | + |
| 61 | + res = TEEC_InvokeCommand(&sess, TA_ECDH_CMD_DERIVE_SELFTEST, |
| 62 | + &op, &err_origin); |
| 63 | + if (res == TEEC_ERROR_SHORT_BUFFER) { |
| 64 | + /* Resize and try once more with the exact size TA asked for */ |
| 65 | + size_t need = op.params[0].value.b; |
| 66 | + |
| 67 | + if (need > sizeof(secret)) |
| 68 | + errx(1, "Required secret buffer too big: %zu", need); |
| 69 | + |
| 70 | + op.params[3].tmpref.size = need; |
| 71 | + res = TEEC_InvokeCommand(&sess, TA_ECDH_CMD_DERIVE_SELFTEST, |
| 72 | + &op, &err_origin); |
| 73 | + } |
| 74 | + |
| 75 | + if (res != TEEC_SUCCESS) |
| 76 | + errx(1, "Invoke TA_ECDH_CMD_DERIVE_SELFTEST failed 0x%x origin 0x%x", |
| 77 | + res, err_origin); |
| 78 | + |
| 79 | + size_t secret_len = op.params[0].value.b; |
| 80 | + |
| 81 | + printf("ECDH shared secret (%zu bytes) on curve id %u:\n", |
| 82 | + secret_len, curve); |
| 83 | + hexdump(secret, secret_len); |
| 84 | + |
| 85 | + TEEC_CloseSession(&sess); |
| 86 | + TEEC_FinalizeContext(&ctx); |
| 87 | + return 0; |
| 88 | +} |
0 commit comments