From patchwork Fri Aug 28 17:05:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 248598 Delivered-To: patch@linaro.org Received: by 2002:a92:5b9c:0:0:0:0:0 with SMTP id c28csp1258063ilg; Fri, 28 Aug 2020 10:07:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy15uKTPRZwHeMlHYJcOSLpOghH0Z72Gx50te/M+Wr7ix0sF4f98uxaOqEp+mAbYw6yPMay X-Received: by 2002:ac5:cce9:: with SMTP id k9mr1880353vkn.17.1598634451965; Fri, 28 Aug 2020 10:07:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598634451; cv=none; d=google.com; s=arc-20160816; b=Hhh8/617/y+vujG9R9uNmTXtMsfkl3VAvZ7eHY19f2+Sbk5SVGa5e+6d5ACZnAd65A rAIpT71SYUFlve+aNs7TfDL8JuKTaEeTwLLW8tZakoWgD5l8h5icd46CHs8WG7hxjeUd mtf6fwK3/ASvVbsgU5Mt/2qQIJimynH1ObhnlqWUvDkBEWsgUCWljcOY5K9ORBjFAiC8 v2Vd/ZlUNGJBUUJZt1I/pjZUigQVeZ5vyQs/8M8YrpVPpc6GVK5a/qbbgfHQfXQEGh95 vTqk82kU4u8v1vQQCjADeBnWfE2k6OpK4w09OJ4bYWYBvYli+H8V9Oy6bSIaKb59rg+M Mw8Q== 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=FatlbZdYeHqsze9yHJGjnClTveY0gIW8RCgkQaKgScQ=; b=inYhldCZA/iD4zIvZ006LK73asoAP2+etDByiUz1N4OftIZgO2TZjYiYrDoz6uEKid KCH3Ty2HYcVyjkB6JTDH9rywR1pCYjFJ1B+qczsVEZgLzaCQZXKKJXDpYUVDWMjMkc4j +TkVvGvdxS9amv1KH+ySwpQey0gS5o5BzCXyvBKCAgnkWUNDxoJU8ZI3udjYoUqOoSAO PbrpBFNvzlHbc85DKxDaAGljQH6h9LGNIybrsbUkJF+RE5XAScT1vW90NwPgg84g81Lv cppsprdyuGwWlT2QQEZU/Ao0DIA/5pX403tejaAzzzY5LQLtsIdIzsKtqQZ8S1emh/ql ng0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=clj6F+Zt; 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 j8si421819vkj.59.2020.08.28.10.07.31 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 28 Aug 2020 10:07:31 -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=clj6F+Zt; 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]:37834 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBhqd-0006oz-B1 for patch@linaro.org; Fri, 28 Aug 2020 13:07:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36610) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBhoh-0003YA-6A for qemu-devel@nongnu.org; Fri, 28 Aug 2020 13:05:31 -0400 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]:38502) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBhoe-000309-ND for qemu-devel@nongnu.org; Fri, 28 Aug 2020 13:05:30 -0400 Received: by mail-pg1-x544.google.com with SMTP id l191so726244pgd.5 for ; Fri, 28 Aug 2020 10:05:28 -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=FatlbZdYeHqsze9yHJGjnClTveY0gIW8RCgkQaKgScQ=; b=clj6F+ZtuzsXuk6WQlQQZXHbFPYm6DHm9mErWBPPiMbU7zsMi0XsR24XQsFy++p6l2 SfGzrjeVZTLu3POZpVrpcE5KWf0ZTmwflD1g+hJOdGQnoSM0wK8nPQj/I3asmZa1t84o oTM8w6NtkeGhpgCq+0AWPeXzY5dhwUCEw8U7bv5/fS15jkSjWKuIdWM3H2eNEgRJZWMK acGt261dZz8+cHd+2Z3/6Jl4oueGgELulo41xrAxxE0zyc6SZQWXLBvGmymVlb+ZIP82 EXX9CDp4FLebqeAi4BmgfEf1F9i2cN6VgOPY2x/CCMHWEtj4aKKflc/2bY8Z4UvetJky i/Ug== 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=FatlbZdYeHqsze9yHJGjnClTveY0gIW8RCgkQaKgScQ=; b=l3aD2anKOIt3WSOh6rUNK9b+FkMMYhWfl0lnkvK5NQ4Am+ZdDtjsliFC5Y4j+G4CQa LvWuZvBftYpnUtrABcI1FM3oLCNT/oe9oLTbf3Dhuygh5QZvJ0Dh99rQVkxEv//2unWv YwIjpkwbb7yYb0BZ7CE9DZnGhaKA8GFe70189GHD+h17V3dYztjOma5mydmqYWkCgJOj ElaJtCZmJNvMu0dAj1DPIsDHv1mWf1urMM36WneDSSeRejR00YIsakASNS/T3fcc5tT5 ysFYv8w2ProcIzZxmckFEOl5PAuM5Kdkq+FYprCOOfWsxeWKrGo465YoLyB3FMn/scHf kakQ== X-Gm-Message-State: AOAM533wg2h/yrx9MDHNkfLlVpRnOJJn0zbGt9LkFBTus3yXAMTREGze f/XlJFjKPyI+Eo6UV8sea1qP/xqOdsP5NA== X-Received: by 2002:a65:6897:: with SMTP id e23mr1795129pgt.103.1598634326669; Fri, 28 Aug 2020 10:05:26 -0700 (PDT) Received: from localhost.localdomain ([71.212.141.89]) by smtp.gmail.com with ESMTPSA id q12sm2277495pff.196.2020.08.28.10.05.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 10:05:26 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 01/16] crypto: Assume blocksize is a power of 2 Date: Fri, 28 Aug 2020 10:05:08 -0700 Message-Id: <20200828170523.418603-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200828170523.418603-1-richard.henderson@linaro.org> References: <20200828170523.418603-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::544; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x544.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. Reviewed-by: Daniel P. Berrangé 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 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"