From nobody Wed Nov 12 00:32:09 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1566827948; cv=none; d=zoho.com; s=zohoarc; b=TVEP7ix4VIPtxhcpR+HiAF9QeQESAGIJwJROWXxMI27oKa9cfFMDK5ghFzqqIGsx/KKKNLvDa2536mjEM0NNKb2K500oOZJ5bkLIo8SppPmx/yNObEorbL8o2pwkI78QfvvELTZmT3Z09vSJNP+mBROwGh/xAPa2/dLtrrZvV2U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566827948; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=w83j9c6Gf6KMO5ZiScOCnD7YSeo78bFJVkvlnKu5qGU=; b=Cd3tN0L8QM6ZrV1FXvHisHPPEqBAACb9QYu7iqFVQdzUM5DAND+HKYOvFw9zRxZX20DLlrpoUkbN7H0l0H99spnAjJlYWL0N+/uNQSc6VkVhRhU69TGoHovaVG10YPlR2/b8nF48oYhqEj6OROfbFxTrME/cs304YU78P5JH7GM= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1566827948704122.1109376597667; Mon, 26 Aug 2019 06:59:08 -0700 (PDT) Received: from localhost ([::1]:53142 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i2FWS-0006E9-RL for importer@patchew.org; Mon, 26 Aug 2019 09:59:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38325) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i2FQ1-0000AU-6k for qemu-devel@nongnu.org; Mon, 26 Aug 2019 09:52:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i2FPz-0007IR-Jh for qemu-devel@nongnu.org; Mon, 26 Aug 2019 09:52:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47346) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i2FPv-0007DQ-7b; Mon, 26 Aug 2019 09:52:19 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8C0E61256C3; Mon, 26 Aug 2019 13:52:18 +0000 (UTC) Received: from maximlenovopc.usersys.redhat.com (unknown [10.35.206.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id EA1EC5D70D; Mon, 26 Aug 2019 13:52:10 +0000 (UTC) From: Maxim Levitsky To: qemu-devel@nongnu.org Date: Mon, 26 Aug 2019 16:50:59 +0300 Message-Id: <20190826135103.22410-10-mlevitsk@redhat.com> In-Reply-To: <20190826135103.22410-1-mlevitsk@redhat.com> References: <20190826135103.22410-1-mlevitsk@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.71]); Mon, 26 Aug 2019 13:52:18 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 09/13] qcrypto-block: extract check and parse header X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , qemu-block@nongnu.org, Markus Armbruster , Max Reitz , Stefan Hajnoczi , Maxim Levitsky Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This is just to make qcrypto_block_luks_open more reasonable in size. Signed-off-by: Maxim Levitsky --- crypto/block-luks.c | 254 +++++++++++++++++++++++++------------------- 1 file changed, 146 insertions(+), 108 deletions(-) diff --git a/crypto/block-luks.c b/crypto/block-luks.c index b4dc6fc899..cc9a52c9af 100644 --- a/crypto/block-luks.c +++ b/crypto/block-luks.c @@ -508,6 +508,148 @@ fail: return ret; } =20 +/* + * Does basic sanity checks on the LUKS header + */ +static int +qcrypto_block_luks_check_header(const QCryptoBlockLUKS *luks, Error **errp) +{ + int ret; + + if (memcmp(luks->header.magic, qcrypto_block_luks_magic, + QCRYPTO_BLOCK_LUKS_MAGIC_LEN) !=3D 0) { + error_setg(errp, "Volume is not in LUKS format"); + ret =3D -EINVAL; + goto fail; + } + + if (luks->header.version !=3D QCRYPTO_BLOCK_LUKS_VERSION) { + error_setg(errp, "LUKS version %" PRIu32 " is not supported", + luks->header.version); + ret =3D -ENOTSUP; + goto fail; + } + + return 0; +fail: + return ret; +} + +/* + * Parses the crypto parameters that are stored in the LUKS header + */ + +static int +qcrypto_block_luks_parse_header(QCryptoBlockLUKS *luks, Error **errp) +{ + g_autofree char *cipher_mode =3D g_strdup(luks->header.cipher_mode); + char *ivgen_name, *ivhash_name; + int ret =3D -1; + Error *local_err =3D NULL; + + /* + * The cipher_mode header contains a string that we have + * to further parse, of the format + * + * -[:] + * + * eg cbc-essiv:sha256, cbc-plain64 + */ + ivgen_name =3D strchr(cipher_mode, '-'); + if (!ivgen_name) { + ret =3D -EINVAL; + error_setg(errp, "Unexpected cipher mode string format %s", + luks->header.cipher_mode); + goto out; + } + *ivgen_name =3D '\0'; + ivgen_name++; + + ivhash_name =3D strchr(ivgen_name, ':'); + if (!ivhash_name) { + luks->ivgen_hash_alg =3D 0; + } else { + *ivhash_name =3D '\0'; + ivhash_name++; + + luks->ivgen_hash_alg =3D qcrypto_block_luks_hash_name_lookup(ivhas= h_name, + &local_= err); + if (local_err) { + ret =3D -ENOTSUP; + error_propagate(errp, local_err); + goto out; + } + } + + luks->cipher_mode =3D qcrypto_block_luks_cipher_mode_lookup(cipher_mod= e, + &local_err); + if (local_err) { + ret =3D -ENOTSUP; + error_propagate(errp, local_err); + goto out; + } + + luks->cipher_alg =3D + qcrypto_block_luks_cipher_name_lookup(luks->header.cipher_name, + luks->cipher_mode, + luks->header.master_key_= len, + &local_err); + if (local_err) { + ret =3D -ENOTSUP; + error_propagate(errp, local_err); + goto out; + } + + luks->hash_alg =3D + qcrypto_block_luks_hash_name_lookup(luks->header.hash_spec, + &local_err); + if (local_err) { + ret =3D -ENOTSUP; + error_propagate(errp, local_err); + goto out; + } + + luks->ivgen_alg =3D qcrypto_block_luks_ivgen_name_lookup(ivgen_name, + &local_err); + if (local_err) { + ret =3D -ENOTSUP; + error_propagate(errp, local_err); + goto out; + } + + if (luks->ivgen_alg =3D=3D QCRYPTO_IVGEN_ALG_ESSIV) { + if (!ivhash_name) { + ret =3D -EINVAL; + error_setg(errp, "Missing IV generator hash specification"); + goto out; + } + luks->ivgen_cipher_alg =3D + qcrypto_block_luks_essiv_cipher(luks->cipher_alg, + luks->ivgen_hash_alg, + &local_err); + if (local_err) { + ret =3D -ENOTSUP; + error_propagate(errp, local_err); + goto out; + } + } else { + + /* + * Note we parsed the ivhash_name earlier in the cipher_mode + * spec string even with plain/plain64 ivgens, but we + * will ignore it, since it is irrelevant for these ivgens. + * This is for compat with dm-crypt which will silently + * ignore hash names with these ivgens rather than report + * an error about the invalid usage + */ + luks->ivgen_cipher_alg =3D luks->cipher_alg; + } + ret =3D 0; +out: + return ret; + +} + /* * Given a key slot, and user password, this will attempt to unlock * the master encryption key from the key slot. @@ -720,12 +862,9 @@ qcrypto_block_luks_open(QCryptoBlock *block, Error **errp) { QCryptoBlockLUKS *luks =3D NULL; - Error *local_err =3D NULL; int ret =3D 0; g_autofree uint8_t *masterkey =3D NULL; - char *ivgen_name, *ivhash_name; g_autofree char *password =3D NULL; - g_autofree char *cipher_mode =3D NULL; =20 if (!(flags & QCRYPTO_BLOCK_OPEN_NO_IO)) { if (!options->u.luks.key_secret) { @@ -748,117 +887,16 @@ qcrypto_block_luks_open(QCryptoBlock *block, goto fail; } =20 - - if (memcmp(luks->header.magic, qcrypto_block_luks_magic, - QCRYPTO_BLOCK_LUKS_MAGIC_LEN) !=3D 0) { - error_setg(errp, "Volume is not in LUKS format"); - ret =3D -EINVAL; - goto fail; - } - if (luks->header.version !=3D QCRYPTO_BLOCK_LUKS_VERSION) { - error_setg(errp, "LUKS version %" PRIu32 " is not supported", - luks->header.version); - ret =3D -ENOTSUP; - goto fail; - } - - cipher_mode =3D g_strdup(luks->header.cipher_mode); - - /* - * The cipher_mode header contains a string that we have - * to further parse, of the format - * - * -[:] - * - * eg cbc-essiv:sha256, cbc-plain64 - */ - ivgen_name =3D strchr(cipher_mode, '-'); - if (!ivgen_name) { - ret =3D -EINVAL; - error_setg(errp, "Unexpected cipher mode string format %s", - cipher_mode); - goto fail; - } - *ivgen_name =3D '\0'; - ivgen_name++; - - ivhash_name =3D strchr(ivgen_name, ':'); - if (!ivhash_name) { - luks->ivgen_hash_alg =3D 0; - } else { - *ivhash_name =3D '\0'; - ivhash_name++; - - luks->ivgen_hash_alg =3D qcrypto_block_luks_hash_name_lookup(ivhas= h_name, - &local_= err); - if (local_err) { - ret =3D -ENOTSUP; - error_propagate(errp, local_err); - goto fail; - } - } - - luks->cipher_mode =3D qcrypto_block_luks_cipher_mode_lookup(cipher_mod= e, - &local_err); - if (local_err) { - ret =3D -ENOTSUP; - error_propagate(errp, local_err); - goto fail; - } - - luks->cipher_alg =3D - qcrypto_block_luks_cipher_name_lookup(luks->header.cipher_name, - luks->cipher_mode, - luks->header.master_key_len, - &local_err); - if (local_err) { - ret =3D -ENOTSUP; - error_propagate(errp, local_err); - goto fail; - } - - luks->hash_alg =3D - qcrypto_block_luks_hash_name_lookup(luks->header.hash_spec, - &local_err); - if (local_err) { - ret =3D -ENOTSUP; - error_propagate(errp, local_err); + ret =3D qcrypto_block_luks_check_header(luks, errp); + if (ret) { goto fail; } =20 - luks->ivgen_alg =3D qcrypto_block_luks_ivgen_name_lookup(ivgen_name, - &local_err); - if (local_err) { - ret =3D -ENOTSUP; - error_propagate(errp, local_err); + ret =3D qcrypto_block_luks_parse_header(luks, errp); + if (ret) { goto fail; } =20 - if (luks->ivgen_alg =3D=3D QCRYPTO_IVGEN_ALG_ESSIV) { - if (!ivhash_name) { - ret =3D -EINVAL; - error_setg(errp, "Missing IV generator hash specification"); - goto fail; - } - luks->ivgen_cipher_alg =3D - qcrypto_block_luks_essiv_cipher(luks->cipher_alg, - luks->ivgen_hash_alg, - &local_err); - if (local_err) { - ret =3D -ENOTSUP; - error_propagate(errp, local_err); - goto fail; - } - } else { - /* Note we parsed the ivhash_name earlier in the cipher_mode - * spec string even with plain/plain64 ivgens, but we - * will ignore it, since it is irrelevant for these ivgens. - * This is for compat with dm-crypt which will silently - * ignore hash names with these ivgens rather than report - * an error about the invalid usage - */ - luks->ivgen_cipher_alg =3D luks->cipher_alg; - } =20 if (!(flags & QCRYPTO_BLOCK_OPEN_NO_IO)) { /* Try to find which key slot our password is valid for --=20 2.17.2