WinCE patches
[openssl.git] / crypto / rand / rand_win.c
index 242febe..37f172f 100644 (file)
  * http://developer.intel.com/design/security/rng/redist_license.htm
  */
 #define PROV_INTEL_SEC 22
-#define INTEL_DEF_PROV "Intel Hardware Cryptographic Service Provider"
+#define INTEL_DEF_PROV TEXT("Intel Hardware Cryptographic Service Provider")
 
 static void readtimer(void);
 static void readscreen(void);
@@ -170,7 +170,9 @@ typedef BOOL (WINAPI *THREAD32)(HANDLE, LPTHREADENTRY32);
 typedef BOOL (WINAPI *MODULE32)(HANDLE, LPMODULEENTRY32);
 
 #include <lmcons.h>
+#ifndef OPENSSL_SYS_WINCE
 #include <lmstats.h>
+#endif
 #if 1 /* The NET API is Unicode only.  It requires the use of the UNICODE
        * macro.  When UNICODE is defined LPTSTR becomes LPWSTR.  LMSTR was
        * was added to the Platform SDK to allow the NET API to be used in
@@ -209,20 +211,32 @@ int RAND_poll(void)
         osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ;
         GetVersionEx( &osverinfo ) ;
 
+#ifdef OPENSSL_SYS_WINCE
+       /* poll the CryptoAPI PRNG */
+       /* The CryptoAPI returns sizeof(buf) bytes of randomness */
+       if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
+               {
+               if (CryptGenRandom(hProvider, sizeof(buf), buf))
+                       RAND_add(buf, sizeof(buf), sizeof(buf));
+               CryptReleaseContext(hProvider, 0); 
+               }
+#endif
+
        /* load functions dynamically - not available on all systems */
-       advapi = LoadLibrary("ADVAPI32.DLL");
-       kernel = LoadLibrary("KERNEL32.DLL");
-       user = LoadLibrary("USER32.DLL");
-       netapi = LoadLibrary("NETAPI32.DLL");
+       advapi = LoadLibrary(TEXT("ADVAPI32.DLL"));
+       kernel = LoadLibrary(TEXT("KERNEL32.DLL"));
+       user = LoadLibrary(TEXT("USER32.DLL"));
+       netapi = LoadLibrary(TEXT("NETAPI32.DLL"));
 
+#ifndef OPENSSL_SYS_WINCE
 #if 1 /* There was previously a problem with NETSTATGET.  Currently, this
        * section is still experimental, but if all goes well, this conditional
        * will be removed
        */
        if (netapi)
                {
-               netstatget = (NETSTATGET) GetProcAddress(netapi,"NetStatisticsGet");
-               netfree = (NETFREE) GetProcAddress(netapi,"NetApiBufferFree");
+               netstatget = (NETSTATGET) GetProcAddress(netapi,TEXT("NetStatisticsGet"));
+               netfree = (NETFREE) GetProcAddress(netapi,TEXT("NetApiBufferFree"));
                }
 
        if (netstatget && netfree)
@@ -249,7 +263,9 @@ int RAND_poll(void)
        if (netapi)
                FreeLibrary(netapi);
 #endif /* 1 */
+#endif /* !OPENSSL_SYS_WINCE */
  
+#ifndef OPENSSL_SYS_WINCE
         /* It appears like this can cause an exception deep within ADVAPI32.DLL
          * at random times on Windows 2000.  Reported by Jeffrey Altman.  
          * Only use it on NT.
@@ -280,7 +296,7 @@ int RAND_poll(void)
                        bufsz += 8192;
 
                        length = bufsz;
-                       rc = RegQueryValueEx(HKEY_PERFORMANCE_DATA, "Global",
+                       rc = RegQueryValueEx(HKEY_PERFORMANCE_DATA, TEXT("Global"),
                                NULL, NULL, buf, &length);
                        }
                if (rc == ERROR_SUCCESS)
@@ -304,15 +320,16 @@ int RAND_poll(void)
                        free(buf);
                }
 #endif
+#endif /* !OPENSSL_SYS_WINCE */
 
        if (advapi)
                {
                acquire = (CRYPTACQUIRECONTEXT) GetProcAddress(advapi,
-                       "CryptAcquireContextA");
+                       TEXT("CryptAcquireContextA"));
                gen = (CRYPTGENRANDOM) GetProcAddress(advapi,
-                       "CryptGenRandom");
+                       TEXT("CryptGenRandom"));
                release = (CRYPTRELEASECONTEXT) GetProcAddress(advapi,
-                       "CryptReleaseContext");
+                       TEXT("CryptReleaseContext"));
                }
 
        if (acquire && gen && release)
