Added a central ASL reference-derivation API and wired the filesystem store (plus the PEL stub store) to use it, so all reference creation now goes through the canonical encode+hash path.

This commit is contained in:
Carl Niklas Rydberg 2025-12-21 22:22:17 +01:00
parent 71d2303c3c
commit 39712f7b70
5 changed files with 150 additions and 86 deletions

View file

@ -64,6 +64,7 @@ set(AMDUAT_ASL_SRCS
src/near_core/asl/artifact_io.c
src/near_core/asl/io.c
src/near_core/asl/parse.c
src/near_core/asl/ref_derive.c
src/near_core/asl/ref_io.c
src/near_core/asl/store.c
src/near_core/asl/ref_text.c

View file

@ -0,0 +1,23 @@
#ifndef AMDUAT_ASL_REF_DERIVE_H
#define AMDUAT_ASL_REF_DERIVE_H
#include "amduat/asl/core.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Caller owns any heap allocations returned in out_ref/out_artifact_bytes. */
bool amduat_asl_ref_derive(amduat_artifact_t artifact,
amduat_asl_encoding_profile_id_t profile_id,
amduat_hash_id_t hash_id,
amduat_reference_t *out_ref,
amduat_octets_t *out_artifact_bytes);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* AMDUAT_ASL_REF_DERIVE_H */

View file

@ -2,6 +2,7 @@
#include "asl_store_fs_layout.h"
#include "amduat/asl/core.h"
#include "amduat/asl/ref_derive.h"
#include "amduat/enc/asl1_core.h"
#include "amduat/enc/asl1_core_codec.h"
#include "amduat/hash/asl1.h"
@ -300,8 +301,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
amduat_reference_t *out_ref) {
amduat_asl_store_fs_t *fs;
const amduat_hash_asl1_desc_t *hash_desc;
amduat_reference_t derived_ref;
amduat_octets_t artifact_bytes;
uint8_t *digest;
bool ok;
char *objects_path;
char *profile_path;
@ -329,21 +330,15 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
return AMDUAT_ASL_STORE_ERR_UNSUPPORTED;
}
derived_ref = amduat_reference(0u, amduat_octets(NULL, 0u));
artifact_bytes = amduat_octets(NULL, 0u);
if (!amduat_enc_asl1_core_encode_artifact_v1(artifact, &artifact_bytes)) {
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
digest = (uint8_t *)malloc(hash_desc->digest_len);
if (digest == NULL) {
free((void *)artifact_bytes.data);
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
if (!amduat_hash_asl1_digest(hash_desc->hash_id, artifact_bytes, digest,
hash_desc->digest_len)) {
free((void *)artifact_bytes.data);
free(digest);
if (!amduat_asl_ref_derive(artifact,
fs->config.encoding_profile_id,
fs->config.hash_id,
&derived_ref,
&artifact_bytes)) {
amduat_octets_free(&artifact_bytes);
amduat_reference_free(&derived_ref);
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
@ -355,8 +350,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
object_path = NULL;
ok = amduat_asl_store_fs_layout_build_paths(fs->root_path,
fs->config,
digest,
hash_desc->digest_len,
derived_ref.digest.data,
derived_ref.digest.len,
&objects_path,
&profile_path,
&hash_path,
@ -364,8 +359,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
&level2_path,
&object_path);
if (!ok) {
free((void *)artifact_bytes.data);
free(digest);
amduat_octets_free(&artifact_bytes);
amduat_reference_free(&derived_ref);
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
@ -375,8 +370,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
!amduat_asl_store_fs_ensure_directory(hash_path) ||
!amduat_asl_store_fs_ensure_directory(level1_path) ||
!amduat_asl_store_fs_ensure_directory(level2_path)) {
free((void *)artifact_bytes.data);
free(digest);
amduat_octets_free(&artifact_bytes);
amduat_reference_free(&derived_ref);
free(objects_path);
free(profile_path);
free(hash_path);
@ -390,9 +385,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
artifact_bytes.data,
artifact_bytes.len);
if (cmp_err == AMDUAT_ASL_STORE_OK) {
out_ref->hash_id = fs->config.hash_id;
out_ref->digest = amduat_octets(digest, hash_desc->digest_len);
free((void *)artifact_bytes.data);
*out_ref = derived_ref;
amduat_octets_free(&artifact_bytes);
free(objects_path);
free(profile_path);
free(hash_path);
@ -402,8 +396,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
return AMDUAT_ASL_STORE_OK;
}
if (cmp_err == AMDUAT_ASL_STORE_ERR_INTEGRITY) {
free((void *)artifact_bytes.data);
free(digest);
amduat_octets_free(&artifact_bytes);
amduat_reference_free(&derived_ref);
free(objects_path);
free(profile_path);
free(hash_path);
@ -422,8 +416,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
artifact_bytes.data,
artifact_bytes.len);
if (cmp_err != AMDUAT_ASL_STORE_OK) {
free((void *)artifact_bytes.data);
free(digest);
amduat_octets_free(&artifact_bytes);
amduat_reference_free(&derived_ref);
free(objects_path);
free(profile_path);
free(hash_path);
@ -433,8 +427,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
} else if (write_status != AMDUAT_ASL_STORE_FS_WRITE_OK) {
free((void *)artifact_bytes.data);
free(digest);
amduat_octets_free(&artifact_bytes);
amduat_reference_free(&derived_ref);
free(objects_path);
free(profile_path);
free(hash_path);
@ -446,8 +440,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
if (!amduat_asl_store_fs_fsync_directory(level2_path)) {
unlink(object_path);
free((void *)artifact_bytes.data);
free(digest);
amduat_octets_free(&artifact_bytes);
amduat_reference_free(&derived_ref);
free(objects_path);
free(profile_path);
free(hash_path);
@ -458,8 +452,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
}
if (!amduat_asl_store_fs_fsync_directory(fs->root_path)) {
unlink(object_path);
free((void *)artifact_bytes.data);
free(digest);
amduat_octets_free(&artifact_bytes);
amduat_reference_free(&derived_ref);
free(objects_path);
free(profile_path);
free(hash_path);
@ -469,9 +463,8 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl(
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
out_ref->hash_id = fs->config.hash_id;
out_ref->digest = amduat_octets(digest, hash_desc->digest_len);
free((void *)artifact_bytes.data);
*out_ref = derived_ref;
amduat_octets_free(&artifact_bytes);
free(objects_path);
free(profile_path);
free(hash_path);

View file

@ -0,0 +1,74 @@
#include "amduat/asl/ref_derive.h"
#include "amduat/enc/asl1_core.h"
#include "amduat/enc/asl1_core_codec.h"
#include "amduat/hash/asl1.h"
#include <stdlib.h>
bool amduat_asl_ref_derive(amduat_artifact_t artifact,
amduat_asl_encoding_profile_id_t profile_id,
amduat_hash_id_t hash_id,
amduat_reference_t *out_ref,
amduat_octets_t *out_artifact_bytes) {
amduat_octets_t artifact_bytes;
const amduat_hash_asl1_desc_t *hash_desc;
uint8_t *digest;
if (out_ref == NULL) {
return false;
}
out_ref->hash_id = 0u;
out_ref->digest = amduat_octets(NULL, 0u);
if (out_artifact_bytes != NULL) {
out_artifact_bytes->data = NULL;
out_artifact_bytes->len = 0u;
}
if (artifact.bytes.len != 0u && artifact.bytes.data == NULL) {
return false;
}
artifact_bytes = amduat_octets(NULL, 0u);
switch (profile_id) {
case AMDUAT_ENC_ASL1_CORE_V1:
if (!amduat_enc_asl1_core_encode_artifact_v1(artifact,
&artifact_bytes)) {
return false;
}
break;
default:
return false;
}
hash_desc = amduat_hash_asl1_desc_lookup(hash_id);
if (hash_desc == NULL || hash_desc->digest_len == 0 ||
hash_desc->impl.digest == NULL) {
amduat_octets_free(&artifact_bytes);
return false;
}
digest = (uint8_t *)malloc(hash_desc->digest_len);
if (digest == NULL) {
amduat_octets_free(&artifact_bytes);
return false;
}
if (!amduat_hash_asl1_digest(hash_id,
artifact_bytes,
digest,
hash_desc->digest_len)) {
amduat_octets_free(&artifact_bytes);
free(digest);
return false;
}
if (out_artifact_bytes != NULL) {
*out_artifact_bytes = artifact_bytes;
} else {
amduat_octets_free(&artifact_bytes);
}
out_ref->hash_id = hash_id;
out_ref->digest = amduat_octets(digest, hash_desc->digest_len);
return true;
}

View file

@ -1,6 +1,6 @@
#include "amduat/asl/ref_derive.h"
#include "amduat/asl/store.h"
#include "amduat/enc/asl1_core.h"
#include "amduat/enc/asl1_core_codec.h"
#include "amduat/enc/pel1_result.h"
#include "amduat/enc/pel_program_dag.h"
#include "amduat/enc/pel_trace_dag.h"
@ -111,11 +111,9 @@ static amduat_asl_store_error_t stub_store_put(
amduat_artifact_t artifact,
amduat_reference_t *out_ref) {
stub_store_t *store;
amduat_octets_t encoded;
const amduat_hash_asl1_desc_t *hash_desc;
uint8_t *digest_store = NULL;
uint8_t *digest_out = NULL;
uint8_t *payload;
amduat_reference_t derived_ref;
amduat_reference_t stored_ref;
amduat_artifact_t stored_artifact;
size_t i;
@ -130,70 +128,47 @@ static amduat_asl_store_error_t stub_store_put(
if (store->config.encoding_profile_id != AMDUAT_ENC_ASL1_CORE_V1) {
return AMDUAT_ASL_STORE_ERR_UNSUPPORTED;
}
encoded = amduat_octets(NULL, 0);
if (!amduat_enc_asl1_core_encode_artifact_v1(artifact, &encoded)) {
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
hash_desc = amduat_hash_asl1_desc_lookup(store->config.hash_id);
if (hash_desc == NULL || hash_desc->digest_len == 0) {
free((void *)encoded.data);
return AMDUAT_ASL_STORE_ERR_UNSUPPORTED;
}
digest_store = (uint8_t *)malloc(hash_desc->digest_len);
digest_out = (uint8_t *)malloc(hash_desc->digest_len);
if (digest_store == NULL || digest_out == NULL) {
free((void *)encoded.data);
free(digest_store);
free(digest_out);
derived_ref = amduat_reference(0u, amduat_octets(NULL, 0u));
if (!amduat_asl_ref_derive(artifact,
store->config.encoding_profile_id,
store->config.hash_id,
&derived_ref,
NULL)) {
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
if (!amduat_hash_asl1_digest(store->config.hash_id, encoded, digest_store,
hash_desc->digest_len)) {
free((void *)encoded.data);
free(digest_store);
free(digest_out);
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
memcpy(digest_out, digest_store, hash_desc->digest_len);
free((void *)encoded.data);
stored_ref.hash_id = store->config.hash_id;
stored_ref.digest = amduat_octets(digest_store, hash_desc->digest_len);
out_ref->hash_id = store->config.hash_id;
out_ref->digest = amduat_octets(digest_out, hash_desc->digest_len);
*out_ref = derived_ref;
for (i = 0; i < store->len; ++i) {
stub_store_entry_t *entry = &store->entries[i];
if (amduat_reference_eq(entry->ref, stored_ref)) {
if (amduat_reference_eq(entry->ref, *out_ref)) {
if (!artifact_eq(&entry->artifact, &artifact)) {
free(digest_store);
free(digest_out);
out_ref->hash_id = 0;
out_ref->digest = amduat_octets(NULL, 0);
amduat_reference_free(out_ref);
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
free(digest_store);
return AMDUAT_ASL_STORE_OK;
}
}
if (!amduat_reference_clone(*out_ref, &stored_ref)) {
amduat_reference_free(out_ref);
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
payload = NULL;
if (artifact.bytes.len != 0) {
if (artifact.bytes.data == NULL) {
free(digest_store);
free(digest_out);
out_ref->hash_id = 0;
out_ref->digest = amduat_octets(NULL, 0);
amduat_reference_free(out_ref);
amduat_reference_free(&stored_ref);
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
payload = (uint8_t *)malloc(artifact.bytes.len);
if (payload == NULL) {
free(digest_store);
free(digest_out);
out_ref->hash_id = 0;
out_ref->digest = amduat_octets(NULL, 0);
amduat_reference_free(out_ref);
amduat_reference_free(&stored_ref);
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}
memcpy(payload, artifact.bytes.data, artifact.bytes.len);
@ -204,10 +179,8 @@ static amduat_asl_store_error_t stub_store_put(
if (!stub_store_add_entry(store, stored_ref, stored_artifact)) {
free(payload);
free(digest_store);
free(digest_out);
out_ref->hash_id = 0;
out_ref->digest = amduat_octets(NULL, 0);
amduat_reference_free(out_ref);
amduat_reference_free(&stored_ref);
return AMDUAT_ASL_STORE_ERR_INTEGRITY;
}