ring icon indicating copy to clipboard operation
ring copied to clipboard

Add basic support for s390x

Open uweigand opened this issue 4 years ago • 5 comments
trafficstars

This adds basic support for Linux on s390x, using the fallback implementation of all algorithms and no assembler routines. This addresses https://github.com/briansmith/ring/issues/986 and makes "cargo test" fully pass on s390x.

There were two main changes I had to make to get this working:

  • Provide a bn_mul_mont fallback implementation. Note that while there is a fallback implementation on the Rust side for limbs_mont_mul, some elliptic curve code directly calls bn_mul_mont on the C side, where there is currently no fallback. However, given that we have a bn_from_montgomery_in_place fallback, it is straightforward to add bn_mul_mont (along the lines of the limbs_mont_mul fallback).
  • Support big-endian platforms. This consists of adding a number of endian access primitives in internal.h, and using them where required, currently in aes_nohw.c, poly1305.c, and the p256_scalar_bytes_from_limbs routine (for some reason, the p384 version of that routine is already endian-agnostic). I have chosen to use a plain C implementation of the primitives rather than bswap intrinsics, because that is easier to maintain, doesn't require platform #ifdef's, and still compiles to good code with any recent compiler.

@briansmith, please let me know if this approach makes sense to you or if this should be done differently.

