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

Move crypto functions #783

Merged
merged 2 commits into from
Dec 2, 2022
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
6 changes: 4 additions & 2 deletions soroban-auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ fn verify_ed25519_signature(env: &Env, auth: &Ed25519Signature, name: Symbol, ar
};
let msg_bin = SignaturePayload::V0(msg).serialize(env);

env.verify_sig_ed25519(&auth.public_key, &msg_bin, &auth.signature);
env.crypto()
.ed25519_verify(&auth.public_key, &msg_bin, &auth.signature);
}

fn verify_account_signatures(env: &Env, auth: &AccountSignatures, name: Symbol, args: Vec<RawVal>) {
Expand Down Expand Up @@ -78,7 +79,8 @@ fn verify_account_signatures(env: &Env, auth: &AccountSignatures, name: Symbol,
}
}

env.verify_sig_ed25519(&sig.public_key, &msg_bytes, &sig.signature);
env.crypto()
.ed25519_verify(&sig.public_key, &msg_bytes, &sig.signature);
let signer_weight = acc.signer_weight(&sig.public_key);
if signer_weight == 0 {
panic!("signature doesn't belong to account");
Expand Down
46 changes: 46 additions & 0 deletions soroban-sdk/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! Crypto contains functions for cryptographic functions.
use crate::{env::internal, Bytes, BytesN, Env};

/// Crypto provides access to cryptographic functions.
pub struct Crypto {
env: Env,
}

impl Crypto {
pub(crate) fn new(env: &Env) -> Crypto {
Crypto { env: env.clone() }
}

pub fn env(&self) -> &Env {
&self.env
}

/// Computes a SHA-256 hash.
pub fn sha256(&self, message: &Bytes) -> BytesN<32> {
let env = self.env();
let bin_obj = internal::Env::compute_hash_sha256(env, message.into());
unsafe { BytesN::unchecked_new(bin_obj.in_env(env)) }
}

/// Verifies an ed25519 signature.
///
/// The ed25519 signature (`sig`) is verified as a valid signature of the
/// message (`msg`) by the ed25519 public key (`pk`).
///
/// ### Panics
///
/// Will panic if the signature verification fails.
///
/// ### TODO
///
/// Return a [Result] instead of panicking.
pub fn ed25519_verify(&self, public_key: &BytesN<32>, message: &Bytes, signature: &BytesN<64>) {
let env = self.env();
let _ = internal::Env::verify_sig_ed25519(
env,
message.to_object(),
public_key.to_object(),
signature.to_object(),
);
}
}
37 changes: 14 additions & 23 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub type EnvVal = internal::EnvVal<Env, RawVal>;
pub type EnvObj = internal::EnvVal<Env, Object>;

use crate::{
accounts::Accounts, address::Address, data::Data, deploy::Deployer, events::Events,
ledger::Ledger, logging::Logger, AccountId, Bytes, BytesN, Vec,
accounts::Accounts, address::Address, crypto::Crypto, data::Data, deploy::Deployer,
events::Events, ledger::Ledger, logging::Logger, AccountId, Bytes, BytesN, Vec,
};

/// The [Env] type provides access to the environment the contract is executing
Expand Down Expand Up @@ -141,6 +141,12 @@ impl Env {
Deployer::new(self)
}

/// Get a [Crypto] for accessing the current cryptographic functions.
#[inline(always)]
pub fn crypto(&self) -> Crypto {
Crypto::new(self)
}

/// Get the 32-byte hash identifier of the current executing contract.
pub fn current_contract(&self) -> BytesN<32> {
let id = internal::Env::get_current_contract(self);
Expand Down Expand Up @@ -192,31 +198,16 @@ impl Env {
unsafe { Vec::unchecked_new(stack.in_env(self)) }
}

/// Computes a SHA-256 hash.
#[doc(hidden)]
#[deprecated(note = "use env.crypto().sha259(msg)")]
pub fn compute_hash_sha256(&self, msg: &Bytes) -> BytesN<32> {
let bin_obj = internal::Env::compute_hash_sha256(self, msg.into());
unsafe { BytesN::unchecked_new(bin_obj.in_env(self)) }
self.crypto().sha256(msg)
}

/// Verifies an ed25519 signature.
///
/// The ed25519 signature (`sig`) is verified as a valid signature of the
/// message (`msg`) by the ed25519 public key (`pk`).
///
/// ### Panics
///
/// Will panic if the signature verification fails.
///
/// ### TODO
///
/// Return a [Result] instead of panicking.
#[doc(hidden)]
#[deprecated(note = "use env.crypto().ed25519_verify(pk, msg, sig)")]
pub fn verify_sig_ed25519(&self, pk: &BytesN<32>, msg: &Bytes, sig: &BytesN<64>) {
let _ = internal::Env::verify_sig_ed25519(
self,
msg.to_object(),
pk.to_object(),
sig.to_object(),
);
self.crypto().ed25519_verify(pk, msg, sig);
}

/// Invokes a function of a contract that is registered in the [Env].
Expand Down
1 change: 1 addition & 0 deletions soroban-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ pub use envhidden::*;

pub mod accounts;
mod bytes;
pub mod crypto;
pub mod data;
pub mod deploy;
pub mod events;
Expand Down