kyber icon indicating copy to clipboard operation
kyber copied to clipboard

Test on Windows and fix CMakeList

Open thomwiggers opened this issue 4 years ago • 1 comments

Uses the CMake build files to test on Windows and fixes some small issues with the CMakeList.txts.

thomwiggers avatar Jul 27 '20 11:07 thomwiggers

Weirdly enough, on my branch this passed https://travis-ci.com/github/thomwiggers/kyber-clean/builds/177924662

thomwiggers avatar Aug 17 '20 10:08 thomwiggers

Can you please tell me how to import the library into the project? The library itself compiles successfully with your commands from travis, but the vectors and debug fail the tests. When compiling project, it gives me an error: a reference to an unresolved external symbol "int __cdecl pqcrystals_kyber768_ref_keypair(unsigned char *,unsigned char *)" And the same errors for decapsulation and encapsulation. Error in Russian, perhaps the translation is not accurate. Compiled with command: cl source.cpp /IF:\kyber\ref /link F:\kyber\build\ref\Release\kyber512_ref.lib I also tried to import in visual studio, there is the same error. I also tried to build lib and import with other compilers (gcc and clang), all the same errors. What am I doing wrong?

newTomas avatar Jul 18 '23 01:07 newTomas

This looks like a linker error, how exactly are you trying to build your code. For Windows I recommend using the implementation from PQClean [1], which has a Makefile.Microsoft_nmake.

[1] https://github.com/PQClean/PQClean

cryptojedi avatar Jul 18 '23 10:07 cryptojedi

Windows 11 Pro 22H2 22621.1992 Visual Studio Community 2022 17.6.5 cl.exe version 19.36.32537 for x64 cmake version 3.27.0-rc4 All steps:

  1. clone repo into F:\kyber
  2. create folder build in F:\kyber
  3. open powershell and cd F:\kyber\build
  4. cmake --build . -- /property:Configuration=Release image
  5. cmake --build . -- /property:Configuration=Release image
  6. ctest --output-on-failure -C Release image image
  7. Create source.cpp in F:\kyber-test with content:
#include <kem.h>

int main()
{
    uint8_t pk[CRYPTO_PUBLICKEYBYTES];
    uint8_t sk[CRYPTO_SECRETKEYBYTES];
    uint8_t ct[CRYPTO_CIPHERTEXTBYTES];
    uint8_t key[CRYPTO_BYTES];


    crypto_kem_keypair(pk, sk);
    crypto_kem_enc(ct, key, pk);
    crypto_kem_dec(key, ct, sk);
}

  1. run x64 Native Tools Command Prompt for VS 2022
  2. go to F:\kyber_test
  3. cl source.cpp /IF:\kyber\ref /link F:\kyber\build\ref\Release\kyber512_ref.lib image
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.6.5
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

D:\Program Files\Microsoft Visual Studio\2022\Community>cd F:\kyber_test

D:\Program Files\Microsoft Visual Studio\2022\Community>F:

F:\kyber_test>cl source.cpp /IF:\kyber\ref /link F:\kyber\build\ref\Release\kyber512_ref.lib
Оптимизирующий компилятор Microsoft (R) C/C++ версии 19.36.32537 для x64
(C) Корпорация Майкрософт (Microsoft Corporation).  Все права защищены.

source.cpp
Microsoft (R) Incremental Linker Version 14.36.32537.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:source.exe
F:\kyber\build\ref\Release\kyber512_ref.lib
source.obj
source.obj : error LNK2019: ссылка на неразрешенный внешний символ "int __cdecl pqcrystals_kyber768_ref_keypair(unsigned char *,unsigned char *)" (?pqcrystals_kyber768_ref_keypair@@YAHPEAE0@Z) в функции main.
source.obj : error LNK2019: ссылка на неразрешенный внешний символ "int __cdecl pqcrystals_kyber768_ref_enc(unsigned char *,unsigned char *,unsigned char const *)" (?pqcrystals_kyber768_ref_enc@@YAHPEAE0PEBE@Z) в функции main.
source.obj : error LNK2019: ссылка на неразрешенный внешний символ "int __cdecl pqcrystals_kyber768_ref_dec(unsigned char *,unsigned char const *,unsigned char const *)" (?pqcrystals_kyber768_ref_dec@@YAHPEAEPEBE1@Z) в функции main.
source.exe : fatal error LNK1120: неразрешенных внешних элементов: 3