Once this basic (unoptimized) support is in, we can then build on it by adding assembler optimizations (they're already present in OpenSSL, but would need to be copied over and adapted).

uweigand avatar May 17 '21 12:05 uweigand

Ping? Any comments on this approach?

uweigand avatar Jun 09 '21 11:06 uweigand

hi @uweigand @briansmith, i am also interested in getting ring supported for s390x. Let me know if i can contribute in some way. From this PR, I was able to build ring using cross build --release --target s390x-unknown-linux-gnu btw, how do we get the above tests to run on the PR?

vimalk78 avatar Sep 29 '21 07:09 vimalk78

ping @briansmith @uweigand

vimalk78 avatar Oct 14 '21 15:10 vimalk78

Hi @briansmith, let me try to make another attempt to get this going.

I've just updated the patch to use a different style of endian conversion, as you suggested. This keeps the CRYPTO_bswap4 implementation from boringssl and adds the corresponding CRYPTO_bswap8 implementation from there as well. The other endian-specific accessors are now all defined in term of CRYPTO_bswap4 / CRYPTO_bswap8 plus OPENSSL_memcpy (to handle any alignment issues if necessary).

Does this address your concerns?

If there's anything else I can do to help get this reviewed and merged, please let me know.

uweigand avatar Feb 21 '22 14:02 uweigand

Fixed merge conflict against current mainline. Any comments?

uweigand avatar May 04 '22 15:05 uweigand

Hi @briansmith , can this pr be merged now?

Xynnn007 avatar Nov 04 '22 08:11 Xynnn007

Rebased and fixed merge conflicts against current mainline.

uweigand avatar Nov 04 '22 09:11 uweigand

Added support for cross-building and testing as requested in https://github.com/briansmith/ring/issues/1555. Split into three logically distinct commits.

uweigand avatar Nov 10 '22 13:11 uweigand

PR updated after https://github.com/briansmith/ring/pull/1558 was merged.

uweigand avatar Nov 14 '22 21:11 uweigand

Reworked big-endian support to ensure no asm changes on any little-endian target.

uweigand avatar Nov 23 '22 15:11 uweigand

Hi @briansmith , could this PR be merged or there is something more to do to get it accepted and merged into main?

I successfully compiled it on s390x. Tests passed.

wojciechozga avatar Nov 25 '22 13:11 wojciechozga

This works well and can be the foundation of more big endian targets (like AIX). Any blockers?

ecnelises avatar Mar 02 '23 06:03 ecnelises

I am expecting that this PR is merged because many components of confidential containers, which support s390x, are written in rust. For example, Key Broker Service uses rustls, which depends on ring, to enable https.

tnakaike avatar Mar 16 '23 06:03 tnakaike

@briansmith We would appreciate it if you could take a look. Any blockers for this PR?

kiszk avatar May 14 '23 19:05 kiszk

As Ring is used by PRISMA we would depend on this patch to be able to use Prima on s390x. As we (Metaco and Ripple) offer multi-arch builds for our SW this would help us a lot. We are looking at moving more components to Rust so this crypto library being available in s390x would be very appreciated.

angelnu avatar Aug 31 '23 17:08 angelnu

@uweigand Could you (or whoever is working on this at IBM/RedHat) email me at [email protected] so we could coordinate how we could get this finished without it being blocked on me, and how s390x (and PPC?) support can be best maintained going forward. I have some ideas and I want to hear your ideas.

briansmith avatar Aug 31 '23 18:08 briansmith

@uweigand Could you (or whoever is working on this at IBM/RedHat) email me at [email protected] so we could coordinate how we could get this finished without it being blocked on me, and how s390x (and PPC?) support can be best maintained going forward. I have some ideas and I want to hear your ideas.

I've now reached out to you via email.

uweigand avatar Sep 01 '23 11:09 uweigand

Hi @uweigand could you please CC me ([email protected])? AIX stuff may be related, thanks.

ecnelises avatar Sep 01 '23 11:09 ecnelises

I am planning to do a release very soon, probably later this week. If you can update your PR then I will include it in the release. Thanks for your help here.

briansmith avatar Sep 26 '23 16:09 briansmith

I am planning to do a release very soon, probably later this week. If you can update your PR then I will include it in the release. Thanks for your help here.

Otherwise, if we do the 0.17 release without these changes, we can do a 0.17.1 with them this gets merged.

briansmith avatar Sep 26 '23 16:09 briansmith

I think I've addressed all comments above. Let me know if you would like me to change anything else.

uweigand avatar Sep 26 '23 18:09 uweigand

Codecov Report

Merging #1297 (3062477) into main (bad63fb) will decrease coverage by 0.03%. The diff coverage is 52.94%.

@@            Coverage Diff             @@
##             main    #1297      +/-   ##
==========================================
- Coverage   95.98%   95.96%   -0.03%     
==========================================
  Files         134      134              
  Lines       19921    19945      +24     
  Branches      199      199              
==========================================
+ Hits        19122    19141      +19     
- Misses        762      765       +3     
- Partials       37       39       +2     
Files Coverage Δ
crypto/fipsmodule/aes/aes_nohw.c 86.41% <100.00%> (-0.23%) :arrow_down:
src/lib.rs 34.61% <0.00%> (ø)
crypto/internal.h 70.37% <46.15%> (+1.99%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

codecov[bot] avatar Sep 26 '23 18:09 codecov[bot]

Note that you did all the work to enable s390x in CI but you didn't add s390x to the test, test-features, and coverage build matrices. We should do that so we have the right test coverage.

briansmith avatar Sep 26 '23 19:09 briansmith

Note that you did all the work to enable s390x in CI but you didn't add s390x to the test, test-features, and coverage build matrices. We should do that so we have the right test coverage.

Added to those three matrices.

uweigand avatar Sep 26 '23 20:09 uweigand

I will fix the --no-default-features build failure.

briansmith avatar Sep 26 '23 21:09 briansmith

Please rebase on top of main and resubmit.

briansmith avatar Sep 27 '23 00:09 briansmith

Why did the earlier version of this have changes to p256_shared.h but the newest one doesn't?

briansmith avatar Sep 27 '23 01:09 briansmith

Please rebase on top of main and resubmit.

Done.

Why did the earlier version of this have changes to p256_shared.h but the newest one doesn't?

Huh, I thought I added a comment but it seems to have disappeared. I've removed that change as re-testing showed it is no longer necessary in the current code base.

The change was to fix an endian issue in p256_scalar_bytes_from_limbs, but that function is now only called from the crypto/fipsmodule/ec/p256-nistz.c file, which in turn is only even built at all on the (little-endian) AARCH64 and X86_64 targets. So there is no need for this file to support big-endian platforms, and any code added there would be untested anyway.

uweigand avatar Sep 27 '23 06:09 uweigand

Good news: The refactoring to fix the --no-default-features build was merged into main.

Less good news: There's a merge conflict in crypto/internal.h from the latest BoringSSL merge. Also, PR #1663 moved the stuff in base.h to target.h. I will merge PR #1663 ASAP so you can rebase this on top of main for the release. I am hoping to finish the BoringSSL merge today so there shouldn't be any new conflicts.

briansmith avatar Sep 29 '23 20:09 briansmith

@briansmith: Rebased against current mainline to fix both merge conflicts.

uweigand avatar Sep 30 '23 00:09 uweigand