Разные результаты в разных ОС
От: C3PO  
Дата: 15.11.14 17:32
Оценка:
Всем привет.

Есть программка, передранная из MSDN, чуть-чуть сокращённая и маленько дополненная выводом на экран. Она иллюстрирует обмен ключами Diffie-Hellman и показательно шифрует алгоритмом RC4 массив. Во всех доступных мне версиях Windows (начиная с XP и заканчивая 7x64) она выводит что и должна — текст о том, что другого текста на экране не ожидается. Но в Windows 8.1 x64 появляется проблема со второй расшифровкой буфера ("D2 failed"). При этом функция расшифровки возвращает TRUE, полагая, видимо, что отработала правильно. Ткните меня носом в ошибку. Я внатуре устал.

#include "stdafx.h"
#include <tchar.h>
#include <windows.h>
#include <wincrypt.h>
#pragma comment(lib, "crypt32.lib")

// The key size, in bits.
#define DHKEYSIZE 512

// Prime in little-endian format.
static const BYTE g_rgbPrime[] = 
{
    0x91, 0x02, 0xc8, 0x31, 0xee, 0x36, 0x07, 0xec, 
    0xc2, 0x24, 0x37, 0xf8, 0xfb, 0x3d, 0x69, 0x49, 
    0xac, 0x7a, 0xab, 0x32, 0xac, 0xad, 0xe9, 0xc2, 
    0xaf, 0x0e, 0x21, 0xb7, 0xc5, 0x2f, 0x76, 0xd0, 
    0xe5, 0x82, 0x78, 0x0d, 0x4f, 0x32, 0xb8, 0xcb,
    0xf7, 0x0c, 0x8d, 0xfb, 0x3a, 0xd8, 0xc0, 0xea, 
    0xcb, 0x69, 0x68, 0xb0, 0x9b, 0x75, 0x25, 0x3d,
    0xaa, 0x76, 0x22, 0x49, 0x94, 0xa4, 0xf2, 0x8d 
};

// Generator in little-endian format.
static BYTE g_rgbGenerator[] = 
{
    0x02, 0x88, 0xd7, 0xe6, 0x53, 0xaf, 0x72, 0xc5,
    0x8c, 0x08, 0x4b, 0x46, 0x6f, 0x9f, 0x2e, 0xc4,
    0x9c, 0x5c, 0x92, 0x21, 0x95, 0xb7, 0xe5, 0x58, 
    0xbf, 0xba, 0x24, 0xfa, 0xe5, 0x9d, 0xcb, 0x71, 
    0x2e, 0x2c, 0xce, 0x99, 0xf3, 0x10, 0xff, 0x3b,
    0xcb, 0xef, 0x6c, 0x95, 0x22, 0x55, 0x9d, 0x29,
    0x00, 0xb5, 0x4c, 0x5b, 0xa5, 0x63, 0x31, 0x41,
    0x13, 0x0a, 0xea, 0x39, 0x78, 0x02, 0x6d, 0x62
};

