121 lines
6.0 KiB
Ziran
121 lines
6.0 KiB
Ziran
// Native provider for existing Mesh client content encryption. The account,
|
|
// content key, nonce and authenticated context are supplied by the caller;
|
|
// this module never creates identities, persists keys or sends requests.
|
|
// Libsodium supplies the same XChaCha20-Poly1305 bytes used by existing blobs.
|
|
#scope_module
|
|
sodium :: #system_library "sodium";
|
|
InitializeRaw :: () -> s32 #foreign sodium "sodium_init";
|
|
RandomRaw :: (bytes: *void, count: usize) #foreign sodium "randombytes_buf";
|
|
ClearRaw :: (bytes: *void, count: usize) #foreign sodium "sodium_memzero";
|
|
DigestRaw :: (output: *u8, input: *u8, count: u64) -> s32 #foreign sodium "crypto_hash_sha256";
|
|
HmacRaw :: (output: *u8, input: *u8, count: u64, key: *u8) -> s32 #foreign sodium "crypto_auth_hmacsha256";
|
|
SealRaw :: (output: *u8, size: *u64, input: *u8, count: u64, aad: *u8, aad_count: u64,
|
|
secret: *u8, nonce: *u8, key: *u8) -> s32 #foreign sodium "crypto_aead_xchacha20poly1305_ietf_encrypt";
|
|
OpenRaw :: (output: *u8, size: *u64, secret: *u8, input: *u8, count: u64, aad: *u8,
|
|
aad_count: u64, nonce: *u8, key: *u8) -> s32 #foreign sodium "crypto_aead_xchacha20poly1305_ietf_decrypt";
|
|
StateBytesRaw :: () -> usize #foreign sodium "crypto_hash_sha256_statebytes";
|
|
DigestStartRaw :: (state: *void) -> s32 #foreign sodium "crypto_hash_sha256_init";
|
|
DigestUpdateRaw :: (state: *void, input: *u8, count: u64) -> s32 #foreign sodium "crypto_hash_sha256_update";
|
|
DigestFinishRaw :: (state: *void, output: *u8) -> s32 #foreign sodium "crypto_hash_sha256_final";
|
|
DeriveRaw :: (output: *u8, count: u64, password: *u8, password_count: u64,
|
|
salt: *u8, operations: u64, memory: usize, algorithm: s32) -> s32 #foreign sodium "crypto_pwhash";
|
|
DeviceKeysRaw :: (public_key: *u8, private_key: *u8, seed: *u8) -> s32 #foreign sodium "crypto_sign_seed_keypair";
|
|
SignRaw :: (output: *u8, size: *u64, input: *u8, count: u64, key: *u8) -> s32 #foreign sodium "crypto_sign_detached";
|
|
|
|
Start :: (bytes: []u8) -> *u8 {
|
|
return ifx bytes.count > 0 then *bytes[0] else null
|
|
}
|
|
Fits :: (bytes: []u8) -> bool {
|
|
return bytes.count >= 0 && (size_of(usize) >= 8 || bytes.count <= 4294967295)
|
|
}
|
|
Valid :: (input: []u8, aad: []u8, key: []u8, nonce: []u8) -> bool {
|
|
return Fits(input) && Fits(aad) && key.count == 32 && nonce.count == 24 && InitializeRaw() >= 0
|
|
}
|
|
|
|
#scope_export
|
|
Initialize :: () -> bool { return InitializeRaw() >= 0 }
|
|
Clear :: (bytes: []u8) {
|
|
if Fits(bytes) && bytes.count > 0 {
|
|
ClearRaw(cast(*void)*bytes[0], cast(usize)bytes.count)
|
|
}
|
|
}
|
|
RandomBytes :: (bytes: []u8) -> bool {
|
|
if !Fits(bytes) || InitializeRaw() < 0 { return false }
|
|
if bytes.count > 0 { RandomRaw(cast(*void)*bytes[0], cast(usize)bytes.count) }
|
|
return true
|
|
}
|
|
Digest :: (input: []u8, output: []u8) -> bool {
|
|
return Fits(input) && output.count == 32 && InitializeRaw() >= 0 &&
|
|
DigestRaw(*output[0], Start(input), cast(u64)input.count) == 0
|
|
}
|
|
HmacSha256 :: (input: []u8, key: []u8, output: []u8) -> bool {
|
|
return Fits(input) && key.count == 32 && output.count == 32 && InitializeRaw() >= 0 &&
|
|
HmacRaw(*output[0], Start(input), cast(u64)input.count, *key[0]) == 0
|
|
}
|
|
Seal :: (input: []u8, aad: []u8, key: []u8, nonce: []u8, output: []u8) -> s64 {
|
|
if !Valid(input, aad, key, nonce) || !Fits(output) || output.count < 16 || input.count > output.count - 16 {
|
|
return -1
|
|
}
|
|
size: u64
|
|
if SealRaw(*output[0], *size, Start(input), cast(u64)input.count, Start(aad), cast(u64)aad.count,
|
|
null, *nonce[0], *key[0]) != 0 || size != cast(u64)(input.count + 16) {
|
|
Clear(output)
|
|
return -1
|
|
}
|
|
return cast(s64)size
|
|
}
|
|
Open :: (sealed: []u8, aad: []u8, key: []u8, nonce: []u8, output: []u8) -> s64 {
|
|
if !Valid(sealed, aad, key, nonce) || sealed.count < 16 || !Fits(output) || output.count < sealed.count - 16 {
|
|
Clear(output)
|
|
return -1
|
|
}
|
|
size: u64
|
|
if OpenRaw(Start(output), *size, null, *sealed[0], cast(u64)sealed.count, Start(aad), cast(u64)aad.count,
|
|
*nonce[0], *key[0]) != 0 || size != cast(u64)(sealed.count - 16) {
|
|
Clear(output)
|
|
return -1
|
|
}
|
|
return cast(s64)size
|
|
}
|
|
DigestStateBytes :: () -> usize { return StateBytesRaw() }
|
|
DigestStart :: (state: []u8) -> bool {
|
|
return Fits(state) && state.count >= cast(s64)StateBytesRaw() && InitializeRaw() >= 0 &&
|
|
DigestStartRaw(cast(*void)*state[0]) == 0
|
|
}
|
|
DigestUpdate :: (state: []u8, input: []u8) -> bool {
|
|
return Fits(state) && state.count >= cast(s64)StateBytesRaw() && Fits(input) &&
|
|
DigestUpdateRaw(cast(*void)*state[0], Start(input), cast(u64)input.count) == 0
|
|
}
|
|
DigestFinish :: (state: []u8, output: []u8) -> bool {
|
|
return Fits(state) && state.count >= cast(s64)StateBytesRaw() && output.count == 32 &&
|
|
DigestFinishRaw(cast(*void)*state[0], *output[0]) == 0
|
|
}
|
|
// Compatibility KDF for existing Argon2id-protected exports. The caller owns
|
|
// the container format and supplies its fixed, trusted work factors.
|
|
DerivePassword :: (password: []u8, salt: []u8, output: []u8, operations: u64, memory: usize) -> bool {
|
|
if !Fits(password) || salt.count != 16 || output.count != 32 || operations < 1 || memory < 8192 || InitializeRaw() < 0 {
|
|
Clear(output)
|
|
return false
|
|
}
|
|
if DeriveRaw(*output[0], 32, Start(password), cast(u64)password.count, *salt[0], operations, memory, 2) != 0 {
|
|
Clear(output)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
DevicePublic :: (seed: []u8, output: []u8) -> bool {
|
|
if seed.count != 32 || output.count != 32 || !Initialize() { return false }
|
|
private_key: [64]u8
|
|
defer { Clear(private_key[:]) }
|
|
return DeviceKeysRaw(*output[0], *private_key[0], *seed[0]) == 0
|
|
}
|
|
SignDevice :: (seed: []u8, input: []u8, output: []u8) -> bool {
|
|
if seed.count != 32 || output.count != 64 || !Fits(input) || !Initialize() { return false }
|
|
private_key: [64]u8
|
|
public_key: [32]u8
|
|
defer { Clear(private_key[:]) }
|
|
size: u64
|
|
return DeviceKeysRaw(*public_key[0], *private_key[0], *seed[0]) == 0 &&
|
|
SignRaw(*output[0], *size, Start(input), cast(u64)input.count, *private_key[0]) == 0 && size == 64
|
|
}
|