briansmith / ring

Safe, fast, small crypto using Rust
Other
3.64k stars 682 forks source link

Unknown target cpu : xtensa-esp32s3-espidf #2088

Open lorenzo-pirchio opened 3 weeks ago

lorenzo-pirchio commented 3 weeks ago

Hi, I'm working on a project on esp32s3, I need to use rustls but I have problems with the ring crate since xtensa is not supported . I'm using the esp32-idf-template (https://github.com/esp-rs/esp-idf-template), and I changed the depedencies adding the ones I needed. As suggested by @briansmith, I am using the latest commit on the main branch, checking if there is this change (https://github.com/briansmith/ring/commit/ed5b2a86cccfe30e670b33536fc7e494fb5e9e19), but It still doesn't work. To be sure that I was using the latest commit, I downloaded the repo and added it as a local dependency.

This is my cargo.toml:

`[package] name = "s3_rust" version = "0.1.0" edition = "2021" resolver = "2" rust-version = "1.71"

[[bin]] name = "s3_rust" harness = false # do not use the built in cargo test harness -> resolve rust-analyzer errors

[profile.release] opt-level = "s"

[profile.dev] debug = true # Symbols are nice and they don't increase the size on Flash opt-level = "z"

[features] default = ["std", "embassy", "esp-idf-svc/native"]

pio = ["esp-idf-svc/pio"] std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] alloc = ["esp-idf-svc/alloc"] nightly = ["esp-idf-svc/nightly"] experimental = ["esp-idf-svc/experimental"] embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"]

[dependencies] log = { version = "0.4", default-features = false } esp-idf-svc = { version = "0.48", default-features = false }

[target.'cfg(target_os="espidf")'.dependencies] ring = {path="C:/ring-main/ring-main", features =[ "less-safe-getrandom-espidf"] } rustls = { version = "0.23.7", default-features = false, features = ["ring", "logging", "std", "tls12"] }

[build-dependencies] embuild = "0.31.3" `

This is my config.toml: `[build] target = "xtensa-esp32s3-espidf"

[target.xtensa-esp32s3-espidf] linker = "ldproxy" runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110

[unstable] build-std = ["std", "panic_abort"]

[env] MCU="esp32s3" ESP_IDF_VERSION = "v5.1.3" CRATE_CC_NO_DEFAULTS = "1" `

This is what I get when I do cargo build:

warning: ring@0.17.8: cc1.exe: warning: C:\ring-main\ring-main\pregenerated: No such file or directory [-Wmissing-include-dirs] warning: ring@0.17.8: In file included from C:\ring-main\ring-main\include/ring-core/base.h:74, warning: ring@0.17.8: from C:\ring-main\ring-main\include/ring-core/mem.h:60, warning: ring@0.17.8: from C:\ring-main\ring-main\crypto/curve25519/curve25519.c:22: warning: ring@0.17.8: C:\ring-main\ring-main\include/ring-core/target.h:53:2: error: #error "Unknown target CPU" warning: ring@0.17.8: 53 | #error "Unknown target CPU" warning: ring@0.17.8: | ^~~~~ warning: ring@0.17.8: C:\ring-main\ring-main\include/ring-core/base.h:76:10: fatal error: ring_core_generated/prefix_symbols.h: No such file or directory warning: ring@0.17.8: 76 | #include <ring_core_generated/prefix_symbols.h> warning: ring@0.17.8: | ^~~~~~~~~~ warning: ring@0.17.8: compilation terminated.

If any of you know what is the problem, please help me. Thanks

briansmith commented 3 weeks ago

This is what I get when I do cargo build:

warning: ring@0.17.8: cc1.exe: warning: C:\ring-main\ring-main\pregenerated: No such file or directory [-Wmissing-include-dirs]

It looks like you're building ring from source, but not from within a Git checkout. Thus, build.rs doesn't see .git/ and so it is expecting some files in pregenerated/. But they are not there, because they only exist in the package that's uploaded to crates.io.

cargo build

What is the exact command line of cargo build. As you can see in BUILDING.md, you need to set TARGET_CC and TARGET_AR to the correct C compiler.

lorenzo-pirchio commented 3 weeks ago

Everytime I set from cmd:

And finally I do cargo build. I changed the dependencies in cargo.toml adding ring={git="https://github.com/briansmith/ring.git", features =[ "less-safe-getrandom-espidf"]} but I have all these warnings.

warning: ring@0.17.8: In file included from C:\Users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\include/ring-core/base.h:74, warning: ring@0.17.8: from C:\Users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\include/ring-core/mem.h:60, warning: ring@0.17.8: from C:\Users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto/curve25519/curve25519.c:22: warning: ring@0.17.8: C:\Users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\include/ring-core/target.h:53:2: error: #error "Unknown target CPU" warning: ring@0.17.8: #error "Unknown target CPU" warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: In file included from C:\Users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto/curve25519/internal.h:20, warning: ring@0.17.8: from C:\Users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto/curve25519/curve25519.c:24: warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:219:2: error: #error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT" warning: ring@0.17.8: #error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT" warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:232:15: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t value_barrier_w(crypto_word_t a) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:232:45: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t value_barrier_w(crypto_word_t a) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:244:15: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_msb_w(crypto_word_t a) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:244:49: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_msb_w(crypto_word_t a) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:249:15: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_is_zero_w(crypto_word_t a) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:249:53: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_is_zero_w(crypto_word_t a) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:264:15: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_is_nonzero_w(crypto_word_t a) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:264:56: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_is_nonzero_w(crypto_word_t a) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:269:15: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_eq_w(crypto_word_t a, warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:269:48: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_eq_w(crypto_word_t a, warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:270:48: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: crypto_word_t b) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:277:15: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_select_w(crypto_word_t mask, warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:277:52: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_select_w(crypto_word_t mask, warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:278:52: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: crypto_word_t a, warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:279:52: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: crypto_word_t b) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:291:46: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline uint8_t constant_time_select_8(crypto_word_t mask, uint8_t a, warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:308:59: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: const crypto_word_t mask) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h: In function 'constant_time_conditional_memcpy': warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:313:14: error: implicit declaration of function 'constant_time_select_8'; did you mean 'bssl_constant_time_test_main'? [-Werror=implicit-function-declaration] warning: ring@0.17.8: out[i] = constant_time_select_8(mask, in[i], out[i]); warning: ring@0.17.8: ^~~~~~ warning: ring@0.17.8: bssl_constant_time_test_main warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:313:14: error: nested extern declaration of 'constant_time_select_8' [-Werror=nested-externs] warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h: At top level: warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:322:59: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: const crypto_word_t mask) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h: In function 'constant_time_conditional_memxor': warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:327:15: error: implicit declaration of function 'value_barrier_w' [-Werror=implicit-function-declaration] warning: ring@0.17.8: out[i] ^= value_barrier_w(mask) & in[i]; warning: ring@0.17.8: ^~~~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:327:15: error: nested extern declaration of 'value_barrier_w' [-Werror=nested-externs] warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h: At top level: warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:359:15: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_declassify_w(crypto_word_t v) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: c:\users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto\internal.h:359:56: error: unknown type name 'crypto_word_t' warning: ring@0.17.8: static inline crypto_word_t constant_time_declassify_w(crypto_word_t v) { warning: ring@0.17.8: ^~~~~ warning: ring@0.17.8: C:\Users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto/curve25519/curve25519.c: In function 'x25519_ge_scalarmult_small_precomp': warning: ring@0.17.8: C:\Users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto/curve25519/curve25519.c:721:35: error: implicit declaration of function 'constant_time_eq_w'; did you mean 'bssl_constant_time_test_main'? [-Werror=implicit-function-declaration] warning: ring@0.17.8: cmov(&e, &multiples[j-1], 1&constant_time_eq_w(index, j)); warning: ring@0.17.8: ^~~~~~ warning: ring@0.17.8: bssl_constant_time_test_main warning: ring@0.17.8: C:\Users\lpirc.cargo\git\checkouts\ring-51509f604a4fd25b\70b59d6\crypto/curve25519/curve25519.c:721:35: error: nested extern declaration of 'constant_time_eq_w' [-Werror=nested-externs] warning: ring@0.17.8: cc1.exe: all warnings being treated as errors

error: failed to run custom build command for ring v0.17.8 (https://github.com/briansmith/ring.git#70b59d6e)

It's the same error as https://github.com/briansmith/ring/issues/2008#issue-2216277002

briansmith commented 3 weeks ago

What does xtensa-esp32s3-elf-gcc.exe --version output?

lorenzo-pirchio commented 3 weeks ago

This is xtensa-esp32s3-elf-gcc.exe --version output:

xtensa-esp32s3-elf-gcc.exe (crosstool-NG esp-2021r2-patch5) 8.4.0 Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I added this to target.h:

elif defined(XTENSA)

define OPENSSL_32_BIT

Now, when I compile it, I don't get any errors or warnings, but I don't know actually if it will work fine.

briansmith commented 3 weeks ago

In target.h, you can see:

// Versions of GCC before 10.0 didn't define `__ILP32__` for all 32-bit targets.
#elif defined(__MIPSEL__) || defined(__MIPSEB__) || defined(__PPC__) || defined(__powerpc__) || defined(__csky__)
#define OPENSSL_32_BIT

Ideally, you would upgrade to a newer version of GCC. Then it should "just work." Otherwise, you can find out what symbol to add to the above #elif and add it for xtensa. Note XTENSA isn't the right thing; the symbol we're looking for will start with '__' like the others in the list.

lorenzo-pirchio commented 3 weeks ago

Yes, I wrote Xtensa with '__' as the beginning and end as you said, but in markdown double underscores are used to format text as bold, as you can see in the previous message.

briansmith commented 2 weeks ago

OK, it looks like somebody just needs to submit the PR.

lorenzo-pirchio commented 2 weeks ago

Do you want me to submit a PR with the lines I added for xtensa?

briansmith commented 2 weeks ago

#elif defined(__MIPSEL__) || defined(__MIPSEB__) || defined(__PPC__) || defined(__powerpc__) || defined(__csky__)

^ Please extend that line.

Incidentally, is there no newer version of the xtensa SDK that lets you use GCC 10 or higher?

lorenzo-pirchio commented 1 week ago

I changed to GCC 12 because now I'm using esp-idf 5.1, which requires GCC 12. But unfortunately I have problems with the linker and I have errors about ring. Here they are: Errors.txt I have been trying to find possible solutions but I'm stuck doint this for a week. If you have any suggestion, I'd be thankful to you.

briansmith commented 1 week ago

I changed to GCC 12 because now I'm using esp-idf 5.1, which requires GCC 12. But unfortunately I have problems with the linker and I have errors about ring. Here they are: Errors.txt I have been trying to find possible solutions but I'm stuck doint this for a week. If you have any suggestion, I'd be thankful to you.

https://www.esp32.com/viewtopic.php?t=1612 and other things seem to say that you may need CFLAGS=-mlongcalls when invoking cargo?

juliankrieger commented 1 day ago

@briansmith since I can't find a PR from @lorenzo-pirchio , I will create one myself.

Furthermore, I'm on GCC V 13 (via crosstools) and still encounter the same error.

juliankrieger commented 1 day ago

@briansmith I can confirm building works with

TARGET_CC="xtensa-esp32s3-elf-cc"
TARGET_AR="xtensa-esp32s3-elf-ar"
TARGET_CFLAGS="-mlongcalls"