API C HSM Dinamo
API Proprietária C do HSM Dinamo
pin_block_translate.c

Exemplo de tradução de PIN block .

Veja Nota sobre os exemplos.
#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_TYPE ALG_3DES_112
#define SRC_KEY_VALUE \
{ \
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, \
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 \
}
/*
* Bloco de PIN fictício, gerado a partir da src_key e bloco com os dados
* ABABABABABABABAB
*/
#define FAKE_PINBLOCK \
{ \
0xD9, 0x3B, 0x9C, 0xC2, 0x8B, 0x8F, 0x11, 0xF9 \
}
int main()
{
int nRet = 0;
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
HKEYCTX hSrcKey = NULL;
HKEYCTX hDstKey = NULL;
BYTE pbKeyData[] = SRC_KEY_VALUE;
BYTE pbPinBlock[] = FAKE_PINBLOCK;
BYTE pbOutPinBlock[DES_BLOCK] = {};
char szSrcKey[] = "src_key";
char szDstKey[] = "dst_key";
// 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 conexao 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");
// Importa chave de origem
nRet = DImportKey(hSession, szSrcKey, NULL, PLAINTEXTKEY_BLOB, KEY_TYPE,
TEMPORARY_KEY, pbKeyData, sizeof(pbKeyData), &hSrcKey);
if(nRet){
printf("Falha na funcao: DImportKey\nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("Chave de origem importada com sucesso!\n");
/*
* O contexto da chave pode ser liberado agora. Precisaremos apenas do
* nome da chave daqui em diante.
*/
DDestroyKey(&hSrcKey, 0);
// Gera chave de destino
nRet = DGenerateKey(hSession, szDstKey, KEY_TYPE, TEMPORARY_KEY, &hDstKey);
if(nRet){
printf("Falha na funcao: DGenerateKey\nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("Chave de destino gerada com sucesso!\n");
/*
* O contexto da chave pode ser liberado agora. Precisaremos apenas do
* nome da chave daqui em diante.
*/
DDestroyKey(&hDstKey, 0);
/*
* Traduz o bloco de PIN de acordo com a chave de origem e destino
* neste caso usa a tradução automática/opaca (TP_TRANSLATE_TYPE_AUTO)
* sem necessidade de informar o PAN.
*/
nRet = DPINBlockTranslate(hSession, szSrcKey, szDstKey, TP_TRANSLATE_TYPE_AUTO,
NULL, pbPinBlock, pbOutPinBlock, 0);
if(nRet){
printf("Falha na funcao: DPINBlockTranslate\nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("PIN block traduzido com sucesso!\n");
clean:
if (hSession) {
DCloseSession(&hSession, 0);
printf("Sessao encerrada.\n");
}
printf("Bibliotecas finalizada.\n");
return nRet;
}
Application Programming Interface (API) do HSM Dinamo.
void * HSESSIONCTX
Definition: dinamo.h:68
#define DEFAULT_PORT
Definition: dinamo.h:1896
#define TEMPORARY_KEY
Definition: dinamo.h:1389
#define TP_TRANSLATE_TYPE_AUTO
Definition: dinamo.h:1622
unsigned char BYTE
Definition: dinamo.h:45
#define DES_BLOCK
Definition: dinamo.h:1020
#define ENCRYPTED_CONN
Definition: dinamo.h:560
#define SS_USER_PWD
Definition: dinamo.h:551
void * HKEYCTX
Definition: dinamo.h:70
#define PLAINTEXTKEY_BLOB
Definition: dinamo.h:1346
int AAP_API DPINBlockTranslate(HSESSIONCTX hSession, char *szSrcPEK, char *szDstPEK, BYTE bTransBlockType, char *szPAN, BYTE *pbInPinBlock, BYTE *pbOutPinBlock, DWORD dwParam)
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 DImportKey(HSESSIONCTX hSession, char *szKeyId, HKEYCTX hKEKey, DWORD dwBlobType, int nAlgId, DWORD dwFlags, BYTE *pbData, DWORD dwDataLen, HKEYCTX *phKey)
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:3037
int nPort
Definition: dinamo.h:3039
char szUserId[MAX_USR_LEN]
Definition: dinamo.h:3040
char szAddr[MAX_ADDR_LEN]
Definition: dinamo.h:3038
char szPassword[MAX_USR_PWD]
Definition: dinamo.h:3041