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

Example of displaying a key's attributes.

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 "Key_Name"
BOOL IsRSAKey(DWORD dwType)
{
switch(dwType)
{
return TRUE;
}
return FALSE;
}
int main()
{
int nRet;
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
HKEYCTX hKey = NULL;
DWORD dwDataLen,
dwKeyType,
dwPadding,
dwReadLocked,
dwKeyLen,
dwMode;
BYTE *pbData;
int i;
//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 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("Function failure: DOpenSession \nError code: %d\n",nRet);
goto clean;
}
printf("Session with Dinamo established.\n");
//Gera uma chave simétrica
nRet = DGenerateKey(hSession, KEY_ID, ALG_AES_256, 0, &hKey);
if (nRet)
{
printf("Function failure: DGenerateKey \nError code: %d\n", nRet);
goto clean;
}
printf("Key generated successfully.\n");
//Libera o contexto da chave
DDestroyKey(&hKey, 0);
/*
In this example we generate the key and release the key context in the
sequence.
To demonstrate the use of the API, the context of the key is retrieved again
using the DGetUserKey function.
*/
//Recupera o handle de uma chave armazenada no HSM
nRet = DGetUserKey(hSession, KEY_ID, 0, &hKey);
if (nRet){
printf("Function failed: DGetUserKey \nError code: %d\n",nRet);
goto clean;
}
/* Algorithm */
dwDataLen = sizeof(DWORD);
nRet = DGetKeyParam(hKey, DKP_ALGID,(BYTE *)&dwKeyType, &dwDataLen, 0);
if (nRet){
printf("Function failure: DGetKeyParam \nError code: %d\n",nRet);
goto clean;
}
printf("Key type "DWORD_PRINT"\n", dwKeyType);
/* Size */
dwDataLen = sizeof(DWORD);
nRet = DGetKeyParam(hKey, DKP_KEYLEN,(BYTE *)&dwKeyLen, &dwDataLen, 0);
if (nRet){
printf("Function failure: DGetKeyParam \nError code: %d\n",nRet);
goto clean;
}
printf("Key size "DWORD_PRINT" bits\n", dwKeyLen * 8);
dwDataLen = sizeof(dwReadLocked);
nRet = DGetKeyParam(hKey, DKP_READ_LOCK,(BYTE *)&dwReadLocked, &dwDataLen, 0);
if (nRet){
printf("Function failure: DGetKeyParam \nError code: %d\n",nRet);
goto clean;
}
printf("Key exportable? %s\n", dwReadLocked == 1 ? "No": "Yes " );
if (IsRSAKey(dwKeyType)){
printf("End of asymmetric key attributes.\n");
goto clean;
}
/* IV */
//Recupera o tamanho do IV (memória necessária)
dwDataLen = 0;
nRet = DGetKeyParam(hKey, DKP_IV, NULL, &dwDataLen, 0);
if (nRet){
printf("Function failure: DGetKeyParam \nError code: %d\n",nRet);
goto clean;
}
pbData =(BYTE *)malloc(dwDataLen);
nRet = DGetKeyParam(hKey, DKP_IV, pbData, &dwDataLen, 0);
if (nRet){
printf("Function failure: DGetKeyParam \nError code: %d\n",nRet);
goto clean;
}
printf("IV: ");
for (i = 0; i < (int)dwDataLen; i++)
printf("%02x ", pbData[i]);
printf("\n");
free(pbData);
/* Padding */
dwDataLen = sizeof(DWORD);
nRet = DGetKeyParam(hKey, DKP_PADDING,(BYTE *)&dwPadding, &dwDataLen, 0);
if (nRet){
printf("Function failure: DGetKeyParam \nError code: %d\n",nRet);
goto clean;
}
printf("Padding type "DWORD_PRINT"\n", dwPadding);
/* Symmetric encryption operating mode */
dwDataLen = sizeof(dwMode);
nRet = DGetKeyParam(hKey, DKP_MODE,(BYTE *)&dwMode, &dwDataLen, 0);
if (nRet){
printf("Function failure: DGetKeyParam \nError code: %d\n",nRet);
goto clean;
}
printf("Operation mode "DWORD_PRINT"\n", dwMode);
clean:
// Apenas o contexto da chave é liberado, a chave não é removida do HSM
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.
#define ALG_RSA_4096
Definition dinamo.h:1086
#define DKP_ALGID
Definition dinamo.h:1467
int BOOL
Definition dinamo.h:49
void * HSESSIONCTX
Definition dinamo.h:68
#define DEFAULT_PORT
Definition dinamo.h:1948
#define DKP_READ_LOCK
Definition dinamo.h:1473
#define ALG_RSA_512
Definition dinamo.h:1083
#define DKP_MODE
Definition dinamo.h:1470
#define ALG_RSA_1024_PUB
Definition dinamo.h:1211
#define ALG_AES_256
Definition dinamo.h:1028
#define ALG_RSA_4096_PUB
Definition dinamo.h:1213
#define DWORD_PRINT
Definition dinamo.h:55
#define DKP_IV
Definition dinamo.h:1468
unsigned char BYTE
Definition dinamo.h:45
#define DKP_KEYLEN
Definition dinamo.h:1475
unsigned int DWORD
Definition dinamo.h:46
#define ALG_RSA_2048_PUB
Definition dinamo.h:1212
#define ALG_RSA_1024
Definition dinamo.h:1084
#define ENCRYPTED_CONN
Definition dinamo.h:585
#define SS_USER_PWD
Definition dinamo.h:576
#define ALG_RSA_2048
Definition dinamo.h:1085
#define TRUE
Definition dinamo.h:61
#define FALSE
Definition dinamo.h:58
#define DKP_PADDING
Definition dinamo.h:1469
void * HKEYCTX
Definition dinamo.h:70
#define ALG_RSA_512_PUB
Definition dinamo.h:1210
int AAP_API DGetUserKey(HSESSIONCTX hSession, char *szKeyId, DWORD dwFlags, HKEYCTX *phKey)
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 DGetKeyParam(HKEYCTX hKey, DWORD dwParam, BYTE *pbData, DWORD *pdwDataLen, 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()
Definition dinamo.h:3089