Skip to contentSkip to conversation

RFC: "New" /dev/crypto user-space interface

PATCH In review Miloslav Trmač 7 participants 15 messages Aug 5, 2010 – Aug 11, 2010 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover RFC: "New" /dev/crypto user-space interface

Hello,
following is a patchset providing an user-space interface to the kernel crypto API. It is based on the older, BSD-compatible, implementation, but the user-space interface is different.

These are the major differences compared to the BSD-like interface:

* The API supports key storage and management inside the kernel. An application can thus ask the kernel to generate a key; the key is then referenced via an integer identifier, and the application can be prevented from accessing the raw key data. Such a key can, if so configured, still be wrapped for key transport to the recipient of the message, and unwrapped by the recipient.

The kernel key storage does not span system reboots, but applications can also wrap the keys for persistent storage, receiving an encrypted blob that does not reveal the raw key data, but can be later loaded back into the kernel.

* More algorithms and mechanisms are supported by the API, including public key algorithms (RSA/DSA encryption and signing, D-H key derivation, key wrapping).

Motivations for the extensions: governments are asking for more security features in the operating systems they procure, which make user-space implementations impractical. A few examples:

* Advanced crypto module for OSPP for Common Criteria requires OS services implementing several low-level crypto algorithms (e.g. AES, RSA). This requires the separation of crypto services from the consumer of those services. (The threat model is that apps tend to have more vulnerabilities than libraries and compromise of the app will lead to the ability to access key material.) An user-space library is not separated, options are a) root running daemon that does crypto, but this would be slow due to context switches, scheduler mismatching and all the IPC overhead and b) use crypto that is in the kernel.

* FIPS-140-3 calls out for cryptographic functions to be non-debuggable (ptrace) meaning that you cannot get to the key material. The solution is the same as above.

* GPOSPP requires auditing for crypto events (so does FIPS-140 level 2 cert). To do this you need any crypto to have CAP_AUDIT_WRITE permissions which means making everything that links to openssl, libgcrypt, or nss setuid root. Making firefox and 400 other applications setuid root is a non-starter. So, the solution is again to use crypto in the kernel where auditing needs no special permissions.

Other advantages to having kernel crypto available to user space:

* User space will be able to take advantage of kernel drivers for hardware crypto accelerators.

* glibc, which in some configurations links to libfreebl3.so for hashes necessary for crypt(), will be able to use the kernel implementation; this means one less library to load and dynamically link for each such process.

The code is derived from the original cryptodev-linux patch set; most of the new implementation was written by Nikos Mavrogiannopoulos <[email protected]>. Attributions are included in the respective source files.

The patches are not in a sequence that allows compilation after applying a subset, they were split into logical groups to ease reviewing instead.

#1 lore raw

PATCH 1/4 User-space API definition

This patch introduces the new user-space API, <ncr.h>.

Quick overview:

* open("/dev/crypto") to get a FD, which acts as a namespace for key and session identifiers.

* ioctl(NCRIO_KEY_INIT) to allocate a key object; then generate the key material inside the kernel, load a plaintext key, unwrap a key, or derive a key. Similarly the key material can be copied out of the kernel or wrapped.

* ioctl(NCRIO_SESSION_INIT) to allocate a crypto session (to encrypt, decrypt, hash, sign, or verify signature), the ioctl(NCRIO_SESSION_UPDATE) to act on chunks of data. Deallocate the session, and optionally retrieve session results (e.g. hash or signature), using ioctl(NCRIO_SESSION_FINAL).

There is also NCRIO_SESSION_ONCE for an one-shot crypto operation using a single user->kernel context switch.

Full documentation of the interface follows.

NAME
/dev/crypto - kernel cryptographic module interface

SYNOPSIS

       #include <ncr.h>
       int fd = open("/dev/crypto", O_RDWR);
       int res = ioctl(fd, NCRIO..., &data);

DESCRIPTION
The /dev/crypto device file provides an ioctl(2) interface to the ker- nel-space crypto implementation.

Each open(2) of the /dev/crypto file establishes a separate namespace
within which crypto operations work. The namespace can be shared
across threads and processes by sharing the open file description. Last close of the open file description automatically destroys all objects allocated within the namespace.

All ioctl(2)s have the same form: The user sets up a data structure with input data, and passes a pointer to the data structure as the third parameter to ioctl(2). On success, output data is available in the same structure.

The following operations are defined:

NCRIO_KEY_INIT
Allocate a kernel-space key object. The parameter is not used on input (key attributes are set later, when the key material is
initialized). On success, an integer descriptor for the key
object (valid within the current /dev/crypto namespace) is stored in the provided area.

There is a per-process and per-user (not per-namespace) limit on the number key objects that can be allocated.

NCRIO_KEY_DEINIT
Deallocate a kernel-space key object. The parameter specifies the integer descriptor of the key object. After all other oper- ations using this key object (if any) terminate, the key mate- rial will be cleared and the object will be freed. Note that this may happen both before this operation returns, and after it returns, depending on other references to this key object.

