Files
mesh-client/entity_native.zi

96 lines
3.3 KiB
Ziran

// SIMD-accelerated native entity chunk sealing and opening, backed by
// crypto/native (libsodium). Produces byte-for-byte identical output to
// mesh_client/entity's SealEntityChunk / OpenEntityChunk at ~2.5x the speed
// on 8 MiB chunks. Link libsodium when importing this module.
#scope_module
Entity :: #import "mesh_client/entity";
Native :: #import "crypto/native";
CryptoSha256 :: #import "crypto/sha256";
CryptoWipe :: #import "crypto/wipe";
#scope_file
Decimal :: (index: s64, output: []u8) -> string {
length: s64 = 0
value := index
while true {
output[length] = cast(u8)(48 + value % 10)
length += 1
value /= 10
if value == 0 { break }
}
left: s64 = 0
right := length - 1
while left < right {
swap := output[left]
output[left] = output[right]
output[right] = swap
left += 1
right -= 1
}
return TextView(output[0:length])
}
BuildChunkAAD :: (kind: string, index: s64, output: []u8) -> s64 {
if !Entity.EntityKindValid(kind) || index < 0 || index >= Entity.ENTITY_CHUNK_MAX { return -1 }
prefix := "daochi-entity-v1/blob/"
digits: [8]u8
number := Decimal(index, digits[:])
if prefix.count + kind.count + 1 + number.count > output.count { return -1 }
at: s64 = 0
for b: prefix { output[at] = b; at += 1 }
for b: kind { output[at] = b; at += 1 }
output[at] = 47
at += 1
for b: number { output[at] = b; at += 1 }
return at
}
#scope_export
// SIMD-accelerated version of SealEntityChunk. Verifies SHA-256(plain) == hash
// on single-chunk files so a caller bug can never trigger zero-nonce reuse.
SealEntityChunkNative :: (private_key: []u8, kind: string, hash: string, index: s64,
plain: []u8, output: []u8) -> s64 {
if plain.count < 1 || plain.count > Entity.ENTITY_CHUNK_BYTES || output.count < plain.count + 16 {
return -1
}
if index == 0 && plain.count < Entity.ENTITY_CHUNK_BYTES {
digest: [32]u8
actual_hex: [65]u8
if !Native.Sha256(plain, digest[:]) ||
CryptoSha256.BytesToHex(*digest[0], 32, *actual_hex[0], 65) == 0 ||
TextView(actual_hex[0:64]) != hash {
Native.Clear(output)
return -1
}
}
aad: [96]u8
aad_length := BuildChunkAAD(kind, index, aad[:])
key: [Entity.ENTITY_KEY_BYTES]u8
defer CryptoWipe.SecureWipe(key[:])
nonce: [12]u8
if aad_length < 0 || !Entity.EntityChunkKey(private_key, kind, hash, index, key[:]) {
Native.Clear(output)
return -1
}
return Native.ChaCha20Poly1305Seal(plain, aad[0:aad_length], key[:], nonce[:], output)
}
// SIMD-accelerated version of OpenEntityChunk.
OpenEntityChunkNative :: (private_key: []u8, kind: string, hash: string, index: s64,
sealed: []u8, output: []u8) -> s64 {
if sealed.count <= 16 || sealed.count > Entity.ENTITY_CHUNK_BYTES + 16 ||
output.count < sealed.count - 16 {
return -1
}
aad: [96]u8
aad_length := BuildChunkAAD(kind, index, aad[:])
key: [Entity.ENTITY_KEY_BYTES]u8
defer CryptoWipe.SecureWipe(key[:])
nonce: [12]u8
if aad_length < 0 || !Entity.EntityChunkKey(private_key, kind, hash, index, key[:]) {
Native.Clear(output)
return -1
}
return Native.ChaCha20Poly1305Open(sealed, aad[0:aad_length], key[:], nonce[:], output)
}