resize-dynparts: Make binary fully static 15/318915/3
authorAntoni <a.adaszkiewi@samsung.com>
Wed, 29 Jan 2025 16:49:56 +0000 (17:49 +0100)
committerMateusz Moscicki <m.moscicki2@samsung.com>
Wed, 19 Feb 2025 13:43:09 +0000 (14:43 +0100)
This change ensures that `resize-dynparts` binary provided in a delta
will run on a system with an older GLib version.

OpenSSL dependency of liblp is removed. Linking statically to libcrypto
proved to be to difficult. The only functionality that was used was
calculating sha256 hashes. This is now implemented in source of
liblp.

sha256 functionality taken from:
https://android.googlesource.com/platform/system/core/+/515e1639ef0ab5e3149fafeffce826cf654d616f
files:
src/dynamic-partitions/liblp/sha256/hash-internal.h
src/dynamic-partitions/liblp/sha256/sha256.c
src/dynamic-partitions/liblp/sha256/sha256.h

Change-Id: Ifde513404d1d63ec3a99fe4e29c3ef4a852f0dbe

src/dynamic-partitions/liblp/CMakeLists.txt
src/dynamic-partitions/liblp/sha256/hash-internal.h [new file with mode: 0644]
src/dynamic-partitions/liblp/sha256/sha256.c [new file with mode: 0644]
src/dynamic-partitions/liblp/sha256/sha256.h [new file with mode: 0644]
src/dynamic-partitions/liblp/utility.cpp
src/dynamic-partitions/resize-dynparts/CMakeLists.txt

index ab6d5d2af777138f5a8bb5212676d1ac24b808ba..8d6efd6c43943356754d2382445bf637e8b60384 100644 (file)
@@ -1,7 +1,15 @@
 SET(CMAKE_CXX_STANDARD 17)
 SET(CMAKE_CXX_STANDARD_REQUIRED True)
 