NCRIO_KEY_GENERATE
Clear existing key material in the specified key object, and generate new key material.

The parameter points to struct ncr_key_generate_st, which speci- fies:

desc The key descriptor

params.algorithm
The crypto algorithm with which the key will be used

params.keyflags
Key flags, a combination of NCR_KEY_FLAG_EXPORTABLE (the key material can be exported in plaintext to user space) and NCR_KEY_FLAG_WRAPPABLE (the key material can be wrapped and the result made available to user space).

params.params
Algorithm-specific key parameters: For symmetric keys, key length in bits. For RSA keys, the public exponent and modulus length in bits. For DSA keys, p and q length in bits. For DH keys, the prime and group generator.

Currently only symmetric keys can be generated using this opera- tion.

In addition to generating the key material, the "persistent" key ID is reset to a random value.

NCRIO_KEY_GENERATE_PAIR
Similar to NCRIO_KEY_GENERATE, except that a pair of public/pri- vate keys is generated.

The parameter points to struct ncr_key_generate_st as specified above, with the additional member desc2 used to specify the key descriptor for the public key.

The NCR_KEY_FLAG_EXPORTABLE and NCR_KEY_FLAG_WRAPPABLE flags are automatically set on the public key.

NCRIO_KEY_DERIVE
Derive a new key using one key and additional data.

The parameter points to struct ncr_key_derivation_params_st, which specifies:

derive The derivation algorithm. Currently only NCR_DERIVE_DH
is supported.

newkey The descriptor of the resulting key

keyflags
Flags to use for the resulting key

key The source key descriptor

params Key type-specific parameters. For NCR_DERIVE_DH,
params.params.dh.pub and params.params.dh.pub_size spec- ify the peer's public key.

NCRIO_KEY_EXPORT

Export key material in the specified key object to user space. Only keys with the NCR_KEY_FLAG_EXPORTABLE flag can be exported using this operation.

The parameter points to struct ncr_key_data_st, which specifies:

key The key descriptor

idata Destination buffer

idata_size
Buffer size

Symmetric keys are written directly into the destination buffer. Public and private keys are formatted using ASN.1, except for DH public keys, which are written a raw binary number.

On success, the idata_size member is set to the size of the exported key.

NCRIO_KEY_IMPORT
Clear existing key material in the specified key object, and import key material from user space.

The parameter points to struct ncr_key_data_st, which specifies:

key The key descriptor

idata Source data

idata_size
Source data size

key_id New "persistent" key ID.

key_id_size
Size of data in key_id.

type Key type, one of NCR_KEY_TYPE_SECRET, NCR_KEY_TYPE_PUBLIC
and NCR_KEY_TYPE_PRIVATE.

algorithm
The crypto algorithm with which the key will be used

flags Key flags

The data format is the same as in the NCRIO_KEY_EXPORT opera- tion.

NCRIO_KEY_GET_INFO
Get metadata of an existing key.

The parameter points to struct ncr_key_info_st, which specifies key, the key descriptor. On success, the following members are set:

flags Key flags

type Key type

algorithm
Key algorithm

NCRIO_KEY_WRAP
Wrap one key using another, and write the result to user space. Only keys with the NCR_KEY_FLAG_WRAPPABLE flag can be wrapped using this operation.

The parameter points to struct ncr_key_wrap_st, which specifies:

algorithm
The wrapping algorithm to use, one of
NCR_WALG_AES_RFC3394 and NCR_WALG_AES_RFC5649.

keytowrap
The descriptor of the key to wrap

key The descriptor of the key used for wrapping

params Key type-specific parameters. For the currently sup-
ported wrapping algorithms, params.params.cipher.iv and params.params.cipher.iv_size specify the IV.

io Destination buffer

io_size
Size of the destination buffer

Currently only secret keys can be wrapped, using one of the above-mentioned AES-based algorithms.

On success, the io_size member is set to the size of the wrapped key.

NCRIO_KEY_UNWRAP
Unwrap user-space data into a kernel-space key using another key.

The parameter points to struct ncr_key_wrap_st, which specifies:

algorithm
The wrapping algorithm to use.

keytowrap
The descriptor of the target key object

key The descriptor of the key used for wrapping

params Key type-specific parameters. For the currently sup-
ported wrapping algorithms, params.params.cipher.iv and params.params.cipher.iv_size specify the IV.

io Pointer to the wrapped key

io_size
Size of the wrapped key

The unwrapped key will have the NCR_KEY_FLAG_WRAPPABLE flag set, and the NCR_KEY_FLAG_EXPORTABLE flag clear.

NCRIO_KEY_STORAGE_WRAP
Wrap a key object and associated metadata using the system-wide storage master key, and write the result to user space.

Only keys with the NCR_KEY_FLAG_WRAPPABLE flag can be wrapped using this operation.

