Reputation:
OK-- thanks to Mike, I was able to get Wincrypt to generate a Diffie-Hellman keypair. I figured out out to export the public key, and how to import the other party's public key. According to the docs, upon import of the other party's public key, the shared secret has been computed. Great.
I now need to get ahold of that shared secret, but I don't think its possible. Simply calling CryptExportKey
with a type of PLAINTEXTKEYBLOB
fails unless I call CryptSetKeyParam
to change the algorithm id from CALG_AGREEDKEY_ANY
to something... else. But I don't want something else, I want the shared secret. The API, however, seems designed to discourage this.
Any ideas out there? I should note that the problem here is that I'm only writing one side of an implementation of WiFi Protected Setup. So the protocol is defined for me, and the other party is not giving me HCRYPTKEYs.
Upvotes: 4
Views: 1883
Reputation: 184
This looks like what you need... from: http://msdn.microsoft.com/en-us/library/aa381969(VS.85).aspx
To import a Diffie-Hellman public key and calculate the secret session key
CryptAcquireContext
function to get a handle to the Microsoft Diffie-Hellman Cryptographic Provider.CryptGenKey
function to create a new key, or by calling the CryptGetUserKey
function to retrieve an existing key.CryptImportKey
function, passing a pointer to the public key BLOB in the pbData
parameter, the length of the BLOB in the dwDataLen
parameter, and the handle to the Diffie-Hellman key in the hPubKey
parameter. This causes the calculation, (Y^X) mod P
, to be performed, thus creating the shared, secret key and completing the key exchange. This function call returns a handle to the new, secret, session key in the hKey
parameter.CALG_AGREEDKEY_ANY
. Before the key can be used, it must be converted into a session key type. This is accomplished by calling the CryptSetKeyParam
function with dwParam
set to KP_ALGID
and with pbData
set to a pointer to a ALG_ID
value that represents a session key, such as CALG_RC4
. The key must be converted before using the shared key in the CryptEncrypt
or CryptDecrypt
function. Calls made to either of these functions prior to converting the key type will fail.CryptDestroyKey
function.Upvotes: 2