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

Example of OTP generation/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 KEY_ID "Chave_Teste"
#define KEY_TYPE ALG_AES_256
int main()
{
int nRet = 0;
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
HKEYCTX hKey = NULL;
DWORD pdwBlobLen = sizeof(pbBlob);
struct tag_ISSUE_OTP_BLOB pvParamBlob;
DWORD dwParamBlobLen = 0;
char szOTP[ISSUE_OATH_MAX_OTP_LEN+1] = {0};
//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");
//Geração da chave AES
nRet = DGenerateKey(hSession, KEY_ID, KEY_TYPE, EXPORTABLE_KEY, &hKey);
if(nRet){
printf("Falha na funcao: DGenerateKey \nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("Chave AES gerada com sucesso.\n");
//Inicializa a estrutura do HOTP
pvParamBlob.bSeedLen = ISSUE_OATH_SHA256_LEN;
pvParamBlob.bTruncationOffset = ISSUE_OATH_DYN_TRUNC ;
dwParamBlobLen = sizeof(pvParamBlob);
//Gera a semente.
nRet = DOATHIssueBlob(hSession,
KEY_ID,
&pvParamBlob,
dwParamBlobLen,
pbBlob,
&pdwBlobLen,
0);
if(nRet){
printf("Falha na funcao: DOATHIssueBlob \nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("Geracao da semente feita com sucesso.\n");
//Gera o próximo OTP.
nRet = DOATHGetNextOTP(hSession,
KEY_ID,
pbBlob,
pdwBlobLen,
szOTP,
0);
if(nRet){
printf("Falha na funcao: DOATHGetNextOTP \nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("Proximo OTP gerado com sucesso.\n");
//checa o OTP
nRet = DOATHCheckOTP(hSession,
KEY_ID,
szOTP,
pbBlob,
&pdwBlobLen,
0);
if(nRet){
printf("Falha na funcao: DOATHCheckOTP \nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("Valor OTP verificado com sucesso.\n");
clean:
if(hKey){
//Remove a chave AES
printf("Chave removida com sucesso.\n");
}
if (hSession){
DCloseSession(&hSession, 0);
printf("Sessao encerrada.\n");
}
printf("Bibliotecas finalizadas.\n");
return nRet;
}
Application Programming Interface (API) do HSM Dinamo.
void * HSESSIONCTX
Definição dinamo.h:68
#define DEFAULT_PORT
Definição dinamo.h:1949
#define ISSUE_OATH_MAX_OTP_LEN
Definição dinamo.h:1905
#define REMOVE_FROM_HSM
Definição dinamo.h:1463
unsigned char BYTE
Definição dinamo.h:45
#define ISSUE_OATH_DYN_TRUNC
Definição dinamo.h:1914
unsigned int DWORD
Definição dinamo.h:46
#define ENCRYPTED_CONN
Definição dinamo.h:585
#define SS_USER_PWD
Definição dinamo.h:576
#define ISSUE_OATH_SHA256_LEN
Definição dinamo.h:1910
void * HKEYCTX
Definição dinamo.h:70
#define ISSUE_OATH_GENERATE_HOTP
Definição dinamo.h:1933
#define EXPORTABLE_KEY
Definição dinamo.h:1423
#define ISSUE_OATH_OUTPUT_MAX_BLOB_LEN
Definição dinamo.h:1929
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 DOATHIssueBlob(HSESSIONCTX hSession, char *szMasterKeyId, DWORD dwParamBlobType, void *pvParamBlob, DWORD dwParamBlobLen, BYTE *pbOTPBlob, DWORD *pdwOTPBlobLen, DWORD dwFlags)
int AAP_API DOATHGetNextOTP(HSESSIONCTX hSession, char *szMasterKeyId, BYTE bOTPLen, BYTE *pbOATHBlob, DWORD dwOATHBlobLen, char *szOTP, DWORD dwFlags)
int AAP_API DOATHCheckOTP(HSESSIONCTX hSession, char *szMasterKeyId, char *szOTP, BYTE *pbOATHBlob, DWORD *pdwOATHBlobLen, 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:3090
int nPort
Definição dinamo.h:3092
char szUserId[MAX_USR_LEN]
Definição dinamo.h:3093
char szAddr[MAX_ADDR_LEN]
Definição dinamo.h:3091
char szPassword[MAX_USR_PWD]
Definição dinamo.h:3094