7fae540
commit 0238eb8f3612515f4374381b593dd79116169330
7fae540
Author: John Dennis <jdennis@redhat.com>
7fae540
Date:   Thu Aug 2 16:21:33 2018 -0400
7fae540
7fae540
    fix concatkdf failures on big endian architectures
7fae540
    
7fae540
    Several of the elements used to compute the digest in ECDH-ES key
7fae540
    agreement computation are represented in binary form as a 32-bit
7fae540
    integer length followed by that number of octets.  the length
7fae540
    field. The 32-bit length integer is represented in big endian
7fae540
    format (the 8 most significant bits are in the first octet.).
7fae540
    
7fae540
    The conversion to a 4 byte big endian integer was being computed
7fae540
    in a manner that only worked on little endian architectures. The
7fae540
    function htonl() returns a 32-bit integer whose octet sequence given
7fae540
    the address of the integer is big endian. There is no need for any
7fae540
    further manipulation.
7fae540
    
7fae540
    The existing code used bit shifting on a 32-bit value. In C bit
7fae540
    shifting is endian agnostic for multi-octet values, a right shift
7fae540
    moves most significant bits toward least significant bits. The result
7fae540
    of a bit shift of a multi-octet value on either big or little
7fae540
    archictures will always be the same provided you "view" it as the same
7fae540
    data type (e.g. 32-bit integer). But indexing the octets of that
7fae540
    mulit-octet value will be different depending on endianness, hence the
7fae540
    assembled octets differed depending on endianness.
7fae540
    
7fae540
    Issue: #77
7fae540
    Signed-off-by: John Dennis <jdennis@redhat.com>
7fae540
7fae540
diff --git a/src/concatkdf.c b/src/concatkdf.c
7fae540
index ec064ab..59b845a 100644
7fae540
--- a/src/concatkdf.c
7fae540
+++ b/src/concatkdf.c
7fae540
@@ -29,15 +29,9 @@
7fae540
 ////////////////////////////////////////////////////////////////////////////////
7fae540
 static uint8_t *_apply_uint32(const uint32_t value, uint8_t *buffer)
7fae540
 {
7fae540
-    const uint32_t formatted = htonl(value);
7fae540
-    const uint8_t data[4] = {
7fae540
-        (formatted >> 0) & 0xff,
7fae540
-        (formatted >> 8) & 0xff,
7fae540
-        (formatted >> 16) & 0xff,
7fae540
-        (formatted >> 24) & 0xff
7fae540
-    };
7fae540
-    memcpy(buffer, data, 4);
7fae540
+    const uint32_t big_endian_int32 = htonl(value);
7fae540
 
7fae540
+    memcpy(buffer, &big_endian_int32, 4);
7fae540
     return buffer + 4;
7fae540
 }
7fae540
 
7fae540
diff --git a/test/check_concatkdf.c b/test/check_concatkdf.c
7fae540
index e4325fc..41d0f1c 100644
7fae540
--- a/test/check_concatkdf.c
7fae540
+++ b/test/check_concatkdf.c
7fae540
@@ -60,14 +60,9 @@ _create_otherinfo_header_finish:
7fae540
 
7fae540
 static bool _cmp_uint32(uint8_t **actual, uint32_t expected)
7fae540
 {
7fae540
-    uint32_t value = htonl(expected);
7fae540
-    uint8_t expectedData[] = {
7fae540
-        (value >> 0) & 0xff,
7fae540
-        (value >> 8) & 0xff,
7fae540
-        (value >> 16) & 0xff,
7fae540
-        (value >> 24) & 0xff
7fae540
-    };
7fae540
-    bool result = (0 == memcmp(*actual, expectedData, 4));
7fae540
+    uint32_t big_endian_int32 = htonl(expected);
7fae540
+
7fae540
+    bool result = (0 == memcmp(*actual, &big_endian_int32, 4));
7fae540
     (*actual) += 4;
7fae540
     return result;
7fae540
 }