From c17157e4105bc92f8841083db67a17ac39062749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Tue, 24 Nov 2020 19:04:42 +0100 Subject: [PATCH 1/4] Add mipsel to Github Actions CI setup --- .github/workflows/ci.yml | 4 ++++ mk/cargo.sh | 7 +++++++ mk/install-build-tools.sh | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1c59d83fe5..dda5338bb9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -174,6 +174,7 @@ jobs: - i686-pc-windows-msvc - i686-unknown-linux-gnu - i686-unknown-linux-musl + - mipsel-unknown-linux-gnu - x86_64-pc-windows-gnu - x86_64-pc-windows-msvc - x86_64-apple-darwin @@ -259,6 +260,9 @@ jobs: - target: i686-unknown-linux-musl host_os: ubuntu-18.04 + - target: mipsel-unknown-linux-gnu + host_os: ubuntu-18.04 + - target: x86_64-pc-windows-gnu host_os: windows-latest diff --git a/mk/cargo.sh b/mk/cargo.sh index 85a1cbc385..95c0f25614 100755 --- a/mk/cargo.sh +++ b/mk/cargo.sh @@ -20,6 +20,7 @@ IFS=$'\n\t' rustflags_self_contained="-Clink-self-contained=yes -Clinker=rust-lld" qemu_aarch64="qemu-aarch64 -L /usr/aarch64-linux-gnu" qemu_arm="qemu-arm -L /usr/arm-linux-gnueabihf" +qemu_mipsel="qemu-system-mipsel -L /usr/mipsel-linux-gnu" # Avoid putting the Android tools in `$PATH` because there are tools in this # directory like `clang` that would conflict with the same-named tools that may @@ -87,6 +88,12 @@ case $target in export AR_i686_unknown_linux_musl=llvm-ar-$llvm_version export CARGO_TARGET_I686_UNKNOWN_LINUX_MUSL_RUSTFLAGS="$rustflags_self_contained" ;; + mipsel-unknown-linux-gnu) + export CC_mipsel_unknown_linux_gnu=mipsel-linux-gnu-gcc + export AR_mipsel_unknown_linux_gnu=mipsel-linux-gnu-gcc-ar + export CARGO_TARGET_MIPSEL_UNKNOWN_LINUX_GNU_LINKER=mipsel-linux-gnu-gcc + export CARGO_TARGET_MIPSEL_UNKNOWN_LINUX_GNU_RUNNER="$qemu_mipsel" + ;; x86_64-unknown-linux-musl) export CC_x86_64_unknown_linux_musl=clang-$llvm_version export AR_x86_64_unknown_linux_musl=llvm-ar-$llvm_version diff --git a/mk/install-build-tools.sh b/mk/install-build-tools.sh index db50246e8d..459873cbea 100755 --- a/mk/install-build-tools.sh +++ b/mk/install-build-tools.sh @@ -65,6 +65,12 @@ case $target in --target=i686-unknown-linux-musl|--target=x86_64-unknown-linux-musl) use_clang=1 ;; +--target=mipsel-unknown-linux-gnu) + install_packages \ + qemu-user \ + gcc-mipsel-linux-gnu \ + libc6-dev-mipsel-cross + ;; --target=wasm32-unknown-unknown) # The version of wasm-bindgen-cli must match the wasm-bindgen version. wasm_bindgen_version=$(cargo metadata --format-version 1 | jq -r '.packages | map(select( .name == "wasm-bindgen")) | map(.version) | .[0]') From 53cfcd6ceecad90fde5995b3c073bd8a1af6d3e5 Mon Sep 17 00:00:00 2001 From: Bruno Tavares Date: Mon, 3 May 2021 23:19:56 -0300 Subject: [PATCH 2/4] Add mips support This commit ports the changes from https://github.com/briansmith/ring/pull/1181 into the updated codebase The following command is working: > cross build --target mipsel-unknown-linux-musl But tests are still not working: > cross test --target mipsel-unknown-linux-musl --- Cargo.toml | 2 + build.rs | 37 ++- crypto/fipsmodule/bn/asm/mips-mont.pl | 435 ++++++++++++++++++++++++++ include/ring-core/mips_arch.h | 40 +++ mk/cargo.sh | 7 + mk/install-build-tools.sh | 6 + src/arithmetic/bigint.rs | 36 ++- 7 files changed, 548 insertions(+), 15 deletions(-) create mode 100644 crypto/fipsmodule/bn/asm/mips-mont.pl create mode 100644 include/ring-core/mips_arch.h diff --git a/Cargo.toml b/Cargo.toml index 2d5e11c9b9..ae83fac0d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,6 +58,7 @@ include = [ "crypto/fipsmodule/bn/asm/x86-mont.pl", "crypto/fipsmodule/bn/asm/x86_64-mont.pl", "crypto/fipsmodule/bn/asm/x86_64-mont5.pl", + "crypto/fipsmodule/bn/asm/mips-mont.pl", "crypto/fipsmodule/bn/internal.h", "crypto/fipsmodule/bn/montgomery.c", "crypto/fipsmodule/bn/montgomery_inv.c", @@ -105,6 +106,7 @@ include = [ "examples/checkdigest.rs", "include/ring-core/aes.h", "include/ring-core/arm_arch.h", + "include/ring-core/mips_arch.h", "include/ring-core/base.h", "include/ring-core/check.h", "include/ring-core/cpu.h", diff --git a/build.rs b/build.rs index 64d78b8aa9..4663f13779 100644 --- a/build.rs +++ b/build.rs @@ -30,6 +30,8 @@ const X86: &str = "x86"; const X86_64: &str = "x86_64"; const AARCH64: &str = "aarch64"; const ARM: &str = "arm"; +const MIPS: &str = "mips"; +const MIPS64: &str = "mips64"; #[rustfmt::skip] const RING_SRCS: &[(&[&str], &str)] = &[ @@ -40,12 +42,12 @@ const RING_SRCS: &[(&[&str], &str)] = &[ (&[], "crypto/mem.c"), (&[], "crypto/poly1305/poly1305.c"), - (&[AARCH64, ARM, X86_64, X86], "crypto/crypto.c"), - (&[AARCH64, ARM, X86_64, X86], "crypto/curve25519/curve25519.c"), - (&[AARCH64, ARM, X86_64, X86], "crypto/fipsmodule/ec/ecp_nistz.c"), - (&[AARCH64, ARM, X86_64, X86], "crypto/fipsmodule/ec/gfp_p256.c"), - (&[AARCH64, ARM, X86_64, X86], "crypto/fipsmodule/ec/gfp_p384.c"), - (&[AARCH64, ARM, X86_64, X86], "crypto/fipsmodule/ec/p256.c"), + (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/crypto.c"), + (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/curve25519/curve25519.c"), + (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/fipsmodule/ec/ecp_nistz.c"), + (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/fipsmodule/ec/gfp_p256.c"), + (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/fipsmodule/ec/gfp_p384.c"), + (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/fipsmodule/ec/p256.c"), (&[X86_64, X86], "crypto/cpu-intel.c"), @@ -87,6 +89,8 @@ const RING_SRCS: &[(&[&str], &str)] = &[ (&[AARCH64], "crypto/chacha/asm/chacha-armv8.pl"), (&[AARCH64], "crypto/fipsmodule/modes/asm/ghash-neon-armv8.pl"), (&[AARCH64], SHA512_ARMV8), + + (&[MIPS, MIPS64], "crypto/fipsmodule/bn/asm/mips-mont.pl"), ]; const SHA256_X86_64: &str = "crypto/fipsmodule/sha/asm/sha256-x86_64.pl"; @@ -200,6 +204,20 @@ const ASM_TARGETS: &[AsmTarget] = &[ asm_extension: "S", preassemble: false, }, + AsmTarget { + oss: LINUX_ABI, + arch: "mips", + perlasm_format: "elf", + asm_extension: "S", + preassemble: false, + }, + AsmTarget { + oss: LINUX_ABI, + arch: "mips64", + perlasm_format: "elf", + asm_extension: "S", + preassemble: false, + }, AsmTarget { oss: MACOS_ABI, arch: "aarch64", @@ -309,6 +327,7 @@ fn ring_build_rs_main() { let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); let env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); + let endian = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap(); let (obj_ext, obj_opt) = if env == MSVC { (MSVC_OBJ_EXT, MSVC_OBJ_OPT) } else { @@ -322,6 +341,7 @@ fn ring_build_rs_main() { let target = Target { arch, + endian, os, env, obj_ext, @@ -373,6 +393,7 @@ fn pregenerate_asm_main() { struct Target { arch: String, + endian: String, os: String, env: String, obj_ext: &'static str, @@ -391,6 +412,10 @@ fn build_c_code(target: &Target, pregenerated: PathBuf, out_dir: &Path, ring_cor } } + if target.arch == "mips" && target.endian == "big" { + panic!("MIPS Big-Endian detected. Stoping compilation as BoringSSL code are not available for this platform"); + } + let asm_target = ASM_TARGETS.iter().find(|asm_target| { asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref()) }); diff --git a/crypto/fipsmodule/bn/asm/mips-mont.pl b/crypto/fipsmodule/bn/asm/mips-mont.pl new file mode 100644 index 0000000000..7158c3a8bc --- /dev/null +++ b/crypto/fipsmodule/bn/asm/mips-mont.pl @@ -0,0 +1,435 @@ +#! /usr/bin/env perl +# Copyright 2010-2018 The OpenSSL Project Authors. All Rights Reserved. +# +# Licensed under the Apache License 2.0 (the "License"). You may not use +# this file except in compliance with the License. You can obtain a copy +# in the file LICENSE in the source distribution or at +# https://www.openssl.org/source/license.html + +# +# ==================================================================== +# Written by Andy Polyakov for the OpenSSL +# project. The module is, however, dual licensed under OpenSSL and +# CRYPTOGAMS licenses depending on where you obtain it. For further +# details see http://www.openssl.org/~appro/cryptogams/. +# ==================================================================== + +# This module doesn't present direct interest for OpenSSL, because it +# doesn't provide better performance for longer keys, at least not on +# in-order-execution cores. While 512-bit RSA sign operations can be +# 65% faster in 64-bit mode, 1024-bit ones are only 15% faster, and +# 4096-bit ones are up to 15% slower. In 32-bit mode it varies from +# 16% improvement for 512-bit RSA sign to -33% for 4096-bit RSA +# verify:-( All comparisons are against bn_mul_mont-free assembler. +# The module might be of interest to embedded system developers, as +# the code is smaller than 1KB, yet offers >3x improvement on MIPS64 +# and 75-30% [less for longer keys] on MIPS32 over compiler-generated +# code. + +###################################################################### +# There is a number of MIPS ABI in use, O32 and N32/64 are most +# widely used. Then there is a new contender: NUBI. It appears that if +# one picks the latter, it's possible to arrange code in ABI neutral +# manner. Therefore let's stick to NUBI register layout: +# +($zero,$at,$t0,$t1,$t2)=map("\$$_",(0..2,24,25)); +($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11)); +($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8,$s9,$s10,$s11)=map("\$$_",(12..23)); +($gp,$tp,$sp,$fp,$ra)=map("\$$_",(3,28..31)); +# +# The return value is placed in $a0. Following coding rules facilitate +# interoperability: +# +# - never ever touch $tp, "thread pointer", former $gp; +# - copy return value to $t0, former $v0 [or to $a0 if you're adapting +# old code]; +# - on O32 populate $a4-$a7 with 'lw $aN,4*N($sp)' if necessary; +# +# For reference here is register layout for N32/64 MIPS ABIs: +# +# ($zero,$at,$v0,$v1)=map("\$$_",(0..3)); +# ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11)); +# ($t0,$t1,$t2,$t3,$t8,$t9)=map("\$$_",(12..15,24,25)); +# ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7)=map("\$$_",(16..23)); +# ($gp,$sp,$fp,$ra)=map("\$$_",(28..31)); + +# $output is the last argument if it looks like a file (it has an extension) +# $flavour is the first argument if it doesn't look like a file +$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef; +# supported flavours are o32,n32,64,nubi32,nubi64, default is o32 +$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : "o32"; + +if ($flavour =~ /64|n32/i) { + $PTR_ADD="daddu"; # incidentally works even on n32 + $PTR_SUB="dsubu"; # incidentally works even on n32 + $REG_S="sd"; + $REG_L="ld"; + $SZREG=8; +} else { + $PTR_ADD="addu"; + $PTR_SUB="subu"; + $REG_S="sw"; + $REG_L="lw"; + $SZREG=4; +} +$SAVED_REGS_MASK = ($flavour =~ /nubi/i) ? 0x00fff000 : 0x00ff0000; +# +# +# +###################################################################### + +$output and open STDOUT,">$output"; + +if ($flavour =~ /64|n32/i) { + $LD="ld"; + $ST="sd"; + $MULTU="dmultu"; + $ADDU="daddu"; + $SUBU="dsubu"; + $BNSZ=8; +} else { + $LD="lw"; + $ST="sw"; + $MULTU="multu"; + $ADDU="addu"; + $SUBU="subu"; + $BNSZ=4; +} + +# int bn_mul_mont( +$rp=$a0; # BN_ULONG *rp, +$ap=$a1; # const BN_ULONG *ap, +$bp=$a2; # const BN_ULONG *bp, +$np=$a3; # const BN_ULONG *np, +$n0=$a4; # const BN_ULONG *n0, +$num=$a5; # int num); + +$lo0=$a6; +$hi0=$a7; +$lo1=$t1; +$hi1=$t2; +$aj=$s0; +$bi=$s1; +$nj=$s2; +$tp=$s3; +$alo=$s4; +$ahi=$s5; +$nlo=$s6; +$nhi=$s7; +$tj=$s8; +$i=$s9; +$j=$s10; +$m1=$s11; + +$FRAMESIZE=14; + +$code=<<___; +#include + +.text + +.set noat +.set noreorder + +.align 5 +.globl bn_mul_mont +.ent bn_mul_mont +bn_mul_mont: +___ +$code.=<<___ if ($flavour =~ /o32/i); + lw $n0,16($sp) + lw $num,20($sp) +___ +$code.=<<___; + slt $at,$num,4 + bnez $at,1f + li $t0,0 + b bn_mul_mont_internal + nop +1: jr $ra + li $a0,0 +.end bn_mul_mont + +.align 5 +.ent bn_mul_mont_internal +bn_mul_mont_internal: + .frame $fp,$FRAMESIZE*$SZREG,$ra + .mask 0x40000000|$SAVED_REGS_MASK,-$SZREG + $PTR_SUB $sp,$FRAMESIZE*$SZREG + $REG_S $fp,($FRAMESIZE-1)*$SZREG($sp) + $REG_S $s11,($FRAMESIZE-2)*$SZREG($sp) + $REG_S $s10,($FRAMESIZE-3)*$SZREG($sp) + $REG_S $s9,($FRAMESIZE-4)*$SZREG($sp) + $REG_S $s8,($FRAMESIZE-5)*$SZREG($sp) + $REG_S $s7,($FRAMESIZE-6)*$SZREG($sp) + $REG_S $s6,($FRAMESIZE-7)*$SZREG($sp) + $REG_S $s5,($FRAMESIZE-8)*$SZREG($sp) + $REG_S $s4,($FRAMESIZE-9)*$SZREG($sp) +___ +$code.=<<___ if ($flavour =~ /nubi/i); + $REG_S $s3,($FRAMESIZE-10)*$SZREG($sp) + $REG_S $s2,($FRAMESIZE-11)*$SZREG($sp) + $REG_S $s1,($FRAMESIZE-12)*$SZREG($sp) + $REG_S $s0,($FRAMESIZE-13)*$SZREG($sp) +___ +$code.=<<___; + move $fp,$sp + + .set reorder + $LD $n0,0($n0) + $LD $bi,0($bp) # bp[0] + $LD $aj,0($ap) # ap[0] + $LD $nj,0($np) # np[0] + + $PTR_SUB $sp,2*$BNSZ # place for two extra words + sll $num,`log($BNSZ)/log(2)` + li $at,-4096 + $PTR_SUB $sp,$num + and $sp,$at + + $MULTU ($aj,$bi) + $LD $ahi,$BNSZ($ap) + $LD $nhi,$BNSZ($np) + mflo ($lo0,$aj,$bi) + mfhi ($hi0,$aj,$bi) + $MULTU ($lo0,$n0) + mflo ($m1,$lo0,$n0) + + $MULTU ($ahi,$bi) + mflo ($alo,$ahi,$bi) + mfhi ($ahi,$ahi,$bi) + + $MULTU ($nj,$m1) + mflo ($lo1,$nj,$m1) + mfhi ($hi1,$nj,$m1) + $MULTU ($nhi,$m1) + $ADDU $lo1,$lo0 + sltu $at,$lo1,$lo0 + $ADDU $hi1,$at + mflo ($nlo,$nhi,$m1) + mfhi ($nhi,$nhi,$m1) + + move $tp,$sp + li $j,2*$BNSZ +.align 4 +.L1st: + .set noreorder + $PTR_ADD $aj,$ap,$j + $PTR_ADD $nj,$np,$j + $LD $aj,($aj) + $LD $nj,($nj) + + $MULTU ($aj,$bi) + $ADDU $lo0,$alo,$hi0 + $ADDU $lo1,$nlo,$hi1 + sltu $at,$lo0,$hi0 + sltu $t0,$lo1,$hi1 + $ADDU $hi0,$ahi,$at + $ADDU $hi1,$nhi,$t0 + mflo ($alo,$aj,$bi) + mfhi ($ahi,$aj,$bi) + + $ADDU $lo1,$lo0 + sltu $at,$lo1,$lo0 + $MULTU ($nj,$m1) + $ADDU $hi1,$at + addu $j,$BNSZ + $ST $lo1,($tp) + sltu $t0,$j,$num + mflo ($nlo,$nj,$m1) + mfhi ($nhi,$nj,$m1) + + bnez $t0,.L1st + $PTR_ADD $tp,$BNSZ + .set reorder + + $ADDU $lo0,$alo,$hi0 + sltu $at,$lo0,$hi0 + $ADDU $hi0,$ahi,$at + + $ADDU $lo1,$nlo,$hi1 + sltu $t0,$lo1,$hi1 + $ADDU $hi1,$nhi,$t0 + $ADDU $lo1,$lo0 + sltu $at,$lo1,$lo0 + $ADDU $hi1,$at + + $ST $lo1,($tp) + + $ADDU $hi1,$hi0 + sltu $at,$hi1,$hi0 + $ST $hi1,$BNSZ($tp) + $ST $at,2*$BNSZ($tp) + + li $i,$BNSZ +.align 4 +.Louter: + $PTR_ADD $bi,$bp,$i + $LD $bi,($bi) + $LD $aj,($ap) + $LD $ahi,$BNSZ($ap) + $LD $tj,($sp) + + $MULTU ($aj,$bi) + $LD $nj,($np) + $LD $nhi,$BNSZ($np) + mflo ($lo0,$aj,$bi) + mfhi ($hi0,$aj,$bi) + $ADDU $lo0,$tj + $MULTU ($lo0,$n0) + sltu $at,$lo0,$tj + $ADDU $hi0,$at + mflo ($m1,$lo0,$n0) + + $MULTU ($ahi,$bi) + mflo ($alo,$ahi,$bi) + mfhi ($ahi,$ahi,$bi) + + $MULTU ($nj,$m1) + mflo ($lo1,$nj,$m1) + mfhi ($hi1,$nj,$m1) + + $MULTU ($nhi,$m1) + $ADDU $lo1,$lo0 + sltu $at,$lo1,$lo0 + $ADDU $hi1,$at + mflo ($nlo,$nhi,$m1) + mfhi ($nhi,$nhi,$m1) + + move $tp,$sp + li $j,2*$BNSZ + $LD $tj,$BNSZ($tp) +.align 4 +.Linner: + .set noreorder + $PTR_ADD $aj,$ap,$j + $PTR_ADD $nj,$np,$j + $LD $aj,($aj) + $LD $nj,($nj) + + $MULTU ($aj,$bi) + $ADDU $lo0,$alo,$hi0 + $ADDU $lo1,$nlo,$hi1 + sltu $at,$lo0,$hi0 + sltu $t0,$lo1,$hi1 + $ADDU $hi0,$ahi,$at + $ADDU $hi1,$nhi,$t0 + mflo ($alo,$aj,$bi) + mfhi ($ahi,$aj,$bi) + + $ADDU $lo0,$tj + addu $j,$BNSZ + $MULTU ($nj,$m1) + sltu $at,$lo0,$tj + $ADDU $lo1,$lo0 + $ADDU $hi0,$at + sltu $t0,$lo1,$lo0 + $LD $tj,2*$BNSZ($tp) + $ADDU $hi1,$t0 + sltu $at,$j,$num + mflo ($nlo,$nj,$m1) + mfhi ($nhi,$nj,$m1) + $ST $lo1,($tp) + bnez $at,.Linner + $PTR_ADD $tp,$BNSZ + .set reorder + + $ADDU $lo0,$alo,$hi0 + sltu $at,$lo0,$hi0 + $ADDU $hi0,$ahi,$at + $ADDU $lo0,$tj + sltu $t0,$lo0,$tj + $ADDU $hi0,$t0 + + $LD $tj,2*$BNSZ($tp) + $ADDU $lo1,$nlo,$hi1 + sltu $at,$lo1,$hi1 + $ADDU $hi1,$nhi,$at + $ADDU $lo1,$lo0 + sltu $t0,$lo1,$lo0 + $ADDU $hi1,$t0 + $ST $lo1,($tp) + + $ADDU $lo1,$hi1,$hi0 + sltu $hi1,$lo1,$hi0 + $ADDU $lo1,$tj + sltu $at,$lo1,$tj + $ADDU $hi1,$at + $ST $lo1,$BNSZ($tp) + $ST $hi1,2*$BNSZ($tp) + + addu $i,$BNSZ + sltu $t0,$i,$num + bnez $t0,.Louter + + .set noreorder + $PTR_ADD $tj,$sp,$num # &tp[num] + move $tp,$sp + move $ap,$sp + li $hi0,0 # clear borrow bit + +.align 4 +.Lsub: $LD $lo0,($tp) + $LD $lo1,($np) + $PTR_ADD $tp,$BNSZ + $PTR_ADD $np,$BNSZ + $SUBU $lo1,$lo0,$lo1 # tp[i]-np[i] + sgtu $at,$lo1,$lo0 + $SUBU $lo0,$lo1,$hi0 + sgtu $hi0,$lo0,$lo1 + $ST $lo0,($rp) + or $hi0,$at + sltu $at,$tp,$tj + bnez $at,.Lsub + $PTR_ADD $rp,$BNSZ + + $SUBU $hi0,$hi1,$hi0 # handle upmost overflow bit + move $tp,$sp + $PTR_SUB $rp,$num # restore rp + not $hi1,$hi0 + +.Lcopy: $LD $nj,($tp) # conditional move + $LD $aj,($rp) + $ST $zero,($tp) + $PTR_ADD $tp,$BNSZ + and $nj,$hi0 + and $aj,$hi1 + or $aj,$nj + sltu $at,$tp,$tj + $ST $aj,($rp) + bnez $at,.Lcopy + $PTR_ADD $rp,$BNSZ + + li $a0,1 + li $t0,1 + + .set noreorder + move $sp,$fp + $REG_L $fp,($FRAMESIZE-1)*$SZREG($sp) + $REG_L $s11,($FRAMESIZE-2)*$SZREG($sp) + $REG_L $s10,($FRAMESIZE-3)*$SZREG($sp) + $REG_L $s9,($FRAMESIZE-4)*$SZREG($sp) + $REG_L $s8,($FRAMESIZE-5)*$SZREG($sp) + $REG_L $s7,($FRAMESIZE-6)*$SZREG($sp) + $REG_L $s6,($FRAMESIZE-7)*$SZREG($sp) + $REG_L $s5,($FRAMESIZE-8)*$SZREG($sp) + $REG_L $s4,($FRAMESIZE-9)*$SZREG($sp) +___ +$code.=<<___ if ($flavour =~ /nubi/i); + $REG_L $s3,($FRAMESIZE-10)*$SZREG($sp) + $REG_L $s2,($FRAMESIZE-11)*$SZREG($sp) + $REG_L $s1,($FRAMESIZE-12)*$SZREG($sp) + $REG_L $s0,($FRAMESIZE-13)*$SZREG($sp) +___ +$code.=<<___; + jr $ra + $PTR_ADD $sp,$FRAMESIZE*$SZREG +.end bn_mul_mont_internal +.rdata +.asciiz "Montgomery Multiplication for MIPS, CRYPTOGAMS by " +___ + +$code =~ s/\`([^\`]*)\`/eval $1/gem; + +print $code; +close STDOUT or die "error closing STDOUT"; \ No newline at end of file diff --git a/include/ring-core/mips_arch.h b/include/ring-core/mips_arch.h new file mode 100644 index 0000000000..e7562b8d8b --- /dev/null +++ b/include/ring-core/mips_arch.h @@ -0,0 +1,40 @@ +/* + * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef OSSL_CRYPTO_MIPS_ARCH_H +# define OSSL_CRYPTO_MIPS_ARCH_H + +# if (defined(__mips_smartmips) || defined(_MIPS_ARCH_MIPS32R3) || \ + defined(_MIPS_ARCH_MIPS32R5) || defined(_MIPS_ARCH_MIPS32R6)) \ + && !defined(_MIPS_ARCH_MIPS32R2) +# define _MIPS_ARCH_MIPS32R2 +# endif + +# if (defined(_MIPS_ARCH_MIPS64R3) || defined(_MIPS_ARCH_MIPS64R5) || \ + defined(_MIPS_ARCH_MIPS64R6)) \ + && !defined(_MIPS_ARCH_MIPS64R2) +# define _MIPS_ARCH_MIPS64R2 +# endif + +# if defined(_MIPS_ARCH_MIPS64R6) +# define dmultu(rs,rt) +# define mflo(rd,rs,rt) dmulu rd,rs,rt +# define mfhi(rd,rs,rt) dmuhu rd,rs,rt +# elif defined(_MIPS_ARCH_MIPS32R6) +# define multu(rs,rt) +# define mflo(rd,rs,rt) mulu rd,rs,rt +# define mfhi(rd,rs,rt) muhu rd,rs,rt +# else +# define dmultu(rs,rt) dmultu rs,rt +# define multu(rs,rt) multu rs,rt +# define mflo(rd,rs,rt) mflo rd +# define mfhi(rd,rs,rt) mfhi rd +# endif + +#endif \ No newline at end of file diff --git a/mk/cargo.sh b/mk/cargo.sh index 95c0f25614..9a1b16d1af 100755 --- a/mk/cargo.sh +++ b/mk/cargo.sh @@ -21,6 +21,7 @@ rustflags_self_contained="-Clink-self-contained=yes -Clinker=rust-lld" qemu_aarch64="qemu-aarch64 -L /usr/aarch64-linux-gnu" qemu_arm="qemu-arm -L /usr/arm-linux-gnueabihf" qemu_mipsel="qemu-system-mipsel -L /usr/mipsel-linux-gnu" +qemu_mips64el="qemu-system-mips64el -L /usr/mips64el-linux-gnu" # Avoid putting the Android tools in `$PATH` because there are tools in this # directory like `clang` that would conflict with the same-named tools that may @@ -94,6 +95,12 @@ case $target in export CARGO_TARGET_MIPSEL_UNKNOWN_LINUX_GNU_LINKER=mipsel-linux-gnu-gcc export CARGO_TARGET_MIPSEL_UNKNOWN_LINUX_GNU_RUNNER="$qemu_mipsel" ;; + mips64el-unknown-linux-gnuabi64) + export CC_mips64el_unknown_linux_gnuabi64=mips64el-linux-gnu-gcc + export AR_mips64el_unknown_linux_gnuabi64=mips64el-linux-gnu-gcc-ar + export CARGO_TARGET_MIPS64EL_UNKNOWN_LINUX_GNUABI64_LINKER=mips64el-linux-gnu-gcc + export CARGO_TARGET_MIPS64EL_UNKNOWN_LINUX_GNUABI64_RUNNER="$qemu_mips64el" + ;; x86_64-unknown-linux-musl) export CC_x86_64_unknown_linux_musl=clang-$llvm_version export AR_x86_64_unknown_linux_musl=llvm-ar-$llvm_version diff --git a/mk/install-build-tools.sh b/mk/install-build-tools.sh index 459873cbea..87f2dd1921 100755 --- a/mk/install-build-tools.sh +++ b/mk/install-build-tools.sh @@ -71,6 +71,12 @@ case $target in gcc-mipsel-linux-gnu \ libc6-dev-mipsel-cross ;; +--target=mips64el-unknown-linux-gnu) + install_packages \ + qemu-user \ + gcc-mips64el-linux-gnu \ + libc6-dev-mips64el-cross + ;; --target=wasm32-unknown-unknown) # The version of wasm-bindgen-cli must match the wasm-bindgen version. wasm_bindgen_version=$(cargo metadata --format-version 1 | jq -r '.packages | map(select( .name == "wasm-bindgen")) | map(.version) | .[0]') diff --git a/src/arithmetic/bigint.rs b/src/arithmetic/bigint.rs index 028be1a4bf..44a1cf0695 100644 --- a/src/arithmetic/bigint.rs +++ b/src/arithmetic/bigint.rs @@ -1194,7 +1194,9 @@ fn limbs_mont_mul(r: &mut [Limb], a: &[Limb], m: &[Limb], n0: &N0) { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86" + target_arch = "x86", + target_arch = "mips", + target_arch = "mips64", ))] unsafe { bn_mul_mont( @@ -1211,7 +1213,9 @@ fn limbs_mont_mul(r: &mut [Limb], a: &[Limb], m: &[Limb], n0: &N0) { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86" + target_arch = "x86", + target_arch = "mips", + target_arch = "mips64", )))] { let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; @@ -1251,7 +1255,9 @@ fn limbs_from_mont_in_place(r: &mut [Limb], tmp: &mut [Limb], m: &[Limb], n0: &N target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86" + target_arch = "x86", + target_arch = "mips", + target_arch = "mips64", )))] fn limbs_mul(r: &mut [Limb], a: &[Limb], b: &[Limb]) { debug_assert_eq!(r.len(), 2 * a.len()); @@ -1282,7 +1288,9 @@ fn limbs_mont_product(r: &mut [Limb], a: &[Limb], b: &[Limb], m: &[Limb], n0: &N target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86" + target_arch = "x86", + target_arch = "mips", + target_arch = "mips64", ))] unsafe { bn_mul_mont( @@ -1299,7 +1307,9 @@ fn limbs_mont_product(r: &mut [Limb], a: &[Limb], b: &[Limb], m: &[Limb], n0: &N target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86" + target_arch = "x86", + target_arch = "mips", + target_arch = "mips64", )))] { let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; @@ -1316,7 +1326,9 @@ fn limbs_mont_square(r: &mut [Limb], m: &[Limb], n0: &N0) { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86" + target_arch = "x86", + target_arch = "mips", + target_arch = "mips64", ))] unsafe { bn_mul_mont( @@ -1333,7 +1345,9 @@ fn limbs_mont_square(r: &mut [Limb], m: &[Limb], n0: &N0) { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86" + target_arch = "x86", + target_arch = "mips", + target_arch = "mips64", )))] { let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; @@ -1347,7 +1361,9 @@ fn limbs_mont_square(r: &mut [Limb], m: &[Limb], n0: &N0) { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86" + target_arch = "x86", + target_arch = "mips", + target_arch = "mips64", ))] prefixed_extern! { // `r` and/or 'a' and/or 'b' may alias. @@ -1367,7 +1383,9 @@ prefixed_extern! { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86" + target_arch = "x86", + target_arch = "mips", + target_arch = "mips64", )) ))] prefixed_extern! { From bd27ae2c87237b61e3fd73dfbbc020e21f0cbf9c Mon Sep 17 00:00:00 2001 From: Bruno Tavares Date: Mon, 3 May 2021 23:57:50 -0300 Subject: [PATCH 3/4] Remove changes based on GitHub feedback --- Cargo.toml | 2 - build.rs | 41 +-- crypto/fipsmodule/bn/asm/mips-mont.pl | 435 -------------------------- include/ring-core/mips_arch.h | 40 --- src/arithmetic/bigint.rs | 36 +-- 5 files changed, 19 insertions(+), 535 deletions(-) delete mode 100644 crypto/fipsmodule/bn/asm/mips-mont.pl delete mode 100644 include/ring-core/mips_arch.h diff --git a/Cargo.toml b/Cargo.toml index ae83fac0d6..2d5e11c9b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,6 @@ include = [ "crypto/fipsmodule/bn/asm/x86-mont.pl", "crypto/fipsmodule/bn/asm/x86_64-mont.pl", "crypto/fipsmodule/bn/asm/x86_64-mont5.pl", - "crypto/fipsmodule/bn/asm/mips-mont.pl", "crypto/fipsmodule/bn/internal.h", "crypto/fipsmodule/bn/montgomery.c", "crypto/fipsmodule/bn/montgomery_inv.c", @@ -106,7 +105,6 @@ include = [ "examples/checkdigest.rs", "include/ring-core/aes.h", "include/ring-core/arm_arch.h", - "include/ring-core/mips_arch.h", "include/ring-core/base.h", "include/ring-core/check.h", "include/ring-core/cpu.h", diff --git a/build.rs b/build.rs index 4663f13779..ed4b212759 100644 --- a/build.rs +++ b/build.rs @@ -30,8 +30,6 @@ const X86: &str = "x86"; const X86_64: &str = "x86_64"; const AARCH64: &str = "aarch64"; const ARM: &str = "arm"; -const MIPS: &str = "mips"; -const MIPS64: &str = "mips64"; #[rustfmt::skip] const RING_SRCS: &[(&[&str], &str)] = &[ @@ -41,13 +39,13 @@ const RING_SRCS: &[(&[&str], &str)] = &[ (&[], "crypto/limbs/limbs.c"), (&[], "crypto/mem.c"), (&[], "crypto/poly1305/poly1305.c"), + (&[], "crypto/curve25519/curve25519.c"), + (&[], "crypto/fipsmodule/ec/ecp_nistz.c"), + (&[], "crypto/fipsmodule/ec/gfp_p256.c"), + (&[], "crypto/fipsmodule/ec/gfp_p384.c"), + (&[], "crypto/fipsmodule/ec/p256.c"), - (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/crypto.c"), - (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/curve25519/curve25519.c"), - (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/fipsmodule/ec/ecp_nistz.c"), - (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/fipsmodule/ec/gfp_p256.c"), - (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/fipsmodule/ec/gfp_p384.c"), - (&[AARCH64, ARM, MIPS, MIPS64, X86_64, X86], "crypto/fipsmodule/ec/p256.c"), + (&[AARCH64, X86_64, X86], "crypto/crypto.c"), (&[X86_64, X86], "crypto/cpu-intel.c"), @@ -89,8 +87,6 @@ const RING_SRCS: &[(&[&str], &str)] = &[ (&[AARCH64], "crypto/chacha/asm/chacha-armv8.pl"), (&[AARCH64], "crypto/fipsmodule/modes/asm/ghash-neon-armv8.pl"), (&[AARCH64], SHA512_ARMV8), - - (&[MIPS, MIPS64], "crypto/fipsmodule/bn/asm/mips-mont.pl"), ]; const SHA256_X86_64: &str = "crypto/fipsmodule/sha/asm/sha256-x86_64.pl"; @@ -204,20 +200,6 @@ const ASM_TARGETS: &[AsmTarget] = &[ asm_extension: "S", preassemble: false, }, - AsmTarget { - oss: LINUX_ABI, - arch: "mips", - perlasm_format: "elf", - asm_extension: "S", - preassemble: false, - }, - AsmTarget { - oss: LINUX_ABI, - arch: "mips64", - perlasm_format: "elf", - asm_extension: "S", - preassemble: false, - }, AsmTarget { oss: MACOS_ABI, arch: "aarch64", @@ -321,13 +303,16 @@ fn main() { fn ring_build_rs_main() { use std::env; + if env::var("CARGO_CFG_TARGET_ENDIAN").unwrap() == "big" { + panic!("Big-endian targets are not supported yet"); + } + let out_dir = env::var("OUT_DIR").unwrap(); let out_dir = PathBuf::from(out_dir); let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); let env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); - let endian = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap(); let (obj_ext, obj_opt) = if env == MSVC { (MSVC_OBJ_EXT, MSVC_OBJ_OPT) } else { @@ -341,7 +326,6 @@ fn ring_build_rs_main() { let target = Target { arch, - endian, os, env, obj_ext, @@ -393,7 +377,6 @@ fn pregenerate_asm_main() { struct Target { arch: String, - endian: String, os: String, env: String, obj_ext: &'static str, @@ -412,10 +395,6 @@ fn build_c_code(target: &Target, pregenerated: PathBuf, out_dir: &Path, ring_cor } } - if target.arch == "mips" && target.endian == "big" { - panic!("MIPS Big-Endian detected. Stoping compilation as BoringSSL code are not available for this platform"); - } - let asm_target = ASM_TARGETS.iter().find(|asm_target| { asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref()) }); diff --git a/crypto/fipsmodule/bn/asm/mips-mont.pl b/crypto/fipsmodule/bn/asm/mips-mont.pl deleted file mode 100644 index 7158c3a8bc..0000000000 --- a/crypto/fipsmodule/bn/asm/mips-mont.pl +++ /dev/null @@ -1,435 +0,0 @@ -#! /usr/bin/env perl -# Copyright 2010-2018 The OpenSSL Project Authors. All Rights Reserved. -# -# Licensed under the Apache License 2.0 (the "License"). You may not use -# this file except in compliance with the License. You can obtain a copy -# in the file LICENSE in the source distribution or at -# https://www.openssl.org/source/license.html - -# -# ==================================================================== -# Written by Andy Polyakov for the OpenSSL -# project. The module is, however, dual licensed under OpenSSL and -# CRYPTOGAMS licenses depending on where you obtain it. For further -# details see http://www.openssl.org/~appro/cryptogams/. -# ==================================================================== - -# This module doesn't present direct interest for OpenSSL, because it -# doesn't provide better performance for longer keys, at least not on -# in-order-execution cores. While 512-bit RSA sign operations can be -# 65% faster in 64-bit mode, 1024-bit ones are only 15% faster, and -# 4096-bit ones are up to 15% slower. In 32-bit mode it varies from -# 16% improvement for 512-bit RSA sign to -33% for 4096-bit RSA -# verify:-( All comparisons are against bn_mul_mont-free assembler. -# The module might be of interest to embedded system developers, as -# the code is smaller than 1KB, yet offers >3x improvement on MIPS64 -# and 75-30% [less for longer keys] on MIPS32 over compiler-generated -# code. - -###################################################################### -# There is a number of MIPS ABI in use, O32 and N32/64 are most -# widely used. Then there is a new contender: NUBI. It appears that if -# one picks the latter, it's possible to arrange code in ABI neutral -# manner. Therefore let's stick to NUBI register layout: -# -($zero,$at,$t0,$t1,$t2)=map("\$$_",(0..2,24,25)); -($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11)); -($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8,$s9,$s10,$s11)=map("\$$_",(12..23)); -($gp,$tp,$sp,$fp,$ra)=map("\$$_",(3,28..31)); -# -# The return value is placed in $a0. Following coding rules facilitate -# interoperability: -# -# - never ever touch $tp, "thread pointer", former $gp; -# - copy return value to $t0, former $v0 [or to $a0 if you're adapting -# old code]; -# - on O32 populate $a4-$a7 with 'lw $aN,4*N($sp)' if necessary; -# -# For reference here is register layout for N32/64 MIPS ABIs: -# -# ($zero,$at,$v0,$v1)=map("\$$_",(0..3)); -# ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11)); -# ($t0,$t1,$t2,$t3,$t8,$t9)=map("\$$_",(12..15,24,25)); -# ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7)=map("\$$_",(16..23)); -# ($gp,$sp,$fp,$ra)=map("\$$_",(28..31)); - -# $output is the last argument if it looks like a file (it has an extension) -# $flavour is the first argument if it doesn't look like a file -$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef; -# supported flavours are o32,n32,64,nubi32,nubi64, default is o32 -$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : "o32"; - -if ($flavour =~ /64|n32/i) { - $PTR_ADD="daddu"; # incidentally works even on n32 - $PTR_SUB="dsubu"; # incidentally works even on n32 - $REG_S="sd"; - $REG_L="ld"; - $SZREG=8; -} else { - $PTR_ADD="addu"; - $PTR_SUB="subu"; - $REG_S="sw"; - $REG_L="lw"; - $SZREG=4; -} -$SAVED_REGS_MASK = ($flavour =~ /nubi/i) ? 0x00fff000 : 0x00ff0000; -# -# -# -###################################################################### - -$output and open STDOUT,">$output"; - -if ($flavour =~ /64|n32/i) { - $LD="ld"; - $ST="sd"; - $MULTU="dmultu"; - $ADDU="daddu"; - $SUBU="dsubu"; - $BNSZ=8; -} else { - $LD="lw"; - $ST="sw"; - $MULTU="multu"; - $ADDU="addu"; - $SUBU="subu"; - $BNSZ=4; -} - -# int bn_mul_mont( -$rp=$a0; # BN_ULONG *rp, -$ap=$a1; # const BN_ULONG *ap, -$bp=$a2; # const BN_ULONG *bp, -$np=$a3; # const BN_ULONG *np, -$n0=$a4; # const BN_ULONG *n0, -$num=$a5; # int num); - -$lo0=$a6; -$hi0=$a7; -$lo1=$t1; -$hi1=$t2; -$aj=$s0; -$bi=$s1; -$nj=$s2; -$tp=$s3; -$alo=$s4; -$ahi=$s5; -$nlo=$s6; -$nhi=$s7; -$tj=$s8; -$i=$s9; -$j=$s10; -$m1=$s11; - -$FRAMESIZE=14; - -$code=<<___; -#include - -.text - -.set noat -.set noreorder - -.align 5 -.globl bn_mul_mont -.ent bn_mul_mont -bn_mul_mont: -___ -$code.=<<___ if ($flavour =~ /o32/i); - lw $n0,16($sp) - lw $num,20($sp) -___ -$code.=<<___; - slt $at,$num,4 - bnez $at,1f - li $t0,0 - b bn_mul_mont_internal - nop -1: jr $ra - li $a0,0 -.end bn_mul_mont - -.align 5 -.ent bn_mul_mont_internal -bn_mul_mont_internal: - .frame $fp,$FRAMESIZE*$SZREG,$ra - .mask 0x40000000|$SAVED_REGS_MASK,-$SZREG - $PTR_SUB $sp,$FRAMESIZE*$SZREG - $REG_S $fp,($FRAMESIZE-1)*$SZREG($sp) - $REG_S $s11,($FRAMESIZE-2)*$SZREG($sp) - $REG_S $s10,($FRAMESIZE-3)*$SZREG($sp) - $REG_S $s9,($FRAMESIZE-4)*$SZREG($sp) - $REG_S $s8,($FRAMESIZE-5)*$SZREG($sp) - $REG_S $s7,($FRAMESIZE-6)*$SZREG($sp) - $REG_S $s6,($FRAMESIZE-7)*$SZREG($sp) - $REG_S $s5,($FRAMESIZE-8)*$SZREG($sp) - $REG_S $s4,($FRAMESIZE-9)*$SZREG($sp) -___ -$code.=<<___ if ($flavour =~ /nubi/i); - $REG_S $s3,($FRAMESIZE-10)*$SZREG($sp) - $REG_S $s2,($FRAMESIZE-11)*$SZREG($sp) - $REG_S $s1,($FRAMESIZE-12)*$SZREG($sp) - $REG_S $s0,($FRAMESIZE-13)*$SZREG($sp) -___ -$code.=<<___; - move $fp,$sp - - .set reorder - $LD $n0,0($n0) - $LD $bi,0($bp) # bp[0] - $LD $aj,0($ap) # ap[0] - $LD $nj,0($np) # np[0] - - $PTR_SUB $sp,2*$BNSZ # place for two extra words - sll $num,`log($BNSZ)/log(2)` - li $at,-4096 - $PTR_SUB $sp,$num - and $sp,$at - - $MULTU ($aj,$bi) - $LD $ahi,$BNSZ($ap) - $LD $nhi,$BNSZ($np) - mflo ($lo0,$aj,$bi) - mfhi ($hi0,$aj,$bi) - $MULTU ($lo0,$n0) - mflo ($m1,$lo0,$n0) - - $MULTU ($ahi,$bi) - mflo ($alo,$ahi,$bi) - mfhi ($ahi,$ahi,$bi) - - $MULTU ($nj,$m1) - mflo ($lo1,$nj,$m1) - mfhi ($hi1,$nj,$m1) - $MULTU ($nhi,$m1) - $ADDU $lo1,$lo0 - sltu $at,$lo1,$lo0 - $ADDU $hi1,$at - mflo ($nlo,$nhi,$m1) - mfhi ($nhi,$nhi,$m1) - - move $tp,$sp - li $j,2*$BNSZ -.align 4 -.L1st: - .set noreorder - $PTR_ADD $aj,$ap,$j - $PTR_ADD $nj,$np,$j - $LD $aj,($aj) - $LD $nj,($nj) - - $MULTU ($aj,$bi) - $ADDU $lo0,$alo,$hi0 - $ADDU $lo1,$nlo,$hi1 - sltu $at,$lo0,$hi0 - sltu $t0,$lo1,$hi1 - $ADDU $hi0,$ahi,$at - $ADDU $hi1,$nhi,$t0 - mflo ($alo,$aj,$bi) - mfhi ($ahi,$aj,$bi) - - $ADDU $lo1,$lo0 - sltu $at,$lo1,$lo0 - $MULTU ($nj,$m1) - $ADDU $hi1,$at - addu $j,$BNSZ - $ST $lo1,($tp) - sltu $t0,$j,$num - mflo ($nlo,$nj,$m1) - mfhi ($nhi,$nj,$m1) - - bnez $t0,.L1st - $PTR_ADD $tp,$BNSZ - .set reorder - - $ADDU $lo0,$alo,$hi0 - sltu $at,$lo0,$hi0 - $ADDU $hi0,$ahi,$at - - $ADDU $lo1,$nlo,$hi1 - sltu $t0,$lo1,$hi1 - $ADDU $hi1,$nhi,$t0 - $ADDU $lo1,$lo0 - sltu $at,$lo1,$lo0 - $ADDU $hi1,$at - - $ST $lo1,($tp) - - $ADDU $hi1,$hi0 - sltu $at,$hi1,$hi0 - $ST $hi1,$BNSZ($tp) - $ST $at,2*$BNSZ($tp) - - li $i,$BNSZ -.align 4 -.Louter: - $PTR_ADD $bi,$bp,$i - $LD $bi,($bi) - $LD $aj,($ap) - $LD $ahi,$BNSZ($ap) - $LD $tj,($sp) - - $MULTU ($aj,$bi) - $LD $nj,($np) - $LD $nhi,$BNSZ($np) - mflo ($lo0,$aj,$bi) - mfhi ($hi0,$aj,$bi) - $ADDU $lo0,$tj - $MULTU ($lo0,$n0) - sltu $at,$lo0,$tj - $ADDU $hi0,$at - mflo ($m1,$lo0,$n0) - - $MULTU ($ahi,$bi) - mflo ($alo,$ahi,$bi) - mfhi ($ahi,$ahi,$bi) - - $MULTU ($nj,$m1) - mflo ($lo1,$nj,$m1) - mfhi ($hi1,$nj,$m1) - - $MULTU ($nhi,$m1) - $ADDU $lo1,$lo0 - sltu $at,$lo1,$lo0 - $ADDU $hi1,$at - mflo ($nlo,$nhi,$m1) - mfhi ($nhi,$nhi,$m1) - - move $tp,$sp - li $j,2*$BNSZ - $LD $tj,$BNSZ($tp) -.align 4 -.Linner: - .set noreorder - $PTR_ADD $aj,$ap,$j - $PTR_ADD $nj,$np,$j - $LD $aj,($aj) - $LD $nj,($nj) - - $MULTU ($aj,$bi) - $ADDU $lo0,$alo,$hi0 - $ADDU $lo1,$nlo,$hi1 - sltu $at,$lo0,$hi0 - sltu $t0,$lo1,$hi1 - $ADDU $hi0,$ahi,$at - $ADDU $hi1,$nhi,$t0 - mflo ($alo,$aj,$bi) - mfhi ($ahi,$aj,$bi) - - $ADDU $lo0,$tj - addu $j,$BNSZ - $MULTU ($nj,$m1) - sltu $at,$lo0,$tj - $ADDU $lo1,$lo0 - $ADDU $hi0,$at - sltu $t0,$lo1,$lo0 - $LD $tj,2*$BNSZ($tp) - $ADDU $hi1,$t0 - sltu $at,$j,$num - mflo ($nlo,$nj,$m1) - mfhi ($nhi,$nj,$m1) - $ST $lo1,($tp) - bnez $at,.Linner - $PTR_ADD $tp,$BNSZ - .set reorder - - $ADDU $lo0,$alo,$hi0 - sltu $at,$lo0,$hi0 - $ADDU $hi0,$ahi,$at - $ADDU $lo0,$tj - sltu $t0,$lo0,$tj - $ADDU $hi0,$t0 - - $LD $tj,2*$BNSZ($tp) - $ADDU $lo1,$nlo,$hi1 - sltu $at,$lo1,$hi1 - $ADDU $hi1,$nhi,$at - $ADDU $lo1,$lo0 - sltu $t0,$lo1,$lo0 - $ADDU $hi1,$t0 - $ST $lo1,($tp) - - $ADDU $lo1,$hi1,$hi0 - sltu $hi1,$lo1,$hi0 - $ADDU $lo1,$tj - sltu $at,$lo1,$tj - $ADDU $hi1,$at - $ST $lo1,$BNSZ($tp) - $ST $hi1,2*$BNSZ($tp) - - addu $i,$BNSZ - sltu $t0,$i,$num - bnez $t0,.Louter - - .set noreorder - $PTR_ADD $tj,$sp,$num # &tp[num] - move $tp,$sp - move $ap,$sp - li $hi0,0 # clear borrow bit - -.align 4 -.Lsub: $LD $lo0,($tp) - $LD $lo1,($np) - $PTR_ADD $tp,$BNSZ - $PTR_ADD $np,$BNSZ - $SUBU $lo1,$lo0,$lo1 # tp[i]-np[i] - sgtu $at,$lo1,$lo0 - $SUBU $lo0,$lo1,$hi0 - sgtu $hi0,$lo0,$lo1 - $ST $lo0,($rp) - or $hi0,$at - sltu $at,$tp,$tj - bnez $at,.Lsub - $PTR_ADD $rp,$BNSZ - - $SUBU $hi0,$hi1,$hi0 # handle upmost overflow bit - move $tp,$sp - $PTR_SUB $rp,$num # restore rp - not $hi1,$hi0 - -.Lcopy: $LD $nj,($tp) # conditional move - $LD $aj,($rp) - $ST $zero,($tp) - $PTR_ADD $tp,$BNSZ - and $nj,$hi0 - and $aj,$hi1 - or $aj,$nj - sltu $at,$tp,$tj - $ST $aj,($rp) - bnez $at,.Lcopy - $PTR_ADD $rp,$BNSZ - - li $a0,1 - li $t0,1 - - .set noreorder - move $sp,$fp - $REG_L $fp,($FRAMESIZE-1)*$SZREG($sp) - $REG_L $s11,($FRAMESIZE-2)*$SZREG($sp) - $REG_L $s10,($FRAMESIZE-3)*$SZREG($sp) - $REG_L $s9,($FRAMESIZE-4)*$SZREG($sp) - $REG_L $s8,($FRAMESIZE-5)*$SZREG($sp) - $REG_L $s7,($FRAMESIZE-6)*$SZREG($sp) - $REG_L $s6,($FRAMESIZE-7)*$SZREG($sp) - $REG_L $s5,($FRAMESIZE-8)*$SZREG($sp) - $REG_L $s4,($FRAMESIZE-9)*$SZREG($sp) -___ -$code.=<<___ if ($flavour =~ /nubi/i); - $REG_L $s3,($FRAMESIZE-10)*$SZREG($sp) - $REG_L $s2,($FRAMESIZE-11)*$SZREG($sp) - $REG_L $s1,($FRAMESIZE-12)*$SZREG($sp) - $REG_L $s0,($FRAMESIZE-13)*$SZREG($sp) -___ -$code.=<<___; - jr $ra - $PTR_ADD $sp,$FRAMESIZE*$SZREG -.end bn_mul_mont_internal -.rdata -.asciiz "Montgomery Multiplication for MIPS, CRYPTOGAMS by " -___ - -$code =~ s/\`([^\`]*)\`/eval $1/gem; - -print $code; -close STDOUT or die "error closing STDOUT"; \ No newline at end of file diff --git a/include/ring-core/mips_arch.h b/include/ring-core/mips_arch.h deleted file mode 100644 index e7562b8d8b..0000000000 --- a/include/ring-core/mips_arch.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved. - * - * Licensed under the Apache License 2.0 (the "License"). You may not use - * this file except in compliance with the License. You can obtain a copy - * in the file LICENSE in the source distribution or at - * https://www.openssl.org/source/license.html - */ - -#ifndef OSSL_CRYPTO_MIPS_ARCH_H -# define OSSL_CRYPTO_MIPS_ARCH_H - -# if (defined(__mips_smartmips) || defined(_MIPS_ARCH_MIPS32R3) || \ - defined(_MIPS_ARCH_MIPS32R5) || defined(_MIPS_ARCH_MIPS32R6)) \ - && !defined(_MIPS_ARCH_MIPS32R2) -# define _MIPS_ARCH_MIPS32R2 -# endif - -# if (defined(_MIPS_ARCH_MIPS64R3) || defined(_MIPS_ARCH_MIPS64R5) || \ - defined(_MIPS_ARCH_MIPS64R6)) \ - && !defined(_MIPS_ARCH_MIPS64R2) -# define _MIPS_ARCH_MIPS64R2 -# endif - -# if defined(_MIPS_ARCH_MIPS64R6) -# define dmultu(rs,rt) -# define mflo(rd,rs,rt) dmulu rd,rs,rt -# define mfhi(rd,rs,rt) dmuhu rd,rs,rt -# elif defined(_MIPS_ARCH_MIPS32R6) -# define multu(rs,rt) -# define mflo(rd,rs,rt) mulu rd,rs,rt -# define mfhi(rd,rs,rt) muhu rd,rs,rt -# else -# define dmultu(rs,rt) dmultu rs,rt -# define multu(rs,rt) multu rs,rt -# define mflo(rd,rs,rt) mflo rd -# define mfhi(rd,rs,rt) mfhi rd -# endif - -#endif \ No newline at end of file diff --git a/src/arithmetic/bigint.rs b/src/arithmetic/bigint.rs index 44a1cf0695..028be1a4bf 100644 --- a/src/arithmetic/bigint.rs +++ b/src/arithmetic/bigint.rs @@ -1194,9 +1194,7 @@ fn limbs_mont_mul(r: &mut [Limb], a: &[Limb], m: &[Limb], n0: &N0) { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86", - target_arch = "mips", - target_arch = "mips64", + target_arch = "x86" ))] unsafe { bn_mul_mont( @@ -1213,9 +1211,7 @@ fn limbs_mont_mul(r: &mut [Limb], a: &[Limb], m: &[Limb], n0: &N0) { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86", - target_arch = "mips", - target_arch = "mips64", + target_arch = "x86" )))] { let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; @@ -1255,9 +1251,7 @@ fn limbs_from_mont_in_place(r: &mut [Limb], tmp: &mut [Limb], m: &[Limb], n0: &N target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86", - target_arch = "mips", - target_arch = "mips64", + target_arch = "x86" )))] fn limbs_mul(r: &mut [Limb], a: &[Limb], b: &[Limb]) { debug_assert_eq!(r.len(), 2 * a.len()); @@ -1288,9 +1282,7 @@ fn limbs_mont_product(r: &mut [Limb], a: &[Limb], b: &[Limb], m: &[Limb], n0: &N target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86", - target_arch = "mips", - target_arch = "mips64", + target_arch = "x86" ))] unsafe { bn_mul_mont( @@ -1307,9 +1299,7 @@ fn limbs_mont_product(r: &mut [Limb], a: &[Limb], b: &[Limb], m: &[Limb], n0: &N target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86", - target_arch = "mips", - target_arch = "mips64", + target_arch = "x86" )))] { let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; @@ -1326,9 +1316,7 @@ fn limbs_mont_square(r: &mut [Limb], m: &[Limb], n0: &N0) { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86", - target_arch = "mips", - target_arch = "mips64", + target_arch = "x86" ))] unsafe { bn_mul_mont( @@ -1345,9 +1333,7 @@ fn limbs_mont_square(r: &mut [Limb], m: &[Limb], n0: &N0) { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86", - target_arch = "mips", - target_arch = "mips64", + target_arch = "x86" )))] { let mut tmp = [0; 2 * MODULUS_MAX_LIMBS]; @@ -1361,9 +1347,7 @@ fn limbs_mont_square(r: &mut [Limb], m: &[Limb], n0: &N0) { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86", - target_arch = "mips", - target_arch = "mips64", + target_arch = "x86" ))] prefixed_extern! { // `r` and/or 'a' and/or 'b' may alias. @@ -1383,9 +1367,7 @@ prefixed_extern! { target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64", - target_arch = "x86", - target_arch = "mips", - target_arch = "mips64", + target_arch = "x86" )) ))] prefixed_extern! { From d80f17814262258b4a2c8e3b71de7329dd366d5b Mon Sep 17 00:00:00 2001 From: Bruno Tavares Date: Tue, 4 May 2021 00:13:22 -0300 Subject: [PATCH 4/4] Keep files only for the big 4 --- build.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index ed4b212759..84010f43a0 100644 --- a/build.rs +++ b/build.rs @@ -41,11 +41,11 @@ const RING_SRCS: &[(&[&str], &str)] = &[ (&[], "crypto/poly1305/poly1305.c"), (&[], "crypto/curve25519/curve25519.c"), (&[], "crypto/fipsmodule/ec/ecp_nistz.c"), - (&[], "crypto/fipsmodule/ec/gfp_p256.c"), - (&[], "crypto/fipsmodule/ec/gfp_p384.c"), (&[], "crypto/fipsmodule/ec/p256.c"), - (&[AARCH64, X86_64, X86], "crypto/crypto.c"), + (&[AARCH64, ARM, X86_64, X86], "crypto/crypto.c"), + (&[AARCH64, ARM, X86_64, X86], "crypto/fipsmodule/ec/gfp_p256.c"), + (&[AARCH64, ARM, X86_64, X86], "crypto/fipsmodule/ec/gfp_p384.c"), (&[X86_64, X86], "crypto/cpu-intel.c"),