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
|
/*
* Exynos FMP libary
*
* Copyright (C) 2015 Samsung Electronics Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/smc.h>
#include <crypto/fmp.h>
#include <asm/cacheflush.h>
#include "fmp_fips_info.h"
#define WORD_SIZE 4
#define byte2word(b0, b1, b2, b3) \
(((unsigned int)(b0) << 24) | \
((unsigned int)(b1) << 16) | \
((unsigned int)(b2) << 8) | (b3))
#define get_word(x, c) byte2word(((unsigned char *)(x) + 4 * (c))[0], \
((unsigned char *)(x) + 4 * (c))[1], \
((unsigned char *)(x) + 4 * (c))[2], \
((unsigned char *)(x) + 4 * (c))[3])
static int check_aes_xts_size(struct fmp_table_setting *table,
bool cmdq_enabled)
{
int size;
if (cmdq_enabled)
size = GET_CMDQ_LENGTH(table);
else
size = GET_LENGTH(table);
return (size > MAX_AES_XTS_TRANSFER_SIZE) ? size : 0;
}
static int check_aes_xts_key(char *key, enum fmp_crypto_key_size key_size)
{
char *enckey, *twkey;
enckey = key;
twkey = key + key_size;
return (memcmp(enckey, twkey, key_size)) ? 0 : -1;
}
int fmplib_set_algo_mode(struct exynos_fmp *fmp,
struct fmp_table_setting *table,
struct fmp_crypto_setting *crypto,
enum fmp_crypto_enc_mode enc_mode,
bool cmdq_enabled)
{
int ret = 0;
enum fmp_crypto_algo_mode algo_mode;
if (!fmp || !fmp->dev) {
pr_err("%s: invalid fmp device\n", __func__);
ret = -EINVAL;
goto err;
}
if (!table || !crypto) {
dev_err(fmp->dev, "%s: Invalid fmp table or crypto\n",
__func__);
ret = -EINVAL;
goto err;
}
algo_mode = crypto->algo_mode;
if (algo_mode == EXYNOS_FMP_ALGO_MODE_AES_XTS) {
ret = check_aes_xts_size(table, cmdq_enabled);
if (ret) {
dev_err(fmp->dev,
"%s: Fail FMP XTS due to invalid size(%d)\n",
__func__, ret);
ret = -EINVAL;
goto err;
}
}
switch (enc_mode) {
case EXYNOS_FMP_FILE_ENC:
if (cmdq_enabled)
SET_CMDQ_FAS(table, algo_mode);
else
SET_FAS(table, algo_mode);
break;
case EXYNOS_FMP_DISK_ENC:
if (cmdq_enabled)
SET_CMDQ_DAS(table, algo_mode);
else
SET_DAS(table, algo_mode);
break;
default:
dev_err(fmp->dev, "%s: Invalid fmp enc mode %d\n",
__func__, enc_mode);
ret = -EINVAL;
goto err;
}
err:
return ret;
}
int fmplib_set_key(struct exynos_fmp *fmp,
struct fmp_table_setting *table,
struct fmp_crypto_setting *crypto,
enum fmp_crypto_enc_mode enc_mode)
{
int ret = 0;
enum fmp_crypto_algo_mode algo_mode;
enum fmp_crypto_key_size key_size;
char *key;
int idx, max;
if (!fmp || !fmp->dev) {
pr_err("%s: invalid fmp device\n", __func__);
ret = -EINVAL;
goto err;
}
if (!table || !crypto) {
dev_err(fmp->dev, "%s: Invalid fmp table or crypto\n",
__func__);
ret = -EINVAL;
goto err;
}
algo_mode = crypto->algo_mode;
key = crypto->key;
key_size = crypto->key_size;
if (algo_mode == EXYNOS_FMP_ALGO_MODE_AES_XTS) {
if (check_aes_xts_key(key, key_size)) {
dev_err(fmp->dev,
"%s: Fail FMP XTS due to the same enc and twkey\n",
__func__);
ret = -EINVAL;
goto err;
}
}
switch (enc_mode) {
case EXYNOS_FMP_FILE_ENC:
if (algo_mode == EXYNOS_FMP_ALGO_MODE_AES_CBC) {
max = key_size / WORD_SIZE;
for (idx = 0; idx < max; idx++)
*(&table->file_enckey0 + idx) =
get_word(key, max - (idx + 1));
} else if (algo_mode == EXYNOS_FMP_ALGO_MODE_AES_XTS) {
key_size *= 2;
max = key_size / WORD_SIZE;
for (idx = 0; idx < (max / 2); idx++)
*(&table->file_enckey0 + idx) =
get_word(key, (max / 2) - (idx + 1));
for (idx = 0; idx < (max / 2); idx++)
*(&table->file_twkey0 + idx) =
get_word(key, max - (idx + 1));
}
break;
case EXYNOS_FMP_DISK_ENC:
__flush_dcache_area(key, (size_t)FMP_MAX_KEY_SIZE);
ret = exynos_smc(SMC_CMD_FMP_DISK_KEY_STORED, 0,
virt_to_phys(key), key_size);
if (ret) {
dev_err(fmp->dev, "%s: Fail to set FMP disk key. ret = %d\n",
__func__, ret);
fmp->status_disk_key = KEY_ERROR;
ret = -EINVAL;
goto err;
}
fmp->status_disk_key = KEY_STORED;
break;
default:
dev_err(fmp->dev, "%s: Invalid fmp enc mode %d\n", __func__, enc_mode);
ret = -EINVAL;
goto err;
}
err:
return ret;
}
int fmplib_set_key_size(struct exynos_fmp *fmp,
struct fmp_table_setting *table,
struct fmp_crypto_setting *crypto,
enum fmp_crypto_enc_mode enc_mode,
bool cmdq_enabled)
{
int ret = 0;
enum fmp_crypto_key_size key_size;
if (!fmp || !fmp->dev) {
pr_err("%s: invalid fmp device\n", __func__);
ret = -EINVAL;
goto err;
}
if (!table || !crypto) {
dev_err(fmp->dev, "%s: Invalid fmp table or crypto\n",
__func__);
ret = -EINVAL;
goto err;
}
key_size = crypto->key_size;
switch (enc_mode) {
case EXYNOS_FMP_FILE_ENC:
if (cmdq_enabled)
SET_CMDQ_KEYLEN(table, (key_size == FMP_KEY_SIZE_32) ? FKL_CMDQ : 0);
else
SET_KEYLEN(table, (key_size == FMP_KEY_SIZE_32) ? FKL : 0);
break;
case EXYNOS_FMP_DISK_ENC:
if (cmdq_enabled)
SET_CMDQ_KEYLEN(table, (key_size == FMP_KEY_SIZE_32) ? DKL_CMDQ : 0);
else
SET_KEYLEN(table, (key_size == FMP_KEY_SIZE_32) ? DKL : 0);
break;
default:
dev_err(fmp->dev, "%s: Invalid fmp enc mode %d\n", __func__, enc_mode);
ret = -EINVAL;
goto err;
}
err:
return ret;
}
int fmplib_clear_disk_key(struct exynos_fmp *fmp)
{
int ret = 0;
if (!fmp || !fmp->dev) {
pr_err("%s: invalid fmp device\n", __func__);
ret = -EINVAL;
goto err;
}
ret = exynos_smc(SMC_CMD_FMP_DISK_KEY_CLEAR, 0, 0, 0);
if (ret) {
dev_err(fmp->dev, "%s: Fail to clear FMP disk key. ret = %d\n",
__func__, ret);
fmp->status_disk_key = KEY_ERROR;
ret = -EPERM;
goto err;
}
fmp->status_disk_key = KEY_CLEAR;
err:
return ret;
}
int fmplib_clear(struct exynos_fmp *fmp, struct fmp_table_setting *table)
{
int ret = 0;
struct exynos_fmp_fips_test_vops *test_vops;
if (!fmp || !fmp->dev) {
pr_err("%s: invalid fmp device\n", __func__);
ret = -EINVAL;
goto err;
}
if (!table) {
dev_err(fmp->dev, "%s: Invalid fmp table\n", __func__);
ret = -EINVAL;
goto err;
}
test_vops = (struct exynos_fmp_fips_test_vops *)fmp->test_vops;
if (test_vops) {
ret = test_vops->zeroization(table, "before");
if (ret)
dev_err(fmp->dev,
"%s: Fail to check zeroization(before)\n",
__func__);
}
memset(&table->file_iv0, 0, sizeof(__le32) * 24);
if (test_vops) {
ret = test_vops->zeroization(table, "after");
if (ret)
dev_err(fmp->dev,
"%s: Fail to check zeroization(after)\n",
__func__);
}
err:
return ret;
}
int fmplib_set_iv(struct exynos_fmp *fmp,
struct fmp_table_setting *table,
struct fmp_crypto_setting *crypto,
enum fmp_crypto_enc_mode enc_mode)
{
int idx, max;
int ret = 0;
uint8_t *iv;
if (!fmp || !fmp->dev) {
pr_err("%s: invalid fmp device\n", __func__);
ret = -EINVAL;
goto err;
}
if (!table || !crypto) {
dev_err(fmp->dev, "%s: Invalid fmp table or crypto\n",
__func__);
ret = -EINVAL;
goto err;
}
iv = crypto->iv;
max = FMP_IV_SIZE_16 / WORD_SIZE;
switch (enc_mode) {
case EXYNOS_FMP_FILE_ENC:
for (idx = 0; idx < max; idx++)
*(&table->file_iv0 + idx) = get_word(iv, max - (idx + 1));
break;
case EXYNOS_FMP_DISK_ENC:
for (idx = 0; idx < max; idx++)
*(&table->disk_iv0 + idx) = get_word(iv, max - (idx + 1));
break;
default:
dev_err(fmp->dev, "%s: Invalid fmp enc mode %d\n", __func__, enc_mode);
ret = -EINVAL;
goto err;
}
err:
return ret;
}
|