Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libsodium #466

Open
shirinebadi opened this issue Aug 28, 2024 · 16 comments
Open

Libsodium #466

shirinebadi opened this issue Aug 28, 2024 · 16 comments

Comments

@shirinebadi
Copy link

Hi. I'm trying to import libsodium and do a simple encryption and I'm following the steps in keystone-demo. However, I'm facing some issues.

1- I don't know how to see the error. When I do make -j 32 in keystone directory it just simply shows:

eystone-examples 9d15d5f7ba77712c Building
make[5]: *** [CMakeFiles/Makefile2:995: test-shirin/CMakeFiles/shirin.dir/all] Error 2
make[4]: *** [CMakeFiles/Makefile2:228: CMakeFiles/examples.dir/rule] Error 2
make[3]: *** [Makefile:124: examples] Error 2
make[2]: *** [package/pkg-generic.mk:293: /users/shirine/keystone/build-generic64/buildroot.build/build/keystone-examples-9d15d5f7ba77712c/.stamp_built] Error 2
make[1]: *** [Makefile:82: _all] Error 2
make: *** [Makefile:93: buildroot] Error 2

2- When I include client, I have no problem, but with including server I'm facing above error:

target_link_libraries(${eapp_bin} "-nostdlib -static" ${KEYSTONE_LIB_EAPP} ${KEYSTONE_LIB_EDGE} ${LIBSODIUM_DIR}/.libs/libsodium.a)
@grg-haas
Copy link
Collaborator

Hi @shirinebadi! The full build log should be in build-generic64/build.log -- feel free to upload it here and I can help diagnose.

@shirinebadi
Copy link
Author

Hi. Thank you for your help.
I checked the log and this is the error:

fatal error: sodium.h: No such file or directory
   9 | #include "sodium.h"

@grg-haas
Copy link
Collaborator

It seems that in your CMakeLists.txt, that you have an external build of libsodium at ${LIBSODIUM_DIR} that you then link against. In that external build, do you have a sodium.h file? If so, you can include the directory of that file in your eapp bin by using target_include_directories:

target_include_directories(${eapp_bin} PRIVATE <directory of sodium.h>)

@shirinebadi
Copy link
Author

shirinebadi commented Aug 28, 2024

Here is how my CMakeLists.txt look like:

target_link_libraries(${eapp_bin} "-nostdlib -static -T" ${KEYSTONE_LIB_EAPP} ${KEYSTONE_LIB_EDGE} ${LIBSODIUM_DIR}/.libs/libsodium.a)

target_include_directories(${eapp_bin}
  PUBLIC ${KEYSTONE_SDK_DIR}/include/app
  PUBLIC ${KEYSTONE_SDK_DIR}/include/edge
  PRIVATE ${LIBSODIUM_DIR}/include)

But I'm still facing this issue.

@grg-haas
Copy link
Collaborator

It may be worth it to clean out your examples directory from Buildroot:

BUILDROOT_TARGET="keystone-examples-dirclean" make

And then try the build again. Sometimes, stale files can get stuck in the build process

@shirinebadi
Copy link
Author

Thanks for your helps.

@shirinebadi
Copy link
Author

Do you think this error has something to do with how I built libsodium?

error adding symbols: file in wrong format
collect2: error: ld returned 1 exit status

@grg-haas
Copy link
Collaborator

That could do it yeah, you would need to also compile libsodium for the same RISC-V architecture as your eapp

@shirinebadi
Copy link
Author

I just followed these steps:

if [ ! -d libsodium_server ]
then
  git clone https://github.com/jedisct1/libsodium.git libsodium_server
  cd libsodium_server
  git checkout 4917510626c55c1f199ef7383ae164cf96044aea
  patch -p1 < $DEMO_DIR/sodium_patches/configure.ac.patch
  ./autogen.sh
  ./configure --host=riscv64-unknown-linux-gnu --disable-ssp --disable-asm --without-pthreads
  make
  cd ..
fi
export LIBSODIUM_DIR=$(pwd)/libsodium_server/src/libsodium/

