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
|
/*
* Exynos FMP driver
*
* Copyright (C) 2018 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/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/slab.h>
#include <linux/crypto.h>
#include <linux/scatterlist.h>
#include <crypto/fmp.h>
#include <crypto/diskcipher.h>
static int fmp_crypt(struct crypto_tfm *tfm, void *priv)
{
struct fmp_crypto_info *ci = crypto_tfm_ctx(tfm);
return exynos_fmp_crypt(ci, priv);
}
static int fmp_clear(struct crypto_tfm *tfm, void *priv)
{
struct fmp_crypto_info *ci = crypto_tfm_ctx(tfm);
return exynos_fmp_clear(ci, priv);
}
static int fmp_setkey(struct crypto_tfm *tfm, const char *in_key, u32 keylen,
bool persistent)
{
struct fmp_crypto_info *ci = crypto_tfm_ctx(tfm);
return exynos_fmp_setkey(ci, (char *)in_key, keylen, persistent);
}
static int fmp_clearkey(struct crypto_tfm *tfm)
{
struct fmp_crypto_info *ci = crypto_tfm_ctx(tfm);
return exynos_fmp_clearkey(ci);
}
#ifndef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
static int fmp_do_test_crypt(struct crypto_tfm *tfm,
struct diskcipher_test_request *req)
{
struct fmp_crypto_info *ci = crypto_tfm_ctx(tfm);
struct crypto_diskcipher *diskc = __crypto_diskcipher_cast(tfm);
if (!req) {
pr_err("%s: invalid parameter\n", __func__);
return -EINVAL;
}
return exynos_fmp_test_crypt(ci, req->iv, diskc->ivsize,
sg_virt(req->src), sg_virt(req->dst),
req->cryptlen, req->enc ? 1 : 0, diskc);
}
#endif
static void *fmp_ctx;
static inline void fmp_algo_init(struct crypto_tfm *tfm,
enum fmp_crypto_algo_mode algo)
{
struct fmp_crypto_info *ci = crypto_tfm_ctx(tfm);
struct crypto_diskcipher *diskc = __crypto_diskcipher_cast(tfm);
/* This field's stongly aligned 'fmp_crypto_info->use_diskc' */
diskc->algo = (u32)algo;
diskc->ivsize = FMP_IV_SIZE_16;
ci->ctx = fmp_ctx;
ci->algo_mode = algo;
}
static int fmp_aes_xts_init(struct crypto_tfm *tfm)
{
fmp_algo_init(tfm, EXYNOS_FMP_ALGO_MODE_AES_XTS);
return 0;
}
static int fmp_cbc_aes_init(struct crypto_tfm *tfm)
{
fmp_algo_init(tfm, EXYNOS_FMP_ALGO_MODE_AES_CBC);
return 0;
}
static struct diskcipher_alg fmp_algs[] = {{
.base = {
.cra_name = "xts(aes)-disk",
.cra_driver_name = "xts(aes)-disk(fmp)",
.cra_priority = 200,
.cra_module = THIS_MODULE,
.cra_ctxsize = sizeof(struct fmp_crypto_info),
.cra_init = fmp_aes_xts_init,
}
}, {
.base = {
.cra_name = "cbc(aes)-disk",
.cra_driver_name = "cbc(aes)-disk(fmp)",
.cra_priority = 200,
.cra_module = THIS_MODULE,
.cra_ctxsize = sizeof(struct fmp_crypto_info),
.cra_init = fmp_cbc_aes_init,
}
} };
static int exynos_fmp_probe(struct platform_device *pdev)
{
struct diskcipher_alg *alg;
int ret;
int i;
fmp_ctx = exynos_fmp_init(pdev);
if (!fmp_ctx) {
dev_err(&pdev->dev,
"%s: Fail to register diskciphero\n", __func__);
return -EINVAL;
}
dev_set_drvdata(&pdev->dev, fmp_ctx);
for (i = 0; i < ARRAY_SIZE(fmp_algs); i++) {
alg = &fmp_algs[i];
alg->setkey = fmp_setkey;
alg->clearkey = fmp_clearkey;
alg->crypt = fmp_crypt;
alg->clear = fmp_clear;
#ifdef USE_FREE_REQ
alg->freectrl.max_io_ms = 3000;
#endif
#ifndef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
alg->do_crypt = fmp_do_test_crypt;
#endif
}
ret = crypto_register_diskciphers(fmp_algs, ARRAY_SIZE(fmp_algs));
if (ret) {
dev_err(&pdev->dev,
"%s: Fail to register diskciphero. ret = %d\n",
__func__, ret);
return -EINVAL;
}
dev_info(&pdev->dev, "Exynos FMP driver is registered to diskcipher\n");
return 0;
}
static int exynos_fmp_remove(struct platform_device *pdev)
{
void *drv_data = dev_get_drvdata(&pdev->dev);
if (!drv_data) {
pr_err("%s: Fail to get drvdata\n", __func__);
return 0;
}
crypto_unregister_diskciphers(fmp_algs, ARRAY_SIZE(fmp_algs));
exynos_fmp_exit(drv_data);
return 0;
}
static const struct of_device_id exynos_fmp_match[] = {
{.compatible = "samsung,exynos-fmp"},
{},
};
static struct platform_driver exynos_fmp_driver = {
.driver = {
.name = "exynos-fmp",
.owner = THIS_MODULE,
.pm = NULL,
.of_match_table = exynos_fmp_match,
},
.probe = exynos_fmp_probe,
.remove = exynos_fmp_remove,
};
static int __init fmp_init(void)
{
return platform_driver_register(&exynos_fmp_driver);
}
late_initcall(fmp_init);
static void __exit fmp_exit(void)
{
platform_driver_unregister(&exynos_fmp_driver);
}
module_exit(fmp_exit);
MODULE_DESCRIPTION("Exynos Spedific crypto algo driver");
|