Files
n_signer/src/otp_pad.h

77 lines
2.4 KiB
C

/*
* otp_pad.h — OTP pad state for n_signer (one pad per session).
*
* Bound at startup via otp_pad_bind(); accessed by the otp_encrypt /
* otp_decrypt dispatcher verbs. See plans/otp_nostr_integration.md.
*/
#ifndef NSIGNER_OTP_PAD_H
#define NSIGNER_OTP_PAD_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Version string stamped into ASCII-armored output. */
#define NSIGNER_OTP_VERSION "v0.0.2-otp"
/* Returns 1 if a pad has been bound at startup, 0 otherwise. */
int otp_pad_is_bound(void);
/* Return the bound pad's 64-char hex checksum, or NULL if unbound. */
const char *otp_pad_chksum(void);
/* Return the bound pad's directory, or NULL if unbound. */
const char *otp_pad_dir(void);
/* Current offset (bytes consumed) of the bound pad, or 0 if unbound. */
uint64_t otp_pad_current_offset(void);
/* Total size in bytes of the bound pad, or 0 if unbound. */
uint64_t otp_pad_size(void);
/*
* Bind a pad at startup. Returns 0 on success, non-zero on error.
* `pads_dir` is the directory containing <chksum>.pad and <chksum>.state.
* `pad_spec` is a full 64-char chksum or a unique prefix.
* `allow_blkback` non-zero skips the blkback guard (for qvm-block testing).
*/
int otp_pad_bind(const char *pads_dir, const char *pad_spec, int allow_blkback);
/* Unbind and zeroize all pad state. Idempotent. */
void otp_pad_unbind(void);
/*
* Encrypt plaintext bytes with the bound pad.
*
* `encoding` is "ascii" (default) or "binary".
* On success returns 0 and sets:
* *out_payload — malloc'd, caller frees (NUL-terminated for ascii)
* *out_payload_len — length of payload
* *out_new_offset — pad offset after this encryption
*/
int otp_pad_encrypt(const unsigned char *plaintext, size_t pt_len,
const char *encoding,
char **out_payload, size_t *out_payload_len,
uint64_t *out_new_offset);
/*
* Decrypt a ciphertext (ASCII armor or binary .otp blob) with the bound pad.
*
* `encoding` is "ascii", "binary", or NULL (auto-detect by magic bytes).
* On success returns 0 and sets:
* *out_plaintext — malloc'd, caller frees
* *out_pt_len — length of plaintext
*/
int otp_pad_decrypt(const char *input, size_t input_len,
const char *encoding,
unsigned char **out_plaintext, size_t *out_pt_len);
#ifdef __cplusplus
}
#endif
#endif /* NSIGNER_OTP_PAD_H */