Skip to content

Commit b44f470

Browse files
amey-raghatateAkshay-Belsare
authored andcommitted
acipher: add dynamic algorithm selection and decryption support
- Extend acipher example to support for RSA algorithms: - TEE_ALG_RSAES_PKCS1_V1_5 - TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1/224/256/384/512 - Users can select algorithm and key size at runtime via: `optee_example_acipher <key_size> <string> <algo>` - Supported key sizes: 2048, 3072, 4096 bits - Defaults to TA_ALG_PKCS1_V1_5 if no algorithm is specified - Enhances the acipher example for flexible testing of multiple RSA modes Signed-off-by: Amey Avinash Raghatate <ameyavinash.raghatate@amd.com> State: waiting Link: linaro-swg#133
1 parent 6868266 commit b44f470

File tree

3 files changed

+148
-18
lines changed

3 files changed

+148
-18
lines changed

acipher/host/main.c

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,29 @@
1616
/* For the UUID (found in the TA's h-file(s)) */
1717
#include <acipher_ta.h>
1818

19+
#define ENCRYPT 1
20+
#define DECRYPT 0
21+
1922
static void usage(int argc, char *argv[])
2023
{
2124
const char *pname = "acipher";
2225

2326
if (argc)
2427
pname = argv[0];
2528

26-
fprintf(stderr, "usage: %s <key_size> <string to encrypt>\n", pname);
29+
fprintf(stderr, "%s: %s <key_size> <string to encrypt> <algo name>\n",
30+
__func__, pname);
2731
exit(1);
2832
}
2933

3034
static void get_args(int argc, char *argv[], size_t *key_size, void **inbuf,
31-
size_t *inbuf_len)
35+
size_t *inbuf_len, uint32_t *algo_num)
3236
{
3337
char *ep;
3438
long ks;
39+
char *algo;
3540

36-
if (argc != 3) {
41+
if ((argc > 4) || (argc < 3)) {
3742
warnx("Unexpected number of arguments %d (expected 2)",
3843
argc - 1);
3944
usage(argc, argv);
@@ -52,6 +57,32 @@ static void get_args(int argc, char *argv[], size_t *key_size, void **inbuf,
5257

5358
*inbuf = argv[2];
5459
*inbuf_len = strlen(argv[2]);
60+
61+
if (argc > 3) {
62+
algo = argv[3];
63+
printf("%s algo selected\n", algo);
64+
if (strcmp(algo, "TA_ALG_OAEP_MGF1_SHA1") == 0) {
65+
*algo_num = TA_ALG_OAEP_MGF1_SHA1;
66+
} else if (strcmp(algo, "TA_ALG_OAEP_MGF1_SHA224") == 0) {
67+
*algo_num = TA_ALG_OAEP_MGF1_SHA224;
68+
} else if (strcmp(algo, "TA_ALG_OAEP_MGF1_SHA256") == 0) {
69+
*algo_num = TA_ALG_OAEP_MGF1_SHA256;
70+
} else if (strcmp(algo, "TA_ALG_OAEP_MGF1_SHA384") == 0) {
71+
*algo_num = TA_ALG_OAEP_MGF1_SHA384;
72+
} else if (strcmp(algo, "TA_ALG_OAEP_MGF1_SHA512") == 0) {
73+
*algo_num = TA_ALG_OAEP_MGF1_SHA512;
74+
} else if (strcmp(algo, "TA_ALG_PKCS1_V1_5") == 0) {
75+
*algo_num = TA_ALG_PKCS1_V1_5;
76+
} else {
77+
printf("%s algo is invalid\n", algo);
78+
usage(argc, argv);
79+
}
80+
} else {
81+
printf("TA_ALG_PKCS1_V1_5 algo selected\n");
82+
*algo_num = TA_ALG_PKCS1_V1_5;
83+
}
84+
85+
5586
}
5687

5788
static void teec_err(TEEC_Result res, uint32_t eo, const char *str)
@@ -69,10 +100,13 @@ int main(int argc, char *argv[])
69100
size_t key_size;
70101
void *inbuf;
71102
size_t inbuf_len;
103+
void *outbuf = NULL;
104+
size_t outbuf_len = 0;
72105
size_t n;
106+
uint32_t algo_num;
73107
const TEEC_UUID uuid = TA_ACIPHER_UUID;
74108

75-
get_args(argc, argv, &key_size, &inbuf, &inbuf_len);
109+
get_args(argc, argv, &key_size, &inbuf, &inbuf_len, &algo_num);
76110

77111
res = TEEC_InitializeContext(NULL, &ctx);
78112
if (res)
@@ -95,26 +129,62 @@ int main(int argc, char *argv[])
95129
memset(&op, 0, sizeof(op));
96130
op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
97131
TEEC_MEMREF_TEMP_OUTPUT,
98-
TEEC_NONE, TEEC_NONE);
132+
TEEC_VALUE_INPUT, TEEC_VALUE_INPUT);
99133
op.params[0].tmpref.buffer = inbuf;
100134
op.params[0].tmpref.size = inbuf_len;
135+
op.params[2].value.a = ENCRYPT; /* encrypt */
136+
op.params[3].value.a = algo_num;
101137

102138
res = TEEC_InvokeCommand(&sess, TA_ACIPHER_CMD_ENCRYPT, &op, &eo);
103139
if (eo != TEEC_ORIGIN_TRUSTED_APP || res != TEEC_ERROR_SHORT_BUFFER)
104140
teec_err(res, eo, "TEEC_InvokeCommand(TA_ACIPHER_CMD_ENCRYPT)");
105141

142+
outbuf_len = op.params[1].tmpref.size;
106143
op.params[1].tmpref.buffer = malloc(op.params[1].tmpref.size);
107144
if (!op.params[1].tmpref.buffer)
108145
err(1, "Cannot allocate out buffer of size %zu",
109-
op.params[1].tmpref.size);
146+
outbuf_len);
110147

