Skip to contentSkip to conversation

checksum: Add SHA384 and SHA512 hash & hmac types.

PATCH Applied Andrew Zaborowski 2 participants 12 messages Jul 24, 2015 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/17 checksum: Add SHA384 and SHA512 hash & hmac types.

---
 ell/checksum.c | 20 ++++++++++++++++++--
 ell/checksum.h |  2 ++
 2 files changed, 20 insertions(+), 2 deletions(-)
ell/checksum.c +18 -2
index 243470f..882f5f6 100644
@@ -73,7 +73,7 @@ struct sockaddr_alg {
  * Checksum handling
  */
 
-#define is_valid_type(type)  ((type) <= L_CHECKSUM_SHA256)
+#define is_valid_type(type)  ((type) <= L_CHECKSUM_SHA512)
 
 /**
  * l_checksum:
@@ -136,6 +136,12 @@ LIB_EXPORT struct l_checksum *l_checksum_new(enum l_checksum_type type)
 	case L_CHECKSUM_SHA256:
 		name = "sha256";
 		break;
+	case L_CHECKSUM_SHA384:
+		name = "sha384";
+		break;
+	case L_CHECKSUM_SHA512:
+		name = "sha512";
+		break;
 	}
 
 	fd = create_alg(name);
@@ -205,6 +211,12 @@ struct l_checksum *l_checksum_new_hmac(enum l_checksum_type type,
 	case L_CHECKSUM_SHA256:
 		name = "hmac(sha256)";
 		break;
+	case L_CHECKSUM_SHA384:
+		name = "hmac(sha384)";
+		break;
+	case L_CHECKSUM_SHA512:
+		name = "hmac(sha512)";
+		break;
 	}
 
 	fd = create_alg(name);
@@ -338,13 +350,17 @@ LIB_EXPORT char *l_checksum_get_string(struct l_checksum *checksum)
 		{ .name = "md5", .digest_len = 16 },
 		{ .name = "sha1", .digest_len = 20 },
 		{ .name = "sha256", .digest_len = 32 },
+		{ .name = "sha384", .digest_len = 48 },
+		{ .name = "sha512", .digest_len = 64 },
 		{ .name = "cmac(aes)", .digest_len = 16 },
 		{ .name = "hmac(md5)", .digest_len = 16 },
 		{ .name = "hmac(sha1)", .digest_len = 20 },
 		{ .name = "hmac(sha256)", .digest_len = 32 },
+		{ .name = "hmac(sha384)", .digest_len = 48 },
+		{ .name = "hmac(sha512)", .digest_len = 64 },
 		{ .name = NULL, .digest_len = 0 },
 	};
-	unsigned char digest[32];
+	unsigned char digest[64];
 	unsigned int i;
 
 	if (unlikely(!checksum))
ell/checksum.h +2 -0
index 8e163c4..c516580 100644
@@ -35,6 +35,8 @@ enum l_checksum_type {
 	L_CHECKSUM_MD5,
 	L_CHECKSUM_SHA1,
 	L_CHECKSUM_SHA256,
+	L_CHECKSUM_SHA384,
+	L_CHECKSUM_SHA512,
 };
 
 struct l_checksum *l_checksum_new(enum l_checksum_type type);
Signature · 1 line
2.1.4
#1 lore raw

PATCH 2/17 checksum: Add l_checksum_clone API.

In the kernel API we use and also in GLib's API the l_checksum_get_digest() equivalents can only be called once on each object meaning that in a situation when multiple digest values at different points of a running stream are needed the API becomes impractical. The new _clone() call returns a clone of the checksum's state at the current time, on which get_digest() can be called.

---
 ell/checksum.c | 24 ++++++++++++++++++++++++
 ell/checksum.h |  1 +
 2 files changed, 25 insertions(+)
ell/checksum.c +24 -0
index 882f5f6..7604ee0 100644
@@ -242,6 +242,30 @@ struct l_checksum *l_checksum_new_hmac(enum l_checksum_type type,
 }
 
 /**
+ * l_checksum_clone:
+ * @checksum: parent checksum object
+ *
+ * Creates a new checksum with an independent copy of parent @checksum's
+ * state.  l_checksum_get_digest can then be called on the parent or the
+ * clone without affecting the state of the other object.
+ **/
+LIB_EXPORT struct l_checksum *l_checksum_clone(struct l_checksum *checksum)
+{
+	struct l_checksum *clone;
+
+	clone = l_new(struct l_checksum, 1);
+	clone->sk = accept4(checksum->sk, NULL, 0, SOCK_CLOEXEC);
+
+	if (clone->sk < 0) {
+		l_free(clone);
+		return NULL;
+	}
+
+	strcpy(clone->alg_name, checksum->alg_name);
+	return clone;
+}
+
+/**
  * l_checksum_free:
  * @checksum: checksum object
  *
ell/checksum.h +1 -0
index c516580..34401a6 100644
@@ -43,6 +43,7 @@ struct l_checksum *l_checksum_new(enum l_checksum_type type);
 struct l_checksum *l_checksum_new_cmac_aes(const void *key, size_t key_len);
 struct l_checksum *l_checksum_new_hmac(enum l_checksum_type type,
 					const void *key, size_t key_len);
+struct l_checksum *l_checksum_clone(struct l_checksum *checksum);
 
 void l_checksum_free(struct l_checksum *checksum);
 
Signature · 1 line
2.1.4
#2 lore raw

Re: [PATCH 02/17] checksum: Add l_checksum_clone API.

Hi Andrew,

Quoted 11 lines · Andrew Zaborowski

In the kernel API we use and also in GLib's API the l_checksum_get_digest() equivalents can only be called once on each object meaning that in a situation when multiple digest values at different points of a running stream are needed the API becomes impractical. The new _clone() call returns a clone of the checksum's state at the current time, on which get_digest() can be called.

---
  ell/checksum.c | 24 ++++++++++++++++++++++++
  ell/checksum.h |  1 +
  2 files changed, 25 insertions(+)

<snip>

ell/checksum.c struct l_checksum *l_checksum_new_hmac(enum l_checksum_type type, · line 255

Quoted 4 lines
ell/checksum.c +4 -0
+LIB_EXPORT struct l_checksum *l_checksum_clone(struct l_checksum *checksum)
+{
+	struct l_checksum *clone;
+

Patch has been applied, but I also added a tiny sanity check here to make sure that checksum != NULL in a follow-on commit.

Regards,
-Denis

#3 lore raw

PATCH 3/17 cipher: Add 3DES EDE and AES CBC-mode cipher types.

In reply to Andrew Zaborowski

This again only adds the definitions needed to use the kernel implementations, no real code here.

---
 ell/cipher.c | 8 +++++++-
 ell/cipher.h | 2 ++
 2 files changed, 9 insertions(+), 1 deletion(-)
ell/cipher.c +7 -1
index 2c4fd90..b7577d5 100644
@@ -76,7 +76,7 @@ struct af_alg_iv {
 #define SOL_ALG 279
 #endif
 
-#define is_valid_type(type)  ((type) <= L_CIPHER_ARC4)
+#define is_valid_type(type)  ((type) <= L_CIPHER_DES3_EDE_CBC)
 
 struct l_cipher {
 	enum l_cipher_type type;
@@ -103,9 +103,15 @@ static int create_alg(enum l_cipher_type type,
 	case L_CIPHER_AES:
 		strcpy((char *) salg.salg_name, "ecb(aes)");
 		break;
+	case L_CIPHER_AES_CBC:
+		strcpy((char *) salg.salg_name, "cbc(aes)");
+		break;
 	case L_CIPHER_ARC4:
 		strcpy((char *) salg.salg_name, "ecb(arc4)");
 		break;
+	case L_CIPHER_DES3_EDE_CBC:
+		strcpy((char *) salg.salg_name, "cbc(des3_ede)");
+		break;
 	}
 
 	if (bind(sk, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
ell/cipher.h +2 -0
index efa1ad2..8e91884 100644
@@ -31,7 +31,9 @@ struct l_cipher;
 
 enum l_cipher_type {
 	L_CIPHER_AES = 0,
+	L_CIPHER_AES_CBC,
 	L_CIPHER_ARC4,
+	L_CIPHER_DES3_EDE_CBC,
 };
 
 struct l_cipher *l_cipher_new(enum l_cipher_type type,
Signature · 1 line
2.1.4
#4 lore raw

Re: [PATCH 03/17] cipher: Add 3DES EDE and AES CBC-mode cipher types.

Hi Andrew,

Quoted 7 lines · Andrew Zaborowski

This again only adds the definitions needed to use the kernel implementations, no real code here.

---
  ell/cipher.c | 8 +++++++-
  ell/cipher.h | 2 ++
  2 files changed, 9 insertions(+), 1 deletion(-)

Applied, thanks.

Regards,
-Denis

#5 lore raw

PATCH 4/17 cipher: Add l_cipher_set_iv.

In reply to Andrew Zaborowski

This allows setting the IV on block ciphers for either the encryption or decryption operations. It will affect the outcome of any later _encrypt or _decrypt calls.

---
 ell/cipher.c | 28 ++++++++++++++++++++++++++++
 ell/cipher.h |  3 +++
 2 files changed, 31 insertions(+)
ell/cipher.c +28 -0
index b7577d5..3619f2c 100644
@@ -228,3 +228,31 @@ LIB_EXPORT bool l_cipher_decrypt(struct l_cipher *cipher,
 
 	return operate_cipher(cipher->decrypt_sk, ALG_OP_DECRYPT, in, out, len);
 }
+
+LIB_EXPORT bool l_cipher_set_iv(struct l_cipher *cipher, const uint8_t *iv,
+					size_t iv_length, bool encrypt)
+{
+	char c_msg_buf[CMSG_SPACE(4 + iv_length)];
+	struct msghdr msg = {};
+	struct cmsghdr *c_msg;
+	uint32_t len = iv_length;
+
+	msg.msg_control = c_msg_buf;
+	msg.msg_controllen = sizeof(c_msg_buf);
+
+	c_msg = CMSG_FIRSTHDR(&msg);
+	c_msg->cmsg_level = SOL_ALG;
+	c_msg->cmsg_type = ALG_SET_IV;
+	c_msg->cmsg_len = CMSG_LEN(4 + iv_length);
+	memcpy(CMSG_DATA(c_msg) + 0, &len, 4);
+	memcpy(CMSG_DATA(c_msg) + 4, iv, iv_length);
+
+	msg.msg_iov = NULL;
+	msg.msg_iovlen = 0;
+
+	if (sendmsg(encrypt ? cipher->encrypt_sk : cipher->decrypt_sk,
+			&msg, 0) < 0)
+		return false;
+
+	return true;
+}
ell/cipher.h +3 -0
index 8e91884..74b96a3 100644
@@ -47,6 +47,9 @@ bool l_cipher_encrypt(struct l_cipher *cipher,
 bool l_cipher_decrypt(struct l_cipher *cipher,
 			const void *in, void *out, size_t len);
 
+bool l_cipher_set_iv(struct l_cipher *cipher, const uint8_t *iv,
+			size_t iv_length, bool encrypt);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.1.4
#6 lore raw

PATCH 5/17 Add ASN.1 BER / DER decoding utilities as asn1-private.h

In reply to Andrew Zaborowski

This is a minimal set of utilities to allow parsing digital certificate and RSA encryption related structures. They work on simple uint8_t arrays. The same operations could be implemented on top of a cleaner iterator-based API once that is added but since the required operations are so simple adding that API to ell didn't seem justified at this point.
One property of the structures used in X509 certificates is that they use many levels of nested SEQUENCE and SET substructures, hence the find_der_elem_by_path utility.

---
 Makefile.am        |   3 +-
 ell/asn1-private.h | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100644 ell/asn1-private.h
Makefile.am +2 -1
index 0861cec..fb2a35c 100644
@@ -75,7 +75,8 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/hwdb.c \
 			ell/cipher.c \
 			ell/random.c \
-			ell/uintset.c
+			ell/uintset.c \
+			ell/asn1-private.h
 
 ell_libell_la_LDFLAGS = -no-undefined \
 			-version-info $(ELL_CURRENT):$(ELL_REVISION):$(ELL_AGE)
ell/asn1-private.h new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.1.4
#7 lore raw

PATCH 6/17 Add RSA PKCS#1 1.5 assymetric key encryption operations.

In reply to Andrew Zaborowski

RSA support is required for different X509 digital certificate and TLS operations and is since recently present in the kernel. Being an asymetric key cipher the API is not the same as that used by the current symmetric ciphers in cipher.c based on the algif_skcipher driver. At this time there's no corresponding algif_akcipher for userspace to asymmetric key ciphers in Linux mainline or the crypto tree so this code works against a patch that applies on the mainline kernel 4a10a91756ef381bced7b88cfb9232f660b92d93. Another algif_akcipher implementation has been posted this week to the linux-kernel-cryptapi list but comments so far indicate that it will be a while before it is finalised so temporarily acceping this implementation against a patched kernel may be a better option than waiting for the mainline support.

There's another twist to RSA in that it is normally used with pre/post processing step before/after applying actual RSA operations to the data, called a padding scheme - usually one two standards. We implement the PKCS#1 v1.5 padding scheme. This may also end up being implemented in the kernel but this is unclear. I decided not to call the ell files ell/akcipher.{c,h} but rather ell/rsa.c and ell/rsa-private.h until that is clear because we can then decide the API based on that.

ell/rsa.c only implements the padding scheme, reusing ell/cipher.c for access to the kernel socket API.

---
 Makefile.am       |   4 +-
 ell/cipher.c      |   6 +-
 ell/cipher.h      |   1 +
 ell/rsa-private.h |  41 +++++++++
 ell/rsa.c         | 254 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 304 insertions(+), 2 deletions(-)
 create mode 100644 ell/rsa-private.h
 create mode 100644 ell/rsa.c
Makefile.am +3 -1
index fb2a35c..aab2f9e 100644
@@ -76,7 +76,9 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/cipher.c \
 			ell/random.c \
 			ell/uintset.c \
-			ell/asn1-private.h
+			ell/asn1-private.h \
+			ell/rsa-private.h \
+			ell/rsa.c
 
 ell_libell_la_LDFLAGS = -no-undefined \
 			-version-info $(ELL_CURRENT):$(ELL_REVISION):$(ELL_AGE)
ell/cipher.c +5 -1
index 3619f2c..b85ba6b 100644
@@ -76,7 +76,7 @@ struct af_alg_iv {
 #define SOL_ALG 279
 #endif
 
-#define is_valid_type(type)  ((type) <= L_CIPHER_DES3_EDE_CBC)
+#define is_valid_type(type)  ((type) <= L_CIPHER_RSA)
 
 struct l_cipher {
 	enum l_cipher_type type;
@@ -112,6 +112,10 @@ static int create_alg(enum l_cipher_type type,
 	case L_CIPHER_DES3_EDE_CBC:
 		strcpy((char *) salg.salg_name, "cbc(des3_ede)");
 		break;
+	case L_CIPHER_RSA:
+		strcpy((char *) salg.salg_name, "rsa");
+		strcpy((char *) salg.salg_type, "akcipher");
+		break;
 	}
 
 	if (bind(sk, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
ell/cipher.h +1 -0
index 74b96a3..a4db8c2 100644
@@ -34,6 +34,7 @@ enum l_cipher_type {
 	L_CIPHER_AES_CBC,
 	L_CIPHER_ARC4,
 	L_CIPHER_DES3_EDE_CBC,
+	L_CIPHER_RSA,
 };
 
 struct l_cipher *l_cipher_new(enum l_cipher_type type,
ell/rsa-private.h new file +41 -0

41 lines not on this page · raw message

ell/rsa.c new file +254 -0

254 lines not on this page · raw message

Signature · 1 line
2.1.4
#8 lore raw

PATCH 7/17 Add cert.{c,h} for basic X509 certificate operations.

In reply to Andrew Zaborowski

Loading, extracting specific information pieces (those used by TLS) and cryptographic verification functions are implemented. Only RSA-based PKCS#1 digital signatures are supported.

---
 Makefile.am        |   5 +-
 ell/cert-private.h |  25 +++
 ell/cert.c         | 618 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/cert.h         |  64 ++++++
 ell/ell.h          |   1 +
 5 files changed, 712 insertions(+), 1 deletion(-)
 create mode 100644 ell/cert-private.h
 create mode 100644 ell/cert.c
 create mode 100644 ell/cert.h
Makefile.am +4 -1
index aab2f9e..576641f 100644
@@ -36,7 +36,8 @@ pkginclude_HEADERS = ell/ell.h \
 			ell/hwdb.h \
 			ell/cipher.h \
 			ell/random.h \
-			ell/uintset.h
+			ell/uintset.h \
+			ell/cert.h
 
 lib_LTLIBRARIES = ell/libell.la
 
@@ -77,6 +78,8 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/random.c \
 			ell/uintset.c \
 			ell/asn1-private.h \
+			ell/cert-private.h \
+			ell/cert.c \
 			ell/rsa-private.h \
 			ell/rsa.c
 
ell/cert-private.h new file +25 -0
new file mode 100644
index 0000000..e0e65e4
@@ -0,0 +1,25 @@
+/*
+ *
+ *  Embedded Linux library
+ *
+ *  Copyright (C) 2011-2014  Intel Corporation. All rights reserved.
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+void pkcs1_write_digest_info(enum l_checksum_type type,
+				uint8_t *out, size_t *out_len,
+				const uint8_t *hash, size_t hash_len);
ell/cert.c new file +618 -0

618 lines not on this page · raw message

ell/cert.h new file +64 -0

64 lines not on this page · raw message

ell/ell.h +1 -0
index a5baf0d..2c88216 100644
@@ -38,6 +38,7 @@
 #include <ell/cipher.h>
 #include <ell/random.h>
 #include <ell/uintset.h>
+#include <ell/cert.h>
 
 #include <ell/netlink.h>
 #include <ell/genl.h>
Signature · 1 line
2.1.4
#9 lore raw

PATCH 8/17 cert: Add RSA private key utilities.

In reply to Andrew Zaborowski
---
 ell/cert.c | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/cert.h |  10 ++++
 2 files changed, 174 insertions(+)
ell/cert.c +164 -0
index ff9bf66..9c257de 100644
@@ -560,6 +560,170 @@ LIB_EXPORT enum l_cert_key_type l_cert_get_pubkey_type(struct l_cert *cert)
 	return pkcs1_encryption_oids[i].key_type;
 }
 
+static uint8_t *l_cert_decode_key(const uint8_t *buf_in, size_t size_in,
+					size_t *size_out,
+					const char *passphrase, bool *encrypted)
+{
+	uint8_t *bin;
+	size_t bin_size;
+
+	if (size_in > strlen(PEM_ENC_PRIVKEY_START) +
+			strlen(PEM_ENC_PRIVKEY_END) &&
+			!memcmp(buf_in, PEM_ENC_PRIVKEY_START,
+				strlen(PEM_ENC_PRIVKEY_START))) {
+		*encrypted = true;
+
+		if (!passphrase)
+			return NULL;
+	} else if (size_in > strlen(PEM_PRIVKEY_START) +
+			strlen(PEM_PRIVKEY_END) &&
+			!memcmp(buf_in, PEM_PRIVKEY_START,
+				strlen(PEM_PRIVKEY_START))) {
+		*encrypted = false;
+	} else if (size_in > strlen(PEM_RSA_PRIVKEY_START) +
+			strlen(PEM_RSA_PRIVKEY_END) &&
+			!memcmp(buf_in, PEM_RSA_PRIVKEY_START,
+				strlen(PEM_RSA_PRIVKEY_START))) {
+		*encrypted = false;
+	} else {
+		*encrypted = false;
+
+		return NULL;
+	}
+
+	/* Find the size of the decoded DER body */
+	bin_size = pem_decode(buf_in, size_in, NULL);
+	if (!bin_size)
+		return NULL;
+
+	bin = l_malloc(bin_size);
+	pem_decode(buf_in, size_in, bin);
+
+	if (!*encrypted) {
+		*size_out = bin_size;
+		return bin;
+	}
+
+	/*
+	 * If encrypted we need to parse the AlgorithmIdentifier and its
+	 * parameters, the key derivation function, its parameters, etc (TODO).
+	 */
+	l_cert_free_key(bin, bin_size);
+
+	return NULL;
+}
+
+LIB_EXPORT uint8_t *l_cert_load_key_file(const char *filename,
+						const char *passphrase,
+						size_t *len,
+						bool *encrypted)
+{
+	int fd;
+	struct stat st;
+	uint8_t *data;
+	uint8_t *key;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0)
+		return NULL;
+
+	if (fstat(fd, &st) < 0) {
+		close(fd);
+
+		return NULL;
+	}
+
+	data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+	if (data == MAP_FAILED) {
+		close(fd);
+
+		return NULL;
+	}
+
+	key = l_cert_decode_key(data, st.st_size, len, passphrase, encrypted);
+
+	munmap(data, st.st_size);
+	close(fd);
+
+	return key;
+}
+
+LIB_EXPORT void l_cert_free_key(uint8_t *key, size_t len)
+{
+	if (key) {
+		memset(key, 0, len);
+		l_free(key);
+	}
+}
+
+/*
+ * Extract a ASN1 RsaKey-formatted public+private key structure in the
+ * form used in the kernel.  It is simpler than the PKCS#1 form as it only
+ * contains the N, E and D integers and also correctly parses as a PKCS#1
+ * RSAPublicKey.
+ */
+LIB_EXPORT uint8_t *l_cert_extract_rsakey(uint8_t *pkcs1_key,
+						size_t pkcs1_key_len,
+						size_t *out_len)
+{
+	uint8_t *key, *ptr, *ver, *n, *e, *d;
+	uint8_t tag;
+	size_t ver_len, n_len, e_len, d_len;
+	int pos;
+
+	/* Unpack the outer SEQUENCE */
+	pkcs1_key = der_find_elem(pkcs1_key, pkcs1_key_len, 0, &tag,
+					&pkcs1_key_len);
+	if (!pkcs1_key || tag != ASN1_ID_SEQUENCE)
+		return NULL;
+
+	/* Check if the version element if present */
+	ver = der_find_elem(pkcs1_key, pkcs1_key_len, 0, &tag, &ver_len);
+	if (!ver || tag != ASN1_ID_INTEGER)
+		return NULL;
+
+	pos = (ver_len == 1 && ver[0] == 0x00) ? 1 : 0;
+
+	n = der_find_elem(pkcs1_key, pkcs1_key_len, pos + 0, &tag, &n_len);
+	if (!n || tag != ASN1_ID_INTEGER)
+		return NULL;
+
+	e = der_find_elem(pkcs1_key, pkcs1_key_len, pos + 1, &tag, &e_len);
+	if (!e || tag != ASN1_ID_INTEGER)
+		return NULL;
+
+	d = der_find_elem(pkcs1_key, pkcs1_key_len, pos + 2, &tag, &d_len);
+	if (!d || tag != ASN1_ID_INTEGER)
+		return NULL;
+
+	/* New SEQUENCE length including tags and lengths */
+	*out_len = 1 + (n_len >= 0x80 ? n_len >= 0x100 ? 3 : 2 : 1) + n_len +
+		1 + (e_len >= 0x80 ? e_len >= 0x100 ? 3 : 2 : 1) + e_len +
+		1 + (d_len >= 0x80 ? d_len >= 0x100 ? 3 : 2 : 1) + d_len;
+	ptr = key = l_malloc(*out_len);
+
+	*ptr++ = ASN1_ID_SEQUENCE;
+	write_asn1_definite_length(&ptr, *out_len);
+
+	*ptr++ = ASN1_ID_INTEGER;
+	write_asn1_definite_length(&ptr, n_len);
+	memcpy(ptr, n, n_len);
+	ptr += n_len;
+
+	*ptr++ = ASN1_ID_INTEGER;
+	write_asn1_definite_length(&ptr, e_len);
+	memcpy(ptr, e, e_len);
+	ptr += e_len;
+
+	*ptr++ = ASN1_ID_INTEGER;
+	write_asn1_definite_length(&ptr, d_len);
+	memcpy(ptr, d, d_len);
+	ptr += d_len;
+
+	*out_len = ptr - key;
+	return key;
+}
+
 static const uint8_t pkcs1_digest_info_md5_start[] = {
 	0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
 	0x02, 0x05, 0x05, 0x00, 0x04, 0x10,
ell/cert.h +10 -0
index bbea5eb..b4d8b57 100644
@@ -57,6 +57,16 @@ uint8_t *l_cert_find_pubkey(struct l_cert *cert, int *pubkey_len);
 
 enum l_cert_key_type l_cert_get_pubkey_type(struct l_cert *cert);
 
+/* Private keys */
+
+uint8_t *l_cert_load_key_file(const char *filename, const char *passphrase,
+				size_t *len, bool *encrypted);
+
+void l_cert_free_key(uint8_t *key, size_t len);
+
+uint8_t *l_cert_extract_rsakey(uint8_t *pkcs1_key, size_t pkcs1_key_len,
+				size_t *out_len);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.1.4
#10 lore raw

PATCH 9/17 unit: Add an RSA encryption/decryption test.

In reply to Andrew Zaborowski
---
 unit/test-cipher.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
unit/test-cipher.c +99 -0
index 2ee268c..044cb7d 100644
@@ -27,6 +27,7 @@
 #include <assert.h>
 
 #include <ell/ell.h>
+#include <ell/cert.h>
 
 #define FIXED_STR  "The quick brown fox jumps over the lazy dog. " \
 		   "Jackdaws love my big sphinx of quartz. "       \
@@ -122,6 +123,102 @@ static void test_arc4(const void *data)
 	l_cipher_free(cipher);
 }
 
