From: Simon Josefsson Date: Tue, 19 Aug 2008 15:58:08 +0000 (+0200) Subject: lib/gc-gnulib.c: Indentation cleanup. Add some comments regarding Windows crypto... X-Git-Tag: v0.1~7135^2~3 X-Git-Url: http://erislabs.net/gitweb/?a=commitdiff_plain;h=5840dbbbf33d47e03b35f68b25223b3f8472e443;p=gnulib.git lib/gc-gnulib.c: Indentation cleanup. Add some comments regarding Windows crypto stuff, from Mono. --- diff --git a/ChangeLog b/ChangeLog index 0b064bbb6..1fc6da287 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2008-08-19 Simon Josefsson + + * lib/gc-gnulib.c: Indentation cleanup. Add some comments + regarding Windows crypto stuff, from Mono. + 2008-08-19 Adam Strzelecki (tiny change) * lib/gc-gnulib.c: Use CRYPT_VERIFY_CONTEXT. Try to use Intel CSP diff --git a/lib/gc-gnulib.c b/lib/gc-gnulib.c index 98214ed15..8aaa5c248 100644 --- a/lib/gc-gnulib.c +++ b/lib/gc-gnulib.c @@ -92,11 +92,22 @@ gc_init (void) { #ifdef GNULIB_GC_RANDOM # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ - if(g_hProv) - CryptReleaseContext(g_hProv, 0); - if(!CryptAcquireContext(&g_hProv, NULL, NULL, PROV_INTEL_SEC, CRYPT_VERIFY_CONTEXT)) - if(!CryptAcquireContext(&g_hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFY_CONTEXT)) - return GC_RANDOM_ERROR; + 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 @@ -108,9 +119,9 @@ gc_done (void) { #ifdef GNULIB_GC_RANDOM # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ - if(g_hProv) + if (g_hProv) { - CryptReleaseContext(g_hProv, 0); + CryptReleaseContext (g_hProv, 0); g_hProv = 0; } # endif @@ -127,9 +138,9 @@ static Gc_rc randomize (int level, char *data, size_t datalen) { #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ - if(!g_hProv) + if (!g_hProv) return GC_RANDOM_ERROR; - CryptGenRandom(g_hProv, (DWORD)datalen, data); + CryptGenRandom (g_hProv, (DWORD) datalen, data); #else int fd; const char *device; @@ -214,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 @@ -359,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); @@ -417,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); @@ -456,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++) @@ -465,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; @@ -526,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; @@ -595,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];