Remove array splitting from polyfill::convert.

Lifetime elision infers the wrong bounds. The code could be fixed by using
explicit lifetime annotations, but it's safer to just avoid the issue
altogether. The problem doesn't seem to affect any of the current code; the
problem was only noticed when trying to use the removed code for new uses.
This commit is contained in:
Brian Smith 2019-07-01 12:32:30 -10:00
parent 284619bc44
commit 03b9d14d15
4 changed files with 18 additions and 48 deletions

View File

@ -22,13 +22,12 @@ use crate::{c, endian::*, polyfill::convert::*};
#[repr(C)]
pub struct Key([Block; KEY_BLOCKS]);
impl From<&'_ [u8; KEY_LEN]> for Key {
fn from(value: &[u8; KEY_LEN]) -> Self {
impl Key {
#[inline]
pub fn from(value: &[u8; KEY_LEN]) -> Self {
Self(<[Block; KEY_BLOCKS]>::from_(value))
}
}
impl Key {
#[inline] // Optimize away match on `counter`.
pub fn encrypt_in_place(&self, counter: Counter, in_out: &mut [u8]) {
unsafe {

View File

@ -34,7 +34,8 @@ use super::{
chacha20_poly1305::derive_poly1305_key,
poly1305, Nonce, Tag,
};
use crate::{constant_time, endian::*, error, polyfill::convert::*};
use crate::{constant_time, endian::*, error};
use core::convert::TryInto;
/// A key for sealing packets.
pub struct SealingKey {
@ -150,10 +151,10 @@ struct Key {
impl Key {
pub fn new(key_material: &[u8; KEY_LEN]) -> Key {
// The first half becomes K_2 and the second half becomes K_1.
let (k_2, k_1) = key_material.into_();
let (k_2, k_1) = key_material.split_at(chacha::KEY_LEN);
Key {
k_1: chacha::Key::from(k_1),
k_2: chacha::Key::from(k_2),
k_1: chacha::Key::from(k_1.try_into().unwrap()),
k_2: chacha::Key::from(k_2.try_into().unwrap()),
}
}
}

View File

@ -18,9 +18,7 @@ use super::{super::ops::*, ED25519_PUBLIC_KEY_LEN};
use crate::{
digest, error,
io::der,
pkcs8,
polyfill::convert::Into_,
rand,
pkcs8, rand,
signature::{self, KeyPair as SigningKeyPair},
};
use core::convert::TryInto;
@ -181,9 +179,8 @@ impl Ed25519KeyPair {
/// Returns the signature of the message `msg`.
pub fn sign(&self, msg: &[u8]) -> signature::Signature {
signature::Signature::new(|signature_bytes| {
let (signature_bytes, _unused) = signature_bytes.into_();
// Borrow `signature_bytes`.
let (signature_r, signature_s) = signature_bytes.into_();
let (signature_bytes, _unused) = signature_bytes.split_at_mut(ELEM_LEN + SCALAR_LEN);
let (signature_r, signature_s) = signature_bytes.split_at_mut(ELEM_LEN);
let nonce = {
let mut ctx = digest::Context::new(&digest::SHA512);
ctx.update(&self.private_prefix);
@ -196,11 +193,16 @@ impl Ed25519KeyPair {
unsafe {
GFp_x25519_ge_scalarmult_base(&mut r, &nonce);
}
*signature_r = r.into_encoded_point();
signature_r.copy_from_slice(&r.into_encoded_point());
let hram_digest = eddsa_digest(signature_r, &self.public_key.as_ref(), msg);
let hram = digest_scalar(hram_digest);
unsafe {
GFp_x25519_sc_muladd(signature_s, &hram, &self.private_scalar, &nonce);
GFp_x25519_sc_muladd(
signature_s.try_into().unwrap(),
&hram,
&self.private_scalar,
&nonce,
);
}
SIGNATURE_LEN
@ -260,5 +262,3 @@ static PKCS8_TEMPLATE: pkcs8::Template = pkcs8::Template {
curve_id_index: 0,
private_key_index: 0x10,
};
impl_array_split!(u8, SIGNATURE_LEN, signature::MAX_LEN - SIGNATURE_LEN);

View File

@ -36,33 +36,3 @@ where
T::from_(self)
}
}
macro_rules! impl_array_split {
($ty:ty, $first:expr, $second:expr) => {
#[allow(unused_qualifications)]
impl crate::polyfill::convert::From_<&[$ty; $first + $second]>
for (&[$ty; $first], &[$ty; $second])
{
#[inline]
fn from_(to_split: &[$ty; $first + $second]) -> Self {
let first: *const u8 = &to_split[0];
let split_at: *const u8 = &to_split[$first];
unsafe { (core::mem::transmute(first), core::mem::transmute(split_at)) }
}
}
#[allow(unused_qualifications)]
impl crate::polyfill::convert::From_<&mut [$ty; $first + $second]>
for (&mut [$ty; $first], &mut [$ty; $second])
{
#[inline]
fn from_(to_split: &mut [$ty; $first + $second]) -> Self {
let first: *mut u8 = &mut to_split[0];
let split_at: *mut u8 = &mut to_split[$first];
unsafe { (core::mem::transmute(first), core::mem::transmute(split_at)) }
}
}
};
}
impl_array_split!(u8, 32, 32);