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

introducing macro LTC_NO_WIN32_BCRYPT #653

Closed
wants to merge 1 commit into from

Conversation

karel-m
Copy link
Member

@karel-m karel-m commented Aug 17, 2024

Forcing the use of BCryptGenRandom with newer MS Visual Studio makes me troubles with my CryptX module.

See https://www.cpantesters.org/cpan/report/00af9ff6-6e86-1014-b515-6e66cb952333

Defining LTC_NO_WIN32_BCRYPT avoids using BCryptGenRandom

@karel-m karel-m requested a review from sjaeckel August 17, 2024 20:21
@sjaeckel
Copy link
Member

hmm, we're also building against 2022 and the build is fine!? shouldn't you better fix the build instead of disabling the "correct/modern API"? I'm just asking :) if you prefer it like that, we can also merge it!

@karel-m
Copy link
Member Author

karel-m commented Aug 18, 2024

The thing is that when building perl bindings with VS2022 I need to explicitly instruct linker to link with bcrypt.lib the trick via #pragma comment(lib, "bcrypt.lib") in source code does not work due to the way how perl bindings are compiled and linked.

Although BCryptGenRandom might be called "modern API" I still consider CryptGenRandom more traditional and definitelly correct as well.

I only suggest to have an option to opt out from using BCryptGenRandom this is what this patch does.

@levitte
Copy link
Collaborator

levitte commented Aug 20, 2024

This PR doesn't make sense to me. The use of BCryptGenRandom() or CryptGenRandom() is a build-time decision, while tomcrypt_cfg.h is a public header that somehow should reflect how libtomcrypt was built.

All this considered, I don't quite understand why LTC_WIN32_BCRYPT is even defined in a public header... unless it's something for others to use as an indicator if bcrypt.lib should be added when linking, which is actually reasonable.

Actually, the way tomcrypt_cfg.h is designed and meant to be used is pretty clear, and the comment at the top does clarify it:

/* This is the build config file.
*
* With this you can setup what to include/exclude automatically during any build. Just comment
* out the line that #define's the word for the thing you want to remove. phew!
*/

In other words, if you want a special build, you're supposed to patch (and I guess, carry around the patch). For example like this:

diff --git a/src/headers/tomcrypt_cfg.h b/src/headers/tomcrypt_cfg.h
index 3d90d03c..93e2c3d2 100644
--- a/src/headers/tomcrypt_cfg.h
+++ b/src/headers/tomcrypt_cfg.h
@@ -313,7 +313,7 @@ typedef unsigned long ltc_mp_digit;
 #endif
 
 #if defined(_MSC_VER) && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600 && !defined(LTC_WIN32_BCRYPT)
-#   define LTC_WIN32_BCRYPT
+#   undef LTC_WIN32_BCRYPT
 #endif
 
 /* Define `LTC_NO_NULL_TERMINATION_CHECK` in the user code

@levitte
Copy link
Collaborator

levitte commented Aug 20, 2024

... maybe I'm being a bit too loud. Dunno. I just had a look at tomcrypt_custom.h, and that gave me pause. I'll dig deeper this evening...

levitte added a commit to levitte/libtomcrypt that referenced this pull request Aug 20, 2024
1. It's internal anyway, so doesn't quite need to be reflected in a
   public header file.
2. This makes it easy for someone build this library to choose not
   to use BCryptGenRandom() by simply definining LTC_NO_WIN32_BCRYPT,
   which was hard to reflect in an unmodified public header file.

Alternative to libtom#653
@levitte
Copy link
Collaborator

levitte commented Aug 20, 2024

I gave this another thought, and found that the easiest is to make LTC_WIN32_BCRYPT a private macro, see #660. Fortunately, this macro doesn't exist in any release, so that won't break anything, as far as I can tell.

@karel-m
Copy link
Member Author

karel-m commented Aug 20, 2024 via email

@levitte
Copy link
Collaborator

levitte commented Aug 20, 2024

My vote is obviously on #660 😉

@sjaeckel
Copy link
Member

Thanks @karel-m for bringing this up!

Since #660 is basically this patch, but in tomcrypt_private.h, I'm closing this PR.

@sjaeckel sjaeckel closed this Aug 21, 2024
levitte added a commit to levitte/libtomcrypt that referenced this pull request Aug 22, 2024
1. It's internal anyway, so doesn't quite need to be reflected in a
   public header file.
2. This makes it easy for someone build this library to choose not
   to use BCryptGenRandom() by simply definining LTC_NO_WIN32_BCRYPT,
   which was hard to reflect in an unmodified public header file.

Alternative to libtom#653
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

Successfully merging this pull request may close these issues.

3 participants