The parameter points to struct ncr_key_storage_wrap_st, which specifies:

keytowrap
The descriptor of the key to wrap

io Destination buffer

io_size
Size of the destination buffer

On success, the io_size member is set to the size of the wrapped key.

Both symmetric and asymmetric keys can be wrapped using this
operation. The wrapped data includes the following information
in addition to the raw key material:

o Key type

o Key flags

o Key algorithm

o "Persistent" key ID.

NCRIO_KEY_STORAGE_UNWRAP
Unwrap key and associated metadata created using NCRIO_KEY_STOR- AGE_WRAP, and restore the information into a specified key object.

The parameter points to struct ncr_key_storage_wrap_st, which specifies:

keytowrap
The target key descriptor

io Wrapped data

io_size
Size of the wrapped data

See NCRIO_KEY_STORAGE_WRAP above for the list of attributes that will be restored.

NCRIO_SESSION_INIT
Allocate a session for performing crypto operations.

The parameter points to struct ncr_session_st, which specifies:

algorithm
The crypto algorithm to use.

key The key to use for the operation, if required.

params Parameters for the operation. For symmetric ciphers, the
IV. For RSA operations, the format, used hash algorithms and PSS salt length. for DSA, the signature hash algo- rithm.

op The operation to perform, one of NCR_OP_ENCRYPT,
NCR_OP_DECRYPT, NCR_OP_SIGN and NCR_OP_VERIFY. Use
NCR_OP_SIGN for computing an unkeyed hash as well as keyed hashes and signatures.

On success, an integer descriptor for the created session (valid within the current /dev/crypto namespace) is stored into the ses member.

NCRIO_SESSION_UPDATE
Update an existing crypto session with new data (for operations, such as hashing, for which data can be supplied in pieces), or perform a single operation using the session context (for opera- tions, such as public key encryption, that work on separate units of data).

The parameter points to struct ncr_session_op_st, which speci- fies:

ses The integer descriptor of the session.

type Type of the data references used for this operation,
either NCR_KEY_DATA or NCR_DIRECT_DATA.

data.udata.input, data.udata.input_size
If type == NCR_DIRECT_DATA, input data for the operation.

data.kdata.input
If type == NCR_KEY_DATA, integer key descriptor serving as input for the operation. This can be currently used only to compute or verify a signature or hash of a sym- metric key: the keying material is directly used as input data for the underlying hash.

data.udata.output, data,udata.output_size
If type == NCR_DIRECT_DATA, output buffer for the opera- tion.

data.kdata.output, data,kdata.output_size
If type == NCR_KEY_DATA, output buffer for the operation.

For the NCR_OP_ENCRYPT and NCR_OP_DECRYPT operations using sym- metric ciphers, the operation is performed on the input data, resulting in an output data block of the same size; for opera- tions using public-key cryptography, a single operation is per- formed on the input data, resulting in output data. In both cases, the relevant output_data member is set to the size of valid output data on success.

For the NCR_OP_SIGN and NCR_OP_VERIFY operations, the input data is supplied to the underlying hash function; no output data is produced.

NCRIO_SESSION_FINAL
Finalize an existing crypto session and deallocate it.

The parameter points to struct ncr_session_op_st, as described
in the NCRIO_SESSION_UPDATE section above. If the parameter
specifies valid input data, it is processed as if using NCRIO_SESSION_UPDATE; thus, the last update operation can be performed together with the finalization in one step.

There is no specific finalization operation performed for NCR_OP_ENCRYPT and NCR_OP_DECRYPT.

For the NCR_OP_SIGN operation, the signature is created and written as output data.

For the NCR_OP_VERIFY operation, a signature specified as input using the output data fields is verified; the result of this
operation (NCR_SUCCESS or NCR_VERIFICATION_FAILED) will be
stored into the err member. (Note that the ioctl(2) operation will indicate success even if the signature verification fails, as long all inputs were specified correctly.)

The session will be deallocated even if the NCRIO_SESSION_FINAL operation reports an error, as long as valid session descriptor was specified.

NCRIO_SESSION_ONCE
Perform an one-shot crypto operation, allocating a temporary session, supplying a single instance of data, and finalizing the session in one operation.

The parameter points to struct ncr_session_once_op_st, which includes arguments for one NCRIO_SESSION_INIT and one NCRIO_SES-
SION_FINAL operation. The ses member for the NCRIO_SES-
SION_FINAL sub-operation is ignored, the sub-operation automati- cally uses the temporary session.

NCRIO_MASTER_KEY_SET
Set the system-wide storage master key. Only a process with
EUID 0 and the CAP_SYS_ADMIN capability is allowed to perform this operation. Once a master key is set, it can be changed only by rebooting the system and setting a different key.

The parameter points to struct ncr_master_key_st, which speci- fies:

key Pointer to the key material in user space.

key_size
Size of the key material in bytes.

Currently only an AES key with size 16, 24, or 32 bytes is acceptable.

