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

Improve Windows compatibility #471

Merged
merged 5 commits into from
May 22, 2019
Merged

Conversation

ilammy
Copy link
Collaborator

@ilammy ilammy commented May 21, 2019

There are miscellaneous changes to ensure that Themis code can be compiled with Microsoft Visual Studio C Compiler.

  • soter/ed25519: fix incorrect unsigned negation

MSVC produces an error for this line. It may be okay with gcc and clang, but actually negating an unsigned value does not make sense, though it's not clear what effect it should have according to the standard.

gcc and clang on x86_64 compile this:

unsigned int b;
b = -b;

into this:

negl %eax

So they treat unsigned negation as two's complement, as if the number was signed). Replace the current expression with its equivalent.

The algorithm implies that b may only have 0 and 1 as values. Thus this line should be equivalent to

b = b ? UINT_MAX : 0;

but who knows what weird code paths lead here, so let's play it safe and use something completely equivalent. My little crypto: C is magic.

  • themis/secure_session: provide ssize_t on Windows

Fun fact: Windows does not have <sys/types.h> header and does not provide ssize_t in <WinSock2.h>. It is completely POSIX-specific. However, we use this type in Secure Session API.

Provide our definition of ssize_t on Windows. We use actual size_t types from MSVC's <stddef.h>, but mark them as signed.

An alternative approach is to replace ssize_t with something else, like int32_t. However, this will require massive changes in all language wrappers which all expect ssize_t. This might actually become necessary when we will be supporting Windows in language wrappers, but some of them may provide ssize_t in their FFI libraries so let's not jump the gun.

  • tests/soter: include <unistd.h> only for NIST

Currently NIST test suite runner is POSIX-specific (due to popen(), pathconf(), etc.) It does not compile on Windows with MSVC. Furthermore, Windows does not have <unistd.h> header.

Include <unistd.h> only when compiling NIST test runner. With this we can build Soter tests on Windows with NO_NIST_STS=1. We can add Windows support later.

  • tests/themis: fix implicit default initialization

MSVC does not support default initialization syntax. Initialize Secure Session callbacks explicitly to avoid compilation errors on Windows.

MSVC produces an error for this line. It may be okay with gcc and clang,
but actually negating an unsigned value does not make sense, though it's
not clear what effect it should have according to the standard.

gcc and clang on x86_64 compile this:

    unsigned int b;
    b = -b;

into this:

    negl %eax

So they treat unsigned negation as two's complement, as if the number
was signed). Replace the current expression with its equivalent.

The algorithm implies that "b" may only have 0 and 1 as values. Thus
this line should be equivalent to

    b = b ? UINT_MAX : 0;

but who knows what weird code paths lead here, so let's play it safe
and use something completely equivalent. My little crypto: C is magic.
Fun fact: Windows does not have <sys/types.h> header and does not
provide ssize_t in <WinSock2.h>. It is completely POSIX-specific.
However, we use this type in Secure Session API.

Provide our definition of ssize_t on Windows. We use actual size_t
types from MSVC's <stddef.h>, but mark them as "signed".

An alternative approach is to replace ssize_t with something else,
like int32_t. However, this will require massive changes in all
language wrappers which all expect ssize_t. This might actually
become necessary when we will be supporting Windows in language
wrappers, but some of them may provide ssize_t in their FFI
libraries so let's not jump the gun.
Currently NIST test suite runner is POSIX-specific (due to popen(),
pathconf(), etc.) It does not compile on Windows with MSVC.
Furthermore, Windows does not have <unistd.h> header.

Include <unistd.h> only when compiling NIST test runner. With this
we can build Soter tests on Windows with NO_NIST_STS=1.

We can add Windows support later.
MSVC does not support default initialization syntax. Initialize Secure
Session callbacks explicitily to avoid compilation errors on Windows.
@ilammy ilammy added core Themis Core written in C, its packages O-Windows 🖥️ Operating system: Windows labels May 21, 2019
Copy link
Collaborator

@Lagovas Lagovas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Contributor

@vixentael vixentael left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ilammy ilammy mentioned this pull request May 22, 2019
@ilammy ilammy merged commit 889170d into cossacklabs:master May 22, 2019
@ilammy ilammy deleted the windows/misc branch May 23, 2019 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Themis Core written in C, its packages O-Windows 🖥️ Operating system: Windows
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants