Skip to content

Commit fb244f7

Browse files
nandojvejhedberg
authored andcommitted
lib: updatehub: Extract sha256 final method
Extract finish sha256 calc method. Signed-off-by: Gerson Fernando Budke <[email protected]> Signed-off-by: Otavio Salvador <[email protected]> (cherry picked from commit 1fe1b0e)
1 parent f7ac49e commit fb244f7

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

lib/updatehub/updatehub.c

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,40 @@ static int send_request(enum coap_msgtype msgtype, enum coap_method method,
322322
return ret;
323323
}
324324

325+
static bool install_update_cb_sha256(void)
326+
{
327+
u8_t image_hash[TC_SHA256_DIGEST_SIZE];
328+
char buffer[3], sha256_image_dowloaded[TC_SHA256_BLOCK_SIZE + 1];
329+
int i, buffer_len = 0;
330+
331+
if (tc_sha256_final(image_hash, &ctx.sha256sum) < 1) {
332+
LOG_ERR("Could not finish sha256sum");
333+
return false;
334+
}
335+
336+
memset(&sha256_image_dowloaded, 0, TC_SHA256_BLOCK_SIZE + 1);
337+
for (i = 0; i < TC_SHA256_DIGEST_SIZE; i++) {
338+
snprintk(buffer, sizeof(buffer), "%02x", image_hash[i]);
339+
buffer_len = buffer_len + strlen(buffer);
340+
strncat(&sha256_image_dowloaded[i], buffer,
341+
MIN(TC_SHA256_BLOCK_SIZE, buffer_len));
342+
}
343+
344+
if (strncmp(sha256_image_dowloaded,
345+
update_info.sha256sum_image,
346+
strlen(update_info.sha256sum_image)) != 0) {
347+
LOG_ERR("SHA256SUM of image are not the same");
348+
ctx.code_status = UPDATEHUB_DOWNLOAD_ERROR;
349+
return false;
350+
}
351+
352+
return true;
353+
}
354+
325355
static void install_update_cb(void)
326356
{
327357
struct coap_packet response_packet;
328-
char buffer[3], sha256_image_dowloaded[TC_SHA256_BLOCK_SIZE + 1];
329358
u8_t *data = k_malloc(MAX_DOWNLOAD_DATA);
330-
int i, buffer_len = 0;
331359
int rcvd = -1;
332360

333361
if (data == NULL) {
@@ -383,26 +411,7 @@ static void install_update_cb(void)
383411
}
384412

385413
if (ctx.downloaded_size == ctx.block.total_size) {
386-
u8_t image_hash[TC_SHA256_DIGEST_SIZE];
387-
388-
if (tc_sha256_final(image_hash, &ctx.sha256sum) < 1) {
389-
LOG_ERR("Could not finish sha256sum");
390-
ctx.code_status = UPDATEHUB_DOWNLOAD_ERROR;
391-
goto cleanup;
392-
}
393-
394-
memset(&sha256_image_dowloaded, 0, TC_SHA256_BLOCK_SIZE + 1);
395-
for (i = 0; i < TC_SHA256_DIGEST_SIZE; i++) {
396-
snprintk(buffer, sizeof(buffer), "%02x", image_hash[i]);
397-
buffer_len = buffer_len + strlen(buffer);
398-
strncat(&sha256_image_dowloaded[i], buffer,
399-
MIN(TC_SHA256_BLOCK_SIZE, buffer_len));
400-
}
401-
402-
if (strncmp(sha256_image_dowloaded,
403-
update_info.sha256sum_image,
404-
strlen(update_info.sha256sum_image)) != 0) {
405-
LOG_ERR("SHA256SUM of image are not the same");
414+
if (!install_update_cb_sha256())
406415
ctx.code_status = UPDATEHUB_DOWNLOAD_ERROR;
407416
goto cleanup;
408417
}

0 commit comments

Comments
 (0)