From patchwork Thu Aug 13 03:25:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 247661 Delivered-To: patch@linaro.org Received: by 2002:a92:cc90:0:0:0:0:0 with SMTP id x16csp928325ilo; Wed, 12 Aug 2020 20:32:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwuLJtz0+jNHoa0FdTQmfon/p015JbLX25C2DHipoeTvURWzLDEIVyMjyuVBHmXlXcjoFs+ X-Received: by 2002:a25:70c1:: with SMTP id l184mr3607859ybc.87.1597289568806; Wed, 12 Aug 2020 20:32:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597289568; cv=none; d=google.com; s=arc-20160816; b=iyoqGWyylrGCGX25Kc8LqI5aBhYqtupCnZo4msdN2WkmIpvXRvAjF4qVfrj7dY6eYZ UJngSaQHnHYOQ/OxWZkK/wPwautoTIxEznD7ja2c/IH8HHTiYu03xqJGNS8DAdL2p30O a+j5Ui0E9TRb9P0OJ3U2w5jqjiJFXosQ0yrABsuEgUiz/ESaZmgHHsuUM4h3zP7g02hg 3U92XMWcSqblMWBIBXerZBy+/b1i0iI18/kDsBGE6Xakh+OPfZuanmlnUCJGPA1HAhXR IhfYqdsSpXPsEB+jYqn500kQ5b/E2FtSJ+1ovummQscnLduFJSGuitz37Y/Egd0gYxiz RIEQ== 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=XfDBqVtcFp4u69HfrPx2aYbk0EB30OybSYsl3oZkJuM=; b=ZHtyrvsCu5dhoAx0bDd5LPm8xf1FMJ73n/b3ddPxHtL3X6XzGvn5mA93LE1KdF6B3K vGWMG52kZ+tbV3u4U5spAKpmRle7CP/lxgwgVE+Px7MusnZIA3dEGDRULEZZpzNVazhe xvnBYtlHMbGCcWdOBSdXG0I+RkVR4lWnb8BIhlmI0gtiYiZ4cDNKeAHojVlZaKv6JTbp ZfhiA8qQT/cbfGMs6+whL0O28Swnz3QFeGcD4t8ZNVAVvieoFqh0TKefaK2yJfeMeagG Mavj2KI8QXryhIzBw56u3LJj5ELIIKAZ5Ggw2/LMN9do2oS8ej4Fi0ZQ+V32DpPLBMSE dIgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BCg5WWAY; 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 m77si4356345ybm.486.2020.08.12.20.32.48 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 12 Aug 2020 20:32:48 -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=BCg5WWAY; 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]:42624 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k63yy-0007Ey-9L for patch@linaro.org; Wed, 12 Aug 2020 23:32:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58678) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k63sQ-0005IM-VK for qemu-devel@nongnu.org; Wed, 12 Aug 2020 23:26:02 -0400 Received: from mail-pg1-x543.google.com ([2607:f8b0:4864:20::543]:40197) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k63sO-0000AE-1R for qemu-devel@nongnu.org; Wed, 12 Aug 2020 23:26:01 -0400 Received: by mail-pg1-x543.google.com with SMTP id h12so2114949pgm.7 for ; Wed, 12 Aug 2020 20:25:59 -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=XfDBqVtcFp4u69HfrPx2aYbk0EB30OybSYsl3oZkJuM=; b=BCg5WWAYUJo/5l2JLBQpDtWdfMAusmXw+9wpC/wXOIbC3IOETdp/nPNe6WbdxMj7dd Cg14wYAHL4XVLRF8uuOfhOc0RUFeVJpyGqFxfS8erzXM1RXtzY5eKzh3Heq3YH68X7KP fKakJHd9x0j260R8P/P2/IYM5CFZypOYeLyCcpjjxZe+DGLX+VhRCCL86kL6fEfcatW5 q19d6FWx8vmD83V6SHIicqB6SdvCnGSjsVmk0m41OuxyQFxMMQUBODD2vZqItT26KKib IJwQeJGWgTKMKOLPU9u1iX1N23HkkjsLgDL3iGJ2dhenlddpiZKjEH9d8cog67Pop8ya IaVg== 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=XfDBqVtcFp4u69HfrPx2aYbk0EB30OybSYsl3oZkJuM=; b=E6nGJkacJU5g86dwbJfOTJD9+441V+Nd4gHTJnHCMfbTvVnukSUEkB8shdBO9ipZFp /9T/429IHdKt/cQDwxK9y2kmfw1yAiZvs5miyhdqsNkSADkzKAhpjApKX01mSWQtixJF 3N7yZtd9691FClELO8JxINjm0nEVGtmVGUQSqLJqOXyMACKR8s0MtibbGizqdWxMBlH7 CWtzEyWNiniai9lRnOL34gPYmvP75bUKFMbcHV+j6kECcooIXpc5btnguYXjKfjPEcV7 hXoCU8iCJPoA/efIQc7eclEySNu/g2/r1jxq6jJVKJUc9Yt/2ICTSyzsUim0N8URU8/D e50A== X-Gm-Message-State: AOAM531Q57pYFLN72GL6yU699t80P4Gk4uTQJl51g2LeDWeqZvigQTfl NR0v05W26QJb5JTUioiZ52naeZOzj70= X-Received: by 2002:a05:6a00:7c8:: with SMTP id n8mr2455089pfu.123.1597289158079; Wed, 12 Aug 2020 20:25:58 -0700 (PDT) Received: from localhost.localdomain ([71.212.141.89]) by smtp.gmail.com with ESMTPSA id m19sm3633164pgd.21.2020.08.12.20.25.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 20:25:57 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 14/17] crypto/builtin: Split and simplify AES_encrypt_cbc Date: Wed, 12 Aug 2020 20:25:34 -0700 Message-Id: <20200813032537.2888593-15-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::543; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x543.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" Split into encrypt/decrypt functions, dropping the "enc" argument. Now that the function is private to this file, we know that "len" is a multiple of AES_BLOCK_SIZE. So drop the odd block size code. Name the functions do_aes_*crypt_cbc to match the *_ecb functions. Reorder and re-type the arguments to match as well. Signed-off-by: Richard Henderson --- crypto/cipher-builtin.inc.c | 91 +++++++++++++++---------------------- 1 file changed, 36 insertions(+), 55 deletions(-) -- 2.25.1 Reviewed-by: Daniel P. Berrangé diff --git a/crypto/cipher-builtin.inc.c b/crypto/cipher-builtin.inc.c index 416d44b38e..4295f93af5 100644 --- a/crypto/cipher-builtin.inc.c +++ b/crypto/cipher-builtin.inc.c @@ -100,61 +100,44 @@ static void do_aes_decrypt_ecb(const void *vctx, size_t len, } } -static void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, - const unsigned long length, const AES_KEY *key, - unsigned char *ivec, const int enc) +static void do_aes_encrypt_cbc(const AES_KEY *key, size_t len, uint8_t *out, + const uint8_t *in, uint8_t *ivec) { - unsigned long n; - unsigned long len = length; - unsigned char tmp[AES_BLOCK_SIZE]; + uint8_t tmp[AES_BLOCK_SIZE]; + size_t n; - assert(in && out && key && ivec); - - if (enc) { - while (len >= AES_BLOCK_SIZE) { - for (n = 0; n < AES_BLOCK_SIZE; ++n) { - tmp[n] = in[n] ^ ivec[n]; - } - AES_encrypt(tmp, out, key); - memcpy(ivec, out, AES_BLOCK_SIZE); - len -= AES_BLOCK_SIZE; - in += AES_BLOCK_SIZE; - out += AES_BLOCK_SIZE; - } - if (len) { - for (n = 0; n < len; ++n) { - tmp[n] = in[n] ^ ivec[n]; - } - for (n = len; n < AES_BLOCK_SIZE; ++n) { - tmp[n] = ivec[n]; - } - AES_encrypt(tmp, tmp, key); - memcpy(out, tmp, AES_BLOCK_SIZE); - memcpy(ivec, tmp, AES_BLOCK_SIZE); - } - } else { - while (len >= AES_BLOCK_SIZE) { - memcpy(tmp, in, AES_BLOCK_SIZE); - AES_decrypt(in, out, key); - for (n = 0; n < AES_BLOCK_SIZE; ++n) { - out[n] ^= ivec[n]; - } - memcpy(ivec, tmp, AES_BLOCK_SIZE); - len -= AES_BLOCK_SIZE; - in += AES_BLOCK_SIZE; - out += AES_BLOCK_SIZE; - } - if (len) { - memcpy(tmp, in, AES_BLOCK_SIZE); - AES_decrypt(tmp, tmp, key); - for (n = 0; n < len; ++n) { - out[n] = tmp[n] ^ ivec[n]; - } - memcpy(ivec, tmp, AES_BLOCK_SIZE); + /* We have already verified that len % AES_BLOCK_SIZE == 0. */ + while (len) { + for (n = 0; n < AES_BLOCK_SIZE; ++n) { + tmp[n] = in[n] ^ ivec[n]; } + AES_encrypt(tmp, out, key); + memcpy(ivec, out, AES_BLOCK_SIZE); + len -= AES_BLOCK_SIZE; + in += AES_BLOCK_SIZE; + out += AES_BLOCK_SIZE; } } +static void do_aes_decrypt_cbc(const AES_KEY *key, size_t len, uint8_t *out, + const uint8_t *in, uint8_t *ivec) +{ + uint8_t tmp[AES_BLOCK_SIZE]; + size_t n; + + /* We have already verified that len % AES_BLOCK_SIZE == 0. */ + while (len) { + memcpy(tmp, in, AES_BLOCK_SIZE); + AES_decrypt(in, out, key); + for (n = 0; n < AES_BLOCK_SIZE; ++n) { + out[n] ^= ivec[n]; + } + memcpy(ivec, tmp, AES_BLOCK_SIZE); + len -= AES_BLOCK_SIZE; + in += AES_BLOCK_SIZE; + out += AES_BLOCK_SIZE; + } +} static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher, const void *in, @@ -170,9 +153,8 @@ static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher, do_aes_encrypt_ecb(&ctxt->state.aes.key, len, out, in); break; case QCRYPTO_CIPHER_MODE_CBC: - AES_cbc_encrypt(in, out, len, - &ctxt->state.aes.key.enc, - ctxt->state.aes.iv, 1); + do_aes_encrypt_cbc(&ctxt->state.aes.key.enc, len, out, in, + ctxt->state.aes.iv); break; case QCRYPTO_CIPHER_MODE_XTS: xts_encrypt(&ctxt->state.aes.key, @@ -204,9 +186,8 @@ static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher, do_aes_decrypt_ecb(&ctxt->state.aes.key, len, out, in); break; case QCRYPTO_CIPHER_MODE_CBC: - AES_cbc_encrypt(in, out, len, - &ctxt->state.aes.key.dec, - ctxt->state.aes.iv, 0); + do_aes_decrypt_cbc(&ctxt->state.aes.key.dec, len, out, in, + ctxt->state.aes.iv); break; case QCRYPTO_CIPHER_MODE_XTS: xts_decrypt(&ctxt->state.aes.key,