87 lines
2.6 KiB
C
87 lines
2.6 KiB
C
|
|
#include "amduat/asl/ref_derive.h"
|
||
|
|
#include "amduat/enc/asl1_core_codec.h"
|
||
|
|
#include "amduat/hash/asl1.h"
|
||
|
|
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
static int test_ref_derive_roundtrip(void) {
|
||
|
|
static const uint8_t k_payload[] = {
|
||
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f
|
||
|
|
};
|
||
|
|
amduat_artifact_t artifact = amduat_artifact_with_type(
|
||
|
|
amduat_octets(k_payload, sizeof(k_payload)),
|
||
|
|
amduat_type_tag(0x12345678u));
|
||
|
|
amduat_reference_t ref = amduat_reference(0u, amduat_octets(NULL, 0u));
|
||
|
|
amduat_octets_t artifact_bytes = amduat_octets(NULL, 0u);
|
||
|
|
amduat_octets_t encoded = amduat_octets(NULL, 0u);
|
||
|
|
amduat_artifact_t decoded = amduat_artifact(amduat_octets(NULL, 0u));
|
||
|
|
const amduat_hash_asl1_desc_t *hash_desc;
|
||
|
|
uint8_t digest[64];
|
||
|
|
int exit_code = 1;
|
||
|
|
|
||
|
|
if (!amduat_asl_ref_derive(artifact,
|
||
|
|
AMDUAT_ENC_ASL1_CORE_V1,
|
||
|
|
AMDUAT_HASH_ASL1_ID_SHA256,
|
||
|
|
&ref,
|
||
|
|
&artifact_bytes)) {
|
||
|
|
fprintf(stderr, "ref_derive failed\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
|
||
|
|
hash_desc = amduat_hash_asl1_desc_lookup(ref.hash_id);
|
||
|
|
if (hash_desc == NULL || hash_desc->digest_len == 0) {
|
||
|
|
fprintf(stderr, "hash desc lookup failed\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
if (hash_desc->digest_len > sizeof(digest)) {
|
||
|
|
fprintf(stderr, "digest buffer too small\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
if (!amduat_hash_asl1_digest(ref.hash_id,
|
||
|
|
artifact_bytes,
|
||
|
|
digest,
|
||
|
|
hash_desc->digest_len)) {
|
||
|
|
fprintf(stderr, "hash digest failed\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
if (ref.digest.len != hash_desc->digest_len ||
|
||
|
|
memcmp(ref.digest.data, digest, hash_desc->digest_len) != 0) {
|
||
|
|
fprintf(stderr, "derived digest mismatch\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!amduat_enc_asl1_core_encode_artifact_v1(artifact, &encoded)) {
|
||
|
|
fprintf(stderr, "encode_artifact failed\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
if (artifact_bytes.len != encoded.len ||
|
||
|
|
(artifact_bytes.len != 0 &&
|
||
|
|
memcmp(artifact_bytes.data, encoded.data, encoded.len) != 0)) {
|
||
|
|
fprintf(stderr, "artifact bytes mismatch\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!amduat_enc_asl1_core_decode_artifact_v1(artifact_bytes, &decoded)) {
|
||
|
|
fprintf(stderr, "decode_artifact failed\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
if (!amduat_artifact_eq(artifact, decoded)) {
|
||
|
|
fprintf(stderr, "decoded artifact mismatch\n");
|
||
|
|
goto cleanup;
|
||
|
|
}
|
||
|
|
|
||
|
|
exit_code = 0;
|
||
|
|
|
||
|
|
cleanup:
|
||
|
|
amduat_reference_free(&ref);
|
||
|
|
amduat_octets_free(&artifact_bytes);
|
||
|
|
amduat_octets_free(&encoded);
|
||
|
|
amduat_artifact_free(&decoded);
|
||
|
|
return exit_code;
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(void) {
|
||
|
|
return test_ref_derive_roundtrip();
|
||
|
|
}
|