signverify.c

Exemplo de assinatura digital e verificação.

Veja Nota sobre os exemplos.
#include <windows.h>
#include <stdio.h>
#include <dinamo.h> /* header do Dinamo */
#define HOST_ADDR "10.0.62.47"
#define USER_ID "master"
#define USER_PWD "12345678"
#define HASH_ID ALG_SHA1
#define KEY_ID "Chave_Teste"
#define KEY_TYPE ALG_RSA_1024
void main()
{
int nRet;
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
HHASHCTX hHash = NULL;
HKEYCTX hKey = NULL;
BYTE pbData[256];
DWORD cbData;
BYTE *pbSignature = NULL;
DWORD cbSignature;
//Inicializa as bibliotecas do Dinamo
nRet = DInitialize(0);
if (nRet){
printf("Falha na funcao: DInitialize \nCodigo de erro: %d\n",nRet);
exit(1);
}
printf("Bibliotecas inicializadas.\n");
//Inicializa a estrutura para conexão com o HSM
strncpy(authPwd.szAddr, HOST_ADDR, sizeof(authPwd.szAddr));
authPwd.nPort = DEFAULT_PORT;
strncpy(authPwd.szUserId, USER_ID, sizeof(authPwd.szUserId));
strncpy(authPwd.szPassword, USER_PWD, sizeof(authPwd.szPassword));
nRet = DOpenSession(&hSession, SS_USER_PWD, (BYTE *)&authPwd, sizeof(authPwd), ENCRYPTED_CONN);
if (nRet){
printf("Falha na funcao: DOpenSession \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Sessao com o Dinamo estabelecida.\n");
//Cria um contexto de hash
nRet = DCreateHash(hSession, HASH_ID, 0, 0, &hHash);
if (nRet){
printf("Falha na funcao: DCreateHash \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Contexto de hash criado.\n");
//Preenche o buffer com dados quaisquer
cbData = sizeof(pbData);
memset(pbData, 'A', cbData);
//Adiciona dados ao hash
nRet = DHashData(hHash, pbData, cbData, 0);
if (nRet){
printf("Falha na funcao: DHashData \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Dados adicionados ao hash.\n");
nRet = DGenerateKey(hSession, KEY_ID, KEY_TYPE, 0, &hKey);
if (nRet){
printf("Falha na funcao: DGenerateKey \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Chave criada com sucesso.\n");
nRet = DSignHash(hHash, hKey, 0, NULL, &cbSignature);
if (D_MORE_DATA != nRet){
printf("Falha na funcao: DSignHash \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Tamanho da assinatura: %d bytes.\n", cbSignature);
pbSignature = NULL;
pbSignature = malloc(cbSignature);
nRet = DSignHash(hHash, hKey, 0, pbSignature, &cbSignature);
if (nRet){
printf("Falha na funcao: DSignHash \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Buffer assinado.\n");
nRet = DVerifySignature(hHash, pbSignature, cbSignature, hKey, 0);
if (nRet){
printf("Falha na funcao: DVerifySignature \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Assinatura verificada com sucesso.\n");
//Remove a chave do HSM
nRet = DDestroyKey(&hKey, REMOVE_FROM_HCM);
if (nRet){
printf("Falha na funcao: DDestroyKey \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Chave removida com sucesso.\n");
clean:
if (pbSignature)
free(pbSignature);
if (hKey){
DDestroyKey(&hKey, 0);
printf("Contexto da chave liberado.\n");
}
if (hHash){
DDestroyHash(&hHash);
printf("Contexto de hash liberado.\n");
}
if (hSession){
DCloseSession(&hSession, 0);
printf("Sessao encerrada.\n");
}
printf("Bibliotecas finalizada.\n");
}