/** * libasos.c * مكتبة المستخدم ASOS - التنفيذ * ASOS Userspace Library - Implementation * * Userspace wrapper around ASOS syscall 450. * Handles intent construction, trust nonce generation, * and result parsing. * * Author: Ahmad Ali Parr * License: Sovereign Source */ #include "libasos.h" #include "../kernel-module/intent_schema.h" #include #include #include #include #include #include /* Syscall number (custom) */ #define __NR_submit_system_intent 450 /* ═══════════════════════════════════════════════════════════════ * Intent Handle | معالج النية * ═══════════════════════════════════════════════════════════════ */ struct asos_intent { struct SystemIntent raw; /* Kernel structure */ void *payload_copy; /* Owned payload buffer */ }; struct asos_result { struct IntentResult raw; /* Kernel result structure */ }; /* ═══════════════════════════════════════════════════════════════ * Trust Nonce Generation | توليد nonce الثقة * ═══════════════════════════════════════════════════════════════ */ /** * generate_trust_nonce - Generate Blake3 trust nonce * @intent: Intent structure (partially filled) * @nonce_out: Output buffer (32 bytes) * * For Phase 4, this is a simplified hash. Phase 5 will use * proper Blake3 library (libblake3). */ static void generate_trust_nonce(const struct SystemIntent *intent, uint8_t *nonce_out) { /* Simple hash for Phase 4: XOR of intent fields * Phase 5 will replace with real Blake3 implementation */ uint32_t hash = 0; hash ^= intent->intent_type; hash ^= intent->priority; hash ^= (uint32_t)(intent->timestamp_ns & 0xFFFFFFFF); hash ^= (uint32_t)(intent->timestamp_ns >> 32); /* Hash goal_schema bytes */ for (size_t i = 0; i < strlen(intent->goal_schema); i++) { hash ^= intent->goal_schema[i] << ((i % 4) * 8); } /* Fill nonce buffer with hash pattern */ for (int i = 0; i < 32; i++) { nonce_out[i] = (hash >> ((i % 4) * 8)) & 0xFF; } } /* ═══════════════════════════════════════════════════════════════ * Intent Builder | بناء النوايا * ═══════════════════════════════════════════════════════════════ */ asos_intent_t *asos_intent_new(asos_intent_type_t type, asos_priority_t priority) { asos_intent_t *intent = calloc(1, sizeof(asos_intent_t)); if (!intent) return NULL; intent->raw.intent_type = (uint32_t)type; intent->raw.priority = (uint32_t)priority; intent->raw.flags = 0; intent->raw.payload_len = 0; intent->raw.payload_data = NULL; intent->payload_copy = NULL; /* Timestamp (monotonic clock) */ struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); intent->raw.timestamp_ns = (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec; return intent; } int asos_intent_set_goal(asos_intent_t *intent, const char *goal) { if (!intent || !goal) return -1; size_t len = strlen(goal); if (len >= MAX_GOAL_SCHEMA) return -1; strncpy(intent->raw.goal_schema, goal, MAX_GOAL_SCHEMA - 1); intent->raw.goal_schema[MAX_GOAL_SCHEMA - 1] = '\0'; return 0; } int asos_intent_set_payload(asos_intent_t *intent, const void *data, size_t len) { if (!intent || !data || len > MAX_INTENT_PAYLOAD) return -1; /* Allocate payload copy */ if (intent->payload_copy) free(intent->payload_copy); intent->payload_copy = malloc(len); if (!intent->payload_copy) return -1; memcpy(intent->payload_copy, data, len); intent->raw.payload_data = intent->payload_copy; intent->raw.payload_len = (uint32_t)len; return 0; } void asos_intent_free(asos_intent_t *intent) { if (!intent) return; if (intent->payload_copy) free(intent->payload_copy); free(intent); } /* ═══════════════════════════════════════════════════════════════ * Intent Submission | تقديم النوايا * ═══════════════════════════════════════════════════════════════ */ asos_result_t *asos_submit_intent(asos_intent_t *intent) { if (!intent) return NULL; /* Generate trust nonce */ generate_trust_nonce(&intent->raw, intent->raw.trust_nonce); /* Invoke syscall 450 */ long ret = syscall(__NR_submit_system_intent, &intent->raw); if (ret < 0) { fprintf(stderr, "asos_submit_intent: syscall failed, errno=%ld\n", ret); return NULL; } /* TODO Phase 4: Wait for result from ring buffer * * For now, create stub result (kernel doesn't implement result yet) */ asos_result_t *result = calloc(1, sizeof(asos_result_t)); if (!result) return NULL; result->raw.status = 0; /* Success stub */ result->raw.kar_action = intent->raw.intent_type; result->raw.resource_handle = 0; result->raw.bytes_transferred = intent->raw.payload_len; result->raw.execution_time_ns = 1000000; /* 1ms stub */ result->raw.kar_agent_id = 0; /* Stub WORM seal */ memcpy(result->raw.worm_seal, intent->raw.trust_nonce, TRUST_NONCE_SIZE); return result; } int asos_submit_intent_async(asos_intent_t *intent, asos_callback_t callback, void *user_data) { /* TODO Phase 4: Implement async submission with pthread */ (void)intent; (void)callback; (void)user_data; return -1; /* Not implemented yet */ } /* ═══════════════════════════════════════════════════════════════ * Result Inspection | فحص النتائج * ═══════════════════════════════════════════════════════════════ */ int asos_result_status(const asos_result_t *result) { if (!result) return ASOS_EINVALID_INTENT; return result->raw.status; } size_t asos_result_bytes_transferred(const asos_result_t *result) { if (!result) return 0; return result->raw.bytes_transferred; } uint64_t asos_result_execution_time_ns(const asos_result_t *result) { if (!result) return 0; return result->raw.execution_time_ns; } int asos_result_worm_seal(const asos_result_t *result, uint8_t *seal_out) { if (!result || !seal_out) return -1; memcpy(seal_out, result->raw.worm_seal, TRUST_NONCE_SIZE); return 0; } void asos_result_free(asos_result_t *result) { if (result) free(result); } /* ═══════════════════════════════════════════════════════════════ * Error Strings | سلاسل الأخطاء * ═══════════════════════════════════════════════════════════════ */ const char *asos_strerror(int errnum) { switch (errnum) { case ASOS_EINVALID_INTENT: return "Invalid intent structure"; case ASOS_ETRUST_NONCE_FAIL: return "Trust nonce validation failed"; case ASOS_EKAR_POLICY_REJECT: return "Policy engine rejected intent"; case ASOS_EKAR_NO_AGENT: return "No suitable agent available"; case ASOS_EKAR_TIMEOUT: return "Intent processing timeout"; case ASOS_EWORM_SEAL_FAIL: return "Failed to generate WORM seal"; default: return "Unknown error"; } } /* ═══════════════════════════════════════════════════════════════ * Library Init/Cleanup | تهيئة/تنظيف المكتبة * ═══════════════════════════════════════════════════════════════ */ int asos_init(void) { /* TODO: Initialize any global state (e.g., ring buffer mmap) */ return 0; } void asos_cleanup(void) { /* TODO: Cleanup global state */ }