The attached code will make it so that the RAND_screen function can be used
to seed OpenSSL on CE.  It replaces the readscreen function provided with
the library in the crypto\rand\rand_win.c function.  I have not tested it on
non-CE devices, so you may want to #ifdef it in if you will be supporting
standard Windows as well.

static void readscreen(void)
{
        HDC hdc, hdcScr;
        int nWidth, nHeight;
        BITMAPINFO dibInfo;
        BYTE *pBGR = NULL;
        HBITMAP hNewBitmap, hOldBitmap;
        int nBufSize;
        int nChunkSize;
        int y;

        // Take a snapshot of the screen
        hdcScr = GetDC(NULL);
        hdc = CreateCompatibleDC(hdcScr);

        // Get screen resolution
        nWidth = GetDeviceCaps(hdc, HORZRES);
        nHeight = GetDeviceCaps(hdc, VERTRES);

        // Fill in the DIB structure
        dibInfo.bmiHeader.biBitCount = 24;
        dibInfo.bmiHeader.biClrImportant = 0;
        dibInfo.bmiHeader.biClrUsed = 0;
        dibInfo.bmiHeader.biCompression = 0;
        dibInfo.bmiHeader.biHeight = nHeight;
        dibInfo.bmiHeader.biPlanes = 1;
        dibInfo.bmiHeader.biSize = 40;
        dibInfo.bmiHeader.biSizeImage = nWidth*nHeight*3;
        dibInfo.bmiHeader.biWidth = nWidth;
        dibInfo.bmiHeader.biXPelsPerMeter = 3780;
        dibInfo.bmiHeader.biYPelsPerMeter = 3780;
        dibInfo.bmiColors[0].rgbBlue = 0;
        dibInfo.bmiColors[0].rgbGreen = 0;
        dibInfo.bmiColors[0].rgbRed = 0;
        dibInfo.bmiColors[0].rgbReserved = 0;

        // Create a new device independent bitmap and retrieve
        // a pointer to its bit storage -- this is the raw
        // bitmap data that will be hashed
        hNewBitmap = CreateDIBSection(hdc,
                (const BITMAPINFO*)&dibInfo,
                DIB_RGB_COLORS, (void**)&pBGR, NULL, 0);
        hOldBitmap = (HBITMAP) SelectObject(hdc, hNewBitmap);

        // Copy the bitmap into the new device context -- this will
        // also copy the bitmap to the DIB
        BitBlt(hdc, 0, 0, nWidth, nHeight, hdcScr, 0, 0, SRCCOPY);

        // Determine the buffer size of the screen data
        nBufSize = 3 * nWidth * nHeight;
        nChunkSize = nBufSize / ( 3 * 16 );

        /* Now go through the whole screen, repeatedly grabbing n lines */
        for ( y = 0; y < nBufSize; y += nChunkSize )
        {
                unsigned char md[MD_DIGEST_LENGTH];

                /* Get the hash of the bitmap */
                MD(pBGR + y, nChunkSize, md);

                /* Seed the random generator with the hash value */
                RAND_seed(md, MD_DIGEST_LENGTH);
        }

        // Restore the empty bitmap to the device context -- this
        // clears up any potential resource leaks
        SelectObject(hdc, hOldBitmap);

        // Delete the DIB
        DeleteObject(hNewBitmap);

        // Done with the DCs.
        DeleteDC( hdc );
        DeleteDC( hdcScr );
}


The code is derived from an article in Dr. Dobb's Journal (December 2002,
"Automated Testing & Windows CE"), so you should have no problems
distributing it.  Unfortunately, I haven't found a better way get random
information on the CE devices.

One additional suggestion would be to use the RAND_write_file and
RAND_load_file functions each time you exit and restart your application.
This will allow your randomness to "accumulate" each time your appliction is
run.

Brant Thomsen
Sr. Software Engineer
Wavelink Corporation

> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Behalf Of Antonio Ruiz
> Martínez
> Sent: Tuesday, March 22, 2005 12:23 PM
> To: openssl-users@openssl.org
> Subject: RAND_Bytes in Windows CE (Pocket PC or smartphone)
>
>
> Hello!
>
>     I'm writing you because when I call to the function to make a pkcs#7
> enveloped data, I get the following error:
> 1673169562:error:2406064:random number generator:SSLEAY_RAND_BYTES_PRNG
> not seeded:.\crypto\rand\md_rand.c:503:You need to read the OpenSSL FAQ.
>
> I read it but the problems are related to Unix System, but what about
> Windows CE?
> Could you help me to solve it, please?
> Thanks in advance,
> Regards,
> Antonio.

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to