feat(#1208): seal log packages to Offband's public key
Ephemeral X25519, HKDF-SHA256 and AES-256-GCM with the 53-byte header as additional data, in the format the log-intake Worker checks. Uses the vetted cryptography package (2.9.0). The test vectors come from Python's cryptography library via tools/logs/make_envelope_vectors.py, an implementation independent of the app, and the Dart output matches them byte for byte; the pull tool will open the same vectors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>docs/1232-region-scope-guide
parent
6533095d73
commit
1246b1ddc4
@ -0,0 +1,79 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:crypto/crypto.dart' as crypto;
|
||||
import 'package:cryptography/cryptography.dart';
|
||||
|
||||
/// Encrypts a log package so only Offband can read it (#1208). The format
|
||||
/// matches `OffbandMesh/log-intake` README and `tools/logs/offband_logs.py`:
|
||||
///
|
||||
/// `OBLG` | version 1 | key id (4) | ephemeral X25519 public (32) | nonce (12)
|
||||
/// | AES-256-GCM ciphertext + 16-byte tag, with the 53-byte header as AAD.
|
||||
///
|
||||
/// The key is HKDF-SHA256 of the X25519 secret, salted with the ephemeral
|
||||
/// and recipient public keys, info `offband-log-v1`. Every package gets a
|
||||
/// fresh ephemeral key pair, so the app holds only Offband's public key and
|
||||
/// needs nothing back from anyone.
|
||||
class LogEnvelope {
|
||||
const LogEnvelope._();
|
||||
|
||||
static const List<int> magic = [0x4F, 0x42, 0x4C, 0x47];
|
||||
static const int version = 1;
|
||||
static const int headerLength = 53;
|
||||
static const int keyLength = 32;
|
||||
static const int nonceLength = 12;
|
||||
static final List<int> _info = utf8.encode('offband-log-v1');
|
||||
|
||||
/// First 4 bytes of SHA-256 of [publicKey]: which key an upload was sealed
|
||||
/// for, so the key can be rotated without guessing.
|
||||
static List<int> keyId(List<int> publicKey) =>
|
||||
crypto.sha256.convert(publicKey).bytes.sublist(0, 4);
|
||||
|
||||
/// Throws [ArgumentError] when [recipientPublicKey] is not a 32-byte key:
|
||||
/// a package sealed to the wrong key could never be read.
|
||||
static Future<Uint8List> seal(
|
||||
List<int> plaintext,
|
||||
List<int> recipientPublicKey, {
|
||||
SimpleKeyPair? ephemeral,
|
||||
List<int>? nonce,
|
||||
}) async {
|
||||
if (recipientPublicKey.length != keyLength) {
|
||||
throw ArgumentError.value(
|
||||
recipientPublicKey.length,
|
||||
'recipientPublicKey',
|
||||
'must be $keyLength bytes',
|
||||
);
|
||||
}
|
||||
final x25519 = X25519();
|
||||
final pair = ephemeral ?? await x25519.newKeyPair();
|
||||
final ephemeralPublic = (await pair.extractPublicKey()).bytes;
|
||||
final shared = await x25519.sharedSecretKey(
|
||||
keyPair: pair,
|
||||
remotePublicKey: SimplePublicKey(
|
||||
recipientPublicKey,
|
||||
type: KeyPairType.x25519,
|
||||
),
|
||||
);
|
||||
final key = await Hkdf(hmac: Hmac.sha256(), outputLength: 32).deriveKey(
|
||||
secretKey: shared,
|
||||
nonce: [...ephemeralPublic, ...recipientPublicKey],
|
||||
info: _info,
|
||||
);
|
||||
final aes = AesGcm.with256bits();
|
||||
final iv = nonce ?? aes.newNonce();
|
||||
final header = [
|
||||
...magic,
|
||||
version,
|
||||
...keyId(recipientPublicKey),
|
||||
...ephemeralPublic,
|
||||
...iv,
|
||||
];
|
||||
final box = await aes.encrypt(
|
||||
plaintext,
|
||||
secretKey: key,
|
||||
nonce: iv,
|
||||
aad: header,
|
||||
);
|
||||
return Uint8List.fromList([...header, ...box.cipherText, ...box.mac.bytes]);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
{
|
||||
"info": "offband-log-v1",
|
||||
"vectors": [
|
||||
{
|
||||
"recipientPrivate": "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20",
|
||||
"recipientPublic": "07a37cbc142093c8b755dc1b10e86cb426374ad16aa853ed0bdfc0b2b86d1c7c",
|
||||
"ephemeralPrivate": "65666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f8081828384",
|
||||
"nonce": "000102030405060708090a0b",
|
||||
"plaintext": "6f666662616e64206c6f67207061636b616765",
|
||||
"envelope": "4f424c4701aaa8fff75714769d116bf76436ae74bc793d2c30ad1903c59ac5273805c7e2698b410c36000102030405060708090a0bed27c668da4989eb6da896d21f060a02e003d20409b6ad7a71fe3d59145ce4fdf46a5d"
|
||||
},
|
||||
{
|
||||
"recipientPrivate": "0707070707070707070707070707070707070707070707070707070707070707",
|
||||
"recipientPublic": "13be4feaeaf204c7fd3358fc9c00721881d174278128227ec674f37f7fe97b6d",
|
||||
"ephemeralPrivate": "0909090909090909090909090909090909090909090909090909090909090909",
|
||||
"nonce": "ffffffffffffffffffffffff",
|
||||
"plaintext": "",
|
||||
"envelope": "4f424c47010f6d255357db4b359f23ae5e146e4e2512056704722506348c150c14753d0c933d04d421ffffffffffffffffffffffff00bafbc9ae82f37de64a55890d982b96"
|
||||
},
|
||||
{
|
||||
"recipientPrivate": "c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7",
|
||||
"recipientPublic": "4d5bab89b0733d9d8dcecf04f321c90b761b7765a6bdb2bddbfad3e7abdf1f66",
|
||||
"ephemeralPrivate": "32333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f5051",
|
||||
"nonce": "28292a2b2c2d2e2f30313233",
|
||||
"plaintext": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
|
||||
"envelope": "4f424c4701e3226371e05b1ae322024838095ffdefd865468ddb77b14b2dd275175ea0ab4294fa6d7a28292a2b2c2d2e2f30313233e029532085e8f6d860e218ed4bf5da360648bf42c29eee31397e496f5c4bde84ea96b884c0692c4956c3ee188aa4f91f06a55120d1084169bbc788f9b9132b65dad5c7786910cfebff6ad558bb414d3a74252fabd0b92b3a0b452c47c956d5a521cff6073845417be7f93e9db04d84d3beee5fd71ce7ca9940b25b541dc4384678679a266ddb79ce5f1c1dba4dd49759bdd1554a7e227896140d8ca2a3b8ac899012cb7cbc02fab889b1f3966a01b987ba34e7fa1eb4ab89e7ae83425391860bfcaf056cf5177b221bf3e018b367b233fe39b3e076c04259fbdfde5a723ccf0acd12826253625696c008096c602e6b25fa965ed7fa490a3b516f0bab73c66019c652b4a63037702fc0431f0a93d20bfebc35fb936754d41a690c0994ea59a8f5f9184612b1a47b7defa61cd69df564f225f69645efd4cc2994c6d7409c96e23abb40b890c2fa813bdf67f1c0a3c26dcd09664180054498d79f10db92768ff1306277a3f98fef8ca25c8880b8636c85f8066be8dc818476705ee9dc313de83f434f26bcca7d20c5434725638a628d2377ed1431e0ab5756681bf7abb771fe7134a6cfac54fd528a3a6981246376eb408811b34ba123a86da28d9f41c180279973f319c33a1ce2e916544ea882b600e52a5789787b0d4c2ad6b4875945968d7f879bd853c50bac041e91810ca0ae5643917c2fde76b8616bbaa9b4eee5feee6bfc2ee7982231717eedb41bacf2c3c7d83e54881ec469bc3041a3d242166e6fa8ff4ff1a639e385a4352d2768c004026826d455ce4bd442590b8344b592157c86090527647377f744151d5ff7c79a0debb810cfc78c5bdf6f367c756687d78e26f419e78193981140959ca6beea6ceee53a7a55eb12473f6acbad97fd9e3d7dd032911484264d468b6cf8eb9f20b550d4a3df9cac6c14bbe19b18e70e615ad40764d0cee6e14ab6f88b1030af04caf941640739bd9c93b6b1c8fb4db8bd14c536688dee21af219cf6f069c937008bb4fa4a444ed9c870a5b0e84fa8e98cc9953265416c30e066ca6c62a6fd634de1555c3a5b3628918f71158c09d2b406896d62c74dec87091b682ff269fe70d3a7c9812638cd72619ab2310e8a49fdc3c3df338f9929628144b6d6107d70593a78982d338d99c193ce034727596fc29c591c3d1dbe9dc21faf1fbccbf9567385efb756e35f5bcde80c98cb9fc997ae9fc8dff813396c14c9c85fb8578384e4cffd191270648f9cd13244ff697a982d22e1db4876612fdb14af892ee1021c80225eb434f465d3248a802ac0df187ea94bda5a0c7b25127f0eea8bd383dbb24e3fbb03d256be4017794d3c0785381eb3422d1f92a0c3066033e4ba8ae5718fe2fc41f6334c320edfa101ec51bd7ba999d85c0703c5b34f55e579408ca380a1d1b631f585d3cd4a7b2867d452d463c07df020e5834b43961cedc379d94a59564aab258d8021"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,100 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cryptography/cryptography.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/services/log_envelope.dart';
|
||||
|
||||
List<int> _bytes(String hex) => [
|
||||
for (var i = 0; i < hex.length; i += 2)
|
||||
int.parse(hex.substring(i, i + 2), radix: 16),
|
||||
];
|
||||
|
||||
String _hex(List<int> b) =>
|
||||
b.map((x) => x.toRadixString(16).padLeft(2, '0')).join();
|
||||
|
||||
/// Vectors made by tools/logs/make_envelope_vectors.py with Python's
|
||||
/// `cryptography` library, an implementation independent of this app.
|
||||
final _vectors =
|
||||
(jsonDecode(
|
||||
File(
|
||||
'test/fixtures/log_envelope_vectors.json',
|
||||
).readAsStringSync(),
|
||||
)
|
||||
as Map<String, dynamic>)['vectors']
|
||||
as List<dynamic>;
|
||||
|
||||
void main() {
|
||||
test('matches the independent reference byte for byte', () async {
|
||||
expect(_vectors, hasLength(3));
|
||||
for (final v in _vectors.cast<Map<String, dynamic>>()) {
|
||||
final ephemeral = await X25519().newKeyPairFromSeed(
|
||||
_bytes(v['ephemeralPrivate'] as String),
|
||||
);
|
||||
final sealed = await LogEnvelope.seal(
|
||||
_bytes(v['plaintext'] as String),
|
||||
_bytes(v['recipientPublic'] as String),
|
||||
ephemeral: ephemeral,
|
||||
nonce: _bytes(v['nonce'] as String),
|
||||
);
|
||||
expect(_hex(sealed), v['envelope']);
|
||||
}
|
||||
});
|
||||
|
||||
test('a fresh package opens with the recipient key and no other', () async {
|
||||
final recipient = await X25519().newKeyPair();
|
||||
final recipientPublic = (await recipient.extractPublicKey()).bytes;
|
||||
final plaintext = utf8.encode('log package');
|
||||
final sealed = await LogEnvelope.seal(plaintext, recipientPublic);
|
||||
|
||||
expect(sealed.sublist(0, 5), [...LogEnvelope.magic, LogEnvelope.version]);
|
||||
expect(sealed.sublist(5, 9), LogEnvelope.keyId(recipientPublic));
|
||||
expect(sealed.length, LogEnvelope.headerLength + plaintext.length + 16);
|
||||
expect(await _open(sealed, recipient), plaintext);
|
||||
await expectLater(
|
||||
_open(sealed, await X25519().newKeyPair()),
|
||||
throwsA(isA<SecretBoxAuthenticationError>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('two packages never share a key pair or a nonce', () async {
|
||||
final recipientPublic =
|
||||
(await (await X25519().newKeyPair()).extractPublicKey()).bytes;
|
||||
final a = await LogEnvelope.seal([1], recipientPublic);
|
||||
final b = await LogEnvelope.seal([1], recipientPublic);
|
||||
expect(a.sublist(9, 41), isNot(b.sublist(9, 41)));
|
||||
expect(a.sublist(41, 53), isNot(b.sublist(41, 53)));
|
||||
});
|
||||
|
||||
test('refuses a public key of the wrong size', () {
|
||||
expect(
|
||||
() => LogEnvelope.seal([1], List.filled(31, 0)),
|
||||
throwsArgumentError,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<int>> _open(List<int> sealed, SimpleKeyPair recipient) async {
|
||||
final header = sealed.sublist(0, LogEnvelope.headerLength);
|
||||
final ephemeralPublic = header.sublist(9, 41);
|
||||
final recipientPublic = (await recipient.extractPublicKey()).bytes;
|
||||
final shared = await X25519().sharedSecretKey(
|
||||
keyPair: recipient,
|
||||
remotePublicKey: SimplePublicKey(ephemeralPublic, type: KeyPairType.x25519),
|
||||
);
|
||||
final key = await Hkdf(hmac: Hmac.sha256(), outputLength: 32).deriveKey(
|
||||
secretKey: shared,
|
||||
nonce: [...ephemeralPublic, ...recipientPublic],
|
||||
info: utf8.encode('offband-log-v1'),
|
||||
);
|
||||
final body = sealed.sublist(LogEnvelope.headerLength);
|
||||
return AesGcm.with256bits().decrypt(
|
||||
SecretBox(
|
||||
body.sublist(0, body.length - 16),
|
||||
nonce: header.sublist(41, 53),
|
||||
mac: Mac(body.sublist(body.length - 16)),
|
||||
),
|
||||
secretKey: key,
|
||||
aad: header,
|
||||
);
|
||||
}
|
||||
@ -0,0 +1,80 @@
|
||||
"""Reference vectors for the Offband log envelope (#1208).
|
||||
|
||||
Writes test/fixtures/log_envelope_vectors.json. The Dart encoder must
|
||||
reproduce each envelope byte for byte, and offband_logs.py must open it. This
|
||||
uses Python's `cryptography` library, so it is independent of the app.
|
||||
|
||||
python tools/logs/make_envelope_vectors.py test/fixtures/log_envelope_vectors.json
|
||||
"""
|
||||
import hashlib
|
||||
import json
|
||||
import sys
|
||||
|
||||
from cryptography.hazmat.primitives import hashes, serialization
|
||||
from cryptography.hazmat.primitives.asymmetric.x25519 import (
|
||||
X25519PrivateKey,
|
||||
X25519PublicKey,
|
||||
)
|
||||
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||||
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
||||
|
||||
INFO = b"offband-log-v1"
|
||||
|
||||
|
||||
def raw_public(key):
|
||||
return key.public_key().public_bytes(
|
||||
serialization.Encoding.Raw, serialization.PublicFormat.Raw
|
||||
)
|
||||
|
||||
|
||||
def raw_private(key):
|
||||
return key.private_bytes(
|
||||
serialization.Encoding.Raw,
|
||||
serialization.PrivateFormat.Raw,
|
||||
serialization.NoEncryption(),
|
||||
)
|
||||
|
||||
|
||||
def seal(plaintext, recipient_public, ephemeral, nonce):
|
||||
eph_public = raw_public(ephemeral)
|
||||
shared = ephemeral.exchange(X25519PublicKey.from_public_bytes(recipient_public))
|
||||
key = HKDF(
|
||||
algorithm=hashes.SHA256(),
|
||||
length=32,
|
||||
salt=eph_public + recipient_public,
|
||||
info=INFO,
|
||||
).derive(shared)
|
||||
key_id = hashlib.sha256(recipient_public).digest()[:4]
|
||||
header = b"OBLG" + bytes([1]) + key_id + eph_public + nonce
|
||||
return header + AESGCM(key).encrypt(nonce, plaintext, header)
|
||||
|
||||
|
||||
CASES = [
|
||||
(bytes(range(1, 33)), bytes(range(101, 133)), bytes(range(12)), b"offband log package"),
|
||||
(bytes([7] * 32), bytes([9] * 32), bytes([0xFF] * 12), b""),
|
||||
(bytes(range(200, 232)), bytes(range(50, 82)), bytes(range(40, 52)), bytes(range(256)) * 4),
|
||||
]
|
||||
|
||||
|
||||
def main(out_path):
|
||||
vectors = []
|
||||
for recipient_seed, eph_seed, nonce, plaintext in CASES:
|
||||
recipient = X25519PrivateKey.from_private_bytes(recipient_seed)
|
||||
ephemeral = X25519PrivateKey.from_private_bytes(eph_seed)
|
||||
vectors.append(
|
||||
{
|
||||
"recipientPrivate": raw_private(recipient).hex(),
|
||||
"recipientPublic": raw_public(recipient).hex(),
|
||||
"ephemeralPrivate": raw_private(ephemeral).hex(),
|
||||
"nonce": nonce.hex(),
|
||||
"plaintext": plaintext.hex(),
|
||||
"envelope": seal(plaintext, raw_public(recipient), ephemeral, nonce).hex(),
|
||||
}
|
||||
)
|
||||
with open(out_path, "w", encoding="utf-8", newline="\n") as f:
|
||||
json.dump({"info": INFO.decode(), "vectors": vectors}, f, indent=2)
|
||||
f.write("\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1])
|
||||
Loading…
Reference in new issue