From nobody Tue Feb 10 10:03:49 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1598634471; cv=none; d=zohomail.com; s=zohoarc; b=RZ3RB5IKhAqEQZS5nTmNCReffQoL/tck3mycHFBbx4I+VGj9yA+mYOU1oR5gTmP/3Sy+jdExlbGLkhKugEMHw2iMEcHEkDuo5k93HPCda9w42+tjJfzj5JsyxLp9yTylrCdt957HjiwzV98xYWKtE3d0K9mXSakl0AlYyrSiFWY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598634471; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FatlbZdYeHqsze9yHJGjnClTveY0gIW8RCgkQaKgScQ=; b=hik3ZpScOuKRJPQBQJmaIySNlHBFt2/ColUOBi0AURWG6oNxWIwefUxMuQyalvPRObwcs0nHGA31HtnNRTF4q4CNZZQAt+YIIcZPB4lnU8Hy2A54erele7nWOGvkSIE8QWV9yevDEoaMvZzt0J41toq60Yi9NKOWZT/WOwPTUxs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass 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 1598634471539347.55694818532027; Fri, 28 Aug 2020 10:07:51 -0700 (PDT) Received: from localhost ([::1]:41820 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBhqw-0008Nl-NP for importer@patchew.org; Fri, 28 Aug 2020 13:07:50 -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) 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) 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-Google-Smtp-Source: ABdhPJxtk3AuHy5uDI0+ZPWu+hKUY2XcOssbvB++NqsUBXe9y9hNMFyKpEb/jREgYqFXohdtVnVpmg== X-Received: by 2002:a65:6897:: with SMTP id e23mr1795129pgt.103.1598634326669; 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) 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=C3=A9 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(-) 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 =3D cipher->opaque; =20 - 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 =3D cipher->opaque; =20 - 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(QCry= ptoCipherAlgorithm alg, g_assert_not_reached(); } } + g_assert(is_power_of_2(ctx->blocksize)); =20 #ifdef CONFIG_QEMU_PRIVATE_XTS if (mode =3D=3D QCRYPTO_CIPHER_MODE_XTS) { @@ -305,7 +306,7 @@ qcrypto_gcrypt_cipher_encrypt(QCryptoCipher *cipher, QCryptoCipherGcrypt *ctx =3D cipher->opaque; gcry_error_t err; =20 - 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 =3D cipher->opaque; gcry_error_t err; =20 - 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(QCry= ptoCipherAlgorithm alg, QCryptoCipherAlgorithm_str(alg)); goto error; } + g_assert(is_power_of_2(ctx->blocksize)); =20 if (mode =3D=3D QCRYPTO_CIPHER_MODE_XTS && ctx->blocksize !=3D XTS_BLOCK_SIZE) { @@ -613,7 +614,7 @@ qcrypto_nettle_cipher_encrypt(QCryptoCipher *cipher, { QCryptoCipherNettle *ctx =3D cipher->opaque; =20 - 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 =3D cipher->opaque; =20 - 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 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/host-utils.h" #include "qapi/error.h" #include "crypto/cipher.h" #include "cipherpriv.h" --=20 2.25.1