124 lines
3.2 KiB
Nim
124 lines
3.2 KiB
Nim
# MARKUS MAIWALD (ARCHITECT) | VOXIS FORGE (AI)
|
|
# RUMPK CORE // NPL FORMAT
|
|
# The Contract of Execution.
|
|
|
|
{.push stackTrace: off, lineTrace: off.}
|
|
|
|
import crypto
|
|
|
|
# =========================================================
|
|
# Constants
|
|
# =========================================================
|
|
|
|
const NPL_MAGIC*: array[4, uint8] = [0x7F'u8, 0x4E'u8, 0x50'u8, 0x4C'u8]
|
|
const NPL_VERSION* = 1'u8
|
|
const ARCH_ARM64* = 0xAA'u8
|
|
const ARCH_X86_64* = 0xEE'u8
|
|
const ARCH_RISCV64* = 0x55'u8
|
|
const NPL_HEADER_SIZE* = 128
|
|
|
|
# =========================================================
|
|
# Types
|
|
# =========================================================
|
|
|
|
type
|
|
NPLHeader* {.packed.} = object
|
|
magic*: array[4, uint8]
|
|
version*: uint8
|
|
arch*: uint8
|
|
flags*: uint16
|
|
reserved0*: array[8, uint8] # Alignment padding for signature
|
|
signature*: array[64, uint8]
|
|
body_size*: uint64
|
|
reserved1*: array[40, uint8] # Remaining padding
|
|
|
|
PayloadEntry* = proc() {.cdecl.}
|
|
|
|
# Global last error (avoids struct return issues)
|
|
var nplLastError*: cstring = nil
|
|
|
|
# =========================================================
|
|
# Architecture Detection
|
|
# =========================================================
|
|
|
|
proc currentArch*(): uint8 =
|
|
when defined(arm64) or defined(aarch64):
|
|
return ARCH_ARM64
|
|
elif defined(amd64) or defined(x86_64):
|
|
return ARCH_X86_64
|
|
elif defined(riscv64):
|
|
return ARCH_RISCV64
|
|
else:
|
|
return 0x00'u8
|
|
|
|
# =========================================================
|
|
# NPL Loader (returns nil on failure, sets nplLastError)
|
|
# =========================================================
|
|
|
|
proc loadNpl*(rawPtr: pointer, maxLen: uint64): PayloadEntry =
|
|
nplLastError = nil
|
|
|
|
if rawPtr == nil:
|
|
nplLastError = "null pointer"
|
|
return nil
|
|
|
|
let header = cast[ptr NPLHeader](rawPtr)
|
|
|
|
# Check Magic
|
|
if header.magic[0] != NPL_MAGIC[0] or
|
|
header.magic[1] != NPL_MAGIC[1] or
|
|
header.magic[2] != NPL_MAGIC[2] or
|
|
header.magic[3] != NPL_MAGIC[3]:
|
|
nplLastError = "bad magic"
|
|
return nil
|
|
|
|
# Check Version
|
|
if header.version != NPL_VERSION:
|
|
nplLastError = "bad version"
|
|
return nil
|
|
|
|
# Check Architecture
|
|
if header.arch != currentArch():
|
|
nplLastError = "bad arch"
|
|
return nil
|
|
|
|
# Check Bounds
|
|
let totalSize = NPL_HEADER_SIZE.uint64 + header.body_size
|
|
if totalSize > maxLen:
|
|
nplLastError = "truncated"
|
|
return nil
|
|
|
|
# Get body pointer
|
|
let bodyPtr = cast[pointer](cast[uint64](rawPtr) + NPL_HEADER_SIZE.uint64)
|
|
|
|
# Verify Signature (REAL CRYPTO)
|
|
if not verify_npl_signature(header.signature, bodyPtr, header.body_size):
|
|
nplLastError = "signature fail"
|
|
return nil
|
|
|
|
# Cast body to function pointer
|
|
return cast[PayloadEntry](bodyPtr)
|
|
|
|
# =========================================================
|
|
# Helper: Build Test Payload
|
|
# =========================================================
|
|
|
|
proc buildTestPayload*(buffer: ptr array[256, uint8], arch: uint8,
|
|
codeBytes: openArray[uint8]) =
|
|
# Clear buffer
|
|
for i in 0..<256:
|
|
buffer[i] = 0
|
|
|
|
let header = cast[ptr NPLHeader](buffer)
|
|
header.magic = NPL_MAGIC
|
|
header.version = NPL_VERSION
|
|
header.arch = arch
|
|
header.flags = 1
|
|
header.body_size = codeBytes.len.uint64
|
|
|
|
# Copy code to body
|
|
for i, b in codeBytes:
|
|
buffer[NPL_HEADER_SIZE + i] = b
|
|
|
|
{.pop.}
|