CONFIGURATION
The NCRIO_KEY_STORAGE_WRAP and NCRIO_KEY_STORAGE_UNWRAP ioctl()s work only after a storage master key is configured by the system administra- tor. See NCRIO_MASTER_KEY_SET above.

---
 Kbuild      |    2 
 cryptodev.h |   64 ++++++++++++
 ncr.h       |  312 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 378 insertions(+)
include/linux/Kbuild +2 -0
index 756f831..f35589a 100644
@@ -51,6 +51,7 @@ header-y += comstats.h
 header-y += const.h
 header-y += cgroupstats.h
 header-y += cramfs_fs.h
+header-y += cryptodev.h
 header-y += cycx_cfm.h
 header-y += dcbnl.h
 header-y += dlmconstants.h
@@ -116,6 +117,7 @@ header-y += mmtimer.h
 header-y += mqueue.h
 header-y += mtio.h
 header-y += ncp_no.h
+header-y += ncr.h
 header-y += neighbour.h
 header-y += net_dropmon.h
 header-y += net_tstamp.h
include/linux/cryptodev.h new file +64 -0

64 lines not on this page · raw message

include/linux/ncr.h new file +312 -0

312 lines not on this page · raw message

#2 lore raw

PATCH 3/4 Auditing infrastructure

In reply to Miloslav Trmač

This is used throughout the code. Included for completeness, undergoing separate review on linux-audit.

---
 include/linux/audit.h |   38 +++++++++++++
 kernel/auditfilter.c  |    2
 kernel/auditsc.c      |  136 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 176 insertions(+)
include/linux/audit.h +38 -0
index 3c7a358..8faa4e0 100644
@@ -122,6 +122,9 @@
 #define AUDIT_MAC_UNLBL_STCADD	1416	/* NetLabel: add a static label */
 #define AUDIT_MAC_UNLBL_STCDEL	1417	/* NetLabel: del a static label */
 
+#define AUDIT_CRYPTO_STORAGE_KEY    1600 /* Key storage key configured */
+#define AUDIT_CRYPTO_USERSPACE_OP   1601 /* User-space crypto operation */
+
 #define AUDIT_FIRST_KERN_ANOM_MSG   1700
 #define AUDIT_LAST_KERN_ANOM_MSG    1799
 #define AUDIT_ANOM_PROMISCUOUS      1700 /* Device changed promiscuous mode */
@@ -207,6 +210,7 @@
 #define AUDIT_OBJ_TYPE	21
 #define AUDIT_OBJ_LEV_LOW	22
 #define AUDIT_OBJ_LEV_HIGH	23
+#define AUDIT_CRYPTO_OP	24
 
 				/* These are ONLY useful when checking
 				 * at syscall exit time (AUDIT_AT_EXIT). */
