]> git.proxmox.com Git - mirror_kronosnet.git/commitdiff
[poc] add io-hashing PoC
authorFabio M. Di Nitto <fdinitto@redhat.com>
Sat, 12 Nov 2016 05:30:47 +0000 (06:30 +0100)
committerFabio M. Di Nitto <fdinitto@redhat.com>
Tue, 15 Nov 2016 04:31:51 +0000 (05:31 +0100)
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
configure.ac
poc-code/Makefile.am
poc-code/iov-hash/.gitignore [new file with mode: 0644]
poc-code/iov-hash/Makefile.am [new file with mode: 0644]
poc-code/iov-hash/main.c [new file with mode: 0644]

index 2211c45e4b0e2fa42cf4fd65caa091d11bf0de76..74bb7a5c1b841de31b1dac7dc006f14bd15ac54a 100644 (file)
@@ -377,6 +377,7 @@ AC_CONFIG_FILES([
                docs/Makefile
                poc-code/Makefile
                poc-code/iov-enc/Makefile
+               poc-code/iov-hash/Makefile
                poc-code/access-list/Makefile
                ])
 
index a82a78aab464e11cf5ae6d76d59016b32ca73e7e..8f6fda5c1abcb762da52614220223ed95195c511 100644 (file)
@@ -10,4 +10,4 @@ MAINTAINERCLEANFILES  = Makefile.in
 
 include $(top_srcdir)/build-aux/check.mk
 
