Jens Wiklander | 9396d27 | 2018-08-15 11:01:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (c) 2018, Linaro Limited |
| 4 | */ |
| 5 | |
| 6 | #include <err.h> |
| 7 | #include <inttypes.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | /* OP-TEE TEE client API (built by optee_client) */ |
| 13 | #include <tee_client_api.h> |
| 14 | |
| 15 | /* To the the UUID (found the the TA's h-file(s)) */ |
| 16 | #include <acipher_ta.h> |
| 17 | |
| 18 | static void usage(int argc, char *argv[]) |
| 19 | { |
| 20 | const char *pname = "acipher"; |
| 21 | |
| 22 | if (argc) |
| 23 | pname = argv[0]; |
| 24 | |
| 25 | fprintf(stderr, "usage: %s <key_size> <string to encrypt>\n", pname); |
| 26 | exit(1); |
| 27 | } |
| 28 | |
| 29 | static void get_args(int argc, char *argv[], size_t *key_size, void **inbuf, |
| 30 | size_t *inbuf_len) |
| 31 | { |
| 32 | char *ep; |
| 33 | long ks; |
| 34 | |
| 35 | if (argc != 3) { |
| 36 | warnx("Unexpected number of arguments %d (expected 2)", |
| 37 | argc - 1); |
| 38 | usage(argc, argv); |
| 39 | } |
| 40 | |
| 41 | ks = strtol(argv[1], &ep, 0); |
| 42 | if (*ep) { |
| 43 | warnx("cannot parse key_size \"%s\"", argv[1]); |
| 44 | usage(argc, argv); |
| 45 | } |
| 46 | if (ks < 0 || ks == LONG_MAX) { |
| 47 | warnx("bad key_size \"%s\" (%ld)", argv[1], ks); |
| 48 | usage(argc, argv); |
| 49 | } |
| 50 | *key_size = ks; |
| 51 | |
| 52 | *inbuf = argv[2]; |
| 53 | *inbuf_len = strlen(argv[2]); |
| 54 | } |
| 55 | |
| 56 | static void teec_err(TEEC_Result res, uint32_t eo, const char *str) |
| 57 | { |
| 58 | errx(1, "%s: %#" PRIx32 " (error origin %#" PRIx32 ")", str, res, eo); |
| 59 | } |
| 60 | |
| 61 | int main(int argc, char *argv[]) |
| 62 | { |
| 63 | TEEC_Result res; |
| 64 | uint32_t eo; |
| 65 | TEEC_Context ctx; |
| 66 | TEEC_Session sess; |
| 67 | TEEC_Operation op; |
| 68 | size_t key_size; |
| 69 | void *inbuf; |
| 70 | size_t inbuf_len; |
| 71 | size_t n; |
| 72 | const TEEC_UUID uuid = TA_ACIPHER_UUID; |
| 73 | |
| 74 | get_args(argc, argv, &key_size, &inbuf, &inbuf_len); |
| 75 | |
| 76 | res = TEEC_InitializeContext(NULL, &ctx); |
| 77 | if (res) |
| 78 | errx(1, "TEEC_InitializeContext(NULL, x): %#" PRIx32, res); |
| 79 | |
| 80 | res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, |
| 81 | NULL, &eo); |
| 82 | if (res) |
| 83 | teec_err(res, eo, "TEEC_OpenSession(TEEC_LOGIN_PUBLIC)"); |
| 84 | |
| 85 | memset(&op, 0, sizeof(op)); |
| 86 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, |
| 87 | TEEC_NONE, TEEC_NONE); |
| 88 | op.params[0].value.a = key_size; |
| 89 | |
| 90 | res = TEEC_InvokeCommand(&sess, TA_ACIPHER_CMD_GEN_KEY, &op, &eo); |
| 91 | if (res) |
| 92 | teec_err(res, eo, "TEEC_InvokeCommand(TA_ACIPHER_CMD_GEN_KEY)"); |
| 93 | |
| 94 | memset(&op, 0, sizeof(op)); |
| 95 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 96 | TEEC_MEMREF_TEMP_OUTPUT, |
| 97 | TEEC_NONE, TEEC_NONE); |
| 98 | op.params[0].tmpref.buffer = inbuf; |
| 99 | op.params[0].tmpref.size = inbuf_len; |
| 100 | |
| 101 | res = TEEC_InvokeCommand(&sess, TA_ACIPHER_CMD_ENCRYPT, &op, &eo); |
| 102 | if (eo != TEEC_ORIGIN_TRUSTED_APP || res != TEEC_ERROR_SHORT_BUFFER) |
| 103 | teec_err(res, eo, "TEEC_InvokeCommand(TA_ACIPHER_CMD_ENCRYPT)"); |
| 104 | |
| 105 | op.params[1].tmpref.buffer = malloc(op.params[1].tmpref.size); |
| 106 | if (!op.params[1].tmpref.buffer) |
Etienne Carriere | 86a25e8 | 2019-03-11 16:20:13 +0100 | [diff] [blame^] | 107 | err(1, "Cannot allocate out buffer of size %zu", |
Jens Wiklander | 9396d27 | 2018-08-15 11:01:42 +0200 | [diff] [blame] | 108 | op.params[1].tmpref.size); |
| 109 | |
| 110 | res = TEEC_InvokeCommand(&sess, TA_ACIPHER_CMD_ENCRYPT, &op, &eo); |
| 111 | if (res) |
| 112 | teec_err(res, eo, "TEEC_InvokeCommand(TA_ACIPHER_CMD_ENCRYPT)"); |
| 113 | |
| 114 | printf("Encrypted buffer: "); |
| 115 | for (n = 0; n < op.params[1].tmpref.size; n++) |
| 116 | printf("%02x ", ((uint8_t *)op.params[1].tmpref.buffer)[n]); |
| 117 | printf("\n"); |
| 118 | return 0; |
| 119 | } |