summaryrefslogtreecommitdiffstats
path: root/drivers/s390/crypto/pkey_base.c
blob: fea24332283835731834fe7a552d478ba7ba4cb1 (plain)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// SPDX-License-Identifier: GPL-2.0
/*
 *  pkey base: debug feature, pkey handler registry
 *
 *  Copyright IBM Corp. 2024
 */

#define KMSG_COMPONENT "pkey"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

#include <linux/cpufeature.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/rculist.h>

#include "pkey_base.h"

MODULE_LICENSE("GPL");
MODULE_AUTHOR("IBM Corporation");
MODULE_DESCRIPTION("s390 protected key base and api");

/*
 * pkey debug feature
 */
debug_info_t *pkey_dbf_info;
EXPORT_SYMBOL(pkey_dbf_info);

/*
 * pkey handler registry
 */

static DEFINE_SPINLOCK(handler_list_write_lock);
static LIST_HEAD(handler_list);

int pkey_handler_register(struct pkey_handler *handler)
{
	const struct pkey_handler *h;

	if (!handler ||
	    !handler->is_supported_key ||
	    !handler->is_supported_keytype)
		return -EINVAL;

	if (!try_module_get(handler->module))
		return -ENXIO;

	spin_lock(&handler_list_write_lock);

	rcu_read_lock();
	list_for_each_entry_rcu(h, &handler_list, list) {
		if (h == handler) {
			rcu_read_unlock();
			spin_unlock(&handler_list_write_lock);
			module_put(handler->module);
			return -EEXIST;
		}
	}
	rcu_read_unlock();

	list_add_rcu(&handler->list, &handler_list);
	spin_unlock(&handler_list_write_lock);
	synchronize_rcu();

	module_put(handler->module);

	PKEY_DBF_INFO("%s pkey handler '%s' registered\n", __func__,
		      handler->name ?: "<no name>");

	return 0;
}
EXPORT_SYMBOL(pkey_handler_register);

int pkey_handler_unregister(struct pkey_handler *handler)
{
	spin_lock(&handler_list_write_lock);
	list_del_rcu(&handler->list);
	INIT_LIST_HEAD_RCU(&handler->list);
	spin_unlock(&handler_list_write_lock);
	synchronize_rcu();

	PKEY_DBF_INFO("%s pkey handler '%s' unregistered\n", __func__,
		      handler->name ?: "<no name>");

	return 0;
}
EXPORT_SYMBOL(pkey_handler_unregister);

/*
 * Handler invocation functions.
 */

const struct pkey_handler *pkey_handler_get_keybased(const u8 *key, u32 keylen)
{
	const struct pkey_handler *h;

	rcu_read_lock();
	list_for_each_entry_rcu(h, &handler_list, list) {
		if (!try_module_get(h->module))
			continue;
		if (h->is_supported_key(key, keylen)) {
			rcu_read_unlock();
			return h;
		}
		module_put(h->module);
	}
	rcu_read_unlock();

	return NULL;
}
EXPORT_SYMBOL(pkey_handler_get_keybased);

const struct pkey_handler *pkey_handler_get_keytypebased(enum pkey_key_type kt)
{
	const struct pkey_handler *h;

	rcu_read_lock();
	list_for_each_entry_rcu(h, &handler_list, list) {
		if (!try_module_get(h->module))
			continue;
		if (h->is_supported_keytype(kt)) {
			rcu_read_unlock();
			return h;
		}
		module_put(h->module);
	}
	rcu_read_unlock();

	return NULL;
}
EXPORT_SYMBOL(pkey_handler_get_keytypebased);

void pkey_handler_put(const struct pkey_handler *handler)
{
	const struct pkey_handler *h;

	if (!handler)
		return;

	rcu_read_lock();
	list_for_each_entry_rcu(h, &handler_list, list) {
		if (h == handler) {
			module_put(h->module);
			break;
		}
	}
	rcu_read_unlock();
}
EXPORT_SYMBOL(pkey_handler_put);

int pkey_handler_key_to_protkey(const struct pkey_apqn *apqns, size_t nr_apqns,
				const u8 *key, u32 keylen,
				u8 *protkey, u32 *protkeylen, u32 *protkeytype)
{
	const struct pkey_handler *h;
	int rc = -ENODEV;

	h = pkey_handler_get_keybased(key, keylen);
	if (h && h->key_to_protkey) {
		rc = h->key_to_protkey(apqns, nr_apqns, key, keylen,
				       protkey, protkeylen,
				       protkeytype);
	}
	pkey_handler_put(h);

	return rc;
}
EXPORT_SYMBOL(pkey_handler_key_to_protkey);

/*
 * This handler invocation is special as there may be more than
 * one handler providing support for the very same key (type).
 * And the handler may not respond true on is_supported_key(),
 * so simple try and check return value here.
 */
int pkey_handler_slowpath_key_to_protkey(const struct pkey_apqn *apqns,
					 size_t nr_apqns,
					 const u8 *key, u32 keylen,
					 u8 *protkey, u32 *protkeylen,
					 u32 *protkeytype)
{
	const struct pkey_handler *h, *htmp[10];
	int i, n = 0, rc = -ENODEV;

	rcu_read_lock();
	list_for_each_entry_rcu(h, &handler_list, list) {
		if (!try_module_get(h->module))
			continue;
		if (h->slowpath_key_to_protkey && n < ARRAY_SIZE(htmp))
			htmp[n++] = h;
		else
			module_put(h->module);
	}
	rcu_read_unlock();

	for (i = 0; i < n; i++) {
		h = htmp[i];
		if (rc)
			rc = h->slowpath_key_to_protkey(apqns, nr_apqns,
							key, keylen,
							protkey, protkeylen,
							protkeytype);
		module_put(h->module);
	}

	return rc;
}
EXPORT_SYMBOL(pkey_handler_slowpath_key_to_protkey);

