From patchwork Fri Aug 28 17:05:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 248606 Delivered-To: patch@linaro.org Received: by 2002:a92:5b9c:0:0:0:0:0 with SMTP id c28csp1262997ilg; Fri, 28 Aug 2020 10:13:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyuRoGUT1b4P0caObpHnX+Uu4c5ajNuNEJrnGhn0pLyiumNTbVa+52IkRjeQN8pC4MV2/sH X-Received: by 2002:a25:8491:: with SMTP id v17mr3930204ybk.227.1598634794429; Fri, 28 Aug 2020 10:13:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598634794; cv=none; d=google.com; s=arc-20160816; b=MZnfaDHzSNzSg7c+vVBBJ/k7szngDZ1nfYu4UGYwX9AcRKVUaF6p+yBPjnuLhtJ2Is N3iAMCBsO/gtT4YwFIHI4N/jgCGx7FGquuvtkaSvt8dYgKOtq3SaURk+uSxJb4Vs3jKE 7e1u+lLjvZaPvLd9LAek8qH+5s5/RnL6a0rebi2y9QhO/+uUfA8Wso1heQdW3492DnAc Dr3jh7Z0VAsXXuH4Z5yu8v6A3hjLKUkn31S5xqlsmg4XPDb3LbcgiHT+/pFTP/hgUaTE KFP4zI2oQOa47bn8+jIcFnTXtVLVwzpqTeKJAb5E1hqd0d2+Y8dAanlquiinz0ibLmXW Jg7w== 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=9pef8gAf1wsVL1CMsBcENDnjN1Fz2AwxX3zrLli9AqA=; b=T6cE9hsTQOQxECsSXNhHcwC5SQVDrbrZ/h1SsNo2LfyX9clPDyP/MZfVyIQom5Ri1Q dJOaN38KlDMHvOoUBC2AkHWRMA6Hvr/wkLPbyY5cFn73tbOwsW6jMRePp8kxKX5S3gSw ZV66qfVr9DlVAYYeVRgEmRKpPEpJIj3HHGXA+tLaeaCgNRtVtPxmEp7Ib2LP7OG+7yFp LIeDVU/ojrWs5cUvWmxZxBOtIRacWYa6bMgS8i5y4+Ad4bRmd0/rzd9riDVA81bGRrz/ LIkXBcEgCS9U+Z5zM9XVrxh5GIrNZ9cgQ3GodhRsFknnLEPKPZ/1wn/eapdSoHzrJOF9 06jg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Amz+3ytM; 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 5si1536486ybu.1.2020.08.28.10.13.14 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 28 Aug 2020 10:13:14 -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=Amz+3ytM; 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]:37310 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBhw9-00018s-UI for patch@linaro.org; Fri, 28 Aug 2020 13:13:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36790) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBhou-00041Z-O4 for qemu-devel@nongnu.org; Fri, 28 Aug 2020 13:05:44 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]:40480) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBhos-000370-Gz for qemu-devel@nongnu.org; Fri, 28 Aug 2020 13:05:44 -0400 Received: by mail-pf1-x42d.google.com with SMTP id c142so873777pfb.7 for ; Fri, 28 Aug 2020 10:05:42 -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=9pef8gAf1wsVL1CMsBcENDnjN1Fz2AwxX3zrLli9AqA=; b=Amz+3ytMmbpQhWupxfLouxMaZDgJQDIS6kmT8arZH33roOHAHvFzmpunSQiHXyipbr q9hTxtpM89RMO7CLm0a5c03RZpQhaoGI7hpdRfXTQSFk34r9QD6mGO+oIa4F4JcJkQge AyrAYniTVAjMhKTffJAnw33mZTsFERaxkRcVwR+Ttm2nEtKR9TLpp/PbLjaD63isONDY nyiJWGqWlLSuuQ4xBeVaHDMDG7UDfr/eq4LIGt9wnVAEjKD+dCcUrKdXY7VvjbR6XSBE VeB5sQtwJejV7zN4PyksWPN9KnP+DuSv1vHKcSwHtOY2l5FuOPiP2clBGz7wjhlW8sSh Ataw== 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=9pef8gAf1wsVL1CMsBcENDnjN1Fz2AwxX3zrLli9AqA=; b=ARIO0a6EwDcBTkne7Jmflsd3+Cpfn7t2tmBVShR1oDZxxRoH0vCT8oO3h6uysO7F+j EO1PGMjom+qDWS1NACkEJ6MyIROFyRwWPQ3RaRW4QpVLL7y1+tiuD/mjhZaCFbqEVDA0 IzR7nUeJeWsxdwv2PmIx2NGtDe6oM083Px5omADrOXEY6cOw1YwTTOvuQbxVPF1kvatX 3Q4b+vynfUM15ssGjVjxe5H3EVA0yoY71YzdXwHFBtp5gM5NrOUb+1XIX49bssbF84W+ Lrijtpt+z2K+yUX+mCLJTTkk9+XjWdv0hsCVQaKxvAVuImcuIRcYjThjdG8k1ZTHHmNa Q1HA== X-Gm-Message-State: AOAM5309gB5Z5+RlTJwkkbY0Ay7A2fgqtnBgcNEzt5dHuafAVoeKzvSs YpleQ+z8LRM2YBmOSUSDvH1zXYF+pqwasQ== X-Received: by 2002:a63:d918:: with SMTP id r24mr1736363pgg.158.1598634340748; Fri, 28 Aug 2020 10:05:40 -0700 (PDT) Received: from localhost.localdomain ([71.212.141.89]) by smtp.gmail.com with ESMTPSA id q12sm2277495pff.196.2020.08.28.10.05.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 10:05:39 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 12/16] crypto/builtin: Move AES_cbc_encrypt into cipher-builtin.inc.c Date: Fri, 28 Aug 2020 10:05:19 -0700 Message-Id: <20200828170523.418603-13-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::42d; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42d.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" 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. Reviewed-by: Daniel P. Berrangé Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson --- include/crypto/aes.h | 4 --- crypto/aes.c | 51 --------------------------------- crypto/cipher-builtin.c.inc | 56 +++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 55 deletions(-) -- 2.25.1 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.c.inc b/crypto/cipher-builtin.c.inc index 8e21f2673f..61baad265a 100644 --- a/crypto/cipher-builtin.c.inc +++ b/crypto/cipher-builtin.c.inc @@ -104,6 +104,62 @@ 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) +{ + 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,