C/C++ API
HSM Dinamo
Loading...
Looking for...
No entries found
sign_verify.c

Example of digital signature and verification.

See Note on examples.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <dinamo.h> /* header do Dinamo */
#define HOST_ADDR "127.0.0.1"
#define USER_ID "master"
#define USER_PWD "12345678"
#define HASH_ID ALG_SHA1
#define KEY_ID "Test_Key"
#define KEY_TYPE ALG_RSA_1024
int main()
{
int nRet = 0;
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("Function failure: DInitialize \nError code: %d\n",nRet);
goto clean;
}
printf("Libraries initialized.\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("Function failure: DOpenSession \nError code: %d\n",nRet);
goto clean;
}
printf("Session with Dinamo established.\n");
//Cria um contexto de hash
nRet = DCreateHash(hSession, HASH_ID, 0, 0, &hHash);
if (nRet){
printf("Function failure: DCreateHash \nError code: %d\n",nRet);
goto clean;
}
printf("Hash context created.\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("Function failure: DHashData \nError code: %d\n",nRet);
goto clean;
}
printf("Data added to hash.\n");
nRet = DGenerateKey(hSession, KEY_ID, KEY_TYPE, 0, &hKey);
if (nRet){
printf("Function failed: DGenerateKey \nError code: %d\n",nRet);
goto clean;
}
printf("Key created successfully.\n");
nRet = DSignHash(hHash, hKey, 0, NULL, &cbSignature);
if ( D_MORE_DATA != nRet){
printf("Function failure: DSignHash \nError code: %d\n",nRet);
goto clean;
}
printf("Signature size: "DWORD_PRINT" bytes.\n", cbSignature);
pbSignature = NULL;
pbSignature = malloc(cbSignature);
nRet = DSignHash(hHash, hKey, 0, pbSignature, &cbSignature);
if (nRet){
printf("Function failure: DSignHash \nError code: %d\n",nRet);
goto clean;
}
printf("Buffer signed.\n");
nRet = DVerifySignature(hHash, pbSignature, cbSignature, hKey, 0);
if (nRet){
printf("Function failure: DVerifySignature \nError code: %d\n",nRet);
goto clean;
}
printf("Signature successfully verified.\n");
//Remove a chave do HSM
nRet = DDestroyKey(&hKey, REMOVE_FROM_HCM);
if (nRet){
printf("Function failed: DDestroyKey \nError code: %d\n",nRet);
goto clean;
}
printf("Key successfully removed.\n");
clean:
if (pbSignature)
free(pbSignature);
if (hKey){
DDestroyKey(&hKey, 0);
printf("Key context released.\n");
}
if (hHash){
DDestroyHash(&hHash);
printf("Hash context released.\n");
}
if (hSession){
DCloseSession(&hSession, 0);
printf("Session closed.\n");
}
printf("Libraries finished.\n");
return nRet;
}
HSM Application Programming Interface (API) Dinamo.
void * HSESSIONCTX
Definition dinamo.h:68
#define DEFAULT_PORT
Definition dinamo.h:1948
#define DWORD_PRINT
Definition dinamo.h:55
unsigned char BYTE
Definition dinamo.h:45
#define REMOVE_FROM_HCM
Definition dinamo.h:1463
unsigned int DWORD
Definition dinamo.h:46
void * HHASHCTX
Definition dinamo.h:69
#define ENCRYPTED_CONN
Definition dinamo.h:585
#define SS_USER_PWD
Definition dinamo.h:576
#define D_MORE_DATA
Definition dinamo.h:144
void * HKEYCTX
Definition dinamo.h:70
int AAP_API DCreateHash(HSESSIONCTX hSession, int nAlgId, HKEYCTX hKey, DWORD dwFlags, HHASHCTX *hHash)
int AAP_API DDestroyHash(HHASHCTX *phHash)
int AAP_API DHashData(HHASHCTX hHash, BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
int AAP_API DSignHash(HHASHCTX hHash, HKEYCTX hKey, DWORD dwFlags, BYTE *pbSignature, DWORD *pdwSigLen)
int AAP_API DVerifySignature(HHASHCTX hHash, BYTE *pbSignature, DWORD dwSigLen, HKEYCTX hPubKey, DWORD dwFlags)
int AAP_API DGenerateKey(HSESSIONCTX hSession, char *szKeyId, int nAlgId, DWORD dwFlags, HKEYCTX *phKey)
int AAP_API DDestroyKey(HKEYCTX *phKey, DWORD dwFlags)
int AAP_API DOpenSession(HSESSIONCTX *phSession, DWORD dwParam, BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
int AAP_API DCloseSession(HSESSIONCTX *phSession, DWORD dwFlags)
int AAP_API DInitialize(DWORD dwReserved)
int AAP_API DFinalize()
Definition dinamo.h:3089
int nPort
Definition dinamo.h:3091
char szUserId[MAX_USR_LEN]
Definition dinamo.h:3092
char szAddr[MAX_ADDR_LEN]
Definition dinamo.h:3090
char szPassword[MAX_USR_PWD]
Definition dinamo.h:3093