mirage / hacl

Archived. Curve25519 support has been integrated into mirage-crypto-ec (via fiat-crypto). Hacl bindings are available from the hacl-star opam package. OCaml bindings for HACL* elliptic curves
https://github.com/mirage/mirage-crypto
Other
20 stars 5 forks source link

htole64 #30

Closed dinosaure closed 4 years ago

dinosaure commented 4 years ago

From #29, a question still exists where ocaml-freestanding does not provide htole64 or more generally, <inttypes.h> & these functions. From the extraction point-of-view, 2 problems appear:

The first problem can be fixed with gcc -DPRIu64=... but it's a hack. The second problem is solved by mirage-crypto (eg. src/native/bitfn.h which defines such functions).

1) From my perspective, even if it's a duplicate, we should be able to define by ourselves such functions into hacl (but it can be error-prone).

2) An other solution is to extend ocaml-freestanding to provide such macro/function and help by this way mirage-crypto and hacl. However. as it's explained into mirage/ocaml-freestanding#60, it's not really the proper way.

Should we choose 1) or 2) ?

/cc @hannesm /cc @mato

hannesm commented 4 years ago

@dinosaure thanks for opening this issue as a new one. I think the PRIu is dealt with in this repository (not sure whether this is "appropriate" and "sustainable", but I'd be keen to first get an update story for the imported C code before figuring out what to do there.

For htole64 and le64toh, while reading the source here (src/kremlib.h) I can spot there are already definitions thereof, but they are guarded in a long ifdef chain. I'd suggest we do as follows (for the freestanding build at least, I have not looked into the xen build):

thus: no modification of ocaml-freestanding needed; no dependency onto mirage-crypto needed. :)

hannesm commented 4 years ago

(I can try to figure out a PR for the approach outlined above during this week)

hannesm commented 4 years ago

it turns out to be a bit more complicated:

now, a cross-build of hacl excutes (amongst other things):

$ (cd _build/default/freestanding && /usr/home/hannes/.opam/4.10.0/bin/ocamlc.opt -g -I /usr/home/hannes/.opam/4.10.0/lib/ocaml-freestanding -ccopt -I/usr/home/hannes/.opam/4.10.0/lib/pkgconfig/../../include/ocaml-freestanding -ccopt -ffreestanding -ccopt -fstack-protector-strong -ccopt -nostdlibinc -ccopt -isystem -ccopt /usr/home/hannes/.opam/4.10.0/lib/pkgconfig/../../include/solo5-bindings-hvt/crt -ccopt -I/usr/home/hannes/.opam/4.10.0/lib/pkgconfig/../../include/solo5-bindings-hvt/solo5 -ccopt -g -o Hacl_Curve25519.o Hacl_Curve25519.c)

which (for reasons unknown to me) evaluates the #elif defined(__FreeBSD__) to true, and using this way includes (the copied) sys/endian.h that defines the right symbols.

On Linux, the #if defined(__linux__) is true, and #include <endian.h> entails the inclusion of ocaml-freestanding endian.h, which is not very complete (i.e. missing the le64toh definitions etc.).

I guess I have some questions @mato:

mato commented 4 years ago

why does #if defined(FreeBSD) evaluate to true?

Because we're appropriating the host toolchain, and it always defines that.

have a specific #if defined(freestanding) cases

Defining __freestanding__ is a good idea (at the ocaml-freestanding layer), as you propose in https://github.com/mirage/ocaml-freestanding/pull/73. However, it won't make __FreeBSD__ go away.

and passing -undef to the C compiler would remove all the pre-defined macros

Where is -undef documented? I didn't know about that. However, it's not a great solution as it'll probably undefine various things we do want such as __<arch>__.

in solo5, should we copy less system headers (or alternatively do the same for a linux host (looking at configure.sh)?

Yes, I merged that PR. Not sure why endian.h was in there, it's a bug.

instead should ocaml-freestanding provide an endian.h

Yes, ocaml-freestanding can provide the needed functions in its own endian.h, as proposed in https://github.com/mirage/ocaml-freestanding/pull/73. I'll comment on the specifics there.

hannesm commented 4 years ago

@mato

Defining freestanding is a good idea (at the ocaml-freestanding layer), as you propose in mirage/ocaml-freestanding#73. However, it won't make FreeBSD go away.

Yes, I'm aware of that (so the #ifdef .. #elif #endif chain first needs to check for __freestanding__) ;)

Where is -undef documented?

from man(1) gcc:

-undef
    Do not predefine any system-specific or GCC-specific macros. The standard predefined macros remain defined.

However, it's not a great solution as it'll probably undefine various things we do want such as __<arch>__.

yes, indeed. I came to the same conclusion.

$ cc -undef -dM -E - < /dev/null 
#define __STDC_HOSTED__ 1
#define __STDC_UTF_16__ 1
#define __STDC_UTF_32__ 1
#define __STDC_VERSION__ 201112L
#define __STDC__ 1

Thanks for your answer and cleanups.

mato commented 4 years ago

Yes, I'm aware of that (so the #ifdef .. #elif #endif chain first needs to check for freestanding) ;)

Yeah, that's probably the best option. We could also manually pass -UFreeBSD and suchlike, but I'd prefer to avoid that (maintenance nightmare, pollutes CFLAGS, etc.)

dinosaure commented 4 years ago

Still open for me, I need to check by hands but I will able to do that next week - however, consider it as solved.

hannesm commented 4 years ago

ocaml-freestanding since 0.6.0 now provides (a) inttypes.h and (b) endian.h, with the "missing" functionality for hacl. I just released hacl 0.1.1 with the mirage cross-compilation runes. Closing this issue, please comment / reopen if there's something remaining to be done for you.