@@ -314,6 +318,20 @@ enum {
 #define AUDIT_PERM_READ		4
 #define AUDIT_PERM_ATTR		8
 
+#define AUDIT_CRYPTO_OP_CONTEXT_NEW	1
+#define AUDIT_CRYPTO_OP_CONTEXT_DEL	2
+#define AUDIT_CRYPTO_OP_SESSION_INIT	3
+#define AUDIT_CRYPTO_OP_SESSION_OP	4
+#define AUDIT_CRYPTO_OP_SESSION_FINAL	5
+#define AUDIT_CRYPTO_OP_KEY_IMPORT	6
+#define AUDIT_CRYPTO_OP_KEY_EXPORT	7
+#define AUDIT_CRYPTO_OP_KEY_WRAP	8
+#define AUDIT_CRYPTO_OP_KEY_UNWRAP	9
+#define AUDIT_CRYPTO_OP_KEY_GEN		10
+#define AUDIT_CRYPTO_OP_KEY_DERIVE	11
+#define AUDIT_CRYPTO_OP_KEY_ZEROIZE	12
+#define AUDIT_CRYPTO_OP_KEY_GET_INFO	13
+
 struct audit_status {
 	__u32		mask;		/* Bit mask for valid entries */
 	__u32		enabled;	/* 1 = enabled, 0 = disabled */
@@ -479,6 +497,10 @@ extern int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
 				  const struct cred *new,
 				  const struct cred *old);
 extern void __audit_log_capset(pid_t pid, const struct cred *new, const struct cred *old);
+extern int __audit_log_crypto_op(int op, int context, int session,
+				 const char *operation, const char *algorithm,
+				 int key1, void *key1_id, size_t key1_id_size,
+				 int key2, void *key2_id, size_t key2_id_size);
 
 static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
 {
@@ -532,6 +554,21 @@ static inline void audit_log_capset(pid_t pid, const struct cred *new,
 		__audit_log_capset(pid, new, old);
 }
 
+static inline int audit_log_crypto_op(int op, int context, int session,
+				      const char *operation,
+				      const char *algorithm, int key1,
+				      void *key1_id, size_t key1_id_size,
+				      int key2, void *key2_id,
+				      size_t key2_id_size)
+{
+	if (unlikely(!audit_dummy_context()))
+		return __audit_log_crypto_op(op, context, session, operation,
+					     algorithm, key1, key1_id,
+					     key1_id_size, key2, key2_id,
+					     key2_id_size);
+	return 0;
+}
+
 extern int audit_n_rules;
 extern int audit_signals;
 #else
@@ -565,6 +602,7 @@ extern int audit_signals;
 #define audit_mq_getsetattr(d,s) ((void)0)
 #define audit_log_bprm_fcaps(b, ncr, ocr) ({ 0; })
 #define audit_log_capset(pid, ncr, ocr) ((void)0)
+#define audit_log_crypto_op(op, context, session, key1, key1_id, key1_id_size, key2, key2_id, key2_id_size) (0)
 #define audit_ptrace(t) ((void)0)
 #define audit_n_rules 0
 #define audit_signals 0
kernel/auditfilter.c +2 -0
index a706040..a25a587 100644
@@ -363,6 +363,7 @@ static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
 		case AUDIT_DEVMINOR:
 		case AUDIT_EXIT:
 		case AUDIT_SUCCESS:
+		case AUDIT_CRYPTO_OP:
 			/* bit ops are only useful on syscall args */
 			if (f->op == Audit_bitmask || f->op == Audit_bittest)
 				goto exit_free;
@@ -457,6 +458,7 @@ static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
 		case AUDIT_ARG1:
 		case AUDIT_ARG2:
 		case AUDIT_ARG3:
+		case AUDIT_CRYPTO_OP:
 			break;
 		case AUDIT_ARCH:
 			entry->rule.arch_f = f;
kernel/auditsc.c +136 -0

196 lines not on this page · raw message

#3 lore raw
In reply to Miloslav Trmač
Quoted 5 lines · Miloslav Trmač

Hello,
following is a patchset providing an user-space interface to the kernel crypto API. It is based on the older, BSD-compatible, implementation, but the user-space interface is different.

I only see patch 1/4 and 3/4. Where are 2/4 and 4/4? Neil

Quoted 1 line
#4 lore raw
Quoted 10 lines · ----- "Neil Horman
Quoted 2 lines · Miloslav Trmač

Hello,
following is a patchset providing an user-space interface to the

kernel crypto

Quoted 1 line

API. It is based on the older, BSD-compatible, implementation, but

the

Quoted 2 lines

user-space interface is different.

I only see patch 1/4 and 3/4. Where are 2/4 and 4/4?

Too large for the mailing list I'm afraid. I have uploaded them at http://people.redhat.com/mitr/cryptodev-ncr/ , you should also have received a personal copy yesterday.
Mirek

maintainer
#5 lore raw
In reply to Miloslav Trmač
Quoted 14 lines · Miloslav Trmač

Hello,
following is a patchset providing an user-space interface to the kernel crypto API. It is based on the older, BSD-compatible, implementation, but the user-space interface is different.

These are the major differences compared to the BSD-like interface:

* The API supports key storage and management inside the kernel. An application can thus ask the kernel to generate a key; the key is then referenced via an integer identifier, and the application can be prevented from accessing the raw key data. Such a key can, if so configured, still be wrapped for key transport to the recipient of the message, and unwrapped by the recipient.

Quoted 7 lines

The kernel key storage does not span system reboots, but applications can also wrap the keys for persistent storage, receiving an encrypted blob that does not reveal the raw key data, but can be later loaded back into the kernel.

* More algorithms and mechanisms are supported by the API, including public key algorithms (RSA/DSA encryption and signing, D-H key derivation, key wrapping).

Thanks for the patches.

Unfortunately it fails to satisfy the requirement of supporting all our existing kernel crypto interfaces, such as AEAD, as well as being flexible enough in adding new interfaces such as xor.

So we need to address these issues before this can be integrated into Linux.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#6 lore raw
Quoted 11 lines · ----- "Herbert Xu
Quoted 4 lines · Miloslav Trmač

Hello,
following is a patchset providing an user-space interface to the kernel crypto API. It is based on the older, BSD-compatible, implementation, but the user-space interface is different.

Thanks for the patches.

Unfortunately it fails to satisfy the requirement of supporting all our existing kernel crypto interfaces, such as AEAD, as well as being flexible enough in adding new interfaces such as xor.

Thanks for the review.

I think I can add AEAD (and compression/RNG, if requested) easily enough, I'll send an updated patch set.

(Talking specifically about xor, xor does not seem to be cryptographic operation that should be exposed as such to userspace - and AFAICS it is not integrated in the crypto API algorithm mechanism in the kernel either.)

Is the proposed interface acceptable in the general approach (enums for algorithms/operations, unions for parameters, session init/update/finalize)? With respect to flexibility, do you have specific suggestions or areas of concern?
Mirek

#7 lore raw
Quoted 13 lines · Miloslav Trmac
Quoted 11 lines · ----- "Herbert Xu
Quoted 4 lines · Miloslav Trmač

Hello,
following is a patchset providing an user-space interface to the kernel crypto API. It is based on the older, BSD-compatible, implementation, but the user-space interface is different.

Thanks for the patches.

Unfortunately it fails to satisfy the requirement of supporting all our existing kernel crypto interfaces, such as AEAD, as well as being flexible enough in adding new interfaces such as xor.

Quoted 7 lines

Thanks for the review.

I think I can add AEAD (and compression/RNG, if requested) easily enough, I'll send an updated patch set.

(Talking specifically about xor, xor does not seem to be cryptographic operation that should be exposed as such to userspace - and AFAICS it is not integrated in the crypto API algorithm mechanism in the kernel either.)

Is the proposed interface acceptable in the general approach (enums for algorithms/operations, unions for parameters, session init/update/finalize)? With respect to flexibility, do you have specific suggestions or areas of concern?

I know we spoke about this previously, but since you're asking publically, I'll make my argument here so that we can have the debate in public as well. I'm ok wih the general enumeration of operations in user space (I honestly can't see how you would do it otherwise). What worries me though is the use ioctl for these operations and the flexibility that it denies you in future updates.

Specifically, my concerns are twofold:

1) struct format. By passing down a structure as your doing through an ioctl call, theres no way to extend/modify that structure easily for future use. For instance the integration of aead might I think requires a blocking factor to be sepcified, and entry for which you have no available field in your crypt_ops structure. If you extend the structure in a later release of this code, you create a potential incompatibility with user space because you are no longer guaranteed that the size of the crypt_op structure is the same, and you need to be prepared for a range of sizes to get passed down, at which point it becomes difficult to differentiate between older code thats properly formatted, and newer code thats legitimately broken. You might could add a version field to mitigate that, but it gets ugly pretty quickly.

