From: Jens Alfke Date: Wed, 12 Jan 2022 19:36:44 +0000 (-0800) Subject: Allow headers to declare a C++ namespace X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=d68589e6f5d258b4782e88d003ebd00326f275d0;p=Monocypher.git Allow headers to declare a C++ namespace If the preprocessor symbol `MONOCYPHER_CPP_NAMESPACE` is defined, the Monocypher headers will be wrapped in a C++ namespace instead of in `extern "C"`. The name of the namespace is the value of the symbol. This requires also compiling Monocypher's .c files as C++, either by: - Using a compiler option like `--x c++`; - Using an IDE setting, like the Identity & Type inspector in Xcode; - Putting `#include "monocypher.c" in a C++ file --- diff --git a/AUTHORS.md b/AUTHORS.md index 343aa1d..65a16ec 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -55,3 +55,7 @@ Mike Hamburg explained comb algorithms, including the signed all-bits-set comb described in his 2012 paper, Fast and compact elliptic-curve cryptography. This made EdDSA signatures over twice as fast. + +Jens Alfke added some #ifdefs that enabled Monocypher to compile into +a C++ namespace, preventing symbol collisions with similarly-named +functions in other crypto libraries. diff --git a/src/deprecated/aead-incr.c b/src/deprecated/aead-incr.c index 3d58692..a0e3ce2 100644 --- a/src/deprecated/aead-incr.c +++ b/src/deprecated/aead-incr.c @@ -87,6 +87,10 @@ #include "aead-incr.h" +#ifdef MONOCYPHER_CPP_NAMESPACE +namespace MONOCYPHER_CPP_NAMESPACE { +#endif + #define FOR_T(type, i, start, end) for (type i = (start); i < (end); i++) #define FOR(i, start, end) FOR_T(size_t, i, start, end) #define WIPE_CTX(ctx) crypto_wipe(ctx , sizeof(*(ctx))) @@ -227,3 +231,7 @@ int crypto_unlock_final(crypto_lock_ctx *ctx, const u8 mac[16]) WIPE_BUFFER(real_mac); return mismatch; } + +#ifdef MONOCYPHER_CPP_NAMESPACE +} +#endif diff --git a/src/deprecated/aead-incr.h b/src/deprecated/aead-incr.h index c349b1f..795babe 100644 --- a/src/deprecated/aead-incr.h +++ b/src/deprecated/aead-incr.h @@ -92,7 +92,9 @@ #include #include "monocypher.h" -#ifdef __cplusplus +#ifdef MONOCYPHER_CPP_NAMESPACE +namespace MONOCYPHER_CPP_NAMESPACE { +#elif defined(__cplusplus) extern "C" { #endif diff --git a/src/deprecated/chacha20.c b/src/deprecated/chacha20.c index 61a283d..e793050 100644 --- a/src/deprecated/chacha20.c +++ b/src/deprecated/chacha20.c @@ -79,6 +79,10 @@ #include "chacha20.h" #include "monocypher.h" +#ifdef MONOCYPHER_CPP_NAMESPACE +namespace MONOCYPHER_CPP_NAMESPACE { +#endif + #define FOR_T(type, i, start, end) for (type i = (start); i < (end); i++) #define FOR(i, start, end) FOR_T(size_t, i, start, end) #define WIPE_CTX(ctx) crypto_wipe(ctx , sizeof(*(ctx))) @@ -146,3 +150,7 @@ void crypto_chacha20_stream(crypto_chacha_ctx *ctx, u8 *stream, size_t size) { crypto_chacha20_encrypt(ctx, stream, 0, size); } + +#ifdef MONOCYPHER_CPP_NAMESPACE +} +#endif diff --git a/src/deprecated/chacha20.h b/src/deprecated/chacha20.h index 5208a08..afb8b5a 100644 --- a/src/deprecated/chacha20.h +++ b/src/deprecated/chacha20.h @@ -82,7 +82,9 @@ #include #include -#ifdef __cplusplus +#ifdef MONOCYPHER_CPP_NAMESPACE +namespace MONOCYPHER_CPP_NAMESPACE { +#elif defined(__cplusplus) extern "C" { #endif diff --git a/src/monocypher.c b/src/monocypher.c index 2859b5e..30642e6 100644 --- a/src/monocypher.c +++ b/src/monocypher.c @@ -53,6 +53,10 @@ #include "monocypher.h" +#ifdef MONOCYPHER_CPP_NAMESPACE +namespace MONOCYPHER_CPP_NAMESPACE { +#endif + ///////////////// /// Utilities /// ///////////////// @@ -2957,3 +2961,7 @@ int crypto_unlock(u8 *plain_text, return crypto_unlock_aead(plain_text, key, nonce, mac, 0, 0, cipher_text, text_size); } + +#ifdef MONOCYPHER_CPP_NAMESPACE +} +#endif diff --git a/src/monocypher.h b/src/monocypher.h index fc6886c..50f0fcb 100644 --- a/src/monocypher.h +++ b/src/monocypher.h @@ -57,7 +57,9 @@ #include #include -#ifdef __cplusplus +#ifdef MONOCYPHER_CPP_NAMESPACE +namespace MONOCYPHER_CPP_NAMESPACE { +#elif defined(__cplusplus) extern "C" { #endif diff --git a/src/optional/monocypher-ed25519.c b/src/optional/monocypher-ed25519.c index f173fd8..d5f4cfa 100644 --- a/src/optional/monocypher-ed25519.c +++ b/src/optional/monocypher-ed25519.c @@ -53,6 +53,10 @@ #include "monocypher-ed25519.h" +#ifdef MONOCYPHER_CPP_NAMESPACE +namespace MONOCYPHER_CPP_NAMESPACE { +#endif + ///////////////// /// Utilities /// ///////////////// @@ -407,3 +411,7 @@ void crypto_from_ed25519_private(u8 x25519[32], const u8 eddsa[32]) COPY(x25519, a, 32); WIPE_BUFFER(a); } + +#ifdef MONOCYPHER_CPP_NAMESPACE +} +#endif diff --git a/src/optional/monocypher-ed25519.h b/src/optional/monocypher-ed25519.h index 6a40703..98ffbd1 100644 --- a/src/optional/monocypher-ed25519.h +++ b/src/optional/monocypher-ed25519.h @@ -56,7 +56,9 @@ #include "monocypher.h" -#ifdef __cplusplus +#ifdef MONOCYPHER_CPP_NAMESPACE +namespace MONOCYPHER_CPP_NAMESPACE { +#elif defined(__cplusplus) extern "C" { #endif