Skip to content

Commit

Permalink
Support for PowerPC big-endian
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Dec 12, 2023
1 parent f3aa3ed commit 4e6ac05
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ jobs:
- arm-linux-androideabi
- armv7-linux-androideabi
- aarch64-linux-android
- powerpc64-unknown-linux-gnu
- powerpc-unknown-linux-gnu
steps:
- uses: actions/checkout@v3
with:
Expand Down
17 changes: 14 additions & 3 deletions aws-lc-rs/src/aead/chacha20_poly1305_openssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ mod tests {
};
use crate::aead::Nonce;
use crate::cipher::chacha::ChaCha20Key;
use crate::endian::LittleEndian;
use crate::test;

#[test]
Expand All @@ -221,10 +222,20 @@ mod tests {
let chacha_key = chacha_key.as_slice();
let chacha_key_bytes: [u8; 32] = <[u8; 32]>::try_from(chacha_key).unwrap();
let chacha_key = ChaCha20Key::from(chacha_key_bytes);
let iv = Nonce::from(&[45u32, 897, 4567]);
let poly1305_key = derive_poly1305_key(&chacha_key, iv);
{
let iv = Nonce::from(&[45u32, 897, 4567]);
let poly1305_key = derive_poly1305_key(&chacha_key, iv);
assert_eq!(&expected_poly1305_key, &poly1305_key.key_and_nonce);
}

assert_eq!(&expected_poly1305_key, &poly1305_key.key_and_nonce);
{
let x = LittleEndian::from(45u32);
let y = LittleEndian::from(897);
let z = LittleEndian::from(4567);
let iv = Nonce::from(&[x, y, z]);
let poly1305_key = derive_poly1305_key(&chacha_key, iv);
assert_eq!(&expected_poly1305_key, &poly1305_key.key_and_nonce);
}
}

#[test]
Expand Down
29 changes: 22 additions & 7 deletions aws-lc-rs/src/aead/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR ISC

use crate::endian::{ArrayEncoding, BigEndian, Encoding};
use crate::endian::{ArrayEncoding, BigEndian, Encoding, LittleEndian};
use crate::error;
use crate::iv::FixedLength;
use std::mem::transmute_copy;

/// A nonce for a single AEAD opening or sealing operation.
///
Expand Down Expand Up @@ -51,21 +50,37 @@ impl From<&[u8; NONCE_LEN]> for Nonce {
}
}

#[allow(useless_deprecated)] // https://github.com/rust-lang/rust/issues/39935
#[deprecated]
impl From<&[u32; NONCE_LEN / 4]> for Nonce {
#[inline]
fn from(values: &[u32; NONCE_LEN / 4]) -> Self {
unsafe {
let bytes: [u8; NONCE_LEN] = transmute_copy(values);
Nonce(FixedLength::from(bytes))
let mut nonce = [LittleEndian::ZERO; NONCE_LEN / 4];
for i in 0..(NONCE_LEN / 4) {
nonce[i] = LittleEndian::from(values[i]);
}
Nonce::from(&nonce)
}
}

impl From<&[BigEndian<u32>; NONCE_LEN / 4]> for Nonce {
#[inline]
fn from(values: &[BigEndian<u32>; NONCE_LEN / 4]) -> Self {
Nonce(FixedLength::from(values.as_byte_array()))
}
}

impl From<&[LittleEndian<u32>; NONCE_LEN / 4]> for Nonce {
#[inline]
fn from(nonce: &[LittleEndian<u32>; NONCE_LEN / 4]) -> Self {
Nonce(FixedLength::from(nonce.as_byte_array()))
}
}

impl From<BigEndian<u32>> for Nonce {
#[inline]
fn from(number: BigEndian<u32>) -> Self {
let nonce = [BigEndian::ZERO, BigEndian::ZERO, number];
Nonce(FixedLength::from(*(nonce.as_byte_array())))
Nonce::from([BigEndian::ZERO, BigEndian::ZERO, number].as_byte_array())
}
}

Expand Down
7 changes: 7 additions & 0 deletions aws-lc-sys/builder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ fn get_cmake_config(manifest_dir: &PathBuf) -> cmake::Config {
fn prepare_cmake_build(manifest_dir: &PathBuf, build_prefix: String) -> cmake::Config {
let mut cmake_cfg = get_cmake_config(manifest_dir);

if ["powerpc64", "powerpc"]
.iter()
.any(|arch| target_arch().eq_ignore_ascii_case(arch))
{
cmake_cfg.define("ENABLE_EXPERIMENTAL_BIG_ENDIAN_SUPPORT", "1");
}

if OutputLibType::default() == OutputLibType::Dynamic {
cmake_cfg.define("BUILD_SHARED_LIBS", "1");
} else {
Expand Down

0 comments on commit 4e6ac05

Please sign in to comment.