From patchwork Thu Oct 18 14:36:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Weinberger X-Patchwork-Id: 985955 X-Patchwork-Delegate: david.oberhollenzer@sigma-star.at Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=nod.at Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="RUVRq91T"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42bX3h4S3Tz9sC2 for ; Fri, 19 Oct 2018 01:48:32 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=gCSNLgoyu8PvaKTsB9wdpehGEG+U0wG57NZcOz2SdFE=; b=RUVRq91TOPcvoe F5qTeSIiMFCBsOuEbe5T9ywhe4dB3yYXO7SDZYW04N4QC+1DJrcq/mgD1OmbCjZ5mAFqwWuqlScy0 jEmejczYfPBSXWOu/H2Wqs2WvqTV4DOTRua+UigMXWP14YGsGWJ4NzKVqkktie00FeuEQ5ztgO4N/ Vpm8LP0Kl0ot8ftlLtIKiUk8AhFc87D+Zha51E8k8zJdVRqSb7vRmYNuU44kzrnWsU/McGf9mD8mB zJypuRCMpb5jeSHbYMLRZ8TrxPk1Q3/D+1+jU3AOFHuBCbYK5A2w51PGo/ljwvlXn+c0WdPw76l4F T6qNy+c0cU25LoqdD34Q==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gD9b0-0002V5-F9; Thu, 18 Oct 2018 14:48:18 +0000 Received: from lilium.sigma-star.at ([109.75.188.150]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gD9Rj-0004Dq-Az for linux-mtd@lists.infradead.org; Thu, 18 Oct 2018 14:39:31 +0000 Received: from localhost (localhost [127.0.0.1]) by lilium.sigma-star.at (Postfix) with ESMTP id 88A0218011B70; Thu, 18 Oct 2018 16:38:19 +0200 (CEST) From: Richard Weinberger To: linux-mtd@lists.infradead.org Subject: [PATCH 16/42] mkfs.ubifs: Implement file contents encryption Date: Thu, 18 Oct 2018 16:36:52 +0200 Message-Id: <20181018143718.26298-17-richard@nod.at> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181018143718.26298-1-richard@nod.at> References: <20181018143718.26298-1-richard@nod.at> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181018_073843_603011_EB31F66D X-CRM114-Status: GOOD ( 13.48 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 T_SPF_PERMERROR SPF: test of record failed (permerror) X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Richard Weinberger , david.oberhollenzer@sigma-star.at Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Signed-off-by: Richard Weinberger --- ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 39 ++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c index 4ffd8fd51e41..b7d68c60d481 100644 --- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c +++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c @@ -1817,10 +1817,9 @@ static int add_file(const char *path_name, struct stat *st, ino_t inum, } /* Make data node */ memset(dn, 0, UBIFS_DATA_NODE_SZ); - data_key_init(&key, inum, block_no++); + data_key_init(&key, inum, block_no); dn->ch.node_type = UBIFS_DATA_NODE; key_write(&key, &dn->key); - dn->size = cpu_to_le32(bytes_read); out_len = NODE_BUFFER_SIZE - UBIFS_DATA_NODE_SZ; if (c->default_compr == UBIFS_COMPR_NONE && (flags & FS_COMPR_FL)) @@ -1834,7 +1833,39 @@ static int add_file(const char *path_name, struct stat *st, ino_t inum, compr_type = compress_data(buf, bytes_read, &dn->data, &out_len, use_compr); dn->compr_type = cpu_to_le16(compr_type); - //TODO: encrypt + dn->size = cpu_to_le32(bytes_read); + + if (!fctx) { + dn->compr_size = 0; + } else { + void *inbuf, *outbuf, *crypt_key; + size_t ret, pad_len = round_up(out_len, FS_CRYPTO_BLOCK_SIZE); + + dn->compr_size = out_len; + + inbuf = xzalloc(pad_len); + outbuf = xzalloc(pad_len); + + memcpy(inbuf, &dn->data, out_len); + + crypt_key = calc_fscrypt_subkey(fctx); + if (!crypt_key) + return err_msg("could not compute subkey"); + + ret = encrypt_block_aes128_cbc(inbuf, pad_len, crypt_key, block_no, + outbuf); + if (ret != pad_len) + return err_msg("encrypt_block_aes128_cbc returned %zi instead of %zi", ret, pad_len); + + memcpy(&dn->data, outbuf, pad_len); + + out_len = pad_len; + + free(inbuf); + free(outbuf); + free(crypt_key); + } + dn_len = UBIFS_DATA_NODE_SZ + out_len; /* Add data node to file system */ err = add_node(&key, NULL, 0, dn, dn_len); @@ -1842,6 +1873,8 @@ static int add_file(const char *path_name, struct stat *st, ino_t inum, close(fd); return err; } + + block_no++; } while (ret != 0); if (close(fd) == -1)