2) Use of pointers. Thats just a pain. You have the compat ioctl stuff in place to handle the 32/64 bit conversions, but it would be really nice if you could avoid having to maintain that extra code path.

Thats why I had suggested the use of a netlink protocol to manage this kind of interface. There are other issue to manage there, but they're really not that big a deal, comparatively speaking, and that interface allows for the easy specification of arbitrary length data in a fashion that doesn't cause user space breakage when additional algorithms/apis are added.

Thats just my opinion, I'm sure others have differing views. I'd be interested to hear what others think.

Regards
Neil

Quoted 1 line
#8 lore raw
Quoted 14 lines · Neil Horman

Specifically, my concerns are twofold:

1) struct format. By passing down a structure as your doing through an ioctl call, theres no way to extend/modify that structure easily for future use. For instance the integration of aead might I think requires a blocking factor to be sepcified, and entry for which you have no available field in your crypt_ops structure. If you extend the structure in a later release of this code, you create a potential incompatibility with user space because you are no longer guaranteed that the size of the crypt_op structure is the same, and you need to be prepared for a range of sizes to get passed down, at which point it becomes difficult to differentiate between older code thats properly formatted, and newer code thats legitimately broken. You might could add a version field to mitigate that, but it gets ugly pretty quickly.

Yeah, this does call out for versioning. But the ioctls are just for crypto parameter setup. Hopefully, that doesn't change too much since its just to select an algorithm, possibly ask for a key to be wrapped and loaded, or ask for a key to be created and exported. After setup, you just start using the device.

Quoted 5 lines

Thats why I had suggested the use of a netlink protocol to manage this kind of interface. There are other issue to manage there, but they're really not that big a deal, comparatively speaking, and that interface allows for the easy specification of arbitrary length data in a fashion that doesn't cause user space breakage when additional algorithms/apis are added.

The problem with the netlink approach is that auditing is not as good because netlink is an async protocol. The kernel can only use the credentials that ride in the skb with the command since there is no guarantee the process has not changed credentials by the time you get the packet. Adding more credentials to the netlink headers is also not good. As it is now, the auditing is synchronous with the syscall and we can get at more information and reliably create the audit records called out by the protection profiles or FIPS-140 level 2.

-Steve

#9 lore raw
Quoted 38 lines · Steve Grubb
Quoted 14 lines · Neil Horman

Specifically, my concerns are twofold:

1) struct format. By passing down a structure as your doing through an ioctl call, theres no way to extend/modify that structure easily for future use. For instance the integration of aead might I think requires a blocking factor to be sepcified, and entry for which you have no available field in your crypt_ops structure. If you extend the structure in a later release of this code, you create a potential incompatibility with user space because you are no longer guaranteed that the size of the crypt_op structure is the same, and you need to be prepared for a range of sizes to get passed down, at which point it becomes difficult to differentiate between older code thats properly formatted, and newer code thats legitimately broken. You might could add a version field to mitigate that, but it gets ugly pretty quickly.

