Skip to contentSkip to conversation

Add dsa crypto ops

PATCH In review David Härdeman 4 participants 16 messages Jan 23, 2006 – Jan 26, 2006 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore
#1 lore raw

PATCH 2/4 Add dsa crypto ops

In reply to a message not in the archive

Adds dsa cryptographic operations. Since a dsa signature is always two 160-bit integer, I've modeled the dsa crypto as a hash algorithm.

--

crypto/Kconfig +7 -0
Index: vanilla-kernel/crypto/Kconfig
===================================================================
@@ -342,6 +342,13 @@
 	  Multiprecision maths library from GnuPG. Used for some
 	  crypto algorithms.
 
+config CRYPTO_DSA
+	tristate "Digital Signature Algorithm (EXPERIMENTAL)"
+	depends on CRYPTO && CRYPTO_MPILIB && EXPERIMENTAL
+	help
+	  Digital Signature Algorithm is used in a number of applications
+	  such as ssh and gpg.
+
 config CRYPTO_TEST
 	tristate "Testing module"
 	depends on CRYPTO
crypto/Makefile +1 -0
Index: vanilla-kernel/crypto/Makefile
===================================================================
@@ -30,6 +30,7 @@
 obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
 obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
 obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
+obj-$(CONFIG_CRYPTO_DSA) += dsa.o
 obj-$(CONFIG_CRYPTO_MPILIB) += mpi/
 
 obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o
crypto/dsa.c new file +230 -0

230 lines not on this page · raw message

include/linux/dsa.h new file +39 -0
Index: vanilla-kernel/include/linux/dsa.h
===================================================================
@@ -0,0 +1,39 @@
+/* dsa.h: digital signature architecture
+ *
+ * Copyright (C) 2005 David Härdeman ([email protected]).
+ * All Rights Reserved.
+ *
+ * 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.
+ */
+
+#ifndef _LINUX_DSA_H
+#define _LINUX_DSA_H
+#ifdef __KERNEL__
+
+#include <linux/mpi.h>
+
+#define DSA_DIGEST_SIZE         44
+#define DSA_HMAC_BLOCK_SIZE     64
+#define DSA_PART_P              0
+#define DSA_PART_Q              1
+#define DSA_PART_G              2
+#define DSA_PART_Y              3
+#define DSA_PART_X              4
+#define DSA_PARTS               5
+#define DSA_PUBLIC_PARTS        4
+
+struct key_payload_dsa {
+    MPI part[DSA_PARTS]; /* p,q,g,y,x */
+};
+
+#if 0
+#define dprintk(x...) printk(x)
+#else
+#define dprintk(x...) do { } while(0)
+#endif
+
+#endif
+#endif
#3 lore raw

PATCH 4/4 Add dsa key type

In reply to David Härdeman

Adds the dsa in-kernel key type.

--

security/Kconfig +8 -0
Index: vanilla-kernel/security/Kconfig
===================================================================
@@ -21,6 +21,14 @@
 
 	  If you are unsure as to whether this is required, answer N.
 
+config KEYS_DSA_KEYS
+	tristate "Support DSA keys (EXPERIMENTAL)"
+	depends on KEYS && EXPERIMENTAL
+	select CRYPTO_MPILIB
+	select CRYPTO_DSA
+	help
+	  This option provides support for retaining DSA keys in the kernel.
+
 config KEYS_DEBUG_PROC_KEYS
 	bool "Enable the /proc/keys file by which all keys may be viewed"
 	depends on KEYS
security/keys/Makefile +1 -0
Index: vanilla-kernel/security/keys/Makefile
===================================================================
@@ -13,4 +13,5 @@
 	user_defined.o
 
 obj-$(CONFIG_KEYS_COMPAT) += compat.o
+obj-$(CONFIG_KEYS_DSA_KEYS) += dsa_key.o
 obj-$(CONFIG_PROC_FS) += proc.o
security/keys/dsa_key.c new file +372 -0

372 lines not on this page · raw message

#2 lore raw

PATCH 3/4 Add encryption ops to the keyctl syscall

In reply to David Härdeman

Changes the keyctl syscall to accept six arguments (is it valid to do so?) and adds encryption as one of the supported ops for in-kernel keys.

--

include/linux/compat.h +2 -2
Index: vanilla-kernel/include/linux/compat.h
===================================================================
@@ -132,8 +132,8 @@
 long compat_sys_shmctl(int first, int second, void __user *uptr);
 long compat_sys_semtimedop(int semid, struct sembuf __user *tsems,
 		unsigned nsems, const struct compat_timespec __user *timeout);
