X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fgc-gnulib.c;h=8aaa5c24853a2161a635b96fa7ffea82a59e4092;hb=fcfced6a46fa2b6c8dd701264d852d254f9fd042;hp=5ce31c15002ff95945ce0208a7b03932d5f62948;hpb=c9426d5ffc6b1ea62d5860a965ba1546ec910286;p=gnulib.git diff --git a/lib/gc-gnulib.c b/lib/gc-gnulib.c index 5ce31c150..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 @@ -68,15 +68,65 @@ # include "rijndael-api-fst.h" #endif +/* The results of open() in this file are not used with fchdir, + therefore save some unnecessary work in fchdir.c. */ +#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; } @@ -87,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; @@ -135,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; } @@ -169,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 @@ -182,7 +240,7 @@ typedef struct _gc_cipher_ctx { arcfour_context arcfourContext; #endif #ifdef GNULIB_GC_DES - des_ctx desContext; + gl_des_ctx desContext; #endif #ifdef GNULIB_GC_RIJNDAEL rijndaelKeyInstance aesEncKey; @@ -300,7 +358,7 @@ gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key) case GC_DES: if (keylen != 8) return GC_INVALID_CIPHER; - des_setkey (&ctx->desContext, key); + gl_des_setkey (&ctx->desContext, key); break; #endif @@ -314,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); @@ -372,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); @@ -411,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++) @@ -420,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; @@ -438,7 +496,7 @@ gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data) #ifdef GNULIB_GC_DES case GC_DES: for (; len >= 8; len -= 8, data += 8) - des_ecb_encrypt (&ctx->desContext, data, data); + gl_des_ecb_encrypt (&ctx->desContext, data, data); break; #endif @@ -481,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; @@ -510,7 +568,7 @@ gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data) #ifdef GNULIB_GC_DES case GC_DES: for (; len >= 8; len -= 8, data += 8) - des_ecb_decrypt (&ctx->desContext, data, data); + gl_des_ecb_decrypt (&ctx->desContext, data, data); break; #endif @@ -541,8 +599,7 @@ gc_cipher_close (gc_cipher_handle handle) { _gc_cipher_ctx *ctx = handle; - if (ctx) - free (ctx); + free (ctx); return GC_OK; } @@ -551,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]; @@ -576,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;