From patchwork Fri Aug 28 17:05:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 248603 Delivered-To: patch@linaro.org Received: by 2002:a92:5b9c:0:0:0:0:0 with SMTP id c28csp1260340ilg; Fri, 28 Aug 2020 10:10:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxFrEg1QeyasqzhA/jrZ7b1UJEMDJPJby79+QohUYdGB8QuILrNfLRINUZvQgcWBpEidO0C X-Received: by 2002:a25:a363:: with SMTP id d90mr3817272ybi.23.1598634604243; Fri, 28 Aug 2020 10:10:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598634604; cv=none; d=google.com; s=arc-20160816; b=Udxs+jTjIk3xEZfBld9dZDjPUtVHfGv31eL3kuRN+Kg6kHrOmQTDcnfqtEpI+P4Fx7 BQNv7AWBknK7Vq1hbvlGGuoHPEqjv2K6Hr172La1MG0leuBJ8b0V6n8QihqP7JttkpVM coUsPrLiv+1Qed5o7mUvMAqu27MHGw7x4cq02h+epfrE+VEZC23m7JkfXJ3dtvJIOci6 0nP9HOwq1p/qK99qK0RlSkoi77r+FfYiYwGJHDI0pvU8rGf5IspQMEIp4LOxwl66pks3 9FbTW52oVA6NCEdkdmN7Zpk0dUF9MM2jSTslk9RWx3XIW/BzqV4Mp9nj2KWkMKHAS4DV O5EA== 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=Z0jSasqYMa/kYWQRBUn0M5RkDRX5OspYiVQvjoGRm10=; b=wGYV9m89NsHpH5/7oLEkGIKERDb92oLazNt87EmkTXwddOOveA79YOyqMrllKNEFbc jqUPHc3QSIBof+7v8gsR3EwFCGSDOkF8LwfCzw59KQPxLkRwnKWrgHHyeugNuiYgFcr8 qGgySQoL+2x8mG+OXRoJBXnVpDVDvNYIp9RDINOnv0Gn62bw5jT+Bwdr3AjQS6z1VrP2 vfta4Vp6hi9NVhVzdmx9z5m3n8QdlGpd6ZFedcyk1+PAF41720nihqBxQ/fJTxcYyYtC vvuHvyORag4pvVrRNZ8ZQsKq2osjs6oBQh40ci3f2DrC6mY8rY51Y027RjVQuHrWWwV/ efxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=roMHS2o+; 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=fail (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 j142si1493844ybj.379.2020.08.28.10.10.04 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 28 Aug 2020 10:10:04 -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=fail header.i=@linaro.org header.s=google header.b=roMHS2o+; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:54166 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBht5-0004vI-Mv for patch@linaro.org; Fri, 28 Aug 2020 13:10:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36770) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBhot-0003xN-4X for qemu-devel@nongnu.org; Fri, 28 Aug 2020 13:05:43 -0400 Received: from mail-pj1-x1042.google.com ([2607:f8b0:4864:20::1042]:53854) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBhor-00036E-96 for qemu-devel@nongnu.org; Fri, 28 Aug 2020 13:05:42 -0400 Received: by mail-pj1-x1042.google.com with SMTP id nv17so23386pjb.3 for ; Fri, 28 Aug 2020 10:05:40 -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=Z0jSasqYMa/kYWQRBUn0M5RkDRX5OspYiVQvjoGRm10=; b=roMHS2o+qL8y8fbaQz7gA08dLUtzfxbTpBShd8UzaAfxUM5gRXDrYd/FVT68VjffzY XE9t5PHoHhdoKUBitXa5NNiIaTHqn0sbObZ+8jKtakPXq2Ijj/+1dnZgATZ/8hjW7o/6 5uGa1wG23vHd32mgJNvkrz5TpxP14QSxixP5t700uK8XOUjOYk07oXvHQpk875E9nzxS hC6TYckscanY3NiLIQmMKJPy0CjziMuQQRavjGcQMUOf8Ro6o2g8oEVeiR5NQ1nrbwzy LmyTUvFZRy+7wOoPeopoN2aKVIDJkppffdLAxm718Nq9S2ExgfUPZJoa2ig4StaVyWgK qhMg== 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=Z0jSasqYMa/kYWQRBUn0M5RkDRX5OspYiVQvjoGRm10=; b=lqe/R3JwdNQnedav1BUy0y6p37Sw+iIl4Jf/UysSME3r8nB55PC9lbApJnrA27HhKS cmRPWGtSSIeKID9IoJW1lLLBl9/i+xVeQ/gDCLPaxNlZBw/NT7WEIkuuYQ2SdiJ4vhVd P6pTR0nCjGBjdO0TMOgFNrMCpCCvJKUCsIjPfIVInYWSKi0PolnAwb7k6SGVbS6ktzWs GsYh0QNHaeJ4U3Qr2iPv+7aIGEKDkvQXUFtvs/pHYE7TxyhHIaew1s4rNzjBacdHODx7 nHmFa2Q6cPac8U7q/l+Hm/MChcNOly5juE+bmGcoUlSyhSrOZd9L0kbTRfmTQeesU4pi XCIQ== X-Gm-Message-State: AOAM531eOHqTTHeLSWiw8TrcmpYZdJNppwYPI07Hyh9t0vszt+LT4+mQ mM4jPuzSAv/zN1cBYdmxsyj5gIc9rv2ECw== X-Received: by 2002:a17:90a:858a:: with SMTP id m10mr47912pjn.185.1598634339389; Fri, 28 Aug 2020 10:05:39 -0700 (PDT) Received: from localhost.localdomain ([71.212.141.89]) by smtp.gmail.com with ESMTPSA id q12sm2277495pff.196.2020.08.28.10.05.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 10:05:38 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 11/16] crypto/builtin: Merge qcrypto_cipher_aes_{ecb, xts}_{en, de}crypt Date: Fri, 28 Aug 2020 10:05:18 -0700 Message-Id: <20200828170523.418603-12-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::1042; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1042.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: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , berrange@redhat.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" There's no real reason we need two separate helper functions here. Standardize on the function signature required for xts_encrypt. Rename to do_aes_{en,de}crypt_ecb, since the helper does not itself do anything with respect to xts. Reviewed-by: Daniel P. Berrangé Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson --- crypto/cipher-builtin.c.inc | 73 +++++++++++-------------------------- 1 file changed, 22 insertions(+), 51 deletions(-) -- 2.25.1 diff --git a/crypto/cipher-builtin.c.inc b/crypto/cipher-builtin.c.inc index e2ae5d090c..8e21f2673f 100644 --- a/crypto/cipher-builtin.c.inc +++ b/crypto/cipher-builtin.c.inc @@ -72,65 +72,38 @@ static void qcrypto_cipher_free_aes(QCryptoCipher *cipher) g_free(cipher); } - -static void qcrypto_cipher_aes_ecb_encrypt(const AES_KEY *key, - const void *in, - void *out, - size_t len) +static void do_aes_encrypt_ecb(const void *vctx, + size_t len, + uint8_t *out, + const uint8_t *in) { - const uint8_t *inptr = in; - uint8_t *outptr = out; + const QCryptoCipherBuiltinAESContext *ctx = vctx; /* We have already verified that len % AES_BLOCK_SIZE == 0. */ while (len) { - AES_encrypt(inptr, outptr, key); - inptr += AES_BLOCK_SIZE; - outptr += AES_BLOCK_SIZE; + AES_encrypt(in, out, &ctx->enc); + in += AES_BLOCK_SIZE; + out += AES_BLOCK_SIZE; len -= AES_BLOCK_SIZE; } } - -static void qcrypto_cipher_aes_ecb_decrypt(const AES_KEY *key, - const void *in, - void *out, - size_t len) +static void do_aes_decrypt_ecb(const void *vctx, + size_t len, + uint8_t *out, + const uint8_t *in) { - const uint8_t *inptr = in; - uint8_t *outptr = out; + const QCryptoCipherBuiltinAESContext *ctx = vctx; /* We have already verified that len % AES_BLOCK_SIZE == 0. */ while (len) { - AES_decrypt(inptr, outptr, key); - inptr += AES_BLOCK_SIZE; - outptr += AES_BLOCK_SIZE; + AES_decrypt(in, out, &ctx->dec); + in += AES_BLOCK_SIZE; + out += AES_BLOCK_SIZE; len -= AES_BLOCK_SIZE; } } - -static void qcrypto_cipher_aes_xts_encrypt(const void *ctx, - size_t length, - uint8_t *dst, - const uint8_t *src) -{ - const QCryptoCipherBuiltinAESContext *aesctx = ctx; - - qcrypto_cipher_aes_ecb_encrypt(&aesctx->enc, src, dst, length); -} - - -static void qcrypto_cipher_aes_xts_decrypt(const void *ctx, - size_t length, - uint8_t *dst, - const uint8_t *src) -{ - const QCryptoCipherBuiltinAESContext *aesctx = ctx; - - qcrypto_cipher_aes_ecb_decrypt(&aesctx->dec, src, dst, length); -} - - static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher, const void *in, void *out, @@ -142,8 +115,7 @@ static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher, switch (cipher->mode) { case QCRYPTO_CIPHER_MODE_ECB: - qcrypto_cipher_aes_ecb_encrypt(&ctxt->state.aes.key.enc, - in, out, len); + do_aes_encrypt_ecb(&ctxt->state.aes.key, len, out, in); break; case QCRYPTO_CIPHER_MODE_CBC: AES_cbc_encrypt(in, out, len, @@ -153,8 +125,8 @@ static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher, case QCRYPTO_CIPHER_MODE_XTS: xts_encrypt(&ctxt->state.aes.key, &ctxt->state.aes.key_tweak, - qcrypto_cipher_aes_xts_encrypt, - qcrypto_cipher_aes_xts_decrypt, + do_aes_encrypt_ecb, + do_aes_decrypt_ecb, ctxt->state.aes.iv, len, out, in); break; @@ -177,8 +149,7 @@ static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher, switch (cipher->mode) { case QCRYPTO_CIPHER_MODE_ECB: - qcrypto_cipher_aes_ecb_decrypt(&ctxt->state.aes.key.dec, - in, out, len); + do_aes_decrypt_ecb(&ctxt->state.aes.key, len, out, in); break; case QCRYPTO_CIPHER_MODE_CBC: AES_cbc_encrypt(in, out, len, @@ -188,8 +159,8 @@ static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher, case QCRYPTO_CIPHER_MODE_XTS: xts_decrypt(&ctxt->state.aes.key, &ctxt->state.aes.key_tweak, - qcrypto_cipher_aes_xts_encrypt, - qcrypto_cipher_aes_xts_decrypt, + do_aes_encrypt_ecb, + do_aes_decrypt_ecb, ctxt->state.aes.iv, len, out, in); break;