From patchwork Thu Aug 13 03:25:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 247659 Delivered-To: patch@linaro.org Received: by 2002:a92:cc90:0:0:0:0:0 with SMTP id x16csp927385ilo; Wed, 12 Aug 2020 20:31:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxoNF3v/yQrKQTMAMsCCbDdzT1SB5k8Kk6ehP3/J42WoNAO2TclS3cDFXY/EeuG9iyGlRgr X-Received: by 2002:a25:c78d:: with SMTP id w135mr3838856ybe.497.1597289470907; Wed, 12 Aug 2020 20:31:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597289470; cv=none; d=google.com; s=arc-20160816; b=jy26TDVho0E7QzkbKmYLLua8NkCUdO2fCRjEkeWhOoxjskKVZTVilVVi0qb+PsR34y neKIvh05T65mpOSOIP6xL4IiiJk98m/YPmRPSsqKkGd2i5Bo206pVO/BUVeZqYXLCrI0 yICFv9lEQLrrI091HpRbPOlguV124yLEqg+dGB1wmsuSUJ9Kg/TvteEfyTZh4jsqenVI +kfCRxMj7NSidqjH9xy/YhDu62v5Lk9guyhSFR4bXtiPPAWJZmSCATQppUoWH7plnUGs HuSKB+ZFXx2aEy3+IJChHR1UCNfmRMkE1Zeyh5vG0Gz7NEvP80jwzNtCV0NOxWOvfCse qZ5g== 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=dJVKjiEkOYni5kS+6oXRQCkue35GHv0VpKcWBZdFBBk=; b=WMZGSet6FOy9UR2jIXn5FzG7w4BXm21IkH+g7IuD6arvPHnu88gw2hGXVGRWQiWR0L NJQ9IAahGjaP3BboULpXQFJ6q4rSX90Xhxe4ajFD3Z5p8+XqJZzVbHBkNEFC1yJNI1rE ll15rSdiIWVgBKGANj+Ccq5inG697gOKoBehQk6/V7BQaeloEy4lprT3upErKwv7svx5 ssFkI2fqu6mSeLrfzA7SiSlhN3ryU7E56Mf+MdsrBWjH+lnmTeXNDN5QN5+Uuu39bTG4 8WWOpa9NCnbOOT8VFJEmHsfoJGIoNi1y8xwkua43cH2ocqQA1B4Fd182E2dX+rJG6eOc XGAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fsFpya0W; 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 y12si3689744ybg.209.2020.08.12.20.31.10 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 12 Aug 2020 20:31:10 -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=fsFpya0W; 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]:37288 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k63xO-000552-9g for patch@linaro.org; Wed, 12 Aug 2020 23:31:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58646) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k63sO-0005EM-D8 for qemu-devel@nongnu.org; Wed, 12 Aug 2020 23:26:00 -0400 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]:35917) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k63sM-0000A2-In for qemu-devel@nongnu.org; Wed, 12 Aug 2020 23:26:00 -0400 Received: by mail-pg1-x52b.google.com with SMTP id p37so2124553pgl.3 for ; Wed, 12 Aug 2020 20:25:58 -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=dJVKjiEkOYni5kS+6oXRQCkue35GHv0VpKcWBZdFBBk=; b=fsFpya0W+7tQF58WN/FjXL1mu4h31IJ/l+AlLL5xRGa1ANaljw9PKxzv1NWJfVi2Db E7/NcjUHIYsFGSVp2zqZVR5+iAYlEKMouTRtC8hDwdh+8NMYxgEooiJOmeeS1VAn5KIo nQlaNImeB+gXpEPCh3rVcc6B0ghDxNWXqgaYmjY5bU9Zpj5M1H2G4QewQEJab5Q/vBfo zNmH5sFV8rBwKFuV0db7DE8ZiQ3ZHmGY+uPDZkNTTrAiM/jPuY3m3xNPXQ217x9PSvEi ziLRL0I/fE0VQcTPWtV0I0i/F0Mr/QRbwA5Tn5jHtDjtKGHX1aKBlXBzwu9Sq6cr5NBD XB+A== 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=dJVKjiEkOYni5kS+6oXRQCkue35GHv0VpKcWBZdFBBk=; b=Yf++E5susFA7mXHtSq3ZsH2gA6RdMlpMYpdhPgGOZ85BdC7Norr+0koW/qYWbVllrr BAjiKlp8Z3v3LrmVvXPIM0nA4uhDvEGNBUYCaLiuv8FHdCPYZFz/ncjatirzMG3HxE8E DD/3PCIIYpqNTU63eVXwc09gtEMHQINICecK8ULQWm4NDroPYP9jptsXqUjai+4Hfy9n 0e8Z8kOOn4E4owG2tRyhsd+fRj+GdlK/NYNLs09escardMtstBIeslG8c17Cxs4zxwg0 jknXKmy9r9M8eXK9FD7ZdQ3F4O1tAgHg4iCfkQiZTUkV7YPtLaiomP1HGBT1fF1C24mo y0dw== X-Gm-Message-State: AOAM532pQUKfDO5dPHqZIgOBMxTKmZLZSoskHcfvRAEnyMnFgYW4djRl H0kzEJSY9j1eUECASzSbZe0Rr9bk5To= X-Received: by 2002:a62:c182:: with SMTP id i124mr2344803pfg.225.1597289156789; Wed, 12 Aug 2020 20:25:56 -0700 (PDT) Received: from localhost.localdomain ([71.212.141.89]) by smtp.gmail.com with ESMTPSA id m19sm3633164pgd.21.2020.08.12.20.25.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 20:25:56 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 13/17] crypto/builtin: Move AES_cbc_encrypt into cipher-builtin.inc.c Date: Wed, 12 Aug 2020 20:25:33 -0700 Message-Id: <20200813032537.2888593-14-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::52b; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52b.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" By making the function private, we will be able to make further simplifications. Re-indent the migrated code and fix the missing braces for CODING_STYLE. Signed-off-by: Richard Henderson --- include/crypto/aes.h | 4 --- crypto/aes.c | 51 --------------------------------- crypto/cipher-builtin.inc.c | 56 +++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 55 deletions(-) -- 2.25.1 Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Daniel P. Berrangé diff --git a/include/crypto/aes.h b/include/crypto/aes.h index 12fb321b89..ba297d6a73 100644 --- a/include/crypto/aes.h +++ b/include/crypto/aes.h @@ -16,7 +16,6 @@ typedef struct aes_key_st AES_KEY; #define AES_set_decrypt_key QEMU_AES_set_decrypt_key #define AES_encrypt QEMU_AES_encrypt #define AES_decrypt QEMU_AES_decrypt -#define AES_cbc_encrypt QEMU_AES_cbc_encrypt int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); @@ -27,9 +26,6 @@ void AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key); -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); extern const uint8_t AES_sbox[256]; extern const uint8_t AES_isbox[256]; diff --git a/crypto/aes.c b/crypto/aes.c index 0f6a195af8..159800df65 100644 --- a/crypto/aes.c +++ b/crypto/aes.c @@ -1599,54 +1599,3 @@ void AES_decrypt(const unsigned char *in, unsigned char *out, } #endif /* AES_ASM */ - -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) -{ - - unsigned long n; - unsigned long len = length; - unsigned char tmp[AES_BLOCK_SIZE]; - - 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); - } - } -} diff --git a/crypto/cipher-builtin.inc.c b/crypto/cipher-builtin.inc.c index 4d971a2b82..416d44b38e 100644 --- a/crypto/cipher-builtin.inc.c +++ b/crypto/cipher-builtin.inc.c @@ -100,6 +100,62 @@ 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) +{ + unsigned long n; + unsigned long len = length; + unsigned char tmp[AES_BLOCK_SIZE]; + + 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); + } + } +} + + static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher, const void *in, void *out,