int pkey_handler_gen_key(const struct pkey_apqn *apqns, size_t nr_apqns,
			 u32 keytype, u32 keysubtype,
			 u32 keybitsize, u32 flags,
			 u8 *keybuf, u32 *keybuflen, u32 *keyinfo)
{
	const struct pkey_handler *h;
	int rc = -ENODEV;

	h = pkey_handler_get_keytypebased(keysubtype);
	if (h && h->gen_key) {
		rc = h->gen_key(apqns, nr_apqns, keytype, keysubtype,
				keybitsize, flags,
				keybuf, keybuflen, keyinfo);
	}
	pkey_handler_put(h);

	return rc;
}
EXPORT_SYMBOL(pkey_handler_gen_key);

int pkey_handler_clr_to_key(const struct pkey_apqn *apqns, size_t nr_apqns,
			    u32 keytype, u32 keysubtype,
			    u32 keybitsize, u32 flags,
			    const u8 *clrkey, u32 clrkeylen,
			    u8 *keybuf, u32 *keybuflen, u32 *keyinfo)
{
	const struct pkey_handler *h;
	int rc = -ENODEV;

	h = pkey_handler_get_keytypebased(keysubtype);
	if (h && h->clr_to_key) {
		rc = h->clr_to_key(apqns, nr_apqns, keytype, keysubtype,
				   keybitsize, flags, clrkey, clrkeylen,
				   keybuf, keybuflen, keyinfo);
	}
	pkey_handler_put(h);

	return rc;
}
EXPORT_SYMBOL(pkey_handler_clr_to_key);

int pkey_handler_verify_key(const u8 *key, u32 keylen,
			    u16 *card, u16 *dom,
			    u32 *keytype, u32 *keybitsize, u32 *flags)
{
	const struct pkey_handler *h;
	int rc = -ENODEV;

	h = pkey_handler_get_keybased(key, keylen);
	if (h && h->verify_key) {
		rc = h->verify_key(key, keylen, card, dom,
				   keytype, keybitsize, flags);
	}
	pkey_handler_put(h);

	return rc;
}
EXPORT_SYMBOL(pkey_handler_verify_key);

int pkey_handler_apqns_for_key(const u8 *key, u32 keylen, u32 flags,
			       struct pkey_apqn *apqns, size_t *nr_apqns)
{
	const struct pkey_handler *h;
	int rc = -ENODEV;

	h = pkey_handler_get_keybased(key, keylen);
	if (h && h->apqns_for_key)
		rc = h->apqns_for_key(key, keylen, flags, apqns, nr_apqns);
	pkey_handler_put(h);

	return rc;
}
EXPORT_SYMBOL(pkey_handler_apqns_for_key);

int pkey_handler_apqns_for_keytype(enum pkey_key_type keysubtype,
				   u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
				   struct pkey_apqn *apqns, size_t *nr_apqns)
{
	const struct pkey_handler *h;
	int rc = -ENODEV;

	h = pkey_handler_get_keytypebased(keysubtype);
	if (h && h->apqns_for_keytype) {
		rc = h->apqns_for_keytype(keysubtype,
					  cur_mkvp, alt_mkvp, flags,
					  apqns, nr_apqns);
	}
	pkey_handler_put(h);

	return rc;
}
EXPORT_SYMBOL(pkey_handler_apqns_for_keytype);

void pkey_handler_request_modules(void)
{
#ifdef CONFIG_MODULES
	static const char * const pkey_handler_modules[] = {
		"pkey_cca", "pkey_ep11", "pkey_pckmo" };
	int i;

	for (i = 0; i < ARRAY_SIZE(pkey_handler_modules); i++) {
		const struct pkey_handler *h;
		bool found = false;

		rcu_read_lock();
		list_for_each_entry_rcu(h, &handler_list, list) {
			if (h->module &&
			    !strcmp(h->module->name, pkey_handler_modules[i])) {
				found = true;
				break;
			}
		}
		rcu_read_unlock();
		if (!found) {
			pr_debug("request_module(%s)\n", pkey_handler_modules[i]);
			request_module(pkey_handler_modules[i]);
		}
	}
#endif
}
EXPORT_SYMBOL(pkey_handler_request_modules);

/*
 * Module init
 */
static int __init pkey_init(void)
{
	int rc;

	/* init debug feature */
	pkey_dbf_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
	debug_register_view(pkey_dbf_info, &debug_sprintf_view);
	debug_set_level(pkey_dbf_info, 4);

	/* the handler registry does not need any init */

	rc = pkey_api_init();
	if (rc)
		debug_unregister(pkey_dbf_info);

	return rc;
}

/*
 * Module exit
 */
static void __exit pkey_exit(void)
{
	pkey_api_exit();
}

module_cpu_feature_match(S390_CPU_FEATURE_MSA, pkey_init);
module_exit(pkey_exit);