Yeah, this does call out for versioning. But the ioctls are just for crypto parameter setup. Hopefully, that doesn't change too much since its just to select an algorithm, possibly ask for a key to be wrapped and loaded, or ask for a key to be created and exported. After setup, you just start using the device.

Quoted 5 lines

Thats why I had suggested the use of a netlink protocol to manage this kind of interface. There are other issue to manage there, but they're really not that big a deal, comparatively speaking, and that interface allows for the easy specification of arbitrary length data in a fashion that doesn't cause user space breakage when additional algorithms/apis are added.

The problem with the netlink approach is that auditing is not as good because netlink is an async protocol. The kernel can only use the credentials that ride in the skb with the command since there is no guarantee the process has not changed credentials by the time you get the packet. Adding more credentials to the netlink headers is also not good. As it is now, the auditing is synchronous with the syscall and we can get at more information and reliably create the audit records called out by the protection profiles or FIPS-140 level 2.

Quoted 1 line

-Steve

I think thats pretty easy to serialize though. All you need to do is enforce a rule in the kernel that dictates any creditial changes to a given context must be serialized behind all previously submitted crypto operations. It might make life a bit tougher on the audit code, but netlink contains pid/sequence data in all messages so that audit can correlate requests and responses easily.

Neil

#10 lore raw
Quoted 14 lines · ----- "Neil Horman
Quoted 6 lines · Steve Grubb
Quoted 5 lines

Thats why I had suggested the use of a netlink protocol to manage this kind of interface. There are other issue to manage there, but they're really not that big a deal, comparatively speaking, and that interface allows for the easy specification of arbitrary length data in a fashion that doesn't cause user space breakage when additional algorithms/apis are added.

Quoted 7 lines

The problem with the netlink approach is that auditing is not as good because netlink is an async protocol. The kernel can only use the credentials that ride in the skb with the command since there is no guarantee the process has not changed credentials by the time you get the packet. Adding more credentials to the netlink headers is also not good. As it is now, the auditing is synchronous with the syscall and we can get at more information and reliably create the audit records called out by the protection

Quoted 8 lines

profiles or

Quoted 3 lines

FIPS-140 level 2.

-Steve

I think thats pretty easy to serialize though. All you need to do is enforce a rule in the kernel that dictates any creditial changes to a given context must be serialized behind all previously submitted crypto operations.

That would be a bit unusual from the layering/semantics aspect - why should credential changes care about crypto operations when they don't care about any other operations? - and it would require pretty widespread changes throughout the kernel core.
Mirek

#11 lore raw
Quoted 19 lines · Miloslav Trmac
Quoted 17 lines · ----- "Neil Horman
Quoted 6 lines · Steve Grubb
Quoted 5 lines

Thats why I had suggested the use of a netlink protocol to manage this kind of interface. There are other issue to manage there, but they're really not that big a deal, comparatively speaking, and that interface allows for the easy specification of arbitrary length data in a fashion that doesn't cause user space breakage when additional algorithms/apis are added.

Quoted 7 lines

The problem with the netlink approach is that auditing is not as good because netlink is an async protocol. The kernel can only use the credentials that ride in the skb with the command since there is no guarantee the process has not changed credentials by the time you get the packet. Adding more credentials to the netlink headers is also not good. As it is now, the auditing is synchronous with the syscall and we can get at more information and reliably create the audit records called out by the protection

profiles or

Quoted 2 lines

FIPS-140 level 2.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

-Steve

I think thats pretty easy to serialize though. All you need to do is enforce a rule in the kernel that dictates any creditial changes to a given context must be serialized behind all previously submitted crypto operations.

That would be a bit unusual from the layering/semantics aspect - why should credential changes care about crypto operations when they don't care about any other operations? - and it would require pretty widespread changes throughout the kernel core.
Mirek

I'm sorry, I thought steve was referring to credentials in the sense of changing keys/etc while crypto operations were in flight. If you're referring to users credentials in terms of permissions to use a device or service, then I think its all moot anyway. As you say why should credential changes care about crypto ops when they don't care about other ops. If you have permissions to use a device/service, changing those permissions doesnt really change the fact that you're in the process of using that service. We could do some modicum of credential check when requests are submitted and deny service based on that check, but anything already submitted was done when you had permission to do so and should be allowed to complete.

Neil

#12 lore raw
In reply to Miloslav Trmač

Hi Miloslav,

first, thanks a lot for working on the userspace API, we have long missed this API and I've asked some times in the past about the status and proposals have been stalled some times, so it is really fun to see that something is happening!

We recognize that since Redhat is providing hardware for governments, this work will likely continue until the desired mechanism is available in a form that can be integrated and shipped, which gives great confidence that it is going to happen this time.

At ST-Ericsson we have considered to provide a user API to the kernel crypto facilities as well, we have some rough initial ideas on how to go about this. It has some similarities to what you/Redhat has presented, but we have some further goals:

1. We'd like the API have to be general enough to not change with new algorithms. Having to recompile user space programs becasuse of a minor change is messing things up for us. This may be solved by a functionality which presents the available crypto resources in the user API. (As opposed to using enums for the algorithms.)

