Refactor json_segment_create_crypt helper.

Refactor crypt segment json helper body into
separate routine so that it can be reused later
in future hw-opal-crypt segment helper.
This commit is contained in:
Ondrej Kozina
2023-05-24 14:50:28 +02:00
parent 47ac021c03
commit d6107bf241

View File

@@ -245,37 +245,50 @@ json_object *json_segment_create_linear(uint64_t offset, const uint64_t *length,
return jobj;
}
static bool json_add_crypt_fields(json_object *jobj_segment, uint64_t iv_offset,
const char *cipher, const char *integrity,
uint32_t sector_size, unsigned reencryption)
{
json_object *jobj_integrity;
assert(cipher);
json_object_object_add(jobj_segment, "iv_tweak", crypt_jobj_new_uint64(iv_offset));
json_object_object_add(jobj_segment, "encryption", json_object_new_string(cipher));
json_object_object_add(jobj_segment, "sector_size", json_object_new_int(sector_size));
if (integrity) {
jobj_integrity = json_object_new_object();
if (!jobj_integrity)
return false;
json_object_object_add(jobj_integrity, "type", json_object_new_string(integrity));
json_object_object_add(jobj_integrity, "journal_encryption", json_object_new_string("none"));
json_object_object_add(jobj_integrity, "journal_integrity", json_object_new_string("none"));
json_object_object_add(jobj_segment, "integrity", jobj_integrity);
}
if (reencryption)
LUKS2_segment_set_flag(jobj_segment, "in-reencryption");
return true;
}
json_object *json_segment_create_crypt(uint64_t offset,
uint64_t iv_offset, const uint64_t *length,
const char *cipher, const char *integrity,
uint32_t sector_size, unsigned reencryption)
{
json_object *jobj_integrity, *jobj = _segment_create_generic("crypt", offset, length);
json_object *jobj = _segment_create_generic("crypt", offset, length);
if (!jobj)
return NULL;
json_object_object_add(jobj, "iv_tweak", crypt_jobj_new_uint64(iv_offset));
json_object_object_add(jobj, "encryption", json_object_new_string(cipher));
json_object_object_add(jobj, "sector_size", json_object_new_int(sector_size));
if (json_add_crypt_fields(jobj, iv_offset, cipher, integrity, sector_size, reencryption))
return jobj;
if (integrity) {
jobj_integrity = json_object_new_object();
if (!jobj_integrity) {
json_object_put(jobj);
return NULL;
}
json_object_object_add(jobj_integrity, "type", json_object_new_string(integrity));
json_object_object_add(jobj_integrity, "journal_encryption", json_object_new_string("none"));
json_object_object_add(jobj_integrity, "journal_integrity", json_object_new_string("none"));
json_object_object_add(jobj, "integrity", jobj_integrity);
}
if (reencryption)
LUKS2_segment_set_flag(jobj, "in-reencryption");
return jobj;
json_object_put(jobj);
return NULL;
}
uint64_t LUKS2_segment_offset(struct luks2_hdr *hdr, int segment, unsigned blockwise)