#include "crypto/controlse/csecure_element.hxx"
#include "crypto/controlse/ccertificate.hxx"
#include "crypto/controlse/cpublic_key.hxx"
#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
extern "C"
{
#include <nuttx/crypto/se05x.h>
}
namespace Controlse
{
CSecureElement::CSecureElement(const char *se05x_device)
: se05x_fd(open(se05x_device, O_RDONLY)), close_device_at_destructor(true)
{
}
CSecureElement::CSecureElement(int fd)
: se05x_fd(fd), close_device_at_destructor(false)
{
}
CSecureElement::~CSecureElement()
{
if ((se05x_fd >= 0) && close_device_at_destructor)
{
close(se05x_fd);
}
}
bool CSecureElement::IsReady() const { return se05x_fd >= 0; }
bool CSecureElement::GenerateKey(struct se05x_generate_keypair_s &args) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_GENERATE_KEYPAIR, &args);
}
return result;
}
bool CSecureElement::SetKey(struct se05x_key_transmission_s &args) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_SET_KEY, &args);
}
return result;
}
bool CSecureElement::GetKey(struct se05x_key_transmission_s &args) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_GET_KEY, &args);
}
return result;
}
bool CSecureElement::DeleteKey(uint32_t id) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_DELETE_KEY, id);
}
return result;
}
bool CSecureElement::SetData(struct se05x_key_transmission_s &args) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_SET_DATA, &args);
}
return result;
}
bool CSecureElement::GetData(struct se05x_key_transmission_s &args) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_GET_DATA, &args);
}
return result;
}
bool CSecureElement::CreateSignature(struct se05x_signature_s &args) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_CREATE_SIGNATURE, &args);
}
return result;
}
bool CSecureElement::Verify(struct se05x_signature_s &args) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_VERIFY_SIGNATURE, &args);
}
return result;
}
bool CSecureElement::DeriveSymmetricalKey(
struct se05x_derive_key_s &args) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_DERIVE_SYMM_KEY, &args);
}
return result;
}
bool CSecureElement::GetUid(struct se05x_uid_s &args) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_GET_UID, &args);
}
return result;
}
bool CSecureElement::GetInfo(struct se05x_info_s &args) const
{
bool result = false;
if (se05x_fd >= 0)
{
result = 0 == ioctl(se05x_fd, SEIOC_GET_INFO, &args);
}
return result;
}
CCertificate *CSecureElement::GetCertificate(uint32_t keystore_id)
{
return new CCertificate(*this, keystore_id);
}
CPublicKey *CSecureElement::GetPublicKey(uint32_t keystore_id)
{
return new CPublicKey(*this, keystore_id);
}
}