xref: /linux/arch/s390/include/asm/pkey.h (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Kernelspace interface to the pkey device driver
4  *
5  * Copyright IBM Corp. 2016, 2023
6  *
7  * Author: Harald Freudenberger <freude@de.ibm.com>
8  *
9  */
10 
11 #ifndef _KAPI_PKEY_H
12 #define _KAPI_PKEY_H
13 
14 #include <linux/ioctl.h>
15 #include <linux/types.h>
16 #include <linux/delay.h>
17 #include <uapi/asm/pkey.h>
18 
19 /*
20  * In-kernel API: Transform an key blob (of any type) into a protected key.
21  * @param key pointer to a buffer containing the key blob
22  * @param keylen size of the key blob in bytes
23  * @param protkey pointer to buffer receiving the protected key
24  * @param xflags additional execution flags (see PKEY_XFLAG_* definitions below)
25  *	  As of now the only supported flags are PKEY_XFLAG_NOMEMALLOC
26  *	  and PKEY_XFLAG_NOCLEARKEY.
27  * @return 0 on success, negative errno value on failure
28  */
29 int pkey_key2protkey(const u8 *key, u32 keylen,
30 		     u8 *protkey, u32 *protkeylen, u32 *protkeytype,
31 		     u32 xflags);
32 
33 /*
34  * If this flag is given in the xflags parameter, the pkey implementation
35  * is not allowed to allocate memory but instead should fall back to use
36  * preallocated memory or simple fail with -ENOMEM.
37  * This flag is for protected key derive within a cipher or similar
38  * which must not allocate memory which would cause io operations - see
39  * also the CRYPTO_ALG_ALLOCATES_MEMORY flag in crypto.h.
40  */
41 #define PKEY_XFLAG_NOMEMALLOC 0x0001
42 
43 /*
44  * Do not accept a clear key token as source for a protected key.
45  */
46 #define PKEY_XFLAG_NOCLEARKEY 0x0002
47 
48 static inline int pkey_handle_expired(void)
49 {
50 	/*
51 	 * Protected key expired due to relocation to another host. The long
52 	 * running re-wrap has no asynchronous completion notification, so
53 	 * polling is required. Trigger a re-schedule of this request by
54 	 * returning -ENOSPC ("hardware queue full") to the crypto engine.
55 	 * To avoid immediately re-invocation of this callback,
56 	 * tell the scheduler to voluntarily give up the CPU here.
57 	 */
58 	msleep(1);
59 	pr_debug("rescheduling request\n");
60 	return -ENOSPC;
61 }
62 
63 #endif /* _KAPI_PKEY_H */
64