Optimize SHA1 calculation during image verification 05/270405/8
authorMateusz Moscicki <m.moscicki2@samsung.com>
Mon, 31 Jan 2022 11:46:26 +0000 (12:46 +0100)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Mon, 21 Feb 2022 14:59:47 +0000 (15:59 +0100)
There is no need to read the entire partition into memory when
calculating the checksum. In the case of e.g. rootfs this may not event
be possible.

Change-Id: I1165024ac8795cedc8ed9904ed35bea626d6601b

ss_engine/SS_UPI.c

index 71d10a1a7b596433541a773c3b657edfe39dd8bd..87e3054f90bf6d33ce6d5294016389cf3081b96b 100755 (executable)
@@ -2256,10 +2256,10 @@ Cleanup:
 
 int SS_IMGVerfiyPartition(ua_dataSS_t * ua_dataSS)
 {
-       FileInfo source_file;
        int ulResult = S_SS_SUCCESS;
        uint8_t source_sha1[SHA_DIGEST_SIZE];
        uint8_t target_sha1[SHA_DIGEST_SIZE];
+       uint8_t calculated_sha1[SHA_DIGEST_SIZE];
        size_t free_space = 0;
 
        if (!(ua_dataSS && ua_dataSS->update_cfg && ua_dataSS->parti_info && ua_dataSS->parti_info->ua_blk_name)) {
@@ -2289,22 +2289,18 @@ int SS_IMGVerfiyPartition(ua_dataSS_t * ua_dataSS)
                return E_SS_FAILURE;
        }
 
-       source_file.size = ua_dataSS->update_cfg->soure_img_size;
-       source_file.data = NULL;
-       if (SS_LoadPartition(ua_dataSS->parti_info->ua_blk_name, &source_file) == 0) {
-               if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) {
-                       LOGL(LOG_SSENGINE, "SS_IMGVerfiyPartition - SHA matches with source [%s] \n",
-                                ua_dataSS->parti_info->ua_blk_name);
-               } else {                          // Need not compare with Target sha as once upgraded, it should NOT verify same partition again.
-                       unsigned char actualShaBuffer[41] = { 0, };
-                       hex_digest(source_file.sha1, actualShaBuffer, SHA_DIGEST_SIZE);
-                       LOGE("SS_IMGVerfiyPartition - SHA mismatch with SRC [%s] Expected [%s] Actual [%s]\n",
-                               ua_dataSS->parti_info->ua_blk_name, ua_dataSS->update_cfg->soure_sha1, actualShaBuffer);
-                       SS_SetUpgradeState(E_SS_IMGSRCCURRUPTED);
-                       ulResult = E_SS_FAILURE;
-               }
+       SS_CalculateFileSha(ua_dataSS->parti_info->ua_blk_name, ua_dataSS->update_cfg->soure_img_size, calculated_sha1);
+       if (memcmp(calculated_sha1, source_sha1, SHA_DIGEST_SIZE) == 0) {
+               LOGL(LOG_SSENGINE, "SS_IMGVerfiyPartition - SHA matches with source [%s] \n",
+                        ua_dataSS->parti_info->ua_blk_name);
+       } else {                          // Need not compare with Target sha as once upgraded, it should NOT verify same partition again.
+               unsigned char actualShaBuffer[41] = { 0, };
+               hex_digest(calculated_sha1, actualShaBuffer, SHA_DIGEST_SIZE);
+               LOGE("SS_IMGVerfiyPartition - SHA mismatch with SRC [%s] Expected [%s] Actual [%s]\n",
+                       ua_dataSS->parti_info->ua_blk_name, ua_dataSS->update_cfg->soure_sha1, actualShaBuffer);
+               SS_SetUpgradeState(E_SS_IMGSRCCURRUPTED);
+               ulResult = E_SS_FAILURE;
        }
-       SS_Free(source_file.data);
        if (ulResult == S_SS_SUCCESS) {
                if (ua_dataSS->ui_progress)
                        ua_dataSS->ui_progress(ua_dataSS, 100);