Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

udate dependencies and make examples work #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
cargo-features = ["edition"]

[package]
authors = ["Without Boats <[email protected]>"]
description = "bridge non-PGP system to PGP data format"
Expand All @@ -11,21 +9,42 @@ version = "0.4.0"
repository = "https://github.com/withoutboats/pbp"

[dependencies]
base64 = "0.9.2"
byteorder = "1.1.0"
digest = "0.7.0"
sha1 = "0.2.0"
typenum = "1.9.0"
failure = "0.1.1"
bitflags = "1.0.1"
base64 = "0.11"
byteorder = "1"
digest = "0.8"
sha1 = "0.6"
typenum = "1.11"
failure = "0.1"
bitflags = "1.2"
rand = "0.6"
sha2 = "0.8"

[dependencies.ed25519-dalek]
version = "0.7.0"
version = "0.9"
optional = true

[features]
dalek = ["ed25519-dalek"]

[dev-dependencies]
rand = "0.5.4"
sha2 = "0.6.0"
ed25519-dalek = "0.9"

[[example]]
required-features = ["dalek"]
name = "print"
path = "examples/print.rs"

[[example]]
required-features = ["dalek"]
name = "read_sig"
path = "examples/read_sig.rs"

[[example]]
required-features = ["dalek"]
name = "round_trip"
path = "examples/round_trip.rs"

[[example]]
required-features = ["dalek"]
name = "verify_sig"
path = "examples/verify_sig.rs"
29 changes: 21 additions & 8 deletions examples/print.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
extern crate rand;
extern crate sha2;
extern crate ed25519_dalek as dalek;
extern crate pbp;
extern crate rand;
extern crate sha2;

use std::time::SystemTime;

use rand::OsRng;
use sha2::{Sha256, Sha512};
use dalek::Keypair;
use pbp::{PgpKey, KeyFlags};
use failure::Error;
use pbp::{KeyFlags, PgpKey};
use rand::rngs::OsRng;
use sha2::{Sha256, Sha512};

fn main() {
fn main() -> Result<(), Error> {
let mut cspring = OsRng::new().unwrap();
let keypair = Keypair::generate::<Sha512>(&mut cspring);
let keypair = Keypair::generate::<Sha512, _>(&mut cspring);
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)?
.as_secs();

let key = PgpKey::from_dalek::<Sha256, Sha512>(&keypair, KeyFlags::NONE, "withoutboats");
let key = PgpKey::from_dalek::<Sha256, Sha512>(
&keypair,
KeyFlags::NONE,
timestamp as u32,
"withoutboats",
);
println!("{}", key);

Ok(())
}
13 changes: 8 additions & 5 deletions examples/read_sig.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate rand;
extern crate sha2;
extern crate ed25519_dalek as dalek;
extern crate pbp;
extern crate rand;
extern crate sha2;

use std::io::{self, BufRead};

Expand All @@ -20,14 +20,17 @@ fn main() {
stdin.read_line(&mut buf).unwrap();
if buf.trim().starts_with("-----") && buf.trim().ends_with("-----") {
armor.push_str(&buf);
if in_armor { break }
else { in_armor = true; }
if in_armor {
break;
} else {
in_armor = true;
}
} else if in_armor {
armor.push_str(&buf);
}
}

if PgpSig::from_ascii_armor(&armor).is_some() {
if PgpSig::from_ascii_armor(&armor).ok().is_some() {
println!("Valid PGP Signature");
}
}
43 changes: 32 additions & 11 deletions examples/round_trip.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
extern crate rand;
extern crate sha2;
extern crate ed25519_dalek as dalek;
extern crate pbp;
extern crate rand;
extern crate sha2;

use std::time::SystemTime;

use rand::OsRng;
use sha2::{Sha256, Sha512};
use dalek::Keypair;
use pbp::{PgpKey, PgpSig, SigType, KeyFlags};
use failure::Error;
use pbp::{KeyFlags, PgpKey, PgpSig, SigType};
use rand::rngs::OsRng;
use sha2::Digest;
use sha2::{Sha256, Sha512};

const DATA: &[u8] = b"How will I ever get out of this labyrinth?";