@shirinebadi
Copy link
Author

shirinebadi commented Aug 29, 2024

So I could able to cross-compile it for RiscV, however, I'm encountering this error:

../libsodium_builds/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ref.c:93: undefined reference to `__stack_chk_fail'

@grg-haas
Copy link
Collaborator

grg-haas commented Aug 29, 2024 via email

@shirinebadi
Copy link
Author

Thanks for your helps! This is getting me crazy and I'm so new to it. I think there is a huge problem with randombytes_sysrandom.c, it give me several errors and

randombytes_sysrandom.c:(.text+0x544): undefined reference to `close'
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: /users/shirine/keystone/libsodium_builds/libsodium/src/libsodium//.libs/libsodium.a(libsodium_la-blake2b-ref.o): in function `.L58':
blake2b-ref.c:(.text+0x90e): undefined reference to `__assert_fail'
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: 
/users/shirine/keystone/libsodium_builds/libsodium/src/libsodium//.libs/libsodium.a(libsodium_la-ed25519_ref10.o): in function `sc25519_reduce':

ed25519_ref10.c:(.text+0xb67e): undefined reference to `abort'

@grg-haas
Copy link
Collaborator

grg-haas commented Aug 29, 2024 via email

@grg-haas
Copy link
Collaborator

Wait sorry -- actually, the best way to solve this would most likely be to remove nostdlib from your link options. That option inhibits the automatic inclusion of libc, libgcc, and various other necessary libraries for this.

@shirinebadi
Copy link
Author

This is how my CMAke looks like now:

add_executable(${eapp_bin} ${eapp_src})
target_link_libraries(${eapp_bin} ${KEYSTONE_LIB_EAPP} ${KEYSTONE_LIB_EDGE}  ${LIBSODIUM_CLIENT_DIR}/.libs/libsodium.a)

target_include_directories(${eapp_bin}
  PUBLIC ${KEYSTONE_SDK_DIR}/include/app
  PUBLIC ${KEYSTONE_SDK_DIR}/include/edge)

set_target_properties(${eapp_bin}
  PROPERTIES LINK_FLAGS "-static -T ${CMAKE_CURRENT_SOURCE_DIR}/app.lds")

(After I removed nostdlib), now I get bunch of other errors:

/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: shirin: section .tdata lma 0x97498 adjusted to 0x974e0
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: /users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/riscv64-buildroot-linux-gnu/sysroot/usr/lib/crt1.o: in function `.L0 ':
start.o:(.text+0x30): undefined reference to `__global_pointer$'
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: /users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/riscv64-buildroot-linux-gnu/sysroot/usr/lib/libc.a(libc-start.o): in function `.L0 ':
libc-start.c:(.text+0xc): undefined reference to `__fini_array_end'
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: libc-start.c:(.text+0x14): undefined reference to `__fini_array_start'
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: libc-start.c:(.text+0x178): undefined reference to `__ehdr_start'
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: /users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/riscv64-buildroot-linux-gnu/sysroot/usr/lib/libc.a(libc-start.o): in function `.L20':
libc-start.c:(.text+0x2e8): undefined reference to `__preinit_array_start'
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: libc-start.c:(.text+0x2f0): undefined reference to `__preinit_array_end'
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: /users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/riscv64-buildroot-linux-gnu/sysroot/usr/lib/libc.a(libc-start.o): in function `.L0 ':
libc-start.c:(.text+0x30c): undefined reference to `__init_array_start'
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: libc-start.c:(.text+0x314): undefined reference to `__init_array_end'
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: shirin: hidden symbol `__fini_array_end' isn't defined
/users/shirine/keystone/build-generic64/buildroot.build/per-package/keystone-examples/host/bin/../lib/gcc/riscv64-buildroot-linux-gnu/11.4.0/../../../../riscv64-buildroot-linux-gnu/bin/ld: final link failed: bad value

@shirinebadi
Copy link
Author

Oh my bad!!!!!!! I just fixed the CMake. app.lds was making trouble. Thanks for your time and helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants