psa_sim_serialise.pl now creates the updated .c file

Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
This commit is contained in:
Tom Cosgrove 2024-06-10 15:50:53 +01:00
parent db87a44898
commit f252f61611
2 changed files with 188 additions and 4 deletions

View File

@ -85,6 +85,7 @@ static ssize_t allocate_hash_operation_slot(void)
return -1; /* all in use */
}
/* Find the slot given the handle */
static ssize_t find_hash_slot_by_handle(psasim_client_handle_t handle)
{
for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) {
@ -450,10 +451,6 @@ int psasim_deserialise_psa_hash_operation_t(uint8_t **pos,
return 1;
}
/* On the server side, we have a certain number of slots. One array holds the
* psa_XXX_operation_t values by slot, the other holds the client-side handles
* for the slots.
*/
size_t psasim_server_serialise_psa_hash_operation_t_needs(psa_hash_operation_t *operation)
{
(void) operation;

View File

@ -69,7 +69,17 @@ if ($which eq "h") {
} elsif ($which eq "c") {
my $have_operation_types = (grep(/psa_\w+_operation_t/, @types)) ? 1 : 0;
print c_header();
print c_define_types_for_operation_types() if $have_operation_types;
for my $type (@types) {
next unless $type =~ /^psa_(\w+)_operation_t$/;
print define_operation_type_data_and_functions($1);
}
print c_define_begins();
for my $type (@types) {
if ($type eq "buffer") {
@ -82,6 +92,12 @@ if ($which eq "h") {
print define_needs($type);
print define_serialise($type);
print define_deserialise($type);
if ($type =~ /^psa_\w+_operation_t$/) {
print define_server_needs($type);
print define_server_serialise($type);
print define_server_deserialise($type);
}
}
}
@ -363,6 +379,25 @@ size_t psasim_serialise_${type_d}_needs($type value)
EOF
}
sub define_server_needs
{
my ($type) = @_;
my $type_d = $type;
$type_d =~ s/ /_/g;
return <<EOF;
size_t psasim_server_serialise_${type_d}_needs($type *operation)
{
(void) operation;
/* We will actually return a handle */
return sizeof(psasim_operation_t);
}
EOF
}
sub define_needs_isa
{
my ($type, $isa) = @_;
@ -407,6 +442,44 @@ int psasim_serialise_$type_d(uint8_t **pos,
EOF
}
sub define_server_serialise
{
my ($type) = @_;
my $t;
if ($type =~ /^psa_(\w+)_operation_t$/) {
$t = $1;
} else {
die("$0: define_server_serialise: $type: not supported\n");
}
my $type_d = $type;
$type_d =~ s/ /_/g;
return align_signature(<<EOF);
int psasim_server_serialise_$type_d(uint8_t **pos,
size_t *remaining,
$type *operation)
{
psasim_operation_t client_operation;
if (*remaining < sizeof(client_operation)) {
return 0;
}
ssize_t slot = operation - ${t}_operations;
client_operation.handle = ${t}_operation_handles[slot];
memcpy(*pos, &client_operation, sizeof(client_operation));
*pos += sizeof(client_operation);
return 1;
}
EOF
}
sub define_serialise_isa
{
my ($type, $isa) = @_;
@ -455,6 +528,54 @@ int psasim_deserialise_$type_d(uint8_t **pos,
EOF
}
sub define_server_deserialise
{
my ($type) = @_;
my $t;
if ($type =~ /^psa_(\w+)_operation_t$/) {
$t = $1;
} else {
die("$0: define_server_serialise: $type: not supported\n");
}
my $type_d = $type;
$type_d =~ s/ /_/g;
return align_signature(<<EOF);
int psasim_server_deserialise_$type_d(uint8_t **pos,
size_t *remaining,
$type **operation)
{
psasim_operation_t client_operation;
if (*remaining < sizeof(psasim_operation_t)) {
return 0;
}
memcpy(&client_operation, *pos, sizeof(psasim_operation_t));
*pos += sizeof(psasim_operation_t);
*remaining -= sizeof(psasim_operation_t);
ssize_t slot;
if (client_operation.handle == 0) { /* We need a new handle */
slot = allocate_${t}_operation_slot();
} else {
slot = find_${t}_slot_by_handle(client_operation.handle);
}
if (slot < 0) {
return 0;
}
*operation = &${t}_operations[slot];
return 1;
}
EOF
}
sub define_deserialise_isa
{
my ($type, $isa) = @_;
@ -639,6 +760,72 @@ sub c_header
* data types (e.g. int), types typedef'd to those, and even structures that
* don't contain pointers.
*/
EOF
}
sub c_define_types_for_operation_types
{
return <<'EOF';
/* include/psa/crypto_platform.h:typedef uint32_t mbedtls_psa_client_handle_t;
* but we don't get it on server builds, so redefine it here with a unique type name
*/
typedef uint32_t psasim_client_handle_t;
typedef struct psasim_operation_s {
psasim_client_handle_t handle;
} psasim_operation_t;
#define MAX_LIVE_HANDLES_PER_CLASS 100 /* this many slots */
EOF
}
sub define_operation_type_data_and_functions
{
my ($type) = @_; # e.g. 'hash' rather than 'psa_hash_operation_t'
return <<EOF;
static psa_${type}_operation_t ${type}_operations[MAX_LIVE_HANDLES_PER_CLASS];
static psasim_client_handle_t ${type}_operation_handles[MAX_LIVE_HANDLES_PER_CLASS];
static psasim_client_handle_t next_${type}_operation_handle = 1;
/* Get a free slot */
static ssize_t allocate_${type}_operation_slot(void)
{
psasim_client_handle_t handle = next_${type}_operation_handle++;
if (next_${type}_operation_handle == 0) { /* wrapped around */
fprintf(stderr, "MAX HASH HANDLES REACHED\\n");
exit(1);
}
for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) {
if (${type}_operation_handles[i] == 0) {
${type}_operation_handles[i] = handle;
return i;
}
}
return -1; /* all in use */
}
/* Find the slot given the handle */
static ssize_t find_${type}_slot_by_handle(psasim_client_handle_t handle)
{
for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) {
if (${type}_operation_handles[i] == handle) {
return i;
}
}
return -1; /* all in use */
}
EOF
}
sub c_define_begins
{
return <<'EOF';
size_t psasim_serialise_begin_needs(void)
{