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
|
/*
* Exynos FMP FIPS functional test
*
* Copyright (C) 2017 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/string.h>
#include <linux/printk.h>
#include <linux/device.h>
#include <crypto/fmp.h>
#include "fmp_fips_info.h"
#include "fmp_fips_main.h"
#include "fmp_fips_func_test.h"
static char *fips_functest_mode;
static char *fips_functest_KAT_list[] = {
"aes-xts",
"aes-cbc",
"sha256",
"hmac-sha256",
"integrity",
"zeroization"
};
void set_fmp_fips_functest_KAT_mode(const int num)
{
if (num >= 0 && num < FMP_FUNCTEST_KAT_CASE_NUM)
fips_functest_mode = fips_functest_KAT_list[num];
else
fips_functest_mode = FMP_FUNCTEST_NO_TEST;
}
char *get_fmp_fips_functest_mode(void)
{
if (fips_functest_mode)
return fips_functest_mode;
else
return FMP_FUNCTEST_NO_TEST;
}
static int fmp_func_test_integrity(HMAC_SHA256_CTX *desc,
unsigned long *start_addr)
{
int ret = 0;
if (!strcmp("integrity", get_fmp_fips_functest_mode())) {
pr_info("FIPS(%s): Failing Integrity Test\n", __func__);
ret = hmac_sha256_update(desc,
(unsigned char *)start_addr, 1);
}
return ret;
}
static int fmp_func_test_zeroization(struct fmp_table_setting *table,
char *str)
{
if (!table || !str) {
pr_err("%s: invalid fmp table address or string\n", __func__);
return -EINVAL;
}
if (!strcmp("zeroization", get_fmp_fips_functest_mode())) {
pr_info("FIPS FMP descriptor zeroize (%s) ", str);
print_hex_dump(KERN_ERR, "FIPS FMP descriptor zeroize: ",
DUMP_PREFIX_NONE, 16, 1, &table->file_iv0,
sizeof(__le32) * 20, false);
}
return 0;
}
static int fmp_func_test_hmac_sha256(char *digest, char *algorithm)
{
char *str = get_fmp_fips_functest_mode();
if (!digest || !algorithm) {
pr_err("%s: invalid digest address or algorithm\n", __func__);
return -EINVAL;
}
if (!strcmp(algorithm, str))
digest[0] += 1;
return 0;
}
static int fmp_func_test_aes(const int mode, char *key, unsigned char klen)
{
char *str = get_fmp_fips_functest_mode();
if (!key || (klen == 0)) {
pr_err("%s: invalid fmp key or key length\n", __func__);
return -EINVAL;
}
if ((!strcmp("aes-xts", str) && XTS_MODE == mode)
|| (!strcmp("aes-cbc", str) && CBC_MODE == mode))
key[0] += 1;
return 0;
}
const struct exynos_fmp_fips_test_vops exynos_fmp_fips_test_ops = {
.integrity = fmp_func_test_integrity,
.zeroization = fmp_func_test_zeroization,
.hmac_sha256 = fmp_func_test_hmac_sha256,
.aes = fmp_func_test_aes,
};
int exynos_fmp_func_test_KAT_case(struct platform_device *pdev,
struct exynos_fmp *fmp,
struct exynos_fmp_variant_ops *fmp_vops)
{
int i, ret;
struct fmp_data_setting data;
struct device *dev;
if (!fmp || !fmp->dev) {
pr_err("%s: invalid fmp context or device\n", __func__);
ret = -EINVAL;
goto out;
}
if (!fmp_vops) {
pr_err("%s: invalid fmp variant ops\n", __func__);
ret = -EINVAL;
goto out;
}
dev = fmp->dev;
fmp->test_vops = (struct exynos_fmp_fips_test_vops *)&exynos_fmp_fips_test_ops;
for (i = 0; i < FMP_FUNCTEST_KAT_CASE_NUM; i++) {
set_fmp_fips_functest_KAT_mode(i);
dev_info(dev, "FIPS FUNC : -----------------------------------\n");
dev_info(dev, "FIPS FUNC : Test case %d - [%s]\n",
i + 1, get_fmp_fips_functest_mode());
dev_info(dev, "FIPS FUNC : -----------------------------------\n");
ret = exynos_fmp_fips_init(fmp);
if (ret) {
dev_err(dev, "FIPS FUNC : Fail to initialize fmp fips. ret(%d)",
ret);
exynos_fmp_fips_exit(fmp);
goto out;
}
dev_info(dev, "FIPS FUNC : (%d-1) Selftest done. FMP FIPS status : %s\n",
i + 1, in_fmp_fips_err() ? "FAILED" : "PASSED");
memset(&data.disk, 0, sizeof(struct fmp_crypto_setting));
memset(&data.file, 0, sizeof(struct fmp_crypto_setting));
data.table = NULL;
data.cmdq_enabled = 0;
dev_info(dev, "FIPS FUNC : (%d-2) Try to set config\n", i + 1);
ret = fmp_vops->config(pdev, &data);
if (ret)
dev_info(dev,
"FIPS FUNC : (%d-3) Fail FMP config as expected. ret(%d)\n",
i + 1, ret);
exynos_fmp_fips_exit(fmp);
}
set_fmp_fips_functest_KAT_mode(-1);
dev_info(dev, "FIPS FUNC : Functional test end\n");
dev_info(dev, "FIPS FUNC : -----------------------------------\n");
dev_info(dev, "FIPS FUNC : Normal init start\n");
dev_info(dev, "FIPS FUNC : -----------------------------------\n");
ret = 0;
out:
fmp->test_vops = NULL;
return ret;
}
|