Skip to content
pi-c Documentation
Login

Documentation

/* The cryptography of remote control (docs/design/remote-protocol.md), on the vendored BearSSL:
 * Noise_IK and Noise_IKpsk1 over P-256, AES-256-GCM and SHA-256 (sections 2 and 3), the relay's
 * records and the transport's explicit counter, and the Web Push pieces (VAPID's ES256 token and
 * RFC 8291's aes128gcm body, section 5). Pure functions over caller buffers but for the entropy a
 * new key pair takes. Both handshake roles are here: pic is the responder; the initiator plays the
 * phone in the tests. */
#ifndef PIC_NOISE_H
#define PIC_NOISE_H
#include <stddef.h>
#include <stdint.h>

#define PIC_NOISE_PUB 65   /* 0x04 || X || Y */
#define PIC_NOISE_PRIV 32
#define PIC_NOISE_TAG 16
#define PIC_NOISE_SID 8
#define PIC_NOISE_RECORD_CAP 16417 /* type, session id, counter, flag, 16383 bytes, tag */
#define PIC_NOISE_CHUNK 16383      /* envelope bytes in one transport record */

enum { PIC_NOISE_REC_M1 = 1, PIC_NOISE_REC_M2 = 2, PIC_NOISE_REC_DATA = 3, PIC_NOISE_REC_RESET = 4 };

typedef struct {
    unsigned char priv[PIC_NOISE_PRIV], pub[PIC_NOISE_PUB];
} pic_noise_keypair;

/* A new key pair from the platform's entropy, or (PRIV not NULL) the one PRIV makes. 0, or -1. */
int pic_noise_keypair_make(pic_noise_keypair *k, const unsigned char *priv);
/* 1 when PUB is an uncompressed point on P-256 other than infinity, else 0. */
int pic_noise_pub_valid(const unsigned char pub[PIC_NOISE_PUB]);

/* One handshake's state: the symmetric state and the keys (Noise sections 5.2 and 5.3). */
typedef struct {
    int initiator, psk_mode, step;
    unsigned char h[32], ck[32], k[32];
    int has_k;
    uint64_t n;
    pic_noise_keypair s, e;
    int fixed_e; /* e was given (the vectors); else made at the first write */
    unsigned char rs[PIC_NOISE_PUB], re[PIC_NOISE_PUB];
    unsigned char psk[32];
} pic_noise_hs;

/* Starts a handshake: IKpsk1 when PSK is not NULL, else IK. S is this side's static key; RS the
 * responder's static public key (the initiator's only). */
void pic_noise_hs_init(pic_noise_hs *hs, int initiator, const pic_noise_keypair *s, const unsigned char *rs,
                       const unsigned char *psk, const unsigned char *prologue, size_t prologue_len);
/* Uses PRIV as this side's ephemeral (the vectors), before its write. 0, or -1. */
int pic_noise_hs_ephemeral(pic_noise_hs *hs, const unsigned char *priv);
/* Message 1 (initiator writes, responder reads) and message 2 (the reverse). A write puts the
 * Noise message in OUT (CAP bytes) and its length in *N; a read puts the payload in OUT. 0, or -1
 * (a read that does not verify, a bad point, too small a buffer; the state is then unusable). After
 * message 1 is read, hs->rs holds the initiator's static key. */
int pic_noise_write1(pic_noise_hs *hs, const void *payload, size_t len, unsigned char *out, size_t cap, size_t *n);
int pic_noise_read1(pic_noise_hs *hs, const unsigned char *msg, size_t len, unsigned char *out, size_t cap, size_t *n);
int pic_noise_write2(pic_noise_hs *hs, const void *payload, size_t len, unsigned char *out, size_t cap, size_t *n);
int pic_noise_read2(pic_noise_hs *hs, const unsigned char *msg, size_t len, unsigned char *out, size_t cap, size_t *n);
/* Message 1 with an early tag and an optional prekey P, IK only (flak's request, ticket 113;
 * tools/noise-prekey-vectors.js is the reference). The initiator writes e, es, s, ss,
 * EncryptAndHash(empty), then when PREKEY is not NULL MixHash(BIND), MixKey(DH(e, P)),
 * MixKey(DH(s, P)), then EncryptAndHash(payload). PREKEY NULL requires BIND NULL; BIND NULL
 * requires BIND_LEN 0. Without a prekey the first PIC_NOISE_PUB * 2 + 32 bytes are what
 * pic_noise_write1 writes for an empty payload. The responder reads in two halves: begin consumes
 * e, es, s, ss and the tag from the start of MSG, sets *used, and leaves hs->rs authenticated (or
 * the sender holds the responder's private key); finish takes the rest of the message, mixes the
 * prekey the same way (DH(P, re), DH(P, rs)) and decrypts the payload into OUT (CAP bytes). Between
 * the halves only finish or pic_noise_hs_wipe may follow. Each returns 0, or -1 (an IKpsk1 state,
 * the wrong role or order, an invalid point or an all-zero DH output, too small a buffer, a tag that
 * does not verify); after -1 the state is wiped and refuses every call. Message 2 is unchanged. */
