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

Consume gridiron 0.4 #21

Merged
merged 7 commits into from
Dec 10, 2018
Merged
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: rust
cache: cargo
rust:
- 1.30.0
- 1.31.0
branches:
only:
- master
Expand Down
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ sha2 = "~0.8"
num-traits = "~0.2"
lazy_static = "~1.2"
arrayvec = "~0.4"
ed25519-dalek = "1.0.0-pre.0"
# ed25519-dalek = "1.0.0-pre.0"
ed25519-dalek = { git = "https://github.com/IronCoreLabs/ed25519-dalek", branch = "rand-0.6"}
curve25519-dalek = "1.0.0-pre.1"
clear_on_drop = "~0.2"
gridiron = "0.2"
gridiron = "0.4"
quick-error = "~1.2"
hex="~0.3"


[profile.dev]
opt-level = 2 # Build deps with optimization, don't build ourselves with optimization
Expand All @@ -38,7 +41,6 @@ lto = true
[dev-dependencies]
proptest = "~0.8"
rand_chacha = "~0.1"
hex = "~0.3"
criterion = "~0.2"

[features]
Expand Down
118 changes: 34 additions & 84 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<CR: rand::CryptoRng + rand::RngCore> Api<Sha256, Ed25519, RandomBytes<CR>>
}
}

quick_error!{
quick_error! {
/// Errors generated by the API
#[derive(Debug, PartialEq)]
pub enum ApiErr {
Expand Down Expand Up @@ -785,6 +785,12 @@ impl<R: RandomBytesGen, H: Sha256Hashing, S: Ed25519Signing> KeyGenOps for Api<H
/// Encrypt, Decrypt, Transform, and supporting operations.
pub trait CryptoOps {
/// Using the random_bytes, generate a random element of G_T, which is one of the rth roots of unity in FP12.
///
/// What it means to be an rth root (for Fp256):
/// let curve_order = 6500054969564660373279643874235990574257040605390378638988106296904416679996; (this is "r" -- also defined as the prime for Fr256)
/// let rth_pow = plaintext.pow(curve_order);
/// assert_eq!(rth_pow, Fp12Elem::one());
/// Note that this cannot be implemented here as we do not define a way to do: Fp12.pow(Fp256)
fn gen_plaintext(&mut self) -> Plaintext;

/// Convert our plaintext into a DecryptedSymmetricKey by hashing it.
Expand Down Expand Up @@ -887,7 +893,8 @@ impl<R: RandomBytesGen, H: Sha256Hashing, S: Ed25519Signing> CryptoOps for Api<H
&self.curve_points,
&self.sha_256,
&self.ed25519,
).map(Plaintext::from)?)
)
.map(Plaintext::from)?)
}

fn transform(
Expand Down Expand Up @@ -956,7 +963,8 @@ impl PublicKey {
x,
y,
_internal_key: *internal_key,
}).ok_or_else(|| internal::homogeneouspoint::PointErr::ZeroPoint)?)
})
.ok_or_else(|| internal::homogeneouspoint::PointErr::ZeroPoint)?)
}