111148
res = TEEC_InvokeCommand(&sess, TA_ACIPHER_CMD_ENCRYPT, &op, &eo);
112149
if (res)
113150
teec_err(res, eo, "TEEC_InvokeCommand(TA_ACIPHER_CMD_ENCRYPT)");
114151

152+
outbuf = malloc(outbuf_len);
153+
if (!outbuf)
154+
err(1, "Cannot allocate out buffer of size %zu", outbuf_len);
155+
156+
memmove(outbuf, op.params[1].tmpref.buffer, outbuf_len);
115157
printf("Encrypted buffer: ");
116-
for (n = 0; n < op.params[1].tmpref.size; n++)
117-
printf("%02x ", ((uint8_t *)op.params[1].tmpref.buffer)[n]);
158+
for (n = 0; n < outbuf_len; n++)
159+
printf("%02x ", ((uint8_t *)outbuf)[n]);
118160
printf("\n");
161+
162+
memset(&op, 0, sizeof(op));
163+
op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
164+
TEEC_MEMREF_TEMP_OUTPUT,
165+
TEEC_VALUE_INPUT, TEEC_VALUE_INPUT);
166+
op.params[0].tmpref.buffer = outbuf;
167+
op.params[0].tmpref.size = outbuf_len;
168+
op.params[2].value.a = DECRYPT; /* decrypt */
169+
op.params[3].value.a = algo_num;
170+
171+
res = TEEC_InvokeCommand(&sess, TA_ACIPHER_CMD_ENCRYPT, &op, &eo);
172+
if (eo != TEEC_ORIGIN_TRUSTED_APP || res != TEEC_ERROR_SHORT_BUFFER)
173+
teec_err(res, eo, "TEEC_InvokeCommand(TA_ACIPHER_CMD_DYCRYPT)");
174+
175+
op.params[1].tmpref.buffer = malloc(op.params[1].tmpref.size);
176+
if (!op.params[1].tmpref.buffer)
177+
err(1, "Cannot allocate out buffer of size %zu",
178+
outbuf_len);
179+
180+
res = TEEC_InvokeCommand(&sess, TA_ACIPHER_CMD_ENCRYPT, &op, &eo);
181+
if (res)
182+
teec_err(res, eo, "TEEC_InvokeCommand(TA_ACIPHER_CMD_DYCRYPT)");
183+
184+
if (memcmp(inbuf, op.params[1].tmpref.buffer, op.params[1].tmpref.size))
185+
printf("message is not matching\n");
186+
else
187+
printf("message is matching successfully\n");
188+
119189
return 0;
120190
}