fn main() {
fn main() -> Result<(), Error> {
let mut cspring = OsRng::new().unwrap();
let keypair = Keypair::generate::<Sha512>(&mut cspring);

let key = PgpKey::from_dalek::<Sha256, Sha512>(&keypair, KeyFlags::SIGN, "withoutboats");
let sig = PgpSig::from_dalek::<Sha256, Sha512>(&keypair, DATA, key.fingerprint(), SigType::BinaryDocument);
if sig.verify_dalek::<Sha256, Sha512>(DATA, &keypair.public) {
let keypair = Keypair::generate::<Sha512, _>(&mut cspring);
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)?
.as_secs();
let key = PgpKey::from_dalek::<sha2::Sha256, sha2::Sha512>(
&keypair,
KeyFlags::NONE,
timestamp as u32,
"withoutboats",
);
let sig = PgpSig::from_dalek::<Sha256, Sha512>(
&keypair,
DATA,
key.fingerprint(),
SigType::BinaryDocument,
timestamp as u32,
);
if sig.verify_dalek::<Sha256, Sha512, _>(&keypair.public, |hasher| {
hasher.input(DATA);
}) {
println!("Verified successfully.");
} else {
println!("Could not verify.");
}

Ok(())
}
15 changes: 8 additions & 7 deletions examples/verify_sig.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
#![feature(fs_read_write)]

extern crate pbp;
extern crate sha2;

use std::env;
use std::fs;
use std::path::PathBuf;

use sha2::{Sha256, Sha512};
use pbp::{PgpKey, PgpSig};
use sha2::Digest;
use sha2::{Sha256, Sha512};

fn main() {
let root = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let props = root.join("examples").join("props");

let sig: String = fs::read_string(props.join("sig.txt")).unwrap();
let key: String = fs::read_string(props.join("key.txt")).unwrap();
let data: String = fs::read_string(props.join("data.txt")).unwrap();
let sig: String = fs::read_to_string(props.join("sig.txt")).unwrap();
let key: String = fs::read_to_string(props.join("key.txt")).unwrap();
let data: String = fs::read_to_string(props.join("data.txt")).unwrap();

let sig = PgpSig::from_ascii_armor(&sig).unwrap();
let key = PgpKey::from_ascii_armor(&key).unwrap();

if sig.verify_dalek::<Sha256, Sha512>(data.as_bytes(), &key.to_dalek().unwrap()) {
if sig.verify_dalek::<Sha256, Sha512, _>(&key.to_dalek().unwrap(), |hasher| {
hasher.input(&data);
}) {
println!("Verified signature.");
} else {
println!("Could not verify signature.");
Expand Down
41 changes: 22 additions & 19 deletions src/ascii_armor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ impl From<base64::DecodeError> for PgpError {
}

// Convert from an ASCII armored string into binary data.
pub fn remove_ascii_armor(s: &str, expected_header: &str, expected_footer: &str) -> Result<Vec<u8>, PgpError> {
pub fn remove_ascii_armor(
s: &str,
expected_header: &str,
expected_footer: &str,
) -> Result<Vec<u8>, PgpError> {
let lines: Vec<&str> = s.lines().map(|s| s.trim()).collect();
let header = lines.first().ok_or(InvalidAsciiArmor)?;
let footer = lines.last().ok_or(InvalidAsciiArmor)?;
Expand All @@ -28,51 +32,51 @@ pub fn remove_ascii_armor(s: &str, expected_header: &str, expected_footer: &str)
|| header.trim_matches('-').trim() != expected_header
|| footer.trim_matches('-').trim() != expected_footer
{
return Err(InvalidAsciiArmor)
return Err(InvalidAsciiArmor);
}

// Find the end of the header section
let end_of_headers = 1 + lines.iter().take_while(|l| !l.is_empty()).count();
if end_of_headers >= lines.len() - 2 { return Err(InvalidAsciiArmor) }
if end_of_headers >= lines.len() - 2 {
return Err(InvalidAsciiArmor);
}

// Decode the base64'd data
let ascii_armored: String = lines[end_of_headers..lines.len() - 2].concat();
let data = base64::decode(&ascii_armored)?;

// Confirm checksum
let cksum_line = &lines[lines.len() - 2];
if !cksum_line.starts_with("=") || !cksum_line.len() > 1 {
return Err(InvalidAsciiArmor)

if !cksum_line.starts_with("=") || !(cksum_line.len() > 1) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like we need a () here for cksum_line.len() > 1

return Err(InvalidAsciiArmor);
}
let mut cksum = [0; 4];
base64::decode_config_slice(&cksum_line[1..], base64::STANDARD, &mut cksum[..])?;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I can't get this work against examples/props/sig.txt. After comparing the cksum buffers I found that this code gives [184, 23, 112, 0], while the correct one should be [0, 184, 23, 112].

base64::decode_config_slice(&cksum_line[1..], base64::URL_SAFE, &mut cksum[1..])?;
let mut cksum_buf = [0; 4];
BigEndian::write_u32(&mut cksum_buf, checksum_crc24(&data));

if BigEndian::read_u32(&cksum[..]) != checksum_crc24(&data) {
return Err(InvalidAsciiArmor)
return Err(InvalidAsciiArmor);
}

Ok(data)
}
}

// Ascii armors data into the formatter
pub fn ascii_armor(
header: &'static str,
footer: &'static str,
data: &[u8],
f: &mut fmt::Formatter
) -> fmt::Result
{
data: &[u8],
f: &mut fmt::Formatter,
) -> fmt::Result {
// Header Line
f.write_str("-----")?;
f.write_str(header)?;
f.write_str("-----\n\n")?;

// Base64'd data
let b64_cfg = base64::Config::new(
base64::CharacterSet::Standard,
true,
false,
base64::LineWrap::Wrap(76, base64::LineEnding::LF),
);
let b64_cfg = base64::Config::new(base64::CharacterSet::Standard, true);
f.write_str(&base64::encode_config(data, b64_cfg))?;
f.write_str("\n=")?;

Expand Down Expand Up @@ -101,7 +105,6 @@ fn checksum_crc24(data: &[u8]) -> u32 {
crc ^= (byte as u32) << 16;

for _ in 0..8 {

crc <<= 1;

if (crc & 0x_0100_0000) != 0 {
Expand Down
Loading