F:\kyber_test>

If you add #define KYBER_K 2 to the top of source.cpp you get this: image The lib file contains the necessary symbols. dumpbin /symbols F:\kyber\build\ref\Release\kyber512_ref.lib > kyber512_ref_symbols.txt kyber512_ref_symbols.txt

newTomas avatar Jul 18 '23 11:07 newTomas

With PQClean I also get unresolved external symbol. Help please.

newTomas avatar Jul 19 '23 11:07 newTomas

With PQClean I also get unresolved external symbol. Help please.

FWIW, even further downstream, I'd suggest taking a look at liboqs where there's Windows CI (passing).

baentsch avatar Jul 19 '23 11:07 baentsch

For Windows I recommend using the implementation from PQClean

@cryptojedi Is there a reason the PQClean github workflows (containing Windows as I realize only now) are not displayed on the project README? Or is their status summarily shown somewhere else?

baentsch avatar Jul 19 '23 11:07 baentsch

For Windows I recommend using the implementation from PQClean

@cryptojedi Is there a reason the PQClean github workflows (containing Windows as I realize only now) are not displayed on the project README? Or is their status summarily shown somewhere else?

We have a bajillion of them. https://github.com/PQClean/PQClean/blob/master/.github/workflows/BADGES.md

Not sure why they're failing right now though.

thomwiggers avatar Jul 19 '23 13:07 thomwiggers

Thanks for the pointer.

Not sure why they're failing right now though.

The log seems pretty clear about that: "Prohibited use of preprocessor conditional"

baentsch avatar Jul 19 '23 13:07 baentsch

Thanks for the pointer.

Not sure why they're failing right now though.

The log seems pretty clear about that: "Prohibited use of preprocessor conditional"

I meant I did not have time to look into it 😉

thomwiggers avatar Jul 19 '23 13:07 thomwiggers

ACK. https://github.com/PQClean/PQClean/pull/492 seems like it would be fixing that.

baentsch avatar Jul 19 '23 13:07 baentsch

ACK. PQClean/PQClean#492 seems like it would be fixing that.

No, this change had no effect. I switched to liboqs, everything works great. Just tell me, please, did the creators of kyber check this lib? It is safe?

newTomas avatar Jul 20 '23 02:07 newTomas

It is safe?

What do you mean by this?

baentsch avatar Jul 20 '23 07:07 baentsch

It is safe?

What do you mean by this?

Is the algorithm implemented correctly? Are there any vulnerabilities? I recently saw a lib in which key pair were not generated and the code from the example worked with empty keys. This lib had a lot of stars and no one paid attention to the obvious vulnerability. That's why I wanted to use the official implementation.

newTomas avatar Jul 20 '23 07:07 newTomas

OQS uses the code from this repository; it extracts it via a set of scripts that are publicly available in the liboqs repository. I would expect any vulnerability in liboqs's Kyber to also be present here.

You might be interested in an implementation of Kyber that has actual proofs of correctness attached to it. See https://github.com/formosa-crypto/libjade and https://eprint.iacr.org/2023/215.pdf

thomwiggers avatar Jul 20 '23 07:07 thomwiggers

I would expect any vulnerability in liboqs's Kyber to also be present here

Agreed. With one caveat: The ARM code in liboqs doesn't come straight from this repo.

an implementation of Kyber that has actual proofs of correctness attached to it. See https://github.com/formosa-crypto/libjade and https://eprint.iacr.org/2023/215.pdf

Hence https://github.com/open-quantum-safe/liboqs/issues/1466. Anyone reading: Please state interest in getting that issue resolved (sooner than later) to help liboqs priority setting.

baentsch avatar Jul 20 '23 09:07 baentsch