From 9f2479ed3cc56ccba5d07e08399a8016edb87901 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 18 Oct 2018 16:36:57 +0200 Subject: mkfs.ubifs: Cleanup add_dent_node, user path encryption helper Signed-off-by: David Oberhollenzer Signed-off-by: Richard Weinberger --- ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 55 ++++++++++--------------------------- 1 file changed, 15 insertions(+), 40 deletions(-) (limited to 'ubifs-utils') diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c index 49a895a..ae1d267 100644 --- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c +++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c @@ -1703,53 +1703,28 @@ static int add_dent_node(ino_t dir_inum, const char *name, ino_t inum, set_dent_cookie(dent); if (!fctx) { - dent_key_init(c, &key, dir_inum, dname.name, dname.len); - dent->nlen = cpu_to_le16(dname.len); - memcpy(dent->name, dname.name, dname.len); - dent->name[dname.len] = '\0'; - len = UBIFS_DENT_NODE_SZ + dname.len + 1; - kname_len = dname.len; kname = strdup(name); if (!kname) return err_msg("cannot allocate memory"); } else { - void *inbuf, *outbuf, *crypt_key; unsigned int max_namelen = type == UBIFS_ITYPE_LNK ? UBIFS_MAX_INO_DATA : UBIFS_MAX_NLEN; - unsigned int padding = 4 << (fctx->flags & FS_POLICY_FLAGS_PAD_MASK); - unsigned int cryptlen; - - cryptlen = max_t(unsigned int, dname.len, FS_CRYPTO_BLOCK_SIZE); - cryptlen = round_up(cryptlen, padding); - cryptlen = min(cryptlen, max_namelen); - - inbuf = xmalloc(cryptlen); - /* CTS mode needs a block size aligned buffer */ - outbuf = xmalloc(round_up(cryptlen, FS_CRYPTO_BLOCK_SIZE)); - - memset(inbuf, 0, cryptlen); - memcpy(inbuf, dname.name, dname.len); - - crypt_key = calc_fscrypt_subkey(fctx); - if (!crypt_key) - return err_msg("could not compute subkey"); - if (encrypt_aes128_cbc_cts(inbuf, cryptlen, crypt_key, outbuf) < 0) - return err_msg("could not encrypt filename"); - - dent->nlen = cpu_to_le16(cryptlen); - memcpy(dent->name, outbuf, cryptlen); - dent->name[cryptlen] = '\0'; - len = UBIFS_DENT_NODE_SZ + cryptlen + 1; - - dent_key_init(c, &key, dir_inum, outbuf, cryptlen); - - kname_len = cryptlen; - kname = xmalloc(cryptlen); - memcpy(kname, outbuf, cryptlen); - free(crypt_key); - free(inbuf); - free(outbuf); + int ret; + + ret = encrypt_path((void **)&kname, dname.name, dname.len, + max_namelen, fctx); + if (ret < 0) + return ret; + + kname_len = ret; } + + dent_key_init(c, &key, dir_inum, kname, kname_len); + dent->nlen = cpu_to_le16(kname_len); + memcpy(dent->name, kname, kname_len); + dent->name[kname_len] = '\0'; + len = UBIFS_DENT_NODE_SZ + kname_len + 1; + key_write(&key, dent->key); return add_node(&key, kname, kname_len, dent, len); -- cgit v1.2.3