X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=bluesky%2Fcrypto.c;h=6981f45be9b0c8f197e5b1e03aa2132f530f7e56;hb=a5b5871865301712bc22ffc3d86a425dc0b45991;hp=defe1b334d12c9b8832c7248295fb70e0a4c7bc1;hpb=9047d936bd8bbf6acc6799e559c2766d4c487263;p=bluesky.git diff --git a/bluesky/crypto.c b/bluesky/crypto.c index defe1b3..6981f45 100644 --- a/bluesky/crypto.c +++ b/bluesky/crypto.c @@ -7,21 +7,19 @@ */ #include +#include #include #include #include #include #include -#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 * with an external cryptographic library. */ -#define CRYPTO_BLOCK_SIZE 16 /* 128-bit AES */ -#define CRYPTO_KEY_SIZE 16 - GCRY_THREAD_OPTION_PTHREAD_IMPL; void bluesky_crypt_init() @@ -31,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"); @@ -46,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, 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) {