+/*
+ * openssl genrsa 1024 | openssl rsa -outform DER | \
+ *	hexdump -v -e '"0x" 1/1 "%02x" ", "'
+ */
+static uint8_t rsa_priv_key[] = {
+	0x30, 0x82, 0x02, 0x5c, /* SEQUENCE */
+	0x02, 0x01, /* INTEGER - version */
+	0x00,
+	0x02, 0x81, 0x81, /* INTEGER - n */
+	0x00, 0xbe, 0x8c, 0xe7, 0x6c, 0x7d, 0x97, 0xbd, 0x63, 0xf6, 0x00, 0x65,
+	0x34, 0x23, 0xd9, 0xc6, 0xce, 0x6d, 0x1b, 0x30, 0x8f, 0xfa, 0x9b, 0x8e,
+	0xbe, 0x97, 0x01, 0x10, 0x5b, 0x49, 0x7c, 0xba, 0xd5, 0x47, 0x5e, 0xb0,
+	0x62, 0x24, 0xd9, 0xb0, 0xe2, 0x35, 0x5b, 0x05, 0xeb, 0x0a, 0x69, 0xc9,
+	0x5c, 0x02, 0x6d, 0xf2, 0x89, 0x6d, 0xab, 0xaa, 0x43, 0x53, 0x52, 0xaa,
+	0xed, 0xc9, 0xb6, 0x98, 0x17, 0x8a, 0x38, 0x9a, 0x80, 0x5b, 0xd1, 0x70,
+	0xc7, 0xc9, 0x16, 0x1f, 0x34, 0xb3, 0x27, 0x40, 0xa2, 0x39, 0xbe, 0xf8,
+	0x01, 0xfa, 0x9e, 0x04, 0x9f, 0x58, 0xc1, 0xa1, 0x19, 0x78, 0xcf, 0xfe,
+	0x9c, 0x76, 0x43, 0xba, 0x69, 0x05, 0x67, 0x9a, 0xfc, 0xc4, 0x56, 0xbb,
+	0xc1, 0x7f, 0xdb, 0xf0, 0xb9, 0x31, 0x2c, 0x4f, 0xc2, 0xd9, 0x42, 0xf6,
+	0x3d, 0x1b, 0x48, 0x31, 0x88, 0xc2, 0xb5, 0xb9, 0xa5,
+	0x02, 0x03, /* INTEGER - e */
+	0x01, 0x00, 0x01,
+	0x02, 0x81, 0x80, /* INTEGER - d */
+	0x0b, 0x34, 0xa2, 0x0f, 0x61, 0x3b, 0x61, 0x29, 0xd5, 0xb7, 0xa4, 0x3b,
+	0xf2, 0xb7, 0xc5, 0xd7, 0x31, 0xd7, 0x5d, 0x7d, 0xba, 0x11, 0x17, 0xcd,
+	0xe1, 0x77, 0x70, 0x8c, 0xcd, 0xbf, 0x86, 0x05, 0x30, 0xd3, 0x42, 0xb0,
+	0x22, 0xd6, 0xa2, 0x6e, 0x4b, 0x10, 0xf5, 0x42, 0x23, 0x34, 0xa1, 0x60,
+	0xc5, 0xcb, 0xcd, 0x6d, 0x83, 0x83, 0x8a, 0xd9, 0xb6, 0xb6, 0xaf, 0xd2,
+	0x98, 0x00, 0x22, 0xe5, 0x75, 0x67, 0xbe, 0x0b, 0x95, 0xe7, 0x76, 0xee,
+	0x22, 0x03, 0xdd, 0x49, 0x02, 0x86, 0xd7, 0x2b, 0x96, 0xee, 0xb1, 0x1e,
+	0x49, 0x02, 0xdf, 0xa9, 0x2e, 0x0f, 0x71, 0x33, 0xb5, 0xc6, 0x37, 0xb5,
+	0x6b, 0x58, 0xa1, 0xbb, 0x1c, 0xb9, 0xa2, 0x33, 0x70, 0x01, 0xca, 0xd4,
+	0xae, 0x96, 0xc3, 0x7d, 0x36, 0xc2, 0xe7, 0x50, 0x33, 0x17, 0xcc, 0xdc,
+	0x28, 0x7c, 0x7b, 0xdf, 0x25, 0x06, 0x57, 0xa9,
+	0x02, 0x41, /* INTEGER - p */
+	0x00, 0xf1, 0xec, 0x4f, 0xdf, 0xb7, 0x05, 0x2e, 0xdd, 0x1c, 0x85, 0x9d,
+	0x99, 0xb7, 0x60, 0x8c, 0x9b, 0x47, 0x1c, 0x04, 0x2f, 0x6f, 0x8a, 0xd5,
+	0x89, 0x1c, 0x2e, 0xe2, 0x0d, 0x68, 0x9c, 0xf6, 0xb0, 0x09, 0x32, 0x70,
+	0x06, 0x9f, 0x52, 0x7e, 0xc5, 0x46, 0x93, 0x60, 0x57, 0x77, 0xa8, 0xd3,
+	0x92, 0xbb, 0x24, 0x9c, 0x24, 0x35, 0x14, 0x99, 0x62, 0xe2, 0xce, 0xa2,
+	0x9b, 0x62, 0xd3, 0xe0, 0x0f,
+	0x02, 0x41, /* INTEGER - q */
+	0x00, 0xc9, 0xa3, 0x58, 0x17, 0x03, 0xfb, 0x19, 0x06, 0xa4, 0x75, 0xfb,
+	0xa2, 0xb3, 0x84, 0x2a, 0x7f, 0xf7, 0x66, 0x80, 0xb5, 0xd6, 0xda, 0xcf,
+	0xb2, 0x9e, 0x4f, 0x0a, 0xd3, 0xcd, 0xa5, 0x94, 0xc4, 0x80, 0x53, 0xc6,
+	0xbf, 0x7e, 0xe0, 0x2c, 0xc0, 0x71, 0xdb, 0x9c, 0xe0, 0x92, 0x66, 0x41,
+	0x6b, 0x64, 0x9e, 0x7a, 0xf3, 0xe7, 0x45, 0x15, 0x3f, 0xe4, 0xbc, 0xc1,
+	0x91, 0x40, 0x2a, 0x57, 0x0b,
+	0x02, 0x41, /* INTEGER - d mod (p - 1) */
+	0x00, 0x88, 0x9f, 0x4f, 0x00, 0x65, 0x68, 0x9c, 0xed, 0xac, 0x14, 0xdd,
+	0x4b, 0x19, 0x1f, 0x82, 0x68, 0x92, 0xc1, 0x04, 0xb0, 0x11, 0x4b, 0x13,
+	0x8a, 0xaa, 0x0a, 0xe4, 0x08, 0x74, 0x82, 0xe8, 0x61, 0xc3, 0xdf, 0xe3,
+	0x1a, 0x2a, 0x51, 0xb9, 0x5c, 0x09, 0x9e, 0x63, 0x33, 0x22, 0x55, 0x8a,
+	0x9e, 0x7b, 0xe7, 0x91, 0xf2, 0x74, 0xb3, 0x9c, 0x68, 0x16, 0xf4, 0x61,
+	0x2a, 0x65, 0xa6, 0x88, 0x0b,
+	0x02, 0x40, /* INTEGER - d mod (q - 1) */
+	0x56, 0x3c, 0xab, 0x07, 0x24, 0xe7, 0xb6, 0x5b, 0x55, 0xe9, 0x33, 0xd6,
+	0xf1, 0x09, 0xfc, 0x97, 0x40, 0x3b, 0x31, 0x9f, 0x13, 0xa5, 0xff, 0xa0,
+	0x77, 0xfe, 0x7c, 0x35, 0xfb, 0xc4, 0xee, 0x6c, 0x60, 0x29, 0xf4, 0x5d,
+	0xa0, 0x28, 0xc6, 0x5b, 0x04, 0x17, 0x15, 0xf0, 0x22, 0x0c, 0xe3, 0xbb,
+	0xc7, 0x8b, 0xd4, 0x30, 0x0e, 0x60, 0x48, 0x67, 0x4c, 0x2f, 0xc2, 0x65,
+	0x99, 0xd8, 0xc1, 0xe3,
+	0x02, 0x40, /* INTEGER - inv q mod p */
+	0x12, 0x89, 0x47, 0x76, 0xb6, 0xbb, 0x18, 0x64, 0xc3, 0x19, 0xe7, 0xa9,
+	0x93, 0xce, 0xec, 0xd6, 0x4b, 0xaa, 0xad, 0x46, 0x4f, 0x24, 0x0d, 0xf3,
+	0xbf, 0xc2, 0x7c, 0x95, 0x3e, 0xc9, 0x27, 0x8a, 0x4b, 0xbc, 0x9e, 0x56,
+	0x2a, 0x8c, 0x07, 0x5f, 0xb6, 0x07, 0x21, 0xb7, 0xc9, 0xae, 0x8c, 0x58,
+	0x04, 0x65, 0x02, 0xa2, 0x08, 0x03, 0x40, 0xf3, 0x71, 0x8c, 0x89, 0xf7,
+	0xaa, 0xb0, 0x6a, 0x75,
+};
+
+static void test_rsa(const void *data)
+{
+	struct l_cipher *cipher;
+	char buf[128];
+	uint8_t *short_key;
+	size_t short_key_len;
+
+	short_key = l_cert_extract_rsakey(rsa_priv_key, sizeof(rsa_priv_key),
+						&short_key_len);
+
+	cipher = l_cipher_new(L_CIPHER_RSA, short_key, short_key_len);
+	l_free(short_key);
+
+	assert(cipher);
+	assert(l_cipher_encrypt(cipher, FIXED_STR, buf, 128));
+
+	assert(memcmp(FIXED_STR, buf, 128));
+
+	assert(l_cipher_decrypt(cipher, buf, buf, 128));
+
+	assert(!memcmp(FIXED_STR, buf, 128));
+
+	l_cipher_free(cipher);
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -132,5 +229,7 @@ int main(int argc, char *argv[])
 
 	l_test_add("arc4", test_arc4, NULL);
 
+	l_test_add("rsa", test_rsa, NULL);
+
 	return l_test_run();
 }
Signature · 1 line
2.1.4
#11 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 5 lines · Andrew Zaborowski
---
  ell/checksum.c | 20 ++++++++++++++++++--
  ell/checksum.h |  2 ++
  2 files changed, 20 insertions(+), 2 deletions(-)

Patch has been applied, thanks.

Regards,
-Denis