-asmlinkage long compat_sys_keyctl(u32 option,
-			      u32 arg2, u32 arg3, u32 arg4, u32 arg5);
+asmlinkage long compat_sys_keyctl(u32 option, u32 arg2, u32 arg3, 
+                                  u32 arg4, u32 arg5, u32 arg6);
 
 asmlinkage ssize_t compat_sys_readv(unsigned long fd,
 		const struct compat_iovec __user *vec, unsigned long vlen);
include/linux/key.h +11 -0
Index: vanilla-kernel/include/linux/key.h
===================================================================
@@ -220,6 +220,17 @@
 	 */
 	long (*read)(const struct key *key, char __user *buffer, size_t buflen);
 
+	/* encrypt data using a key (optional)
+	 * - permission checks will be done by the caller
+	 * - the key's semaphore will be readlocked by the caller
+	 * - should return the amount of data that would be returned from the
+         *   encryption even if the buffer is NULL
+         * - expects the output buffer to be able to hold the result
+	 */
+	long (*encrypt)(const struct key *key,
+                        char __user *inbuffer, size_t inbuflen,
+                        char __user *outbuffer, size_t outbuflen);
+
 	/* handle request_key() for this type instead of invoking
 	 * /sbin/request-key (optional)
 	 * - key is the key to instantiate
include/linux/keyctl.h +1 -0
Index: vanilla-kernel/include/linux/keyctl.h
===================================================================
@@ -49,5 +49,6 @@
 #define KEYCTL_SET_REQKEY_KEYRING	14	/* set default request-key keyring */
 #define KEYCTL_SET_TIMEOUT		15	/* set key timeout */
 #define KEYCTL_ASSUME_AUTHORITY		16	/* assume request_key() authorisation */
+#define KEYCTL_ENCRYPT                  17      /* encrypt a chunk of data using key */
 
 #endif /*  _LINUX_KEYCTL_H */
include/linux/syscalls.h +3 -2
Index: vanilla-kernel/include/linux/syscalls.h
===================================================================
@@ -504,8 +504,9 @@
 				const char __user *_callout_info,
 				key_serial_t destringid);
 
-asmlinkage long sys_keyctl(int cmd, unsigned long arg2, unsigned long arg3,
-			   unsigned long arg4, unsigned long arg5);
+asmlinkage long sys_keyctl(int cmd, unsigned long arg2, 
+                           unsigned long arg3, unsigned long arg4, 
+                           unsigned long arg5, unsigned long arg6);
 
 asmlinkage long sys_ioprio_set(int which, int who, int ioprio);
 asmlinkage long sys_ioprio_get(int which, int who);
security/keys/compat.c +7 -2
Index: vanilla-kernel/security/keys/compat.c
===================================================================
@@ -23,8 +23,8 @@
  *   registers on taking a 32-bit syscall are zero
  * - if you can, you should call sys_keyctl directly
  */
