feat: add pure-MoonBit SHA-512, Ed25519, AES-256-CBC, and signed RouterInfo/LeaseSet2
This commit is contained in:
1 parent
c95e9cfc21
commit
dff69dfac6
9 files changed
+1330
-4
No files matched your search
@@ -107,3 +107,96 @@ test "LeaseSet2 encode and decode round-trip" {
|
||||
let decoded = @common.LeaseSet2::decode(@encoding.Reader::new(w.to_bytes()))
|
||||
assert_eq(decoded, ls2)
|
||||
}
|
||||
|
||||
///|
|
||||
test "RouterInfo Ed25519 sign, encode/decode round-trip, and verify" {
|
||||
let ed_seed = @encoding.hex_decode(
|
||||
"9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60",
|
||||
)
|
||||
let ed_pub = @crypto.ed25519_public_key(ed_seed)
|
||||
let x_priv = @encoding.hex_decode(
|
||||
"77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a",
|
||||
)
|
||||
let x_pub = @crypto.x25519_public_key(x_priv)
|
||||
let ri_id = @common.RouterIdentity::new_x25519_ed25519(x_pub, ed_pub)
|
||||
let addr_opts = @common.Mapping::from_entries([
|
||||
("host", "127.0.0.1"),
|
||||
("port", "14567"),
|
||||
("v", "2"),
|
||||
])
|
||||
let ri : @common.RouterInfo = {
|
||||
identity: ri_id,
|
||||
published: 1700000000000UL,
|
||||
addresses: [@common.RouterAddress::new(b'\x03', "NTCP2", addr_opts)],
|
||||
options: @common.Mapping::from_entries([
|
||||
("caps", "LR"),
|
||||
("netId", "2"),
|
||||
("router.version", "0.9.64"),
|
||||
]),
|
||||
signature: b"",
|
||||
}
|
||||
let signed = ri.sign_ed25519(ed_seed)
|
||||
assert_eq(signed.signature.length(), 64)
|
||||
assert_eq(signed.verify_signature(), true)
|
||||
|
||||
// Wire round-trip preserves valid signature
|
||||
let raw = signed.to_bytes()
|
||||
let decoded = @common.RouterInfo::decode(@encoding.Reader::new(raw))
|
||||
assert_eq(decoded, signed)
|
||||
assert_eq(decoded.verify_signature(), true)
|
||||
|
||||
// Tampering with published timestamp invalidates signature
|
||||
let tampered : @common.RouterInfo = {
|
||||
identity: decoded.identity,
|
||||
published: decoded.published + 1UL,
|
||||
addresses: decoded.addresses,
|
||||
options: decoded.options,
|
||||
signature: decoded.signature,
|
||||
}
|
||||
assert_eq(tampered.verify_signature(), false)
|
||||
}
|
||||
|
||||
///|
|
||||
test "LeaseSet2 Ed25519 sign, encode/decode round-trip, and verify" {
|
||||
let ed_seed = @encoding.hex_decode(
|
||||
"4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb",
|
||||
)
|
||||
let ed_pub = @crypto.ed25519_public_key(ed_seed)
|
||||
let x_priv = @encoding.hex_decode(
|
||||
"5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb",
|
||||
)
|
||||
let x_pub = @crypto.x25519_public_key(x_priv)
|
||||
let dest = @common.Destination::new_x25519_ed25519(x_pub, ed_pub)
|
||||
let gw = @common.Hash::sha256(b"gateway-router")
|
||||
let ls2 : @common.LeaseSet2 = {
|
||||
destination: dest,
|
||||
published_sec: 1700000000U,
|
||||
expires_offset: 600U,
|
||||
flags: 0U,
|
||||
options: @common.Mapping::new(),
|
||||
encryption_keys: [{ key_type: X25519, key_data: x_pub, }],
|
||||
leases: [{ tunnel_gw: gw, tunnel_id: 777U, end_date_sec: 1700000600U, }],
|
||||
signature: b"",
|
||||
}
|
||||
let signed = ls2.sign_ed25519(ed_seed)
|
||||
assert_eq(signed.signature.length(), 64)
|
||||
assert_eq(signed.verify_signature(), true)
|
||||
assert_eq(signed.signing_bytes()[0], b'\x03')
|
||||
|
||||
let raw = signed.to_bytes()
|
||||
let decoded = @common.LeaseSet2::decode(@encoding.Reader::new(raw))
|
||||
assert_eq(decoded, signed)
|
||||
assert_eq(decoded.verify_signature(), true)
|
||||
|
||||
let tampered : @common.LeaseSet2 = {
|
||||
destination: decoded.destination,
|
||||
published_sec: decoded.published_sec + 1U,
|
||||
expires_offset: decoded.expires_offset,
|
||||
flags: decoded.flags,
|
||||
options: decoded.options,
|
||||
encryption_keys: decoded.encryption_keys,
|
||||
leases: decoded.leases,
|
||||
signature: decoded.signature,
|
||||
}
|
||||
assert_eq(tampered.verify_signature(), false)
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
package "experiments/moon_i2p/common"
|
||||
|
||||
import {
|
||||
"experiments/moon_i2p/crypto",
|
||||
"experiments/moon_i2p/encoding",
|
||||
"moonbitlang/core/debug",
|
||||
}
|
||||
@@ -12,6 +13,7 @@ import {
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub suberror CommonError {
|
||||
Encoding(@encoding.EncodingError)
|
||||
Crypto(@crypto.CryptoError)
|
||||
InvalidLength(String)
|
||||
InvalidFormat(String)
|
||||
}
|
||||
@@ -126,6 +128,11 @@ pub(all) struct LeaseSet2 {
|
||||
}
|
||||
pub fn LeaseSet2::decode(@encoding.Reader) -> Self raise CommonError
|
||||
pub fn LeaseSet2::encode(Self, @encoding.Writer) -> Unit raise CommonError
|
||||
pub fn LeaseSet2::sign_ed25519(Self, Bytes) -> Self raise CommonError
|
||||
pub fn LeaseSet2::signing_bytes(Self) -> Bytes raise CommonError
|
||||
pub fn LeaseSet2::to_bytes(Self) -> Bytes raise CommonError
|
||||
pub fn LeaseSet2::unsigned_bytes(Self) -> Bytes raise CommonError
|
||||
pub fn LeaseSet2::verify_signature(Self) -> Bool
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) struct LeaseSet2EncryptionKey {
|
||||
@@ -181,7 +188,10 @@ pub(all) struct RouterInfo {
|
||||
}
|
||||
pub fn RouterInfo::decode(@encoding.Reader) -> Self raise CommonError
|
||||
pub fn RouterInfo::encode(Self, @encoding.Writer) -> Unit raise CommonError
|
||||
pub fn RouterInfo::sign_ed25519(Self, Bytes) -> Self raise CommonError
|
||||
pub fn RouterInfo::to_bytes(Self) -> Bytes raise CommonError
|
||||
pub fn RouterInfo::unsigned_bytes(Self) -> Bytes raise CommonError
|
||||
pub fn RouterInfo::verify_signature(Self) -> Bool
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub struct SessionKey {
|
||||
|
||||
+117
-4
@@ -78,6 +78,52 @@ pub fn RouterInfo::encode(
|
||||
w.write_bytes(self.signature)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn RouterInfo::to_bytes(self : RouterInfo) -> Bytes raise CommonError {
|
||||
let w = @encoding.Writer::new(capacity=600)
|
||||
self.encode(w)
|
||||
w.to_bytes()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Signs the `RouterInfo` using an Ed25519 32-byte private `seed` and returns
|
||||
/// a new `RouterInfo` with `signature` populated.
|
||||
pub fn RouterInfo::sign_ed25519(
|
||||
self : RouterInfo,
|
||||
seed : Bytes,
|
||||
) -> RouterInfo raise CommonError {
|
||||
if self.identity.signing_type() != EdDsaSha512Ed25519 {
|
||||
raise InvalidFormat(
|
||||
"RouterInfo identity signing type must be EdDsaSha512Ed25519",
|
||||
)
|
||||
}
|
||||
let body = self.unsigned_bytes()
|
||||
let signature = @crypto.ed25519_sign(seed, body) catch {
|
||||
e => raise Crypto(e)
|
||||
}
|
||||
{
|
||||
identity: self.identity,
|
||||
published: self.published,
|
||||
addresses: self.addresses,
|
||||
options: self.options,
|
||||
signature,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Verifies the `RouterInfo` Ed25519 signature against `self.identity.signing_public_key()`.
|
||||
pub fn RouterInfo::verify_signature(self : RouterInfo) -> Bool {
|
||||
if self.identity.signing_type() != EdDsaSha512Ed25519 {
|
||||
return false
|
||||
}
|
||||
let body = self.unsigned_bytes() catch { _ => return false }
|
||||
@crypto.ed25519_verify(
|
||||
self.identity.signing_public_key(),
|
||||
body,
|
||||
self.signature,
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn RouterInfo::decode(r : @encoding.Reader) -> RouterInfo raise CommonError {
|
||||
let identity = RouterIdentity::decode(r)
|
||||
@@ -175,10 +221,9 @@ pub(all) struct LeaseSet2 {
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn LeaseSet2::encode(
|
||||
self : LeaseSet2,
|
||||
w : @encoding.Writer,
|
||||
) -> Unit raise CommonError {
|
||||
/// Encodes the unsigned body of LeaseSet2 (everything before `signature`).
|
||||
pub fn LeaseSet2::unsigned_bytes(self : LeaseSet2) -> Bytes raise CommonError {
|
||||
let w = @encoding.Writer::new(capacity=512)
|
||||
self.destination.encode(w)
|
||||
w.write_u32_be(self.published_sec)
|
||||
w.write_u16_be(self.expires_offset)
|
||||
@@ -194,9 +239,77 @@ pub fn LeaseSet2::encode(
|
||||
for lease in self.leases {
|
||||
lease.encode(w)
|
||||
}
|
||||
w.to_bytes()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Returns the exact bytes covered by the LeaseSet2 signature:
|
||||
/// single-byte DatabaseStore type `0x03` prepended to `unsigned_bytes()` (Proposal 123).
|
||||
pub fn LeaseSet2::signing_bytes(self : LeaseSet2) -> Bytes raise CommonError {
|
||||
let body = self.unsigned_bytes()
|
||||
let w = @encoding.Writer::new(capacity=1 + body.length())
|
||||
w.write_u8(b'\x03')
|
||||
w.write_bytes(body)
|
||||
w.to_bytes()
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn LeaseSet2::encode(
|
||||
self : LeaseSet2,
|
||||
w : @encoding.Writer,
|
||||
) -> Unit raise CommonError {
|
||||
w.write_bytes(self.unsigned_bytes())
|
||||
w.write_bytes(self.signature)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn LeaseSet2::to_bytes(self : LeaseSet2) -> Bytes raise CommonError {
|
||||
let w = @encoding.Writer::new(capacity=600)
|
||||
self.encode(w)
|
||||
w.to_bytes()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Signs the `LeaseSet2` using an Ed25519 32-byte private `seed` (over `0x03 || unsigned_bytes`).
|
||||
pub fn LeaseSet2::sign_ed25519(
|
||||
self : LeaseSet2,
|
||||
seed : Bytes,
|
||||
) -> LeaseSet2 raise CommonError {
|
||||
if self.destination.inner.signing_type() != EdDsaSha512Ed25519 {
|
||||
raise InvalidFormat(
|
||||
"LeaseSet2 destination signing type must be EdDsaSha512Ed25519",
|
||||
)
|
||||
}
|
||||
let to_sign = self.signing_bytes()
|
||||
let signature = @crypto.ed25519_sign(seed, to_sign) catch {
|
||||
e => raise Crypto(e)
|
||||
}
|
||||
{
|
||||
destination: self.destination,
|
||||
published_sec: self.published_sec,
|
||||
expires_offset: self.expires_offset,
|
||||
flags: self.flags,
|
||||
options: self.options,
|
||||
encryption_keys: self.encryption_keys,
|
||||
leases: self.leases,
|
||||
signature,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Verifies the `LeaseSet2` Ed25519 signature against `self.destination.inner.signing_public_key()`.
|
||||
pub fn LeaseSet2::verify_signature(self : LeaseSet2) -> Bool {
|
||||
if self.destination.inner.signing_type() != EdDsaSha512Ed25519 {
|
||||
return false
|
||||
}
|
||||
let to_sign = self.signing_bytes() catch { _ => return false }
|
||||
@crypto.ed25519_verify(
|
||||
self.destination.inner.signing_public_key(),
|
||||
to_sign,
|
||||
self.signature,
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn LeaseSet2::decode(r : @encoding.Reader) -> LeaseSet2 raise CommonError {
|
||||
let destination = Destination::decode(r)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#derive(Eq, Debug)
|
||||
pub suberror CommonError {
|
||||
Encoding(@encoding.EncodingError)
|
||||
Crypto(@crypto.CryptoError)
|
||||
InvalidLength(String)
|
||||
InvalidFormat(String)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user