- if (key)
- memcpy(&vk->key, key, keylength);
- else
- memset(&vk->key, 0, keylength);
+ vk->id = -1;
+ vk->next = NULL;
+
+ /* keylength 0 is valid => no key */
+ if (vk->keylength) {
+ if (key)
+ memcpy(&vk->key, key, keylength);
+ else
+ crypt_safe_memzero(&vk->key, keylength);
+ }
+
+ return vk;
+}
+
+int crypt_volume_key_set_description(struct volume_key *vk, const char *key_description)
+{
+ if (!vk)
+ return -EINVAL;
+
+ free(CONST_CAST(void*)vk->key_description);
+ vk->key_description = NULL;
+ if (key_description && !(vk->key_description = strdup(key_description)))
+ return -ENOMEM;
+
+ return 0;
+}
+
+void crypt_volume_key_set_id(struct volume_key *vk, int id)
+{
+ if (vk && id >= 0)
+ vk->id = id;
+}
+
+int crypt_volume_key_get_id(const struct volume_key *vk)
+{
+ return vk ? vk->id : -1;
+}
+
+struct volume_key *crypt_volume_key_by_id(struct volume_key *vks, int id)
+{
+ struct volume_key *vk = vks;
+
+ if (id < 0)
+ return NULL;
+
+ while (vk && vk->id != id)
+ vk = vk->next;