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

Example of encryption with symmetric keys.

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 KEY_ID "sym_key"
#define KEY_TYPE ALG_3DES_168
int main()
{
int nRet = 0;
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
HKEYCTX hKey = NULL;
BYTE pbData[256];
DWORD cbData;
//Inicializa as bibliotecas do Dinamo
nRet = DInitialize(0);
if (nRet){
printf("Falha na funcao: DInitialize \nCodigo de erro: %d\n",nRet);
goto clean;
}
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;
}
//Gera uma chave simétrica
printf("Sessao com o Dinamo estabelecida.\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");
//Dados que serão criptografados
strcpy((char *)pbData, "Dados a serem criptografados.");
cbData = strlen((char *)pbData) + 1;
printf("Original: %s\n", pbData);
printf("Criptografando "DWORD_PRINT" bytes (padding PKCS#5) \n", cbData);
nRet = DEncrypt(hKey, 0, 1, 0, pbData, &cbData, sizeof(pbData));
if (nRet){
printf("Falha na funcao: DEncrypt \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Dados criptografados com sucesso.\nTotal de "DWORD_PRINT" bytes retornados\n", cbData);
nRet = DDecrypt(hKey, 0, 1, 0, pbData, &cbData);
if (nRet){
printf("Falha na funcao: DDecrypt \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Decriptado: %s\n", pbData);
printf(""DWORD_PRINT" bytes\n", cbData);
//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 (hKey){
DDestroyKey(&hKey, 0);
printf("Contexto de chave liberada.\n");
}
if (hSession){
DCloseSession(&hSession, 0);
printf("Sessao encerrada.\n");
}
printf("Bibliotecas finalizada.\n");
return nRet;
}
Application Programming Interface (API) do HSM Dinamo.
void * HSESSIONCTX
Definição dinamo.h:68
#define DEFAULT_PORT
Definição dinamo.h:1934
#define DWORD_PRINT
Definição dinamo.h:55
unsigned char BYTE
Definição dinamo.h:45
#define REMOVE_FROM_HCM
Definição dinamo.h:1449
unsigned int DWORD
Definição dinamo.h:46
#define ENCRYPTED_CONN
Definição dinamo.h:570
#define SS_USER_PWD
Definição dinamo.h:561
void * HKEYCTX
Definição dinamo.h:70
int AAP_API DDecrypt(HKEYCTX hKey, HHASHCTX hHash, BOOL bFinal, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
int AAP_API DEncrypt(HKEYCTX hKey, HHASHCTX hHash, BOOL bFinal, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
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()
Definição dinamo.h:3110
int nPort
Definição dinamo.h:3112
char szUserId[MAX_USR_LEN]
Definição dinamo.h:3113
char szAddr[MAX_ADDR_LEN]
Definição dinamo.h:3111
char szPassword[MAX_USR_PWD]
Definição dinamo.h:3114