]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[build] Fix build failures with random versions of gcc
authorMichael Brown <mcb30@ipxe.org>
Wed, 27 Mar 2024 14:28:47 +0000 (14:28 +0000)
committerMichael Brown <mcb30@ipxe.org>
Wed, 27 Mar 2024 14:28:47 +0000 (14:28 +0000)
For unknown reasons, miscellaneous versions of gcc seem to struggle
with the static assertions used to ensure the correct layout of the
GCM structures.

Adjust the assertions to use offsetof() rather than direct pointer
comparison, on the basis that offsetof() must be a compile-time
constant value.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/crypto/gcm.c
src/include/ipxe/gcm.h

index a32890d595830341dd0035951d419ed37aea1898..b93925d074978be05a98b19f17d44e83774a5188 100644 (file)
@@ -109,6 +109,9 @@ static union gcm_block gcm_cached_mult[256];
  */
 static uint16_t gcm_cached_reduce[256];
 
+/** Offset of a field within GCM context */
+#define gcm_offset( field ) offsetof ( struct gcm_context, field )
+
 /**
  * Reverse bits in a byte
  *
@@ -470,17 +473,13 @@ int gcm_setkey ( struct gcm_context *context, const void *key, size_t keylen,
  */
 void gcm_setiv ( struct gcm_context *context, const void *iv, size_t ivlen ) {
 
-       /* Sanity check: ensure that memset()s will clear expected state */
-       build_assert ( &context->hash < &context->ctr );
-       build_assert ( &context->len < &context->ctr );
-       build_assert ( &context->ctr < &context->key );
-       build_assert ( ( ( void * ) &context->raw_cipher ) >
-                      ( ( void * ) &context->key ) );
-       build_assert ( ( ( void * ) context->raw_ctx ) >
-                      ( ( void * ) &context->key ) );
-
        /* Reset non-key state */
-       memset ( context, 0, offsetof ( typeof ( *context ), key ) );
+       memset ( context, 0, gcm_offset ( key ) );
+       build_assert ( gcm_offset ( key ) > gcm_offset ( hash ) );
+       build_assert ( gcm_offset ( key ) > gcm_offset ( len ) );
+       build_assert ( gcm_offset ( key ) > gcm_offset ( ctr ) );
+       build_assert ( gcm_offset ( key ) < gcm_offset ( raw_cipher ) );
+       build_assert ( gcm_offset ( key ) < gcm_offset ( raw_ctx ) );
 
        /* Reset counter */
        context->ctr.ctr.value = cpu_to_be32 ( 1 );
@@ -499,7 +498,12 @@ void gcm_setiv ( struct gcm_context *context, const void *iv, size_t ivlen ) {
                assert ( context->len.len.add == 0 );
 
                /* Reset non-key, non-counter state */
-               memset ( context, 0, offsetof ( typeof ( *context ), ctr ) );
+               memset ( context, 0, gcm_offset ( ctr ) );
+               build_assert ( gcm_offset ( ctr ) > gcm_offset ( hash ) );
+               build_assert ( gcm_offset ( ctr ) > gcm_offset ( len ) );
+               build_assert ( gcm_offset ( ctr ) < gcm_offset ( key ) );
+               build_assert ( gcm_offset ( ctr ) < gcm_offset ( raw_cipher ) );
+               build_assert ( gcm_offset ( ctr ) < gcm_offset ( raw_ctx ) );
        }
 
        DBGC2 ( context, "GCM %p Y[0]:\n", context );
index 4864445d24ae90a043c9069a93b326be4960dbba..2c785a977c0e98ae8638dd8222d7d6931b339ebc 100644 (file)
@@ -89,10 +89,9 @@ static int _gcm_name ## _setkey ( void *ctx, const void *key,                \
                                  size_t keylen ) {                     \
        struct _gcm_name ## _context *context = ctx;                    \
        build_assert ( _blocksize == sizeof ( context->gcm.key ) );     \
-       build_assert ( ( ( void * ) &context->gcm ) ==                  \
-                      ( ( void * ) context ) );                        \
-       build_assert ( ( ( void * ) &context->raw ) ==                  \
-                      ( ( void * ) context->gcm.raw_ctx ) );           \
+       build_assert ( offsetof ( typeof ( *context ), gcm ) == 0 );    \
+       build_assert ( offsetof ( typeof ( *context ), raw ) ==         \
+                      offsetof ( typeof ( *context ), gcm.raw_ctx ) ); \
        return gcm_setkey ( &context->gcm, key, keylen, &_raw_cipher ); \
 }                                                                      \
 static void _gcm_name ## _setiv ( void *ctx, const void *iv,           \