-add_library(lp STATIC builder.cpp reader.cpp utility.cpp writer.cpp)
+set(lp_srcs
+       builder.cpp
+       reader.cpp
+       utility.cpp
+       writer.cpp
+       sha256/sha256.c
+)
+
+add_library(lp STATIC ${lp_srcs})
 
 target_compile_definitions(lp PUBLIC -D_FILE_OFFSET_BITS=64)
 target_include_directories(lp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
diff --git a/src/dynamic-partitions/liblp/sha256/hash-internal.h b/src/dynamic-partitions/liblp/sha256/hash-internal.h
new file mode 100644 (file)
index 0000000..898ad6b
--- /dev/null
@@ -0,0 +1,31 @@
+// Copyright 2007 Google Inc. All Rights Reserved.
+// Author: mschilder@google.com (Marius Schilder)
+#ifndef SECURITY_UTIL_LITE_HASH_INTERNAL_H__
+#define SECURITY_UTIL_LITE_HASH_INTERNAL_H__
+#include <stdint.h>
+#ifdef __cplusplus
+extern "C" {
+#endif  // __cplusplus
+struct HASH_CTX;  // forward decl
+typedef struct HASH_VTAB {
+  void (* const init)(struct HASH_CTX*);
+  void (* const update)(struct HASH_CTX*, const void*, int);
+  const uint8_t* (* const final)(struct HASH_CTX*);
+  const uint8_t* (* const hash)(const void*, int, uint8_t*);
+  int size;
+} HASH_VTAB;
+typedef struct HASH_CTX {
+  const HASH_VTAB * f;
+  uint64_t count;
+  uint8_t buf[64];
+  uint32_t state[8];  // upto SHA2
+} HASH_CTX;
+#define HASH_init(ctx) (ctx)->f->init(ctx)
+#define HASH_update(ctx, data, len) (ctx)->f->update(ctx, data, len)
+#define HASH_final(ctx) (ctx)->f->final(ctx)
+#define HASH_hash(data, len, digest) (ctx)->f->hash(data, len, digest)
+#define HASH_size(ctx) (ctx)->f->size
+#ifdef __cplusplus
+}
+#endif  // __cplusplus
+#endif  // SECURITY_UTIL_LITE_HASH_INTERNAL_H__
\ No newline at end of file
diff --git a/src/dynamic-partitions/liblp/sha256/sha256.c b/src/dynamic-partitions/liblp/sha256/sha256.c
new file mode 100644 (file)
index 0000000..0523758
--- /dev/null
@@ -0,0 +1,161 @@
+/* sha256.c
+**
+** Copyright 2013, The Android Open Source Project
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are met:
+**     * Redistributions of source code must retain the above copyright
+**       notice, this list of conditions and the following disclaimer.
+**     * Redistributions in binary form must reproduce the above copyright
+**       notice, this list of conditions and the following disclaimer in the
+**       documentation and/or other materials provided with the distribution.
+**     * Neither the name of Google Inc. nor the names of its contributors may
+**       be used to endorse or promote products derived from this software
+**       without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
+** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+// Optimized for minimal code size.
+//#include "mincrypt/sha256.h"
+#include "sha256.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits))))
+#define shr(value, bits) ((value) >> (bits))
+static const uint32_t K[64] = {
+    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
+    0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
+    0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
+    0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
+    0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
+    0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
+    0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
+    0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
+    0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };
+static void SHA256_Transform(SHA256_CTX* ctx) {
+    uint32_t W[64];
+    uint32_t A, B, C, D, E, F, G, H;
+    uint8_t* p = ctx->buf;
+    int t;
+    for(t = 0; t < 16; ++t) {
+        uint32_t tmp =  *p++ << 24;
+        tmp |= *p++ << 16;
+        tmp |= *p++ << 8;
+        tmp |= *p++;
+        W[t] = tmp;
+    }
+    for(; t < 64; t++) {
+        uint32_t s0 = ror(W[t-15], 7) ^ ror(W[t-15], 18) ^ shr(W[t-15], 3);
+        uint32_t s1 = ror(W[t-2], 17) ^ ror(W[t-2], 19) ^ shr(W[t-2], 10);
+        W[t] = W[t-16] + s0 + W[t-7] + s1;
+    }
+    A = ctx->state[0];
+    B = ctx->state[1];
+    C = ctx->state[2];
+    D = ctx->state[3];
+    E = ctx->state[4];
+    F = ctx->state[5];
+    G = ctx->state[6];
+    H = ctx->state[7];
+    for(t = 0; t < 64; t++) {
+        uint32_t s0 = ror(A, 2) ^ ror(A, 13) ^ ror(A, 22);
+        uint32_t maj = (A & B) ^ (A & C) ^ (B & C);
+        uint32_t t2 = s0 + maj;
+        uint32_t s1 = ror(E, 6) ^ ror(E, 11) ^ ror(E, 25);
+        uint32_t ch = (E & F) ^ ((~E) & G);
+        uint32_t t1 = H + s1 + ch + K[t] + W[t];
+        H = G;
+        G = F;
+        F = E;
+        E = D + t1;
+        D = C;
+        C = B;
+        B = A;
+        A = t1 + t2;
+    }
+    ctx->state[0] += A;
+    ctx->state[1] += B;
+    ctx->state[2] += C;
+    ctx->state[3] += D;
+    ctx->state[4] += E;
+    ctx->state[5] += F;
+    ctx->state[6] += G;
+    ctx->state[7] += H;
+}
+static const HASH_VTAB SHA256_VTAB = {
+    SHA256_init,
+    SHA256_update,
+    SHA256_final,
+    SHA256_hash,
+    SHA256_DIGEST_SIZE
+};
+void SHA256_init(SHA256_CTX* ctx) {
+    ctx->f = &SHA256_VTAB;
+    ctx->state[0] = 0x6a09e667;
+    ctx->state[1] = 0xbb67ae85;
+    ctx->state[2] = 0x3c6ef372;
+    ctx->state[3] = 0xa54ff53a;
+    ctx->state[4] = 0x510e527f;
+    ctx->state[5] = 0x9b05688c;
+    ctx->state[6] = 0x1f83d9ab;
+    ctx->state[7] = 0x5be0cd19;
+    ctx->count = 0;
+}
+void SHA256_update(SHA256_CTX* ctx, const void* data, int len) {
+    int i = (int) (ctx->count & 63);
+    const uint8_t* p = (const uint8_t*)data;
+    ctx->count += len;
+    while (len--) {
+        ctx->buf[i++] = *p++;
+        if (i == 64) {
+            SHA256_Transform(ctx);
+            i = 0;
+        }
+    }
+}
+const uint8_t* SHA256_final(SHA256_CTX* ctx) {
+    uint8_t *p = ctx->buf;
+    uint64_t cnt = ctx->count * 8;
+    int i;
+    SHA256_update(ctx, (uint8_t*)"\x80", 1);
+    while ((ctx->count & 63) != 56) {
+        SHA256_update(ctx, (uint8_t*)"\0", 1);
+    }
+    for (i = 0; i < 8; ++i) {
+        uint8_t tmp = (uint8_t) (cnt >> ((7 - i) * 8));
+        SHA256_update(ctx, &tmp, 1);
+    }
+    for (i = 0; i < 8; i++) {
+        uint32_t tmp = ctx->state[i];
+        *p++ = tmp >> 24;
+        *p++ = tmp >> 16;
+        *p++ = tmp >> 8;
+        *p++ = tmp >> 0;
+    }
+    return ctx->buf;
+}
+/* Convenience function */
+const uint8_t* SHA256_hash(const void* data, int len, uint8_t* digest) {
+    SHA256_CTX ctx;
+    SHA256_init(&ctx);
+    SHA256_update(&ctx, data, len);
+    memcpy(digest, SHA256_final(&ctx), SHA256_DIGEST_SIZE);
+    return digest;
+}
\ No newline at end of file
diff --git a/src/dynamic-partitions/liblp/sha256/sha256.h b/src/dynamic-partitions/liblp/sha256/sha256.h
new file mode 100644 (file)
index 0000000..806dab5
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+// Author: mschilder@google.com (Marius Schilder)
+#ifndef SECURITY_UTIL_LITE_SHA256_H__
+#define SECURITY_UTIL_LITE_SHA256_H__
+#include <stdint.h>
+#include "hash-internal.h"
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+typedef HASH_CTX SHA256_CTX;
+void SHA256_init(SHA256_CTX* ctx);
+void SHA256_update(SHA256_CTX* ctx, const void* data, int len);
+const uint8_t* SHA256_final(SHA256_CTX* ctx);
+// Convenience method. Returns digest address.
+const uint8_t* SHA256_hash(const void* data, int len, uint8_t* digest);
+#define SHA256_DIGEST_SIZE 32
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+#endif  // SECURITY_UTIL_LITE_SHA256_H_
\ No newline at end of file
index ad0b5f9757c0e3e2abdbf6540fb9c3e89bbe4edd..cdfe69443e0b0c6fda82f4551e54fbd1c1496e31 100644 (file)
@@ -30,8 +30,7 @@
 #include <string>
 #include <vector>
 
-#include <openssl/sha.h>
-#include <openssl/evp.h>
+#include "sha256/sha256.h"
 
 #include "utility.h"
 
@@ -78,10 +77,7 @@ const LpMetadataBlockDevice* GetMetadataSuperBlockDevice(const LpMetadata& metad
 }
 
 void SHA256(const void* data, size_t length, uint8_t out[32]) {
-    const EVP_MD *md = EVP_sha256();
-
-    if (!EVP_Digest(data, length, out, nullptr, md, nullptr))
-        LERROR << __PRETTY_FUNCTION__ << "Unable to compute hash";
+    SHA256_hash(data, (int)length, out);
 }
 
 uint32_t SlotNumberForSlotSuffix(const std::string& suffix) {
index 54ee949b427b18a659ada8a7597020df5f6daeae..2d72d7bbc5b359a4a77f9309ca8ee02380ca587e 100644 (file)
@@ -6,9 +6,13 @@ SET(RESIZEDYNPARTSEXENAME "resize-dynparts")
 
 ADD_EXECUTABLE(${RESIZEDYNPARTSEXENAME} ${RESIZEDYNPARTSSRCS})
 TARGET_COMPILE_OPTIONS(${RESIZEDYNPARTSEXENAME} PRIVATE -Wall -Wextra -pedantic -fPIE)
+target_link_options(${RESIZEDYNPARTSEXENAME} PRIVATE "-static")
+set_target_properties(${RESIZEDYNPARTSEXENAME} PROPERTIES
+               LINK_SEARCH_START_STATIC ON
+               LINK_SEARCH_END_STATIC ON
+       )
 
 TARGET_LINK_LIBRARIES(${RESIZEDYNPARTSEXENAME} PRIVATE lp)
-
 INSTALL(TARGETS ${RESIZEDYNPARTSEXENAME} DESTINATION ${BINDIR})
 
 add_executable(resize-dynparts-test test/test.cpp lib.cpp ../testlib/metadataio.h ../testlib/metadataio.cpp)