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
336
337
338
339
340
341
342
343
344
|
/*
* 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.
*
* Functions related to blk-crypt(for inline encryption) handling
*
* NOTE :
* blk-crypt implementation not depends on a inline encryption hardware.
*
* TODO :
* We need to agonize what lock we use between spin_lock_irq and spin_lock
* for algorithm_manager.
*/
#include <linux/module.h>
#include <linux/blkdev.h>
#include <crypto/skcipher.h>
#define BLK_CRYPT_ALG_NAMELEN_MAX (15)
#define BLK_CRYPT_VERSION "1.0.0"
#define BLK_CRYPT_MODE_PRIVATE (127)
struct blk_crypt_algorithm {
struct list_head list;
atomic_t ref;
unsigned int mode;
struct block_device *bdev;
char name[BLK_CRYPT_ALG_NAMELEN_MAX+1];
const struct blk_crypt_algorithm_cbs *hw_cbs;
};
struct blk_crypt_context {
struct blk_crypt_algorithm *bc_alg;
void *bc_private;
};
struct blk_crypt_algorithm_manager {
struct list_head list;
spinlock_t lock;
atomic_t ref;
struct blk_crypt_algorithm *last_acc;
};
/* Initialize algorithm manager */
static struct blk_crypt_algorithm_manager alg_manager = {
.list = LIST_HEAD_INIT(alg_manager.list),
.lock = __SPIN_LOCK_UNLOCKED(alg_manager.lock),
.ref = ATOMIC_INIT(0),
.last_acc = NULL,
};
static struct kmem_cache *blk_crypt_cachep = NULL;
/* Static functions */
/**
* blk_crypt_alloc_context() - allocate blk_crypt_context structure
* @mode: filesystem encryption mode.
*
* Find hardware algorithm matched with specified mode and
* calling the hw callback function to allocate crypt_info structure.
*
* Return: Allocated blk_crypt_context if succeed to find algorithm mode,
* -ENOKEY otherwise.
*/
static blk_crypt_t *blk_crypt_alloc_context(struct block_device *bdev, const char *cipher_str)
{
struct blk_crypt_algorithm *alg;
struct blk_crypt_context *bctx;
int res = -ENOENT;
spin_lock_irq(&alg_manager.lock);
if (alg_manager.last_acc && !strcmp(alg_manager.last_acc->name, cipher_str)) {
alg = alg_manager.last_acc;
atomic_inc(&alg->ref);
goto unlock;
}
list_for_each_entry(alg, &alg_manager.list, list) {
if (strcmp(alg->name, cipher_str)) {
pr_info("blk-crypt: no matched alg(%s) != requested alg(%s)\n",
alg->name, cipher_str);
continue;
}
if (alg->bdev && alg->bdev != bdev) {
pr_info("blk-crypt: no matched alg(%s)->bdev != bdev\n",
cipher_str);
continue;
}
alg_manager.last_acc = alg;
atomic_inc(&alg->ref);
break;
}
unlock:
spin_unlock_irq(&alg_manager.lock);
if (alg == (void *)&alg_manager.list)
return ERR_PTR(-ENOENT);
bctx = kmem_cache_zalloc(blk_crypt_cachep, GFP_NOFS);
if (!bctx) {
res = -ENOMEM;
goto err_free;
}
bctx->bc_alg = alg;
bctx->bc_private = alg->hw_cbs->alloc();
if (IS_ERR(bctx->bc_private)) {
res = PTR_ERR(bctx->bc_private);
pr_err("blk-crypt: failed to alloc data for alg(%s), err:%d\n",
alg->name, res);
goto err_free;
}
return bctx;
err_free:
atomic_dec(&alg->ref);
if (bctx)
kmem_cache_free(blk_crypt_cachep, bctx);
return ERR_PTR(res);
}
static inline blk_crypt_t *__bio_crypt(const struct bio *bio)
{
if (bio && (bio->bi_opf & REQ_CRYPT))
return bio->bi_cryptd;
return NULL;
}
bool blk_crypt_encrypted(const struct bio *bio)
{
return __bio_crypt(bio) ? true : false;
}
bool blk_crypt_mergeable(const struct bio *a, const struct bio *b)
{
#ifdef CONFIG_BLK_DEV_CRYPT
if (__bio_crypt(a) != __bio_crypt(b))
return false;
#ifdef CONFIG_BLK_DEV_CRYPT_DUN
if (__bio_crypt(a)) {
if (!bio_dun(a) ^ !bio_dun(b))
return false;
if (bio_dun(a) && bio_end_dun(a) != bio_dun(b))
return false;
}
#endif
#endif
return true;
}
/* Filesystem APIs */
blk_crypt_t *blk_crypt_get_context(struct block_device *bdev, const char *cipher_str)
{
struct blk_crypt_t *bctx;
bctx = blk_crypt_alloc_context(bdev, cipher_str);
if (IS_ERR(bctx)) {
pr_debug("error allocating diskciher '%s' err: %d",
cipher_str, PTR_ERR(bctx));
return bctx;
}
return bctx;
}
unsigned char *blk_crypt_get_key(blk_crypt_t *bc_ctx)
{
struct blk_crypt_context *bctx = bc_ctx;
if (!bctx)
return ERR_PTR(-EINVAL);
return bctx->bc_alg->hw_cbs->get_key(bctx->bc_private);
}
int blk_crypt_set_key(blk_crypt_t *bc_ctx, u8 *raw_key, u32 keysize)
{
struct blk_crypt_context *bctx = bc_ctx;
if (!bctx)
return -EINVAL;
return bctx->bc_alg->hw_cbs->set_key(bctx->bc_private, raw_key, keysize);
}
void *blk_crypt_get_data(blk_crypt_t *bc_ctx)
{
struct blk_crypt_context *bctx = bc_ctx;
return bctx->bc_private;
}
void blk_crypt_put_context(blk_crypt_t *bc_ctx)
{
struct blk_crypt_context *bctx = bc_ctx;
if (!bctx)
return;
bctx->bc_alg->hw_cbs->free(bctx->bc_private);
atomic_dec(&(bctx->bc_alg->ref));
kmem_cache_free(blk_crypt_cachep, bctx);
}
/* H/W algorithm APIs */
static int blk_crypt_initialize()
{
if (likely(blk_crypt_cachep))
return 0;
blk_crypt_cachep = KMEM_CACHE(blk_crypt_context, SLAB_RECLAIM_ACCOUNT);
if (!blk_crypt_cachep)
return -ENOMEM;
return 0;
}
/**
* blk_crypt_alg_register() - register a hw algorithm in algorithm manager
* @name: name of algorithm
* @mode: filesystem encryption mode
* @hw_cbs: hardware callback functions
*
* A mode and name must be unique value so that these values are used to
* differentiate each algorithm. If the algorithm manager finds duplicated
* mode or name in algorithm list, it considers that as existing algorithm.
*
* Return: hw algorithm pointer on success,
* -EEXIST if already registered.
*/
void *blk_crypt_alg_register(struct block_device *bdev, const char *name,
const unsigned int mode, const struct blk_crypt_algorithm_cbs *hw_cbs)
{
struct blk_crypt_algorithm *hw_alg, *new_alg;
if (unlikely(blk_crypt_initialize())) {
pr_err("blk-crypt: failed to initialize\n");
return ERR_PTR(-ENOMEM);
}
if (!name || strlen(name) > BLK_CRYPT_ALG_NAMELEN_MAX) {
pr_err("blk-crypt: invalid algorithm name or namelen\n");
return ERR_PTR(-EINVAL);
}
/* Allocate new dhe_algorithm */
new_alg = kzalloc(sizeof(struct blk_crypt_algorithm), GFP_KERNEL);
if (!new_alg)
return ERR_PTR(-ENOMEM);
/* Initialize new hw_algorithm */
strncpy(new_alg->name, name, BLK_CRYPT_ALG_NAMELEN_MAX);
new_alg->bdev = bdev;
new_alg->mode = mode;
new_alg->hw_cbs = hw_cbs;
INIT_LIST_HEAD(&new_alg->list);
/* Check the existence of same thing. */
spin_lock_irq(&alg_manager.lock);
list_for_each_entry(hw_alg, &alg_manager.list, list) {
/* an algorithm can be allocated for multiple devices */
if (hw_alg->bdev && bdev && (hw_alg->bdev != bdev))
continue;
if (hw_alg->mode == mode)
break;
if (!strcmp(hw_alg->name, name))
break;
}
/* Already exist, can not register new one */
if (&alg_manager.list != &hw_alg->list) {
spin_unlock_irq(&alg_manager.lock);
pr_err("blk-crypt: algorithm(%s) already exists\n", name);
kfree(new_alg);
return ERR_PTR(-EEXIST);
}
/* Add to algorithm manager */
list_add(&new_alg->list, &alg_manager.list);
spin_unlock_irq(&alg_manager.lock);
pr_info("blk_crypt: registed algorithm(%s)\n", name);
return new_alg;
}
EXPORT_SYMBOL(blk_crypt_alg_register);
/**
* blk_crypt_alg_unregister() - unregister hw algorithm from the algorithm manager
* @handle: The pointer of hw algorithm that returned by blk_crypt_alg_register().
*
* Free and unregister specified handle if there is no ref count.
*/
int blk_crypt_alg_unregister(void *handle)
{
struct blk_crypt_algorithm *alg;
if (!handle)
return -EINVAL;
alg = (struct blk_crypt_algorithm *)handle;
/* prevent unregister if an algorithm is being referenced */
if (atomic_read(&alg->ref))
return -EBUSY;
/* just unlink this algorithm from the manager */
spin_lock_irq(&alg_manager.lock);
list_del_init(&alg->list);
spin_unlock_irq(&alg_manager.lock);
kfree(handle);
return 0;
}
EXPORT_SYMBOL(blk_crypt_alg_unregister);
/* Blk-crypt core functions */
static int __init blk_crypt_init(void)
{
pr_info("%s\n", __func__);
blk_crypt_initialize();
return 0;
}
static void __exit blk_crypt_exit(void)
{
pr_info("%s\n", __func__);
if (atomic_read(&alg_manager.ref)) {
pr_warn("blk-crypt: remailed %d registered algorithm\n",
atomic_read(&alg_manager.ref));
}
kmem_cache_destroy(blk_crypt_cachep);
blk_crypt_cachep = NULL;
}
module_init(blk_crypt_init);
module_exit(blk_crypt_exit);
|