-SUBDIRS                        = access-list iov-enc
+SUBDIRS                        = access-list iov-enc iov-hash
diff --git a/poc-code/iov-hash/.gitignore b/poc-code/iov-hash/.gitignore
new file mode 100644 (file)
index 0000000..1f05f55
--- /dev/null
@@ -0,0 +1 @@
+nss_hash
diff --git a/poc-code/iov-hash/Makefile.am b/poc-code/iov-hash/Makefile.am
new file mode 100644 (file)
index 0000000..b55c57a
--- /dev/null
@@ -0,0 +1,19 @@
+#
+# Copyright (C) 2016 Red Hat, Inc.  All rights reserved.
+#
+# Author: Fabio M. Di Nitto <fabbione@kronosnet.org>
+#
+# This software licensed under GPL-2.0+, LGPL-2.0+
+#
+
+MAINTAINERCLEANFILES   = Makefile.in
+
+include $(top_srcdir)/build-aux/check.mk
+
+noinst_PROGRAMS                = nss_hash
+
+nss_hash_SOURCES       = main.c
+
+nss_hash_CFLAGS                = $(nss_CFLAGS)
+
+nss_hash_LDFLAGS       = $(nss_LIBS)
diff --git a/poc-code/iov-hash/main.c b/poc-code/iov-hash/main.c
new file mode 100644 (file)
index 0000000..d2f5166
--- /dev/null
@@ -0,0 +1,171 @@
+/* Example code to illustrate DES enccryption/decryption using NSS.
+ * The example skips the details of obtaining the Key & IV to use, and
+ * just uses a hardcoded Key & IV.
+ * Note: IV is only needed if Cipher Blocking Chaining (CBC) mode of encryption
+ *       is used
+ *
+ * The recommended approach is to store and transport WRAPPED (encrypted)
+ * DES Keys (IVs can be in the clear). However, it is a common (and dangerous)
+ * practice to use raw DES Keys. This example shows the use of a RAW key.
+ */
+
+
+#include <nss.h>
+#include <pk11pub.h>
+#include <prerror.h>
+#include <blapit.h>
+
+/* example Key & IV */
+unsigned char gKey[] = {0xe8, 0xa7, 0x7c, 0xe2, 0x05, 0x63, 0x6a, 0x31};
+unsigned char gIV[] = {0xe4, 0xbb, 0x3b, 0xd3, 0xc3, 0x71, 0x2e, 0x58};
+
+int main(int argc, char **argv)
+{
+  CK_MECHANISM_TYPE  hashMech;
+  PK11SlotInfo*      slot = NULL;
+  PK11SymKey*        SymKey = NULL;
+  SECItem            SecParam;
+  PK11Context*       HashContext = NULL;
+  SECItem            keyItem;
+  SECStatus          rv, rv1, rv2;
+  unsigned char      buf1[1024], buf2[1024];
+  char              data[1024];
+  int                i;
+  unsigned int       tmp2_outlen;
+
+  /* Initialize NSS
+ *    * If your application code has already initialized NSS, you can skip it
+ *       * here.
+ *          * This code uses the simplest of the Init functions, which does not
+ *             * require a NSS database to exist
+ *                */
+  rv = NSS_NoDB_Init(".");
+  if (rv != SECSuccess)
+  {
+    fprintf(stderr, "NSS initialization failed (err %d)\n",
+            PR_GetError());
+    goto out;
+  }
+
+  /* choose mechanism: CKM_DES_CBC_PAD, CKM_DES3_ECB, CKM_DES3_CBC..... 
+ *    * Note that some mechanisms (*_PAD) imply the padding is handled for you
+ *       * by NSS. If you choose something else, then data padding is the
+ *          * application's responsibility
+ *             */
+  hashMech = CKM_SHA_1_HMAC;
+  slot = PK11_GetBestSlot(hashMech, NULL);
+  /* slot = PK11_GetInternalKeySlot(); is a simpler alternative but in
+ *    * theory, it *may not* return the optimal slot for the operation. For
+ *       * DES ops, Internal slot is typically the best slot
+ *          */
+  if (slot == NULL)
+  {
+    fprintf(stderr, "Unable to find security device (err %d)\n",
+            PR_GetError());
+    goto out;
+  }
+
+  /* NSS passes blobs around as SECItems. These contain a pointer to
+ *    * data and a length. Turn the raw key into a SECItem. */
+  keyItem.type = siBuffer;
+  keyItem.data = gKey;
+  keyItem.len = sizeof(gKey);
+
+  /* Turn the raw key into a key object. We use PK11_OriginUnwrap
+ *    * to indicate the key was unwrapped - which is what should be done
+ *       * normally anyway - using raw keys isn't a good idea */
+  SymKey = PK11_ImportSymKey(slot, hashMech, PK11_OriginUnwrap, CKA_SIGN,
+                             &keyItem, NULL);
+  if (SymKey == NULL)
+  {
+    fprintf(stderr, "Failure to import key into NSS (err %d)\n",
+            PR_GetError());
+    goto out;
+  }
+
+  SecParam.type = siBuffer;
+  SecParam.data = 0;
+  SecParam.len = 0;
+
+  /* sample data we'll hash */
+  strcpy(data, "Hash me!");
+  fprintf(stderr, "Clear Data: %s\n", data);
+
+  /* ========================= START SECTION ============================= */
+  /* If using the the same key and iv over and over, stuff before this     */
+  /* section and after this section needs to be done only ONCE             */
+
+  /* Create cipher context */
+  HashContext = PK11_CreateContextBySymKey(hashMech, CKA_SIGN,
+                                          SymKey, &SecParam);
+
+  if (!HashContext) {
+    fprintf(stderr, "no hash context today?\n");
+    goto out;
+  }
+
+  if (PK11_DigestBegin(HashContext) != SECSuccess) {
+    fprintf(stderr, "hash doesn't begin?\n");
+    goto out;
+  }
+
+  rv1 = PK11_DigestOp(HashContext, (unsigned char *)data, strlen(data)+1);
+
+  rv2 = PK11_DigestFinal(HashContext, buf2, &tmp2_outlen, SHA1_BLOCK_LENGTH);
+
+  PK11_DestroyContext(HashContext, PR_TRUE);
+  if (rv1 != SECSuccess || rv2 != SECSuccess)
+    goto out;
+
+  fprintf(stderr, "Hash Data: ");
+  for (i=0; i<tmp2_outlen; i++)
+    fprintf(stderr, "%02x ", buf2[i]);
+  fprintf(stderr, "\n");
+
+  /* =========================== END SECTION ============================= */
+
+  /* ========================= START SECTION ============================= */
+  /* If using the the same key and iv over and over, stuff before this     */
+  /* section and after this section needs to be done only ONCE             */
+
+  memset(buf1, 0, sizeof(buf1));
+  memset(buf2, 0, sizeof(buf2));
+
+  /* Create cipher context */
+  HashContext = PK11_CreateContextBySymKey(hashMech, CKA_SIGN,
+                                          SymKey, &SecParam);
+
+  if (!HashContext) {
+    fprintf(stderr, "no hash context today?\n");
+    goto out;
+  }
+
+  if (PK11_DigestBegin(HashContext) != SECSuccess) {
+    fprintf(stderr, "hash doesn't begin?\n");
+    goto out;
+  }
+
+  rv1 = PK11_DigestOp(HashContext, (unsigned char *)data, 5);
+  rv1 = PK11_DigestOp(HashContext, (unsigned char *)data+5, 4);
+
+  rv2 = PK11_DigestFinal(HashContext, buf2, &tmp2_outlen, SHA1_BLOCK_LENGTH);
+
+  PK11_DestroyContext(HashContext, PR_TRUE);
+  if (rv1 != SECSuccess || rv2 != SECSuccess)
+    goto out;
+
+  fprintf(stderr, "Hash Data: ");
+  for (i=0; i<tmp2_outlen; i++)
+    fprintf(stderr, "%02x ", buf2[i]);
+  fprintf(stderr, "\n");
+
+  /* =========================== END SECTION ============================= */
+
+out:
+  if (SymKey)
+    PK11_FreeSymKey(SymKey);
+
+ return 0;
+
+}