21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Table 5-8. Symmetric ciphers supported by Microsoft Cryptographic Service Providers (continued)<br />

Cipher<br />

Cryptographic<br />

Service Provider ALG_ID constant Key length Block size<br />

AES AES CALG_AES_192 192 bits 128 bits<br />

AES AES CALG_AES_256 256 bits 128 bits<br />

The default cipher mode to be used depends on the underlying CSP and the algorithm<br />

that’s being used, but it’s generally CBC mode. The Microsoft Base and<br />

Enhanced CSPs provide support for CBC, CFB, ECB, and OFB modes (see Recipe 5.4<br />

for a discussion of cipher modes). The mode can be set using the CryptSetKeyParam( )<br />

function:<br />

BOOL SpcSetKeyMode(HCRYPTKEY hKey, DWORD dwMode) {<br />

return CryptSetKeyParam(hKey, KP_MODE, (BYTE *)&dwMode, 0);<br />

}<br />

#define SpcSetMode_CBC(hKey) SpcSetKeyMode((hKey), CRYPT_MODE_CBC)<br />

#define SpcSetMode_CFB(hKey) SpcSetKeyMode((hKey), CRYPT_MODE_CFB)<br />

#define SpcSetMode_ECB(hKey) SpcSetKeyMode((hKey), CRYPT_MODE_ECB)<br />

#define SpcSetMode_OFB(hKey) SpcSetKeyMode((hKey), CRYPT_MODE_OFB)<br />

In addition, the initialization vector for block ciphers will be set to zero, which is<br />

almost certainly not what you want. The function presented below, SpcSetIV( ), will<br />

allow you to set the IV for a key explicitly or will generate a random one for you. The<br />

IV should always be the same size as the block size for the cipher in use.<br />

BOOL SpcSetIV(HCRYPTPROV hProvider, HCRYPTKEY hKey, BYTE *pbIV) {<br />

BOOL bResult;<br />

BYTE *pbTemp;<br />

DWORD dwBlockLen, dwDataLen;<br />

if (!pbIV) {<br />

dwDataLen = sizeof(dwBlockLen);<br />

if (!CryptGetKeyParam(hKey, KP_BLOCKLEN, (BYTE *)&dwBlockLen, &dwDataLen, 0))<br />

return FALSE;<br />

dwBlockLen /= 8;<br />

if (!(pbTemp = (BYTE *)LocalAlloc(LMEM_FIXED, dwBlockLen))) return FALSE;<br />

bResult = CryptGenRandom(hProvider, dwBlockLen, pbTemp);<br />

if (bResult)<br />

bResult = CryptSetKeyParam(hKey, KP_IV, pbTemp, 0);<br />

LocalFree(pbTemp);<br />

return bResult;<br />

}<br />

return CryptSetKeyParam(hKey, KP_IV, pbIV, 0);<br />

}<br />

Once you have a key object, it can be used for encrypting and decrypting data. Access<br />

to the low-level algorithm implementation is not permitted through CryptoAPI.<br />

Instead, a high-level OpenSSLEVP-like interface is provided (see Recipes 5.17 and<br />

5.22 for details on OpenSSL’s EVP API), though it’s somewhat simpler. Both<br />

Using Symmetric Encryption with Microsoft’s CryptoAPI | 241<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!