-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use base64::{engine::general_purpose, Engine as _}; | ||
use clap::{Args, Parser, Subcommand}; | ||
use x25519_dalek::{StaticSecret, PublicKey}; | ||
use std::convert::TryInto; | ||
use std::fs; | ||
use std::io::{self, Read, Write}; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Parser)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Glome, | ||
} | ||
|
||
#[derive(Args)] | ||
struct TagArgs { | ||
/// Path to secret key | ||
#[arg(short, long, value_name = "FILE")] | ||
key: PathBuf, | ||
/// Path to peer's public key | ||
#[arg(short, long, value_name = "FILE")] | ||
peer: PathBuf, | ||
/// Message counter index | ||
#[arg(short, long, value_name = "n")] | ||
counter: Option<u8>, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
enum Glome { | ||
/// Generate a new secret key and print it to stdout | ||
Genkey, | ||
/// Read a private key from stdin and write its public key to stdout | ||
Pubkey, | ||
/// Tag a message read from stdin | ||
Tag(TagArgs), | ||
} | ||
|
||
fn genkey() -> io::Result<()> { | ||
io::stdout().write_all(StaticSecret::random().as_bytes()) | ||
} | ||
|
||
// TODO(burgerdev): this is the pre-0.1.0 way of writing public keys! | ||
fn pubkey() -> io::Result<()> { | ||
let mut buf: [u8; 32] = [0; 32]; | ||
io::stdin().read_exact(&mut buf)?; | ||
let sk: StaticSecret = buf.into(); | ||
let pk: PublicKey = (&sk).into(); | ||
io::stdout().write_all(pk.as_bytes()) | ||
} | ||
|
||
fn read_key(path: &PathBuf) -> [u8; 32] { | ||
let b: Box<[u8; 32]> = fs::read(path) | ||
.expect(format!("file {:?} should be readable", path).as_str()) | ||
.into_boxed_slice() | ||
.try_into() | ||
.expect(format!("file {:?} should contain exactly 32 bytes", path).as_str()); | ||
*b | ||
} | ||
|
||
fn gentag(args: &TagArgs) -> io::Result<()> { | ||
let ours: StaticSecret = read_key(&args.key).into(); | ||
let theirs: PublicKey = read_key(&args.peer).into(); | ||
|
||
let mut buf = Vec::new(); | ||
io::stdin().read_to_end(&mut buf)?; | ||
|
||
let t = glome::tag(&ours, &theirs, args.counter.unwrap_or_default() /* TODO */, &buf); | ||
|
||
let encoded = general_purpose::URL_SAFE.encode(&t); | ||
|
||
io::stdout().write_all(encoded.as_bytes()) | ||
} | ||
|
||
fn main() -> io::Result<()> { | ||
match &Cli::parse().command { | ||
Glome::Genkey => genkey(), | ||
Glome::Pubkey => pubkey(), | ||
Glome::Tag(tag_args) => gentag(tag_args), | ||
} | ||
} |