summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiloslav Trmač <mitr@redhat.com>2010-11-02 19:58:05 +0100
committerMiloslav Trmač <mitr@redhat.com>2010-11-02 19:58:05 +0100
commitfd80d48ded8f550f9d2853721b5200bde0d951fa (patch)
tree4ee240597bf82fd4945a645756e4786b3c648bf6
parent843862b7ce325e70a0ff715006fb29fffd81e0a6 (diff)
downloadncrypto-fd80d48ded8f550f9d2853721b5200bde0d951fa.tar.gz
ncrypto-fd80d48ded8f550f9d2853721b5200bde0d951fa.tar.xz
ncrypto-fd80d48ded8f550f9d2853721b5200bde0d951fa.zip
Add symmetric key extraction support
Also allow marking keys as "sensitive" (= CKA_SENSITIVE = non-extractable), which is an API change.
-rw-r--r--Makefile.am6
-rw-r--r--include/ncrypto/ncrypto.h9
-rw-r--r--lib/internal.h2
-rw-r--r--lib/ncrypto_local.c41
-rw-r--r--tests/symm_ciphers.c11
-rw-r--r--tests/symm_keys.c135
-rw-r--r--tests/symm_signatures.c11
7 files changed, 201 insertions, 14 deletions
diff --git a/Makefile.am b/Makefile.am
index f1a5885..0e6c301 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -35,7 +35,8 @@ AM_CPPFLAGS = -I $(top_srcdir)/include $(GLIB_CFLAGS) $(NSS_CFLAGS) \
## Targets
lib_LTLIBRARIES = lib/libncrypto.la
pkginclude_HEADERS = include/ncrypto/ncrypto.h
-TESTS = tests/digests tests/rsa tests/symm_ciphers tests/symm_signatures
+TESTS = tests/digests tests/rsa tests/symm_ciphers tests/symm_keys \
+ tests/symm_signatures
## Rules
noinst_PROGRAMS = $(TESTS)
@@ -53,5 +54,8 @@ tests_rsa_LDFLAGS = -no-install
tests_symm_ciphers_LDADD = lib/libncrypto.la $(GLIB_LIBS)
tests_symm_ciphers_LDFLAGS = -no-install
+tests_symm_keys_LDADD = lib/libncrypto.la $(GLIB_LIBS)
+tests_symm_keys_LDFLAGS = -no-install
+
tests_symm_signatures_LDADD = lib/libncrypto.la $(GLIB_LIBS)
tests_symm_signatures_LDFLAGS = -no-install
diff --git a/include/ncrypto/ncrypto.h b/include/ncrypto/ncrypto.h
index 01964f9..76c695a 100644
--- a/include/ncrypto/ncrypto.h
+++ b/include/ncrypto/ncrypto.h
@@ -55,10 +55,15 @@ CK_RV ncr_get_random_bytes (void *dest, size_t size);
struct ncr_symm_key;
+/* "Sensitive" corresponds to CKA_SENSITIVE. */
CK_RV ncr_symm_key_create (struct ncr_symm_key **key, CK_KEY_TYPE type,
- const void *value, size_t value_size);
+ _Bool sensitive, const void *value,
+ size_t value_size);
CK_RV ncr_symm_key_generate (struct ncr_symm_key **key, CK_MECHANISM_TYPE mech,
- size_t value_size);
+ _Bool sensitive, size_t value_size);
+CK_RV ncr_symm_key_set_sensitive (struct ncr_symm_key *key);
+CK_RV ncr_symm_key_export (struct ncr_symm_key *key, void *dest,
+ size_t *dest_size_ptr);
CK_RV ncr_symm_key_destroy (struct ncr_symm_key *key);
/* Asymmetric keys */
diff --git a/lib/internal.h b/lib/internal.h
index 97fb77d..8b6d25b 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -29,12 +29,14 @@ Red Hat author: Miloslav Trmač <mitr@redhat.com> */
#include <config.h>
+#include <stdbool.h>
#include <stdint.h>
#include <ncrypto/ncrypto.h>
struct ncr_symm_key
{
CK_KEY_TYPE type;
+ bool sensitive;
size_t size;
uint8_t value[];
};
diff --git a/lib/ncrypto_local.c b/lib/ncrypto_local.c
index 5e0077d..1e88d2a 100644
--- a/lib/ncrypto_local.c
+++ b/lib/ncrypto_local.c
@@ -62,7 +62,7 @@ ncr_get_random_bytes (void *dest, size_t size)
CK_RV
ncr_symm_key_create (struct ncr_symm_key **key, CK_KEY_TYPE type,
- const void *value, size_t value_size)
+ _Bool sensitive, const void *value, size_t value_size)
{
struct ncr_symm_key *k;
@@ -76,6 +76,7 @@ ncr_symm_key_create (struct ncr_symm_key **key, CK_KEY_TYPE type,
return CKR_HOST_MEMORY;
k->type = type;
+ k->sensitive = sensitive;
k->size = value_size;
memcpy (k->value, value, value_size);
*key = k;
@@ -137,7 +138,7 @@ des3_fixup_key (uint8_t value[static 3 * DES_KEY_SIZE])
CK_RV
ncr_symm_key_generate (struct ncr_symm_key **key, CK_MECHANISM_TYPE mech,
- size_t value_size)
+ _Bool sensitive, size_t value_size)
{
struct ncr_symm_key *k;
CK_KEY_TYPE type;
@@ -174,6 +175,7 @@ ncr_symm_key_generate (struct ncr_symm_key **key, CK_MECHANISM_TYPE mech,
return CKR_HOST_MEMORY;
k->type = type;
+ k->sensitive = sensitive;
k->size = value_size;
regenerate:
res = ncr_get_random_bytes (k->value, value_size);
@@ -194,6 +196,41 @@ ncr_symm_key_generate (struct ncr_symm_key **key, CK_MECHANISM_TYPE mech,
}
CK_RV
+ncr_symm_key_set_sensitive (struct ncr_symm_key *key)
+{
+ g_return_val_if_fail (key != NULL, CKR_KEY_HANDLE_INVALID);
+ key->sensitive = true;
+ return CKR_OK;
+}
+
+CK_RV
+ncr_symm_key_export (struct ncr_symm_key *key, void *dest,
+ size_t *dest_size_ptr)
+{
+ g_return_val_if_fail (key != NULL, CKR_KEY_HANDLE_INVALID);
+ g_return_val_if_fail (dest_size_ptr != NULL, CKR_ARGUMENTS_BAD);
+
+ g_return_val_if_fail (!key->sensitive, CKR_ATTRIBUTE_SENSITIVE);
+
+ if (dest == NULL)
+ {
+ *dest_size_ptr = key->size;
+ return CKR_OK;
+ }
+ if (*dest_size_ptr < key->size)
+ {
+ *dest_size_ptr = key->size;
+ return CKR_BUFFER_TOO_SMALL;
+ }
+
+ g_return_val_if_fail (dest != NULL, CKR_ARGUMENTS_BAD);
+
+ memcpy (dest, key->value, key->size);
+ *dest_size_ptr = key->size;
+ return CKR_OK;
+}
+
+CK_RV
ncr_symm_key_destroy (struct ncr_symm_key *key)
{
g_return_val_if_fail (key != NULL, CKR_KEY_HANDLE_INVALID);
diff --git a/tests/symm_ciphers.c b/tests/symm_ciphers.c
index 1995eeb..0722a0e 100644
--- a/tests/symm_ciphers.c
+++ b/tests/symm_ciphers.c
@@ -26,6 +26,7 @@ POSSIBILITY OF SUCH DAMAGE.
Red Hat author: Miloslav Trmač <mitr@redhat.com> */
#include <assert.h>
+#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@@ -110,7 +111,7 @@ main (void)
res = ncr_symm_cipher_alloc (&sess, tvs[i].mech);
assert (res == CKR_OK);
- res = ncr_symm_key_create (&key, tvs[i].key_type, tvs[i].key,
+ res = ncr_symm_key_create (&key, tvs[i].key_type, true, tvs[i].key,
tvs[i].key_size);
assert (res == CKR_OK);
@@ -165,7 +166,7 @@ main (void)
res = ncr_symm_cipher_alloc (&sess, tvs[i].mech);
assert (res == CKR_OK);
- res = ncr_symm_key_create (&key, tvs[i].key_type, tvs[i].key,
+ res = ncr_symm_key_create (&key, tvs[i].key_type, true, tvs[i].key,
tvs[i].key_size);
assert (res == CKR_OK);
@@ -206,7 +207,8 @@ main (void)
res = ncr_symm_cipher_alloc (&sess, tvs[i].mech);
assert (res == CKR_OK);
- res = ncr_symm_key_generate (&key, tvs[i].key_gen_mech, tvs[i].key_size);
+ res = ncr_symm_key_generate (&key, tvs[i].key_gen_mech, true,
+ tvs[i].key_size);
assert (res == CKR_OK);
for (j = 0; j < 2; j++)
@@ -258,7 +260,8 @@ main (void)
res = ncr_symm_cipher_alloc (&sess, tvs[i].mech);
assert (res == CKR_OK);
- res = ncr_symm_key_generate (&key, tvs[i].key_gen_mech, tvs[i].key_size);
+ res = ncr_symm_key_generate (&key, tvs[i].key_gen_mech, true,
+ tvs[i].key_size);
assert (res == CKR_OK);
for (j = 0; j < 2; j++)
diff --git a/tests/symm_keys.c b/tests/symm_keys.c
new file mode 100644
index 0000000..aee5396
--- /dev/null
+++ b/tests/symm_keys.c
@@ -0,0 +1,135 @@
+/* ncr_symm_key_* tests.
+
+Copyright 2010 Red Hat, Inc.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+Red Hat author: Miloslav Trmač <mitr@redhat.com> */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <glib.h>
+#include <ncrypto/ncrypto.h>
+
+static void
+log_silent (const gchar *log_domain, GLogLevelFlags log_level,
+ const gchar *message, gpointer user_data)
+{
+ (void)log_domain;
+ (void)log_level;
+ (void)message;
+ (void)user_data;
+}
+
+static void
+check_set_sentitive_failure (struct ncr_symm_key *key)
+{
+ uint8_t dest[256];
+ size_t dest_size;
+ CK_RV res;
+
+ /* Extraction of a sensitive value is a programming error, so we complain to
+ stderr. Hide this in the test output. */
+
+ g_log_set_default_handler (log_silent, NULL);
+
+ dest_size = sizeof (dest);
+ res = ncr_symm_key_export (key, dest, &dest_size);
+ assert (res == CKR_ATTRIBUTE_SENSITIVE);
+
+ g_log_set_default_handler (g_log_default_handler, NULL);
+}
+
+int
+main (void)
+{
+ static const uint8_t input[32]
+ = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
+
+ uint8_t dest[256];
+ size_t dest_size;
+ struct ncr_symm_key *key;
+ CK_RV res;
+
+ res = ncr_symm_key_create (&key, CKK_AES, false, input, sizeof (input));
+ assert (res == CKR_OK);
+
+ dest_size = sizeof (dest);
+ res = ncr_symm_key_export (key, dest, &dest_size);
+ assert (res == CKR_OK);
+ assert (dest_size == sizeof (input));
+ assert (memcmp (dest, input, dest_size) == 0);
+
+ res = ncr_symm_key_set_sensitive (key);
+ assert (res == CKR_OK);
+
+ res = ncr_symm_key_set_sensitive (key);
+ assert (res == CKR_OK);
+
+ check_set_sentitive_failure (key);
+
+ res = ncr_symm_key_destroy (key);
+ assert (res == CKR_OK);
+
+
+ res = ncr_symm_key_create (&key, CKK_AES, true, input, sizeof (input));
+ assert (res == CKR_OK);
+
+ check_set_sentitive_failure (key);
+
+ res = ncr_symm_key_destroy (key);
+ assert (res == CKR_OK);
+
+
+ res = ncr_symm_key_generate (&key, CKM_AES_KEY_GEN, false, sizeof (input));
+ assert (res == CKR_OK);
+
+ dest_size = sizeof (dest);
+ res = ncr_symm_key_export (key, dest, &dest_size);
+ assert (res == CKR_OK);
+ assert (dest_size == sizeof (input));
+
+ res = ncr_symm_key_set_sensitive (key);
+ assert (res == CKR_OK);
+
+ res = ncr_symm_key_set_sensitive (key);
+ assert (res == CKR_OK);
+
+ check_set_sentitive_failure (key);
+
+ res = ncr_symm_key_destroy (key);
+ assert (res == CKR_OK);
+
+
+ res = ncr_symm_key_generate (&key, CKM_AES_KEY_GEN, true, sizeof (input));
+ assert (res == CKR_OK);
+
+ check_set_sentitive_failure (key);
+
+ res = ncr_symm_key_destroy (key);
+ assert (res == CKR_OK);
+ return EXIT_SUCCESS;
+}
diff --git a/tests/symm_signatures.c b/tests/symm_signatures.c
index 003d19d..d53eeef 100644
--- a/tests/symm_signatures.c
+++ b/tests/symm_signatures.c
@@ -26,6 +26,7 @@ POSSIBILITY OF SUCH DAMAGE.
Red Hat author: Miloslav Trmač <mitr@redhat.com> */
#include <assert.h>
+#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@@ -81,7 +82,7 @@ main (void)
res = ncr_symm_signature_alloc (&sess, tvs[i].mech);
assert (res == CKR_OK);
- res = ncr_symm_key_create (&key, CKK_GENERIC_SECRET, tvs[i].key,
+ res = ncr_symm_key_create (&key, CKK_GENERIC_SECRET, true, tvs[i].key,
tvs[i].key_size);
assert (res == CKR_OK);
@@ -132,7 +133,7 @@ main (void)
res = ncr_symm_signature_alloc (&sess, tvs[i].mech);
assert (res == CKR_OK);
- res = ncr_symm_key_create (&key, CKK_GENERIC_SECRET, tvs[i].key,
+ res = ncr_symm_key_create (&key, CKK_GENERIC_SECRET, true, tvs[i].key,
tvs[i].key_size);
assert (res == CKR_OK);
@@ -225,7 +226,7 @@ main (void)
res = ncr_symm_signature_alloc (&sess, tvs[i].mech);
assert (res == CKR_OK);
- res = ncr_symm_key_create (&key, CKK_GENERIC_SECRET, tvs[i].key,
+ res = ncr_symm_key_create (&key, CKK_GENERIC_SECRET, true, tvs[i].key,
tvs[i].key_size);
assert (res == CKR_OK);
@@ -269,7 +270,7 @@ main (void)
res = ncr_symm_signature_alloc (&sess, tvs[i].mech);
assert (res == CKR_OK);
- res = ncr_symm_key_generate (&key, CKM_GENERIC_SECRET_KEY_GEN,
+ res = ncr_symm_key_generate (&key, CKM_GENERIC_SECRET_KEY_GEN, true,
tvs[i].key_size);
assert (res == CKR_OK);
@@ -318,7 +319,7 @@ main (void)
res = ncr_symm_signature_alloc (&sess, tvs[i].mech);
assert (res == CKR_OK);
- res = ncr_symm_key_generate (&key, CKM_GENERIC_SECRET_KEY_GEN,
+ res = ncr_symm_key_generate (&key, CKM_GENERIC_SECRET_KEY_GEN, true,
tvs[i].key_size);
assert (res == CKR_OK);