mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Marc Kleine-Budde <mkl@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH v3 7/9] crypto: add simple keystore
Date: Sun, 25 Oct 2015 22:03:34 +0100	[thread overview]
Message-ID: <1445807016-6637-8-git-send-email-mkl@pengutronix.de> (raw)
In-Reply-To: <1445807016-6637-1-git-send-email-mkl@pengutronix.de>

This patch adds a simple keystore to barebox. The keystore implements a simple
key-value store to hold arbitrary values.

Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 crypto/Kconfig            |  6 ++++
 crypto/Makefile           |  1 +
 crypto/keystore.c         | 80 +++++++++++++++++++++++++++++++++++++++++++++++
 include/crypto/keystore.h | 26 +++++++++++++++
 4 files changed, 113 insertions(+)
 create mode 100644 crypto/keystore.c
 create mode 100644 include/crypto/keystore.h

diff --git a/crypto/Kconfig b/crypto/Kconfig
index 27047889a658..41145a312d8e 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -87,4 +87,10 @@ config CRYPTO_PBKDF2
 	select DIGEST_SHA1_GENERIC
 	bool
 
+config CRYPTO_KEYSTORE
+	bool "Keystore"
+	help
+	  This is a simple keystore, which can be used to pass keys
+	  between several components via simple interface.
+
 endmenu
diff --git a/crypto/Makefile b/crypto/Makefile
index f39de718e5e7..c6d17787ca4e 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -11,3 +11,4 @@ obj-$(CONFIG_DIGEST_SHA384_GENERIC)	+= sha4.o
 obj-$(CONFIG_DIGEST_SHA512_GENERIC)	+= sha4.o
 
 obj-$(CONFIG_CRYPTO_PBKDF2)	+= pbkdf2.o
+obj-$(CONFIG_CRYPTO_KEYSTORE)	+= keystore.o
diff --git a/crypto/keystore.c b/crypto/keystore.c
new file mode 100644
index 000000000000..90b470fe6717
--- /dev/null
+++ b/crypto/keystore.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2015 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License version 2 as published by the
+ * Free Software Foundation.
+ */
+
+#include <common.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+
+static LIST_HEAD(keystore_list);
+
+#define for_each_key(key) list_for_each_entry(key, &keystore_list, list)
+
+struct keystore_key {
+	struct list_head list;
+	const char *name;
+	const u8 *secret;
+	int secret_len;
+};
+
+static int keystore_compare(struct list_head *a, struct list_head *b)
+{
+	const char *na = list_entry(a, struct keystore_key, list)->name;
+	const char *nb = list_entry(b, struct keystore_key, list)->name;
+
+	return strcmp(na, nb);
+}
+
+/**
+ * @param[in] name Name of the secret to get
+ * @param[out] secret Double pointer to memory representing the secret, do _not_ free() after use
+ * @param[out] secret_len Pointer to length of the secret
+ */
+int keystore_get_secret(const char *name, const u8 **secret, int *secret_len)
+{
+	struct keystore_key *key;
+
+	for_each_key(key) {
+		if (!strcmp(name, key->name)) {
+			if (!secret || !secret_len)
+				return 0;
+
+			*secret = key->secret;
+			*secret_len = key->secret_len;
+
+			return 0;
+		}
+	}
+
+	return -ENOENT;
+}
+
+/**
+ * @param[in] name Name of the secret to set
+ * @param[in] secret Pointer to memory holding the secret
+ * @param[in] secret_len Length of the secret
+ */
+int keystore_set_secret(const char *name, const u8 *secret, int secret_len)
+{
+	struct keystore_key *key;
+	int ret;
+
+	/* check if key is already in store */
+	ret = keystore_get_secret(name, NULL, NULL);
+	if (!ret)
+		return -EBUSY;
+
+	key = xzalloc(sizeof(*key));
+	INIT_LIST_HEAD(&key->list);
+	key->name = xstrdup(name);
+	key->secret = xmemdup(secret, secret_len);
+	key->secret_len = secret_len;
+
+	list_add_sort(&key->list, &keystore_list, keystore_compare);
+
+	return 0;
+}
diff --git a/include/crypto/keystore.h b/include/crypto/keystore.h
new file mode 100644
index 000000000000..29915854b851
--- /dev/null
+++ b/include/crypto/keystore.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2015 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License version 2 as published by the
+ * Free Software Foundation.
+ */
+
+#ifndef __CRYPTO_KEYSTORE_H
+#define __CRYPTO_KEYSTORE_H
+
+#ifdef CONFIG_CRYPTO_KEYSTORE
+int keystore_get_secret(const char *name, const u8 **secret, int *secret_len);
+int keystore_set_secret(const char *name, const u8 *secret, int secret_len);
+#else
+static inline int keystore_get_secret(const char *name, const u8 **secret, int *secret_len)
+{
+	return -EINVAL;
+}
+static inline int keystore_set_secret(const char *name, const u8 *secret, int secret_len)
+{
+	return 0;
+}
+#endif
+
+#endif
-- 
2.6.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2015-10-25 21:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-25 21:03 [PATCH v3 0/8] state framework enhancements Marc Kleine-Budde
2015-10-25 21:03 ` [PATCH v3 1/9] of_path: of_find_path() factor out device detection logic into separate function Marc Kleine-Budde
2015-10-25 21:03 ` [PATCH v3 2/9] of_path: add of_find_path_by_node() Marc Kleine-Budde
2015-10-28  6:20   ` Sascha Hauer
2015-10-28  8:26     ` Marc Kleine-Budde
2015-10-25 21:03 ` [PATCH v3 3/9] state: make use of of_find_path_by_node() and add return -EPROBE_DEFER if device is not available Marc Kleine-Budde
2015-10-25 21:03 ` [PATCH v3 4/9] state: use name of device node as name if alias " Marc Kleine-Budde
2015-10-25 21:03 ` [PATCH v3 5/9] state: disable load command Marc Kleine-Budde
2015-10-25 21:03 ` [PATCH v3 6/9] crypto: Kconfig: add submenu for crypto related config options Marc Kleine-Budde
2015-10-25 21:03 ` Marc Kleine-Budde [this message]
2015-10-25 21:03 ` [PATCH v3 8/9] state: prepare raw backend for hmac support Marc Kleine-Budde
2015-10-25 21:03 ` [PATCH v3 9/9] state: backend_raw: add hamc support Marc Kleine-Budde
2015-10-28  6:24   ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1445807016-6637-8-git-send-email-mkl@pengutronix.de \
    --to=mkl@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox