From patchwork Sat Jun 22 19:34:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 167504 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp2256070ilk; Sat, 22 Jun 2019 12:35:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqy6RIZY4VjOyUZWCpYi8gDu/8Xw8eYhpmQFIaXwB+Yd7qSqu0kz95j3OLF4deCE73z1x7PV X-Received: by 2002:a17:902:2a27:: with SMTP id i36mr21271940plb.161.1561232105655; Sat, 22 Jun 2019 12:35:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561232105; cv=none; d=google.com; s=arc-20160816; b=fxdWsqBhiY+rEN3xcs2UL6BASIkcXaTdnTtgQ8GF0VFhtqIcjcxs1yF9vrecvFRl6f IkGsBQVJ3r/gyKTi+5KGOtfq7r+NBSVZatBiaEN9semNhYB/KH6fgumGtdA0wRXAU8eg r4J00634l0m6jhhPoY+qqK0IhWDGBkqQmRF4rY0hysU52XGmv1V1wbys+ZMy7b2u0gBB X9MYQikGGsZL2Ie4QKko20mc6pZVL/31FrXuBnjDQSNjcgUr60YjrXx48qKzNqwKlEGJ FZkc0DNhh+6qERF7sC6omMsNVYyNMD8ZH2hOW8FVyoMR1ITRaC0jLSFyPG8/aK4vgBzj 3p3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=i5YAml7KVGLr/1+CAScarRaj3NSVjbEGTREd9Bd1Ltw=; b=CWGSadrhBl6t4jeVR66S81M9gLc09YIpmXA/zek13lR+FRvTXTDsbnOFWzHYRxLUoz gtmLCb707xMPXp1nVI6nKlAO/fwj3YEjpuzeu7dCzs0UjjXb8uxLxEHPwycLqd1xok/x 9n95XUAJzVdt8jK9DP6tjSJCk5k1RxYmuI9Rq8uT9lOz9jObj48grPG/xcl0f4d4E2Ce cZVxSph2FOMu+Z/2CCI4hx8SkW8cWZlGRGg7nfB6/7oJbYivvtQtAMeuMwdgV4tSqz7w WIQqwxt6QdsXMYLNibMizcH8vJ7bZcnV68wPiOSgOcEhQP1bucBjtD4hPeCn7w3OBjke rKAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MbMFFh0L; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l21si5511699pgb.409.2019.06.22.12.35.05; Sat, 22 Jun 2019 12:35:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MbMFFh0L; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726388AbfFVTfF (ORCPT + 3 others); Sat, 22 Jun 2019 15:35:05 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:34690 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726286AbfFVTfD (ORCPT ); Sat, 22 Jun 2019 15:35:03 -0400 Received: by mail-wm1-f67.google.com with SMTP id w9so11349209wmd.1 for ; Sat, 22 Jun 2019 12:35:02 -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=i5YAml7KVGLr/1+CAScarRaj3NSVjbEGTREd9Bd1Ltw=; b=MbMFFh0LVBuA2Ka3k502n9AtagHKkTgUoB+sYJXNU6ryLYM9VrunnEJz2HHY+Sng7D 1hWgtcFtTSyeYji16HNVHN9hKNBLRs/iGdQdiTjh0xg1ycfiD3ndE5/Q0ko7EhBSoO5f efkGR/9VeB9g1RYM93GUR8DVnAjXR2DbZw06JxXj2epN7C6xlZHhXFa/lo1+1Q5pnvNT oIJDgMqZPBNsAxGGAGghocZRdGi376hUOy9ZrQFJxfXoPB0NZ8bvluP8Z6+fxgXtcBAt uidDQhxIPb7FyidBTE0hS8wrGWAl8BPOadwJFXsg7Kb9eYbbA30o8THue20ceXTSCQ/u wQyQ== 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=i5YAml7KVGLr/1+CAScarRaj3NSVjbEGTREd9Bd1Ltw=; b=o4kgYbz43WaqSdtawcOvf6jv2NsRY/2t6AA71cyZRLTdDP+c6n31vrbZ+quIA1/OWz bVv5rji9hlnaX6hDQiUQO6WaVslglcGXmeVVmTLvjRuJCkwP+/J4Ji2KOE6HREBswVQ7 KpW2hL57lG38Eor6U9Zh853taTwpeHBGL4A1T3IUZMTTlFdcpEfxBU5cC4h/5CZXTl/v tntRWVYHwD0vkdljoJ0vWMtcd/eUh/U3zJD2uKk2d7dE/9t1LQRUHaDGF67MU1Hw+Wce 5C7JayxL02LSTD7RKQ4ttczsoHEutXKSvwFlKOfwCZ1R71mAbyGpnHYCj44y3WD+RgUN klOg== X-Gm-Message-State: APjAAAUH3FxRzWxdvw2mA0O5KrN4Je9Qks4pI/n2KPfdy5tMUrMUMNgd mFQtFiOWYLcjtyOhdDINT2KRzMCx9NuEfPM+ X-Received: by 2002:a7b:cd9a:: with SMTP id y26mr9220661wmj.44.1561232101491; Sat, 22 Jun 2019 12:35:01 -0700 (PDT) Received: from sudo.home ([2a01:cb1d:112:6f00:4bd:3f91:4ef8:ae7e]) by smtp.gmail.com with ESMTPSA id h8sm4814494wmf.12.2019.06.22.12.35.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 22 Jun 2019 12:35:00 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, ebiggers@google.com, Ard Biesheuvel Subject: [PATCH v2 20/26] crypto: arm/aes-ce - provide a synchronous version of ctr(aes) Date: Sat, 22 Jun 2019 21:34:21 +0200 Message-Id: <20190622193427.20336-21-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190622193427.20336-1-ard.biesheuvel@linaro.org> References: <20190622193427.20336-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org AES in CTR mode is used by modes such as GCM and CCM, which are often used in contexts where only synchronous ciphers are permitted. So provide a synchronous version of ctr(aes) based on the existing code. This requires a non-SIMD fallback to deal with invocations occurring from a context where SIMD instructions may not be used. We have a helper for this now in the AES library, so wire that up. Signed-off-by: Ard Biesheuvel --- arch/arm/crypto/aes-ce-glue.c | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) -- 2.20.1 diff --git a/arch/arm/crypto/aes-ce-glue.c b/arch/arm/crypto/aes-ce-glue.c index e6da3e30018b..c3a78c5a5c35 100644 --- a/arch/arm/crypto/aes-ce-glue.c +++ b/arch/arm/crypto/aes-ce-glue.c @@ -10,8 +10,10 @@ #include #include +#include #include #include +#include #include #include #include @@ -289,6 +291,29 @@ static int ctr_encrypt(struct skcipher_request *req) return err; } +static void ctr_encrypt_one(struct crypto_skcipher *tfm, const u8 *src, u8 *dst) +{ + struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); + unsigned long flags; + + /* + * Temporarily disable interrupts to avoid races where + * evicted when the CPU is interrupted to do something + * else. + */ + local_irq_save(flags); + aes_encrypt(ctx, dst, src); + local_irq_restore(flags); +} + +static int ctr_encrypt_sync(struct skcipher_request *req) +{ + if (!crypto_simd_usable()) + return crypto_ctr_encrypt_walk(req, ctr_encrypt_one); + + return ctr_encrypt(req); +} + static int xts_encrypt(struct skcipher_request *req) { struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); @@ -378,6 +403,21 @@ static struct skcipher_alg aes_algs[] = { { .setkey = ce_aes_setkey, .encrypt = ctr_encrypt, .decrypt = ctr_encrypt, +}, { + .base.cra_name = "ctr(aes)", + .base.cra_driver_name = "ctr-aes-ce-sync", + .base.cra_priority = 300 - 1, + .base.cra_blocksize = 1, + .base.cra_ctxsize = sizeof(struct crypto_aes_ctx), + .base.cra_module = THIS_MODULE, + + .min_keysize = AES_MIN_KEY_SIZE, + .max_keysize = AES_MAX_KEY_SIZE, + .ivsize = AES_BLOCK_SIZE, + .chunksize = AES_BLOCK_SIZE, + .setkey = ce_aes_setkey, + .encrypt = ctr_encrypt_sync, + .decrypt = ctr_encrypt_sync, }, { .base.cra_name = "__xts(aes)", .base.cra_driver_name = "__xts-aes-ce", @@ -421,6 +461,9 @@ static int __init aes_init(void) return err; for (i = 0; i < ARRAY_SIZE(aes_algs); i++) { + if (!(aes_algs[i].base.cra_flags & CRYPTO_ALG_INTERNAL)) + continue; + algname = aes_algs[i].base.cra_name + 2; drvname = aes_algs[i].base.cra_driver_name + 2; basename = aes_algs[i].base.cra_driver_name;