-
Notifications
You must be signed in to change notification settings - Fork 1
/
caam-keygen.c
335 lines (275 loc) · 7.86 KB
/
caam-keygen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// SPDX-License-Identifier: BSD-3-Clause
/*
* Copyright 2020 NXP
*/
#include "caam-keygen.h"
#include "caam-keygen_priv.h"
int caam_keygen_fd;
void caam_keygen_usage(void)
{
printf("CAAM keygen usage: caam-keygen [options]\n");
printf("Options:\n");
printf("create <key_name> <key_enc> <key_mode> <key_val>\n");
printf("\t<key_name> the name of the file that will contain the black key.\n");
printf("\tA file with the same name, but with .bb extension, will contain the black blob.\n");
printf("\t<key_enc> can be ecb or ccm\n");
printf("\t<key_mode> can be -s or -t.\n");
printf("\t -s generate a black key from random with the size given in the next argument\n");
printf("\t -t generate a black key from a plaintext given in the next argument\n");
printf("\t<key_val> the size or the plaintext based on the previous argument (<key_mode>)\n");
printf("import <blob_name> <key_name>\n");
printf("\t<blob_name> the absolute path of the file that contains the blob\n");
printf("\t<key_name> the name of the file that will contain the black key.\n");
}
int caam_keygen_create(char *key_name, char *key_enc, char *key_mode,
char *key_value)
{
FILE *f_key, *f_blob;
struct caam_keygen_cmd param;
char *blob_name;
int ret = -1;
/*
* Blob file is generated in the same location as the key,
* in KEYBLOB_LOCATION.
*
* blob_name = KEYBLOB_LOCATION/key_name.bb
* Add 4 for blob name size, including null terminator
*/
blob_name = malloc(strlen(key_name) + strlen(KEYBLOB_LOCATION) + 4);
if (!blob_name) {
printf("Failed to allocate memory for blob name.\n");
return ret;
}
/* blob_name = KEYBLOB_LOCATION/key_name.bb */
strcpy(blob_name, KEYBLOB_LOCATION);
strcpy(blob_name, key_name);
strcat(blob_name, ".bb");
param.key_enc = (uintptr_t)key_enc;
/* add 1 for null terminator */
param.key_enc_len = strlen(key_enc) + 1;
param.key_mode = (uintptr_t)key_mode;
/* add 1 for null terminator */
param.key_mode_len = strlen(key_mode) + 1;
param.key_value = (uintptr_t)key_value;
/* add 1 for null terminator */
param.key_value_len = strlen(key_value) + 1;
param.black_key_len = MAX_BLACK_KEY_SIZE;
param.black_key = (uintptr_t)malloc(param.black_key_len);
if (!param.black_key) {
printf("Failed to allocate memory for black key.\n");
goto error_blob_name;
}
param.blob_len = MAX_BLOB_SIZE;
param.blob = (uintptr_t)malloc(param.blob_len);
if (!param.blob) {
printf("Failed to allocate memory for blob.\n");
goto error_param_black_key;
}
ret = ioctl(caam_keygen_fd, CAAM_KEYGEN_IOCTL_CREATE, ¶m);
if (ret) {
/* Print error message received from kernel space */
printf("%s\n", (char *)(uintptr_t)param.blob);
caam_keygen_usage();
goto error_param_blob;
}
f_blob = fopen(blob_name, "wb");
if (!f_blob) {
printf("Failed to open blob file.\n");
goto error_param_blob;
}
f_key = fopen(key_name, "wb");
if (!f_key) {
printf("Failed to open key file.\n");
goto error_open_keyfile;
}
/* Write key and blob to files */
if (fwrite((void *)(uintptr_t)param.black_key, 1, param.black_key_len,
f_key) != param.black_key_len) {
printf("Failed to write black key to file.\n");
goto error_write;
}
if (fwrite((void *)(uintptr_t)param.blob, 1, param.blob_len, f_blob) !=
param.blob_len) {
printf("Failed to write blob to file.\n");
goto error_write;
}
/* Free resources */
free((void *)(uintptr_t)param.blob);
free((void *)(uintptr_t)param.black_key);
free(blob_name);
/* Close files */
fclose(f_key);
fclose(f_blob);
return 0;
error_write:
fclose(f_key);
error_open_keyfile:
fclose(f_blob);
error_param_blob:
free((void *)(uintptr_t)param.blob);
error_param_black_key:
free((void *)(uintptr_t)param.black_key);
error_blob_name:
free(blob_name);
return ret;
}
int caam_keygen_import(char *blob_name, char *key_name)
{
FILE *f_key, *f_blob;
struct stat blob_st;
struct caam_keygen_cmd param;
int ret = -1;
size_t blob_file_size = 0;
/* Validate arguments for import operation */
if (!blob_name || !key_name) {
printf("Invalid arguments for import operation.\n");
return ret;
}
/* Get blob file size */
if (stat(blob_name, &blob_st)) {
printf("Failed to get blob file status.\n");
return ret;
}
blob_file_size = blob_st.st_size;
/* Check blob size */
if (blob_file_size <= BLOB_OVERHEAD) {
printf("Invalid blob - file too small.\n");
return ret;
}
param.blob_len = blob_file_size;
param.blob = (uintptr_t)malloc(param.blob_len);
if (!param.blob) {
printf("Failed to allocate memory for blob.\n");
return ret;
}
f_blob = fopen(blob_name, "rb");
if (!f_blob) {
printf("Failed to open blob file or the file doesn't exist.\n");
goto error_param_blob;
}
/* Read blob from file */
if (fread((void *)(uintptr_t)param.blob, 1, blob_file_size, f_blob) !=
blob_file_size) {
printf("Failed to read blob from file.\n");
fclose(f_blob);
goto error_param_blob;
}
/* Close blob size */
fclose(f_blob);
param.black_key_len = MAX_BLACK_KEY_SIZE;
param.black_key = (uintptr_t)malloc(param.black_key_len);
if (!param.black_key) {
printf("Failed to allocate memory for black key.\n");
goto error_param_blob;
}
ret = ioctl(caam_keygen_fd, CAAM_KEYGEN_IOCTL_IMPORT, ¶m);
if (ret) {
/* Print error message received from kernel space */
printf("%s\n", (char *)(uintptr_t)param.black_key);
caam_keygen_usage();
goto error_param_black_key;
}
/* Open key file */
f_key = fopen(key_name, "wb");
if (!f_key) {
printf("Failed to open key file.\n");
goto error_param_black_key;
}
/* Write key to file */
if (fwrite((void *)(uintptr_t)param.black_key, 1, param.black_key_len,
f_key) != param.black_key_len) {
printf("Failed to write black key to file.\n");
goto error_write;
}
/* Free resources */
free((void *)(uintptr_t)param.black_key);
free((void *)(uintptr_t)param.blob);
/* Close key file */
fclose(f_key);
return 0;
error_write:
fclose(f_key);
error_param_black_key:
free((void *)(uintptr_t)param.black_key);
error_param_blob:
free((void *)(uintptr_t)param.blob);
return ret;
}
int caam_create_keyblob_path(const char *dir)
{
char *cmd = NULL;
/* + 3 for 2 quotes and null terminator */
cmd = malloc(strlen(dir) + strlen(MKDIR_COMMAND) + 3);
if (!cmd) {
printf("Failed to allocate memory for mkdir command.\n");
return -1;
}
strcpy(cmd, MKDIR_COMMAND);
/* Add quotes for special characters from directory path */
strcat(cmd, QUOTES);
strcat(cmd, dir);
strcat(cmd, QUOTES);
if (system(cmd) < 0) {
printf("Unable to create key and blob location path %s\n", dir);
free(cmd);
return -1;
}
free(cmd);
return 0;
}
int main(int argc, char *argv[])
{
char *key_name = NULL;
char *key_enc = NULL;
char *key_mode = NULL;
char *key_value = NULL;
char *blob_name = NULL;
char *key_path = NULL;
struct stat st = {0};
int status;
const char *op = argc >= 2 ? argv[1] : NULL;
if (argc < 2)
goto out_usage;
if (!strcmp(op, "create")) {
if (argc < 6)
goto out_usage;
key_name = argv[2];
key_enc = argv[3];
key_mode = argv[4];
key_value = argv[5];
} else if (!strcmp(op, "import")) {
if (argc < 4)
goto out_usage;
blob_name = argv[2];
key_name = argv[3];
} else {
goto out_usage;
}
caam_keygen_fd = open(DEVICE_NAME, O_RDWR);
if (caam_keygen_fd < 0) {
printf("Unable to open device %s\n", DEVICE_NAME);
return -1;
}
/* Create the directory for key and blob files */
status = caam_create_keyblob_path(KEYBLOB_LOCATION);
if (status < 0)
return -1;
key_path = malloc(strlen(key_name) + strlen(KEYBLOB_LOCATION) + 1);
if (!key_path) {
printf("Failed to allocate memory for key path.\n");
return -1;
}
strcpy(key_path, KEYBLOB_LOCATION);
strcat(key_path, key_name);
if (!strcmp(op, "create"))
caam_keygen_create(key_path, key_enc, key_mode, key_value);
if (!strcmp(op, "import"))
caam_keygen_import(blob_name, key_path);
close(caam_keygen_fd);
free(key_path);
goto out;
out_usage:
caam_keygen_usage();
out:
return 0;
}