#include "fscrypt.h"
#include "common.h"
-static int do_sha256(const unsigned char *in, size_t len, unsigned char *out)
+static int do_hash(const EVP_MD *md, const unsigned char *in, size_t len, unsigned char *out)
{
unsigned int out_len;
EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
if (!mdctx)
return -1;
- if (EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1)
+ if (EVP_DigestInit_ex(mdctx, md, NULL) != 1)
return -1;
if(EVP_DigestUpdate(mdctx, in, len) != 1)
return -1;
}
- if (do_sha256(key, key_len, sha256) != 0) {
+ if (do_hash(EVP_sha256(), key, key_len, sha256) != 0) {
errmsg("sha256 failed");
return -1;
}
aes_key_len, NULL, 0, derived_key);
}
+int derive_key_descriptor(const void *source_key, void *descriptor)
+{
+ int ret = -1;
+ void *hash1 = xzalloc(EVP_MD_size(EVP_sha512()));
+ void *hash2 = xzalloc(EVP_MD_size(EVP_sha512()));
+
+ if (do_hash(EVP_sha512(), source_key, FS_MAX_KEY_SIZE, hash1) != 0)
+ goto out;
+
+ if (do_hash(EVP_sha512(), hash1, EVP_MD_size(EVP_sha512()), hash2) != 0)
+ goto out;
+
+ memcpy(descriptor, hash2, FS_KEY_DESCRIPTOR_SIZE);
+
+ ret = 0;
+out:
+ free(hash1);
+ free(hash2);
+ return ret;
+}
+
static struct cipher ciphers[] = {
{
.name = "AES-128-CBC",
ssize_t derive_key_aes(const void *deriving_key, const void *source_key,
size_t source_key_len, void *derived_key);
+int derive_key_descriptor(const void *source_key, void *descriptor);
struct cipher *get_cipher(const char *name);
return NULL;
}
- if (parse_key_descriptor(key_descriptor, master_key_descriptor))
- return NULL;
-
if (load_master_key(key_file, fscrypt_cipher))
return NULL;
+ if (!key_descriptor) {
+ if (derive_key_descriptor(fscrypt_masterkey, master_key_descriptor))
+ return NULL;
+ } else {
+ if (parse_key_descriptor(key_descriptor, master_key_descriptor))
+ return NULL;
+ }
+
RAND_bytes((void *)nonce, FS_KEY_DERIVATION_NONCE_SIZE);
new_fctx = xmalloc(sizeof(*new_fctx));