acipher/ta/acipher_ta.c

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ static TEE_Result cmd_gen_key(struct acipher *state, uint32_t pt,
3232

3333
res = TEE_AllocateTransientObject(key_type, key_size, &key);
3434
if (res) {
35-
EMSG("TEE_AllocateTransientObject(%#" PRIx32 ", %" PRId32 "): %#" PRIx32, key_type, key_size, res);
35+
EMSG("TEE_AllocateTransientObject(%#" PRIx32 ", %" PRId32 "): %#"
36+
PRIx32, key_type, key_size, res);
3637
return res;
3738
}
3839

@@ -49,6 +50,33 @@ static TEE_Result cmd_gen_key(struct acipher *state, uint32_t pt,
4950
return TEE_SUCCESS;
5051
}
5152

53+
static TEE_Result select_algo(uint32_t param, uint32_t *algo)
54+
{
55+
switch (param) {
56+
case TA_ALG_PKCS1_V1_5:
57+
*algo = TEE_ALG_RSAES_PKCS1_V1_5;
58+
return TEE_SUCCESS;
59+
case TA_ALG_OAEP_MGF1_SHA1:
60+
*algo = TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA1;
61+
return TEE_SUCCESS;
62+
case TA_ALG_OAEP_MGF1_SHA224:
63+
*algo = TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA224;
64+
return TEE_SUCCESS;
65+
case TA_ALG_OAEP_MGF1_SHA256:
66+
*algo = TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA256;
67+
return TEE_SUCCESS;
68+
case TA_ALG_OAEP_MGF1_SHA384:
69+
*algo = TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA384;
70+
return TEE_SUCCESS;
71+
case TA_ALG_OAEP_MGF1_SHA512:
72+
*algo = TEE_ALG_RSAES_PKCS1_OAEP_MGF1_SHA512;
73+
return TEE_SUCCESS;
74+
default:
75+
EMSG("Invalid algo %u", param);
76+
return TEE_ERROR_BAD_PARAMETERS;
77+
}
78+
}
79+
5280
static TEE_Result cmd_enc(struct acipher *state, uint32_t pt,
5381
TEE_Param params[TEE_NUM_PARAMS])
5482
{
@@ -57,13 +85,14 @@ static TEE_Result cmd_enc(struct acipher *state, uint32_t pt,
5785
uint32_t inbuf_len;
5886
void *outbuf;
5987
uint32_t outbuf_len;
88+
uint32_t alg_num;
6089
TEE_OperationHandle op;
6190
TEE_ObjectInfo key_info;
62-
const uint32_t alg = TEE_ALG_RSAES_PKCS1_V1_5;
91+
uint32_t encrypt;
6392
const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
6493
TEE_PARAM_TYPE_MEMREF_OUTPUT,
65-
TEE_PARAM_TYPE_NONE,
66-
TEE_PARAM_TYPE_NONE);
94+
TEE_PARAM_TYPE_VALUE_INPUT,
95+
TEE_PARAM_TYPE_VALUE_INPUT);
6796

6897
if (pt != exp_pt)
6998
return TEE_ERROR_BAD_PARAMETERS;
@@ -81,10 +110,21 @@ static TEE_Result cmd_enc(struct acipher *state, uint32_t pt,
81110
outbuf = params[1].memref.buffer;
82111
outbuf_len = params[1].memref.size;
83112

84-
res = TEE_AllocateOperation(&op, alg, TEE_MODE_ENCRYPT,
113+
res = select_algo(params[3].value.a, &alg_num);
114+
if (res != TEE_SUCCESS)
115+
return res;
116+
117+
if (params[2].value.a)
118+
encrypt = TEE_MODE_ENCRYPT;
119+
else
120+
encrypt = TEE_MODE_DECRYPT;
121+
122+
res = TEE_AllocateOperation(&op, alg_num, encrypt,
85123
key_info.keySize);
86124
if (res) {
87-
EMSG("TEE_AllocateOperation(TEE_MODE_ENCRYPT, %#" PRIx32 ", %" PRId32 "): %#" PRIx32, alg, key_info.keySize, res);
125+
EMSG("TEE_AllocateOperation(TEE_MODE_ENCRYPT, %#"
126+
PRIx32 ", %" PRId32 "): %#" PRIx32,
127+
alg_num, key_info.keySize, res);
88128
return res;
89129
}
90130

@@ -94,10 +134,22 @@ static TEE_Result cmd_enc(struct acipher *state, uint32_t pt,
94134
goto out;
95135
}
96136

97-
res = TEE_AsymmetricEncrypt(op, NULL, 0, inbuf, inbuf_len, outbuf,
98-
&outbuf_len);
99-
if (res) {
100-
EMSG("TEE_AsymmetricEncrypt(%" PRId32 ", %" PRId32 "): %#" PRIx32, inbuf_len, params[1].memref.size, res);
137+
if (encrypt == TEE_MODE_ENCRYPT) {
138+
res = TEE_AsymmetricEncrypt(op, NULL, 0, inbuf,
139+
inbuf_len, outbuf, &outbuf_len);
140+
if (res) {
141+
EMSG("TEE_AsymmetricEncrypt(%" PRId32 ", %"
142+
PRId32 "): %#" PRIx32, inbuf_len,
143+
params[1].memref.size, res);
144+
}
145+
} else {
146+
res = TEE_AsymmetricDecrypt(op, NULL, 0, inbuf, inbuf_len,
147+
outbuf, &outbuf_len);
148+
if (res) {
149+
EMSG("TEE_AsymmetricDecrypt(%" PRId32 ", %"
150+
PRId32 "): %#" PRIx32, inbuf_len,
151+
params[1].memref.size, res);
152+
}
101153
}
102154
params[1].memref.size = outbuf_len;
103155

acipher/ta/include/acipher_ta.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@
2222
*/
2323
#define TA_ACIPHER_CMD_ENCRYPT 1
2424

25+
#define TA_ALG_PKCS1_V1_5 0
26+
#define TA_ALG_OAEP_MGF1_SHA1 1
27+
#define TA_ALG_OAEP_MGF1_SHA224 2
28+
#define TA_ALG_OAEP_MGF1_SHA256 3
29+
#define TA_ALG_OAEP_MGF1_SHA384 4
30+
#define TA_ALG_OAEP_MGF1_SHA512 5
31+
32+
2533
#endif /* __ACIPHER_TA_H */

0 commit comments

Comments
 (0)