X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fgc-gnulib.c;h=8aaa5c24853a2161a635b96fa7ffea82a59e4092;hb=86514ea5ebf71c35726c21ae077f2a3a10e196e2;hp=7f8d79066be567f28533ea64945be78fb6f8dc39;hpb=eee009073c7e98c56ad5d8b8f9a7e583e7460012;p=gnulib.git diff --git a/lib/gc-gnulib.c b/lib/gc-gnulib.c index 7f8d79066..8aaa5c248 100644 --- a/lib/gc-gnulib.c +++ b/lib/gc-gnulib.c @@ -1,5 +1,5 @@ /* gc-gnulib.c --- Common gnulib internal crypto interface functions - * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson + * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Simon Josefsson * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -73,15 +73,60 @@ #undef open #undef close +#ifdef GNULIB_GC_RANDOM +# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +# include +# include +HCRYPTPROV g_hProv = 0; +# ifndef PROV_INTEL_SEC +# define PROV_INTEL_SEC 22 +# endif +# ifndef CRYPT_VERIFY_CONTEXT +# define CRYPT_VERIFY_CONTEXT 0xF0000000 +# endif +# endif +#endif + Gc_rc gc_init (void) { +#ifdef GNULIB_GC_RANDOM +# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + if (g_hProv) + CryptReleaseContext (g_hProv, 0); + + /* There is no need to create a container for just random data, so + we can use CRYPT_VERIFY_CONTEXT (one call) see: + http://blogs.msdn.com/dangriff/archive/2003/11/19/51709.aspx */ + + /* We first try to use the Intel PIII RNG if drivers are present */ + if (!CryptAcquireContext (&g_hProv, NULL, NULL, + PROV_INTEL_SEC, CRYPT_VERIFY_CONTEXT)) + { + /* not a PIII or no drivers available, use default RSA CSP */ + if (!CryptAcquireContext (&g_hProv, NULL, NULL, + PROV_RSA_FULL, CRYPT_VERIFY_CONTEXT)) + return GC_RANDOM_ERROR; + } +# endif +#endif + return GC_OK; } void gc_done (void) { +#ifdef GNULIB_GC_RANDOM +# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + if (g_hProv) + { + CryptReleaseContext (g_hProv, 0); + g_hProv = 0; + } +# endif +#endif + return; } @@ -92,6 +137,11 @@ gc_done (void) static Gc_rc randomize (int level, char *data, size_t datalen) { +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + if (!g_hProv) + return GC_RANDOM_ERROR; + CryptGenRandom (g_hProv, (DWORD) datalen, data); +#else int fd; const char *device; size_t len = 0; @@ -140,6 +190,7 @@ randomize (int level, char *data, size_t datalen) rc = close (fd); if (rc < 0) return GC_RANDOM_ERROR; +#endif return GC_OK; } @@ -174,9 +225,11 @@ gc_set_allocators (gc_malloc_t func_malloc, { return; } + /* Ciphers. */ -typedef struct _gc_cipher_ctx { +typedef struct _gc_cipher_ctx +{ Gc_cipher alg; Gc_cipher_mode mode; #ifdef GNULIB_GC_ARCTWO @@ -319,7 +372,7 @@ gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key) char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1]; for (i = 0; i < keylen; i++) - sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF); + sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF); rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT, keylen * 8, keyMaterial); @@ -377,7 +430,7 @@ gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv) char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1]; for (i = 0; i < ivlen; i++) - sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF); + sprintf (&ivMaterial[2 * i], "%02x", iv[i] & 0xFF); rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC, ivMaterial); @@ -416,7 +469,7 @@ gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data) case GC_CBC: for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE, - data += ARCTWO_BLOCK_SIZE) + data += ARCTWO_BLOCK_SIZE) { size_t i; for (i = 0; i < ARCTWO_BLOCK_SIZE; i++) @@ -425,7 +478,7 @@ gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data) ARCTWO_BLOCK_SIZE); memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE); } - break; + break; default: return GC_INVALID_CIPHER; @@ -486,7 +539,7 @@ gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data) case GC_CBC: for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE, - data += ARCTWO_BLOCK_SIZE) + data += ARCTWO_BLOCK_SIZE) { char tmpIV[ARCTWO_BLOCK_SIZE]; size_t i; @@ -546,8 +599,7 @@ gc_cipher_close (gc_cipher_handle handle) { _gc_cipher_ctx *ctx = handle; - if (ctx) - free (ctx); + free (ctx); return GC_OK; } @@ -556,7 +608,8 @@ gc_cipher_close (gc_cipher_handle handle) #define MAX_DIGEST_SIZE 20 -typedef struct _gc_hash_ctx { +typedef struct _gc_hash_ctx +{ Gc_hash alg; Gc_hash_mode mode; char hash[MAX_DIGEST_SIZE]; @@ -581,6 +634,8 @@ gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle) Gc_rc rc = GC_OK; ctx = calloc (sizeof (*ctx), 1); + if (!ctx) + return GC_MALLOC_ERROR; ctx->alg = hash; ctx->mode = mode;