pub fn new(
Expand Down Expand Up @@ -1070,11 +1078,8 @@ pub(crate) mod test {
use super::*;
use hex;
use internal::ed25519;
use internal::Square;
use num_traits::{One, Zero};
use rand;
use internal::fp::fp256_unsafe_from;
use rand_chacha;
use std::ops::Mul;

//Writing a BS from conversion to make tests which contain `hex` constants easier to write.
impl From<hex::FromHexError> for ApiErr {
Expand All @@ -1083,28 +1088,6 @@ pub(crate) mod test {
}
}

///Duplicated here for the generate plaintext test
fn pow_for_square<T: One + Mul<T, Output = T> + Copy + Square>(t: T, exp: Fp256) -> T {
use gridiron::digits::util::DigitsArray;
if exp == Fp256::zero() {
T::one()
} else {
let mut mut_exp = exp;
let mut y = T::one();
let mut x = t;
while mut_exp > Fp256::one() {
if !mut_exp.is_even() {
y = x * y;
x = x.square();
} else {
x = x.square();
}
mut_exp = Fp256::new(mut_exp.shift_right_bits(1));
}
y * x
}
}

pub struct DummyEd25519;
impl Ed25519Signing for DummyEd25519 {
fn sign<T: Hashable>(&self, _t: &T, _signing_keypair: &SigningKeypair) -> Ed25519Signature {
Expand Down Expand Up @@ -1198,31 +1181,20 @@ pub(crate) mod test {
fn test_compute_public_key() {
let api = &mut Api::new();
//37777967648492203239675772600961898148040325589588086812374811831221462604944
let parsed_priv_key = Fp256::new([
17189375727870516368,
18224873185075715024,
7861335034745733951,
6018377467983639816,
]);
let parsed_priv_key =
fp256_unsafe_from("5385926b9f6135086d1912901e5a433ffcebc19a30fadbd0ee8cee26ba719c90");
let private_key = &PrivateKey::new(parsed_priv_key.to_bytes_32());

//56377452267431283559088187378398270325210563762492926393848580098576649271541
let parsed_pub_key_x = Fp256::new([
1788354481340266741,
6941240287463798938,
4130173504620995341,
8981446317750070851,
]);
let parsed_pub_key_x =
fp256_unsafe_from("7ca481d71abbae43395152eb7baa230d60543d43e2e8f89a18d182ecf8c3b8f5");
//46643694276241842996939080253335644316475473619096522181405937227991761798154
let parsed_pub_key_y = Fp256::new([
3047215073141965834,
7252896082344953161,
7531499505638418112,
7430769205681594307,
]);
let parsed_pub_key_y =
fp256_unsafe_from("671f653900901fc3688542e5939ba6c064a7768f34fe45492a49e1f6d4d7c40a");
let public_key_expected = PublicKey::try_from(
&internal::PublicKey::from_x_y_fp256(parsed_pub_key_x, parsed_pub_key_y).unwrap(),
).unwrap();
)
.unwrap();

let computed_pub_key = api
.compute_public_key(private_key)
Expand All @@ -1237,20 +1209,11 @@ pub(crate) mod test {
let (_, pub_key) = api.generate_key_pair().unwrap();
let internal_pk = internal::PublicKey::from_x_y_fp256(
//58483620629232886210555514960799664032881966270053836377116209031946678864174
Fp256::new([
16689358048857196846,
6008153970969623586,
15595859671902000750,
9316978295816992987,
]),
fp256_unsafe_from("814c8e65863238dbd86f9fbdbe8f166e536140343b7f3c22e79c82b8af70892e"),
//39604663823550822619127054070927331080305575010367415285113646212320556073913
Fp256::new([
6198767802656867257,
14549110804132857103,
11657926921458149507,
6309386958041887471,
]),
).unwrap();
fp256_unsafe_from("578f72028091b2efa1c946c4caf9e883c9e8d3311e23050f560672795a7dc3b9"),
)
.unwrap();
let expected_pub_key = PublicKey::try_from(&internal_pk).unwrap();
assert_eq!(expected_pub_key, pub_key)
}
Expand Down Expand Up @@ -1297,21 +1260,6 @@ pub(crate) mod test {
)
}

#[test]
fn gen_plaintext_always_produce_rth_root() {
let mut api = api_with(None::<RandomBytes<rand::rngs::ThreadRng>>, DummyEd25519);
let pt = api.gen_plaintext();
let fp12 = <Fp12Elem<Fp256>>::decode(pt.bytes.to_vec()).unwrap();
let curve_order = Fp256::new([
1886713967064937057,
3354493509585025316,
12281294985516866593,
10355184993929758713,
]);
let rth_pow = pow_for_square(fp12, curve_order);
assert_eq!(rth_pow, Fp12Elem::one());
}

#[test]
fn roundtrip_transform_block() {
let mut api = Api::new();
Expand All @@ -1331,12 +1279,12 @@ pub(crate) mod test {
fn decrypt_known_value() -> Result<()> {
let expected_pt = Plaintext::new_from_slice(&hex::decode("3e0348980131e4db298445c3ef424ad60ebfa816069689be559f5ffeecf5e635201172f1bc931833b431a8d7a118e90d516de84e6e4de2f3105695b7699104ee18dd4598f93417ed736b40515a4817499a748be1bf126c132a8a4e8da83780a9054d6e1de22e21e446dbaa3a121d103fdf813a31afac09881beb0a3ae974ffdd537049eea02dade975525c720d152c87b4f0e76645c4cf46ee0e731378ad5c5d12630a32d0610c52c3c56fc0d7666ad6464adeca698a2ee4c44666c05d2e58154b961a595a445b156ce0bdd3e13ffa5b296e8c364aecec6208a0aa54cdea40455032a11458b08d143a51013dcdb8febd01bd93966bff2fc8bbd121efc19fedcb576d82e70838f8f987c5cb887a857d4a6d68c8bbf9196d72b98bea0a62d3fda109a46c28c6d87851223f38712226ba8a5c36197ee016baa27051c398a95c184820e6493c972f7e53936a2abd9c22483d3595fee87ad2a2771af0cc847548bc233f258d4bf77df8265b566ef54c288ad3a8034d18b3af4cb1d71b2da649200fa1")?)?;
let encrypted = EncryptedValue::EncryptedOnceValue{
ephemeral_public_key: PublicKey::new_from_slice((&hex::decode("7013008e19061384a3e6ba1f1a98834cb787b671a0fe181c3adeae15e24c0bba").unwrap(), &hex::decode("3165123233dc537c870673495c7db71239a51647d29113a0d3f5f99eea8de513").unwrap()))?,
encrypted_message: EncryptedMessage::new_from_slice(&hex::decode("2aab5397ef54cd3ea6f3ea3313df53059a47fb35786fb9374dda260af183d0150b062c9ee31feded7c2f966c5323d51954c382c583bb14123ad220c7d1457f7e849e95a28f434df3406561c303084644c6a950218996f871a45e0ebf842d65e828ce3bb04067bc7674edee95b0f697764d546ec760c416c390b869bc18c458c7867fee841d6c50f85a4db4591a4a95b7fbabc2add2f09e4a574d3c21f54b8846247ba2ec7373db45a86df589dd1b5cb5e9178aa14502877fb12d243626081ebd7eb4d501bb9da3d21ba1b4b779d4ffdd468f25e8c2f0cbecca3cd4e0c5960ab55471e42a6183714da09cfc0e70c8bd4ea720618a077c296b4744dfdf898bc95016f5d38e776d750b51da8fc98ef68894f7087730ad7e60d23062c8f216bfc4293c10d1d966203601db3db27eaa50afab06ab1eba9e9bb1f8b8ebc42cf01c73284f0861aab05d492c7d98137a1dcacdca45b277fcb51f665690e21a5549758b0c3654e38745c39c17b953ebfd66e685153a6b6aae1ac2a87f866896bda8d14012")?)?,
auth_hash: AuthHash::new_from_slice(&hex::decode("334bad3490633ebb346fb22a628356f19c299b2be90e5efe0ec344039662c307")?)?,
public_signing_key: PublicSigningKey::new_from_slice(&hex::decode("7ada8837de936ec230afd05b73a378987784534d731ba35f68ecb777846232ab")?)?,
signature: Ed25519Signature::new_from_slice(&hex::decode("312901e121e0637eb0814b1411ec6772147d5ab2063ae781ec2f227748059ac5d892a6eed7c66e1638649903fe3ecbb9c2b5674e87e9b9c39009a175f2177e0f")?)?,
};
ephemeral_public_key: PublicKey::new_from_slice((&hex::decode("7013008e19061384a3e6ba1f1a98834cb787b671a0fe181c3adeae15e24c0bba").unwrap(), &hex::decode("3165123233dc537c870673495c7db71239a51647d29113a0d3f5f99eea8de513").unwrap()))?,
encrypted_message: EncryptedMessage::new_from_slice(&hex::decode("2aab5397ef54cd3ea6f3ea3313df53059a47fb35786fb9374dda260af183d0150b062c9ee31feded7c2f966c5323d51954c382c583bb14123ad220c7d1457f7e849e95a28f434df3406561c303084644c6a950218996f871a45e0ebf842d65e828ce3bb04067bc7674edee95b0f697764d546ec760c416c390b869bc18c458c7867fee841d6c50f85a4db4591a4a95b7fbabc2add2f09e4a574d3c21f54b8846247ba2ec7373db45a86df589dd1b5cb5e9178aa14502877fb12d243626081ebd7eb4d501bb9da3d21ba1b4b779d4ffdd468f25e8c2f0cbecca3cd4e0c5960ab55471e42a6183714da09cfc0e70c8bd4ea720618a077c296b4744dfdf898bc95016f5d38e776d750b51da8fc98ef68894f7087730ad7e60d23062c8f216bfc4293c10d1d966203601db3db27eaa50afab06ab1eba9e9bb1f8b8ebc42cf01c73284f0861aab05d492c7d98137a1dcacdca45b277fcb51f665690e21a5549758b0c3654e38745c39c17b953ebfd66e685153a6b6aae1ac2a87f866896bda8d14012")?)?,
auth_hash: AuthHash::new_from_slice(&hex::decode("334bad3490633ebb346fb22a628356f19c299b2be90e5efe0ec344039662c307")?)?,
public_signing_key: PublicSigningKey::new_from_slice(&hex::decode("7ada8837de936ec230afd05b73a378987784534d731ba35f68ecb777846232ab")?)?,
signature: Ed25519Signature::new_from_slice(&hex::decode("312901e121e0637eb0814b1411ec6772147d5ab2063ae781ec2f227748059ac5d892a6eed7c66e1638649903fe3ecbb9c2b5674e87e9b9c39009a175f2177e0f")?)?,
};
let priv_key = PrivateKey::new_from_slice(&hex::decode(
"3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea",
)?)?;
Expand Down Expand Up @@ -1459,7 +1407,8 @@ pub(crate) mod test {
&group_master_private_key,
user_master_public_key,
&signing_key,
).unwrap();
)
.unwrap();

let user_to_device_transform_key = api
.generate_transform_key(&user_master_private_key, device_public_key, &signing_key)
Expand All @@ -1473,7 +1422,8 @@ pub(crate) mod test {
transformed_to_user,
user_to_device_transform_key,
&signing_key,
).unwrap();
)
.unwrap();
let decrypted_result = api
.decrypt(transformed_to_device, &device_private_key)
.unwrap();
Expand Down
19 changes: 19 additions & 0 deletions src/internal/bit_repr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use gridiron::fp_256::Fp256;
use internal::fp::fr_256::Fr256;

/// A bit representation of a numeric value
pub trait BitRepr {
fn to_bits(&self) -> Vec<u8>;
}

impl BitRepr for Fp256 {
fn to_bits(&self) -> Vec<u8> {
(*self).iter_bit().map(|x| x.0 as u8).collect()
}
}

impl BitRepr for Fr256 {
fn to_bits(&self) -> Vec<u8> {
(*self).iter_bit().map(|x| x.0 as u8).collect()
}
}
2 changes: 1 addition & 1 deletion src/internal/bytedecoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ macro_rules! from_arrayvec_n { ($($n: expr), *) => {
})+
}}

from_arrayvec_n!{32, 128, 384}
from_arrayvec_n! {32, 128, 384}
Loading