c.f how the ALSA mixer presents a lot of things to userspace without using any enums at all in /dev/snd/controlC0 for card 0. For example in include/linux/soundcard.h you find the different control knobs enumerated with strings so as to avoid explicit enums.

We'd try to avoid as many enums as possible, and really only define the necessary information nodes so that userspace can look for strings like "aes-plain" instead, which is the same that dmcrypt is using, and there are already descriptions of available algorithms in /proc/crypto (from crypto/proc.c) using this format.

2. To avoid security hazards the API would benefit from being programmed with at least some secure programming concepts in mid. Input argument checking separate from algorithm separate from output argument checking, and erasing of information from stacks and buffers. More or less the advice you will find in places like:
http://www.dwheeler.com/secure-programs/

(Evidently we and others will help out reviewing and patching up proposed code in this aspect, also since you're working on government business I believe security audits will be mandatory?)

For internal keys, a function for compare of HMAC function results could improve security considerably.

3. A general interface for stream ciphers would be nice. The only differences are that they do not operate on blocks, but bits, and that they
always require
an IV. Arguably this can be added later if the aspect if just considered when devising the interface. The recent discussion in this thread
regarding netlink
points in a direction where streams are a natural part of the concept I believe.

There are some submission-related comments as well:

- The API description in the commit log of patch 1 should be a file in Documentation/crypto/usespace-api.txt or so instead, this is of general interest.

- The big patch 0002 to crypto/userspace is including the Makefile changes for patch 0004 making it non-bisectable. Also it is very large, is it possible to break it down a little? Many files are prefixed "ncr-*" and I don't see why - can this prefix simply be removed?

I hope the patch 0004 with software fallbacks is not strictly required by the userspace API so these two can be considered separately? Else can you describe how the dependecies go.. surely it must be possible to patch in the software fallbacks in libtom* separately?

- The big patch containing the entire libtomcrypt should be destined to drivers/staging or similar at this point (OK it is not a driver, should have been lib/staging if such a thing existed). The sheer size of it makes it very hard to review, and all attempts at breaking it in smaller pieces would be much appreciated. For example is it possible to have one patch per algorithm?

Also: isn't this creating a fork of the library? Not that it matters much as Linux is finding good use of it.

Since it is a fork, it should be adopted to the Linux source hiearchy, and since here every algorithm is directly in crypto/ please remove all the libtomcrypt subdir and put all directly into crypto/ for simplicity (subdirs below like hashes, headers etc are nice and should be preserved though).

libtomath seems to be duplicating a lot of in-kernel stuff already found inside the kernel, and needs to be merged with care. Arguably parts of this can be cleaned-up later but it'd be nice to make some initial attempts at unifying the math infrastructure.

Overall, thanks for working on this.

Yours,
Linus Walleij (et al)

#13 lore raw

2010/8/11 Linus Walleij <[email protected]>:

Hello,

Quoted 5 lines

Hi Miloslav,
  c.f how the ALSA mixer presents a lot of things to userspace without using any enums at all in /dev/snd/controlC0 for card 0. For example in include/linux/soundcard.h you find the different control knobs enumerated with strings so as to avoid explicit enums.

This is a double edged sword. Although it provides freedom at the userspace, it would also allow crypto algorithms that were not considered by the original design to be used with unpredictable results. Moreover typos are found at run-time rather than compile-time. For this and similar reasons interfaces of crypto libraries and PKCS #11 define explicitly the allowed algorithms. This design followed this principle.

Quoted 4 lines

2. To avoid security hazards the API would benefit from being programmed with at least some secure programming concepts in mid. Input argument checking separate from algorithm separate from output argument checking, and erasing of information from stacks and buffers. More or less

What do you consider as a threat here? Is it for the kernel returing unerased buffers and stack to userspace?

Quoted 8 lines

3. A general interface for stream ciphers would be nice. The only differences are that they do not operate on blocks, but bits, and that they always require
  an IV. Arguably this can be added later if the aspect if just considered when devising the interface. The recent discussion in this thread regarding netlink
  points in a direction where streams are a natural part of the concept I believe.

This interface works (or should) with any kind of cipher. It is designed to be wrappable to a pkcs #11 module, to be used easily as backend by existing crypto applications and libraries.

regards,
Nikos

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#15 lore raw

Re: [PATCH 2/4] /dev/crypto implementation

Review

Quoted 5 lines · Miloslav Trmač
(quoted diff) +4 -0
+static const struct algo_properties_st algo_properties[] = {
+	{ .algo = NCR_ALG_NULL, .kstr = "ecb(cipher_null)", 
+		.needs_iv = 0, .is_symmetric=1, .can_encrypt=1,
+		.key_type = NCR_KEY_TYPE_INVALID },

Hard-coding names like this is completely unacceptable. User-space should be able to specify the algorithm as a string.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt