Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4111908rdb; Thu, 14 Sep 2023 12:05:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGIXHjXjysK4x4F6mn1emmnNpK3YVvTBpf6Xz1rVh/Qu82/THqN0QQjExioLAkUbleL/QdT X-Received: by 2002:a17:902:ed8e:b0:1b0:6e16:b92c with SMTP id e14-20020a170902ed8e00b001b06e16b92cmr6159288plj.54.1694718358445; Thu, 14 Sep 2023 12:05:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694718358; cv=none; d=google.com; s=arc-20160816; b=IwGBX4mGVFmxSXJGsr5qiAPYtgjiHsnmgXLpMwe4OodJYOIot3bDW2DhzJmwN93x3t uqA7tRlZbxNayJ+ZA82rVx+DnJfmloMSyQljCeYyT9DlwhIs43bLQVSCbzc+oTga1+BV oCJQNhTgnGEEgMaIELwmazl9k1+apgI9+8AaoRDrbIiLQG+eay1aspfK84YkNnfYhS0Y pDJv8aXsC5be63To2PpZY79v46pscknTYP1B4HzkfImEY/0iTQou27gFX0P1FEIFSDor zz9nPlAVf7fwsORT47bBduQrwKuhZHfAtxZwcw6ANQFQUhCs+4i/iUJEEJACxskJOTDa WLVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:sender:dkim-signature; bh=+FVWyFAXDAKMnSKDDC7jOyr+ANX3gHPcKvdHW241elE=; fh=okneah7i9WVE7NFkyyxtFud/d0rWX8LR4CpF9Ro7Ixg=; b=TqkAK65xXswllJMevfb2yVIVBpFt5uyQoXdGmgGtqdNbbDXbOzhc6yrPdReduILuJZ C50G/Im4h+bx2ho3z2R6Z38/rhYlV7CDjXJRFiSVNbwldS51LKy6VwTOwmVJ7dbDkF3Q E0hKul72qHeBgb6v4tm0A91WsBGn1LZj/3ZtGX7jwk+kaJty0x/Dg6fqRgWZyf20smVR HGX3b/IEY8o+NpgiWl+rqJsFyYfp46ob6UcY8ybNv4lzJqp477tRd0qQxjA2LcpeMmkg En6IaFdgKV/F25s7vKShKGkz0N38YXbNj1NR2GhLzngSOcod8vAX72/9hZekJQrK3BTr rRbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="pX/0ivHq"; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id ij6-20020a170902ab4600b001bee6def27dsi2078323plb.235.2023.09.14.12.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 12:05:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="pX/0ivHq"; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id C8BDE82B2E6D; Thu, 14 Sep 2023 01:56:57 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236076AbjINI45 (ORCPT + 99 others); Thu, 14 Sep 2023 04:56:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236378AbjINI44 (ORCPT ); Thu, 14 Sep 2023 04:56:56 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DCFC5CF3 for ; Thu, 14 Sep 2023 01:56:51 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-26b44247123so573402a91.2 for ; Thu, 14 Sep 2023 01:56:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694681811; x=1695286611; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:from:to:cc:subject:date:message-id :reply-to; bh=+FVWyFAXDAKMnSKDDC7jOyr+ANX3gHPcKvdHW241elE=; b=pX/0ivHqJhpw+k9Xa8BoFhLNGdh6j0o2sBe4W8nHqyHI8HQrULoOWLWL1gqQyQroCD EgPXLDsgs5nYJSNZ9CE5XaHo5wWfYE2qzy7jHtfr1LlLeD6unqfkHryrbb6j3U+cYEKo 9kK32z/xN4hTWPr1h9TbMb4QJrZVmSkJP04GBYZgK+21SF10b4/rbbOIra0mZdgv3ttU IlmWgJVyoEm4Wne7BFLlG/OcHGVKxIduA5VTY3N5znWjQnSCX6Mva3ADlP0SKU2ezbu8 7DjEgkNk7CNAVnLIlseUaAoYbQy4gjN9FmVUKWWLFZEGJwoBbSBavBZauuq8iTN+dXmF oR+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694681811; x=1695286611; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+FVWyFAXDAKMnSKDDC7jOyr+ANX3gHPcKvdHW241elE=; b=lgO+zems7lQbw+vpWkyE6W9xg7OreA/XPoXa80LC321XxFBqsML9G31z/zAplgSH8r BXUEI9pZAtG/JxTY1tlchXjRNMyrRm+HfeAKCpua8oFmzIJhy6GfacEotfFkRWm6716h JDT2OVxwLtcLP+KtUq2znQgAtVwS5+bamcbk1ml6x84o9tEQE+XutQgYuHg9NJGBQZDV MdPtUuJb2LG4ISO0XfRr3WoAVPbrfi0Q6WKc1KRyePzrY+v+6TlQ38MhFDYy/ktk7CUk tonBASaByLGT0OLyc/DzVwYDsZExYUS8GzCDgNgcwnDzt4QuRc5UCNYHgNyIOE/inSwe 6fkA== X-Gm-Message-State: AOJu0YwTYch12vBUvA809Dgw0E2UBQidBMWfdKUJG1EUVFOJPj/vXsbW bddJrbwmVPS2jFrvJ352Ras= X-Received: by 2002:a17:90b:4c89:b0:26f:4685:5b53 with SMTP id my9-20020a17090b4c8900b0026f46855b53mr5194832pjb.28.1694681811198; Thu, 14 Sep 2023 01:56:51 -0700 (PDT) Received: from gondor.apana.org.au ([2404:c804:1b2a:5507:c00a:8aff:fe00:b003]) by smtp.gmail.com with ESMTPSA id b9-20020a17090a6e0900b0025bfda134ccsm889031pjk.16.2023.09.14.01.56.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 01:56:50 -0700 (PDT) Sender: Herbert Xu Date: Thu, 14 Sep 2023 16:56:50 +0800 From: Herbert Xu To: Ard Biesheuvel Cc: Linux Crypto Mailing List Subject: Re: [PATCH 0/8] crypto: Add lskcipher API type Message-ID: References: <20230914082828.895403-1-herbert@gondor.apana.org.au> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 14 Sep 2023 01:56:58 -0700 (PDT) X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email On Thu, Sep 14, 2023 at 10:51:21AM +0200, Ard Biesheuvel wrote: > > So the intent is for lskcipher to ultimately supplant the current > cipher entirely, right? And lskcipher can be used directly by clients > of the crypto API, in which case kernel VAs may be used directly, but > no async support is available, while skcipher API clients will gain > access to lskciphers via a generic wrapper (if needed?) > > That makes sense but it would help to spell this out. Yes that's the idea. It is pretty much exactly the same as how shash and ahash are handled and used. Because of the way I structured the ecb transition code (it will take an old cipher and repackage it as an lskcipher), we need to convert the templates first and then do the cipher => lskcipher conversion. > I'd be happy to help out here but I'll be off on vacation for ~3 weeks > after this week so i won't get around to it before mid October. What I > will do (if it helps) is rebase my recent RISC-V scalar AES cipher > patches onto this, and implement ecb(aes) instead (which is the idea > IIUC?) That sounds good. In fact let me attach the aes-generic proof- of-concept conversion (it can only be applied after all templates have been converted, so if you test it now everything but ecb/cbc will be broken). Thanks, -- Email: Herbert Xu Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt -- diff --git a/crypto/aes_generic.c b/crypto/aes_generic.c index 666474b81c6a..afb74ee04193 100644 --- a/crypto/aes_generic.c +++ b/crypto/aes_generic.c @@ -47,14 +47,13 @@ * --------------------------------------------------------------------------- */ -#include -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include static inline u8 byte(const u32 x, const unsigned n) { @@ -1123,7 +1122,7 @@ EXPORT_SYMBOL_GPL(crypto_it_tab); /** * crypto_aes_set_key - Set the AES key. - * @tfm: The %crypto_tfm that is used in the context. + * @tfm: The %crypto_lskcipher that is used in the context. * @in_key: The input key. * @key_len: The size of the key. * @@ -1133,10 +1132,10 @@ EXPORT_SYMBOL_GPL(crypto_it_tab); * * Return: 0 on success; -EINVAL on failure (only happens for bad key lengths) */ -int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, - unsigned int key_len) +int crypto_aes_set_key(struct crypto_lskcipher *tfm, const u8 *in_key, + unsigned int key_len) { - struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); + struct crypto_aes_ctx *ctx = crypto_lskcipher_ctx(tfm); return aes_expandkey(ctx, in_key, key_len); } @@ -1173,9 +1172,9 @@ EXPORT_SYMBOL_GPL(crypto_aes_set_key); f_rl(bo, bi, 3, k); \ } while (0) -static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) +static void aes_encrypt_one(struct crypto_lskcipher *tfm, const u8 *in, u8 *out) { - const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); + const struct crypto_aes_ctx *ctx = crypto_lskcipher_ctx(tfm); u32 b0[4], b1[4]; const u32 *kp = ctx->key_enc + 4; const int key_len = ctx->key_length; @@ -1212,6 +1211,17 @@ static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) put_unaligned_le32(b0[3], out + 12); } +static int crypto_aes_encrypt(struct crypto_lskcipher *tfm, const u8 *src, + u8 *dst, unsigned nbytes, u8 *iv, bool final) +{ + const unsigned int bsize = AES_BLOCK_SIZE; + + for (; nbytes >= bsize; src += bsize, dst += bsize, nbytes -= bsize) + aes_encrypt_one(tfm, src, dst); + + return nbytes && final ? -EINVAL : nbytes; +} + /* decrypt a block of text */ #define i_rn(bo, bi, n, k) do { \ @@ -1243,9 +1253,9 @@ static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) i_rl(bo, bi, 3, k); \ } while (0) -static void crypto_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) +static void aes_decrypt_one(struct crypto_lskcipher *tfm, const u8 *in, u8 *out) { - const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm); + const struct crypto_aes_ctx *ctx = crypto_lskcipher_ctx(tfm); u32 b0[4], b1[4]; const int key_len = ctx->key_length; const u32 *kp = ctx->key_dec + 4; @@ -1282,33 +1292,41 @@ static void crypto_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) put_unaligned_le32(b0[3], out + 12); } -static struct crypto_alg aes_alg = { - .cra_name = "aes", - .cra_driver_name = "aes-generic", - .cra_priority = 100, - .cra_flags = CRYPTO_ALG_TYPE_CIPHER, - .cra_blocksize = AES_BLOCK_SIZE, - .cra_ctxsize = sizeof(struct crypto_aes_ctx), - .cra_module = THIS_MODULE, - .cra_u = { - .cipher = { - .cia_min_keysize = AES_MIN_KEY_SIZE, - .cia_max_keysize = AES_MAX_KEY_SIZE, - .cia_setkey = crypto_aes_set_key, - .cia_encrypt = crypto_aes_encrypt, - .cia_decrypt = crypto_aes_decrypt - } - } +static int crypto_aes_decrypt(struct crypto_lskcipher *tfm, const u8 *src, + u8 *dst, unsigned nbytes, u8 *iv, bool final) +{ + const unsigned int bsize = AES_BLOCK_SIZE; + + for (; nbytes >= bsize; src += bsize, dst += bsize, nbytes -= bsize) + aes_decrypt_one(tfm, src, dst); + + return nbytes && final ? -EINVAL : nbytes; +} + +static struct lskcipher_alg aes_alg = { + .co = { + .base.cra_name = "aes", + .base.cra_driver_name = "aes-generic", + .base.cra_priority = 100, + .base.cra_blocksize = AES_BLOCK_SIZE, + .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, + }, + .setkey = crypto_aes_set_key, + .encrypt = crypto_aes_encrypt, + .decrypt = crypto_aes_decrypt, }; static int __init aes_init(void) { - return crypto_register_alg(&aes_alg); + return crypto_register_lskcipher(&aes_alg); } static void __exit aes_fini(void) { - crypto_unregister_alg(&aes_alg); + crypto_unregister_lskcipher(&aes_alg); } subsys_initcall(aes_init); diff --git a/include/crypto/aes.h b/include/crypto/aes.h index 2090729701ab..947109e24360 100644 --- a/include/crypto/aes.h +++ b/include/crypto/aes.h @@ -6,8 +6,9 @@ #ifndef _CRYPTO_AES_H #define _CRYPTO_AES_H +#include +#include #include -#include #define AES_MIN_KEY_SIZE 16 #define AES_MAX_KEY_SIZE 32 @@ -18,6 +19,8 @@ #define AES_MAX_KEYLENGTH (15 * 16) #define AES_MAX_KEYLENGTH_U32 (AES_MAX_KEYLENGTH / sizeof(u32)) +struct crypto_lskcipher; + /* * Please ensure that the first two fields are 16-byte aligned * relative to the start of the structure, i.e., don't move them! @@ -48,8 +51,8 @@ static inline int aes_check_keylen(unsigned int keylen) return 0; } -int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, - unsigned int key_len); +int crypto_aes_set_key(struct crypto_lskcipher *tfm, const u8 *in_key, + unsigned int key_len); /** * aes_expandkey - Expands the AES key as described in FIPS-197