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

Example of EFT to generate and verify CVV.

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 PAN "4123456789012345"
#define EXPIRATION_DATE "8701"
#define SERVICE_CODE "101"
#define CVV_RESULT "561"
#define KEY_ID "KEY_CVV"
#define KEY_VALUE { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }
#define KEY_TYPE ALG_3DES_112
int main()
{
int nRet = 0;
char szCVV[MIN_CVV_LEN];
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
HKEYCTX hKey = NULL;
BYTE pbKeyData[] = KEY_VALUE;
//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 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("Function failure: DOpenSession \nError code: %d\n", nRet);
goto clean;
}
printf("Session with Dinamo established.\n");
// Importa chave temporaria
nRet = DImportKey( hSession, KEY_ID, NULL, PLAINTEXTKEY_BLOB, KEY_TYPE, TEMPORARY_KEY, pbKeyData, sizeof(pbKeyData), &hKey );
if(nRet){
printf("Function failure: DImportKey\nError code: %d\n", nRet);
goto clean;
}
// Gera CVV baseado na chave temporaria e demais parametrizacoes
nRet = DGenerateCVV( hSession, KEY_ID, PAN, EXPIRATION_DATE, SERVICE_CODE, szCVV, 0);
if(nRet){
printf("Function failure: DGenerateCVV\nError code: %d\n", nRet);
goto clean;
}
// Validacao do algoritmo - compara CVV resultante com o CVV esperado
// (com chave e demais parametrizacoes iguais o resultado e sempre o mesmo)
if( strcmp( szCVV, CVV_RESULT ) ) {
printf("Failed to compare generated CVV (%s) and expected CVV (%s).", szCVV, CVV_RESULT);
}
// Verifica se o CVV gerado d realmente valido no HSM (usando mesma chave e parametrizacoes)
nRet = DVerifyCVV( hSession, KEY_ID, PAN, EXPIRATION_DATE, SERVICE_CODE, szCVV, 0);
if(nRet){
printf("Function failure: DVerifyCVV\nError code: %d\n", nRet);
goto clean;
}
printf("CVV successfully verified!");
clean:
if (hKey) {
DDestroyKey(&hKey, 0);
printf("Key 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 MIN_CVV_LEN
Definition dinamo.h:1585
#define DEFAULT_PORT
Definition dinamo.h:1948
#define TEMPORARY_KEY
Definition dinamo.h:1424
unsigned char BYTE
Definition dinamo.h:45
#define ENCRYPTED_CONN
Definition dinamo.h:585
#define SS_USER_PWD
Definition dinamo.h:576
void * HKEYCTX
Definition dinamo.h:70
#define PLAINTEXTKEY_BLOB
Definition dinamo.h:1381
int AAP_API DVerifyCVV(HSESSIONCTX hSession, char *szKeyId, char *szPAN, char *szExpirationDate, char *szServiceCode, char *szCVV, DWORD dwParam)
int AAP_API DGenerateCVV(HSESSIONCTX hSession, char *szKeyId, char *szPAN, char *szExpirationDate, char *szServiceCode, char *szCVV, DWORD dwParam)
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: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