From patchwork Thu Aug 13 03:25:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 247649 Delivered-To: patch@linaro.org Received: by 2002:a92:cc90:0:0:0:0:0 with SMTP id x16csp924789ilo; Wed, 12 Aug 2020 20:26:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxu3KBeDvhSRm7w2WW5+HRse2p5IMwYWXLC+7liOgZ1HjqJSllUZrbpYJp4fVThLN+ve1k7 X-Received: by 2002:a25:d707:: with SMTP id o7mr3658604ybg.418.1597289163167; Wed, 12 Aug 2020 20:26:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597289163; cv=none; d=google.com; s=arc-20160816; b=hvXYGusPieZN3vz6OToLWfqM4gyRncTiMi5f2+N2s9q2EaXVAw9/wojTQs/W/SlKTs U8TTs05sUv/D6He7pxiYBV31aoQsPnJSLeQaKmEkBq1go7NMUlSIKtOpUX9ntVdIKoXz k5XJXzYreITSkh3/KhBBkD5vpGD3ytRGGg+iDNxKtpVo0/jP1n4cysMvdYQrQN9fO25/ SlSXZkfwX7PMOPqASsCiexcl6FKbWqR7efj15YSvyrAXkOrulWzIZdu8lA5NxKYKLz8R ml0lMnCE9OIfi3+fjmoFWT/1S1ki1vi7kFs1sjfyjH8mdWZylFnMUb39yCjL3YhJjqaH VUwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=ttKbooO9380v1ETeZhXOVKlGzyg1tlctRYSN45UvQQc=; b=kY+gGmKyovDbWeevkK21wI6aASxzkw2lB71wVlBZNAcczgoH+jZ3jMAjpiS5qmeUiL 2uz+aHu4P+eoYHHC0m1Lk6OgCrQJp/oZu88W3whmenxskdGlFGhdWMWZ53BtrIwU+P4X UGVlJs+1A+0pV4URMKMNvjaJinzE+9eVSKtzdh7yByvwaVvXOW/N4kaeSAj1IaBXiwoF T6YQT0M95rr6/QdbucDGNAKsVDAABLCekynlfvJPopHO8m0SN85TuwhyrUirvtaylWiI r6NcwUk6pqNx1H82nn8eB8ohFlQhcK+kPQhTQIsbxrUjGOrdTBcWz4k/iM0JrZ1bTkpn /T/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="T/pDw9F8"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id m66si5230483ybm.273.2020.08.12.20.26.03 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 12 Aug 2020 20:26:03 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="T/pDw9F8"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:44452 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k63sP-0004pH-Ie for patch@linaro.org; Wed, 12 Aug 2020 23:26:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58500) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k63sB-0004mb-PJ for qemu-devel@nongnu.org; Wed, 12 Aug 2020 23:25:47 -0400 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]:45317) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k63s9-0008VL-Vu for qemu-devel@nongnu.org; Wed, 12 Aug 2020 23:25:47 -0400 Received: by mail-pg1-x541.google.com with SMTP id x6so2105265pgx.12 for ; Wed, 12 Aug 2020 20:25:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ttKbooO9380v1ETeZhXOVKlGzyg1tlctRYSN45UvQQc=; b=T/pDw9F8LncEoJRmsfGCHCOzbgZZV3UpSXJ6fnzGtindqTSaJLcFU5rbOAG719HsMA m0caPuMbRhijy4z6n9O6ThAc5riE5Ga+Ww/wLOmCpXU4qXRWOHeJ0aurpgIERzLhOsUN gD8YRMwYO8TU3C/62gP+tTHeuq1WX5UQzN4QM1bdM7ugUu9wiS7sGyNu9MatAX0H6dIk y7SqmgweR1847aVYcZtQX+7fowPd+ztS5ENfMboHdTa2RH3WRW146/a6CkSYCg0XnTUM 4JHcaMIV2+DiV9QnZHRigqxiGT8/pIx+LcsS5e2OVr4U/gWc4jytAdrSEDATq6URV1vf 2cZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ttKbooO9380v1ETeZhXOVKlGzyg1tlctRYSN45UvQQc=; b=CgVuAVZe2ZM52VnBuU/D0UF0dnXUDgDighePejyz95snYI7CT1F3iUIP58jF1Nlj8r +duT5UWC0N10acZaLc6I/Y0IT8hM2oApMyRjYGMQOVG1t1bIP80Uw0S+8qbzAECWJ9Fn A1KHPELGcbe62xQTqt92BVVr+Rqx1CWSrk8IyCoTbdapexfLyWgLJXK3GvyO3ID7zE7V H2c3OhWsJDX5a5fgA6kkGbBnbVNrkmg8ejkpksfSmq7nwKr1vohx0IbczzYsiL+V4OEt NqdRmpW7ZHV5F9MWBa3mqXeuhealC1fxCMZbmR+qUkm0B7zjFKSOYRELU93GEQBQB/Zo ngiA== X-Gm-Message-State: AOAM5328/mvtag/yNxeOneZlzdCq4tjQhNDsnOmtYol9nLQNqkxZi6JF yCZPfLw6zp3cqwuXDW4kZ7yehJfcmOg= X-Received: by 2002:a65:6384:: with SMTP id h4mr1971691pgv.196.1597289144143; Wed, 12 Aug 2020 20:25:44 -0700 (PDT) Received: from localhost.localdomain ([71.212.141.89]) by smtp.gmail.com with ESMTPSA id m19sm3633164pgd.21.2020.08.12.20.25.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 20:25:43 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 03/17] crypto: Assume blocksize is a power of 2 Date: Wed, 12 Aug 2020 20:25:23 -0700 Message-Id: <20200813032537.2888593-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200813032537.2888593-1-richard.henderson@linaro.org> References: <20200813032537.2888593-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::541; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x541.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: berrange@redhat.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" The check in the encode/decode path using full division has a noticeable amount of overhead. By asserting the blocksize is a power of 2, we can reduce this check to a mask. Signed-off-by: Richard Henderson --- crypto/cipher-builtin.c | 4 ++-- crypto/cipher-gcrypt.c | 5 +++-- crypto/cipher-nettle.c | 5 +++-- crypto/cipher.c | 1 + 4 files changed, 9 insertions(+), 6 deletions(-) -- 2.25.1 Reviewed-by: Daniel P. Berrangé diff --git a/crypto/cipher-builtin.c b/crypto/cipher-builtin.c index 35cf7820d9..6eafd39da0 100644 --- a/crypto/cipher-builtin.c +++ b/crypto/cipher-builtin.c @@ -484,7 +484,7 @@ qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher, { QCryptoCipherBuiltin *ctxt = cipher->opaque; - if (len % ctxt->blocksize) { + if (len & (ctxt->blocksize - 1)) { error_setg(errp, "Length %zu must be a multiple of block size %zu", len, ctxt->blocksize); return -1; @@ -503,7 +503,7 @@ qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher, { QCryptoCipherBuiltin *ctxt = cipher->opaque; - if (len % ctxt->blocksize) { + if (len & (ctxt->blocksize - 1)) { error_setg(errp, "Length %zu must be a multiple of block size %zu", len, ctxt->blocksize); return -1; diff --git a/crypto/cipher-gcrypt.c b/crypto/cipher-gcrypt.c index 2864099527..81e4745bff 100644 --- a/crypto/cipher-gcrypt.c +++ b/crypto/cipher-gcrypt.c @@ -245,6 +245,7 @@ static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, g_assert_not_reached(); } } + g_assert(is_power_of_2(ctx->blocksize)); #ifdef CONFIG_QEMU_PRIVATE_XTS if (mode == QCRYPTO_CIPHER_MODE_XTS) { @@ -305,7 +306,7 @@ qcrypto_gcrypt_cipher_encrypt(QCryptoCipher *cipher, QCryptoCipherGcrypt *ctx = cipher->opaque; gcry_error_t err; - if (len % ctx->blocksize) { + if (len & (ctx->blocksize - 1)) { error_setg(errp, "Length %zu must be a multiple of block size %zu", len, ctx->blocksize); return -1; @@ -344,7 +345,7 @@ qcrypto_gcrypt_cipher_decrypt(QCryptoCipher *cipher, QCryptoCipherGcrypt *ctx = cipher->opaque; gcry_error_t err; - if (len % ctx->blocksize) { + if (len & (ctx->blocksize - 1)) { error_setg(errp, "Length %zu must be a multiple of block size %zu", len, ctx->blocksize); return -1; diff --git a/crypto/cipher-nettle.c b/crypto/cipher-nettle.c index 7e9a4cc199..0677fdfd33 100644 --- a/crypto/cipher-nettle.c +++ b/crypto/cipher-nettle.c @@ -576,6 +576,7 @@ static QCryptoCipherNettle *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, QCryptoCipherAlgorithm_str(alg)); goto error; } + g_assert(is_power_of_2(ctx->blocksize)); if (mode == QCRYPTO_CIPHER_MODE_XTS && ctx->blocksize != XTS_BLOCK_SIZE) { @@ -613,7 +614,7 @@ qcrypto_nettle_cipher_encrypt(QCryptoCipher *cipher, { QCryptoCipherNettle *ctx = cipher->opaque; - if (len % ctx->blocksize) { + if (len & (ctx->blocksize - 1)) { error_setg(errp, "Length %zu must be a multiple of block size %zu", len, ctx->blocksize); return -1; @@ -666,7 +667,7 @@ qcrypto_nettle_cipher_decrypt(QCryptoCipher *cipher, { QCryptoCipherNettle *ctx = cipher->opaque; - if (len % ctx->blocksize) { + if (len & (ctx->blocksize - 1)) { error_setg(errp, "Length %zu must be a multiple of block size %zu", len, ctx->blocksize); return -1; diff --git a/crypto/cipher.c b/crypto/cipher.c index e5adb56271..2722dc7d87 100644 --- a/crypto/cipher.c +++ b/crypto/cipher.c @@ -19,6 +19,7 @@ */ #include "qemu/osdep.h" +#include "qemu/host-utils.h" #include "qapi/error.h" #include "crypto/cipher.h" #include "cipherpriv.h"