upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/btfchan.h
diff options
context:
space:
mode:
authorBitflash-sh <btf01@proton.me>2026-07-24 11:18:11 -0300
committerBitflash-sh <btf01@proton.me>2026-07-24 11:18:11 -0300
commit51d617e70f90a972185917ea0096c1ecef2bfa8b (patch)
tree9fbbcd867de9de9d0776afdd02f594ba312c46eb /src/btfchan.h
Bitflash: fair-launch release
Satoshi's bitcoin-0.1.0 reborn with RandomX CPU proof of work, anonymous .btf addressing over Nostr with CGNAT rendezvous, automatic peer discovery, a headless Linux node, and Windows GUI wallet. Fair launch, no premine, 21M cap.
Diffstat (limited to 'src/btfchan.h')
-rw-r--r--src/btfchan.h59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/btfchan.h b/src/btfchan.h
new file mode 100644
index 0000000..7963f9d
--- /dev/null
+++ b/src/btfchan.h
@@ -0,0 +1,59 @@
1// Copyright (c) 2026 Bitflash developers
2// Distributed under the MIT/X11 software license.
3//
4// End-to-end encrypted channel for Bitflash (ported from the Itzal echan.rs).
5// Wraps a raw pipe in an authenticated stream so relays/rendezvous nodes see
6// only ciphertext and cannot MITM: only the endpoint holding the static secret
7// can derive the key.
8//
9// Handshake: the client makes an ephemeral X25519 key and sends its public half
10// to the endpoint. Both sides compute the same box (X25519 ECDH -> shared key
11// for XChaCha20-Poly1305, i.e. libsodium crypto_box). Wire frame:
12// [len:4 BE][nonce:24][ciphertext || 16-byte tag]
13// with a fresh random 24-byte nonce per frame (safe to pick at random).
14
15#ifndef BITFLASH_BTFCHAN_H
16#define BITFLASH_BTFCHAN_H
17
18#include <vector>
19#include <stdint.h>
20#include <stddef.h>
21
22namespace btf
23{
24
25static const int CHAN_KEYBYTES = 32; // X25519 public/secret and shared key
26static const int CHAN_NONCEBYTES = 24; // XChaCha20-Poly1305 nonce
27static const int CHAN_MACBYTES = 16; // Poly1305 tag
28
29// Must be called once before any channel use (initializes libsodium). Idempotent.
30bool ChanInit();
31
32// Generate a static X25519 keypair (e.g. the endpoint's long-term key).
33void ChanKeypair(unsigned char pk[32], unsigned char sk[32]);
34
35// Derive the X25519 public key from a stored secret key (X25519 base mult).
36void ChanPublicFromSecret(unsigned char pk[32], const unsigned char sk[32]);
37
38// Client side: given the endpoint's static public key, produce an ephemeral
39// public key (to hand the endpoint) and the shared key. Returns false on error.
40bool ChanClientHandshake(const unsigned char endpoint_static_pub[32],
41 unsigned char eph_pub_out[32], unsigned char shared_out[32]);
42
43// Endpoint side: rebuild the same shared key from the client's ephemeral public
44// key and my static secret. Only the static-secret holder can do this.
45bool ChanEndpointHandshake(const unsigned char client_eph_pub[32],
46 const unsigned char my_static_sk[32], unsigned char shared_out[32]);
47
48// Encrypt a plaintext chunk into a full wire frame ([len][nonce][ct||tag]).
49std::vector<unsigned char> ChanEncryptFrame(const unsigned char shared[32],
50 const unsigned char* pt, size_t n);
51
52// Decrypt a frame BODY (nonce||ciphertext||tag, i.e. without the 4-byte length
53// prefix). Returns true and fills `out` on success; false on auth failure.
54bool ChanDecryptBody(const unsigned char shared[32],
55 const unsigned char* body, size_t n, std::vector<unsigned char>& out);
56
57} // namespace btf
58
59#endif