int pic_noise_write1_prekey(pic_noise_hs *hs, const unsigned char prekey[PIC_NOISE_PUB], const unsigned char *bind,
                            size_t bind_len, const void *payload, size_t len, unsigned char *out, size_t cap, size_t *n);
int pic_noise_read1_begin(pic_noise_hs *hs, const unsigned char *msg, size_t len, size_t *used);
int pic_noise_read1_finish(pic_noise_hs *hs, const unsigned char *payload, size_t len, const pic_noise_keypair *prekey,
                           const unsigned char *bind, size_t bind_len, unsigned char *out, size_t cap, size_t *n);
/* After message 2: the up (initiator to responder) and down keys, Split(). */
void pic_noise_split(const pic_noise_hs *hs, unsigned char up[32], unsigned char down[32]);
/* The six digits both screens show, from the handshake hash: "333054". */
void pic_noise_sas(const unsigned char h[32], char out[7]);
/* Wipes the handshake's secrets. */
void pic_noise_hs_wipe(pic_noise_hs *hs);

/* A session's transport: a key and counter per lane. */
typedef struct {
    unsigned char sid[PIC_NOISE_SID];
    unsigned char send_k[32], recv_k[32];
    uint64_t send_n;
    int64_t last; /* the highest counter accepted, -1 at first */
} pic_noise_transport;

/* The transport for the side that is the responder (pic) or the initiator (the phone). */
void pic_noise_transport_init(pic_noise_transport *t, int initiator, const unsigned char sid[PIC_NOISE_SID],
                              const unsigned char up[32], const unsigned char down[32]);
/* One transport record of FLAG (0x00 more follows, 0x01 last) and LEN (at most PIC_NOISE_CHUNK)
 * bytes, under the next counter: its length, written to OUT (PIC_NOISE_RECORD_CAP bytes). */
size_t pic_noise_record(pic_noise_transport *t, int flag, const void *data, size_t len, unsigned char *out);
/* Reads a transport record: 0 with the flag in *flag and the data in OUT (PIC_NOISE_CHUNK bytes)
 * and its length in *n, and *gap 1 when records were lost before it; -1 to drop it (a replay, a
 * record that does not verify, a malformed one), with nothing changed. */
int pic_noise_open_record(pic_noise_transport *t, const unsigned char *rec, size_t len, int *flag, unsigned char *out,
                          size_t *n, int *gap);
/* A handshake record's head: TYPE, SID, then the Noise message. Its length. */
size_t pic_noise_head(int type, const unsigned char sid[PIC_NOISE_SID], const unsigned char *msg, size_t len,
                      unsigned char *out);

/* The primitives the derivations take (section 2's HMAC and SHA-256). */
void pic_noise_sha256(unsigned char out[32], const void *a, size_t an, const void *b, size_t bn);
void pic_noise_hmac(unsigned char out[32], const void *key, size_t kn, const void *a, size_t an, const void *b,
                    size_t bn);
/* trunc16(SHA256(LABEL || TOKEN)): a mailbox or channel id from its 32-byte token. */
void pic_noise_id(unsigned char out[16], const char *label, const unsigned char token[32]);

/* The pairing secret's derivations (section 2): psk = HMAC(ps, "pic-rc/1 pair psk"), the token
 * shown to the relay, and the pairing channel's id. */
void pic_noise_pair_derive(const unsigned char ps[32], unsigned char psk[32], unsigned char token[32],
                           unsigned char channel[16]);
/* The offer's 122 bytes: 0x01, mailbox id, pic's static public key, ps, expires (Unix seconds).
 * read: 0 with the parts, or -1 when the bytes are not a version-1 offer. */
#define PIC_NOISE_OFFER 122
void pic_noise_offer(const unsigned char mailbox[16], const unsigned char pub[PIC_NOISE_PUB], const unsigned char ps[32],
                     uint64_t expires, unsigned char out[PIC_NOISE_OFFER]);
int pic_noise_offer_read(const unsigned char *in, size_t n, unsigned char mailbox[16], unsigned char pub[PIC_NOISE_PUB],
                         unsigned char ps[32], uint64_t *expires);

/* base64url without padding. b64u_enc: OUT holds 4 * ceil(N / 3) + 1; returns the length.
 * b64u_dec: the decoded length, or -1 for bad input or too small a buffer. */
size_t pic_b64u_enc(const unsigned char *in, size_t n, char *out);
long pic_b64u_dec(const char *in, size_t n, unsigned char *out, size_t cap);

/* VAPID (RFC 8292): the ES256 signature of INPUT (the JWT's "header.claims"), 64 bytes r || s,
 * deterministic (RFC 6979). 0, or -1. */
int pic_noise_es256(const unsigned char priv[PIC_NOISE_PRIV], const char *input, unsigned char sig[64]);
/* RFC 8291's body for one push: SALT (16), the ephemeral AS key pair, the subscription's UA_PUB
 * (65) and AUTH (16). OUT holds LEN + 103 bytes; returns the length, or 0 on failure. */
size_t pic_noise_webpush(const pic_noise_keypair *as, const unsigned char salt[16], const unsigned char ua_pub[65],
                         const unsigned char auth[16], const void *pt, size_t len, unsigned char *out);

#endif