Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp3697289pxy; Tue, 4 May 2021 08:02:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxuD7OStp1vderwcGJjlDOdhbIlggWR88f8GGFe5JbcqDBx5uydmhgSeddEJeG7vF4ofuph X-Received: by 2002:a17:906:5acd:: with SMTP id x13mr21597579ejs.243.1620140554651; Tue, 04 May 2021 08:02:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620140554; cv=none; d=google.com; s=arc-20160816; b=NgE6LH+q0lKWne2h0pJ/BVY1TAv7h1h1xx8gnG+V4zBCDXlNDjLNCtbqXihk9/EsBR 4aUN0rS9ihBJOu/qAixnOROdg7h29G7xgHdJI3qoYomKSJiRN6OWyxy7FHFjBPl39a3w s9gR5w7AK4g/htJXf0UkA4sEGmsHGrM1yk2DpjS6BpiTHLmhKzN6FKJZjj+lBT0iOoEH JepW7OMCswHyCT6+oNzerCIN2s4C4MqL0TKE7sJWPTrjwVQoUc04/tvQCrlEgcVDOkvC CQX8dDh2fDW1j0KhHuWTEUkRHhVzi/XlvGN69aAjSoAbOluG209Bhg7G0x0uTdGSvCNj I4Nw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JMYW12LTBZO0IZcSGTCQhCiS4Zcg5+/BzYNJVHEcYMM=; b=CsEvReMvVeOk9YGQBqCsnsyw5nEEYJXmBD7UAKvEg2qrXeItW3xitCmMpVWPzzoYzV OVb+crIcMr16ukdTK00MUHqbgNaf6zoS8RCMK+vpYFrpdGV1a42sNEw/XuDVGX2fjcMZ xMRvXDHtqfWOvdDiIVtBGfzEObcZn22UpC+z/TDfE8YRYvX0UthBg9MASK2aQYWfFBS4 jSQr/gLaBjK6M+DsSvQOZ8ZnAlRtad0SdtXgRnAPVZo+X8ppQXNt8UEuEYuh8coAjYGN MSDI7LavUMC1iewS+iBCIkCnMETSDSGF8iKHzAJZzSJ4x1W13WOqvGYyFm00TYW2OJgJ qmNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=OcjDrj83; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p15si2737437ejj.88.2021.05.04.08.02.10; Tue, 04 May 2021 08:02:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=OcjDrj83; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230507AbhEDNOA (ORCPT + 99 others); Tue, 4 May 2021 09:14:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230373AbhEDNN7 (ORCPT ); Tue, 4 May 2021 09:13:59 -0400 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43DECC061574 for ; Tue, 4 May 2021 06:13:04 -0700 (PDT) Received: by mail-ed1-x52f.google.com with SMTP id b17so7348883ede.0 for ; Tue, 04 May 2021 06:13:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JMYW12LTBZO0IZcSGTCQhCiS4Zcg5+/BzYNJVHEcYMM=; b=OcjDrj83vTKIVAy3nKDoO5yJN2qKlxPBgOdZI7aZZT2YRLeg7t6BkpCrkmg15lDwcL //WTvN6tiND6F5RCLbORHQTLU9x3hw6rkgAxlYcAwiL2GpSL6IRIjFUTdKSqk20YMFHJ ysw3EJgBMGeURdhzgGdxXZsiIxoCJedAj583G7WutsCZl0us/RhY7dWCakH8jWa1JW59 V/B+77BywC4F+yn+JKCXmyvn2qQ4TYVzhz9HzqD9M3i21Bsrpndb3rWLdSPEMo7WjaHW o2rVauACU/lhHKtXPfRYQPSkykqRm+nvEqvhXdxdmOHnBKMIqlO8u16LKO60245QpcGO lzJQ== 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=JMYW12LTBZO0IZcSGTCQhCiS4Zcg5+/BzYNJVHEcYMM=; b=sph4KoZaX5nXKkq/t/iTHeJJwNz/EUrXDrUuwfkEQFaQLqYBBEWnWnFFAkkQ5DB7+b wIqmYeH6C8pthliTK/Mgfwts3wP9kxDuKFaLCC2EPcTntgUZDknmVjEXwElcvxsqjCZ+ wn7PHzeWb0TO2u64LLZd1Sj8EGTyKHeMeCwXIBi9ZVz/C1TqVZKZId/jMFG1QHUjjTMN LHROVqoCrgkIhUM4p5w1MrSRoAS0otV3lNCoEeY3I0yz04K0Byxtu9eoMJxILXX0YgIx OAOvUQmHSnoMa4Yzm4LWo3ZHYb6cpdOr5oR7AxMjImd64o6SA1pAQWH4xFbMo/W82iwq wW/Q== X-Gm-Message-State: AOAM5308wvNSn0rEmAhAogrtPP00qtPdF9OZbW1sKtie0FBYtZpt9Kbu VRgJbQhckOI8IeTGwCZqKM7w9lHs408TJA== X-Received: by 2002:a05:6402:50c6:: with SMTP id h6mr9919699edb.327.1620133982619; Tue, 04 May 2021 06:13:02 -0700 (PDT) Received: from agape ([5.171.72.210]) by smtp.gmail.com with ESMTPSA id h23sm1359628ejx.90.2021.05.04.06.13.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 May 2021 06:13:02 -0700 (PDT) From: Fabio Aiuto To: gregkh@linuxfoundation.org Cc: linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] staging: rtl8723bs: use in-kernel aes encryption in OMAC1 routines Date: Tue, 4 May 2021 15:12:56 +0200 Message-Id: <8c1c680a6498045746ca579e4caa3c87d13b3b49.1620133620.git.fabioaiuto83@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org replace private aes encryption subroutines with public in-kernel ones in OMAC1 computation routines. Signed-off-by: Fabio Aiuto --- drivers/staging/rtl8723bs/core/rtw_security.c | 113 ++---------------- .../staging/rtl8723bs/include/rtw_security.h | 1 + 2 files changed, 9 insertions(+), 105 deletions(-) diff --git a/drivers/staging/rtl8723bs/core/rtw_security.c b/drivers/staging/rtl8723bs/core/rtw_security.c index 0cb603aa4ee6..4f82dc9a3642 100644 --- a/drivers/staging/rtl8723bs/core/rtw_security.c +++ b/drivers/staging/rtl8723bs/core/rtw_security.c @@ -1931,99 +1931,6 @@ const u8 rcons[] = { /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ }; -/* - * Expand the cipher key into the encryption key schedule. - * - * @return the number of rounds for the given cipher key size. - */ -static void rijndaelKeySetupEnc(u32 rk[/*44*/], const u8 cipherKey[]) -{ - int i; - u32 temp; - - rk[0] = GETU32(cipherKey); - rk[1] = GETU32(cipherKey + 4); - rk[2] = GETU32(cipherKey + 8); - rk[3] = GETU32(cipherKey + 12); - for (i = 0; i < 10; i++) { - temp = rk[3]; - rk[4] = rk[0] ^ - TE421(temp) ^ TE432(temp) ^ TE443(temp) ^ TE414(temp) ^ - RCON(i); - rk[5] = rk[1] ^ rk[4]; - rk[6] = rk[2] ^ rk[5]; - rk[7] = rk[3] ^ rk[6]; - rk += 4; - } -} - -static void rijndaelEncrypt(u32 rk[/*44*/], u8 pt[16], u8 ct[16]) -{ - u32 s0, s1, s2, s3, t0, t1, t2, t3; - int Nr = 10; - int r; - - /* - * map byte array block to cipher state - * and add initial round key: - */ - s0 = GETU32(pt) ^ rk[0]; - s1 = GETU32(pt + 4) ^ rk[1]; - s2 = GETU32(pt + 8) ^ rk[2]; - s3 = GETU32(pt + 12) ^ rk[3]; - -#define ROUND(i, d, s) \ - do { \ - d##0 = TE0(s##0) ^ TE1(s##1) ^ TE2(s##2) ^ TE3(s##3) ^ rk[4 * i]; \ - d##1 = TE0(s##1) ^ TE1(s##2) ^ TE2(s##3) ^ TE3(s##0) ^ rk[4 * i + 1]; \ - d##2 = TE0(s##2) ^ TE1(s##3) ^ TE2(s##0) ^ TE3(s##1) ^ rk[4 * i + 2]; \ - d##3 = TE0(s##3) ^ TE1(s##0) ^ TE2(s##1) ^ TE3(s##2) ^ rk[4 * i + 3]; \ - } while (0) - - /* Nr - 1 full rounds: */ - r = Nr >> 1; - for (;;) { - ROUND(1, t, s); - rk += 8; - if (--r == 0) - break; - ROUND(0, s, t); - } - -#undef ROUND - - /* - * apply last round and - * map cipher state to byte array block: - */ - s0 = TE41(t0) ^ TE42(t1) ^ TE43(t2) ^ TE44(t3) ^ rk[0]; - PUTU32(ct, s0); - s1 = TE41(t1) ^ TE42(t2) ^ TE43(t3) ^ TE44(t0) ^ rk[1]; - PUTU32(ct + 4, s1); - s2 = TE41(t2) ^ TE42(t3) ^ TE43(t0) ^ TE44(t1) ^ rk[2]; - PUTU32(ct + 8, s2); - s3 = TE41(t3) ^ TE42(t0) ^ TE43(t1) ^ TE44(t2) ^ rk[3]; - PUTU32(ct + 12, s3); -} - -static void *aes_encrypt_init(u8 *key, size_t len) -{ - u32 *rk; - - if (len != 16) - return NULL; - rk = rtw_malloc(AES_PRIV_SIZE); - if (rk == NULL) - return NULL; - rijndaelKeySetupEnc(rk, key); - return rk; -} - -static void aes_128_encrypt(void *ctx, u8 *plain, u8 *crypt) -{ - rijndaelEncrypt(ctx, plain, crypt); -} - static void gf_mulx(u8 *pad) { int i, carry; @@ -2037,11 +1944,6 @@ static void gf_mulx(u8 *pad) pad[AES_BLOCK_SIZE - 1] ^= 0x87; } -static void aes_encrypt_deinit(void *ctx) -{ - kfree_sensitive(ctx); -} - /** * omac1_aes_128_vector - One-Key CBC MAC (OMAC1) hash with AES-128 * @key: 128-bit key for the hash operation @@ -2058,13 +1960,14 @@ static void aes_encrypt_deinit(void *ctx) static int omac1_aes_128_vector(u8 *key, size_t num_elem, u8 *addr[], size_t *len, u8 *mac) { - void *ctx; + struct crypto_aes_ctx ctx; u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE]; u8 *pos, *end; size_t i, e, left, total_len; + int ret; - ctx = aes_encrypt_init(key, 16); - if (ctx == NULL) + ret = aes_expandkey(&ctx, key, 16); + if (ret) return -1; memset(cbc, 0, AES_BLOCK_SIZE); @@ -2087,12 +1990,12 @@ static int omac1_aes_128_vector(u8 *key, size_t num_elem, } } if (left > AES_BLOCK_SIZE) - aes_128_encrypt(ctx, cbc, cbc); + aes_encrypt(&ctx, cbc, cbc); left -= AES_BLOCK_SIZE; } memset(pad, 0, AES_BLOCK_SIZE); - aes_128_encrypt(ctx, pad, pad); + aes_encrypt(&ctx, pad, pad); gf_mulx(pad); if (left || total_len == 0) { @@ -2110,8 +2013,8 @@ static int omac1_aes_128_vector(u8 *key, size_t num_elem, for (i = 0; i < AES_BLOCK_SIZE; i++) pad[i] ^= cbc[i]; - aes_128_encrypt(ctx, pad, mac); - aes_encrypt_deinit(ctx); + aes_encrypt(&ctx, pad, mac); + memzero_explicit(&ctx, sizeof(ctx)); return 0; } diff --git a/drivers/staging/rtl8723bs/include/rtw_security.h b/drivers/staging/rtl8723bs/include/rtw_security.h index 5c787e999aab..032e520317e7 100644 --- a/drivers/staging/rtl8723bs/include/rtw_security.h +++ b/drivers/staging/rtl8723bs/include/rtw_security.h @@ -7,6 +7,7 @@ #ifndef __RTW_SECURITY_H_ #define __RTW_SECURITY_H_ +#include #define _NO_PRIVACY_ 0x0 #define _WEP40_ 0x1 -- 2.20.1