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

Example of HSM ECDH generation.

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 ECDH_KEY_NAME "ecdh_key"
#define ECDH_PAIR_KEY_NAME "ecdh_pair_key"
#define ECDH_NEW_DERIVED_KEY "ecdh_derived_key"
#define ECDH_KEY_ALG (ALG_ECC_BRAINPOOL_P512T1)
int main()
{
int nRet = 0;
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
HKEYCTX hKey = NULL;
HKEYCTX hPairKey = NULL;
BYTE pbPubKey[1024] = {0};
DWORD dwPubKeyLen = sizeof(pbPubKey);
BYTE pbSecret[1024] = {0};
DWORD dwSecretLen = sizeof(pbSecret);
//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;
}
printf("Sessao com o Dinamo estabelecida.\n");
nRet = DGenerateKey(hSession, ECDH_KEY_NAME, ECDH_KEY_ALG, 0, &hKey);
if(nRet)
{
printf("Falha ao gerar chave principal.\n");
goto clean;
}
printf("Chave principal gerada com sucesso.\n");
nRet = DGenerateKey(hSession, ECDH_PAIR_KEY_NAME, ECDH_KEY_ALG, 0, &hPairKey);
if(nRet)
{
printf("Falha ao gerar chave do par.\n");
goto clean;
}
printf("Chave do par gerada com sucesso.\n");
nRet = DExportKey(hPairKey, NULL, PUBLICKEY_BLOB, 0, pbPubKey, &dwPubKeyLen);
if(nRet)
{
printf("Falha ao exportar chave publica do par.\n");
goto clean;
}
printf("Chave publica do par exportada com sucesso.\n");
nRet = DGenEcdhKey(hSession,
ECDH_KEY_NAME,
pbPubKey,
dwPubKeyLen,
pbSecret,
&dwSecretLen,
0);
if(nRet)
{
printf("Falha ao gerar ECDH RAW Secret.\n");
goto clean;
}
printf("Segredo ECDH RAW secret gerado com sucesso.\n");
BYTE pbKDFData[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18};
GEN_ECDH_X9_63 stEcdhX963Info;
memset(&stEcdhX963Info, 0, sizeof(stEcdhX963Info));
strncpy(stEcdhX963Info.szTargetKeyName, ECDH_NEW_DERIVED_KEY, sizeof(stEcdhX963Info.szTargetKeyName));
stEcdhX963Info.dwTargetKeyAlg = ALG_AES_256;
stEcdhX963Info.dbPubKey.pvData = pbPubKey;
stEcdhX963Info.dbPubKey.dwDataLen = dwPubKeyLen;
stEcdhX963Info.dbKDFData.pvData = pbKDFData;
stEcdhX963Info.dbKDFData.dwDataLen = sizeof(pbKDFData);
nRet = DGenEcdhKey(hSession,
ECDH_KEY_NAME,
&stEcdhX963Info,
sizeof(stEcdhX963Info),
NULL,
NULL,
0);
if(nRet)
{
printf("Falha ao gerar ECDH X9.63 Secret.\n");
goto clean;
}
printf("Chave ECDH X9.64 derivada com sucesso.\n");
nRet = DRemoveObj(hSession, ECDH_NEW_DERIVED_KEY);
if(nRet)
{
printf("Falha ao remover chave derivada.\n");
goto clean;
}
printf("Chave derivada removida com sucesso.\n");
clean:
printf("Contexto das chaves foram liberados.\n");
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:1949
#define PUBLICKEY_BLOB
Definição dinamo.h:1380
#define ALG_AES_256
Definição dinamo.h:1029
#define TEMPORARY_KEY
Definição dinamo.h:1425
#define REMOVE_FROM_HSM
Definição dinamo.h:1463
unsigned char BYTE
Definição dinamo.h:45
unsigned int DWORD
Definição dinamo.h:46
#define DN_GEN_KEY_KDF_RAW_SECRET
Definição dinamo.h:2026
#define DN_GEN_KEY_X9_63_SHA256
Definição dinamo.h:2027
#define ENCRYPTED_CONN
Definição dinamo.h:585
#define SS_USER_PWD
Definição dinamo.h:576
void * HKEYCTX
Definição dinamo.h:70
#define EXPORTABLE_KEY
Definição dinamo.h:1423
int AAP_API DGenEcdhKey(HSESSIONCTX hSession, DWORD dwOP, char *szPriKey, void *pvInData, DWORD dwInDataLen, BYTE *pbOutData, DWORD *pdwOutDataLen, 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 DExportKey(HKEYCTX hKey, HKEYCTX hKEKey, DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
int AAP_API DRemoveObj(HSESSIONCTX hSession, char *szObjId)
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
void * pvData
Definição dinamo.h:2320
DWORD dwDataLen
Definição dinamo.h:2321
Definição dinamo.h:3413
DWORD dwTargetKeyAlg
Definição dinamo.h:3417
DWORD dwTargetKeyAttributes
Definição dinamo.h:3418
char szTargetKeyName[MAX_OBJ_ID_FQN_LEN]
Definição dinamo.h:3414
DBLOB dbPubKey
Definição dinamo.h:3419
DBLOB dbKDFData
Definição dinamo.h:3420