Rewrite NFS RPC dispatching.
[bluesky.git] / bluesky / crypto.c
index 8aeba3a..adf7d03 100644 (file)
@@ -7,13 +7,14 @@
  */
 
 #include <stdint.h>
+#include <assert.h>
 #include <errno.h>
 #include <pthread.h>
 #include <glib.h>
 #include <string.h>
 #include <gcrypt.h>
 
-#include "bluesky.h"
+#include "bluesky-private.h"
 
 /* Cryptographic operations.  The rest of the BlueSky code merely calls into
  * the functions in this file, so this is the only point where we interface
@@ -28,8 +29,6 @@ void bluesky_crypt_init()
     if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
         return;
 
-    g_print("libgcrypt not yet initialized, initializing...\n");
-
     if (!gcry_check_version(GCRYPT_VERSION))
         g_error("libgcrypt version mismatch\n");
 
@@ -43,6 +42,22 @@ void bluesky_crypt_random_bytes(guchar *buf, gint len)
     gcry_randomize(buf, len, GCRY_STRONG_RANDOM);
 }
 
+/* Hash a string down to an encryption key. */
+void bluesky_crypt_hash_key(const char *keystr, uint8_t *out)
+{
+    guint8 raw_csum[32];
+    gsize csum_len = sizeof(raw_csum);
+
+    assert(CRYPTO_KEY_SIZE == 16);
+
+    GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
+    g_checksum_update(csum, (const guchar *)keystr, strlen(keystr));
+    g_checksum_get_digest(csum, raw_csum, &csum_len);
+    g_checksum_free(csum);
+
+    memcpy(out, raw_csum, CRYPTO_KEY_SIZE);
+}
+
 /* Encrypt a data block. */
 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key)
 {