-/*
- * Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
/** \file gcrypt.c Libgrcypt-based encryption/decryption routines. */
}
/*
- * This is called at the beginning of every program that uses libgcrypt. We
- * don't have to initialize any random seed here, but we must initialize the
- * gcrypt library. This task is performed by gcry_check_version() which can
- * also check that the gcrypt library version is at least the minimal required
- * version.
+ * This is called at the beginning of every program that uses libgcrypt. The
+ * call to gcry_check_version() initializes the gcrypt library and checks that
+ * we have at least the minimal required version.
*/
void init_random_seed_or_die(void)
{
const char *req_ver = "1.5.0";
+ int seed;
- if (gcry_check_version(req_ver))
- return;
- PARA_EMERG_LOG("fatal: need at least libgcrypt-%s, have: %s\n",
- req_ver, gcry_check_version(NULL));
- exit(EXIT_FAILURE);
+ if (!gcry_check_version(req_ver)) {
+ PARA_EMERG_LOG("fatal: need at least libgcrypt-%s, have: %s\n",
+ req_ver, gcry_check_version(NULL));
+ exit(EXIT_FAILURE);
+ }
+ get_random_bytes_or_die((unsigned char *)&seed, sizeof(seed));
+ srandom(seed);
}
/** S-expression for the public part of an RSA key. */
gcry_cipher_hd_t handle;
};
-struct stream_cipher *sc_new(const unsigned char *data, int len,
- bool use_aes)
+struct stream_cipher *sc_new(const unsigned char *data, int len)
{
gcry_error_t gret;
struct stream_cipher *sc = para_malloc(sizeof(*sc));
- if (use_aes) {
- assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
- gret = gcry_cipher_open(&sc->handle, GCRY_CIPHER_AES128,
- GCRY_CIPHER_MODE_CTR, 0);
- assert(gret == 0);
- gret = gcry_cipher_setkey(sc->handle, data,
- AES_CRT128_BLOCK_SIZE);
- assert(gret == 0);
- gret = gcry_cipher_setctr(sc->handle,
- data + AES_CRT128_BLOCK_SIZE, AES_CRT128_BLOCK_SIZE);
- assert(gret == 0);
- return sc;
- }
- gret = gcry_cipher_open(&sc->handle, GCRY_CIPHER_ARCFOUR,
- GCRY_CIPHER_MODE_STREAM, 0);
- if (gret) {
- PARA_ERROR_LOG("%s\n", gcrypt_strerror(gret));
- free(sc);
- return NULL;
- }
- gret = gcry_cipher_setkey(sc->handle, data, (size_t)len);
+ assert(len >= 2 * AES_CRT128_BLOCK_SIZE);
+ gret = gcry_cipher_open(&sc->handle, GCRY_CIPHER_AES128,
+ GCRY_CIPHER_MODE_CTR, 0);
+ assert(gret == 0);
+ gret = gcry_cipher_setkey(sc->handle, data,
+ AES_CRT128_BLOCK_SIZE);
+ assert(gret == 0);
+ gret = gcry_cipher_setctr(sc->handle,
+ data + AES_CRT128_BLOCK_SIZE, AES_CRT128_BLOCK_SIZE);
assert(gret == 0);
return sc;
}