e65605e26c362f6f11058eedce020499d28e5d08
[openssl.git] / apps / winrand.c
1 /*
2  * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*-
11  * Usage: winrand [filename]
12  *
13  * Collects entropy from mouse movements and other events and writes
14  * random data to filename or .rnd
15  */
16
17 #include <windows.h>
18 #include <openssl/opensslv.h>
19 #include <openssl/rand.h>
20
21 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
22 const char *filename;
23
24 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
25                    PSTR cmdline, int iCmdShow)
26 {
27     static char appname[] = "OpenSSL";
28     HWND hwnd;
29     MSG msg;
30     WNDCLASSEX wndclass;
31     char buffer[200];
32
33     if (cmdline[0] == '\0')
34         filename = RAND_file_name(buffer, sizeof buffer);
35     else
36         filename = cmdline;
37
38     RAND_load_file(filename, -1);
39
40     wndclass.cbSize = sizeof(wndclass);
41     wndclass.style = CS_HREDRAW | CS_VREDRAW;
42     wndclass.lpfnWndProc = WndProc;
43     wndclass.cbClsExtra = 0;
44     wndclass.cbWndExtra = 0;
45     wndclass.hInstance = hInstance;
46     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
47     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
48     wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
49     wndclass.lpszMenuName = NULL;
50     wndclass.lpszClassName = appname;
51     wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
52     RegisterClassEx(&wndclass);
53
54     hwnd = CreateWindow(appname, OPENSSL_VERSION_TEXT,
55                         WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
56                         CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance,
57                         NULL);
58
59     ShowWindow(hwnd, iCmdShow);
60     UpdateWindow(hwnd);
61
62     while (GetMessage(&msg, NULL, 0, 0)) {
63         TranslateMessage(&msg);
64         DispatchMessage(&msg);
65     }
66
67     return msg.wParam;
68 }
69
70 LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
71 {
72     HDC hdc;
73     PAINTSTRUCT ps;
74     RECT rect;
75     static int seeded = 0;
76
77     switch (iMsg) {
78     case WM_PAINT:
79         hdc = BeginPaint(hwnd, &ps);
80         GetClientRect(hwnd, &rect);
81         DrawText(hdc, "Seeding the PRNG. Please move the mouse!", -1,
82                  &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
83         EndPaint(hwnd, &ps);
84         return 0;
85
86     case WM_DESTROY:
87         PostQuitMessage(0);
88         return 0;
89     }
90
91     if (RAND_event(iMsg, wParam, lParam) == 1 && seeded == 0) {
92         seeded = 1;
93         if (RAND_write_file(filename) <= 0)
94             MessageBox(hwnd, "Couldn't write random file!",
95                        "OpenSSL", MB_OK | MB_ICONERROR);
96         PostQuitMessage(0);
97     }
98
99     return DefWindowProc(hwnd, iMsg, wParam, lParam);
100 }