From patchwork Fri Aug 28 17:05:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 248601 Delivered-To: patch@linaro.org Received: by 2002:a92:5b9c:0:0:0:0:0 with SMTP id c28csp1259931ilg; Fri, 28 Aug 2020 10:09:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyMIVt4qD6Oba5LN67PMSq0oxXfvF2I8B2StLlpgnOv/8TSB9SpBGE1K14l2DPaqap2Zzg X-Received: by 2002:a25:843:: with SMTP id 64mr3613948ybi.311.1598634575106; Fri, 28 Aug 2020 10:09:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598634575; cv=none; d=google.com; s=arc-20160816; b=rocD+lG02zm7mFb/4AiTGBN8GjbMBKFmQOgEhtc1nf76pTYUIgG4JMB3H5hNLOIJ7n vKV+r7TAApHtUmvqQUmkjrcn/2nKzKCJmfT5hwG2bukmTcjzNDxigD9XXnOrngyKU1fA w4HpgWnQDjH495fraWi76WMnbajWjb1CeBHqVVQ/uALM+DzdEDQYpw8+UtjiJg8a0Fc1 izHKrT4I1vN21TluYqQ8VInH4UZ+hXHM/3IvAPJROpul875VvdIdo/4vp2o+Aif/Tj8l kpSrmP90DBA5+NhEynTMp1vQ9c6bon4U0BWMyYtz3nJZ4ZoJwvi3XKK/DZZhZ3QB4HpF huBA== 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=R08qzgHyL/bf1N6eFfMci3b2adBB+mTS2SLfm7s3DVE=; b=R4j5eYLrBvpwE8HvTtG9qLUUSCqLl1fWBckoBJEKGgc3/o3kJU9aPPRmADrH1M82le j+WYRhBLhiCvncJ+x9jY7bbyJmSLyHAgoaVbgsnyMdylsKtiUDwfbyfisVs9TxDKUEGp iNz4Aw2ryJSt3pSETlYIflYiAKJaNSkkUjUNACi3AyisaolV+/N67Zht38pCjoI5Zmhk Eu4NNCB7lRRzKPWVtgh5yOLETOG4DlzHndp8Y2OccPraktiIIumMxKSWGx/ZrObGrD9Q ET/kb/+oOIbUcdWAjBjbIR5VWHEQE9n8HxvPvQIgEo/lk1t/jEDG7vB8mC8xjPPnMxTZ vYwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=s8U035LO; 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 w2si1611585ybe.261.2020.08.28.10.09.35 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 28 Aug 2020 10:09:35 -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=s8U035LO; 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]:49646 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBhsc-00036i-JJ for patch@linaro.org; Fri, 28 Aug 2020 13:09:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36798) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBhov-000446-Kv for qemu-devel@nongnu.org; Fri, 28 Aug 2020 13:05:45 -0400 Received: from mail-pj1-x1041.google.com ([2607:f8b0:4864:20::1041]:35125) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBhot-00037K-NB for qemu-devel@nongnu.org; Fri, 28 Aug 2020 13:05:45 -0400 Received: by mail-pj1-x1041.google.com with SMTP id g6so31868pjl.0 for ; Fri, 28 Aug 2020 10:05:43 -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=R08qzgHyL/bf1N6eFfMci3b2adBB+mTS2SLfm7s3DVE=; b=s8U035LOyXRw5MhJdmmyVpJUu5OJ6CULXwcL6utmvGyD2v3+TJ3m4LzOthcQPqBKV6 uV4rD08GqEZGISDJ3kD+4pgCYgLsEEGDcLq9lA77GGjxc0QnH3v4a2QcmObP3ab6EboH NvA4n5YOgtzNZfGvNrkrKWqTO28CAbcoJ3tlu7GlfgLvxYlZjqRsWAjubjTwxQuUr9v9 frRpJaWXIByrBQyup4RM3ic/0Y+aXcOJTRzm+I7mFI9sDm5Iz0pWWhnJvQkKTf1Ny4BM GZUxRe2vJ/j6JJ7WDE0+PesI3Z8mx3N6AujoF8zqw395cNVZTtjrWJ4ptiemRgcmwSq+ e9Og== 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=R08qzgHyL/bf1N6eFfMci3b2adBB+mTS2SLfm7s3DVE=; b=QNNRJ4CQMo85T9jsnRHCUkbVI0C77ft+AWZiURf/s2yLJyGQiyxohRWO2m6HatM/W0 X3yAqevC0iVxhP/j+GRk+ftXh/t0drLrBOWV/S85OvX0wsVfEVKkm11ZnkkUIQlsaxQr sTCAuhRQufRTc8986vzOseHznmN+79e4aNxg1acWZxrcj5dnbSUVacoQhQu4cEaWX/4i yoYtIb/Jg3FCThtfNvMCn80NoHbxcy4IfgUEJGhAPgWAUoMJLTy/jhC7QXu7vGqVrhAz SwKxH2eiNWdMV76pDSIrPsMBF78xqVDlTuBDp5vIz7hkvEfudEsphGyFOmFR3tp8403J YheA== X-Gm-Message-State: AOAM531swGGAshhYEsUyMlNdQc2Ug3pY+GejiXuQpkoV+nucIARqdpqN CX+6/5ZuvrSVKNuPKMadx4Z2jPWuSFuq4g== X-Received: by 2002:a17:90a:9915:: with SMTP id b21mr58544pjp.109.1598634341917; Fri, 28 Aug 2020 10:05:41 -0700 (PDT) Received: from localhost.localdomain ([71.212.141.89]) by smtp.gmail.com with ESMTPSA id q12sm2277495pff.196.2020.08.28.10.05.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 10:05:41 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 13/16] crypto/builtin: Split and simplify AES_encrypt_cbc Date: Fri, 28 Aug 2020 10:05:20 -0700 Message-Id: <20200828170523.418603-14-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::1041; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1041.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. Reviewed-by: Daniel P. Berrangé Signed-off-by: Richard Henderson --- crypto/cipher-builtin.c.inc | 97 ++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 55 deletions(-) -- 2.25.1 diff --git a/crypto/cipher-builtin.c.inc b/crypto/cipher-builtin.c.inc index 61baad265a..b1fe3b08c3 100644 --- a/crypto/cipher-builtin.c.inc +++ b/crypto/cipher-builtin.c.inc @@ -104,61 +104,50 @@ static void do_aes_decrypt_ecb(const void *vctx, } } -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, @@ -174,9 +163,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, @@ -208,9 +196,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,