@@ -366,9 +383,9 @@ int RAND_poll(void)
                GETFOREGROUNDWINDOW win;
                GETQUEUESTATUS queue;
 
-               win = (GETFOREGROUNDWINDOW) GetProcAddress(user, "GetForegroundWindow");
-               cursor = (GETCURSORINFO) GetProcAddress(user, "GetCursorInfo");
-               queue = (GETQUEUESTATUS) GetProcAddress(user, "GetQueueStatus");
+               win = (GETFOREGROUNDWINDOW) GetProcAddress(user, TEXT("GetForegroundWindow"));
+               cursor = (GETCURSORINFO) GetProcAddress(user, TEXT("GetCursorInfo"));
+               queue = (GETQUEUESTATUS) GetProcAddress(user, TEXT("GetQueueStatus"));
 
                if (win)
                        {
@@ -439,17 +456,17 @@ int RAND_poll(void)
                MODULEENTRY32 m;
 
                snap = (CREATETOOLHELP32SNAPSHOT)
-                       GetProcAddress(kernel, "CreateToolhelp32Snapshot");
-               heap_first = (HEAP32FIRST) GetProcAddress(kernel, "Heap32First");
-               heap_next = (HEAP32NEXT) GetProcAddress(kernel, "Heap32Next");
-               heaplist_first = (HEAP32LIST) GetProcAddress(kernel, "Heap32ListFirst");
-               heaplist_next = (HEAP32LIST) GetProcAddress(kernel, "Heap32ListNext");
-               process_first = (PROCESS32) GetProcAddress(kernel, "Process32First");
-               process_next = (PROCESS32) GetProcAddress(kernel, "Process32Next");
-               thread_first = (THREAD32) GetProcAddress(kernel, "Thread32First");
-               thread_next = (THREAD32) GetProcAddress(kernel, "Thread32Next");
-               module_first = (MODULE32) GetProcAddress(kernel, "Module32First");
-               module_next = (MODULE32) GetProcAddress(kernel, "Module32Next");
+                       GetProcAddress(kernel, TEXT("CreateToolhelp32Snapshot"));
+               heap_first = (HEAP32FIRST) GetProcAddress(kernel, TEXT("Heap32First"));
+               heap_next = (HEAP32NEXT) GetProcAddress(kernel, TEXT("Heap32Next"));
+               heaplist_first = (HEAP32LIST) GetProcAddress(kernel, TEXT("Heap32ListFirst"));
+               heaplist_next = (HEAP32LIST) GetProcAddress(kernel, TEXT("Heap32ListNext"));
+               process_first = (PROCESS32) GetProcAddress(kernel, TEXT("Process32First"));
+               process_next = (PROCESS32) GetProcAddress(kernel, TEXT("Process32Next"));
+               thread_first = (THREAD32) GetProcAddress(kernel, TEXT("Thread32First"));
+               thread_next = (THREAD32) GetProcAddress(kernel, TEXT("Thread32Next"));
+               module_first = (MODULE32) GetProcAddress(kernel, TEXT("Module32First"));
+               module_next = (MODULE32) GetProcAddress(kernel, TEXT("Module32Next"));
 
                if (snap && heap_first && heap_next && heaplist_first &&
                        heaplist_next && process_first && process_next &&
@@ -584,7 +601,7 @@ static void readtimer(void)
        DWORD w;
        LARGE_INTEGER l;
        static int have_perfc = 1;
-#ifdef _MSC_VER
+#if defined(_MSC_VER) && !defined(OPENSSL_SYS_WINCE)
        static int have_tsc = 1;
        DWORD cyclecount;
 
@@ -637,6 +654,7 @@ static void readtimer(void)
 
 static void readscreen(void)
 {
+#ifndef OPENSSL_SYS_WINCE
   HDC          hScrDC;         /* screen DC */
   HDC          hMemDC;         /* memory DC */
   HBITMAP      hBitmap;        /* handle for our bitmap */
@@ -650,7 +668,7 @@ static void readscreen(void)
   int          n = 16;         /* number of screen lines to grab at a time */
 
   /* Create a screen DC and a memory DC compatible to screen DC */
-  hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
+  hScrDC = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
   hMemDC = CreateCompatibleDC(hScrDC);
 
   /* Get screen resolution */
@@ -697,6 +715,7 @@ static void readscreen(void)
   DeleteObject(hBitmap);
   DeleteDC(hMemDC);
   DeleteDC(hScrDC);
+#endif /* !OPENSSL_SYS_WINCE */
 }
 
 #endif