127 lines
4.5 KiB
C
127 lines
4.5 KiB
C
#include "amduat/enc/pel1_result.h"
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static const uint8_t k_expected_result_bytes[] = {
|
|
0x00, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,
|
|
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
|
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
|
0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x01, 0x02, 0x02,
|
|
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
|
|
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
|
|
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
|
|
0x00, 0x22, 0x00, 0x01, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
|
|
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
|
|
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
|
|
0x00, 0x00, 0x00, 0x22, 0x00, 0x01, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
|
0x11, 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x01,
|
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x01, 0x00,
|
|
0x00, 0x00, 0x22, 0x00, 0x01, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
|
|
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
|
|
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
|
|
0x30, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x01, 0x01, 0x01,
|
|
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
|
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
|
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00,
|
|
};
|
|
|
|
static void fill_digest(uint8_t *out, uint8_t value) {
|
|
memset(out, value, 32);
|
|
}
|
|
|
|
static amduat_reference_t make_ref(uint8_t value, uint8_t *storage) {
|
|
fill_digest(storage, value);
|
|
return amduat_reference(0x0001, amduat_octets(storage, 32));
|
|
}
|
|
|
|
static bool bytes_equal(amduat_octets_t bytes,
|
|
const uint8_t *expected,
|
|
size_t expected_len) {
|
|
if (bytes.len != expected_len) {
|
|
return false;
|
|
}
|
|
if (bytes.len == 0) {
|
|
return true;
|
|
}
|
|
return memcmp(bytes.data, expected, expected_len) == 0;
|
|
}
|
|
|
|
static int test_result_encoding(void) {
|
|
amduat_pel_surface_execution_result_t result;
|
|
amduat_reference_t input_refs[2];
|
|
amduat_reference_t output_refs[1];
|
|
amduat_octets_t encoded;
|
|
amduat_pel_surface_execution_result_t decoded;
|
|
uint8_t s[32], p[32], i0[32], i1[32], o0[32], t[32];
|
|
int exit_code = 1;
|
|
|
|
memset(&result, 0, sizeof(result));
|
|
result.pel1_version = 1;
|
|
result.scheme_ref = make_ref(0x01, s);
|
|
result.program_ref = make_ref(0x02, p);
|
|
|
|
input_refs[0] = make_ref(0x10, i0);
|
|
input_refs[1] = make_ref(0x11, i1);
|
|
result.input_refs = input_refs;
|
|
result.input_refs_len = 2;
|
|
|
|
output_refs[0] = make_ref(0x20, o0);
|
|
result.output_refs = output_refs;
|
|
result.output_refs_len = 1;
|
|
|
|
result.has_params_ref = false;
|
|
result.has_store_failure = false;
|
|
result.has_trace_ref = true;
|
|
result.trace_ref = make_ref(0x30, t);
|
|
|
|
result.core_result.pel1_version = 1;
|
|
result.core_result.status = AMDUAT_PEL_EXEC_STATUS_OK;
|
|
result.core_result.scheme_ref = result.scheme_ref;
|
|
result.core_result.summary.kind = AMDUAT_PEL_EXEC_ERROR_NONE;
|
|
result.core_result.summary.status_code = 0;
|
|
result.core_result.diagnostics = NULL;
|
|
result.core_result.diagnostics_len = 0;
|
|
|
|
if (!amduat_enc_pel1_result_encode_v1(&result, &encoded)) {
|
|
fprintf(stderr, "encode failed\n");
|
|
return exit_code;
|
|
}
|
|
|
|
if (!bytes_equal(encoded, k_expected_result_bytes,
|
|
sizeof(k_expected_result_bytes))) {
|
|
fprintf(stderr, "encoded bytes mismatch\n");
|
|
goto cleanup;
|
|
}
|
|
|
|
if (!amduat_enc_pel1_result_decode_v1(encoded, &decoded)) {
|
|
fprintf(stderr, "decode failed\n");
|
|
goto cleanup;
|
|
}
|
|
|
|
if (decoded.input_refs_len != 2 || decoded.output_refs_len != 1) {
|
|
fprintf(stderr, "decoded lengths mismatch\n");
|
|
goto cleanup_decoded;
|
|
}
|
|
|
|
exit_code = 0;
|
|
|
|
cleanup_decoded:
|
|
amduat_enc_pel1_result_free(&decoded);
|
|
cleanup:
|
|
free((void *)encoded.data);
|
|
return exit_code;
|
|
}
|
|
|
|
int main(void) {
|
|
return test_result_encoding();
|
|
}
|