BYTE g_rgbData[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

int _tmain(int argc, _TCHAR* argv[])
{
    puts("It is expected the program should not write to the display anything but this text.");

    UNREFERENCED_PARAMETER(argc);
    UNREFERENCED_PARAMETER(argv);
    
    BOOL fReturn;
    HCRYPTPROV hProvParty1 = NULL; 
    HCRYPTPROV hProvParty2 = NULL; 
    DATA_BLOB P;
    DATA_BLOB G;
    HCRYPTKEY hPrivateKey1 = NULL;
    HCRYPTKEY hPrivateKey2 = NULL;
    PBYTE pbKeyBlob1 = NULL;
    PBYTE pbKeyBlob2 = NULL;
    HCRYPTKEY hSessionKey1 = NULL;
    HCRYPTKEY hSessionKey2 = NULL;
    PBYTE pbData = NULL;
    LPBYTE copy = NULL;

    P.cbData = DHKEYSIZE/8;
    P.pbData = (BYTE*)(g_rgbPrime);

    G.cbData = DHKEYSIZE/8;
    G.pbData = (BYTE*)(g_rgbGenerator);

    fReturn = CryptAcquireContext(&hProvParty1, NULL, MS_ENH_DSS_DH_PROV, PROV_DSS_DH, CRYPT_VERIFYCONTEXT);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptGenKey(hProvParty1, CALG_DH_EPHEM, DHKEYSIZE << 16 | CRYPT_EXPORTABLE | CRYPT_PREGEN, &hPrivateKey1);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptSetKeyParam(hPrivateKey1, KP_P, (PBYTE)&P, 0);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptSetKeyParam(hPrivateKey1, KP_G, (PBYTE)&G, 0);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptSetKeyParam(hPrivateKey1, KP_X, NULL, 0);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptAcquireContext(&hProvParty2, NULL, MS_ENH_DSS_DH_PROV, PROV_DSS_DH, CRYPT_VERIFYCONTEXT);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptGenKey(hProvParty2, CALG_DH_EPHEM, DHKEYSIZE << 16 | CRYPT_EXPORTABLE | CRYPT_PREGEN, &hPrivateKey2);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptSetKeyParam(hPrivateKey2, KP_P, (PBYTE)&P, 0);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptSetKeyParam(hPrivateKey2, KP_G, (PBYTE)&G, 0);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptSetKeyParam(hPrivateKey2, KP_X, NULL, 0);
    if(!fReturn)
        goto ErrorExit;

    DWORD dwDataLen1;

    fReturn = CryptExportKey(hPrivateKey1, NULL, PUBLICKEYBLOB, 0, NULL, &dwDataLen1);
    if(!fReturn)
        goto ErrorExit;

    if(!(pbKeyBlob1 = (PBYTE)malloc(dwDataLen1)))
        goto ErrorExit;

    fReturn = CryptExportKey(hPrivateKey1, 0, PUBLICKEYBLOB, 0, pbKeyBlob1, &dwDataLen1);
    if(!fReturn)
        goto ErrorExit;

    DWORD dwDataLen2;

    fReturn = CryptExportKey(hPrivateKey2, NULL, PUBLICKEYBLOB, 0, NULL, &dwDataLen2);
    if(!fReturn)
        goto ErrorExit;

    if(!(pbKeyBlob2 = (PBYTE)malloc(dwDataLen2)))
        goto ErrorExit;

    fReturn = CryptExportKey(hPrivateKey2, 0, PUBLICKEYBLOB, 0, pbKeyBlob2, &dwDataLen2);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptImportKey(hProvParty1, pbKeyBlob2, dwDataLen2, hPrivateKey1, 0, &hSessionKey1);
    if(!fReturn)
        goto ErrorExit;
    
    fReturn = CryptImportKey(hProvParty2, pbKeyBlob1, dwDataLen1, hPrivateKey2, 0, &hSessionKey2);
    if(!fReturn)
        goto ErrorExit;

    ALG_ID Algid = CALG_RC4;

    fReturn = CryptSetKeyParam(hSessionKey1, KP_ALGID, (PBYTE)&Algid, 0);
    if(!fReturn)
        goto ErrorExit;

    fReturn = CryptSetKeyParam(hSessionKey2, KP_ALGID, (PBYTE)&Algid, 0);
    if(!fReturn)
        goto ErrorExit;

    DWORD dwLength = sizeof(g_rgbData);
    fReturn = CryptEncrypt(hSessionKey1, 0, TRUE, 0, NULL, &dwLength, sizeof(g_rgbData));
    if(!fReturn)
        goto ErrorExit;

    pbData = (PBYTE)malloc(dwLength);
    if(!pbData)
        goto ErrorExit;

    memcpy(pbData, g_rgbData, sizeof(g_rgbData)); 

    dwLength = sizeof(g_rgbData);
    fReturn = CryptEncrypt(hSessionKey1, 0, TRUE, 0, pbData, &dwLength, sizeof(g_rgbData));
    if(!fReturn)
        goto ErrorExit;
  
    copy = (PBYTE)malloc(dwLength);
    memcpy(copy, pbData, dwLength);

    dwLength = sizeof(g_rgbData);
    fReturn = CryptDecrypt(hSessionKey2, 0, TRUE, 0, pbData, &dwLength);
    if(!fReturn)
        goto ErrorExit;

    if (memcmp(g_rgbData, pbData, sizeof(g_rgbData))){
        puts("D1 failed");
    }

    memcpy(pbData, g_rgbData, sizeof(g_rgbData)); 

    dwLength = sizeof(g_rgbData);
    fReturn = CryptEncrypt(hSessionKey2, 0, TRUE, 0, pbData, &dwLength, sizeof(g_rgbData));
    if(!fReturn)
        goto ErrorExit;

    dwLength = sizeof(g_rgbData);
    fReturn = CryptDecrypt(hSessionKey1, 0, TRUE, 0, pbData, &dwLength);
    if(!fReturn)
        goto ErrorExit;

    if (memcmp(g_rgbData, pbData, sizeof(g_rgbData))){
        puts("D2 failed");
    }

    memcpy(pbData, copy, dwLength);

    dwLength = sizeof(g_rgbData);
    fReturn = CryptDecrypt(hSessionKey1, 0, TRUE, 0, pbData, &dwLength);
    if(!fReturn)
        goto ErrorExit;

    if (memcmp(g_rgbData, pbData, sizeof(g_rgbData))){
        puts("D3 failed");
    }

ErrorExit:
    if (copy)
    {
        free(copy);
        copy = NULL;
    }

    if(pbData)
    {
        free(pbData);
        pbData = NULL;
    }

    if(hSessionKey2)
    {
        CryptDestroyKey(hSessionKey2);
        hSessionKey2 = NULL;
    }

    if(hSessionKey1)
    {
        CryptDestroyKey(hSessionKey1);
        hSessionKey1 = NULL;
    }

    if(pbKeyBlob2)
    {
        free(pbKeyBlob2);
        pbKeyBlob2 = NULL;
    }

    if(pbKeyBlob1)
    {
        free(pbKeyBlob1);
        pbKeyBlob1 = NULL;
    }

    if(hPrivateKey2)
    {
        CryptDestroyKey(hPrivateKey2);
        hPrivateKey2 = NULL;
    }

    if(hPrivateKey1)
    {
        CryptDestroyKey(hPrivateKey1);
        hPrivateKey1 = NULL;
    }

    if(hProvParty2)
    {
        CryptReleaseContext(hProvParty2, 0);
        hProvParty2 = NULL;
    }

    if(hProvParty1)
    {
        CryptReleaseContext(hProvParty1, 0);
        hProvParty1 = NULL;
    }

    return 0;
}


P.S. Если вы поможете решить проблему и вы в Москве — я вам лично ящик пива завезу.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.