-asmlinkage long compat_sys_keyctl(u32 option,
-				  u32 arg2, u32 arg3, u32 arg4, u32 arg5)
+asmlinkage long compat_sys_keyctl(u32 option, u32 arg2, u32 arg3, 
+				  u32 arg4, u32 arg5, u32 arg6)
 {
 	switch (option) {
 	case KEYCTL_GET_KEYRING_ID:
@@ -80,6 +80,11 @@
 	case KEYCTL_ASSUME_AUTHORITY:
 		return keyctl_assume_authority(arg2);
 
+	case KEYCTL_ENCRYPT:
+		return keyctl_encrypt_with_key(arg2,
+					       compat_ptr(arg3), arg4,
+					       compat_ptr(arg5), arg6);
+
 	default:
 		return -EOPNOTSUPP;
 	}
security/keys/keyctl.c +70 -2

86 lines not on this page · raw message

maintainer
#12 lore raw
In reply to David Härdeman

crypto/dsa.c line 122

Quoted 13 lines · David H?rdeman
crypto/dsa.c new file +12 -0
+static int dsa_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
+{
+       struct dsa_ctx *dctx = ctx;
+
+       if (keylen != sizeof(struct key_payload_dsa *)) {
+               printk("Invalid key size in dsa_setkey\n");
+               return -EINVAL;
+       }
+
+       dctx->key = (struct key_payload_dsa *)key;
+       return 0;
+}

This is bad. You're putting a pointer to an object with an unknown lifetime into the tfm.

Is there anything wrong with allocating the memory for it and storing the key in the tfm like everyone else?

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#7 lore raw

Re: [PATCH 03/04] Add encryption ops to the keyctl syscall

In reply to David Härdeman
Quoted 3 lines · On Mon, 23 Jan 2006 21:42:32 +0100 David Härdeman wrote:

Changes the keyctl syscall to accept six arguments (is it valid to do so?) and adds encryption as one of the supported ops for in-kernel keys.

Does this say anything about the amount of syscall testing that this code has had?

---
~Randy

#13 lore raw
In reply to Herbert Xu
Quoted 18 lines · Herbert Xu

crypto/dsa.c line 122

Quoted 13 lines · David H?rdeman
crypto/dsa.c new file +12 -0
+static int dsa_setkey(void *ctx, const u8 *key, unsigned int keylen, u32 *flags)
+{
+       struct dsa_ctx *dctx = ctx;
+
+       if (keylen != sizeof(struct key_payload_dsa *)) {
+               printk("Invalid key size in dsa_setkey\n");
+               return -EINVAL;
+       }
+
+       dctx->key = (struct key_payload_dsa *)key;
+       return 0;
+}

This is bad. You're putting a pointer to an object with an unknown lifetime into the tfm.

Quoted 2 lines

Is there anything wrong with allocating the memory for it and storing the key in the tfm like everyone else?

No, not at all, it's a mistake and I'll change it...

maintainer
#14 lore raw

Re: [PATCH 01/04] Add multi-precision-integer maths library

In reply to a message not in the archive
Quoted 3 lines · David Härdeman

Adds the multi-precision-integer maths library which was originally taken from GnuPG and ported to the kernel by David Howells in 2004
(http://people.redhat.com/~dhowells/modsign/modsign-269rc4mm1-2.diff.bz2)

You should update these files from a latest Fedora, Rawhide or RHEL kernel to pick up the bug fixes that have been made.

David

maintainer
#8 lore raw

Re: [PATCH 03/04] Add encryption ops to the keyctl syscall

In reply to David Härdeman
Quoted 2 lines · David Härdeman

Changes the keyctl syscall to accept six arguments (is it valid to do so?) and adds encryption as one of the supported ops for in-kernel keys.

I tried to avoid doing that for it required arch support as I recall, but I'm not sure which arch I was thinking of. It looks like it ought to be okay... it's no worse than mmap.

include/linux/key.h line 229

Quoted 4 lines
include/linux/key.h +4 -0
+	 * - should return the amount of data that would be returned from the
+         *   encryption even if the buffer is NULL
+         * - expects the output buffer to be able to hold the result
+	 */

Can you use TAB chars here please.

Quoted 2 lines

#define KEYCTL_ASSUME_AUTHORITY 16 /* assume request_key() authorisation */
+#define KEYCTL_ENCRYPT 17 /* encrypt a chunk of data using key */

And here.

security/keys/keyctl.c line 1092

Quoted 4 lines
security/keys/keyctl.c +4 -0
+	key = key_ref_to_ptr(key_ref);
+
+	/* see if we can read it directly */
+	ret = key_permission(key_ref, KEY_READ);

You don't actually need to calculate key until after you've done all those checks, so I'd move it further down the file. You can use the function to release key references in the error handling or have a separate error handling return path.

security/keys/keyctl.c line 1117

Quoted 3 lines
security/keys/keyctl.c +3 -0
+			down_read(&key->sem);
+			ret = key->type->encrypt(key, data, dlen, result, rlen);
+			up_read(&key->sem);

Do we really want to restrict the key type implementor to using the r/w semaphore. Would it be better to let the type decide whether it wants to use the semaphore or let it use RCU if it so desires?

David

maintainer
#4 lore raw

Re: [PATCH 04/04] Add dsa key type

In reply to David Härdeman
Quoted 1 line · David Härdeman

Adds the dsa in-kernel key type.

Please add a header file in include/keys/ to provide access to the key type definition and any special payload structures you use.

security/keys/dsa_key.c line 53

Quoted 2 lines
security/keys/dsa_key.c new file +2 -0
+static char *
+sign(const struct key_payload_dsa *skey, 

Please be consistent about sticking prefixes on the names of your functions. This ought to be called something like dsa_sign.

security/keys/dsa_key.c line 76

Quoted 1 line
security/keys/dsa_key.c new file +1 -0
+		printk("dsa: crypto_digest_setkey failed with error %i\n", i);

Should involve KERN_ERR or something like.

David

maintainer
#11 lore raw

Re: [PATCH 03/04] Add encryption ops to the keyctl syscall

In reply to David Härdeman
Quoted 1 line · David Härdeman

and adds encryption as one of the supported ops for in-kernel keys.

Do not forget to update Documentation/keys.txt.

David

#5 lore raw

Re: [PATCH 04/04] Add dsa key type

In reply to David Howells
Quoted 6 lines · David Howells
Quoted 1 line · David Härdeman

Adds the dsa in-kernel key type.

Please add a header file in include/keys/ to provide access to the key type definition and any special payload structures you use.

That was already added by one of the earlier patches as include/dsa.h since its shared by crypto/dsa.c and security/keys/dsa_key.c, do you prefer some other solution?

Quoted 5 lines

security/keys/dsa_key.c line 53

Quoted 2 lines
security/keys/dsa_key.c new file +2 -0
+static char *
+sign(const struct key_payload_dsa *skey, 

Please be consistent about sticking prefixes on the names of your functions. This ought to be called something like dsa_sign.

Ok

Quoted 3 lines

security/keys/dsa_key.c line 76

Quoted 1 line
security/keys/dsa_key.c new file +1 -0
+		printk("dsa: crypto_digest_setkey failed with error %i\n", i);

Should involve KERN_ERR or something like.

Ok

Regards,
David

#9 lore raw

Re: [PATCH 03/04] Add encryption ops to the keyctl syscall

In reply to David Howells
Quoted 9 lines · David Howells

security/keys/keyctl.c line 1092

Quoted 4 lines
security/keys/keyctl.c +4 -0
+	key = key_ref_to_ptr(key_ref);
+
+	/* see if we can read it directly */
+	ret = key_permission(key_ref, KEY_READ);

You don't actually need to calculate key until after you've done all those checks, so I'd move it further down the file. You can use the function to release key references in the error handling or have a separate error handling return path.

Do you mean that I should move the key_ref_to_ptr call to right after the can_read_key label? If that is the case, shouldn't the same thing be done for keyctl_read_key?

Quoted 7 lines

security/keys/keyctl.c line 1117

Quoted 3 lines
security/keys/keyctl.c +3 -0
+			down_read(&key->sem);
+			ret = key->type->encrypt(key, data, dlen, result, rlen);
+			up_read(&key->sem);

Do we really want to restrict the key type implementor to using the r/w semaphore. Would it be better to let the type decide whether it wants to use the semaphore or let it use RCU if it so desires?

Ok, I'll move the semaphore into the dsa key type instead and change the appropriate comments.

Re,
David

#15 lore raw

Re: [PATCH 01/04] Add multi-precision-integer maths library

In reply to David Howells
Quoted 8 lines · David Howells
Quoted 3 lines · David Härdeman

Adds the multi-precision-integer maths library which was originally taken from GnuPG and ported to the kernel by David Howells in 2004
(http://people.redhat.com/~dhowells/modsign/modsign-269rc4mm1-2.diff.bz2)

You should update these files from a latest Fedora, Rawhide or RHEL kernel to pick up the bug fixes that have been made.

Somewhat confusing...I downloaded kernel-2.6.15-1.1871_FC5.src.rpm and extracted linux-2.6-modsign-mpilib.patch from the srpm. After diffing the mpi dirs created by the previously mentioned patch and that from the Fedora kernel I get:

(david@austin:~/src/kernel/div)$ diff -Nurbw linux-2.6.9-rc4-mm1/crypto/mpi/ linux-902/crypto/mpi/
diff -Nurbw linux-2.6.9-rc4-mm1/crypto/mpi/mpi-div.c linux-902/crypto/mpi/mpi-div.c
--- linux-2.6.9-rc4-mm1/crypto/mpi/mpi-div.c    2006-01-24 22:56:09.000000000 +0100
+++ linux-902/crypto/mpi/mpi-div.c      2006-01-24 22:55:18.000000000 +0100
@@ -101,7 +101,7 @@
    MPI temp_divisor = NULL;
    if( quot == divisor || rem == divisor ) {
-       if (mpi_copy( &temp_divisor, divisor ) < 0);
+       if (mpi_copy( &temp_divisor, divisor ) < 0)
       return -ENOMEM;
       divisor = temp_divisor;
    }

Was that all the difference there was or am I missing something?

Re,
David

maintainer
#6 lore raw

Re: [PATCH 04/04] Add dsa key type

In reply to David Härdeman
Quoted 3 lines · David Härdeman

That was already added by one of the earlier patches as include/dsa.h since its shared by crypto/dsa.c and security/keys/dsa_key.c, do you prefer some other solution?

That'll do then.

David

maintainer
#10 lore raw

Re: [PATCH 03/04] Add encryption ops to the keyctl syscall

In reply to David Härdeman
Quoted 3 lines · David Härdeman

Do you mean that I should move the key_ref_to_ptr call to right after the can_read_key label? If that is the case, shouldn't the same thing be done for keyctl_read_key?

Quite possibly. I'll have a look at it when I get back from NZ. Actually, it's possible that the compiler shifts it down anyway since it has no side effects.

David

maintainer
#16 lore raw

Re: [PATCH 01/04] Add multi-precision-integer maths library

In reply to David Härdeman
Quoted 4 lines · David Härdeman

Somewhat confusing...I downloaded kernel-2.6.15-1.1871_FC5.src.rpm and extracted linux-2.6-modsign-mpilib.patch from the srpm. After diffing the mpi dirs created by the previously mentioned patch and that from the Fedora kernel I get:

That may be the only fix you need. Check the related patches also for overlap.

David