void utils_decrypt_report(const char *report) { int decrypt_len; char *dec; memset(decrypted_report, 0, sizeof(decrypted_report)); yajl_val json_node = yajl_tree_parse(report, NULL, 0); if (!json_node) { strcpy(decrypted_report, "/* error: Failed to parse report. */"); return; } size_t i, r = json_node->u.object.len; for (i = 0; i < r; i++) { const char *ciphertext_path[] = { cmd_str(CMD_ciphertext), (const char *) 0 }; const char *echo_path[] = { "echo", (const char *) 0 }; const char *ciphertext = YAJL_GET_STRING(yajl_tree_get(json_node, ciphertext_path, yajl_t_string)); const char *echo = YAJL_GET_STRING(yajl_tree_get(json_node, echo_path, yajl_t_string)); if (ciphertext) { dec = aes_cbc_b64_decrypt((const unsigned char *)ciphertext, strlens(ciphertext), &decrypt_len, PASSWORD_STAND); if (!dec) { strcpy(decrypted_report, "/* error: Failed to decrypt. */"); goto exit; } sprintf(decrypted_report, "/* ciphertext */ %.*s", decrypt_len, dec); free(dec); goto exit; } else if (echo) { dec = aes_cbc_b64_decrypt((const unsigned char *)echo, strlens(echo), &decrypt_len, PASSWORD_VERIFY); if (!dec) { strcpy(decrypted_report, "/* error: Failed to decrypt echo. */"); goto exit; } sprintf(decrypted_report, "/* echo */ %.*s", decrypt_len, dec); free(dec); goto exit; } } strcpy(decrypted_report, report); exit: yajl_tree_free(json_node); return; }
// Encrypted storage static int memory_eeprom_crypt(const uint8_t *write_b, uint8_t *read_b, const int32_t addr) { int enc_len, dec_len; char *enc, *dec, enc_r[MEM_PAGE_LEN * 4 + 1] = {0}; if (read_b) { enc = aes_cbc_b64_encrypt((unsigned char *)utils_uint8_to_hex(read_b, MEM_PAGE_LEN), MEM_PAGE_LEN * 2, &enc_len, PASSWORD_MEMORY); if (!enc) { goto err; } memcpy(enc_r, enc, enc_len); free(enc); } if (write_b) { char enc_w[MEM_PAGE_LEN * 4 + 1] = {0}; enc = aes_cbc_b64_encrypt((unsigned char *)utils_uint8_to_hex(write_b, MEM_PAGE_LEN), MEM_PAGE_LEN * 2, &enc_len, PASSWORD_MEMORY); if (!enc) { goto err; } memcpy(enc_w, enc, enc_len); free(enc); if (memory_eeprom((uint8_t *)enc_w, (uint8_t *)enc_r, addr, MEM_PAGE_LEN) == STATUS_ERROR) { goto err; } if (memory_eeprom((uint8_t *)enc_w + MEM_PAGE_LEN, (uint8_t *)enc_r + MEM_PAGE_LEN, addr + MEM_PAGE_LEN, MEM_PAGE_LEN) == STATUS_ERROR) { goto err; } if (memory_eeprom((uint8_t *)enc_w + MEM_PAGE_LEN * 2, (uint8_t *)enc_r + MEM_PAGE_LEN * 2, addr + MEM_PAGE_LEN * 2, MEM_PAGE_LEN) == STATUS_ERROR) { goto err; } if (memory_eeprom((uint8_t *)enc_w + MEM_PAGE_LEN * 3, (uint8_t *)enc_r + MEM_PAGE_LEN * 3, addr + MEM_PAGE_LEN * 3, MEM_PAGE_LEN) == STATUS_ERROR) { goto err; } } else { if (memory_eeprom(NULL, (uint8_t *)enc_r, addr, MEM_PAGE_LEN) == STATUS_ERROR) { goto err; } if (memory_eeprom(NULL, (uint8_t *)enc_r + MEM_PAGE_LEN, addr + MEM_PAGE_LEN, MEM_PAGE_LEN) == STATUS_ERROR) { goto err; } if (memory_eeprom(NULL, (uint8_t *)enc_r + MEM_PAGE_LEN * 2, addr + MEM_PAGE_LEN * 2, MEM_PAGE_LEN) == STATUS_ERROR) { goto err; } if (memory_eeprom(NULL, (uint8_t *)enc_r + MEM_PAGE_LEN * 3, addr + MEM_PAGE_LEN * 3, MEM_PAGE_LEN) == STATUS_ERROR) { goto err; } } dec = aes_cbc_b64_decrypt((unsigned char *)enc_r, MEM_PAGE_LEN * 4, &dec_len, PASSWORD_MEMORY); if (!dec) { goto err; } memcpy(read_b, utils_hex_to_uint8(dec), MEM_PAGE_LEN); memset(dec, 0, dec_len); free(dec); utils_clear_buffers(); return STATUS_SUCCESS; err: utils_clear_buffers(); return STATUS_ERROR; }