jets: replace urcrypt with ares_crypto functions in ed jets

This commit is contained in:
Matthew LeVan 2023-12-20 13:52:50 -05:00
parent 8437982ea3
commit 70a3744218
2 changed files with 41 additions and 58 deletions

View File

@ -3,7 +3,7 @@ use crate::jets::bits::util::met;
use crate::jets::util::slot;
use crate::jets::{JetErr, Result};
use crate::noun::{IndirectAtom, Noun, D, NO, YES};
use urcrypt_sys::*;
use ares_crypto::ed25519::{ac_ed_puck, ac_ed_shar, ac_ed_sign, ac_ed_veri};
crate::gdb!();
@ -16,16 +16,13 @@ pub fn jet_puck(context: &mut Context, subject: Noun) -> Result {
}
unsafe {
// allocate 32 byte buffer on
let sed_buffer = &mut [0u8; 32] as *mut u8;
let sed_bytes = sed.as_bytes();
// we need to copy because the atom might be less than 32 bytes and urcrypt expects a
// 32-byte buffer
std::ptr::copy_nonoverlapping(sed_bytes.as_ptr(), sed_buffer, sed_bytes.len());
let mut sed_bytes = &mut [0u8; 32];
sed_bytes.copy_from_slice(&(sed.as_bytes())[0..32]);
let (mut pub_ida, pub_key) = IndirectAtom::new_raw_mut_bytes(stack, 32);
urcrypt_ed_puck(sed_buffer as *const u8, pub_key.as_mut_ptr());
let tmp = &mut [0u8; 32];
ac_ed_puck(&mut sed_bytes, tmp);
pub_key.copy_from_slice(tmp);
Ok(pub_ida.normalize_as_atom().as_noun())
}
@ -47,17 +44,19 @@ pub fn jet_shar(context: &mut Context, subject: Noun) -> Result {
}
unsafe {
let (_, public) = IndirectAtom::new_raw_mut_bytes(stack, 32);
let (_, secret) = IndirectAtom::new_raw_mut_bytes(stack, 32);
let public = &mut [0u8; 32];
let secret = &mut [0u8; 32];
let pub_bytes = pub_key.as_bytes();
let sec_bytes = sec_key.as_bytes();
public[0..pub_bytes.len()].copy_from_slice(pub_bytes);
secret[0..sec_bytes.len()].copy_from_slice(sec_bytes);
secret[0..pub_bytes.len()].copy_from_slice(sec_bytes);
let (mut shar_ida, shar) = IndirectAtom::new_raw_mut_bytes(stack, 32);
urcrypt_ed_shar(public.as_ptr(), secret.as_ptr(), shar.as_mut_ptr());
let tmp = &mut [0u8; 32];
ac_ed_shar(public, secret, tmp);
shar.copy_from_slice(tmp);
Ok(shar_ida.normalize_as_atom().as_noun())
}
@ -65,7 +64,7 @@ pub fn jet_shar(context: &mut Context, subject: Noun) -> Result {
pub fn jet_sign(context: &mut Context, subject: Noun) -> Result {
let stack = &mut context.stack;
let msg = slot(subject, 12)?.as_atom()?;
let mut msg = slot(subject, 12)?.as_atom()?;
let sed = slot(subject, 13)?.as_atom()?;
unsafe {
@ -73,59 +72,47 @@ pub fn jet_sign(context: &mut Context, subject: Noun) -> Result {
if sed_bytes.len() > 32 {
return Err(JetErr::Fail(Error::Deterministic(D(0))));
};
let seed = &mut [0u8; 32];
seed[0..sed_bytes.len()].copy_from_slice(sed_bytes);
let msg_bytes = &(msg.as_bytes())[0..met(3, msg)]; // drop trailing zeros
let (mut _seed_ida, seed) = IndirectAtom::new_raw_mut_bytes(stack, 32);
seed.copy_from_slice(sed_bytes);
let msg_len = met(3, msg);
let message = &mut (msg.as_mut_bytes())[0..msg_len]; // drop trailing zeros
let (mut sig_ida, sig) = IndirectAtom::new_raw_mut_bytes(stack, 64);
urcrypt_ed_sign(
msg_bytes.as_ptr(),
msg_bytes.len(),
seed.as_ptr(),
sig.as_mut_ptr(),
);
sig.reverse(); // LSB first
let tmp = &mut [0u8; 64];
ac_ed_sign(message, seed, tmp);
tmp.reverse();
sig.copy_from_slice(tmp);
Ok(sig_ida.normalize_as_atom().as_noun())
}
}
pub fn jet_veri(context: &mut Context, subject: Noun) -> Result {
let stack = &mut context.stack;
pub fn jet_veri(_context: &mut Context, subject: Noun) -> Result {
let sig = slot(subject, 12)?.as_atom()?;
let msg = slot(subject, 26)?.as_atom()?;
let puk = slot(subject, 27)?.as_atom()?;
unsafe {
// Both are size checked by Hoon, but without crashing
let sig_bytes = sig.as_bytes();
if sig_bytes.len() > 64 {
return Ok(NO);
};
// Both are size checked by Hoon, but without crashing
let sig_bytes = sig.as_bytes();
if sig_bytes.len() > 64 {
return Ok(NO);
};
let signature = &mut [0u8; 64];
signature[0..sig_bytes.len()].copy_from_slice(sig_bytes);
let pub_bytes = puk.as_bytes();
if pub_bytes.len() > 32 {
return Ok(NO);
};
let pub_bytes = puk.as_bytes();
if pub_bytes.len() > 32 {
return Ok(NO);
};
let public_key = &mut [0u8; 32];
public_key[0..pub_bytes.len()].copy_from_slice(pub_bytes);
let (mut _sig_ida, signature) = IndirectAtom::new_raw_mut_bytes(stack, 64);
signature.copy_from_slice(sig_bytes);
let (mut _pub_ida, public_key) = IndirectAtom::new_raw_mut_bytes(stack, 32);
public_key.copy_from_slice(pub_bytes);
let message = &(msg.as_bytes())[0..met(3, msg)]; // drop trailing zeros
let message = &(msg.as_bytes())[0..met(3, msg)]; // drop trailing zeros
let valid = ac_ed_veri(message, public_key, signature);
let valid = urcrypt_ed_veri(
message.as_ptr(),
message.len(),
public_key.as_ptr(),
signature.as_ptr(),
);
Ok(if valid { YES } else { NO })
}
Ok(if valid { YES } else { NO })
}
#[cfg(test)]

View File

@ -3,7 +3,6 @@ use crate::jets::bits::util::met;
use crate::jets::util::slot;
use crate::jets::{JetErr, Result};
use crate::noun::{IndirectAtom, Noun, D};
use urcrypt_sys::*;
use ares_crypto::sha::{ac_sha1, ac_shal, ac_shas, ac_shay};
crate::gdb!();
@ -15,7 +14,8 @@ pub fn jet_shas(context: &mut Context, subject: Noun) -> Result {
let mut ruz = slot(sam, 3)?.as_atom()?;
let sal_bytes = &(sal.as_bytes())[0..met(3, sal)]; // drop trailing zeros
let (mut _salt_ida, mut salt) = unsafe { IndirectAtom::new_raw_mut_bytes(stack, sal_bytes.len()) };
let (mut _salt_ida, mut salt) =
unsafe { IndirectAtom::new_raw_mut_bytes(stack, sal_bytes.len()) };
salt.copy_from_slice(sal_bytes);
let msg_len = met(3, ruz);
@ -23,11 +23,7 @@ pub fn jet_shas(context: &mut Context, subject: Noun) -> Result {
unsafe {
let (mut out_ida, mut out) = IndirectAtom::new_raw_mut_bytes(stack, 32);
ac_shas(
&mut message,
&mut salt,
&mut out,
);
ac_shas(&mut message, &mut salt, &mut out);
Ok(out_ida.normalize_as_atom().as_noun())
}
}