Example of encryption with symmetric keys.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define HOST_ADDR "127.0.0.1"
#define USER_ID "master"
#define USER_PWD "12345678"
#define KEY_ID "sym_key"
#define KEY_TYPE ALG_3DES_168
int main()
{
int nRet = 0;
if (nRet){
printf("Function failure: DInitialize \nError code: %d\n",nRet);
goto clean;
}
printf("Libraries initialized.\n");
strncpy(authPwd.
szAddr, HOST_ADDR,
sizeof(authPwd
.szAddr));
strncpy(authPwd.
szUserId, USER_ID,
sizeof(authPwd
.szUserId));
strncpy(authPwd.
szPassword, USER_PWD,
sizeof(authPwd
.szPassword));
if (nRet){
printf("Function failure: DOpenSession \nError code: %d\n",nRet);
goto clean;
}
printf("Session with Dinamo established.\n");
if (nRet){
printf("Function failed: DGenerateKey \nError code: %d\n",nRet);
goto clean;
}
printf("Key created successfully.\n");
strcpy((char *)pbData, "Data to be encrypted.");
cbData = strlen((char *)pbData) + 1;
printf("Original: %s\n", pbData);
printf(
"Encrypting "DWORD_PRINT" bytes (padding PKCS#5) \n", cbData);
nRet =
DEncrypt(hKey, 0, 1, 0, pbData, &cbData,
sizeof(pbData));
if (nRet){
printf("Function failure: DEncrypt \nError code: %d\n",nRet);
goto clean;
}
printf(
"Data successfully encrypted.\nTotal "DWORD_PRINT" bytes returned\n", cbData);
nRet =
DDecrypt(hKey, 0, 1, 0, pbData, &cbData);
if (nRet){
printf("Function failure: DDecrypt \nError code: %d\n",nRet);
goto clean;
}
printf("Decrypted: %s\n", pbData);
if (nRet){
printf("Function failed: DDestroyKey \nError code: %d\n",nRet);
goto clean;
}
printf("Key successfully removed.\n");
clean:
if (hKey){
printf("Key context released.\n");
}
if (hSession){
printf("Session closed.\n");
}
printf("Libraries finished.\n");
return nRet;
}
HSM Application Programming Interface (API) Dinamo.
void * HSESSIONCTX
Definition dinamo.h:68
#define DEFAULT_PORT
Definition dinamo.h:1948
#define DWORD_PRINT
Definition dinamo.h:55
unsigned char BYTE
Definition dinamo.h:45
#define REMOVE_FROM_HCM
Definition dinamo.h:1463
unsigned int DWORD
Definition dinamo.h:46
#define ENCRYPTED_CONN
Definition dinamo.h:585
#define SS_USER_PWD
Definition dinamo.h:576
void * HKEYCTX
Definition dinamo.h:70
int AAP_API DDecrypt(HKEYCTX hKey, HHASHCTX hHash, BOOL bFinal, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
int AAP_API DEncrypt(HKEYCTX hKey, HHASHCTX hHash, BOOL bFinal, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
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 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 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