API C/C
HSM Dinamo
Cargando...
Buscando...
No se han encontrado entradas
skeep_new_split_recover.c

Ejemplo de creación, división y recuperación de secretos utilizando el módulo de custodia.

Véase la nota sobre ejemplos.
/* skeep_new_split_recover.c
*
* Ejemplo que demuestra cómo crear un secreto, dividirlo en acciones y
* recupéralo de las partes M.
*
* Crear un secreto
* Dividir el secreto en N partes donde M partes son necesarias para
* recuperar el secreto. Opcionalmente: generar la suma de comprobación para cada parte.
* 3. recuperar el secreto de M partes
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dinamo.h" /* Dinamo header */
#define HOST_ADDR "127.0.0.1"
#define USER_ID "maestro"
#define USER_PWD "12345678"
static int OpenSession(HSESSIONCTX *hSession, const char *cszIp, const char *cszUser,
const char *cszContraseña)
{
struct AUTH_PWD authPwd;
/* Inicializa la estructura de conexión con el HSM */
strncpy(authPwd. szAddr, HOST_ADDR, sizeof(authPwd.szAddr));
authPwd. nPort = DEFAULT_PORT;
strncpy(authPwd.szUserId, USER_ID, sizeof(authPwd.szUserId));
strncpy(authPwd. szContraseña, USER_PWD, sizeof(authPwd.szContraseña));
return DOpenSession(hSession, SS_USER_PWD,(BYTE *)&authPwd, sizeof(authPwd), ENCRYPTED_CONN);
}
static void PrintError(const char *cszFunction, int nRet)
{
printf("Fallo de función: %s \nCódigo de error: %d\n", cszFunción, nRet);
}
int main()
{
int nRet = 0;
HSESSIONCTX hSession = NULL;
#define M (2) /* Número mínimo de acciones para recuperar el secreto */
#define N (3) /* Número total de acciones */
const char *szId = "secreto";
BYTE bSecLevel = DN_SKEEP_SEC_LEVEL_160b; /* Nivel de seguridad secreto */
WORD wAuthType = DN_SKEEP_TYPE_NMIND; /* Tipo de autenticación para el secreto. En esta prueba, no es el usuario y contraseña por defecto. */
int i = 0;
SKeepShare shares[N] = {0}; /* Vetor de partes */
SKeepRecoverInfo stRecoverInfo = {0}; /* Estrutura para recuperar o segredo */
Dinamo /* Inicializa las bibliotecas de */
nRet = DInitialize(0);
si (nRet)
{
PrintError("DInitialise", nRet);
ir a limpiar;
}
printf("Bibliotecas inicializadas.\n");
/* Abrir sesión con HSM */
nRet = OpenSession(&hSession, HOST_ADDR, USER_ID, USER_PWD);
si (nRet)
{
PrintError("DOpenSession", nRet);
ir a limpiar;
}
printf("Sesión con Dinamo establecida.\n");
/*
1. Crear un secreto
*/
nRet = DSKeepNewSecret(hSession, szId, bSecLevel, wAuthType, 0);
if(nRet)
{
PrintError("DSKeepNewSecret", nRet);
ir a limpiar;
}
printf("Secreto creado con éxito.\n");
/*
2. Divide el secreto en partes.
El secreto se divide en N partes, de las cuales M son necesarias
para recuperar el secreto.
*/
nRet = DSKeepSplitSecret(hSession, szId, bSecLevel, wAuthType, M, N, shares, 0);
si (nRet)
{
PrintError("DSKeepSplitSecret", nRet);
ir a limpiar;
}
printf("Secreto dividir con éxito.\n");
/*
Opcional: Calcular la suma de comprobación de cada parte.
La suma de comprobación se utiliza para verificar que la pieza se ha introducido correctamente en el
aplicación de la ceremonia.
Lo ideal es guardarlo junto con la pieza y comprobarlo previamente
para recuperar el secreto.
*/
for (i = 0; i < N; i++)
{
char szShareCks[DN_SKEEP_SHARE_CKS_LEN + 1] = {0};
nRet = DSKeepCalcShareCks(DN_SKEEP_GEN_SHARE_CKS, shares[i].szShare, szShareCks);
si (nRet)
{
PrintError("DSKeepSplitSecret", nRet);
ir a limpiar;
}
printf("Acción %d: %s [%s]\n", i, acciones[i].szCompartir, szCompartirCks);
}
/*
3. Recuperar el secreto de M partes
*/
nRet = DSKeepRecoverSecret(hSession, szId, shares, M, &stRecoverInfo, 0);
si (nRet)
{
PrintError("DSKeepRecoverSecret", nRet);
ir a limpiar;
}
printf("Secreto recuperado con éxito.\n");
printf("Secreto: %s\n", stRecoverInfo. szSecret);
/* Eliminar el secreto */
nRet = DSKeepRemoveSecret(hSession, szId, bSecLevel, wAuthType, 0);
si (nRet)
{
PrintError("DSKeepRemoveSecret", nRet);
ir a limpiar;
}
printf("Secreto eliminado con éxito.\n");
limpia:
DCloseSession(&hSession, 0);
printf("Sesión cerrada.\n");
printf("Bibliotecas finalizadas.\n");
devolver nRet;
}
Interfaz de programación de aplicaciones (API) de HSM Dinamo.
void * HSESSIONCTX
Definición dinamo.h:68
#define PUERTO_DEFAULT
Definición dinamo.h:1948
unsigned short WORD
Definición dinamo.h:48
unsigned char BYTE
Definición dinamo.h:45
#define ENCRYPTED_CONN
dinamoDefinición.h:585
#define SS_USER_PWD
dinamoDefinición.h:576
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()
int AAP_API DSKeepNewSecret(HSESSIONCTX hSession, const char *cszId, BYTE bSecLevel, WORD wAuthType, DWORD dwReserved)
#define DN_SKEEP_SEC_LEVEL_160b
dinamoDefinición.h:9083
#define DN_SKEEP_GEN_SHARE_CKS
dinamoDefinición.h:9196
#define DN_SKEEP_SHARE_CKS_LEN
dinamoDefinición.h:9194
int AAP_API DSKeepRemoveSecret(HSESSIONCTX hSession, const char *cszId, BYTE bSecLevel, WORD wAuthType, DWORD dwReserved)
int AAP_API DSKeepCalcShareCks(DWORD dwType, const char *cszShare, char *szCks)
#define DN_SKEEP_TYPE_NMIND
dinamoDefinición.h:9086
int AAP_API DSKeepSplitSecret(HSESSIONCTX hSession, const char *cszId, BYTE bSecLevel, WORD wAuthType, BYTE bM, BYTE bN, SKeepShare *pstShares, DWORD dwReserved)
int AAP_API DSKeepRecoverSecret(HSESSIONCTX hSession, const char *cszId, const SKeepShare *cpstShares, DWORD dwSharesCount, SKeepRecoverInfo *pstRecoverInfo, DWORD dwReserved)
Definición dinamo.h:3089
int nPuerto
Definición dinamo.h:3091
char szUserId[MAX_USR_LEN]
Definición dinamo.h:3092
char szAddr[MAX_ADDR_LEN]
Definición dinamo.h:3090
char szContraseña[MAX_USR_PWD]
dinamoDefinición.h:3093
dinamoDefinición.h:3640
char szSecret[DN_SKEEP_M_OF_N_S_LEN+1]
dinamoDefinición.h:3642
dinamoDefinición.h:3649