rust-crypto icon indicating copy to clipboard operation
rust-crypto copied to clipboard

Add support for ARMv8

Open zacstewart opened this issue 9 years ago • 2 comments

Adds a definition of rust_crypto_util_fixed_time_eq_asm for ARMv8. It's exactly the same as for ARMv7, except that it uses ARMv8 names for registers (wN instead of rN).

[Fixes #383]

zacstewart avatar Aug 14 '16 16:08 zacstewart

I have just tried to compile this with clang but it didn't work. The section should be

#ifdef __aarch64__
uint32_t rust_crypto_util_fixed_time_eq_asm(uint8_t* lhsp, uint8_t* rhsp, size_t count) {
    if (count == 0) {
        return 1;
    }
    uint8_t result = 0;
    asm(
        " \
            1: \
            \
            ldrb w4, [%1]; \
            ldrb w5, [%2]; \
            eor w4, w4, w5; \
            orr %w0, %w0, w4; \
            \
            add %w1, %w1, #1; \
            add %w2, %w2, #1; \
            subs %w3, %w3, #1; \
            bne 1b; \
        "
        : "+&r" (result), "+&r" (lhsp), "+&r" (rhsp), "+&r" (count) // all input and output
        : // input
        : "w4", "w5", "cc" // clobbers
    );
    
    return result;
}
#endif

this compiled on my test system.

xanecs avatar Mar 01 '17 02:03 xanecs

rust_crypto_util_fixed_time_eq_asm is called in util.rs. I've modified a bit to work with ARMv8. Performance is same to the original assembly code.

pub fn fixed_time_eq(lhs: &[u8], rhs: &[u8]) -> bool {
    if lhs.len() != rhs.len() {
        false
    } else {
        // let count = lhs.len() as libc::size_t;
        // unsafe {
        //     let lhsp = lhs.get_unchecked(0);
        //     let rhsp = rhs.get_unchecked(0);
        //     rust_crypto_util_fixed_time_eq_asm(lhsp, rhsp, count) == 0
        // }
        // Replace with...
        lhs.iter().zip(rhs).all(|(a, b)| a == b)
    }
}

I'm using Rust 1.18

tungthanhnguyen avatar Jul 09 '17 08:07 tungthanhnguyen