I have P & G— how do I use the Wincrypt API to generate a Diffie-Hellman keypair?

天涯浪子 提交于 2019-12-11 15:09:49

问题


There's an MSDN article here, but I'm not getting very far:

p = 139;
g = 5;

CRYPT_DATA_BLOB pblob;
pblob.cbData = sizeof( ULONG );
pblob.pbData = ( LPBYTE ) &p;

CRYPT_DATA_BLOB gblob;
gblob.cbData = sizeof( ULONG );
gblob.pbData = ( LPBYTE ) &g;

HCRYPTKEY hKey;
if ( ::CryptGenKey( m_hCryptoProvider, CALG_DH_SF,
                    CRYPT_PREGEN, &hKey ) )
{
    ::CryptSetKeyParam( hKey, KP_P, ( LPBYTE ) &pblob, 0 );

Fails here with NTE_BAD_DATA. I'm using MS_DEF_DSS_DH_PROV. What gives?


回答1:


It may be that it just doesn't like the very short keys you're using.

I found the desktop version of that article which may help, as it has a full example.

EDIT:

The OP realised from the example that you have to tell CryptGenKey how long the keys are, which you do by setting the top 16-bits of the flags to the number of bits you want to use. If you leave this as 0, you get the default key length. This is documented in the Remarks section of the device documentation, and with the dwFlags parameter in the desktop documentation.

For the Diffie-Hellman key-exchange algorithm, the Base provider defaults to 512-bit keys and the Enhanced provider (which is the default) defaults to 1024-bit keys, on Windows XP and later. There doesn't seem to be any documentation for the default lengths on CE.

The code should therefore be:

BYTE p[64] = { 139 }; // little-endian, all other bytes set to 0
BYTE g[64] = { 5 };

CRYPT_DATA_BLOB pblob;
pblob.cbData = sizeof( p);
pblob.pbData = p;

CRYPT_DATA_BLOB gblob;
gblob.cbData = sizeof( g );
gblob.pbData = g;

HCRYPTKEY hKey;
if ( ::CryptGenKey( m_hCryptoProvider, CALG_DH_SF,
                    ( 512 << 16 ) | CRYPT_PREGEN, &hKey ) )
{
    ::CryptSetKeyParam( hKey, KP_P, ( LPBYTE ) &pblob, 0 );



回答2:


It looks to me that KP_P, KP_G, KP_Q are for DSS keys (Digital Signature Standard?). For Diffie-Hellman it looks like you're supposed to use KP_PUB_PARAMS and pass a DATA_BLOB that points to a DHPUBKEY_VER3 structure.

Note that the article you're pointing to is from the Windows Mobile/Windows CE SDK. It wouldn't be the first time that CE worked differently from the desktop/server.

EDIT: CE does not implement KP_PUB_PARAMS. To use this structure on the desktop, see Diffie-Hellman Version 3 Public Key BLOBs.



来源:https://stackoverflow.com/questions/76581/i-have-p-g-how-do-i-use-the-wincrypt-api-to-generate-a-diffie-hellman-keypai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!