libtom / libtomcrypt

LibTomCrypt is a fairly comprehensive, modular and portable cryptographic toolkit that provides developers with a vast array of well known published block ciphers, one-way hash functions, chaining modes, pseudo-random number generators, public key cryptography and a plethora of other routines.
https://www.libtom.net
Other
1.55k stars 457 forks source link

Better macro definition checking in tomcrypt_custom.h #574

Closed friedrichsenm closed 2 years ago

friedrichsenm commented 2 years ago

Prerequisites

Description

Trying to build a minimal version of the library using LTC_NOTHING and adding in the respective pieces can result in lots of unnecessary compiler warnings like the following:

<command-line>: note: this is the location of the previous definition
In file included from ./src/headers/tomcrypt.h:16,
                 from ./src/headers/tomcrypt_private.h:4,
                 from src/stream/sosemanuk/sosemanuk.c:27:
./src/headers/tomcrypt_custom.h:580: warning: "LTC_PKCS_8" redefined
  580 |    #define LTC_PKCS_8
      | 
<command-line>: note: this is the location of the previous definition
   * arm-linux-gnueabi-gcc src/stream/sosemanuk/sosemanuk_memory.o
In file included from ./src/headers/tomcrypt.h:16,
                 from ./src/headers/tomcrypt_private.h:4,
                 from src/stream/sosemanuk/sosemanuk_memory.c:4:
./src/headers/tomcrypt_custom.h:576: warning: "LTC_PKCS_1" redefined
  576 |    #define LTC_PKCS_1
      | 
<command-line>: note: this is the location of the previous definition
In file included from ./src/headers/tomcrypt.h:16,
                 from ./src/headers/tomcrypt_private.h:4,
                 from src/stream/sosemanuk/sosemanuk_memory.c:4:
./src/headers/tomcrypt_custom.h:580: warning: "LTC_PKCS_8" redefined
  580 |    #define LTC_PKCS_8
      | 
<command-line>: note: this is the location of the previous definition
   * arm-linux-gnueabi-gcc src/stream/sosemanuk/sosemanuk_test.o
In file included from ./src/headers/tomcrypt.h:16,
                 from ./src/headers/tomcrypt_private.h:4,
                 from src/stream/sosemanuk/sosemanuk_test.c:4:
./src/headers/tomcrypt_custom.h:576: warning: "LTC_PKCS_1" redefined
  576 |    #define LTC_PKCS_1
      | 
<command-line>: note: this is the location of the previous definition
In file included from ./src/headers/tomcrypt.h:16,
                 from ./src/headers/tomcrypt_private.h:4,
                 from src/stream/sosemanuk/sosemanuk_test.c:4:
./src/headers/tomcrypt_custom.h:580: warning: "LTC_PKCS_8" redefined
  580 |    #define LTC_PKCS_8
      | 
<command-line>: note: this is the location of the previous definition

These come from bits in tomcrypt_custom.h like:

#ifdef LTC_MRSA
   #define LTC_PKCS_1
#endif

#if defined(LTC_MRSA) || defined(LTC_MECC)
   #define LTC_PKCS_8
#endif

#ifdef LTC_PKCS_8
   #define LTC_PADDING
   #define LTC_PBES
#endif

A simple solution would be to just add an extra check to make sure the macros aren't already defined.

Steps to Reproduce

Compile the library with this set of defines (I'm sure there are more ways of triggering this): DLTC_NOTHING DLTC_RIJNDAEL DLTC_CBC_MODE DLTC_SHA3 DLTC_SHA256 DLTC_SHA384 DLTC_SHA512 DLTC_HASH_HELPERS DLTC_FORTUNA DLTC_MRSA DLTC_NO_RSA_CRT_HARDENING DLTC_NO_RSA_BLINDING DLTC_PKCS_1 DLTC_PKCS_5 DLTC_PKCS_8 DLTC_PKCS_12 DLTC_DER DLTC_HMAC

Version

1.18.2-develop

sjaeckel commented 2 years ago

I'm certain there are a lot more ways to trigger this!

It's not our target to handle each and every configuration possibility, but only to give a sane set of defaults and enable hard dependencies if you enable a feature.

Please simply remove clashing defines from your configuration.